Program.cs 8.0 KB

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