MainWindow.xaml.cs 13 KB

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