NewItemNotifier.cs 4.7 KB

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