MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. separatorDeveloperTools.Visibility = Visibility.Visible;
  120. cmdReloadServer.Visibility = Visibility.Visible;
  121. cmOpenExplorer.Visibility = Visibility.Visible;
  122. cmShowLogWindow.Visibility = Visibility.Visible;
  123. });
  124. }
  125. /// <summary>
  126. /// Loads the log window.
  127. /// </summary>
  128. /// <param name="sender">The sender.</param>
  129. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  130. void LoadLogWindow(object sender, EventArgs args)
  131. {
  132. CloseLogWindow();
  133. Dispatcher.InvokeAsync(() =>
  134. {
  135. // Add our log window if specified
  136. if (_configurationManager.Configuration.ShowLogWindow)
  137. {
  138. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  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. Dispatcher.InvokeAsync(() =>
  154. {
  155. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  156. {
  157. win.Close();
  158. }
  159. });
  160. }
  161. /// <summary>
  162. /// Handles the Click event of the cmdApiDocs control.
  163. /// </summary>
  164. /// <param name="sender">The source of the event.</param>
  165. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  166. void cmdApiDocs_Click(object sender, EventArgs e)
  167. {
  168. BrowserLauncher.OpenStandardApiDocumentation(_configurationManager, _appHost, _logger);
  169. }
  170. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  171. {
  172. BrowserLauncher.OpenSwagger(_configurationManager, _appHost, _logger);
  173. }
  174. void cmdGithubWiki_Click(object sender, EventArgs e)
  175. {
  176. BrowserLauncher.OpenGithub(_logger);
  177. }
  178. /// <summary>
  179. /// Occurs when [property changed].
  180. /// </summary>
  181. public event PropertyChangedEventHandler PropertyChanged;
  182. /// <summary>
  183. /// Called when [property changed].
  184. /// </summary>
  185. /// <param name="info">The info.</param>
  186. public void OnPropertyChanged(String info)
  187. {
  188. if (PropertyChanged != null)
  189. {
  190. try
  191. {
  192. PropertyChanged(this, new PropertyChangedEventArgs(info));
  193. }
  194. catch (Exception ex)
  195. {
  196. _logger.ErrorException("Error in event handler", ex);
  197. }
  198. }
  199. }
  200. #region Context Menu events
  201. /// <summary>
  202. /// Handles the click event of the cmOpenExplorer control.
  203. /// </summary>
  204. /// <param name="sender">The source of the event.</param>
  205. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  206. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  207. {
  208. new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager, _displayPreferencesManager, _itemRepository).Show();
  209. }
  210. /// <summary>
  211. /// Handles the click event of the cmOpenDashboard control.
  212. /// </summary>
  213. /// <param name="sender">The source of the event.</param>
  214. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  215. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  216. {
  217. BrowserLauncher.OpenDashboard(_userManager, _configurationManager, _appHost, _logger);
  218. }
  219. /// <summary>
  220. /// Handles the click event of the cmVisitCT control.
  221. /// </summary>
  222. /// <param name="sender">The source of the event.</param>
  223. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  224. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  225. {
  226. BrowserLauncher.OpenCommunity(_logger);
  227. }
  228. /// <summary>
  229. /// Handles the click event of the cmdBrowseLibrary control.
  230. /// </summary>
  231. /// <param name="sender">The source of the event.</param>
  232. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  233. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  234. {
  235. BrowserLauncher.OpenWebClient(_userManager, _configurationManager, _appHost, _logger);
  236. }
  237. /// <summary>
  238. /// Handles the click event of the cmExit control.
  239. /// </summary>
  240. /// <param name="sender">The source of the event.</param>
  241. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  242. private async void cmExit_click(object sender, RoutedEventArgs e)
  243. {
  244. await _appHost.Shutdown().ConfigureAwait(false);
  245. }
  246. /// <summary>
  247. /// Handles the click event of the cmdReloadServer control.
  248. /// </summary>
  249. /// <param name="sender">The source of the event.</param>
  250. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  251. private async void cmdReloadServer_click(object sender, RoutedEventArgs e)
  252. {
  253. await _appHost.Restart().ConfigureAwait(false);
  254. }
  255. /// <summary>
  256. /// Handles the click event of the CmShowLogWindow control.
  257. /// </summary>
  258. /// <param name="sender">The source of the event.</param>
  259. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  260. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  261. {
  262. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  263. _configurationManager.SaveConfiguration();
  264. LoadLogWindow(sender, e);
  265. }
  266. #endregion
  267. }
  268. }