jellyfin-server/MediaBrowser.ServerApplication/App.xaml.cs

159 lines
4.2 KiB
C#
Raw Normal View History

2013-09-21 01:04:14 +00:00
using MediaBrowser.Common.Events;
using MediaBrowser.Controller;
2013-03-07 05:34:00 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2013-02-21 21:06:23 +00:00
using MediaBrowser.Model.Logging;
2013-05-18 22:07:59 +00:00
using MediaBrowser.ServerApplication.Splash;
2013-02-21 01:33:05 +00:00
using System;
using System.Diagnostics;
using System.Windows;
namespace MediaBrowser.ServerApplication
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
2013-09-21 01:04:14 +00:00
public partial class App : Application
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
2013-09-21 01:04:14 +00:00
private readonly ILogger _logger;
/// <summary>
2013-02-24 21:53:54 +00:00
/// Gets or sets the composition root.
/// </summary>
2013-02-24 21:53:54 +00:00
/// <value>The composition root.</value>
2013-09-21 01:04:14 +00:00
private readonly ApplicationHost _appHost;
public event EventHandler AppStarted;
public bool IsRunningAsService { get; private set; }
2013-02-21 21:06:23 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="App" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
2013-09-21 01:04:14 +00:00
public App(ApplicationHost appHost, ILogger logger, bool isRunningAsService)
2013-02-21 21:06:23 +00:00
{
2013-09-21 01:04:14 +00:00
_appHost = appHost;
_logger = logger;
IsRunningAsService = isRunningAsService;
2013-02-21 21:06:23 +00:00
2013-09-21 01:04:14 +00:00
InitializeComponent();
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the name of the uninstaller file.
/// </summary>
/// <value>The name of the uninstaller file.</value>
protected string UninstallerFileName
2013-02-21 01:33:05 +00:00
{
get { return "MediaBrowser.Server.Uninstall.exe"; }
}
public void OnUnhandledException(Exception ex)
2013-02-21 01:33:05 +00:00
{
2013-09-21 01:04:14 +00:00
_logger.ErrorException("UnhandledException", ex);
MessageBox.Show("Unhandled exception: " + ex.Message);
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
LoadApplication();
}
/// <summary>
/// Loads the kernel.
/// </summary>
protected async void LoadApplication()
{
try
{
2013-09-21 01:04:14 +00:00
if (!IsRunningAsService)
{
ShowSplashWindow();
}
2013-05-18 22:07:59 +00:00
2013-09-21 01:04:14 +00:00
await _appHost.Init();
2013-09-21 01:04:14 +00:00
if (!IsRunningAsService)
{
HideSplashWindow();
}
2013-09-21 01:04:14 +00:00
var task = _appHost.RunStartupTasks();
2013-05-18 22:07:59 +00:00
2013-09-21 01:04:14 +00:00
if (!IsRunningAsService)
{
ShowMainWindow();
}
2013-05-18 22:07:59 +00:00
2013-09-21 01:04:14 +00:00
EventHelper.FireEventIfNotNull(AppStarted, this, EventArgs.Empty, _logger);
2013-09-25 18:05:21 +00:00
await task;
}
catch (Exception ex)
{
2013-09-21 01:04:14 +00:00
_logger.ErrorException("Error launching application", ex);
MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
// Shutdown the app with an error code
Shutdown(1);
}
}
2013-09-21 01:04:14 +00:00
private MainWindow _mainWindow;
private void ShowMainWindow()
{
2013-09-21 01:04:14 +00:00
var host = _appHost;
var win = new MainWindow(host.LogManager, host,
host.ServerConfigurationManager, host.UserManager,
host.LibraryManager, host.JsonSerializer,
host.DisplayPreferencesRepository);
win.Show();
_mainWindow = win;
}
2013-09-21 01:04:14 +00:00
private void HideMainWindow()
{
2013-09-21 01:04:14 +00:00
if (_mainWindow != null)
{
_mainWindow.Hide();
_mainWindow = null;
}
}
2013-09-21 01:04:14 +00:00
private SplashWindow _splashWindow;
private void ShowSplashWindow()
{
var win = new SplashWindow(_appHost.ApplicationVersion);
win.Show();
2013-09-21 01:04:14 +00:00
_splashWindow = win;
}
private void HideSplashWindow()
{
if (_splashWindow != null)
2013-04-23 14:53:43 +00:00
{
2013-09-21 01:04:14 +00:00
_splashWindow.Hide();
_splashWindow = null;
2013-04-23 14:53:43 +00:00
}
}
2013-09-21 01:04:14 +00:00
public void ShutdownApplication()
{
Dispatcher.Invoke(Shutdown);
}
2013-02-21 01:33:05 +00:00
}
}