MainWindow.xaml.cs 11 KB

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