MainWindow.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. LoadLogWindow(null, EventArgs.Empty);
  82. _logManager.LoggerLoaded += LoadLogWindow;
  83. _configurationManager.ConfigurationUpdated += Instance_ConfigurationUpdated;
  84. }
  85. /// <summary>
  86. /// Handles the ConfigurationUpdated event of the Instance control.
  87. /// </summary>
  88. /// <param name="sender">The source of the event.</param>
  89. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  90. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  91. {
  92. UpdateButtons();
  93. Dispatcher.InvokeAsync(() =>
  94. {
  95. var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
  96. if ((logWindow == null && _configurationManager.Configuration.ShowLogWindow) || (logWindow != null && !_configurationManager.Configuration.ShowLogWindow))
  97. {
  98. _logManager.ReloadLogger(_configurationManager.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  99. }
  100. });
  101. }
  102. private void UpdateButtons()
  103. {
  104. Dispatcher.InvokeAsync(() =>
  105. {
  106. var developerToolsVisibility = _configurationManager.Configuration.EnableDeveloperTools
  107. ? Visibility.Visible
  108. : Visibility.Collapsed;
  109. separatorDeveloperTools.Visibility = developerToolsVisibility;
  110. cmdReloadServer.Visibility = developerToolsVisibility;
  111. cmOpenExplorer.Visibility = developerToolsVisibility;
  112. });
  113. }
  114. /// <summary>
  115. /// Loads the log window.
  116. /// </summary>
  117. /// <param name="sender">The sender.</param>
  118. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  119. void LoadLogWindow(object sender, EventArgs args)
  120. {
  121. CloseLogWindow();
  122. Dispatcher.InvokeAsync(() =>
  123. {
  124. // Add our log window if specified
  125. if (_configurationManager.Configuration.ShowLogWindow)
  126. {
  127. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(_logManager)));
  128. }
  129. else
  130. {
  131. Trace.Listeners.Remove("MBLogWindow");
  132. }
  133. // Set menu option indicator
  134. cmShowLogWindow.IsChecked = _configurationManager.Configuration.ShowLogWindow;
  135. }, DispatcherPriority.Normal);
  136. }
  137. /// <summary>
  138. /// Closes the log window.
  139. /// </summary>
  140. void CloseLogWindow()
  141. {
  142. Dispatcher.InvokeAsync(() =>
  143. {
  144. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  145. {
  146. win.Close();
  147. }
  148. });
  149. }
  150. /// <summary>
  151. /// Handles the Click event of the cmdApiDocs control.
  152. /// </summary>
  153. /// <param name="sender">The source of the event.</param>
  154. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  155. void cmdApiDocs_Click(object sender, EventArgs e)
  156. {
  157. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  158. Kernel.Instance.WebApplicationName + "/metadata");
  159. }
  160. void cmdSwaggerApiDocs_Click(object sender, EventArgs e)
  161. {
  162. App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" +
  163. Kernel.Instance.WebApplicationName + "/swagger-ui/index.html");
  164. }
  165. void cmdGithubWiki_Click(object sender, EventArgs e)
  166. {
  167. App.OpenUrl("https://github.com/MediaBrowser/MediaBrowser/wiki");
  168. }
  169. /// <summary>
  170. /// Occurs when [property changed].
  171. /// </summary>
  172. public event PropertyChangedEventHandler PropertyChanged;
  173. /// <summary>
  174. /// Called when [property changed].
  175. /// </summary>
  176. /// <param name="info">The info.</param>
  177. public void OnPropertyChanged(String info)
  178. {
  179. if (PropertyChanged != null)
  180. {
  181. try
  182. {
  183. PropertyChanged(this, new PropertyChangedEventArgs(info));
  184. }
  185. catch (Exception ex)
  186. {
  187. _logger.ErrorException("Error in event handler", ex);
  188. }
  189. }
  190. }
  191. #region Context Menu events
  192. /// <summary>
  193. /// Handles the click event of the cmOpenExplorer control.
  194. /// </summary>
  195. /// <param name="sender">The source of the event.</param>
  196. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  197. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  198. {
  199. new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager).Show();
  200. }
  201. /// <summary>
  202. /// Handles the click event of the cmOpenDashboard 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 cmOpenDashboard_click(object sender, RoutedEventArgs e)
  207. {
  208. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  209. OpenDashboard(user);
  210. }
  211. /// <summary>
  212. /// Opens the dashboard.
  213. /// </summary>
  214. private void OpenDashboard(User loggedInUser)
  215. {
  216. App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager);
  217. }
  218. /// <summary>
  219. /// Handles the click event of the cmVisitCT control.
  220. /// </summary>
  221. /// <param name="sender">The source of the event.</param>
  222. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  223. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  224. {
  225. App.OpenUrl("http://community.mediabrowser.tv/");
  226. }
  227. /// <summary>
  228. /// Handles the click event of the cmdBrowseLibrary control.
  229. /// </summary>
  230. /// <param name="sender">The source of the event.</param>
  231. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  232. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  233. {
  234. var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  235. App.OpenDashboardPage("index.html", user, _configurationManager);
  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 void cmExit_click(object sender, RoutedEventArgs e)
  243. {
  244. Application.Current.Shutdown();
  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 void cmdReloadServer_click(object sender, RoutedEventArgs e)
  252. {
  253. App.Instance.Restart();
  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. }