App.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Constants;
  3. using MediaBrowser.Common.Implementations.Updates;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Server.Implementations;
  9. using Microsoft.Win32;
  10. using System;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Net.Cache;
  14. using System.Threading;
  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. // Look for the existence of an update archive
  33. var appPaths = new ServerApplicationPaths();
  34. var updateArchive = Path.Combine(appPaths.TempUpdatePath, Constants.MBServerPkgName + ".zip");
  35. if (File.Exists(updateArchive))
  36. {
  37. // Update is there - execute update
  38. try
  39. {
  40. new ApplicationUpdater().UpdateApplication(MBApplication.MBServer, appPaths, updateArchive);
  41. // And just let the app exit so it can update
  42. return;
  43. }
  44. catch (Exception e)
  45. {
  46. MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
  47. }
  48. }
  49. var application = new App();
  50. application.Run();
  51. }
  52. /// <summary>
  53. /// Gets the instance.
  54. /// </summary>
  55. /// <value>The instance.</value>
  56. public static App Instance
  57. {
  58. get
  59. {
  60. return Current as App;
  61. }
  62. }
  63. /// <summary>
  64. /// The single instance mutex
  65. /// </summary>
  66. private Mutex SingleInstanceMutex;
  67. /// <summary>
  68. /// Gets or sets the logger.
  69. /// </summary>
  70. /// <value>The logger.</value>
  71. protected ILogger Logger { get; set; }
  72. /// <summary>
  73. /// Gets or sets the composition root.
  74. /// </summary>
  75. /// <value>The composition root.</value>
  76. protected ApplicationHost CompositionRoot { get; set; }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="App" /> class.
  79. /// </summary>
  80. /// <param name="logger">The logger.</param>
  81. public App()
  82. {
  83. InitializeComponent();
  84. }
  85. /// <summary>
  86. /// Gets the name of the uninstaller file.
  87. /// </summary>
  88. /// <value>The name of the uninstaller file.</value>
  89. protected string UninstallerFileName
  90. {
  91. get { return "MediaBrowser.Server.Uninstall.exe"; }
  92. }
  93. /// <summary>
  94. /// Raises the <see cref="E:System.Windows.Application.Startup" /> event.
  95. /// </summary>
  96. /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param>
  97. protected override void OnStartup(StartupEventArgs e)
  98. {
  99. bool createdNew;
  100. SingleInstanceMutex = new Mutex(true, @"Local\" + GetType().Assembly.GetName().Name, out createdNew);
  101. if (!createdNew)
  102. {
  103. SingleInstanceMutex = null;
  104. Shutdown();
  105. return;
  106. }
  107. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  108. LoadKernel();
  109. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  110. }
  111. /// <summary>
  112. /// Handles the UnhandledException event of the CurrentDomain control.
  113. /// </summary>
  114. /// <param name="sender">The source of the event.</param>
  115. /// <param name="e">The <see cref="UnhandledExceptionEventArgs" /> instance containing the event data.</param>
  116. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  117. {
  118. var exception = (Exception)e.ExceptionObject;
  119. Logger.ErrorException("UnhandledException", exception);
  120. MessageBox.Show("Unhandled exception: " + exception.Message);
  121. if (!Debugger.IsAttached)
  122. {
  123. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  124. }
  125. }
  126. /// <summary>
  127. /// Handles the SessionEnding event of the SystemEvents control.
  128. /// </summary>
  129. /// <param name="sender">The source of the event.</param>
  130. /// <param name="e">The <see cref="SessionEndingEventArgs" /> instance containing the event data.</param>
  131. void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  132. {
  133. // Try to shut down gracefully
  134. Shutdown();
  135. }
  136. /// <summary>
  137. /// Loads the kernel.
  138. /// </summary>
  139. protected async void LoadKernel()
  140. {
  141. try
  142. {
  143. CompositionRoot = new ApplicationHost();
  144. Logger = CompositionRoot.LogManager.GetLogger("App");
  145. await CompositionRoot.Init();
  146. var win = new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesManager);
  147. win.Show();
  148. }
  149. catch (Exception ex)
  150. {
  151. Logger.ErrorException("Error launching application", ex);
  152. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  153. // Shutdown the app with an error code
  154. Shutdown(1);
  155. }
  156. }
  157. /// <summary>
  158. /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
  159. /// </summary>
  160. /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
  161. protected override void OnExit(ExitEventArgs e)
  162. {
  163. ReleaseMutex();
  164. base.OnExit(e);
  165. CompositionRoot.Dispose();
  166. }
  167. /// <summary>
  168. /// Releases the mutex.
  169. /// </summary>
  170. private void ReleaseMutex()
  171. {
  172. if (SingleInstanceMutex == null)
  173. {
  174. return;
  175. }
  176. SingleInstanceMutex.ReleaseMutex();
  177. SingleInstanceMutex.Close();
  178. SingleInstanceMutex.Dispose();
  179. SingleInstanceMutex = null;
  180. }
  181. /// <summary>
  182. /// Opens the dashboard page.
  183. /// </summary>
  184. /// <param name="page">The page.</param>
  185. public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager)
  186. {
  187. var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
  188. Kernel.Instance.WebApplicationName + "/dashboard/" + page;
  189. if (loggedInUser != null)
  190. {
  191. url = AddAutoLoginToDashboardUrl(url, loggedInUser);
  192. }
  193. OpenUrl(url);
  194. }
  195. /// <summary>
  196. /// Adds the auto login to dashboard URL.
  197. /// </summary>
  198. /// <param name="url">The URL.</param>
  199. /// <param name="user">The user.</param>
  200. /// <returns>System.String.</returns>
  201. public static string AddAutoLoginToDashboardUrl(string url, User user)
  202. {
  203. if (url.IndexOf('?') == -1)
  204. {
  205. url += "?u=" + user.Id;
  206. }
  207. else
  208. {
  209. url += "&u=" + user.Id;
  210. }
  211. return url;
  212. }
  213. /// <summary>
  214. /// Opens the URL.
  215. /// </summary>
  216. /// <param name="url">The URL.</param>
  217. public static void OpenUrl(string url)
  218. {
  219. var process = new Process
  220. {
  221. StartInfo = new ProcessStartInfo
  222. {
  223. FileName = url
  224. },
  225. EnableRaisingEvents = true
  226. };
  227. process.Exited += ProcessExited;
  228. process.Start();
  229. }
  230. /// <summary>
  231. /// Processes the exited.
  232. /// </summary>
  233. /// <param name="sender">The sender.</param>
  234. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  235. static void ProcessExited(object sender, EventArgs e)
  236. {
  237. ((Process)sender).Dispose();
  238. }
  239. /// <summary>
  240. /// Restarts this instance.
  241. /// </summary>
  242. /// <exception cref="System.NotImplementedException"></exception>
  243. public void Restart()
  244. {
  245. Dispatcher.Invoke(ReleaseMutex);
  246. CompositionRoot.Dispose();
  247. System.Windows.Forms.Application.Restart();
  248. Dispatcher.Invoke(Shutdown);
  249. }
  250. /// <summary>
  251. /// Gets the image.
  252. /// </summary>
  253. /// <param name="uri">The URI.</param>
  254. /// <returns>Image.</returns>
  255. /// <exception cref="System.ArgumentNullException">uri</exception>
  256. public Image GetImage(string uri)
  257. {
  258. if (string.IsNullOrEmpty(uri))
  259. {
  260. throw new ArgumentNullException("uri");
  261. }
  262. return GetImage(new Uri(uri));
  263. }
  264. /// <summary>
  265. /// Gets the image.
  266. /// </summary>
  267. /// <param name="uri">The URI.</param>
  268. /// <returns>Image.</returns>
  269. /// <exception cref="System.ArgumentNullException">uri</exception>
  270. public Image GetImage(Uri uri)
  271. {
  272. if (uri == null)
  273. {
  274. throw new ArgumentNullException("uri");
  275. }
  276. return new Image { Source = GetBitmapImage(uri) };
  277. }
  278. /// <summary>
  279. /// Gets the bitmap image.
  280. /// </summary>
  281. /// <param name="uri">The URI.</param>
  282. /// <returns>BitmapImage.</returns>
  283. /// <exception cref="System.ArgumentNullException">uri</exception>
  284. public BitmapImage GetBitmapImage(string uri)
  285. {
  286. if (string.IsNullOrEmpty(uri))
  287. {
  288. throw new ArgumentNullException("uri");
  289. }
  290. return GetBitmapImage(new Uri(uri));
  291. }
  292. /// <summary>
  293. /// Gets the bitmap image.
  294. /// </summary>
  295. /// <param name="uri">The URI.</param>
  296. /// <returns>BitmapImage.</returns>
  297. /// <exception cref="System.ArgumentNullException">uri</exception>
  298. public BitmapImage GetBitmapImage(Uri uri)
  299. {
  300. if (uri == null)
  301. {
  302. throw new ArgumentNullException("uri");
  303. }
  304. var bitmap = new BitmapImage
  305. {
  306. CreateOptions = BitmapCreateOptions.DelayCreation,
  307. CacheOption = BitmapCacheOption.OnDemand,
  308. UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable)
  309. };
  310. bitmap.BeginInit();
  311. bitmap.UriSource = uri;
  312. bitmap.EndInit();
  313. RenderOptions.SetBitmapScalingMode(bitmap, BitmapScalingMode.Fant);
  314. return bitmap;
  315. }
  316. }
  317. }