MainWindow.xaml.cs 12 KB

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