jellyfin/MediaBrowser.ServerApplication/App.xaml.cs

212 lines
6.0 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-05-18 22:07:59 +00:00
await task.ConfigureAwait(false);
}
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
/// <summary>
/// Opens the dashboard page.
/// </summary>
/// <param name="page">The page.</param>
2013-06-03 18:15:35 +00:00
/// <param name="loggedInUser">The logged in user.</param>
/// <param name="configurationManager">The configuration manager.</param>
/// <param name="appHost">The app host.</param>
public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager, IServerApplicationHost appHost)
2013-02-21 01:33:05 +00:00
{
2013-03-07 05:34:00 +00:00
var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
2013-06-03 18:15:35 +00:00
appHost.WebApplicationName + "/dashboard/" + page;
2013-02-21 01:33:05 +00:00
OpenUrl(url);
}
/// <summary>
/// Opens the URL.
/// </summary>
/// <param name="url">The URL.</param>
public static void OpenUrl(string url)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = url
},
EnableRaisingEvents = true
};
process.Exited += ProcessExited;
2013-07-05 13:47:10 +00:00
try
{
process.Start();
}
catch (Exception ex)
{
MessageBox.Show("There was an error launching your web browser. Please check your defualt browser settings.");
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Processes the exited.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
static void ProcessExited(object sender, EventArgs e)
{
((Process)sender).Dispose();
}
}
}