Program.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using MediaBrowser.Common.Constants;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Common.Implementations.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Implementations;
  6. using MediaBrowser.ServerApplication;
  7. using MediaBrowser.ServerApplication.Native;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Windows;
  14. using Gtk;
  15. using Gdk;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Server.Mono
  18. {
  19. public class MainClass
  20. {
  21. private static ApplicationHost _appHost;
  22. private static Mutex _singleInstanceMutex;
  23. private static ILogger _logger;
  24. private static MainWindow _mainWindow;
  25. // The tray Icon
  26. private static StatusIcon trayIcon;
  27. public static void Main (string[] args)
  28. {
  29. Application.Init ();
  30. var appPaths = CreateApplicationPaths();
  31. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  32. logManager.ReloadLogger(LogSeverity.Info);
  33. var logger = _logger = logManager.GetLogger("Main");
  34. BeginLog(logger);
  35. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  36. bool createdNew;
  37. //_singleInstanceMutex = new Mutex(true, @"Local\" + runningPath, out createdNew);
  38. createdNew = true;
  39. if (!createdNew)
  40. {
  41. _singleInstanceMutex = null;
  42. logger.Info("Shutting down because another instance of Media Browser Server is already running.");
  43. return;
  44. }
  45. if (PerformUpdateIfNeeded(appPaths, logger))
  46. {
  47. logger.Info("Exiting to perform application update.");
  48. return;
  49. }
  50. try
  51. {
  52. RunApplication(appPaths, logManager);
  53. }
  54. finally
  55. {
  56. logger.Info("Shutting down");
  57. ReleaseMutex(logger);
  58. _appHost.Dispose();
  59. }
  60. }
  61. private static ServerApplicationPaths CreateApplicationPaths()
  62. {
  63. return new ServerApplicationPaths();
  64. }
  65. /// <summary>
  66. /// Determines whether this instance [can self restart].
  67. /// </summary>
  68. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  69. public static bool CanSelfRestart
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets a value indicating whether this instance can self update.
  78. /// </summary>
  79. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  80. public static bool CanSelfUpdate
  81. {
  82. get
  83. {
  84. return false;
  85. }
  86. }
  87. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  88. {
  89. // TODO: Show splash here
  90. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  91. _appHost = new ApplicationHost(appPaths, logManager);
  92. var task = _appHost.Init();
  93. Task.WaitAll (task);
  94. task = _appHost.RunStartupTasks();
  95. Task.WaitAll (task);
  96. // TODO: Hide splash here
  97. _mainWindow = new MainWindow ();
  98. // Creation of the Icon
  99. // Creation of the Icon
  100. trayIcon = new StatusIcon(new Pixbuf ("tray.png"));
  101. trayIcon.Visible = true;
  102. // When the TrayIcon has been clicked.
  103. trayIcon.Activate += delegate { };
  104. // Show a pop up menu when the icon has been right clicked.
  105. trayIcon.PopupMenu += OnTrayIconPopup;
  106. // A Tooltip for the Icon
  107. trayIcon.Tooltip = "Media Browser Server";
  108. _mainWindow.ShowAll ();
  109. _mainWindow.Visible = false;
  110. Application.Run ();
  111. }
  112. // Create the popup menu, on right click.
  113. static void OnTrayIconPopup (object o, EventArgs args) {
  114. Menu popupMenu = new Menu();
  115. var menuItemBrowse = new ImageMenuItem ("Browse Library");
  116. menuItemBrowse.Image = new Gtk.Image(Stock.MediaPlay, IconSize.Menu);
  117. popupMenu.Add(menuItemBrowse);
  118. menuItemBrowse.Activated += delegate {
  119. BrowserLauncher.OpenWebClient(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  120. };
  121. var menuItemConfigure = new ImageMenuItem ("Configure Media Browser");
  122. menuItemConfigure.Image = new Gtk.Image(Stock.Edit, IconSize.Menu);
  123. popupMenu.Add(menuItemConfigure);
  124. menuItemConfigure.Activated += delegate {
  125. BrowserLauncher.OpenDashboard(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  126. };
  127. var menuItemApi = new ImageMenuItem ("View Api Docs");
  128. menuItemApi.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  129. popupMenu.Add(menuItemApi);
  130. menuItemApi.Activated += delegate {
  131. BrowserLauncher.OpenSwagger(_appHost.ServerConfigurationManager, _appHost, _logger);
  132. };
  133. var menuItemCommunity = new ImageMenuItem ("Visit Community");
  134. menuItemCommunity.Image = new Gtk.Image(Stock.Help, IconSize.Menu);
  135. popupMenu.Add(menuItemCommunity);
  136. menuItemCommunity.Activated += delegate { BrowserLauncher.OpenCommunity(_logger); };
  137. var menuItemGithub = new ImageMenuItem ("Visit Github");
  138. menuItemGithub.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  139. popupMenu.Add(menuItemGithub);
  140. menuItemGithub.Activated += delegate { BrowserLauncher.OpenGithub(_logger); };
  141. var menuItemQuit = new ImageMenuItem ("Exit");
  142. menuItemQuit.Image = new Gtk.Image(Stock.Quit, IconSize.Menu);
  143. popupMenu.Add(menuItemQuit);
  144. menuItemQuit.Activated += delegate { Shutdown(); };
  145. popupMenu.ShowAll();
  146. popupMenu.Popup();
  147. }
  148. /// <summary>
  149. /// Handles the SessionEnding event of the SystemEvents control.
  150. /// </summary>
  151. /// <param name="sender">The source of the event.</param>
  152. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  153. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  154. {
  155. if (e.Reason == SessionEndReasons.SystemShutdown)
  156. {
  157. Shutdown();
  158. }
  159. }
  160. /// <summary>
  161. /// Begins the log.
  162. /// </summary>
  163. /// <param name="logger">The logger.</param>
  164. private static void BeginLog(ILogger logger)
  165. {
  166. logger.Info("Media Browser Server started");
  167. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  168. logger.Info("Server: {0}", Environment.MachineName);
  169. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  170. }
  171. /// <summary>
  172. /// Handles the UnhandledException event of the CurrentDomain control.
  173. /// </summary>
  174. /// <param name="sender">The source of the event.</param>
  175. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  176. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  177. {
  178. var exception = (Exception)e.ExceptionObject;
  179. _logger.ErrorException("UnhandledException", exception);
  180. if (!Debugger.IsAttached)
  181. {
  182. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  183. }
  184. }
  185. /// <summary>
  186. /// Releases the mutex.
  187. /// </summary>
  188. internal static void ReleaseMutex(ILogger logger)
  189. {
  190. if (_singleInstanceMutex == null)
  191. {
  192. return;
  193. }
  194. logger.Debug("Releasing mutex");
  195. _singleInstanceMutex.ReleaseMutex();
  196. _singleInstanceMutex.Close();
  197. _singleInstanceMutex.Dispose();
  198. _singleInstanceMutex = null;
  199. }
  200. /// <summary>
  201. /// Performs the update if needed.
  202. /// </summary>
  203. /// <param name="appPaths">The app paths.</param>
  204. /// <param name="logger">The logger.</param>
  205. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  206. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  207. {
  208. return false;
  209. }
  210. public static void Shutdown()
  211. {
  212. if (trayIcon != null) {
  213. trayIcon.Visible = false;
  214. trayIcon.Dispose ();
  215. trayIcon = null;
  216. }
  217. if (_mainWindow != null) {
  218. _mainWindow.HideAll ();
  219. _mainWindow.Dispose ();
  220. _mainWindow = null;
  221. }
  222. Application.Quit ();
  223. }
  224. public static void Restart()
  225. {
  226. // Second instance will start first, so release the mutex and dispose the http server ahead of time
  227. ReleaseMutex (_logger);
  228. _appHost.Dispose();
  229. if (trayIcon != null) {
  230. trayIcon.Visible = false;
  231. trayIcon.Dispose ();
  232. trayIcon = null;
  233. }
  234. if (_mainWindow != null) {
  235. _mainWindow.HideAll ();
  236. _mainWindow.Dispose ();
  237. _mainWindow = null;
  238. }
  239. Application.Quit ();
  240. }
  241. }
  242. }