App.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. }
  122. /// <summary>
  123. /// Handles the SessionEnding event of the SystemEvents control.
  124. /// </summary>
  125. /// <param name="sender">The source of the event.</param>
  126. /// <param name="e">The <see cref="SessionEndingEventArgs" /> instance containing the event data.</param>
  127. void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  128. {
  129. // Try to shut down gracefully
  130. Shutdown();
  131. }
  132. /// <summary>
  133. /// Loads the kernel.
  134. /// </summary>
  135. protected async void LoadKernel()
  136. {
  137. try
  138. {
  139. CompositionRoot = new ApplicationHost();
  140. var win = new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager);
  141. Logger = CompositionRoot.LogManager.GetLogger("App");
  142. win.Show();
  143. await CompositionRoot.Init();
  144. }
  145. catch (Exception ex)
  146. {
  147. Logger.ErrorException("Error launching application", ex);
  148. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  149. // Shutdown the app with an error code
  150. Shutdown(1);
  151. }
  152. }
  153. /// <summary>
  154. /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
  155. /// </summary>
  156. /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
  157. protected override void OnExit(ExitEventArgs e)
  158. {
  159. ReleaseMutex();
  160. base.OnExit(e);
  161. CompositionRoot.Dispose();
  162. }
  163. /// <summary>
  164. /// Releases the mutex.
  165. /// </summary>
  166. private void ReleaseMutex()
  167. {
  168. if (SingleInstanceMutex == null)
  169. {
  170. return;
  171. }
  172. SingleInstanceMutex.ReleaseMutex();
  173. SingleInstanceMutex.Close();
  174. SingleInstanceMutex.Dispose();
  175. SingleInstanceMutex = null;
  176. }
  177. /// <summary>
  178. /// Opens the dashboard page.
  179. /// </summary>
  180. /// <param name="page">The page.</param>
  181. public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager)
  182. {
  183. var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
  184. Kernel.Instance.WebApplicationName + "/dashboard/" + page;
  185. if (loggedInUser != null)
  186. {
  187. url = AddAutoLoginToDashboardUrl(url, loggedInUser);
  188. }
  189. OpenUrl(url);
  190. }
  191. /// <summary>
  192. /// Adds the auto login to dashboard URL.
  193. /// </summary>
  194. /// <param name="url">The URL.</param>
  195. /// <param name="user">The user.</param>
  196. /// <returns>System.String.</returns>
  197. public static string AddAutoLoginToDashboardUrl(string url, User user)
  198. {
  199. if (url.IndexOf('?') == -1)
  200. {
  201. url += "?u=" + user.Id;
  202. }
  203. else
  204. {
  205. url += "&u=" + user.Id;
  206. }
  207. return url;
  208. }
  209. /// <summary>
  210. /// Opens the URL.
  211. /// </summary>
  212. /// <param name="url">The URL.</param>
  213. public static void OpenUrl(string url)
  214. {
  215. var process = new Process
  216. {
  217. StartInfo = new ProcessStartInfo
  218. {
  219. FileName = url
  220. },
  221. EnableRaisingEvents = true
  222. };
  223. process.Exited += ProcessExited;
  224. process.Start();
  225. }
  226. /// <summary>
  227. /// Processes the exited.
  228. /// </summary>
  229. /// <param name="sender">The sender.</param>
  230. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  231. static void ProcessExited(object sender, EventArgs e)
  232. {
  233. ((Process)sender).Dispose();
  234. }
  235. /// <summary>
  236. /// Restarts this instance.
  237. /// </summary>
  238. /// <exception cref="System.NotImplementedException"></exception>
  239. public void Restart()
  240. {
  241. Dispatcher.Invoke(ReleaseMutex);
  242. CompositionRoot.Dispose();
  243. System.Windows.Forms.Application.Restart();
  244. Dispatcher.Invoke(Shutdown);
  245. }
  246. /// <summary>
  247. /// Gets the image.
  248. /// </summary>
  249. /// <param name="uri">The URI.</param>
  250. /// <returns>Image.</returns>
  251. /// <exception cref="System.ArgumentNullException">uri</exception>
  252. public Image GetImage(string uri)
  253. {
  254. if (string.IsNullOrEmpty(uri))
  255. {
  256. throw new ArgumentNullException("uri");
  257. }
  258. return GetImage(new Uri(uri));
  259. }
  260. /// <summary>
  261. /// Gets the image.
  262. /// </summary>
  263. /// <param name="uri">The URI.</param>
  264. /// <returns>Image.</returns>
  265. /// <exception cref="System.ArgumentNullException">uri</exception>
  266. public Image GetImage(Uri uri)
  267. {
  268. if (uri == null)
  269. {
  270. throw new ArgumentNullException("uri");
  271. }
  272. return new Image { Source = GetBitmapImage(uri) };
  273. }
  274. /// <summary>
  275. /// Gets the bitmap image.
  276. /// </summary>
  277. /// <param name="uri">The URI.</param>
  278. /// <returns>BitmapImage.</returns>
  279. /// <exception cref="System.ArgumentNullException">uri</exception>
  280. public BitmapImage GetBitmapImage(string uri)
  281. {
  282. if (string.IsNullOrEmpty(uri))
  283. {
  284. throw new ArgumentNullException("uri");
  285. }
  286. return GetBitmapImage(new Uri(uri));
  287. }
  288. /// <summary>
  289. /// Gets the bitmap image.
  290. /// </summary>
  291. /// <param name="uri">The URI.</param>
  292. /// <returns>BitmapImage.</returns>
  293. /// <exception cref="System.ArgumentNullException">uri</exception>
  294. public BitmapImage GetBitmapImage(Uri uri)
  295. {
  296. if (uri == null)
  297. {
  298. throw new ArgumentNullException("uri");
  299. }
  300. var bitmap = new BitmapImage
  301. {
  302. CreateOptions = BitmapCreateOptions.DelayCreation,
  303. CacheOption = BitmapCacheOption.OnDemand,
  304. UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable)
  305. };
  306. bitmap.BeginInit();
  307. bitmap.UriSource = uri;
  308. bitmap.EndInit();
  309. RenderOptions.SetBitmapScalingMode(bitmap, BitmapScalingMode.Fant);
  310. return bitmap;
  311. }
  312. }
  313. }