NewItemNotifier.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Windows;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Plugins;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.ServerApplication.Controls;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Windows.Controls.Primitives;
  11. namespace MediaBrowser.ServerApplication
  12. {
  13. /// <summary>
  14. /// Class NewItemNotifier
  15. /// </summary>
  16. public class NewItemNotifier : IServerEntryPoint
  17. {
  18. /// <summary>
  19. /// Holds the list of new items to display when the NewItemTimer expires
  20. /// </summary>
  21. private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();
  22. /// <summary>
  23. /// The amount of time to wait before showing a new item notification
  24. /// This allows us to group items together into one notification
  25. /// </summary>
  26. private const int NewItemDelay = 60000;
  27. /// <summary>
  28. /// The current new item timer
  29. /// </summary>
  30. /// <value>The new item timer.</value>
  31. private Timer NewItemTimer { get; set; }
  32. /// <summary>
  33. /// The _library manager
  34. /// </summary>
  35. private readonly ILibraryManager _libraryManager;
  36. /// <summary>
  37. /// The _logger
  38. /// </summary>
  39. private readonly ILogger _logger;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="NewItemNotifier" /> class.
  42. /// </summary>
  43. /// <param name="libraryManager">The library manager.</param>
  44. /// <param name="logManager">The log manager.</param>
  45. public NewItemNotifier(ILibraryManager libraryManager, ILogManager logManager)
  46. {
  47. _logger = logManager.GetLogger("NewItemNotifier");
  48. _libraryManager = libraryManager;
  49. }
  50. /// <summary>
  51. /// Runs this instance.
  52. /// </summary>
  53. public void Run()
  54. {
  55. _libraryManager.LibraryChanged += libraryManager_LibraryChanged;
  56. }
  57. /// <summary>
  58. /// Handles the LibraryChanged event of the libraryManager control.
  59. /// </summary>
  60. /// <param name="sender">The source of the event.</param>
  61. /// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
  62. void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e)
  63. {
  64. var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList();
  65. // Use a timer to prevent lots of these notifications from showing in a short period of time
  66. if (newItems.Count > 0)
  67. {
  68. lock (_newlyAddedItems)
  69. {
  70. _newlyAddedItems.AddRange(newItems);
  71. if (NewItemTimer == null)
  72. {
  73. NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
  74. }
  75. else
  76. {
  77. NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
  78. }
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Called when the new item timer expires
  84. /// </summary>
  85. /// <param name="state">The state.</param>
  86. private void NewItemTimerCallback(object state)
  87. {
  88. List<BaseItem> newItems;
  89. // Lock the list and release all resources
  90. lock (_newlyAddedItems)
  91. {
  92. newItems = _newlyAddedItems.ToList();
  93. _newlyAddedItems.Clear();
  94. NewItemTimer.Dispose();
  95. NewItemTimer = null;
  96. }
  97. // Show the notification
  98. if (newItems.Count == 1)
  99. {
  100. Application.Current.Dispatcher.InvokeAsync(() =>
  101. {
  102. var window = (MainWindow)Application.Current.MainWindow;
  103. window.Dispatcher.InvokeAsync(() => window.MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger)
  104. {
  105. DataContext = newItems[0]
  106. }, PopupAnimation.Slide, 6000));
  107. });
  108. }
  109. else if (newItems.Count > 1)
  110. {
  111. Application.Current.Dispatcher.InvokeAsync(() =>
  112. {
  113. var window = (MainWindow)Application.Current.MainWindow;
  114. window.Dispatcher.InvokeAsync(() => window.MbTaskbarIcon.ShowCustomBalloon(new MultiItemUpdateNotification(_logger)
  115. {
  116. DataContext = newItems
  117. }, PopupAnimation.Slide, 6000));
  118. });
  119. }
  120. }
  121. /// <summary>
  122. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  123. /// </summary>
  124. public void Dispose()
  125. {
  126. _libraryManager.LibraryChanged -= libraryManager_LibraryChanged;
  127. }
  128. }
  129. }