123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- using MediaBrowser.Common.Kernel;
- using MediaBrowser.Controller;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.Serialization;
- using MediaBrowser.ServerApplication.Controls;
- using MediaBrowser.ServerApplication.Logging;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls.Primitives;
- using System.Windows.Threading;
- namespace MediaBrowser.ServerApplication
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- /// <summary>
- /// Holds the list of new items to display when the NewItemTimer expires
- /// </summary>
- private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();
- /// <summary>
- /// The amount of time to wait before showing a new item notification
- /// This allows us to group items together into one notification
- /// </summary>
- private const int NewItemDelay = 60000;
- /// <summary>
- /// The current new item timer
- /// </summary>
- /// <value>The new item timer.</value>
- private Timer NewItemTimer { get; set; }
- /// <summary>
- /// The _json serializer
- /// </summary>
- private readonly IJsonSerializer _jsonSerializer;
-
- /// <summary>
- /// The _logger
- /// </summary>
- private readonly ILogger _logger;
- /// <summary>
- /// The _app host
- /// </summary>
- private readonly IApplicationHost _appHost;
- /// <summary>
- /// The _log manager
- /// </summary>
- private readonly ILogManager _logManager;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="MainWindow" /> class.
- /// </summary>
- /// <param name="jsonSerializer">The json serializer.</param>
- /// <param name="logger">The logger.</param>
- /// <param name="appHost">The app host.</param>
- /// <exception cref="System.ArgumentNullException">logger</exception>
- public MainWindow(IJsonSerializer jsonSerializer, ILogManager logManager, IApplicationHost appHost)
- {
- if (jsonSerializer == null)
- {
- throw new ArgumentNullException("jsonSerializer");
- }
- if (logManager == null)
- {
- throw new ArgumentNullException("logManager");
- }
- _jsonSerializer = jsonSerializer;
- _logger = logManager.GetLogger("MainWindow");
- _appHost = appHost;
- _logManager = logManager;
- InitializeComponent();
- Loaded += MainWindowLoaded;
- }
- /// <summary>
- /// Mains the window loaded.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- void MainWindowLoaded(object sender, RoutedEventArgs e)
- {
- DataContext = this;
- Instance_ConfigurationUpdated(null, EventArgs.Empty);
- Kernel.Instance.ReloadCompleted += KernelReloadCompleted;
- _logManager.LoggerLoaded += LoadLogWindow;
- Kernel.Instance.HasPendingRestartChanged += Instance_HasPendingRestartChanged;
- Kernel.Instance.ConfigurationUpdated += Instance_ConfigurationUpdated;
- }
- /// <summary>
- /// Handles the ConfigurationUpdated event of the Instance control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- 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<LogWindow>().FirstOrDefault();
- if ((logWindow == null && Kernel.Instance.Configuration.ShowLogWindow) || (logWindow != null && !Kernel.Instance.Configuration.ShowLogWindow))
- {
- _logManager.ReloadLogger(Kernel.Instance.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
- }
- });
- }
- /// <summary>
- /// Sets visibility of the restart message when the kernel value changes
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- void Instance_HasPendingRestartChanged(object sender, EventArgs e)
- {
- Dispatcher.InvokeAsync(() =>
- {
- MbTaskbarIcon.ToolTipText = Kernel.Instance.HasPendingRestart ? "Media Browser Server - Please restart to finish updating." : "Media Browser Server";
- });
- }
- /// <summary>
- /// Handles the LibraryChanged event of the Instance control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
- void Instance_LibraryChanged(object sender, ChildrenChangedEventArgs e)
- {
- var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList();
- // Use a timer to prevent lots of these notifications from showing in a short period of time
- if (newItems.Count > 0)
- {
- lock (_newlyAddedItems)
- {
- _newlyAddedItems.AddRange(newItems);
- if (NewItemTimer == null)
- {
- NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
- }
- else
- {
- NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
- }
- }
- }
- }
- /// <summary>
- /// Called when the new item timer expires
- /// </summary>
- /// <param name="state">The state.</param>
- private void NewItemTimerCallback(object state)
- {
- List<BaseItem> newItems;
- // Lock the list and release all resources
- lock (_newlyAddedItems)
- {
- newItems = _newlyAddedItems.ToList();
- _newlyAddedItems.Clear();
- NewItemTimer.Dispose();
- NewItemTimer = null;
- }
- // Show the notification
- if (newItems.Count == 1)
- {
- Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger)
- {
- DataContext = newItems[0]
- }, PopupAnimation.Slide, 6000));
- }
- else if (newItems.Count > 1)
- {
- Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new MultiItemUpdateNotification(_logger)
- {
- DataContext = newItems
- }, PopupAnimation.Slide, 6000));
- }
- }
- /// <summary>
- /// Loads the log window.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
- 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);
- }
- /// <summary>
- /// Closes the log window.
- /// </summary>
- void CloseLogWindow()
- {
- Dispatcher.InvokeAsync(() =>
- {
- foreach (var win in Application.Current.Windows.OfType<LogWindow>())
- {
- win.Close();
- }
- });
- }
- /// <summary>
- /// Kernels the reload completed.
- /// </summary>
- /// <param name="sender">The sender.</param>
- /// <param name="e">The e.</param>
- void KernelReloadCompleted(object sender, EventArgs e)
- {
- Kernel.Instance.LibraryManager.LibraryChanged -= Instance_LibraryChanged;
- Kernel.Instance.LibraryManager.LibraryChanged += Instance_LibraryChanged;
- if (_appHost.IsFirstRun)
- {
- LaunchStartupWizard();
- }
- }
- /// <summary>
- /// Launches the startup wizard.
- /// </summary>
- private void LaunchStartupWizard()
- {
- App.OpenDashboardPage("wizardStart.html");
- }
- /// <summary>
- /// Handles the Click event of the cmdApiDocs control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
- void cmdApiDocs_Click(object sender, EventArgs e)
- {
- App.OpenUrl("http://localhost:" + Controller.Kernel.Instance.Configuration.HttpServerPortNumber + "/" +
- Controller.Kernel.Instance.WebApplicationName + "/metadata");
- }
- /// <summary>
- /// Occurs when [property changed].
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- /// Called when [property changed].
- /// </summary>
- /// <param name="info">The info.</param>
- 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
- /// <summary>
- /// Handles the click event of the cmOpenExplorer control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
- {
- (new LibraryExplorer(_jsonSerializer, _logger, _appHost)).Show();
- }
- /// <summary>
- /// Handles the click event of the cmOpenDashboard control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
- {
- App.OpenDashboard();
- }
- /// <summary>
- /// Handles the click event of the cmVisitCT control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmVisitCT_click(object sender, RoutedEventArgs e)
- {
- App.OpenUrl("http://community.mediabrowser.tv/");
- }
- /// <summary>
- /// Handles the click event of the cmdBrowseLibrary control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
- {
- App.OpenDashboardPage("index.html");
- }
- /// <summary>
- /// Handles the click event of the cmExit control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmExit_click(object sender, RoutedEventArgs e)
- {
- Application.Current.Shutdown();
- }
- /// <summary>
- /// Handles the click event of the cmdReloadServer control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void cmdReloadServer_click(object sender, RoutedEventArgs e)
- {
- App.Instance.Restart();
- }
- /// <summary>
- /// Handles the click event of the CmShowLogWindow control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
- private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
- {
- Kernel.Instance.Configuration.ShowLogWindow = !Kernel.Instance.Configuration.ShowLogWindow;
- Kernel.Instance.SaveConfiguration();
- LoadLogWindow(sender, e);
- }
- #endregion
- }
- }
|