aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/gui/main/UINotificationManager.java
blob: b6001f7cab1b800089c5f1f18f663340f2d5c7dd (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
149
150
151
152
153
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.gui.main;

import java.util.*;

/**
 * The <tt>UINotificationManager</tt> manages all notifications dedicated to
 * be shown in the main application window. These could be missed calls, voice
 * messages, etc.
 *
 * @author Yana Stamcheva
 */
public class UINotificationManager
{
    /**
     * The list of all notification groups.
     */
    private static Collection<UINotificationGroup> notificationGroups
        = new ArrayList<UINotificationGroup>();

    /**
     * Listener notified for changes in missed calls count.
     */
    private static Collection<UINotificationListener> notificationListeners
        = new ArrayList<UINotificationListener>();

    /**
     * Adds the given <tt>UINotificationListener</tt> to the list of listeners
     * that would be notified on any changes in missed calls count.
     *
     * @param l the <tt>UINotificationListener</tt> to add
     */
    public static void addNotificationListener(UINotificationListener l)
    {
        synchronized (l)
        {
            notificationListeners.add(l);
        }
    }

    /**
     * Removes the given <tt>UINotificationListener</tt> from the list of
     * listeners that are notified on any changes in missed calls count.
     *
     * @param l the <tt>UINotificationListener</tt> to remove
     */
    public static void removeNotificationListener(UINotificationListener l)
    {
        synchronized (l)
        {
            notificationListeners.remove(l);
        }
    }

    /**
     * Adds the given notification to the list of unread notifications and
     * notifies interested listeners.
     *
     * @param notification the <tt>UINotification</tt> to add
     */
    public static void addNotification(UINotification notification)
    {
        UINotificationGroup group = notification.getGroup();

        if (!notificationGroups.contains(group))
            notificationGroups.add(group);

        group.addNotification(notification);

        fireNotificationEvent(notification);
    }

    /**
     * Removes all unread notifications.
     *
     * @param group removes all unread notifications for the given notification
     * group
     */
    public static void removeAllNotifications(UINotificationGroup group)
    {
        group.removeAllNotifications();
    }

    /**
     * Removes all unread notifications from all notification groups.
     */
    public static void removeAllNotifications()
    {
        Iterator<UINotificationGroup> groups = notificationGroups.iterator();

        while (groups.hasNext())
        {
            groups.next().removeAllNotifications();
        }
    }

    /**
     * Returns a list of all unread notifications.
     *
     * @param group the notification group, which notification we're looking
     * for
     * @return a list of all unread notifications
     */
    public static Iterator<UINotification> getUnreadNotifications(
        UINotificationGroup group)
    {
        return group.getUnreadNotifications();
    }

    /**
     * Returns a list of all notification groups.
     *
     * @return a list of all notification groups
     */
    public static Collection<UINotificationGroup> getNotificationGroups()
    {
        return new ArrayList<UINotificationGroup>(notificationGroups);
    }

    /**
     * Notifies interested <tt>UINotificationListener</tt> that a new
     * notification has been received.
     *
     * @param notification the new notification
     */
    private static void fireNotificationEvent(UINotification notification)
    {
        synchronized (notificationListeners)
        {
            Iterator<UINotificationListener> listeners
                = notificationListeners.iterator();

            while (listeners.hasNext())
                listeners.next().notificationReceived(notification);
        }
    }
}