Program.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ILogger _logger;
  23. private static MainWindow _mainWindow;
  24. // The tray Icon
  25. private static StatusIcon trayIcon;
  26. public static void Main (string[] args)
  27. {
  28. Application.Init ();
  29. var appPaths = CreateApplicationPaths();
  30. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  31. logManager.ReloadLogger(LogSeverity.Info);
  32. var logger = _logger = logManager.GetLogger("Main");
  33. BeginLog(logger);
  34. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  35. if (PerformUpdateIfNeeded(appPaths, logger))
  36. {
  37. logger.Info("Exiting to perform application update.");
  38. return;
  39. }
  40. try
  41. {
  42. RunApplication(appPaths, logManager);
  43. }
  44. finally
  45. {
  46. logger.Info("Shutting down");
  47. _appHost.Dispose();
  48. }
  49. }
  50. private static ServerApplicationPaths CreateApplicationPaths()
  51. {
  52. return new ServerApplicationPaths();
  53. }
  54. /// <summary>
  55. /// Determines whether this instance [can self restart].
  56. /// </summary>
  57. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  58. public static bool CanSelfRestart
  59. {
  60. get
  61. {
  62. return false;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance can self update.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  69. public static bool CanSelfUpdate
  70. {
  71. get
  72. {
  73. return false;
  74. }
  75. }
  76. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  77. {
  78. // TODO: Show splash here
  79. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  80. _appHost = new ApplicationHost(appPaths, logManager);
  81. var task = _appHost.Init();
  82. Task.WaitAll (task);
  83. task = _appHost.RunStartupTasks();
  84. Task.WaitAll (task);
  85. // TODO: Hide splash here
  86. _mainWindow = new MainWindow ();
  87. // Creation of the Icon
  88. // Creation of the Icon
  89. trayIcon = new StatusIcon(new Pixbuf ("tray.png"));
  90. trayIcon.Visible = true;
  91. // When the TrayIcon has been clicked.
  92. trayIcon.Activate += delegate { };
  93. // Show a pop up menu when the icon has been right clicked.
  94. trayIcon.PopupMenu += OnTrayIconPopup;
  95. // A Tooltip for the Icon
  96. trayIcon.Tooltip = "Media Browser Server";
  97. _mainWindow.ShowAll ();
  98. _mainWindow.Visible = false;
  99. Application.Run ();
  100. }
  101. // Create the popup menu, on right click.
  102. static void OnTrayIconPopup (object o, EventArgs args) {
  103. Menu popupMenu = new Menu();
  104. var menuItemBrowse = new ImageMenuItem ("Browse Library");
  105. menuItemBrowse.Image = new Gtk.Image(Stock.MediaPlay, IconSize.Menu);
  106. popupMenu.Add(menuItemBrowse);
  107. menuItemBrowse.Activated += delegate {
  108. BrowserLauncher.OpenWebClient(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  109. };
  110. var menuItemConfigure = new ImageMenuItem ("Configure Media Browser");
  111. menuItemConfigure.Image = new Gtk.Image(Stock.Edit, IconSize.Menu);
  112. popupMenu.Add(menuItemConfigure);
  113. menuItemConfigure.Activated += delegate {
  114. BrowserLauncher.OpenDashboard(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
  115. };
  116. var menuItemApi = new ImageMenuItem ("View Api Docs");
  117. menuItemApi.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  118. popupMenu.Add(menuItemApi);
  119. menuItemApi.Activated += delegate {
  120. BrowserLauncher.OpenSwagger(_appHost.ServerConfigurationManager, _appHost, _logger);
  121. };
  122. var menuItemCommunity = new ImageMenuItem ("Visit Community");
  123. menuItemCommunity.Image = new Gtk.Image(Stock.Help, IconSize.Menu);
  124. popupMenu.Add(menuItemCommunity);
  125. menuItemCommunity.Activated += delegate { BrowserLauncher.OpenCommunity(_logger); };
  126. var menuItemGithub = new ImageMenuItem ("Visit Github");
  127. menuItemGithub.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
  128. popupMenu.Add(menuItemGithub);
  129. menuItemGithub.Activated += delegate { BrowserLauncher.OpenGithub(_logger); };
  130. var menuItemQuit = new ImageMenuItem ("Exit");
  131. menuItemQuit.Image = new Gtk.Image(Stock.Quit, IconSize.Menu);
  132. popupMenu.Add(menuItemQuit);
  133. menuItemQuit.Activated += delegate { Shutdown(); };
  134. popupMenu.ShowAll();
  135. popupMenu.Popup();
  136. }
  137. /// <summary>
  138. /// Handles the SessionEnding event of the SystemEvents control.
  139. /// </summary>
  140. /// <param name="sender">The source of the event.</param>
  141. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  142. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  143. {
  144. if (e.Reason == SessionEndReasons.SystemShutdown)
  145. {
  146. Shutdown();
  147. }
  148. }
  149. /// <summary>
  150. /// Begins the log.
  151. /// </summary>
  152. /// <param name="logger">The logger.</param>
  153. private static void BeginLog(ILogger logger)
  154. {
  155. logger.Info("Media Browser Server started");
  156. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  157. logger.Info("Server: {0}", Environment.MachineName);
  158. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  159. }
  160. /// <summary>
  161. /// Handles the UnhandledException event of the CurrentDomain control.
  162. /// </summary>
  163. /// <param name="sender">The source of the event.</param>
  164. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  165. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  166. {
  167. var exception = (Exception)e.ExceptionObject;
  168. _logger.ErrorException("UnhandledException", exception);
  169. if (!Debugger.IsAttached)
  170. {
  171. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  172. }
  173. }
  174. /// <summary>
  175. /// Performs the update if needed.
  176. /// </summary>
  177. /// <param name="appPaths">The app paths.</param>
  178. /// <param name="logger">The logger.</param>
  179. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  180. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  181. {
  182. return false;
  183. }
  184. public static void Shutdown()
  185. {
  186. if (trayIcon != null) {
  187. trayIcon.Visible = false;
  188. trayIcon.Dispose ();
  189. trayIcon = null;
  190. }
  191. if (_mainWindow != null) {
  192. _mainWindow.HideAll ();
  193. _mainWindow.Dispose ();
  194. _mainWindow = null;
  195. }
  196. Application.Quit ();
  197. }
  198. public static void Restart()
  199. {
  200. // Second instance will start first, so dispose so that the http ports will be available to the new instance
  201. _appHost.Dispose();
  202. // Right now this method will just shutdown, but not restart
  203. Shutdown ();
  204. }
  205. }
  206. }