MainWindow.xaml.cs 13 KB

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