123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using MediaBrowser.Common.Constants;
- using MediaBrowser.Common.Implementations.Logging;
- using MediaBrowser.Common.Implementations.Updates;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Server.Implementations;
- using MediaBrowser.ServerApplication;
- using MediaBrowser.ServerApplication.Native;
- using Microsoft.Win32;
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using System.Windows;
- using Gtk;
- using Gdk;
- using System.Threading.Tasks;
- namespace MediaBrowser.Server.Mono
- {
- public class MainClass
- {
- private static ApplicationHost _appHost;
- private static Mutex _singleInstanceMutex;
- private static ILogger _logger;
- private static MainWindow _mainWindow;
- // The tray Icon
- private static StatusIcon trayIcon;
- public static void Main (string[] args)
- {
- Application.Init ();
- var appPaths = CreateApplicationPaths();
- var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
- logManager.ReloadLogger(LogSeverity.Info);
- var logger = _logger = logManager.GetLogger("Main");
- BeginLog(logger);
- AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
- bool createdNew;
- var runningPath = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
- //_singleInstanceMutex = new Mutex(true, @"Local\" + runningPath, out createdNew);
- createdNew = true;
- if (!createdNew)
- {
- _singleInstanceMutex = null;
- logger.Info("Shutting down because another instance of Media Browser Server is already running.");
- return;
- }
- if (PerformUpdateIfNeeded(appPaths, logger))
- {
- logger.Info("Exiting to perform application update.");
- return;
- }
- try
- {
- RunApplication(appPaths, logManager);
- }
- finally
- {
- logger.Info("Shutting down");
- ReleaseMutex(logger);
- _appHost.Dispose();
- }
- }
- private static ServerApplicationPaths CreateApplicationPaths()
- {
- return new ServerApplicationPaths();
- }
- private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
- {
- // TODO: Show splash here
- SystemEvents.SessionEnding += SystemEvents_SessionEnding;
- _appHost = new ApplicationHost(appPaths, logManager);
- var task = _appHost.Init();
- Task.WaitAll (task);
- task = _appHost.RunStartupTasks();
- Task.WaitAll (task);
- // TODO: Hide splash here
- _mainWindow = new MainWindow ();
- // Creation of the Icon
- // Creation of the Icon
- trayIcon = new StatusIcon(new Pixbuf ("tray.png"));
- trayIcon.Visible = true;
- // When the TrayIcon has been clicked.
- trayIcon.Activate += delegate { };
- // Show a pop up menu when the icon has been right clicked.
- trayIcon.PopupMenu += OnTrayIconPopup;
- // A Tooltip for the Icon
- trayIcon.Tooltip = "Media Browser Server";
- _mainWindow.ShowAll ();
- _mainWindow.Visible = false;
- Application.Run ();
- }
- // Create the popup menu, on right click.
- static void OnTrayIconPopup (object o, EventArgs args) {
- Menu popupMenu = new Menu();
- var menuItemBrowse = new ImageMenuItem ("Browse Library");
- menuItemBrowse.Image = new Gtk.Image(Stock.MediaPlay, IconSize.Menu);
- popupMenu.Add(menuItemBrowse);
- menuItemBrowse.Activated += delegate {
- BrowserLauncher.OpenWebClient(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
- };
- var menuItemConfigure = new ImageMenuItem ("Configure Media Browser");
- menuItemConfigure.Image = new Gtk.Image(Stock.Edit, IconSize.Menu);
- popupMenu.Add(menuItemConfigure);
- menuItemConfigure.Activated += delegate {
- BrowserLauncher.OpenDashboard(_appHost.UserManager, _appHost.ServerConfigurationManager, _appHost, _logger);
- };
- var menuItemApi = new ImageMenuItem ("View Api Docs");
- menuItemApi.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
- popupMenu.Add(menuItemApi);
- menuItemApi.Activated += delegate {
- BrowserLauncher.OpenSwagger(_appHost.ServerConfigurationManager, _appHost, _logger);
- };
- var menuItemCommunity = new ImageMenuItem ("Visit Community");
- menuItemCommunity.Image = new Gtk.Image(Stock.Help, IconSize.Menu);
- popupMenu.Add(menuItemCommunity);
- menuItemCommunity.Activated += delegate { BrowserLauncher.OpenCommunity(_logger); };
- var menuItemGithub = new ImageMenuItem ("Visit Github");
- menuItemGithub.Image = new Gtk.Image(Stock.Network, IconSize.Menu);
- popupMenu.Add(menuItemGithub);
- menuItemGithub.Activated += delegate { BrowserLauncher.OpenGithub(_logger); };
- var menuItemQuit = new ImageMenuItem ("Exit");
- menuItemQuit.Image = new Gtk.Image(Stock.Quit, IconSize.Menu);
- popupMenu.Add(menuItemQuit);
- menuItemQuit.Activated += delegate { Shutdown(); };
- popupMenu.ShowAll();
- popupMenu.Popup();
- }
- /// <summary>
- /// Handles the SessionEnding event of the SystemEvents control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
- static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
- {
- if (e.Reason == SessionEndReasons.SystemShutdown)
- {
- Shutdown();
- }
- }
- /// <summary>
- /// Begins the log.
- /// </summary>
- /// <param name="logger">The logger.</param>
- private static void BeginLog(ILogger logger)
- {
- logger.Info("Media Browser Server started");
- logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
- logger.Info("Server: {0}", Environment.MachineName);
- logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
- }
- /// <summary>
- /// Handles the UnhandledException event of the CurrentDomain control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
- static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- var exception = (Exception)e.ExceptionObject;
- _logger.ErrorException("UnhandledException", exception);
- if (!Debugger.IsAttached)
- {
- Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
- }
- }
- /// <summary>
- /// Releases the mutex.
- /// </summary>
- internal static void ReleaseMutex(ILogger logger)
- {
- if (_singleInstanceMutex == null)
- {
- return;
- }
- logger.Debug("Releasing mutex");
- _singleInstanceMutex.ReleaseMutex();
- _singleInstanceMutex.Close();
- _singleInstanceMutex.Dispose();
- _singleInstanceMutex = null;
- }
- /// <summary>
- /// Performs the update if needed.
- /// </summary>
- /// <param name="appPaths">The app paths.</param>
- /// <param name="logger">The logger.</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
- private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
- {
- return false;
- }
- public static void Shutdown()
- {
- if (trayIcon != null) {
- trayIcon.Visible = false;
- trayIcon.Dispose ();
- trayIcon = null;
- }
- if (_mainWindow != null) {
- _mainWindow.HideAll ();
- _mainWindow.Dispose ();
- _mainWindow = null;
- }
- Application.Quit ();
- }
- public static void Restart()
- {
- // Second instance will start first, so release the mutex and dispose the http server ahead of time
- ReleaseMutex (_logger);
- _appHost.Dispose();
- if (trayIcon != null) {
- trayIcon.Visible = false;
- trayIcon.Dispose ();
- trayIcon = null;
- }
- if (_mainWindow != null) {
- _mainWindow.HideAll ();
- _mainWindow.Dispose ();
- _mainWindow = null;
- }
- Application.Quit ();
- }
- }
- }
|