MainWindow.xaml.cs 9.4 KB

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