2
0

App.xaml.cs 11 KB

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