using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.ServerApplication.Splash; using System; using System.Diagnostics; using System.Windows; namespace MediaBrowser.ServerApplication { /// /// Interaction logic for App.xaml /// public partial class App : Application, IApplicationInterface { /// /// Gets or sets the logger. /// /// The logger. protected ILogger Logger { get; set; } /// /// Gets or sets the composition root. /// /// The composition root. protected ApplicationHost CompositionRoot { get; set; } /// /// Initializes a new instance of the class. /// /// The logger. public App() { InitializeComponent(); } public bool IsBackgroundService { get { return false; } } /// /// Gets the name of the uninstaller file. /// /// The name of the uninstaller file. protected string UninstallerFileName { get { return "MediaBrowser.Server.Uninstall.exe"; } } public void OnUnhandledException(Exception ex) { Logger.ErrorException("UnhandledException", ex); MessageBox.Show("Unhandled exception: " + ex.Message); } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); LoadApplication(); } /// /// Loads the kernel. /// protected async void LoadApplication() { try { CompositionRoot = new ApplicationHost(this); Logger = CompositionRoot.LogManager.GetLogger("App"); var splash = new SplashWindow(CompositionRoot.ApplicationVersion); splash.Show(); await CompositionRoot.Init(); splash.Hide(); var task = CompositionRoot.RunStartupTasks(); new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesRepository).Show(); await task.ConfigureAwait(false); } catch (Exception ex) { 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); } } public void ShutdownApplication() { Dispatcher.Invoke(Shutdown); } /// /// Raises the event. /// /// An that contains the event data. protected override void OnExit(ExitEventArgs e) { MainStartup.ReleaseMutex(); base.OnExit(e); if (CompositionRoot != null) { CompositionRoot.Dispose(); } } /// /// Opens the dashboard page. /// /// The page. /// The logged in user. /// The configuration manager. /// The app host. public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager, IServerApplicationHost appHost) { var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" + appHost.WebApplicationName + "/dashboard/" + page; OpenUrl(url); } /// /// Opens the URL. /// /// The URL. public static void OpenUrl(string url) { var process = new Process { StartInfo = new ProcessStartInfo { FileName = url }, EnableRaisingEvents = true }; process.Exited += ProcessExited; try { process.Start(); } catch (Exception ex) { MessageBox.Show("There was an error launching your web browser. Please check your defualt browser settings."); } } /// /// Processes the exited. /// /// The sender. /// The instance containing the event data. static void ProcessExited(object sender, EventArgs e) { ((Process)sender).Dispose(); } /// /// Restarts this instance. /// /// public void RestartApplication() { Dispatcher.Invoke(MainStartup.ReleaseMutex); CompositionRoot.Dispose(); System.Windows.Forms.Application.Restart(); Dispatcher.Invoke(Shutdown); } } }