App.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.ServerApplication.Splash;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Windows;
  9. namespace MediaBrowser.ServerApplication
  10. {
  11. /// <summary>
  12. /// Interaction logic for App.xaml
  13. /// </summary>
  14. public partial class App : Application, IApplicationInterface
  15. {
  16. /// <summary>
  17. /// Gets or sets the logger.
  18. /// </summary>
  19. /// <value>The logger.</value>
  20. protected ILogger Logger { get; set; }
  21. /// <summary>
  22. /// Gets or sets the composition root.
  23. /// </summary>
  24. /// <value>The composition root.</value>
  25. protected ApplicationHost CompositionRoot { get; set; }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="App" /> class.
  28. /// </summary>
  29. /// <param name="logger">The logger.</param>
  30. public App()
  31. {
  32. InitializeComponent();
  33. }
  34. public bool IsBackgroundService
  35. {
  36. get { return false; }
  37. }
  38. /// <summary>
  39. /// Gets the name of the uninstaller file.
  40. /// </summary>
  41. /// <value>The name of the uninstaller file.</value>
  42. protected string UninstallerFileName
  43. {
  44. get { return "MediaBrowser.Server.Uninstall.exe"; }
  45. }
  46. public void OnUnhandledException(Exception ex)
  47. {
  48. Logger.ErrorException("UnhandledException", ex);
  49. MessageBox.Show("Unhandled exception: " + ex.Message);
  50. }
  51. protected override void OnStartup(StartupEventArgs e)
  52. {
  53. base.OnStartup(e);
  54. LoadApplication();
  55. }
  56. /// <summary>
  57. /// Loads the kernel.
  58. /// </summary>
  59. protected async void LoadApplication()
  60. {
  61. try
  62. {
  63. CompositionRoot = new ApplicationHost(this);
  64. Logger = CompositionRoot.LogManager.GetLogger("App");
  65. var splash = new SplashWindow(CompositionRoot.ApplicationVersion);
  66. splash.Show();
  67. await CompositionRoot.Init();
  68. splash.Hide();
  69. var task = CompositionRoot.RunStartupTasks();
  70. new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesRepository).Show();
  71. await task.ConfigureAwait(false);
  72. }
  73. catch (Exception ex)
  74. {
  75. Logger.ErrorException("Error launching application", ex);
  76. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  77. // Shutdown the app with an error code
  78. Shutdown(1);
  79. }
  80. }
  81. public void ShutdownApplication()
  82. {
  83. Dispatcher.Invoke(Shutdown);
  84. }
  85. /// <summary>
  86. /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
  87. /// </summary>
  88. /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
  89. protected override void OnExit(ExitEventArgs e)
  90. {
  91. MainStartup.ReleaseMutex();
  92. base.OnExit(e);
  93. if (CompositionRoot != null)
  94. {
  95. CompositionRoot.Dispose();
  96. }
  97. }
  98. /// <summary>
  99. /// Opens the dashboard page.
  100. /// </summary>
  101. /// <param name="page">The page.</param>
  102. /// <param name="loggedInUser">The logged in user.</param>
  103. /// <param name="configurationManager">The configuration manager.</param>
  104. /// <param name="appHost">The app host.</param>
  105. public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager, IServerApplicationHost appHost)
  106. {
  107. var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
  108. appHost.WebApplicationName + "/dashboard/" + page;
  109. OpenUrl(url);
  110. }
  111. /// <summary>
  112. /// Opens the URL.
  113. /// </summary>
  114. /// <param name="url">The URL.</param>
  115. public static void OpenUrl(string url)
  116. {
  117. var process = new Process
  118. {
  119. StartInfo = new ProcessStartInfo
  120. {
  121. FileName = url
  122. },
  123. EnableRaisingEvents = true
  124. };
  125. process.Exited += ProcessExited;
  126. try
  127. {
  128. process.Start();
  129. }
  130. catch (Exception ex)
  131. {
  132. MessageBox.Show("There was an error launching your web browser. Please check your defualt browser settings.");
  133. }
  134. }
  135. /// <summary>
  136. /// Processes the exited.
  137. /// </summary>
  138. /// <param name="sender">The sender.</param>
  139. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  140. static void ProcessExited(object sender, EventArgs e)
  141. {
  142. ((Process)sender).Dispose();
  143. }
  144. /// <summary>
  145. /// Restarts this instance.
  146. /// </summary>
  147. /// <exception cref="System.NotImplementedException"></exception>
  148. public void RestartApplication()
  149. {
  150. Dispatcher.Invoke(MainStartup.ReleaseMutex);
  151. CompositionRoot.Dispose();
  152. System.Windows.Forms.Application.Restart();
  153. Dispatcher.Invoke(Shutdown);
  154. }
  155. }
  156. }