MainWindow.xaml.cs 13 KB

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