App.xaml.cs 6.0 KB

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