aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/NewItemNotifier.cs
blob: 4864cef62d81f3d2a2aeefef7adddc120ae1cb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System.Windows;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.ServerApplication.Controls;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Controls.Primitives;

namespace MediaBrowser.ServerApplication
{
    /// <summary>
    /// Class NewItemNotifier
    /// </summary>
    public class NewItemNotifier : IServerEntryPoint
    {
        /// <summary>
        /// Holds the list of new items to display when the NewItemTimer expires
        /// </summary>
        private readonly List<BaseItem> _newlyAddedItems = new List<BaseItem>();

        /// <summary>
        /// The amount of time to wait before showing a new item notification
        /// This allows us to group items together into one notification
        /// </summary>
        private const int NewItemDelay = 60000;

        /// <summary>
        /// The current new item timer
        /// </summary>
        /// <value>The new item timer.</value>
        private Timer NewItemTimer { get; set; }

        /// <summary>
        /// The _library manager
        /// </summary>
        private readonly ILibraryManager _libraryManager;

        /// <summary>
        /// The _logger
        /// </summary>
        private readonly ILogger _logger;

        /// <summary>
        /// Initializes a new instance of the <see cref="NewItemNotifier" /> class.
        /// </summary>
        /// <param name="libraryManager">The library manager.</param>
        /// <param name="logManager">The log manager.</param>
        public NewItemNotifier(ILibraryManager libraryManager, ILogManager logManager)
        {
            _logger = logManager.GetLogger("NewItemNotifier");
            _libraryManager = libraryManager;
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public void Run()
        {
            _libraryManager.LibraryChanged += libraryManager_LibraryChanged;
        }

        /// <summary>
        /// Handles the LibraryChanged event of the libraryManager control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
        void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e)
        {
            var newItems = e.ItemsAdded.Where(i => !i.IsFolder).ToList();

            // Use a timer to prevent lots of these notifications from showing in a short period of time
            if (newItems.Count > 0)
            {
                lock (_newlyAddedItems)
                {
                    _newlyAddedItems.AddRange(newItems);

                    if (NewItemTimer == null)
                    {
                        NewItemTimer = new Timer(NewItemTimerCallback, null, NewItemDelay, Timeout.Infinite);
                    }
                    else
                    {
                        NewItemTimer.Change(NewItemDelay, Timeout.Infinite);
                    }
                }
            }
        }

        /// <summary>
        /// Called when the new item timer expires
        /// </summary>
        /// <param name="state">The state.</param>
        private void NewItemTimerCallback(object state)
        {
            List<BaseItem> newItems;

            // Lock the list and release all resources
            lock (_newlyAddedItems)
            {
                newItems = _newlyAddedItems.ToList();
                _newlyAddedItems.Clear();

                NewItemTimer.Dispose();
                NewItemTimer = null;
            }

            // Show the notification
            if (newItems.Count == 1)
            {
                Application.Current.Dispatcher.InvokeAsync(() =>
                {
                    var window = (MainWindow)Application.Current.MainWindow;

                    window.Dispatcher.InvokeAsync(() => window.MbTaskbarIcon.ShowCustomBalloon(new ItemUpdateNotification(_logger)
                    {
                        DataContext = newItems[0]

                    }, PopupAnimation.Slide, 6000));
                });
            }
            else if (newItems.Count > 1)
            {
                Application.Current.Dispatcher.InvokeAsync(() =>
                {
                    var window = (MainWindow)Application.Current.MainWindow;

                    window.Dispatcher.InvokeAsync(() => window.MbTaskbarIcon.ShowCustomBalloon(new MultiItemUpdateNotification(_logger)
                    {
                        DataContext = newItems

                    }, PopupAnimation.Slide, 6000));
                });
            }
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _libraryManager.LibraryChanged -= libraryManager_LibraryChanged;
        }
    }
}