MainWindow.xaml.cs 12 KB

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