MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using MediaBrowser.ServerApplication.Controls;
  8. using MediaBrowser.ServerApplication.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Threading;
  15. using System.Windows;
  16. using System.Windows.Controls.Primitives;
  17. using System.Windows.Threading;
  18. namespace MediaBrowser.ServerApplication
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window, INotifyPropertyChanged
  24. {
  25. /// <summary>
  26. /// Holds the list of new items to display when the NewItemTimer expires
  27. /// </summary>
  28. private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();
  29. /// <summary>
  30. /// The amount of time to wait before showing a new item notification
  31. /// This allows us to group items together into one notification
  32. /// </summary>
  33. private const int NewItemDelay = 60000;
  34. /// <summary>
  35. /// The current new item timer
  36. /// </summary>
  37. /// <value>The new item timer.</value>
  38. private Timer NewItemTimer { get; set; }
  39. /// <summary>
  40. /// The _logger
  41. /// </summary>
  42. private readonly ILogger _logger;
  43. /// <summary>
  44. /// The _app host
  45. /// </summary>
  46. private readonly IApplicationHost _appHost;
  47. /// <summary>
  48. /// The _log manager
  49. /// </summary>
  50. private readonly ILogManager _logManager;
  51. private readonly ILibraryManager _libraryManager;
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="MainWindow" /> class.
  54. /// </summary>
  55. /// <param name="jsonSerializer">The json serializer.</param>
  56. /// <param name="logger">The logger.</param>
  57. /// <param name="appHost">The app host.</param>
  58. /// <exception cref="System.ArgumentNullException">logger</exception>
  59. public MainWindow(ILogManager logManager, IApplicationHost appHost, ILibraryManager libraryManager)
  60. {
  61. if (logManager == null)
  62. {
  63. throw new ArgumentNullException("logManager");
  64. }
  65. _logger = logManager.GetLogger("MainWindow");
  66. _appHost = appHost;
  67. _logManager = logManager;
  68. _libraryManager = libraryManager;
  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. Instance_ConfigurationUpdated(null, EventArgs.Empty);
  81. Kernel.Instance.ReloadCompleted += KernelReloadCompleted;
  82. _logManager.LoggerLoaded += LoadLogWindow;
  83. Kernel.Instance.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. Dispatcher.InvokeAsync(() =>
  93. {
  94. var developerToolsVisibility = Kernel.Instance.Configuration.EnableDeveloperTools
  95. ? Visibility.Visible
  96. : Visibility.Collapsed;
  97. separatorDeveloperTools.Visibility = developerToolsVisibility;
  98. cmdReloadServer.Visibility = developerToolsVisibility;
  99. cmOpenExplorer.Visibility = developerToolsVisibility;
  100. var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
  101. if ((logWindow == null && Kernel.Instance.Configuration.ShowLogWindow) || (logWindow != null && !Kernel.Instance.Configuration.ShowLogWindow))
  102. {
  103. _logManager.ReloadLogger(Kernel.Instance.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  104. }
  105. });
  106. }
  107. /// <summary>
  108. /// Handles the LibraryChanged event of the Instance control.
  109. /// </summary>
  110. /// <param name="sender">The source of the event.</param>
  111. /// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
  112. void Instance_LibraryChanged(object sender, ChildrenChangedEventArgs e)
  113. {
  114. var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList();
  115. // Use a timer to prevent lots of these notifications from showing in a short period of time
  116. if (newItems.Count > 0)
  117. {
  118. lock (_newlyAddedItems)
  119. {
  120. _newlyAddedItems.AddRange(newItems);
  121. if (NewItemTimer == null)
  122. {
  123. NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
  124. }
  125. else
  126. {
  127. NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
  128. }
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Called when the new item timer expires
  134. /// </summary>
  135. /// <param name="state">The state.</param>
  136. private void NewItemTimerCallback(object state)
  137. {
  138. List<BaseItem> newItems;
  139. // Lock the list and release all resources
  140. lock (_newlyAddedItems)
  141. {
  142. newItems = _newlyAddedItems.ToList();
  143. _newlyAddedItems.Clear();
  144. NewItemTimer.Dispose();
  145. NewItemTimer = null;
  146. }
  147. // Show the notification
  148. if (newItems.Count == 1)
  149. {
  150. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger)
  151. {
  152. DataContext = newItems[0]
  153. }, PopupAnimation.Slide, 6000));
  154. }
  155. else if (newItems.Count > 1)
  156. {
  157. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new MultiItemUpdateNotification(_logger)
  158. {
  159. DataContext = newItems
  160. }, PopupAnimation.Slide, 6000));
  161. }
  162. }
  163. /// <summary>
  164. /// Loads the log window.
  165. /// </summary>
  166. /// <param name="sender">The sender.</param>
  167. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  168. void LoadLogWindow(object sender, EventArgs args)
  169. {
  170. CloseLogWindow();
  171. Dispatcher.InvokeAsync(() =>
  172. {
  173. // Add our log window if specified
  174. if (Kernel.Instance.Configuration.ShowLogWindow)
  175. {
  176. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(Kernel.Instance)));
  177. }
  178. else
  179. {
  180. Trace.Listeners.Remove("MBLogWindow");
  181. }
  182. // Set menu option indicator
  183. cmShowLogWindow.IsChecked = Kernel.Instance.Configuration.ShowLogWindow;
  184. }, DispatcherPriority.Normal);
  185. }
  186. /// <summary>
  187. /// Closes the log window.
  188. /// </summary>
  189. void CloseLogWindow()
  190. {
  191. Dispatcher.InvokeAsync(() =>
  192. {
  193. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  194. {
  195. win.Close();
  196. }
  197. });
  198. }
  199. /// <summary>
  200. /// Kernels the reload completed.
  201. /// </summary>
  202. /// <param name="sender">The sender.</param>
  203. /// <param name="e">The e.</param>
  204. void KernelReloadCompleted(object sender, EventArgs e)
  205. {
  206. _libraryManager.LibraryChanged -= Instance_LibraryChanged;
  207. _libraryManager.LibraryChanged += Instance_LibraryChanged;
  208. if (_appHost.IsFirstRun)
  209. {
  210. LaunchStartupWizard();
  211. }
  212. }
  213. /// <summary>
  214. /// Launches the startup wizard.
  215. /// </summary>
  216. private void LaunchStartupWizard()
  217. {
  218. var user = _appHost.Resolve<IUserManager>().Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  219. App.OpenDashboardPage("wizardStart.html", user);
  220. }
  221. /// <summary>
  222. /// Handles the Click event of the cmdApiDocs control.
  223. /// </summary>
  224. /// <param name="sender">The source of the event.</param>
  225. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  226. void cmdApiDocs_Click(object sender, EventArgs e)
  227. {
  228. App.OpenUrl("http://localhost:" + Kernel.Instance.Configuration.HttpServerPortNumber + "/" +
  229. Kernel.Instance.WebApplicationName + "/metadata");
  230. }
  231. /// <summary>
  232. /// Occurs when [property changed].
  233. /// </summary>
  234. public event PropertyChangedEventHandler PropertyChanged;
  235. /// <summary>
  236. /// Called when [property changed].
  237. /// </summary>
  238. /// <param name="info">The info.</param>
  239. public void OnPropertyChanged(String info)
  240. {
  241. if (PropertyChanged != null)
  242. {
  243. try
  244. {
  245. PropertyChanged(this, new PropertyChangedEventArgs(info));
  246. }
  247. catch (Exception ex)
  248. {
  249. _logger.ErrorException("Error in event handler", ex);
  250. }
  251. }
  252. }
  253. #region Context Menu events
  254. /// <summary>
  255. /// Handles the click event of the cmOpenExplorer 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 cmOpenExplorer_click(object sender, RoutedEventArgs e)
  260. {
  261. var explorer = (LibraryExplorer)_appHost.CreateInstance(typeof(LibraryExplorer));
  262. explorer.Show();
  263. }
  264. /// <summary>
  265. /// Handles the click event of the cmOpenDashboard control.
  266. /// </summary>
  267. /// <param name="sender">The source of the event.</param>
  268. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  269. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  270. {
  271. var user = _appHost.Resolve<IUserManager>().Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  272. App.OpenDashboard(user);
  273. }
  274. /// <summary>
  275. /// Handles the click event of the cmVisitCT control.
  276. /// </summary>
  277. /// <param name="sender">The source of the event.</param>
  278. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  279. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  280. {
  281. App.OpenUrl("http://community.mediabrowser.tv/");
  282. }
  283. /// <summary>
  284. /// Handles the click event of the cmdBrowseLibrary control.
  285. /// </summary>
  286. /// <param name="sender">The source of the event.</param>
  287. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  288. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  289. {
  290. var user = _appHost.Resolve<IUserManager>().Users.FirstOrDefault(u => u.Configuration.IsAdministrator);
  291. App.OpenDashboardPage("index.html", user);
  292. }
  293. /// <summary>
  294. /// Handles the click event of the cmExit control.
  295. /// </summary>
  296. /// <param name="sender">The source of the event.</param>
  297. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  298. private void cmExit_click(object sender, RoutedEventArgs e)
  299. {
  300. Application.Current.Shutdown();
  301. }
  302. /// <summary>
  303. /// Handles the click event of the cmdReloadServer control.
  304. /// </summary>
  305. /// <param name="sender">The source of the event.</param>
  306. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  307. private void cmdReloadServer_click(object sender, RoutedEventArgs e)
  308. {
  309. App.Instance.Restart();
  310. }
  311. /// <summary>
  312. /// Handles the click event of the CmShowLogWindow control.
  313. /// </summary>
  314. /// <param name="sender">The source of the event.</param>
  315. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  316. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  317. {
  318. Kernel.Instance.Configuration.ShowLogWindow = !Kernel.Instance.Configuration.ShowLogWindow;
  319. Kernel.Instance.SaveConfiguration();
  320. LoadLogWindow(sender, e);
  321. }
  322. #endregion
  323. }
  324. }