MainWindow.xaml.cs 11 KB

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