MainWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.ConfigurationUpdated += Instance_ConfigurationUpdated;
  91. }
  92. /// <summary>
  93. /// Handles the ConfigurationUpdated event of the Instance control.
  94. /// </summary>
  95. /// <param name="sender">The source of the event.</param>
  96. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  97. void Instance_ConfigurationUpdated(object sender, EventArgs e)
  98. {
  99. Dispatcher.InvokeAsync(() =>
  100. {
  101. var developerToolsVisibility = Kernel.Instance.Configuration.EnableDeveloperTools
  102. ? Visibility.Visible
  103. : Visibility.Collapsed;
  104. separatorDeveloperTools.Visibility = developerToolsVisibility;
  105. cmdReloadServer.Visibility = developerToolsVisibility;
  106. cmOpenExplorer.Visibility = developerToolsVisibility;
  107. var logWindow = App.Instance.Windows.OfType<LogWindow>().FirstOrDefault();
  108. if ((logWindow == null && Kernel.Instance.Configuration.ShowLogWindow) || (logWindow != null && !Kernel.Instance.Configuration.ShowLogWindow))
  109. {
  110. _logManager.ReloadLogger(Kernel.Instance.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  111. }
  112. });
  113. }
  114. /// <summary>
  115. /// Handles the LibraryChanged event of the Instance control.
  116. /// </summary>
  117. /// <param name="sender">The source of the event.</param>
  118. /// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
  119. void Instance_LibraryChanged(object sender, ChildrenChangedEventArgs e)
  120. {
  121. var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList();
  122. // Use a timer to prevent lots of these notifications from showing in a short period of time
  123. if (newItems.Count > 0)
  124. {
  125. lock (_newlyAddedItems)
  126. {
  127. _newlyAddedItems.AddRange(newItems);
  128. if (NewItemTimer == null)
  129. {
  130. NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
  131. }
  132. else
  133. {
  134. NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Called when the new item timer expires
  141. /// </summary>
  142. /// <param name="state">The state.</param>
  143. private void NewItemTimerCallback(object state)
  144. {
  145. List<BaseItem> newItems;
  146. // Lock the list and release all resources
  147. lock (_newlyAddedItems)
  148. {
  149. newItems = _newlyAddedItems.ToList();
  150. _newlyAddedItems.Clear();
  151. NewItemTimer.Dispose();
  152. NewItemTimer = null;
  153. }
  154. // Show the notification
  155. if (newItems.Count == 1)
  156. {
  157. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger)
  158. {
  159. DataContext = newItems[0]
  160. }, PopupAnimation.Slide, 6000));
  161. }
  162. else if (newItems.Count > 1)
  163. {
  164. Dispatcher.InvokeAsync(() => MbTaskbarIcon.ShowCustomBalloon(new MultiItemUpdateNotification(_logger)
  165. {
  166. DataContext = newItems
  167. }, PopupAnimation.Slide, 6000));
  168. }
  169. }
  170. /// <summary>
  171. /// Loads the log window.
  172. /// </summary>
  173. /// <param name="sender">The sender.</param>
  174. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  175. void LoadLogWindow(object sender, EventArgs args)
  176. {
  177. CloseLogWindow();
  178. Dispatcher.InvokeAsync(() =>
  179. {
  180. // Add our log window if specified
  181. if (Kernel.Instance.Configuration.ShowLogWindow)
  182. {
  183. Trace.Listeners.Add(new WindowTraceListener(new LogWindow(Kernel.Instance)));
  184. }
  185. else
  186. {
  187. Trace.Listeners.Remove("MBLogWindow");
  188. }
  189. // Set menu option indicator
  190. cmShowLogWindow.IsChecked = Kernel.Instance.Configuration.ShowLogWindow;
  191. }, DispatcherPriority.Normal);
  192. }
  193. /// <summary>
  194. /// Closes the log window.
  195. /// </summary>
  196. void CloseLogWindow()
  197. {
  198. Dispatcher.InvokeAsync(() =>
  199. {
  200. foreach (var win in Application.Current.Windows.OfType<LogWindow>())
  201. {
  202. win.Close();
  203. }
  204. });
  205. }
  206. /// <summary>
  207. /// Kernels the reload completed.
  208. /// </summary>
  209. /// <param name="sender">The sender.</param>
  210. /// <param name="e">The e.</param>
  211. void KernelReloadCompleted(object sender, EventArgs e)
  212. {
  213. Kernel.Instance.LibraryManager.LibraryChanged -= Instance_LibraryChanged;
  214. Kernel.Instance.LibraryManager.LibraryChanged += Instance_LibraryChanged;
  215. if (_appHost.IsFirstRun)
  216. {
  217. LaunchStartupWizard();
  218. }
  219. }
  220. /// <summary>
  221. /// Launches the startup wizard.
  222. /// </summary>
  223. private void LaunchStartupWizard()
  224. {
  225. App.OpenDashboardPage("wizardStart.html");
  226. }
  227. /// <summary>
  228. /// Handles the Click event of the cmdApiDocs control.
  229. /// </summary>
  230. /// <param name="sender">The source of the event.</param>
  231. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  232. void cmdApiDocs_Click(object sender, EventArgs e)
  233. {
  234. App.OpenUrl("http://localhost:" + Controller.Kernel.Instance.Configuration.HttpServerPortNumber + "/" +
  235. Controller.Kernel.Instance.WebApplicationName + "/metadata");
  236. }
  237. /// <summary>
  238. /// Occurs when [property changed].
  239. /// </summary>
  240. public event PropertyChangedEventHandler PropertyChanged;
  241. /// <summary>
  242. /// Called when [property changed].
  243. /// </summary>
  244. /// <param name="info">The info.</param>
  245. public void OnPropertyChanged(String info)
  246. {
  247. if (PropertyChanged != null)
  248. {
  249. try
  250. {
  251. PropertyChanged(this, new PropertyChangedEventArgs(info));
  252. }
  253. catch (Exception ex)
  254. {
  255. _logger.ErrorException("Error in event handler", ex);
  256. }
  257. }
  258. }
  259. #region Context Menu events
  260. /// <summary>
  261. /// Handles the click event of the cmOpenExplorer control.
  262. /// </summary>
  263. /// <param name="sender">The source of the event.</param>
  264. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  265. private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
  266. {
  267. (new LibraryExplorer(_jsonSerializer, _logger, _appHost)).Show();
  268. }
  269. /// <summary>
  270. /// Handles the click event of the cmOpenDashboard control.
  271. /// </summary>
  272. /// <param name="sender">The source of the event.</param>
  273. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  274. private void cmOpenDashboard_click(object sender, RoutedEventArgs e)
  275. {
  276. App.OpenDashboard();
  277. }
  278. /// <summary>
  279. /// Handles the click event of the cmVisitCT control.
  280. /// </summary>
  281. /// <param name="sender">The source of the event.</param>
  282. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  283. private void cmVisitCT_click(object sender, RoutedEventArgs e)
  284. {
  285. App.OpenUrl("http://community.mediabrowser.tv/");
  286. }
  287. /// <summary>
  288. /// Handles the click event of the cmdBrowseLibrary control.
  289. /// </summary>
  290. /// <param name="sender">The source of the event.</param>
  291. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  292. private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e)
  293. {
  294. App.OpenDashboardPage("index.html");
  295. }
  296. /// <summary>
  297. /// Handles the click event of the cmExit control.
  298. /// </summary>
  299. /// <param name="sender">The source of the event.</param>
  300. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  301. private void cmExit_click(object sender, RoutedEventArgs e)
  302. {
  303. Application.Current.Shutdown();
  304. }
  305. /// <summary>
  306. /// Handles the click event of the cmdReloadServer control.
  307. /// </summary>
  308. /// <param name="sender">The source of the event.</param>
  309. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  310. private void cmdReloadServer_click(object sender, RoutedEventArgs e)
  311. {
  312. App.Instance.Restart();
  313. }
  314. /// <summary>
  315. /// Handles the click event of the CmShowLogWindow control.
  316. /// </summary>
  317. /// <param name="sender">The source of the event.</param>
  318. /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
  319. private void CmShowLogWindow_click(object sender, RoutedEventArgs e)
  320. {
  321. Kernel.Instance.Configuration.ShowLogWindow = !Kernel.Instance.Configuration.ShowLogWindow;
  322. Kernel.Instance.SaveConfiguration();
  323. LoadLogWindow(sender, e);
  324. }
  325. #endregion
  326. }
  327. }