ApplicationHost.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. using MediaBrowser.Api;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Events;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Common.Implementations;
  7. using MediaBrowser.Common.Implementations.ScheduledTasks;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Common.Progress;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Controller.Channels;
  13. using MediaBrowser.Controller.Chapters;
  14. using MediaBrowser.Controller.Collections;
  15. using MediaBrowser.Controller.Configuration;
  16. using MediaBrowser.Controller.Dlna;
  17. using MediaBrowser.Controller.Drawing;
  18. using MediaBrowser.Controller.Dto;
  19. using MediaBrowser.Controller.Entities;
  20. using MediaBrowser.Controller.Entities.Audio;
  21. using MediaBrowser.Controller.Entities.Movies;
  22. using MediaBrowser.Controller.Entities.TV;
  23. using MediaBrowser.Controller.FileOrganization;
  24. using MediaBrowser.Controller.Library;
  25. using MediaBrowser.Controller.LiveTv;
  26. using MediaBrowser.Controller.Localization;
  27. using MediaBrowser.Controller.MediaEncoding;
  28. using MediaBrowser.Controller.Net;
  29. using MediaBrowser.Controller.News;
  30. using MediaBrowser.Controller.Notifications;
  31. using MediaBrowser.Controller.Persistence;
  32. using MediaBrowser.Controller.Plugins;
  33. using MediaBrowser.Controller.Providers;
  34. using MediaBrowser.Controller.Resolvers;
  35. using MediaBrowser.Controller.Security;
  36. using MediaBrowser.Controller.Session;
  37. using MediaBrowser.Controller.Sorting;
  38. using MediaBrowser.Controller.Subtitles;
  39. using MediaBrowser.Controller.Themes;
  40. using MediaBrowser.Dlna;
  41. using MediaBrowser.Dlna.ConnectionManager;
  42. using MediaBrowser.Dlna.ContentDirectory;
  43. using MediaBrowser.Dlna.Main;
  44. using MediaBrowser.LocalMetadata.Providers;
  45. using MediaBrowser.MediaEncoding.BdInfo;
  46. using MediaBrowser.MediaEncoding.Encoder;
  47. using MediaBrowser.MediaEncoding.Subtitles;
  48. using MediaBrowser.Model.Configuration;
  49. using MediaBrowser.Model.Logging;
  50. using MediaBrowser.Model.MediaInfo;
  51. using MediaBrowser.Model.System;
  52. using MediaBrowser.Model.Updates;
  53. using MediaBrowser.Providers.Chapters;
  54. using MediaBrowser.Providers.Manager;
  55. using MediaBrowser.Providers.Subtitles;
  56. using MediaBrowser.Server.Implementations;
  57. using MediaBrowser.Server.Implementations.Channels;
  58. using MediaBrowser.Server.Implementations.Collections;
  59. using MediaBrowser.Server.Implementations.Configuration;
  60. using MediaBrowser.Server.Implementations.Drawing;
  61. using MediaBrowser.Server.Implementations.Dto;
  62. using MediaBrowser.Server.Implementations.EntryPoints;
  63. using MediaBrowser.Server.Implementations.FileOrganization;
  64. using MediaBrowser.Server.Implementations.HttpServer;
  65. using MediaBrowser.Server.Implementations.HttpServer.Security;
  66. using MediaBrowser.Server.Implementations.IO;
  67. using MediaBrowser.Server.Implementations.Library;
  68. using MediaBrowser.Server.Implementations.LiveTv;
  69. using MediaBrowser.Server.Implementations.Localization;
  70. using MediaBrowser.Server.Implementations.MediaEncoder;
  71. using MediaBrowser.Server.Implementations.Notifications;
  72. using MediaBrowser.Server.Implementations.Persistence;
  73. using MediaBrowser.Server.Implementations.Security;
  74. using MediaBrowser.Server.Implementations.ServerManager;
  75. using MediaBrowser.Server.Implementations.Session;
  76. using MediaBrowser.Server.Implementations.Themes;
  77. using MediaBrowser.Server.Implementations.WebSocket;
  78. using MediaBrowser.ServerApplication.EntryPoints;
  79. using MediaBrowser.ServerApplication.FFMpeg;
  80. using MediaBrowser.ServerApplication.IO;
  81. using MediaBrowser.ServerApplication.Native;
  82. using MediaBrowser.ServerApplication.Networking;
  83. using MediaBrowser.WebDashboard.Api;
  84. using MediaBrowser.XbmcMetadata.Providers;
  85. using System;
  86. using System.Collections.Generic;
  87. using System.Globalization;
  88. using System.IO;
  89. using System.Linq;
  90. using System.Reflection;
  91. using System.Threading;
  92. using System.Threading.Tasks;
  93. namespace MediaBrowser.ServerApplication
  94. {
  95. /// <summary>
  96. /// Class CompositionRoot
  97. /// </summary>
  98. public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
  99. {
  100. /// <summary>
  101. /// Gets the server configuration manager.
  102. /// </summary>
  103. /// <value>The server configuration manager.</value>
  104. public IServerConfigurationManager ServerConfigurationManager
  105. {
  106. get { return (IServerConfigurationManager)ConfigurationManager; }
  107. }
  108. /// <summary>
  109. /// Gets the name of the web application that can be used for url building.
  110. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  111. /// </summary>
  112. /// <value>The name of the web application.</value>
  113. public string WebApplicationName
  114. {
  115. get { return "mediabrowser"; }
  116. }
  117. /// <summary>
  118. /// Gets the HTTP server URL prefix.
  119. /// </summary>
  120. /// <value>The HTTP server URL prefix.</value>
  121. private IEnumerable<string> HttpServerUrlPrefixes
  122. {
  123. get
  124. {
  125. var list = new List<string>
  126. {
  127. "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"
  128. };
  129. return list;
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the configuration manager.
  134. /// </summary>
  135. /// <returns>IConfigurationManager.</returns>
  136. protected override IConfigurationManager GetConfigurationManager()
  137. {
  138. return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer);
  139. }
  140. /// <summary>
  141. /// Gets or sets the server manager.
  142. /// </summary>
  143. /// <value>The server manager.</value>
  144. private IServerManager ServerManager { get; set; }
  145. /// <summary>
  146. /// Gets or sets the user manager.
  147. /// </summary>
  148. /// <value>The user manager.</value>
  149. public IUserManager UserManager { get; set; }
  150. /// <summary>
  151. /// Gets or sets the library manager.
  152. /// </summary>
  153. /// <value>The library manager.</value>
  154. internal ILibraryManager LibraryManager { get; set; }
  155. /// <summary>
  156. /// Gets or sets the directory watchers.
  157. /// </summary>
  158. /// <value>The directory watchers.</value>
  159. private ILibraryMonitor LibraryMonitor { get; set; }
  160. /// <summary>
  161. /// Gets or sets the provider manager.
  162. /// </summary>
  163. /// <value>The provider manager.</value>
  164. private IProviderManager ProviderManager { get; set; }
  165. /// <summary>
  166. /// Gets or sets the HTTP server.
  167. /// </summary>
  168. /// <value>The HTTP server.</value>
  169. private IHttpServer HttpServer { get; set; }
  170. private IDtoService DtoService { get; set; }
  171. private IImageProcessor ImageProcessor { get; set; }
  172. private ISeriesOrderManager SeriesOrderManager { get; set; }
  173. /// <summary>
  174. /// Gets or sets the media encoder.
  175. /// </summary>
  176. /// <value>The media encoder.</value>
  177. private IMediaEncoder MediaEncoder { get; set; }
  178. private ISessionManager SessionManager { get; set; }
  179. private ILiveTvManager LiveTvManager { get; set; }
  180. internal ILocalizationManager LocalizationManager { get; set; }
  181. private IEncodingManager EncodingManager { get; set; }
  182. private IChannelManager ChannelManager { get; set; }
  183. /// <summary>
  184. /// Gets or sets the user data repository.
  185. /// </summary>
  186. /// <value>The user data repository.</value>
  187. private IUserDataManager UserDataManager { get; set; }
  188. private IUserRepository UserRepository { get; set; }
  189. internal IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
  190. internal IItemRepository ItemRepository { get; set; }
  191. private INotificationsRepository NotificationsRepository { get; set; }
  192. private IFileOrganizationRepository FileOrganizationRepository { get; set; }
  193. private IProviderRepository ProviderRepository { get; set; }
  194. private INotificationManager NotificationManager { get; set; }
  195. private ISubtitleManager SubtitleManager { get; set; }
  196. private IChapterManager ChapterManager { get; set; }
  197. private IUserViewManager UserViewManager { get; set; }
  198. private IAuthenticationRepository AuthenticationRepository { get; set; }
  199. /// <summary>
  200. /// Initializes a new instance of the <see cref="ApplicationHost"/> class.
  201. /// </summary>
  202. /// <param name="applicationPaths">The application paths.</param>
  203. /// <param name="logManager">The log manager.</param>
  204. public ApplicationHost(ServerApplicationPaths applicationPaths, ILogManager logManager, bool isRunningAsService)
  205. : base(applicationPaths, logManager)
  206. {
  207. _isRunningAsService = isRunningAsService;
  208. }
  209. private readonly bool _isRunningAsService;
  210. public override bool IsRunningAsService
  211. {
  212. get { return _isRunningAsService; }
  213. }
  214. /// <summary>
  215. /// Gets the name.
  216. /// </summary>
  217. /// <value>The name.</value>
  218. public override string Name
  219. {
  220. get
  221. {
  222. return "Media Browser Server";
  223. }
  224. }
  225. /// <summary>
  226. /// Gets a value indicating whether this instance can self restart.
  227. /// </summary>
  228. /// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
  229. public override bool CanSelfRestart
  230. {
  231. get { return NativeApp.CanSelfRestart; }
  232. }
  233. public bool SupportsAutoRunAtStartup
  234. {
  235. get { return NativeApp.SupportsAutoRunAtStartup; }
  236. }
  237. /// <summary>
  238. /// Runs the startup tasks.
  239. /// </summary>
  240. /// <returns>Task.</returns>
  241. public override async Task RunStartupTasks()
  242. {
  243. await base.RunStartupTasks().ConfigureAwait(false);
  244. Logger.Info("Core startup complete");
  245. Parallel.ForEach(GetExports<IServerEntryPoint>(), entryPoint =>
  246. {
  247. try
  248. {
  249. entryPoint.Run();
  250. }
  251. catch (Exception ex)
  252. {
  253. Logger.ErrorException("Error in {0}", ex, entryPoint.GetType().Name);
  254. }
  255. });
  256. LogManager.RemoveConsoleOutput();
  257. }
  258. public override async Task Init(IProgress<double> progress)
  259. {
  260. PerformVersionMigration();
  261. await base.Init(progress).ConfigureAwait(false);
  262. MigrateModularConfigurations();
  263. ApplyDefaultMetadataSettings();
  264. }
  265. private void PerformVersionMigration()
  266. {
  267. DeleteDeprecatedModules();
  268. }
  269. private void MigrateModularConfigurations()
  270. {
  271. var saveConfig = false;
  272. if (ServerConfigurationManager.Configuration.DlnaOptions != null)
  273. {
  274. ServerConfigurationManager.SaveConfiguration("dlna", ServerConfigurationManager.Configuration.DlnaOptions);
  275. ServerConfigurationManager.Configuration.DlnaOptions = null;
  276. saveConfig = true;
  277. }
  278. if (ServerConfigurationManager.Configuration.ChapterOptions != null)
  279. {
  280. ServerConfigurationManager.SaveConfiguration("chapters", ServerConfigurationManager.Configuration.ChapterOptions);
  281. ServerConfigurationManager.Configuration.ChapterOptions = null;
  282. saveConfig = true;
  283. }
  284. if (ServerConfigurationManager.Configuration.ChannelOptions != null)
  285. {
  286. ServerConfigurationManager.SaveConfiguration("channels", ServerConfigurationManager.Configuration.ChannelOptions);
  287. ServerConfigurationManager.Configuration.ChannelOptions = null;
  288. saveConfig = true;
  289. }
  290. if (ServerConfigurationManager.Configuration.NotificationOptions != null)
  291. {
  292. ServerConfigurationManager.SaveConfiguration("notifications", ServerConfigurationManager.Configuration.NotificationOptions);
  293. ServerConfigurationManager.Configuration.NotificationOptions = null;
  294. saveConfig = true;
  295. }
  296. if (saveConfig)
  297. {
  298. ServerConfigurationManager.SaveConfiguration();
  299. }
  300. }
  301. private void ApplyDefaultMetadataSettings()
  302. {
  303. if (!ServerConfigurationManager.Configuration.DefaultMetadataSettingsApplied)
  304. {
  305. // Make sure xbmc metadata is disabled for existing users.
  306. // New users can just take the defaults.
  307. var service = ServerConfigurationManager.Configuration.IsStartupWizardCompleted
  308. ? "Xbmc Nfo"
  309. : "Media Browser Xml";
  310. DisableMetadataService(typeof(Movie), ServerConfigurationManager.Configuration, service);
  311. DisableMetadataService(typeof(MusicAlbum), ServerConfigurationManager.Configuration, service);
  312. DisableMetadataService(typeof(MusicArtist), ServerConfigurationManager.Configuration, service);
  313. DisableMetadataService(typeof(Episode), ServerConfigurationManager.Configuration, service);
  314. DisableMetadataService(typeof(Season), ServerConfigurationManager.Configuration, service);
  315. DisableMetadataService(typeof(Series), ServerConfigurationManager.Configuration, service);
  316. DisableMetadataService(typeof(MusicVideo), ServerConfigurationManager.Configuration, service);
  317. DisableMetadataService(typeof(Trailer), ServerConfigurationManager.Configuration, service);
  318. DisableMetadataService(typeof(AdultVideo), ServerConfigurationManager.Configuration, service);
  319. DisableMetadataService(typeof(Video), ServerConfigurationManager.Configuration, service);
  320. }
  321. ServerConfigurationManager.Configuration.DefaultMetadataSettingsApplied = true;
  322. ServerConfigurationManager.SaveConfiguration();
  323. }
  324. private void DisableMetadataService(Type type, ServerConfiguration config, string service)
  325. {
  326. var options = GetMetadataOptions(type, config);
  327. if (!options.DisabledMetadataSavers.Contains(service, StringComparer.OrdinalIgnoreCase))
  328. {
  329. var list = options.DisabledMetadataSavers.ToList();
  330. list.Add(service);
  331. options.DisabledMetadataSavers = list.ToArray();
  332. }
  333. }
  334. private MetadataOptions GetMetadataOptions(Type type, ServerConfiguration config)
  335. {
  336. var options = config.MetadataOptions
  337. .FirstOrDefault(i => string.Equals(i.ItemType, type.Name, StringComparison.OrdinalIgnoreCase));
  338. if (options == null)
  339. {
  340. var list = config.MetadataOptions.ToList();
  341. options = new MetadataOptions
  342. {
  343. ItemType = type.Name
  344. };
  345. list.Add(options);
  346. config.MetadataOptions = list.ToArray();
  347. }
  348. return options;
  349. }
  350. private void DeleteDeprecatedModules()
  351. {
  352. try
  353. {
  354. MigrateUserFolders();
  355. }
  356. catch (IOException)
  357. {
  358. }
  359. try
  360. {
  361. File.Delete(Path.Combine(ApplicationPaths.PluginsPath, "MBPhoto.dll"));
  362. }
  363. catch (IOException)
  364. {
  365. // Not there, no big deal
  366. }
  367. try
  368. {
  369. File.Delete(Path.Combine(ApplicationPaths.PluginsPath, "MediaBrowser.Plugins.XbmcMetadata.dll"));
  370. }
  371. catch (IOException)
  372. {
  373. // Not there, no big deal
  374. }
  375. Task.Run(() =>
  376. {
  377. try
  378. {
  379. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "remote-images"), true);
  380. }
  381. catch (IOException)
  382. {
  383. // Not there, no big deal
  384. }
  385. try
  386. {
  387. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "chapter-images"), true);
  388. }
  389. catch (IOException)
  390. {
  391. // Not there, no big deal
  392. }
  393. try
  394. {
  395. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-video-images"), true);
  396. }
  397. catch (IOException)
  398. {
  399. // Not there, no big deal
  400. }
  401. try
  402. {
  403. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "extracted-audio-images"), true);
  404. }
  405. catch (IOException)
  406. {
  407. // Not there, no big deal
  408. }
  409. try
  410. {
  411. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "tmdb-tv"), true);
  412. }
  413. catch (IOException)
  414. {
  415. // Not there, no big deal
  416. }
  417. try
  418. {
  419. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "tmdb-collections"), true);
  420. }
  421. catch (IOException)
  422. {
  423. // Not there, no big deal
  424. }
  425. try
  426. {
  427. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "tmdb-movies"), true);
  428. }
  429. catch (IOException)
  430. {
  431. // Not there, no big deal
  432. }
  433. try
  434. {
  435. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "fanart-movies"), true);
  436. }
  437. catch (IOException)
  438. {
  439. // Not there, no big deal
  440. }
  441. try
  442. {
  443. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "fanart-music"), true);
  444. }
  445. catch (IOException)
  446. {
  447. // Not there, no big deal
  448. }
  449. try
  450. {
  451. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "fanart-tv"), true);
  452. }
  453. catch (IOException)
  454. {
  455. // Not there, no big deal
  456. }
  457. try
  458. {
  459. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "tmdb-people"), true);
  460. }
  461. catch (IOException)
  462. {
  463. // Not there, no big deal
  464. }
  465. try
  466. {
  467. Directory.Delete(Path.Combine(ApplicationPaths.DataPath, "tvdb-v3"), true);
  468. }
  469. catch (IOException)
  470. {
  471. // Not there, no big deal
  472. }
  473. });
  474. }
  475. private void MigrateUserFolders()
  476. {
  477. var rootPath = ApplicationPaths.RootFolderPath;
  478. var folders = new DirectoryInfo(rootPath).EnumerateDirectories("*", SearchOption.TopDirectoryOnly).Where(i => !string.Equals(i.Name, "default", StringComparison.OrdinalIgnoreCase))
  479. .ToList();
  480. foreach (var folder in folders)
  481. {
  482. Directory.Delete(folder.FullName, true);
  483. }
  484. }
  485. /// <summary>
  486. /// Registers resources that classes will depend on
  487. /// </summary>
  488. /// <returns>Task.</returns>
  489. protected override async Task RegisterResources(IProgress<double> progress)
  490. {
  491. await base.RegisterResources(progress).ConfigureAwait(false);
  492. RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager, FileSystemManager, JsonSerializer));
  493. RegisterSingleInstance<IServerApplicationHost>(this);
  494. RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
  495. RegisterSingleInstance(ServerConfigurationManager);
  496. LocalizationManager = new LocalizationManager(ServerConfigurationManager, FileSystemManager, JsonSerializer);
  497. RegisterSingleInstance(LocalizationManager);
  498. RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
  499. RegisterSingleInstance<IBlurayExaminer>(() => new BdInfoExaminer());
  500. UserDataManager = new UserDataManager(LogManager);
  501. RegisterSingleInstance(UserDataManager);
  502. UserRepository = await GetUserRepository().ConfigureAwait(false);
  503. RegisterSingleInstance(UserRepository);
  504. DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
  505. RegisterSingleInstance(DisplayPreferencesRepository);
  506. ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
  507. RegisterSingleInstance(ItemRepository);
  508. ProviderRepository = new SqliteProviderInfoRepository(ApplicationPaths, LogManager);
  509. RegisterSingleInstance(ProviderRepository);
  510. FileOrganizationRepository = await GetFileOrganizationRepository().ConfigureAwait(false);
  511. RegisterSingleInstance(FileOrganizationRepository);
  512. AuthenticationRepository = await GetAuthenticationRepository().ConfigureAwait(false);
  513. RegisterSingleInstance(AuthenticationRepository);
  514. UserManager = new UserManager(LogManager.GetLogger("UserManager"), ServerConfigurationManager, UserRepository, XmlSerializer);
  515. RegisterSingleInstance(UserManager);
  516. LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataManager, () => LibraryMonitor, FileSystemManager, () => ProviderManager);
  517. RegisterSingleInstance(LibraryManager);
  518. var musicManager = new MusicManager(LibraryManager);
  519. RegisterSingleInstance<IMusicManager>(new MusicManager(LibraryManager));
  520. LibraryMonitor = new LibraryMonitor(LogManager, TaskManager, LibraryManager, ServerConfigurationManager, FileSystemManager);
  521. RegisterSingleInstance(LibraryMonitor);
  522. ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, LibraryMonitor, LogManager, FileSystemManager);
  523. RegisterSingleInstance(ProviderManager);
  524. SeriesOrderManager = new SeriesOrderManager();
  525. RegisterSingleInstance(SeriesOrderManager);
  526. RegisterSingleInstance<ISearchEngine>(() => new SearchEngine(LogManager, LibraryManager, UserManager));
  527. HttpServer = ServerFactory.CreateServer(this, LogManager, "Media Browser", WebApplicationName, "dashboard/index.html");
  528. RegisterSingleInstance(HttpServer, false);
  529. progress.Report(10);
  530. ServerManager = new ServerManager(this, JsonSerializer, LogManager.GetLogger("ServerManager"), ServerConfigurationManager);
  531. RegisterSingleInstance(ServerManager);
  532. var innerProgress = new ActionableProgress<double>();
  533. innerProgress.RegisterAction(p => progress.Report((.75 * p) + 15));
  534. await RegisterMediaEncoder(innerProgress).ConfigureAwait(false);
  535. progress.Report(90);
  536. ImageProcessor = new ImageProcessor(LogManager.GetLogger("ImageProcessor"), ServerConfigurationManager.ApplicationPaths, FileSystemManager, JsonSerializer, MediaEncoder);
  537. RegisterSingleInstance(ImageProcessor);
  538. DtoService = new DtoService(Logger, LibraryManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager, () => ChannelManager);
  539. RegisterSingleInstance(DtoService);
  540. SessionManager = new SessionManager(UserDataManager, ServerConfigurationManager, Logger, UserRepository, LibraryManager, UserManager, musicManager, DtoService, ImageProcessor, ItemRepository, JsonSerializer, this, HttpClient, AuthenticationRepository);
  541. RegisterSingleInstance(SessionManager);
  542. var newsService = new Server.Implementations.News.NewsService(ApplicationPaths, JsonSerializer);
  543. RegisterSingleInstance<INewsService>(newsService);
  544. var fileOrganizationService = new FileOrganizationService(TaskManager, FileOrganizationRepository, LogManager.GetLogger("FileOrganizationService"), LibraryMonitor, LibraryManager, ServerConfigurationManager, FileSystemManager, ProviderManager);
  545. RegisterSingleInstance<IFileOrganizationService>(fileOrganizationService);
  546. progress.Report(15);
  547. ChannelManager = new ChannelManager(UserManager, DtoService, LibraryManager, Logger, ServerConfigurationManager, FileSystemManager, UserDataManager, JsonSerializer, LocalizationManager);
  548. RegisterSingleInstance(ChannelManager);
  549. var appThemeManager = new AppThemeManager(ApplicationPaths, FileSystemManager, JsonSerializer, Logger);
  550. RegisterSingleInstance<IAppThemeManager>(appThemeManager);
  551. var dlnaManager = new DlnaManager(XmlSerializer, FileSystemManager, ApplicationPaths, LogManager.GetLogger("Dlna"), JsonSerializer);
  552. RegisterSingleInstance<IDlnaManager>(dlnaManager);
  553. var contentDirectory = new ContentDirectory(dlnaManager, UserDataManager, ImageProcessor, LibraryManager, ServerConfigurationManager, UserManager, LogManager.GetLogger("UpnpContentDirectory"), HttpClient);
  554. RegisterSingleInstance<IContentDirectory>(contentDirectory);
  555. var connectionManager = new ConnectionManager(dlnaManager, ServerConfigurationManager, LogManager.GetLogger("UpnpConnectionManager"), HttpClient);
  556. RegisterSingleInstance<IConnectionManager>(connectionManager);
  557. var collectionManager = new CollectionManager(LibraryManager, FileSystemManager, LibraryMonitor, LogManager.GetLogger("CollectionManager"));
  558. RegisterSingleInstance<ICollectionManager>(collectionManager);
  559. LiveTvManager = new LiveTvManager(ServerConfigurationManager, FileSystemManager, Logger, ItemRepository, ImageProcessor, UserDataManager, DtoService, UserManager, LibraryManager, TaskManager, LocalizationManager);
  560. RegisterSingleInstance(LiveTvManager);
  561. UserViewManager = new UserViewManager(LibraryManager, LocalizationManager, FileSystemManager, UserManager, ChannelManager, LiveTvManager);
  562. RegisterSingleInstance(UserViewManager);
  563. NotificationManager = new NotificationManager(LogManager, UserManager, ServerConfigurationManager);
  564. RegisterSingleInstance(NotificationManager);
  565. RegisterSingleInstance<IEncryptionManager>(new EncryptionManager());
  566. SubtitleManager = new SubtitleManager(LogManager.GetLogger("SubtitleManager"), FileSystemManager, LibraryMonitor, LibraryManager, ItemRepository);
  567. RegisterSingleInstance(SubtitleManager);
  568. ChapterManager = new ChapterManager(LibraryManager, LogManager.GetLogger("ChapterManager"), ServerConfigurationManager, ItemRepository);
  569. RegisterSingleInstance(ChapterManager);
  570. EncodingManager = new EncodingManager(ServerConfigurationManager, FileSystemManager, Logger,
  571. MediaEncoder, ChapterManager);
  572. RegisterSingleInstance(EncodingManager);
  573. var authContext = new AuthorizationContext();
  574. RegisterSingleInstance<IAuthorizationContext>(authContext);
  575. RegisterSingleInstance<ISessionContext>(new SessionContext(UserManager, authContext, SessionManager));
  576. RegisterSingleInstance<IAuthService>(new AuthService(UserManager, SessionManager, authContext, ServerConfigurationManager));
  577. RegisterSingleInstance<ISubtitleEncoder>(new SubtitleEncoder(LibraryManager, LogManager.GetLogger("SubtitleEncoder"), ApplicationPaths, FileSystemManager, MediaEncoder));
  578. var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
  579. var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false));
  580. var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().ConfigureAwait(false));
  581. await ConfigureNotificationsRepository().ConfigureAwait(false);
  582. progress.Report(92);
  583. await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
  584. progress.Report(100);
  585. SetStaticProperties();
  586. await ((UserManager)UserManager).Initialize().ConfigureAwait(false);
  587. SetKernelProperties();
  588. }
  589. protected override INetworkManager CreateNetworkManager()
  590. {
  591. return new NetworkManager();
  592. }
  593. protected override IFileSystem CreateFileSystemManager()
  594. {
  595. return FileSystemFactory.CreateFileSystemManager(LogManager);
  596. }
  597. /// <summary>
  598. /// Registers the media encoder.
  599. /// </summary>
  600. /// <returns>Task.</returns>
  601. private async Task RegisterMediaEncoder(IProgress<double> progress)
  602. {
  603. var info = await new FFMpegDownloader(Logger, ApplicationPaths, HttpClient, ZipClient, FileSystemManager).GetFFMpegInfo(progress).ConfigureAwait(false);
  604. MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), JsonSerializer, info.EncoderPath, info.ProbePath, info.Version);
  605. RegisterSingleInstance(MediaEncoder);
  606. }
  607. /// <summary>
  608. /// Sets the kernel properties.
  609. /// </summary>
  610. private void SetKernelProperties()
  611. {
  612. LocalizedStrings.StringFiles = GetExports<LocalizedStringData>();
  613. }
  614. /// <summary>
  615. /// Gets the user repository.
  616. /// </summary>
  617. /// <returns>Task{IUserRepository}.</returns>
  618. private async Task<IUserRepository> GetUserRepository()
  619. {
  620. var repo = new SqliteUserRepository(JsonSerializer, LogManager, ApplicationPaths);
  621. await repo.Initialize().ConfigureAwait(false);
  622. return repo;
  623. }
  624. /// <summary>
  625. /// Gets the file organization repository.
  626. /// </summary>
  627. /// <returns>Task{IUserRepository}.</returns>
  628. private async Task<IFileOrganizationRepository> GetFileOrganizationRepository()
  629. {
  630. var repo = new SqliteFileOrganizationRepository(LogManager, ServerConfigurationManager.ApplicationPaths);
  631. await repo.Initialize().ConfigureAwait(false);
  632. return repo;
  633. }
  634. private async Task<IAuthenticationRepository> GetAuthenticationRepository()
  635. {
  636. var repo = new AuthenticationRepository(LogManager.GetLogger("AuthenticationRepository"), ServerConfigurationManager.ApplicationPaths);
  637. await repo.Initialize().ConfigureAwait(false);
  638. return repo;
  639. }
  640. /// <summary>
  641. /// Configures the repositories.
  642. /// </summary>
  643. /// <returns>Task.</returns>
  644. private async Task ConfigureNotificationsRepository()
  645. {
  646. var repo = new SqliteNotificationsRepository(LogManager, ApplicationPaths);
  647. await repo.Initialize().ConfigureAwait(false);
  648. NotificationsRepository = repo;
  649. RegisterSingleInstance(NotificationsRepository);
  650. }
  651. /// <summary>
  652. /// Configures the repositories.
  653. /// </summary>
  654. /// <returns>Task.</returns>
  655. private async Task ConfigureDisplayPreferencesRepositories()
  656. {
  657. await DisplayPreferencesRepository.Initialize().ConfigureAwait(false);
  658. }
  659. /// <summary>
  660. /// Configures the item repositories.
  661. /// </summary>
  662. /// <returns>Task.</returns>
  663. private async Task ConfigureItemRepositories()
  664. {
  665. await ItemRepository.Initialize().ConfigureAwait(false);
  666. await ProviderRepository.Initialize().ConfigureAwait(false);
  667. ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
  668. }
  669. /// <summary>
  670. /// Configures the user data repositories.
  671. /// </summary>
  672. /// <returns>Task.</returns>
  673. private async Task ConfigureUserDataRepositories()
  674. {
  675. var repo = new SqliteUserDataRepository(ApplicationPaths, LogManager);
  676. await repo.Initialize().ConfigureAwait(false);
  677. ((UserDataManager)UserDataManager).Repository = repo;
  678. }
  679. /// <summary>
  680. /// Dirty hacks
  681. /// </summary>
  682. private void SetStaticProperties()
  683. {
  684. // For now there's no real way to inject these properly
  685. BaseItem.Logger = LogManager.GetLogger("BaseItem");
  686. BaseItem.ConfigurationManager = ServerConfigurationManager;
  687. BaseItem.LibraryManager = LibraryManager;
  688. BaseItem.ProviderManager = ProviderManager;
  689. BaseItem.LocalizationManager = LocalizationManager;
  690. BaseItem.ItemRepository = ItemRepository;
  691. User.XmlSerializer = XmlSerializer;
  692. User.UserManager = UserManager;
  693. LocalizedStrings.ApplicationPaths = ApplicationPaths;
  694. Folder.UserManager = UserManager;
  695. BaseItem.FileSystem = FileSystemManager;
  696. BaseItem.UserDataManager = UserDataManager;
  697. ChannelVideoItem.ChannelManager = ChannelManager;
  698. }
  699. /// <summary>
  700. /// Finds the parts.
  701. /// </summary>
  702. protected override void FindParts()
  703. {
  704. if (IsFirstRun)
  705. {
  706. RegisterServerWithAdministratorAccess();
  707. }
  708. base.FindParts();
  709. HttpServer.Init(GetExports<IRestfulService>(false));
  710. ServerManager.AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  711. StartServer(true);
  712. LibraryManager.AddParts(GetExports<IResolverIgnoreRule>(),
  713. GetExports<IVirtualFolderCreator>(),
  714. GetExports<IItemResolver>(),
  715. GetExports<IIntroProvider>(),
  716. GetExports<IBaseItemComparer>(),
  717. GetExports<ILibraryPostScanTask>());
  718. ProviderManager.AddParts(GetExports<IImageProvider>(),
  719. GetExports<IMetadataService>(),
  720. GetExports<IItemIdentityProvider>(),
  721. GetExports<IItemIdentityConverter>(),
  722. GetExports<IMetadataProvider>(),
  723. GetExports<IMetadataSaver>(),
  724. GetExports<IImageSaver>(),
  725. GetExports<IExternalId>());
  726. SeriesOrderManager.AddParts(GetExports<ISeriesOrderProvider>());
  727. ImageProcessor.AddParts(GetExports<IImageEnhancer>());
  728. LiveTvManager.AddParts(GetExports<ILiveTvService>());
  729. SubtitleManager.AddParts(GetExports<ISubtitleProvider>());
  730. ChapterManager.AddParts(GetExports<IChapterProvider>());
  731. SessionManager.AddParts(GetExports<ISessionControllerFactory>());
  732. ChannelManager.AddParts(GetExports<IChannel>(), GetExports<IChannelFactory>());
  733. NotificationManager.AddParts(GetExports<INotificationService>(), GetExports<INotificationTypeFactory>());
  734. }
  735. /// <summary>
  736. /// Starts the server.
  737. /// </summary>
  738. /// <param name="retryOnFailure">if set to <c>true</c> [retry on failure].</param>
  739. private void StartServer(bool retryOnFailure)
  740. {
  741. try
  742. {
  743. ServerManager.Start(HttpServerUrlPrefixes, ServerConfigurationManager.Configuration.EnableHttpLevelLogging);
  744. }
  745. catch (Exception ex)
  746. {
  747. Logger.ErrorException("Error starting http server", ex);
  748. if (retryOnFailure)
  749. {
  750. RegisterServerWithAdministratorAccess();
  751. StartServer(false);
  752. }
  753. else
  754. {
  755. throw;
  756. }
  757. }
  758. ServerManager.StartWebSocketServer();
  759. }
  760. /// <summary>
  761. /// Called when [configuration updated].
  762. /// </summary>
  763. /// <param name="sender">The sender.</param>
  764. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  765. protected override void OnConfigurationUpdated(object sender, EventArgs e)
  766. {
  767. base.OnConfigurationUpdated(sender, e);
  768. HttpServer.EnableHttpRequestLogging = ServerConfigurationManager.Configuration.EnableHttpLevelLogging;
  769. if (!HttpServer.UrlPrefixes.SequenceEqual(HttpServerUrlPrefixes, StringComparer.OrdinalIgnoreCase))
  770. {
  771. NotifyPendingRestart();
  772. }
  773. else if (!ServerManager.SupportsNativeWebSocket && ServerManager.WebSocketPortNumber != ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber)
  774. {
  775. NotifyPendingRestart();
  776. }
  777. }
  778. /// <summary>
  779. /// Restarts this instance.
  780. /// </summary>
  781. public override async Task Restart()
  782. {
  783. if (!CanSelfRestart)
  784. {
  785. throw new InvalidOperationException("The server is unable to self-restart. Please restart manually.");
  786. }
  787. try
  788. {
  789. await SessionManager.SendServerRestartNotification(CancellationToken.None).ConfigureAwait(false);
  790. }
  791. catch (Exception ex)
  792. {
  793. Logger.ErrorException("Error sending server restart notification", ex);
  794. }
  795. Logger.Debug("Calling NativeApp.Restart");
  796. NativeApp.Restart();
  797. }
  798. /// <summary>
  799. /// Gets or sets a value indicating whether this instance can self update.
  800. /// </summary>
  801. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  802. public override bool CanSelfUpdate
  803. {
  804. get
  805. {
  806. #if DEBUG
  807. return false;
  808. #endif
  809. #pragma warning disable 162
  810. return NativeApp.CanSelfUpdate;
  811. #pragma warning restore 162
  812. }
  813. }
  814. /// <summary>
  815. /// Gets the composable part assemblies.
  816. /// </summary>
  817. /// <returns>IEnumerable{Assembly}.</returns>
  818. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  819. {
  820. var list = GetPluginAssemblies()
  821. .ToList();
  822. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  823. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  824. // Include composable parts in the Api assembly
  825. list.Add(typeof(ApiEntryPoint).Assembly);
  826. // Include composable parts in the Dashboard assembly
  827. list.Add(typeof(DashboardService).Assembly);
  828. // Include composable parts in the Model assembly
  829. list.Add(typeof(SystemInfo).Assembly);
  830. // Include composable parts in the Common assembly
  831. list.Add(typeof(IApplicationHost).Assembly);
  832. // Include composable parts in the Controller assembly
  833. list.Add(typeof(IServerApplicationHost).Assembly);
  834. // Include composable parts in the Providers assembly
  835. list.Add(typeof(ProviderUtils).Assembly);
  836. // Common implementations
  837. list.Add(typeof(TaskManager).Assembly);
  838. // Server implementations
  839. list.Add(typeof(ServerApplicationPaths).Assembly);
  840. // MediaEncoding
  841. list.Add(typeof(MediaEncoder).Assembly);
  842. // Dlna
  843. list.Add(typeof(DlnaEntryPoint).Assembly);
  844. // Local metadata
  845. list.Add(typeof(AlbumXmlProvider).Assembly);
  846. // Xbmc
  847. list.Add(typeof(ArtistNfoProvider).Assembly);
  848. list.AddRange(Assemblies.GetAssembliesWithParts());
  849. // Include composable parts in the running assembly
  850. list.Add(GetType().Assembly);
  851. return list;
  852. }
  853. /// <summary>
  854. /// Gets the plugin assemblies.
  855. /// </summary>
  856. /// <returns>IEnumerable{Assembly}.</returns>
  857. private IEnumerable<Assembly> GetPluginAssemblies()
  858. {
  859. try
  860. {
  861. return Directory.EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  862. .Select(LoadAssembly)
  863. .Where(a => a != null)
  864. .ToList();
  865. }
  866. catch (DirectoryNotFoundException)
  867. {
  868. return new List<Assembly>();
  869. }
  870. }
  871. private readonly string _systemId = Environment.MachineName.GetMD5().ToString();
  872. /// <summary>
  873. /// Gets the system status.
  874. /// </summary>
  875. /// <returns>SystemInfo.</returns>
  876. public virtual SystemInfo GetSystemInfo()
  877. {
  878. return new SystemInfo
  879. {
  880. HasPendingRestart = HasPendingRestart,
  881. Version = ApplicationVersion.ToString(),
  882. IsNetworkDeployed = CanSelfUpdate,
  883. WebSocketPortNumber = ServerManager.WebSocketPortNumber,
  884. SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
  885. FailedPluginAssemblies = FailedAssemblies.ToList(),
  886. InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToList(),
  887. CompletedInstallations = InstallationManager.CompletedInstallations.ToList(),
  888. Id = _systemId,
  889. ProgramDataPath = ApplicationPaths.ProgramDataPath,
  890. LogPath = ApplicationPaths.LogDirectoryPath,
  891. ItemsByNamePath = ApplicationPaths.ItemsByNamePath,
  892. InternalMetadataPath = ApplicationPaths.InternalMetadataPath,
  893. CachePath = ApplicationPaths.CachePath,
  894. MacAddress = GetMacAddress(),
  895. HttpServerPortNumber = HttpServerPort,
  896. OperatingSystem = Environment.OSVersion.ToString(),
  897. CanSelfRestart = CanSelfRestart,
  898. CanSelfUpdate = CanSelfUpdate,
  899. WanAddress = GetWanAddress(),
  900. HasUpdateAvailable = HasUpdateAvailable,
  901. SupportsAutoRunAtStartup = SupportsAutoRunAtStartup,
  902. TranscodingTempPath = ApplicationPaths.TranscodingTempPath,
  903. IsRunningAsService = IsRunningAsService,
  904. ServerName = string.IsNullOrWhiteSpace(ServerConfigurationManager.Configuration.ServerName) ? Environment.MachineName : ServerConfigurationManager.Configuration.ServerName
  905. };
  906. }
  907. public int HttpServerPort
  908. {
  909. get { return ServerConfigurationManager.Configuration.HttpServerPortNumber; }
  910. }
  911. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  912. private string GetWanAddress()
  913. {
  914. var ip = ServerConfigurationManager.Configuration.WanDdns;
  915. if (string.IsNullOrWhiteSpace(ip))
  916. {
  917. ip = WanAddressEntryPoint.WanAddress;
  918. }
  919. if (!string.IsNullOrEmpty(ip))
  920. {
  921. if (!ip.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
  922. !ip.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  923. {
  924. ip = "http://" + ip;
  925. }
  926. return ip + ":" + ServerConfigurationManager.Configuration.HttpServerPortNumber.ToString(_usCulture);
  927. }
  928. return null;
  929. }
  930. /// <summary>
  931. /// Gets the mac address.
  932. /// </summary>
  933. /// <returns>System.String.</returns>
  934. private string GetMacAddress()
  935. {
  936. try
  937. {
  938. return NetworkManager.GetMacAddress();
  939. }
  940. catch (Exception ex)
  941. {
  942. Logger.ErrorException("Error getting mac address", ex);
  943. return null;
  944. }
  945. }
  946. /// <summary>
  947. /// Shuts down.
  948. /// </summary>
  949. public override async Task Shutdown()
  950. {
  951. try
  952. {
  953. await SessionManager.SendServerShutdownNotification(CancellationToken.None).ConfigureAwait(false);
  954. }
  955. catch (Exception ex)
  956. {
  957. Logger.ErrorException("Error sending server shutdown notification", ex);
  958. }
  959. NativeApp.Shutdown();
  960. }
  961. /// <summary>
  962. /// Registers the server with administrator access.
  963. /// </summary>
  964. private void RegisterServerWithAdministratorAccess()
  965. {
  966. Logger.Info("Requesting administrative access to authorize http server");
  967. try
  968. {
  969. ServerAuthorization.AuthorizeServer(
  970. ServerConfigurationManager.Configuration.HttpServerPortNumber,
  971. HttpServerUrlPrefixes.First(),
  972. ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber,
  973. UdpServerEntryPoint.PortNumber,
  974. ConfigurationManager.CommonApplicationPaths.TempDirectory);
  975. }
  976. catch (Exception ex)
  977. {
  978. Logger.ErrorException("Error authorizing server", ex);
  979. }
  980. }
  981. public event EventHandler HasUpdateAvailableChanged;
  982. private bool _hasUpdateAvailable;
  983. public bool HasUpdateAvailable
  984. {
  985. get { return _hasUpdateAvailable; }
  986. set
  987. {
  988. var fireEvent = value && !_hasUpdateAvailable;
  989. _hasUpdateAvailable = value;
  990. if (fireEvent)
  991. {
  992. EventHelper.FireEventIfNotNull(HasUpdateAvailableChanged, this, EventArgs.Empty, Logger);
  993. }
  994. }
  995. }
  996. /// <summary>
  997. /// Checks for update.
  998. /// </summary>
  999. /// <param name="cancellationToken">The cancellation token.</param>
  1000. /// <param name="progress">The progress.</param>
  1001. /// <returns>Task{CheckForUpdateResult}.</returns>
  1002. public override async Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  1003. {
  1004. var availablePackages = await InstallationManager.GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);
  1005. var version = InstallationManager.GetLatestCompatibleVersion(availablePackages, "MBServer", null, ApplicationVersion,
  1006. ConfigurationManager.CommonConfiguration.SystemUpdateLevel);
  1007. var versionObject = version == null || string.IsNullOrWhiteSpace(version.versionStr) ? null : new Version(version.versionStr);
  1008. HasUpdateAvailable = versionObject != null && versionObject >= ApplicationVersion;
  1009. if (versionObject != null && versionObject >= ApplicationVersion)
  1010. {
  1011. Logger.Info("New application version is available: {0}", versionObject);
  1012. }
  1013. return versionObject != null ?
  1014. new CheckForUpdateResult { AvailableVersion = versionObject.ToString(), IsUpdateAvailable = versionObject > ApplicationVersion, Package = version } :
  1015. new CheckForUpdateResult { AvailableVersion = ApplicationVersion.ToString(), IsUpdateAvailable = false };
  1016. }
  1017. /// <summary>
  1018. /// Updates the application.
  1019. /// </summary>
  1020. /// <param name="package">The package that contains the update</param>
  1021. /// <param name="cancellationToken">The cancellation token.</param>
  1022. /// <param name="progress">The progress.</param>
  1023. /// <returns>Task.</returns>
  1024. public override async Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress<double> progress)
  1025. {
  1026. await InstallationManager.InstallPackage(package, progress, cancellationToken).ConfigureAwait(false);
  1027. HasUpdateAvailable = false;
  1028. OnApplicationUpdated(package);
  1029. }
  1030. /// <summary>
  1031. /// Configures the automatic run at startup.
  1032. /// </summary>
  1033. /// <param name="autorun">if set to <c>true</c> [autorun].</param>
  1034. protected override void ConfigureAutoRunAtStartup(bool autorun)
  1035. {
  1036. if (SupportsAutoRunAtStartup)
  1037. {
  1038. Autorun.Configure(autorun);
  1039. }
  1040. }
  1041. }
  1042. }