using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Controller.MediaInfo;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Weather;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Controller
{
///
/// Class Kernel
///
public class Kernel
{
///
/// Gets the instance.
///
/// The instance.
public static Kernel Instance { get; private set; }
///
/// Gets the image manager.
///
/// The image manager.
public ImageManager ImageManager { get; set; }
///
/// Gets the FFMPEG controller.
///
/// The FFMPEG controller.
public FFMpegManager FFMpegManager { get; set; }
///
/// Gets the name of the web application that can be used for url building.
/// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
///
/// The name of the web application.
public string WebApplicationName
{
get { return "mediabrowser"; }
}
///
/// Gets the HTTP server URL prefix.
///
/// The HTTP server URL prefix.
public virtual string HttpServerUrlPrefix
{
get
{
return "http://+:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
}
}
///
/// Gets the list of Localized string files
///
/// The string files.
public IEnumerable StringFiles { get; set; }
///
/// Gets the list of currently registered weather prvoiders
///
/// The weather providers.
public IEnumerable WeatherProviders { get; set; }
///
/// Gets the list of currently registered image processors
/// Image processors are specialized metadata providers that run after the normal ones
///
/// The image enhancers.
public IEnumerable ImageEnhancers { get; set; }
///
/// Gets the list of available user repositories
///
/// The user repositories.
public IEnumerable UserRepositories { get; set; }
///
/// Gets the active user repository
///
/// The user repository.
public IUserRepository UserRepository { get; set; }
///
/// Gets the active user repository
///
/// The display preferences repository.
public IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
///
/// Gets the list of available item repositories
///
/// The item repositories.
public IEnumerable ItemRepositories { get; set; }
///
/// Gets the active item repository
///
/// The item repository.
public IItemRepository ItemRepository { get; set; }
///
/// Gets the list of available DisplayPreferencesRepositories
///
/// The display preferences repositories.
public IEnumerable DisplayPreferencesRepositories { get; set; }
///
/// Gets the list of available item repositories
///
/// The user data repositories.
public IEnumerable UserDataRepositories { get; set; }
///
/// Gets the active user data repository
///
/// The user data repository.
public IUserDataRepository UserDataRepository { get; set; }
private readonly IServerConfigurationManager _configurationManager;
///
/// Creates a kernel based on a Data path, which is akin to our current programdata path
///
/// The configuration manager.
public Kernel(IServerConfigurationManager configurationManager)
{
Instance = this;
_configurationManager = configurationManager;
}
///
/// Called when [composable parts loaded].
///
/// Task.
public Task LoadRepositories(IServerConfigurationManager configurationManager)
{
// Get the current item repository
ItemRepository = GetRepository(ItemRepositories, configurationManager.Configuration.ItemRepository);
var itemRepoTask = ItemRepository.Initialize();
// Get the current user repository
UserRepository = GetRepository(UserRepositories, configurationManager.Configuration.UserRepository);
var userRepoTask = UserRepository.Initialize();
// Get the current item repository
UserDataRepository = GetRepository(UserDataRepositories, configurationManager.Configuration.UserDataRepository);
var userDataRepoTask = UserDataRepository.Initialize();
// Get the current display preferences repository
DisplayPreferencesRepository = GetRepository(DisplayPreferencesRepositories, configurationManager.Configuration.DisplayPreferencesRepository);
var displayPreferencesRepoTask = DisplayPreferencesRepository.Initialize();
return Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask, displayPreferencesRepoTask);
}
///
/// Gets a repository by name from a list, and returns the default if not found
///
///
/// The repositories.
/// The name.
/// ``0.
private T GetRepository(IEnumerable repositories, string name)
where T : class, IRepository
{
var enumerable = repositories as T[] ?? repositories.ToArray();
return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
enumerable.FirstOrDefault();
}
}
}