Program.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. var runningPath = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
  38. //_singleInstanceMutex = new Mutex(true, @"Local\" + runningPath, out createdNew);
  39. createdNew = true;
  40. if (!createdNew)
  41. {
  42. _singleInstanceMutex = null;
  43. logger.Info("Shutting down because another instance of Media Browser Server is already running.");
  44. return;
  45. }
  46. if (PerformUpdateIfNeeded(appPaths, logger))
  47. {
  48. logger.Info("Exiting to perform application update.");
  49. return;
  50. }
  51. try
  52. {
  53. RunApplication(appPaths, logManager);
  54. }
  55. finally
  56. {
  57. logger.Info("Shutting down");
  58. ReleaseMutex(logger);
  59. _appHost.Dispose();
  60. }
  61. }
  62. private static ServerApplicationPaths CreateApplicationPaths()
  63. {
  64. return new ServerApplicationPaths("D:\\MonoTest");
  65. }
  66. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  67. {
  68. // TODO: Show splash here
  69. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  70. _appHost = new ApplicationHost(appPaths, logManager);
  71. var task = _appHost.Init();
  72. Task.WaitAll (task);
  73. task = _appHost.RunStartupTasks();
  74. Task.WaitAll (task);
  75. // TODO: Hide splash here
  76. _mainWindow = new MainWindow ();
  77. // Creation of the Icon
  78. // Creation of the Icon
  79. trayIcon = new StatusIcon(new Pixbuf ("tray.png"));
  80. trayIcon.Visible = true;
  81. // When the TrayIcon has been clicked.
  82. trayIcon.Activate += delegate { };
  83. // Show a pop up menu when the icon has been right clicked.
  84. trayIcon.PopupMenu += OnTrayIconPopup;
  85. // A Tooltip for the Icon
  86. trayIcon.Tooltip = "Media Browser Server";
  87. _mainWindow.ShowAll ();
  88. _mainWindow.Visible = false;
  89. Application.Run ();
  90. }
  91. // Create the popup menu, on right click.
  92. static void OnTrayIconPopup (object o, EventArgs args) {
  93. Menu popupMenu = new Menu();
  94. var menuItemBrowse = new ImageMenuItem ("Browse Library");
  95. menuItemBrowse.Image = new Gtk.Image(Stock.MediaPlay, IconSize.Menu);
  96. popupMenu.Add(menuItemBrowse);
  97. menuItemBrowse.Activated += delegate {
  98. BrowserLauncher.OpenWebClient(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  99. };
  100. var menuItemConfigure = new ImageMenuItem ("Configure Media Browser");
  101. menuItemConfigure.Image = new Gtk.Image(Stock.Edit, IconSize.Menu);
  102. popupMenu.Add(menuItemConfigure);
  103. menuItemConfigure.Activated += delegate {
  104. BrowserLauncher.OpenDashboard(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  105. };
  106. var menuItemApi = new ImageMenuItem ("View Api Docs");
  107. menuItemApi.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  108. popupMenu.Add(menuItemApi);
  109. menuItemApi.Activated += delegate {
  110. BrowserLauncher.OpenSwagger(_appHost.ServerConfigurationManager, _appHost, _logger);
  111. };
  112. var menuItemCommunity = new ImageMenuItem ("Visit Community");
  113. menuItemCommunity.Image = new Gtk.Image(Stock.Help, IconSize.Menu);
  114. popupMenu.Add(menuItemCommunity);
  115. menuItemCommunity.Activated += delegate { BrowserLauncher.OpenCommunity(_logger); };
  116. var menuItemGithub = new ImageMenuItem ("Visit Github");
  117. menuItemGithub.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  118. popupMenu.Add(menuItemGithub);
  119. menuItemGithub.Activated += delegate { BrowserLauncher.OpenGithub(_logger); };
  120. var menuItemQuit = new ImageMenuItem ("Exit");
  121. menuItemQuit.Image = new Gtk.Image(Stock.Quit, IconSize.Menu);
  122. popupMenu.Add(menuItemQuit);
  123. menuItemQuit.Activated += delegate { Shutdown(); };
  124. popupMenu.ShowAll();
  125. popupMenu.Popup();
  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. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  133. {
  134. if (e.Reason == SessionEndReasons.SystemShutdown)
  135. {
  136. Shutdown();
  137. }
  138. }
  139. /// <summary>
  140. /// Begins the log.
  141. /// </summary>
  142. /// <param name="logger">The logger.</param>
  143. private static void BeginLog(ILogger logger)
  144. {
  145. logger.Info("Media Browser Server started");
  146. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  147. logger.Info("Server: {0}", Environment.MachineName);
  148. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  149. }
  150. /// <summary>
  151. /// Handles the UnhandledException event of the CurrentDomain control.
  152. /// </summary>
  153. /// <param name="sender">The source of the event.</param>
  154. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  155. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  156. {
  157. var exception = (Exception)e.ExceptionObject;
  158. _logger.ErrorException("UnhandledException", exception);
  159. if (!Debugger.IsAttached)
  160. {
  161. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  162. }
  163. }
  164. /// <summary>
  165. /// Releases the mutex.
  166. /// </summary>
  167. internal static void ReleaseMutex(ILogger logger)
  168. {
  169. if (_singleInstanceMutex == null)
  170. {
  171. return;
  172. }
  173. logger.Debug("Releasing mutex");
  174. _singleInstanceMutex.ReleaseMutex();
  175. _singleInstanceMutex.Close();
  176. _singleInstanceMutex.Dispose();
  177. _singleInstanceMutex = null;
  178. }
  179. /// <summary>
  180. /// Performs the update if needed.
  181. /// </summary>
  182. /// <param name="appPaths">The app paths.</param>
  183. /// <param name="logger">The logger.</param>
  184. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  185. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  186. {
  187. return false;
  188. }
  189. public static void Shutdown()
  190. {
  191. if (trayIcon != null) {
  192. trayIcon.Visible = false;
  193. trayIcon.Dispose ();
  194. trayIcon = null;
  195. }
  196. if (_mainWindow != null) {
  197. _mainWindow.HideAll ();
  198. _mainWindow.Dispose ();
  199. _mainWindow = null;
  200. }
  201. Application.Quit ();
  202. }
  203. public static void Restart()
  204. {
  205. // Second instance will start first, so release the mutex and dispose the http server ahead of time
  206. ReleaseMutex (_logger);
  207. _appHost.Dispose();
  208. if (trayIcon != null) {
  209. trayIcon.Visible = false;
  210. trayIcon.Dispose ();
  211. trayIcon = null;
  212. }
  213. if (_mainWindow != null) {
  214. _mainWindow.HideAll ();
  215. _mainWindow.Dispose ();
  216. _mainWindow = null;
  217. }
  218. Application.Quit ();
  219. }
  220. }
  221. }