MainWindow.xaml.cs 11 KB

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