MainWindow.xaml.cs 14 KB

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