2013-09-21 03:04:14 +02:00
using MediaBrowser.Controller ;
2013-03-04 06:43:06 +01:00
using MediaBrowser.Controller.Configuration ;
using MediaBrowser.Controller.Entities ;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Library ;
2013-06-18 21:16:27 +02:00
using MediaBrowser.Controller.Persistence ;
2013-02-21 21:26:35 +01:00
using MediaBrowser.Model.Logging ;
2013-03-08 06:08:27 +01:00
using MediaBrowser.Model.Serialization ;
2013-02-22 02:26:35 +01:00
using MediaBrowser.ServerApplication.Logging ;
2013-02-21 02:33:05 +01:00
using System ;
using System.ComponentModel ;
using System.Diagnostics ;
using System.Linq ;
using System.Windows ;
using System.Windows.Threading ;
2013-09-25 02:54:51 +02:00
using MediaBrowser.ServerApplication.Native ;
2013-02-21 02:33:05 +01:00
namespace MediaBrowser.ServerApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window , INotifyPropertyChanged
{
2013-02-21 21:26:35 +01:00
/// <summary>
/// The _logger
/// </summary>
private readonly ILogger _logger ;
2013-02-26 04:43:04 +01:00
/// <summary>
/// The _app host
/// </summary>
2013-06-03 20:15:35 +02:00
private readonly IServerApplicationHost _appHost ;
2013-02-26 04:43:04 +01:00
2013-02-26 18:21:18 +01:00
/// <summary>
/// The _log manager
/// </summary>
private readonly ILogManager _logManager ;
2013-03-04 06:43:06 +01:00
/// <summary>
/// The _configuration manager
/// </summary>
private readonly IServerConfigurationManager _configurationManager ;
2013-03-08 06:08:27 +01:00
private readonly IUserManager _userManager ;
private readonly ILibraryManager _libraryManager ;
private readonly IJsonSerializer _jsonSerializer ;
2013-06-18 21:16:27 +02:00
private readonly IDisplayPreferencesRepository _displayPreferencesManager ;
2013-03-08 06:08:27 +01:00
2013-02-21 02:33:05 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow" /> class.
/// </summary>
2013-04-16 21:43:58 +02:00
/// <param name="logManager">The log manager.</param>
2013-02-26 04:43:04 +01:00
/// <param name="appHost">The app host.</param>
2013-04-16 21:43:58 +02:00
/// <param name="configurationManager">The configuration manager.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="displayPreferencesManager">The display preferences manager.</param>
2013-02-21 21:26:35 +01:00
/// <exception cref="System.ArgumentNullException">logger</exception>
2013-06-18 21:16:27 +02:00
public MainWindow ( ILogManager logManager , IServerApplicationHost appHost , IServerConfigurationManager configurationManager , IUserManager userManager , ILibraryManager libraryManager , IJsonSerializer jsonSerializer , IDisplayPreferencesRepository displayPreferencesManager )
2013-02-21 02:33:05 +01:00
{
2013-02-26 18:21:18 +01:00
if ( logManager = = null )
2013-02-21 21:26:35 +01:00
{
2013-02-26 18:21:18 +01:00
throw new ArgumentNullException ( "logManager" ) ;
2013-02-21 21:26:35 +01:00
}
2013-03-04 06:43:06 +01:00
if ( appHost = = null )
{
throw new ArgumentNullException ( "appHost" ) ;
}
if ( configurationManager = = null )
{
throw new ArgumentNullException ( "configurationManager" ) ;
}
2013-02-21 21:26:35 +01:00
2013-02-26 18:21:18 +01:00
_logger = logManager . GetLogger ( "MainWindow" ) ;
2013-02-26 04:43:04 +01:00
_appHost = appHost ;
2013-02-26 18:21:18 +01:00
_logManager = logManager ;
2013-03-04 06:43:06 +01:00
_configurationManager = configurationManager ;
2013-03-08 06:08:27 +01:00
_userManager = userManager ;
_libraryManager = libraryManager ;
_jsonSerializer = jsonSerializer ;
2013-04-05 21:34:33 +02:00
_displayPreferencesManager = displayPreferencesManager ;
2013-02-21 21:26:35 +01:00
2013-02-21 02:33:05 +01:00
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 ;
2013-03-05 19:11:25 +01:00
UpdateButtons ( ) ;
2013-02-21 02:33:05 +01:00
2013-03-08 21:47:18 +01:00
LoadLogWindow ( null , EventArgs . Empty ) ;
2013-02-26 18:21:18 +01:00
_logManager . LoggerLoaded + = LoadLogWindow ;
2013-03-04 06:43:06 +01:00
_configurationManager . ConfigurationUpdated + = Instance_ConfigurationUpdated ;
2013-04-16 21:43:58 +02:00
if ( _appHost . IsFirstRun )
{
Dispatcher . InvokeAsync ( ( ) = > MbTaskbarIcon . ShowBalloonTip ( "Media Browser" , "Welcome to Media Browser Server!" , Hardcodet . Wpf . TaskbarNotification . BalloonIcon . None ) ) ;
}
2013-02-21 02:33:05 +01:00
}
/// <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 )
2013-03-05 19:11:25 +01:00
{
UpdateButtons ( ) ;
Dispatcher . InvokeAsync ( ( ) = >
{
2013-09-20 19:32:10 +02:00
var logWindow = App . Current . Windows . OfType < LogWindow > ( ) . FirstOrDefault ( ) ;
2013-03-05 19:11:25 +01:00
if ( ( logWindow = = null & & _configurationManager . Configuration . ShowLogWindow ) | | ( logWindow ! = null & & ! _configurationManager . Configuration . ShowLogWindow ) )
{
_logManager . ReloadLogger ( _configurationManager . Configuration . EnableDebugLevelLogging ? LogSeverity . Debug : LogSeverity . Info ) ;
}
} ) ;
}
private void UpdateButtons ( )
2013-02-21 02:33:05 +01:00
{
Dispatcher . InvokeAsync ( ( ) = >
{
2013-03-04 06:43:06 +01:00
var developerToolsVisibility = _configurationManager . Configuration . EnableDeveloperTools
2013-02-21 02:33:05 +01:00
? Visibility . Visible
: Visibility . Collapsed ;
separatorDeveloperTools . Visibility = developerToolsVisibility ;
cmdReloadServer . Visibility = developerToolsVisibility ;
cmOpenExplorer . Visibility = developerToolsVisibility ;
2013-06-16 05:17:42 +02:00
cmShowLogWindow . Visibility = developerToolsVisibility ;
2013-02-21 02:33:05 +01:00
} ) ;
}
/// <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 ( ) ;
2013-03-05 19:11:25 +01:00
2013-02-21 02:33:05 +01:00
Dispatcher . InvokeAsync ( ( ) = >
{
// Add our log window if specified
2013-03-04 06:43:06 +01:00
if ( _configurationManager . Configuration . ShowLogWindow )
2013-02-21 02:33:05 +01:00
{
2013-03-04 17:31:33 +01:00
Trace . Listeners . Add ( new WindowTraceListener ( new LogWindow ( _logManager ) ) ) ;
2013-02-21 02:33:05 +01:00
}
else
{
Trace . Listeners . Remove ( "MBLogWindow" ) ;
}
// Set menu option indicator
2013-03-04 06:43:06 +01:00
cmShowLogWindow . IsChecked = _configurationManager . Configuration . ShowLogWindow ;
2013-02-21 02:33:05 +01:00
} , DispatcherPriority . Normal ) ;
}
/// <summary>
/// Closes the log window.
/// </summary>
void CloseLogWindow ( )
{
Dispatcher . InvokeAsync ( ( ) = >
{
foreach ( var win in Application . Current . Windows . OfType < LogWindow > ( ) )
{
win . Close ( ) ;
}
} ) ;
}
/// <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 )
{
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenUrl ( "http://localhost:" + _configurationManager . Configuration . HttpServerPortNumber + "/" +
_appHost . WebApplicationName + "/metadata" , _logger ) ;
2013-02-21 02:33:05 +01:00
}
2013-03-05 04:34:02 +01:00
void cmdSwaggerApiDocs_Click ( object sender , EventArgs e )
{
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenUrl ( "http://localhost:" + _configurationManager . Configuration . HttpServerPortNumber + "/" +
_appHost . WebApplicationName + "/swagger-ui/index.html" , _logger ) ;
2013-03-05 04:34:02 +01:00
}
2013-03-08 17:40:01 +01:00
void cmdGithubWiki_Click ( object sender , EventArgs e )
{
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenUrl ( "https://github.com/MediaBrowser/MediaBrowser/wiki" , _logger ) ;
2013-03-08 17:40:01 +01:00
}
2013-09-20 19:32:10 +02:00
2013-02-21 02:33:05 +01:00
/// <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 )
{
2013-02-21 21:26:35 +01:00
_logger . ErrorException ( "Error in event handler" , ex ) ;
2013-02-21 02:33:05 +01:00
}
}
}
#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 )
{
2013-04-05 21:34:33 +02:00
new LibraryExplorer ( _jsonSerializer , _logger , _appHost , _userManager , _libraryManager , _displayPreferencesManager ) . Show ( ) ;
2013-02-21 02:33:05 +01:00
}
/// <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 )
{
2013-03-08 06:08:27 +01:00
var user = _userManager . Users . FirstOrDefault ( u = > u . Configuration . IsAdministrator ) ;
2013-03-04 06:43:06 +01:00
OpenDashboard ( user ) ;
2013-02-21 02:33:05 +01:00
}
2013-03-04 06:43:06 +01:00
/// <summary>
/// Opens the dashboard.
/// </summary>
private void OpenDashboard ( User loggedInUser )
{
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenDashboardPage ( "dashboard.html" , loggedInUser , _configurationManager , _appHost , _logger ) ;
2013-03-04 06:43:06 +01:00
}
2013-09-20 19:32:10 +02:00
2013-02-21 02:33:05 +01:00
/// <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 )
{
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenUrl ( "http://community.mediabrowser.tv/" , _logger ) ;
2013-02-21 02:33:05 +01:00
}
/// <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 )
{
2013-03-08 06:08:27 +01:00
var user = _userManager . Users . FirstOrDefault ( u = > u . Configuration . IsAdministrator ) ;
2013-09-25 02:54:51 +02:00
BrowserLauncher . OpenDashboardPage ( "index.html" , user , _configurationManager , _appHost , _logger ) ;
2013-02-21 02:33:05 +01:00
}
/// <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>
2013-09-20 19:32:10 +02:00
private async void cmExit_click ( object sender , RoutedEventArgs e )
2013-02-21 02:33:05 +01:00
{
2013-09-20 19:32:10 +02:00
await _appHost . Shutdown ( ) . ConfigureAwait ( false ) ;
2013-02-21 02:33:05 +01:00
}
/// <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>
2013-09-20 19:32:10 +02:00
private async void cmdReloadServer_click ( object sender , RoutedEventArgs e )
2013-02-21 02:33:05 +01:00
{
2013-09-20 19:32:10 +02:00
await _appHost . Restart ( ) . ConfigureAwait ( false ) ;
2013-02-21 02:33:05 +01:00
}
/// <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 )
{
2013-03-04 06:43:06 +01:00
_configurationManager . Configuration . ShowLogWindow = ! _configurationManager . Configuration . ShowLogWindow ;
_configurationManager . SaveConfiguration ( ) ;
2013-02-21 02:33:05 +01:00
LoadLogWindow ( sender , e ) ;
}
#endregion
}
}