MainWindow.xaml.cs 10 KB

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