2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-03-02 19:48:25 +00:00
|
|
|
|
using MediaBrowser.Common.Constants;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Common.Implementations.Updates;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-27 20:25:45 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-21 21:06:23 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-03-02 19:48:25 +00:00
|
|
|
|
using MediaBrowser.Server.Implementations;
|
2013-05-18 22:07:59 +00:00
|
|
|
|
using MediaBrowser.ServerApplication.Splash;
|
2013-02-22 01:26:35 +00:00
|
|
|
|
using Microsoft.Win32;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using System.IO;
|
2013-02-22 01:26:35 +00:00
|
|
|
|
using System.Net.Cache;
|
|
|
|
|
using System.Threading;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Windows;
|
2013-02-22 01:26:35 +00:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.ServerApplication
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for App.xaml
|
|
|
|
|
/// </summary>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
public partial class App : Application
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-04-23 14:53:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The single instance mutex
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static Mutex _singleInstanceMutex;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines the entry point of the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
2013-04-23 14:53:43 +00:00
|
|
|
|
bool createdNew;
|
|
|
|
|
|
|
|
|
|
_singleInstanceMutex = new Mutex(true, @"Local\" + typeof(App).Assembly.GetName().Name, out createdNew);
|
|
|
|
|
|
|
|
|
|
if (!createdNew)
|
|
|
|
|
{
|
|
|
|
|
_singleInstanceMutex = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-02 19:48:25 +00:00
|
|
|
|
// Look for the existence of an update archive
|
|
|
|
|
var appPaths = new ServerApplicationPaths();
|
2013-04-10 15:38:23 +00:00
|
|
|
|
var updateArchive = Path.Combine(appPaths.TempUpdatePath, Constants.MbServerPkgName + ".zip");
|
2013-03-02 19:48:25 +00:00
|
|
|
|
if (File.Exists(updateArchive))
|
|
|
|
|
{
|
|
|
|
|
// Update is there - execute update
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new ApplicationUpdater().UpdateApplication(MBApplication.MBServer, appPaths, updateArchive);
|
|
|
|
|
|
|
|
|
|
// And just let the app exit so it can update
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(string.Format("Error attempting to update application.\n\n{0}\n\n{1}", e.GetType().Name, e.Message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-26 03:43:04 +00:00
|
|
|
|
var application = new App();
|
2013-02-21 21:06:23 +00:00
|
|
|
|
|
|
|
|
|
application.Run();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The instance.</value>
|
|
|
|
|
public static App Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Current as App;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the logger.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The logger.</value>
|
|
|
|
|
protected ILogger Logger { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// Gets or sets the composition root.
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// </summary>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <value>The composition root.</value>
|
|
|
|
|
protected ApplicationHost CompositionRoot { get; 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-02-26 03:43:04 +00:00
|
|
|
|
public App()
|
2013-02-21 21:06:23 +00:00
|
|
|
|
{
|
2013-02-22 01:26:35 +00:00
|
|
|
|
InitializeComponent();
|
2013-02-21 21:06:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
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>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
protected string UninstallerFileName
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
get { return "MediaBrowser.Server.Uninstall.exe"; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raises the <see cref="E:System.Windows.Application.Startup" /> event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param>
|
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-22 01:26:35 +00:00
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
|
LoadKernel();
|
|
|
|
|
|
|
|
|
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the UnhandledException event of the CurrentDomain control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="UnhandledExceptionEventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var exception = (Exception)e.ExceptionObject;
|
|
|
|
|
|
|
|
|
|
Logger.ErrorException("UnhandledException", exception);
|
|
|
|
|
|
|
|
|
|
MessageBox.Show("Unhandled exception: " + exception.Message);
|
2013-03-13 20:23:46 +00:00
|
|
|
|
|
|
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
|
{
|
|
|
|
|
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
|
|
|
|
}
|
2013-02-22 01:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the SessionEnding event of the SystemEvents control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="SessionEndingEventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Try to shut down gracefully
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads the kernel.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected async void LoadKernel()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-03-04 05:43:06 +00:00
|
|
|
|
CompositionRoot = new ApplicationHost();
|
2013-02-22 01:26:35 +00:00
|
|
|
|
|
2013-03-04 05:43:06 +00:00
|
|
|
|
Logger = CompositionRoot.LogManager.GetLogger("App");
|
2013-02-22 01:26:35 +00:00
|
|
|
|
|
2013-05-18 22:07:59 +00:00
|
|
|
|
var splash = new SplashWindow(CompositionRoot.ApplicationVersion);
|
|
|
|
|
|
|
|
|
|
splash.Show();
|
|
|
|
|
|
2013-03-04 05:43:06 +00:00
|
|
|
|
await CompositionRoot.Init();
|
2013-03-08 05:08:27 +00:00
|
|
|
|
|
2013-05-18 22:07:59 +00:00
|
|
|
|
splash.Hide();
|
|
|
|
|
|
|
|
|
|
var task = CompositionRoot.RunStartupTasks();
|
|
|
|
|
|
|
|
|
|
new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesManager).Show();
|
2013-03-08 05:08:27 +00:00
|
|
|
|
|
2013-05-18 22:07:59 +00:00
|
|
|
|
await task.ConfigureAwait(false);
|
2013-02-22 01:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
|
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ReleaseMutex();
|
|
|
|
|
|
|
|
|
|
base.OnExit(e);
|
|
|
|
|
|
2013-04-23 14:53:43 +00:00
|
|
|
|
if (CompositionRoot != null)
|
|
|
|
|
{
|
|
|
|
|
CompositionRoot.Dispose();
|
|
|
|
|
}
|
2013-02-22 01:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases the mutex.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ReleaseMutex()
|
|
|
|
|
{
|
2013-04-23 14:53:43 +00:00
|
|
|
|
if (_singleInstanceMutex == null)
|
2013-02-22 01:26:35 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-23 14:53:43 +00:00
|
|
|
|
_singleInstanceMutex.ReleaseMutex();
|
|
|
|
|
_singleInstanceMutex.Close();
|
|
|
|
|
_singleInstanceMutex.Dispose();
|
|
|
|
|
_singleInstanceMutex = null;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opens the dashboard page.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="page">The page.</param>
|
2013-03-07 05:34:00 +00:00
|
|
|
|
public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-07 05:34:00 +00:00
|
|
|
|
var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" +
|
2013-03-04 05:43:06 +00:00
|
|
|
|
Kernel.Instance.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;
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// Restarts this instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <exception cref="System.NotImplementedException"></exception>
|
|
|
|
|
public void Restart()
|
|
|
|
|
{
|
|
|
|
|
Dispatcher.Invoke(ReleaseMutex);
|
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
CompositionRoot.Dispose();
|
2013-02-22 01:26:35 +00:00
|
|
|
|
|
|
|
|
|
System.Windows.Forms.Application.Restart();
|
|
|
|
|
|
|
|
|
|
Dispatcher.Invoke(Shutdown);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns>Image.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">uri</exception>
|
|
|
|
|
public Image GetImage(string uri)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(uri))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("uri");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetImage(new Uri(uri));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the image.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns>Image.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">uri</exception>
|
|
|
|
|
public Image GetImage(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
if (uri == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("uri");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Image { Source = GetBitmapImage(uri) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the bitmap image.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns>BitmapImage.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">uri</exception>
|
|
|
|
|
public BitmapImage GetBitmapImage(string uri)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-22 01:26:35 +00:00
|
|
|
|
if (string.IsNullOrEmpty(uri))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("uri");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GetBitmapImage(new Uri(uri));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// Gets the bitmap image.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns>BitmapImage.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">uri</exception>
|
|
|
|
|
public BitmapImage GetBitmapImage(Uri uri)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-22 01:26:35 +00:00
|
|
|
|
if (uri == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("uri");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bitmap = new BitmapImage
|
|
|
|
|
{
|
|
|
|
|
CreateOptions = BitmapCreateOptions.DelayCreation,
|
|
|
|
|
CacheOption = BitmapCacheOption.OnDemand,
|
|
|
|
|
UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bitmap.BeginInit();
|
|
|
|
|
bitmap.UriSource = uri;
|
|
|
|
|
bitmap.EndInit();
|
|
|
|
|
|
|
|
|
|
RenderOptions.SetBitmapScalingMode(bitmap, BitmapScalingMode.Fant);
|
|
|
|
|
return bitmap;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|