App.xaml.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 MediaBrowser.ServerApplication.Splash;
  10. using Microsoft.Win32;
  11. using System;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Net.Cache;
  15. using System.Threading;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. namespace MediaBrowser.ServerApplication
  21. {
  22. /// <summary>
  23. /// Interaction logic for App.xaml
  24. /// </summary>
  25. public partial class App : Application
  26. {
  27. /// <summary>
  28. /// The single instance mutex
  29. /// </summary>
  30. private static Mutex _singleInstanceMutex;
  31. /// <summary>
  32. /// Defines the entry point of the application.
  33. /// </summary>
  34. [STAThread]
  35. public static void Main()
  36. {
  37. bool createdNew;
  38. var runningPath = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
  39. _singleInstanceMutex = new Mutex(true, @"Local\" + runningPath, out createdNew);
  40. if (!createdNew)
  41. {
  42. _singleInstanceMutex = null;
  43. return;
  44. }
  45. // Look for the existence of an update archive
  46. var appPaths = new ServerApplicationPaths();
  47. var updateArchive = Path.Combine(appPaths.TempUpdatePath, Constants.MbServerPkgName + ".zip");
  48. if (File.Exists(updateArchive))
  49. {
  50. // Update is there - execute update
  51. try
  52. {
  53. new ApplicationUpdater().UpdateApplication(MBApplication.MBServer, appPaths, updateArchive);
  54. // And just let the app exit so it can update
  55. return;
  56. }
  57. catch (Exception e)
  58. {
  59. MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
  60. }
  61. }
  62. var application = new App();
  63. application.Run();
  64. }
  65. /// <summary>
  66. /// Gets the instance.
  67. /// </summary>
  68. /// <value>The instance.</value>
  69. public static App Instance
  70. {
  71. get
  72. {
  73. return Current as App;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets or sets the logger.
  78. /// </summary>
  79. /// <value>The logger.</value>
  80. protected ILogger Logger { get; set; }
  81. /// <summary>
  82. /// Gets or sets the composition root.
  83. /// </summary>
  84. /// <value>The composition root.</value>
  85. protected ApplicationHost CompositionRoot { get; set; }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="App" /> class.
  88. /// </summary>
  89. /// <param name="logger">The logger.</param>
  90. public App()
  91. {
  92. InitializeComponent();
  93. }
  94. /// <summary>
  95. /// Gets the name of the uninstaller file.
  96. /// </summary>
  97. /// <value>The name of the uninstaller file.</value>
  98. protected string UninstallerFileName
  99. {
  100. get { return "MediaBrowser.Server.Uninstall.exe"; }
  101. }
  102. /// <summary>
  103. /// Raises the <see cref="E:System.Windows.Application.Startup" /> event.
  104. /// </summary>
  105. /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param>
  106. protected override void OnStartup(StartupEventArgs e)
  107. {
  108. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  109. LoadKernel();
  110. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  111. }
  112. /// <summary>
  113. /// Handles the UnhandledException event of the CurrentDomain control.
  114. /// </summary>
  115. /// <param name="sender">The source of the event.</param>
  116. /// <param name="e">The <see cref="UnhandledExceptionEventArgs" /> instance containing the event data.</param>
  117. void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  118. {
  119. var exception = (Exception)e.ExceptionObject;
  120. Logger.ErrorException("UnhandledException", exception);
  121. MessageBox.Show("Unhandled exception: " + exception.Message);
  122. if (!Debugger.IsAttached)
  123. {
  124. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  125. }
  126. }
  127. /// <summary>
  128. /// Handles the SessionEnding event of the SystemEvents control.
  129. /// </summary>
  130. /// <param name="sender">The source of the event.</param>
  131. /// <param name="e">The <see cref="SessionEndingEventArgs" /> instance containing the event data.</param>
  132. void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  133. {
  134. // Try to shut down gracefully
  135. Shutdown();
  136. }
  137. /// <summary>
  138. /// Loads the kernel.
  139. /// </summary>
  140. protected async void LoadKernel()
  141. {
  142. try
  143. {
  144. CompositionRoot = new ApplicationHost();
  145. Logger = CompositionRoot.LogManager.GetLogger("App");
  146. var splash = new SplashWindow(CompositionRoot.ApplicationVersion);
  147. splash.Show();
  148. await CompositionRoot.Init();
  149. splash.Hide();
  150. var task = CompositionRoot.RunStartupTasks();
  151. new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesRepository).Show();
  152. await task.ConfigureAwait(false);
  153. }
  154. catch (Exception ex)
  155. {
  156. Logger.ErrorException("Error launching application", ex);
  157. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  158. // Shutdown the app with an error code
  159. Shutdown(1);
  160. }
  161. }
  162. /// <summary>
  163. /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
  164. /// </summary>
  165. /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
  166. protected override void OnExit(ExitEventArgs e)
  167. {
  168. ReleaseMutex();
  169. base.OnExit(e);
  170. if (CompositionRoot != null)
  171. {
  172. CompositionRoot.Dispose();
  173. }
  174. }
  175. /// <summary>
  176. /// Releases the mutex.
  177. /// </summary>
  178. private void ReleaseMutex()
  179. {
  180. if (_singleInstanceMutex == null)
  181. {
  182. return;
  183. }
  184. _singleInstanceMutex.ReleaseMutex();
  185. _singleInstanceMutex.Close();
  186. _singleInstanceMutex.Dispose();
  187. _singleInstanceMutex = null;
  188. }
  189. /// <summary>
  190. /// Opens the dashboard page.
  191. /// </summary>
  192. /// <param name="page">The page.</param>
  193. /// <param name="loggedInUser">The logged in user.</param>
  194. /// <param name="configurationManager">The configuration manager.</param>
  195. /// <param name="appHost">The app host.</param>
  196. public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager, IServerApplicationHost appHost)
  197. {
  198. var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
  199. appHost.WebApplicationName + "/dashboard/" + page;
  200. OpenUrl(url);
  201. }
  202. /// <summary>
  203. /// Opens the URL.
  204. /// </summary>
  205. /// <param name="url">The URL.</param>
  206. public static void OpenUrl(string url)
  207. {
  208. var process = new Process
  209. {
  210. StartInfo = new ProcessStartInfo
  211. {
  212. FileName = url
  213. },
  214. EnableRaisingEvents = true
  215. };
  216. process.Exited += ProcessExited;
  217. try
  218. {
  219. process.Start();
  220. }
  221. catch (Exception ex)
  222. {
  223. MessageBox.Show("There was an error launching your web browser. Please check your defualt browser settings.");
  224. }
  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. }
  247. }