using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Logging;
using MediaBrowser.ServerApplication.Logging;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Threading;
namespace MediaBrowser.ServerApplication
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
///
/// The _logger
///
private readonly ILogger _logger;
///
/// The _app host
///
private readonly IApplicationHost _appHost;
///
/// The _log manager
///
private readonly ILogManager _logManager;
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// The logger.
/// The app host.
/// logger
public MainWindow(ILogManager logManager, IApplicationHost appHost)
{
if (logManager == null)
{
throw new ArgumentNullException("logManager");
}
_logger = logManager.GetLogger("MainWindow");
_appHost = appHost;
_logManager = logManager;
InitializeComponent();
Loaded += MainWindowLoaded;
}
///
/// Mains the window loaded.
///
/// The sender.
/// The instance containing the event data.
void MainWindowLoaded(object sender, RoutedEventArgs e)
{
DataContext = this;
Instance_ConfigurationUpdated(null, EventArgs.Empty);
_logManager.LoggerLoaded += LoadLogWindow;
Kernel.Instance.ConfigurationUpdated += Instance_ConfigurationUpdated;
}
///
/// Handles the ConfigurationUpdated event of the Instance control.
///
/// The source of the event.
/// The instance containing the event data.
void Instance_ConfigurationUpdated(object sender, EventArgs e)
{
Dispatcher.InvokeAsync(() =>
{
var developerToolsVisibility = Kernel.Instance.Configuration.EnableDeveloperTools
? Visibility.Visible
: Visibility.Collapsed;
separatorDeveloperTools.Visibility = developerToolsVisibility;
cmdReloadServer.Visibility = developerToolsVisibility;
cmOpenExplorer.Visibility = developerToolsVisibility;
var logWindow = App.Instance.Windows.OfType().FirstOrDefault();
if ((logWindow == null && Kernel.Instance.Configuration.ShowLogWindow) || (logWindow != null && !Kernel.Instance.Configuration.ShowLogWindow))
{
_logManager.ReloadLogger(Kernel.Instance.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
}
});
}
///
/// Loads the log window.
///
/// The sender.
/// The instance containing the event data.
void LoadLogWindow(object sender, EventArgs args)
{
CloseLogWindow();
Dispatcher.InvokeAsync(() =>
{
// Add our log window if specified
if (Kernel.Instance.Configuration.ShowLogWindow)
{
Trace.Listeners.Add(new WindowTraceListener(new LogWindow(Kernel.Instance)));
}
else
{
Trace.Listeners.Remove("MBLogWindow");
}
// Set menu option indicator
cmShowLogWindow.IsChecked = Kernel.Instance.Configuration.ShowLogWindow;
}, DispatcherPriority.Normal);
}
///
/// Closes the log window.
///
void CloseLogWindow()
{
Dispatcher.InvokeAsync(() =>
{
foreach (var win in Application.Current.Windows.OfType())
{
win.Close();
}
});
}
///
/// Handles the Click event of the cmdApiDocs control.
///
/// The source of the event.
/// The instance containing the event data.
void cmdApiDocs_Click(object sender, EventArgs e)
{
App.OpenUrl("http://localhost:" + Kernel.Instance.Configuration.HttpServerPortNumber + "/" +
Kernel.Instance.WebApplicationName + "/metadata");
}
///
/// Occurs when [property changed].
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// Called when [property changed].
///
/// The info.
public void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
try
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
catch (Exception ex)
{
_logger.ErrorException("Error in event handler", ex);
}
}
}
#region Context Menu events
///
/// Handles the click event of the cmOpenExplorer control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
{
var explorer = (LibraryExplorer)_appHost.CreateInstance(typeof(LibraryExplorer));
explorer.Show();
}
///
/// Handles the click event of the cmOpenDashboard control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
{
var user = _appHost.Resolve().Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
App.OpenDashboard(user);
}
///
/// Handles the click event of the cmVisitCT control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmVisitCT_click(object sender, RoutedEventArgs e)
{
App.OpenUrl("http://community.mediabrowser.tv/");
}
///
/// Handles the click event of the cmdBrowseLibrary control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
{
var user = _appHost.Resolve().Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
App.OpenDashboardPage("index.html", user);
}
///
/// Handles the click event of the cmExit control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmExit_click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
///
/// Handles the click event of the cmdReloadServer control.
///
/// The source of the event.
/// The instance containing the event data.
private void cmdReloadServer_click(object sender, RoutedEventArgs e)
{
App.Instance.Restart();
}
///
/// Handles the click event of the CmShowLogWindow control.
///
/// The source of the event.
/// The instance containing the event data.
private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
{
Kernel.Instance.Configuration.ShowLogWindow = !Kernel.Instance.Configuration.ShowLogWindow;
Kernel.Instance.SaveConfiguration();
LoadLogWindow(sender, e);
}
#endregion
}
}