jellyfin-server/MediaBrowser.Common/UI/BaseApplication.cs

124 lines
3.5 KiB
C#
Raw Normal View History

2012-09-13 20:13:43 +00:00
using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Logging;
using MediaBrowser.Model.Progress;
2012-09-13 20:13:43 +00:00
using Microsoft.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace MediaBrowser.Common.UI
{
2012-08-20 00:15:09 +00:00
/// <summary>
/// Serves as a base Application class for both the UI and Server apps.
/// </summary>
2012-09-13 20:13:43 +00:00
public abstract class BaseApplication : Application, INotifyPropertyChanged, ISingleInstanceApp
{
private IKernel Kernel { get; set; }
protected abstract IKernel InstantiateKernel();
protected abstract Window InstantiateMainWindow();
2012-09-13 20:13:43 +00:00
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
2012-08-22 02:50:59 +00:00
protected override void OnStartup(StartupEventArgs e)
{
2012-08-19 22:50:54 +00:00
// Without this the app will shutdown after the splash screen closes
2012-09-11 18:20:12 +00:00
ShutdownMode = ShutdownMode.OnExplicitShutdown;
2012-08-22 02:50:59 +00:00
LoadKernel();
}
2012-08-22 02:50:59 +00:00
private async void LoadKernel()
{
Kernel = InstantiateKernel();
2012-09-11 19:37:14 +00:00
var progress = new Progress<TaskProgress>();
2012-09-11 19:37:14 +00:00
var splash = new Splash(progress);
splash.Show();
try
{
2012-09-04 19:23:15 +00:00
DateTime now = DateTime.UtcNow;
await Kernel.Init(progress);
2012-09-04 19:23:15 +00:00
Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
splash.Close();
2012-09-11 18:20:12 +00:00
ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
2012-09-13 20:13:43 +00:00
OnKernelLoaded();
InstantiateMainWindow().Show();
}
catch (Exception ex)
{
Logger.LogException(ex);
MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
splash.Close();
2012-08-19 22:50:54 +00:00
// Shutdown the app with an error code
Shutdown(1);
}
}
2012-09-13 20:13:43 +00:00
protected virtual void OnKernelLoaded()
{
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
Kernel.Dispose();
}
2012-09-13 20:13:43 +00:00
public bool SignalExternalCommandLineArgs(IList<string> args)
{
OnSecondInstanceLaunched(args);
return true;
}
protected virtual void OnSecondInstanceLaunched(IList<string> args)
{
if (this.MainWindow.WindowState == WindowState.Minimized)
{
this.MainWindow.WindowState = WindowState.Maximized;
}
}
public static void RunApplication<TApplicationType>(string uniqueKey)
where TApplicationType : BaseApplication, IApplication, new()
{
if (SingleInstance<TApplicationType>.InitializeAsFirstInstance(uniqueKey))
{
var application = new TApplicationType();
application.InitializeComponent();
application.Run();
// Allow single instance code to perform cleanup operations
SingleInstance<TApplicationType>.Cleanup();
}
}
}
public interface IApplication
{
void InitializeComponent();
}
}