jellyfin/MediaBrowser.ServerApplication/App.xaml.cs

160 lines
4.3 KiB
C#
Raw Normal View History

2013-09-21 03:04:14 +02:00
using MediaBrowser.Common.Events;
using MediaBrowser.Controller;
2013-03-07 06:34:00 +01:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2013-02-21 22:06:23 +01:00
using MediaBrowser.Model.Logging;
2013-05-19 00:07:59 +02:00
using MediaBrowser.ServerApplication.Splash;
2013-02-21 02:33:05 +01:00
using System;
using System.Diagnostics;
using System.Windows;
namespace MediaBrowser.ServerApplication
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
2013-09-21 03:04:14 +02:00
public partial class App : Application
2013-02-21 02:33:05 +01:00
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
2013-09-21 03:04:14 +02:00
private readonly ILogger _logger;
/// <summary>
2013-02-24 22:53:54 +01:00
/// Gets or sets the composition root.
/// </summary>
2013-02-24 22:53:54 +01:00
/// <value>The composition root.</value>
2013-09-21 03:04:14 +02:00
private readonly ApplicationHost _appHost;
public event EventHandler AppStarted;
public bool IsRunningAsService { get; private set; }
2013-02-21 22:06:23 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="App" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
2013-09-21 03:04:14 +02:00
public App(ApplicationHost appHost, ILogger logger, bool isRunningAsService)
2013-02-21 22:06:23 +01:00
{
2013-09-21 03:04:14 +02:00
_appHost = appHost;
_logger = logger;
IsRunningAsService = isRunningAsService;
2013-02-21 22:06:23 +01:00
2013-09-21 03:04:14 +02:00
InitializeComponent();
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the name of the uninstaller file.
/// </summary>
/// <value>The name of the uninstaller file.</value>
protected string UninstallerFileName
2013-02-21 02:33:05 +01:00
{
get { return "MediaBrowser.Server.Uninstall.exe"; }
}
public void OnUnhandledException(Exception ex)
2013-02-21 02:33:05 +01:00
{
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
{
var initProgress = new Progress<double>();
2013-09-21 03:04:14 +02:00
if (!IsRunningAsService)
{
ShowSplashWindow(initProgress);
2013-09-21 03:04:14 +02:00
}
2013-05-19 00:07:59 +02:00
await _appHost.Init(initProgress);
2013-10-01 00:18:44 +02:00
var task = _appHost.RunStartupTasks();
2013-09-21 03:04:14 +02:00
if (!IsRunningAsService)
{
HideSplashWindow();
}
2013-09-21 03:04:14 +02:00
if (!IsRunningAsService)
{
ShowMainWindow();
}
2013-05-19 00:07:59 +02:00
2013-09-21 03:04:14 +02:00
EventHelper.FireEventIfNotNull(AppStarted, this, EventArgs.Empty, _logger);
2013-09-25 20:05:21 +02:00
await task;
}
catch (Exception ex)
{
2013-09-21 03:04:14 +02: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 03:04:14 +02:00
private MainWindow _mainWindow;
private void ShowMainWindow()
{
2013-09-21 03:04:14 +02:00
var host = _appHost;
var win = new MainWindow(host.LogManager, host,
host.ServerConfigurationManager, host.UserManager,
host.LibraryManager, host.JsonSerializer,
host.DisplayPreferencesRepository,
host.ItemRepository);
2013-09-21 03:04:14 +02:00
win.Show();
_mainWindow = win;
}
2013-09-21 03:04:14 +02:00
private void HideMainWindow()
{
2013-09-21 03:04:14 +02:00
if (_mainWindow != null)
{
_mainWindow.Hide();
_mainWindow = null;
}
}
2013-09-21 03:04:14 +02:00
private SplashWindow _splashWindow;
private void ShowSplashWindow(Progress<double> progress)
2013-09-21 03:04:14 +02:00
{
var win = new SplashWindow(_appHost.ApplicationVersion, progress);
2013-09-21 03:04:14 +02:00
win.Show();
2013-09-21 03:04:14 +02:00
_splashWindow = win;
}
private void HideSplashWindow()
{
if (_splashWindow != null)
2013-04-23 16:53:43 +02:00
{
2013-09-21 03:04:14 +02:00
_splashWindow.Hide();
_splashWindow = null;
2013-04-23 16:53:43 +02:00
}
}
2013-09-21 03:04:14 +02:00
public void ShutdownApplication()
{
Dispatcher.Invoke(Shutdown);
}
2013-02-21 02:33:05 +01:00
}
}