MainWindow.xaml.cs 12 KB

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