MainWindow.xaml.cs 14 KB

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