MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.Persistence;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using MediaBrowser.ServerApplication.Logging;
  9. using System;
  10. using System.ComponentModel;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Windows;
  14. using System.Windows.Threading;
  15. using MediaBrowser.ServerApplication.Native;
  16. namespace MediaBrowser.ServerApplication
  17. {
  18. /// <summary>
  19. /// Interaction logic for MainWindow.xaml
  20. /// </summary>
  21. public partial class MainWindow : Window, INotifyPropertyChanged
  22. {
  23. /// <summary>
  24. /// The _logger
  25. /// </summary>
  26. private readonly ILogger _logger;
  27. /// <summary>
  28. /// The _app host
  29. /// </summary>
  30. private readonly IServerApplicationHost _appHost;
  31. /// <summary>
  32. /// The _log manager
  33. /// </summary>
  34. private readonly ILogManager _logManager;
  35. /// <summary>
  36. /// The _configuration manager
  37. /// </summary>
  38. private readonly IServerConfigurationManager _configurationManager;
  39. private readonly IUserManager _userManager;
  40. private readonly ILibraryManager _libraryManager;
  41. private readonly IJsonSerializer _jsonSerializer;
  42. private readonly IDisplayPreferencesRepository _displayPreferencesManager;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="MainWindow" /> class.
  45. /// </summary>
  46. /// <param name="logManager">The log manager.</param>
  47. /// <param name="appHost">The app host.</param>
  48. /// <param name="configurationManager">The configuration manager.</param>
  49. /// <param name="userManager">The user manager.</param>
  50. /// <param name="libraryManager">The library manager.</param>
  51. /// <param name="jsonSerializer">The json serializer.</param>
  52. /// <param name="displayPreferencesManager">The display preferences manager.</param>
  53. /// <exception cref="System.ArgumentNullException">logger</exception>
  54. public MainWindow(ILogManager logManager, IServerApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesRepository displayPreferencesManager)
  55. {
  56. if (logManager == null)
  57. {
  58. throw new ArgumentNullException("logManager");
  59. }
  60. if (appHost == null)
  61. {
  62. throw new ArgumentNullException("appHost");
  63. }
  64. if (configurationManager == null)
  65. {
  66. throw new ArgumentNullException("configurationManager");
  67. }
  68. _logger = logManager.GetLogger("MainWindow");
  69. _appHost = appHost;
  70. _logManager = logManager;
  71. _configurationManager = configurationManager;
  72. _userManager = userManager;
  73. _libraryManager = libraryManager;
  74. _jsonSerializer = jsonSerializer;
  75. _displayPreferencesManager = displayPreferencesManager;
  76. InitializeComponent();
  77. Loaded += MainWindowLoaded;
  78. }
  79. /// <summary>
  80. /// Mains the window loaded.
  81. /// </summary>
  82. /// <param name="sender">The sender.</param>
  83. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  84. void MainWindowLoaded(object sender, RoutedEventArgs e)
  85. {
  86. DataContext = this;
  87. UpdateButtons();
  88. LoadLogWindow(null, EventArgs.Empty);
  89. _logManager.LoggerLoaded += LoadLogWindow;
  90. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  91. if (_appHost.IsFirstRun)
  92. {
  93. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowBalloonTip("Media Browser", "Welcome to Media Browser Server!", Hardcodet.Wpf.TaskbarNotification.BalloonIcon.None));
  94. }
  95. }
  96. /// <summary>
  97. /// Handles the ConfigurationUpdated event of the Instance control.
  98. /// </summary>
  99. /// <param name="sender">The source of the event.</param>
  100. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  101. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  102. {
  103. UpdateButtons();
  104. Dispatcher.InvokeAsync(() =>
  105. {
  106. var logWindow = App.Current.Windows.OfType<LogWindow>().FirstOrDefault();
  107. if ((logWindow == null && _configurationManager.Configuration.ShowLogWindow) || (logWindow != null && !_configurationManager.Configuration.ShowLogWindow))
  108. {
  109. _logManager.ReloadLogger(_configurationManager.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  110. }
  111. });
  112. }
  113. private void UpdateButtons()
  114. {
  115. Dispatcher.InvokeAsync(() =>
  116. {
  117. var developerToolsVisibility = _configurationManager.Configuration.EnableDeveloperTools
  118. ? Visibility.Visible
  119. : Visibility.Collapsed;
  120. separatorDeveloperTools.Visibility = developerToolsVisibility;
  121. cmdReloadServer.Visibility = developerToolsVisibility;
  122. cmOpenExplorer.Visibility = developerToolsVisibility;
  123. cmShowLogWindow.Visibility = developerToolsVisibility;
  124. });
  125. }
  126. /// <summary>
  127. /// Loads the log window.
  128. /// </summary>
  129. /// <param name="sender">The sender.</param>
  130. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  131. void LoadLogWindow(object sender, EventArgs args)
  132. {
  133. CloseLogWindow();
  134. Dispatcher.InvokeAsync(() =>
  135. {
  136. // Add our log window if specified
  137. if (_configurationManager.Configuration.ShowLogWindow)
  138. {
  139. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  140. }
  141. else
  142. {
  143. Trace.Listeners.Remove("MBLogWindow");
  144. }
  145. // Set menu option indicator
  146. cmShowLogWindow.IsChecked = _configurationManager.Configuration.ShowLogWindow;
  147. }, DispatcherPriority.Normal);
  148. }
  149. /// <summary>
  150. /// Closes the log window.
  151. /// </summary>
  152. void CloseLogWindow()
  153. {
  154. Dispatcher.InvokeAsync(() =>
  155. {
  156. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  157. {
  158. win.Close();
  159. }
  160. });
  161. }
  162. /// <summary>
  163. /// Handles the Click event of the cmdApiDocs control.
  164. /// </summary>
  165. /// <param name="sender">The source of the event.</param>
  166. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  167. void cmdApiDocs_Click(object sender, EventArgs e)
  168. {
  169. BrowserLauncher.OpenStandardApiDocumentation(_configurationManager, _appHost, _logger);
  170. }
  171. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  172. {
  173. BrowserLauncher.OpenSwagger(_configurationManager, _appHost, _logger);
  174. }
  175. void cmdGithubWiki_Click(object sender, EventArgs e)
  176. {
  177. BrowserLauncher.OpenGithub(_logger);
  178. }
  179. /// <summary>
  180. /// Occurs when [property changed].
  181. /// </summary>
  182. public event PropertyChangedEventHandler PropertyChanged;
  183. /// <summary>
  184. /// Called when [property changed].
  185. /// </summary>
  186. /// <param name="info">The info.</param>
  187. public void OnPropertyChanged(String info)
  188. {
  189. if (PropertyChanged != null)
  190. {
  191. try
  192. {
  193. PropertyChanged(this, new PropertyChangedEventArgs(info));
  194. }
  195. catch (Exception ex)
  196. {
  197. _logger.ErrorException("Error in event handler", ex);
  198. }
  199. }
  200. }
  201. #region Context Menu events
  202. /// <summary>
  203. /// Handles the click event of the cmOpenExplorer control.
  204. /// </summary>
  205. /// <param name="sender">The source of the event.</param>
  206. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  207. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  208. {
  209. new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager, _displayPreferencesManager).Show();
  210. }
  211. /// <summary>
  212. /// Handles the click event of the cmOpenDashboard control.
  213. /// </summary>
  214. /// <param name="sender">The source of the event.</param>
  215. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  216. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  217. {
  218. BrowserLauncher.OpenDashboard(_userManager, _configurationManager, _appHost, _logger);
  219. }
  220. /// <summary>
  221. /// Handles the click event of the cmVisitCT control.
  222. /// </summary>
  223. /// <param name="sender">The source of the event.</param>
  224. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  225. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  226. {
  227. BrowserLauncher.OpenCommunity(_logger);
  228. }
  229. /// <summary>
  230. /// Handles the click event of the cmdBrowseLibrary control.
  231. /// </summary>
  232. /// <param name="sender">The source of the event.</param>
  233. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  234. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  235. {
  236. BrowserLauncher.OpenWebClient(_userManager, _configurationManager, _appHost, _logger);
  237. }
  238. /// <summary>
  239. /// Handles the click event of the cmExit control.
  240. /// </summary>
  241. /// <param name="sender">The source of the event.</param>
  242. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  243. private async void cmExit_click(object sender, RoutedEventArgs e)
  244. {
  245. await _appHost.Shutdown().ConfigureAwait(false);
  246. }
  247. /// <summary>
  248. /// Handles the click event of the cmdReloadServer control.
  249. /// </summary>
  250. /// <param name="sender">The source of the event.</param>
  251. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  252. private async void cmdReloadServer_click(object sender, RoutedEventArgs e)
  253. {
  254. await _appHost.Restart().ConfigureAwait(false);
  255. }
  256. /// <summary>
  257. /// Handles the click event of the CmShowLogWindow control.
  258. /// </summary>
  259. /// <param name="sender">The source of the event.</param>
  260. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  261. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  262. {
  263. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  264. _configurationManager.SaveConfiguration();
  265. LoadLogWindow(sender, e);
  266. }
  267. #endregion
  268. }
  269. }