BaseApplication.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Model.Progress;
  4. using Microsoft.Shell;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Windows;
  9. namespace MediaBrowser.Common.UI
  10. {
  11. /// <summary>
  12. /// Serves as a base Application class for both the UI and Server apps.
  13. /// </summary>
  14. public abstract class BaseApplication : Application, INotifyPropertyChanged, ISingleInstanceApp
  15. {
  16. private IKernel Kernel { get; set; }
  17. protected abstract IKernel InstantiateKernel();
  18. protected abstract Window InstantiateMainWindow();
  19. public event PropertyChangedEventHandler PropertyChanged;
  20. public void OnPropertyChanged(String info)
  21. {
  22. if (PropertyChanged != null)
  23. {
  24. PropertyChanged(this, new PropertyChangedEventArgs(info));
  25. }
  26. }
  27. protected override void OnStartup(StartupEventArgs e)
  28. {
  29. // Without this the app will shutdown after the splash screen closes
  30. ShutdownMode = ShutdownMode.OnExplicitShutdown;
  31. LoadKernel();
  32. }
  33. private async void LoadKernel()
  34. {
  35. Kernel = InstantiateKernel();
  36. var progress = new Progress<TaskProgress>();
  37. var splash = new Splash(progress);
  38. splash.Show();
  39. try
  40. {
  41. DateTime now = DateTime.UtcNow;
  42. await Kernel.Init(progress);
  43. Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
  44. splash.Close();
  45. ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  46. OnKernelLoaded();
  47. InstantiateMainWindow().Show();
  48. }
  49. catch (Exception ex)
  50. {
  51. Logger.LogException(ex);
  52. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  53. splash.Close();
  54. // Shutdown the app with an error code
  55. Shutdown(1);
  56. }
  57. }
  58. protected virtual void OnKernelLoaded()
  59. {
  60. }
  61. protected override void OnExit(ExitEventArgs e)
  62. {
  63. base.OnExit(e);
  64. Kernel.Dispose();
  65. }
  66. public bool SignalExternalCommandLineArgs(IList<string> args)
  67. {
  68. OnSecondInstanceLaunched(args);
  69. return true;
  70. }
  71. protected virtual void OnSecondInstanceLaunched(IList<string> args)
  72. {
  73. if (this.MainWindow.WindowState == WindowState.Minimized)
  74. {
  75. this.MainWindow.WindowState = WindowState.Maximized;
  76. }
  77. }
  78. public static void RunApplication<TApplicationType>(string uniqueKey)
  79. where TApplicationType : BaseApplication, IApplication, new()
  80. {
  81. if (SingleInstance<TApplicationType>.InitializeAsFirstInstance(uniqueKey))
  82. {
  83. var application = new TApplicationType();
  84. application.InitializeComponent();
  85. application.Run();
  86. // Allow single instance code to perform cleanup operations
  87. SingleInstance<TApplicationType>.Cleanup();
  88. }
  89. }
  90. }
  91. public interface IApplication
  92. {
  93. void InitializeComponent();
  94. }
  95. }