MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  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. namespace MediaBrowser.ServerApplication
  16. {
  17. /// <summary>
  18. /// Interaction logic for MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window, INotifyPropertyChanged
  21. {
  22. /// <summary>
  23. /// The _logger
  24. /// </summary>
  25. private readonly ILogger _logger;
  26. /// <summary>
  27. /// The _app host
  28. /// </summary>
  29. private readonly IApplicationHost _appHost;
  30. /// <summary>
  31. /// The _log manager
  32. /// </summary>
  33. private readonly ILogManager _logManager;
  34. /// <summary>
  35. /// The _configuration manager
  36. /// </summary>
  37. private readonly IServerConfigurationManager _configurationManager;
  38. private readonly IUserManager _userManager;
  39. private readonly ILibraryManager _libraryManager;
  40. private readonly IJsonSerializer _jsonSerializer;
  41. private readonly IDisplayPreferencesManager _displayPreferencesManager;
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="MainWindow" /> class.
  44. /// </summary>
  45. /// <param name="logManager">The log manager.</param>
  46. /// <param name="appHost">The app host.</param>
  47. /// <param name="configurationManager">The configuration manager.</param>
  48. /// <param name="userManager">The user manager.</param>
  49. /// <param name="libraryManager">The library manager.</param>
  50. /// <param name="jsonSerializer">The json serializer.</param>
  51. /// <param name="displayPreferencesManager">The display preferences manager.</param>
  52. /// <exception cref="System.ArgumentNullException">logger</exception>
  53. public MainWindow(ILogManager logManager, IApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager)
  54. {
  55. if (logManager == null)
  56. {
  57. throw new ArgumentNullException("logManager");
  58. }
  59. if (appHost == null)
  60. {
  61. throw new ArgumentNullException("appHost");
  62. }
  63. if (configurationManager == null)
  64. {
  65. throw new ArgumentNullException("configurationManager");
  66. }
  67. _logger = logManager.GetLogger("MainWindow");
  68. _appHost = appHost;
  69. _logManager = logManager;
  70. _configurationManager = configurationManager;
  71. _userManager = userManager;
  72. _libraryManager = libraryManager;
  73. _jsonSerializer = jsonSerializer;
  74. _displayPreferencesManager = displayPreferencesManager;
  75. InitializeComponent();
  76. Loaded += MainWindowLoaded;
  77. }
  78. /// <summary>
  79. /// Mains the window loaded.
  80. /// </summary>
  81. /// <param name="sender">The sender.</param>
  82. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  83. void MainWindowLoaded(object sender, RoutedEventArgs e)
  84. {
  85. DataContext = this;
  86. UpdateButtons();
  87. LoadLogWindow(null, EventArgs.Empty);
  88. _logManager.LoggerLoaded += LoadLogWindow;
  89. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  90. if (_appHost.IsFirstRun)
  91. {
  92. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowBalloonTip("Media Browser", "Welcome to Media Browser Server!", Hardcodet.Wpf.TaskbarNotification.BalloonIcon.None));
  93. }
  94. }
  95. /// <summary>
  96. /// Handles the ConfigurationUpdated event of the Instance control.
  97. /// </summary>
  98. /// <param name="sender">The source of the event.</param>
  99. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  100. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  101. {
  102. UpdateButtons();
  103. Dispatcher.InvokeAsync(() =>
  104. {
  105. var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
  106. if ((logWindow == null && _configurationManager.Configuration.ShowLogWindow) || (logWindow != null && !_configurationManager.Configuration.ShowLogWindow))
  107. {
  108. _logManager.ReloadLogger(_configurationManager.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  109. }
  110. });
  111. }
  112. private void UpdateButtons()
  113. {
  114. Dispatcher.InvokeAsync(() =>
  115. {
  116. var developerToolsVisibility = _configurationManager.Configuration.EnableDeveloperTools
  117. ? Visibility.Visible
  118. : Visibility.Collapsed;
  119. separatorDeveloperTools.Visibility = developerToolsVisibility;
  120. cmdReloadServer.Visibility = developerToolsVisibility;
  121. cmOpenExplorer.Visibility = developerToolsVisibility;
  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. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  138. }
  139. else
  140. {
  141. Trace.Listeners.Remove("MBLogWindow");
  142. }
  143. // Set menu option indicator
  144. cmShowLogWindow.IsChecked = _configurationManager.Configuration.ShowLogWindow;
  145. }, DispatcherPriority.Normal);
  146. }
  147. /// <summary>
  148. /// Closes the log window.
  149. /// </summary>
  150. void CloseLogWindow()
  151. {
  152. Dispatcher.InvokeAsync(() =>
  153. {
  154. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  155. {
  156. win.Close();
  157. }
  158. });
  159. }
  160. /// <summary>
  161. /// Handles the Click event of the cmdApiDocs control.
  162. /// </summary>
  163. /// <param name="sender">The source of the event.</param>
  164. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  165. void cmdApiDocs_Click(object sender, EventArgs e)
  166. {
  167. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  168. Kernel.Instance.WebApplicationName + "/metadata");
  169. }
  170. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  171. {
  172. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  173. Kernel.Instance.WebApplicationName + "/swagger-ui/index.html");
  174. }
  175. void cmdGithubWiki_Click(object sender, EventArgs e)
  176. {
  177. App.OpenUrl("https://github.com/MediaBrowser/MediaBrowser/wiki");
  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. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  219. OpenDashboard(user);
  220. }
  221. /// <summary>
  222. /// Opens the dashboard.
  223. /// </summary>
  224. private void OpenDashboard(User loggedInUser)
  225. {
  226. App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager);
  227. }
  228. /// <summary>
  229. /// Handles the click event of the cmVisitCT 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 cmVisitCT_click(object sender, RoutedEventArgs e)
  234. {
  235. App.OpenUrl("http://community.mediabrowser.tv/");
  236. }
  237. /// <summary>
  238. /// Handles the click event of the cmdBrowseLibrary 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 void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  243. {
  244. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  245. App.OpenDashboardPage("index.html", user, _configurationManager);
  246. }
  247. /// <summary>
  248. /// Handles the click event of the cmExit 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 void cmExit_click(object sender, RoutedEventArgs e)
  253. {
  254. Application.Current.Shutdown();
  255. }
  256. /// <summary>
  257. /// Handles the click event of the cmdReloadServer 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 cmdReloadServer_click(object sender, RoutedEventArgs e)
  262. {
  263. App.Instance.Restart();
  264. }
  265. /// <summary>
  266. /// Handles the click event of the CmShowLogWindow control.
  267. /// </summary>
  268. /// <param name="sender">The source of the event.</param>
  269. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  270. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  271. {
  272. _configurationManager.Configuration.ShowLogWindow = !_configurationManager.Configuration.ShowLogWindow;
  273. _configurationManager.SaveConfiguration();
  274. LoadLogWindow(sender, e);
  275. }
  276. #endregion
  277. }
  278. }