App.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using MediaBrowser.ClickOnce;
  2. using MediaBrowser.Common.Implementations.Serialization;
  3. using MediaBrowser.Common.Kernel;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Logging.Nlog;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Server.Uninstall;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Net.Cache;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. namespace MediaBrowser.ServerApplication
  20. {
  21. /// <summary>
  22. /// Interaction logic for App.xaml
  23. /// </summary>
  24. public partial class App : Application
  25. {
  26. /// <summary>
  27. /// Defines the entry point of the application.
  28. /// </summary>
  29. [STAThread]
  30. public static void Main()
  31. {
  32. var application = new App(new NLogger("App"));
  33. application.Run();
  34. }
  35. /// <summary>
  36. /// Gets the instance.
  37. /// </summary>
  38. /// <value>The instance.</value>
  39. public static App Instance
  40. {
  41. get
  42. {
  43. return Current as App;
  44. }
  45. }
  46. /// <summary>
  47. /// The single instance mutex
  48. /// </summary>
  49. private Mutex SingleInstanceMutex;
  50. /// <summary>
  51. /// Gets or sets the kernel.
  52. /// </summary>
  53. /// <value>The kernel.</value>
  54. protected IKernel Kernel { get; set; }
  55. /// <summary>
  56. /// Gets or sets the logger.
  57. /// </summary>
  58. /// <value>The logger.</value>
  59. protected ILogger Logger { get; set; }
  60. /// <summary>
  61. /// Gets or sets the composition root.
  62. /// </summary>
  63. /// <value>The composition root.</value>
  64. protected ApplicationHost CompositionRoot { get; set; }
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="App" /> class.
  67. /// </summary>
  68. /// <param name="logger">The logger.</param>
  69. public App(ILogger logger)
  70. {
  71. Logger = logger;
  72. InitializeComponent();
  73. }
  74. /// <summary>
  75. /// Gets the name of the product.
  76. /// </summary>
  77. /// <value>The name of the product.</value>
  78. protected string ProductName
  79. {
  80. get { return Globals.ProductName; }
  81. }
  82. /// <summary>
  83. /// Gets the name of the publisher.
  84. /// </summary>
  85. /// <value>The name of the publisher.</value>
  86. protected string PublisherName
  87. {
  88. get { return Globals.PublisherName; }
  89. }
  90. /// <summary>
  91. /// Gets the name of the suite.
  92. /// </summary>
  93. /// <value>The name of the suite.</value>
  94. protected string SuiteName
  95. {
  96. get { return Globals.SuiteName; }
  97. }
  98. /// <summary>
  99. /// Gets the name of the uninstaller file.
  100. /// </summary>
  101. /// <value>The name of the uninstaller file.</value>
  102. protected string UninstallerFileName
  103. {
  104. get { return "MediaBrowser.Server.Uninstall.exe"; }
  105. }
  106. /// <summary>
  107. /// Gets or sets a value indicating whether [last run at startup value].
  108. /// </summary>
  109. /// <value><c>null</c> if [last run at startup value] contains no value, <c>true</c> if [last run at startup value]; otherwise, <c>false</c>.</value>
  110. private bool? LastRunAtStartupValue { get; set; }
  111. /// <summary>
  112. /// Raises the <see cref="E:System.Windows.Application.Startup" /> event.
  113. /// </summary>
  114. /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param>
  115. protected override void OnStartup(StartupEventArgs e)
  116. {
  117. bool createdNew;
  118. SingleInstanceMutex = new Mutex(true, @"Local\" + GetType().Assembly.GetName().Name, out createdNew);
  119. if (!createdNew)
  120. {
  121. SingleInstanceMutex = null;
  122. Shutdown();
  123. return;
  124. }
  125. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  126. LoadKernel();
  127. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  128. }
  129. /// <summary>
  130. /// Handles the UnhandledException event of the CurrentDomain control.
  131. /// </summary>
  132. /// <param name="sender">The source of the event.</param>
  133. /// <param name="e">The <see cref="UnhandledExceptionEventArgs" /> instance containing the event data.</param>
  134. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  135. {
  136. var exception = (Exception)e.ExceptionObject;
  137. Logger.ErrorException("UnhandledException", exception);
  138. MessageBox.Show("Unhandled exception: " + exception.Message);
  139. }
  140. /// <summary>
  141. /// Handles the SessionEnding event of the SystemEvents control.
  142. /// </summary>
  143. /// <param name="sender">The source of the event.</param>
  144. /// <param name="e">The <see cref="SessionEndingEventArgs" /> instance containing the event data.</param>
  145. void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  146. {
  147. // Try to shut down gracefully
  148. Shutdown();
  149. }
  150. /// <summary>
  151. /// Loads the kernel.
  152. /// </summary>
  153. protected async void LoadKernel()
  154. {
  155. CompositionRoot = new ApplicationHost(Logger);
  156. Kernel = CompositionRoot.Kernel;
  157. try
  158. {
  159. new MainWindow(new JsonSerializer(), Logger).Show();
  160. var now = DateTime.UtcNow;
  161. await Kernel.Init();
  162. var done = (DateTime.UtcNow - now);
  163. Logger.Info("Kernel.Init completed in {0}{1} minutes and {2} seconds.", done.Hours > 0 ? done.Hours + " Hours " : "", done.Minutes, done.Seconds);
  164. await OnKernelLoaded();
  165. }
  166. catch (Exception ex)
  167. {
  168. Logger.ErrorException("Error launching application", ex);
  169. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  170. // Shutdown the app with an error code
  171. Shutdown(1);
  172. }
  173. }
  174. /// <summary>
  175. /// Called when [kernel loaded].
  176. /// </summary>
  177. /// <returns>Task.</returns>
  178. protected Task OnKernelLoaded()
  179. {
  180. return Task.Run(() =>
  181. {
  182. Kernel.ConfigurationUpdated += Kernel_ConfigurationUpdated;
  183. ConfigureClickOnceStartup();
  184. });
  185. }
  186. /// <summary>
  187. /// Handles the ConfigurationUpdated event of the Kernel control.
  188. /// </summary>
  189. /// <param name="sender">The source of the event.</param>
  190. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  191. void Kernel_ConfigurationUpdated(object sender, EventArgs e)
  192. {
  193. if (!LastRunAtStartupValue.HasValue || LastRunAtStartupValue.Value != Kernel.Configuration.RunAtStartup)
  194. {
  195. ConfigureClickOnceStartup();
  196. }
  197. }
  198. /// <summary>
  199. /// Configures the click once startup.
  200. /// </summary>
  201. private void ConfigureClickOnceStartup()
  202. {
  203. try
  204. {
  205. ClickOnceHelper.ConfigureClickOnceStartupIfInstalled(PublisherName, ProductName, SuiteName, Kernel.Configuration.RunAtStartup, UninstallerFileName);
  206. LastRunAtStartupValue = Kernel.Configuration.RunAtStartup;
  207. }
  208. catch (Exception ex)
  209. {
  210. Logger.ErrorException("Error configuring ClickOnce", ex);
  211. }
  212. }
  213. /// <summary>
  214. /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
  215. /// </summary>
  216. /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
  217. protected override void OnExit(ExitEventArgs e)
  218. {
  219. ReleaseMutex();
  220. base.OnExit(e);
  221. Kernel.Dispose();
  222. CompositionRoot.Dispose();
  223. }
  224. /// <summary>
  225. /// Releases the mutex.
  226. /// </summary>
  227. private void ReleaseMutex()
  228. {
  229. if (SingleInstanceMutex == null)
  230. {
  231. return;
  232. }
  233. SingleInstanceMutex.ReleaseMutex();
  234. SingleInstanceMutex.Close();
  235. SingleInstanceMutex.Dispose();
  236. SingleInstanceMutex = null;
  237. }
  238. /// <summary>
  239. /// Opens the dashboard.
  240. /// </summary>
  241. public static void OpenDashboard()
  242. {
  243. OpenDashboardPage("dashboard.html");
  244. }
  245. /// <summary>
  246. /// Opens the dashboard page.
  247. /// </summary>
  248. /// <param name="page">The page.</param>
  249. public static void OpenDashboardPage(string page)
  250. {
  251. var url = "http://localhost:" + Controller.Kernel.Instance.Configuration.HttpServerPortNumber + "/" +
  252. Controller.Kernel.Instance.WebApplicationName + "/dashboard/" + page;
  253. url = AddAutoLoginToDashboardUrl(url);
  254. OpenUrl(url);
  255. }
  256. /// <summary>
  257. /// Adds the auto login to dashboard URL.
  258. /// </summary>
  259. /// <param name="url">The URL.</param>
  260. /// <returns>System.String.</returns>
  261. public static string AddAutoLoginToDashboardUrl(string url)
  262. {
  263. var user = Controller.Kernel.Instance.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  264. if (user != null)
  265. {
  266. if (url.IndexOf('?') == -1)
  267. {
  268. url += "?u=" + user.Id;
  269. }
  270. else
  271. {
  272. url += "&u=" + user.Id;
  273. }
  274. }
  275. return url;
  276. }
  277. /// <summary>
  278. /// Opens the URL.
  279. /// </summary>
  280. /// <param name="url">The URL.</param>
  281. public static void OpenUrl(string url)
  282. {
  283. var process = new Process
  284. {
  285. StartInfo = new ProcessStartInfo
  286. {
  287. FileName = url
  288. },
  289. EnableRaisingEvents = true
  290. };
  291. process.Exited += ProcessExited;
  292. process.Start();
  293. }
  294. /// <summary>
  295. /// Processes the exited.
  296. /// </summary>
  297. /// <param name="sender">The sender.</param>
  298. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  299. static void ProcessExited(object sender, EventArgs e)
  300. {
  301. ((Process)sender).Dispose();
  302. }
  303. /// <summary>
  304. /// Restarts this instance.
  305. /// </summary>
  306. /// <exception cref="System.NotImplementedException"></exception>
  307. public void Restart()
  308. {
  309. Dispatcher.Invoke(ReleaseMutex);
  310. Kernel.Dispose();
  311. System.Windows.Forms.Application.Restart();
  312. Dispatcher.Invoke(Shutdown);
  313. }
  314. /// <summary>
  315. /// Gets the image.
  316. /// </summary>
  317. /// <param name="uri">The URI.</param>
  318. /// <returns>Image.</returns>
  319. /// <exception cref="System.ArgumentNullException">uri</exception>
  320. public Image GetImage(string uri)
  321. {
  322. if (string.IsNullOrEmpty(uri))
  323. {
  324. throw new ArgumentNullException("uri");
  325. }
  326. return GetImage(new Uri(uri));
  327. }
  328. /// <summary>
  329. /// Gets the image.
  330. /// </summary>
  331. /// <param name="uri">The URI.</param>
  332. /// <returns>Image.</returns>
  333. /// <exception cref="System.ArgumentNullException">uri</exception>
  334. public Image GetImage(Uri uri)
  335. {
  336. if (uri == null)
  337. {
  338. throw new ArgumentNullException("uri");
  339. }
  340. return new Image { Source = GetBitmapImage(uri) };
  341. }
  342. /// <summary>
  343. /// Gets the bitmap image.
  344. /// </summary>
  345. /// <param name="uri">The URI.</param>
  346. /// <returns>BitmapImage.</returns>
  347. /// <exception cref="System.ArgumentNullException">uri</exception>
  348. public BitmapImage GetBitmapImage(string uri)
  349. {
  350. if (string.IsNullOrEmpty(uri))
  351. {
  352. throw new ArgumentNullException("uri");
  353. }
  354. return GetBitmapImage(new Uri(uri));
  355. }
  356. /// <summary>
  357. /// Gets the bitmap image.
  358. /// </summary>
  359. /// <param name="uri">The URI.</param>
  360. /// <returns>BitmapImage.</returns>
  361. /// <exception cref="System.ArgumentNullException">uri</exception>
  362. public BitmapImage GetBitmapImage(Uri uri)
  363. {
  364. if (uri == null)
  365. {
  366. throw new ArgumentNullException("uri");
  367. }
  368. var bitmap = new BitmapImage
  369. {
  370. CreateOptions = BitmapCreateOptions.DelayCreation,
  371. CacheOption = BitmapCacheOption.OnDemand,
  372. UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable)
  373. };
  374. bitmap.BeginInit();
  375. bitmap.UriSource = uri;
  376. bitmap.EndInit();
  377. RenderOptions.SetBitmapScalingMode(bitmap, BitmapScalingMode.Fant);
  378. return bitmap;
  379. }
  380. }
  381. }