MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using MahApps.Metro.Controls;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Serialization;
  10. using MediaBrowser.ServerApplication.Logging;
  11. using System;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Windows;
  16. using System.Windows.Threading;
  17. namespace MediaBrowser.ServerApplication
  18. {
  19. /// <summary>
  20. /// Interaction logic for MainWindow.xaml
  21. /// </summary>
  22. public partial class MainWindow : Window, INotifyPropertyChanged
  23. {
  24. /// <summary>
  25. /// The _logger
  26. /// </summary>
  27. private readonly ILogger _logger;
  28. /// <summary>
  29. /// The _app host
  30. /// </summary>
  31. private readonly IServerApplicationHost _appHost;
  32. /// <summary>
  33. /// The _log manager
  34. /// </summary>
  35. private readonly ILogManager _logManager;
  36. /// <summary>
  37. /// The _configuration manager
  38. /// </summary>
  39. private readonly IServerConfigurationManager _configurationManager;
  40. private readonly IUserManager _userManager;
  41. private readonly ILibraryManager _libraryManager;
  42. private readonly IJsonSerializer _jsonSerializer;
  43. private readonly IDisplayPreferencesRepository _displayPreferencesManager;
  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)
  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. _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 logWindow = App.Current.Windows.OfType<LogWindow>().FirstOrDefault();
  108. if ((logWindow == null && _configurationManager.Configuration.ShowLogWindow) || (logWindow != null && !_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. var developerToolsVisibility = _configurationManager.Configuration.EnableDeveloperTools
  119. ? Visibility.Visible
  120. : Visibility.Collapsed;
  121. separatorDeveloperTools.Visibility = developerToolsVisibility;
  122. cmdReloadServer.Visibility = developerToolsVisibility;
  123. cmOpenExplorer.Visibility = developerToolsVisibility;
  124. cmShowLogWindow.Visibility = developerToolsVisibility;
  125. });
  126. }
  127. /// <summary>
  128. /// Loads the log window.
  129. /// </summary>
  130. /// <param name="sender">The sender.</param>
  131. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  132. void LoadLogWindow(object sender, EventArgs args)
  133. {
  134. CloseLogWindow();
  135. Dispatcher.InvokeAsync(() =>
  136. {
  137. // Add our log window if specified
  138. if (_configurationManager.Configuration.ShowLogWindow)
  139. {
  140. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  141. }
  142. else
  143. {
  144. Trace.Listeners.Remove("MBLogWindow");
  145. }
  146. // Set menu option indicator
  147. cmShowLogWindow.IsChecked = _configurationManager.Configuration.ShowLogWindow;
  148. }, DispatcherPriority.Normal);
  149. }
  150. /// <summary>
  151. /// Closes the log window.
  152. /// </summary>
  153. void CloseLogWindow()
  154. {
  155. Dispatcher.InvokeAsync(() =>
  156. {
  157. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  158. {
  159. win.Close();
  160. }
  161. });
  162. }
  163. /// <summary>
  164. /// Handles the Click event of the cmdApiDocs control.
  165. /// </summary>
  166. /// <param name="sender">The source of the event.</param>
  167. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  168. void cmdApiDocs_Click(object sender, EventArgs e)
  169. {
  170. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  171. _appHost.WebApplicationName + "/metadata");
  172. }
  173. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  174. {
  175. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  176. _appHost.WebApplicationName + "/swagger-ui/index.html");
  177. }
  178. void cmdGithubWiki_Click(object sender, EventArgs e)
  179. {
  180. App.OpenUrl("https://github.com/MediaBrowser/MediaBrowser/wiki");
  181. }
  182. /// <summary>
  183. /// Occurs when [property changed].
  184. /// </summary>
  185. public event PropertyChangedEventHandler PropertyChanged;
  186. /// <summary>
  187. /// Called when [property changed].
  188. /// </summary>
  189. /// <param name="info">The info.</param>
  190. public void OnPropertyChanged(String info)
  191. {
  192. if (PropertyChanged != null)
  193. {
  194. try
  195. {
  196. PropertyChanged(this, new PropertyChangedEventArgs(info));
  197. }
  198. catch (Exception ex)
  199. {
  200. _logger.ErrorException("Error in event handler", ex);
  201. }
  202. }
  203. }
  204. #region Context Menu events
  205. /// <summary>
  206. /// Handles the click event of the cmOpenExplorer control.
  207. /// </summary>
  208. /// <param name="sender">The source of the event.</param>
  209. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  210. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  211. {
  212. new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager, _displayPreferencesManager).Show();
  213. }
  214. /// <summary>
  215. /// Handles the click event of the cmOpenDashboard control.
  216. /// </summary>
  217. /// <param name="sender">The source of the event.</param>
  218. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  219. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  220. {
  221. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  222. OpenDashboard(user);
  223. }
  224. /// <summary>
  225. /// Opens the dashboard.
  226. /// </summary>
  227. private void OpenDashboard(User loggedInUser)
  228. {
  229. App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager, _appHost);
  230. }
  231. /// <summary>
  232. /// Handles the click event of the cmVisitCT 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 cmVisitCT_click(object sender, RoutedEventArgs e)
  237. {
  238. App.OpenUrl("http://community.mediabrowser.tv/");
  239. }
  240. /// <summary>
  241. /// Handles the click event of the cmdBrowseLibrary 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 void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  246. {
  247. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  248. App.OpenDashboardPage("index.html", user, _configurationManager, _appHost);
  249. }
  250. /// <summary>
  251. /// Handles the click event of the cmExit control.
  252. /// </summary>
  253. /// <param name="sender">The source of the event.</param>
  254. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  255. private async void cmExit_click(object sender, RoutedEventArgs e)
  256. {
  257. await _appHost.Shutdown().ConfigureAwait(false);
  258. }
  259. /// <summary>
  260. /// Handles the click event of the cmdReloadServer control.
  261. /// </summary>
  262. /// <param name="sender">The source of the event.</param>
  263. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  264. private async void cmdReloadServer_click(object sender, RoutedEventArgs e)
  265. {
  266. await _appHost.Restart().ConfigureAwait(false);
  267. }
  268. /// <summary>
  269. /// Handles the click event of the CmShowLogWindow control.
  270. /// </summary>
  271. /// <param name="sender">The source of the event.</param>
  272. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  273. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  274. {
  275. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  276. _configurationManager.SaveConfiguration();
  277. LoadLogWindow(sender, e);
  278. }
  279. #endregion
  280. }
  281. }