1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Damian 'Mozork' Frick |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file NotificationManager.cc |
---|
31 | @brief Implementation of the NotificationManager class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "OrxonoxStableHeaders.h" |
---|
35 | #include "NotificationManager.h" |
---|
36 | |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | |
---|
39 | #include <set> |
---|
40 | |
---|
41 | #include "Notification.h" |
---|
42 | #include "NotificationQueue.h" |
---|
43 | |
---|
44 | namespace orxonox |
---|
45 | { |
---|
46 | |
---|
47 | const std::string NotificationManager::ALL = "all"; |
---|
48 | const std::string NotificationManager::NONE = "none"; |
---|
49 | |
---|
50 | std::multimap<std::time_t,Notification*> NotificationManager::allNotificationsList_s; |
---|
51 | std::map<int,std::multimap<std::time_t,Notification*>*> NotificationManager::notificationLists_s; |
---|
52 | std::map<NotificationQueue*,int> NotificationManager::queueList_s; |
---|
53 | |
---|
54 | int NotificationManager::highestIndex_s = 0; |
---|
55 | |
---|
56 | /** |
---|
57 | @brief |
---|
58 | Constructor. Registers the Object. |
---|
59 | */ |
---|
60 | NotificationManager::NotificationManager(BaseObject* creator) : BaseObject(creator) |
---|
61 | { |
---|
62 | RegisterObject(NotificationManager); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief |
---|
67 | Destructor. |
---|
68 | */ |
---|
69 | NotificationManager::~NotificationManager() |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | @brief |
---|
75 | Registers a Notification within the NotificationManager and makes sure that the Notification is displayed in all the NotificationQueues associated with its sender. |
---|
76 | @param notification |
---|
77 | The Notification to be registered. |
---|
78 | @return |
---|
79 | Returns true if successful. |
---|
80 | */ |
---|
81 | /*static*/ bool NotificationManager::registerNotification(Notification* notification) |
---|
82 | { |
---|
83 | |
---|
84 | if(notification == NULL) //!< A NULL-Notification cannot be registered. |
---|
85 | return false; |
---|
86 | |
---|
87 | std::time_t time = std::time(0); //TDO: Doesn't this expire? //!< Get current time. |
---|
88 | |
---|
89 | allNotificationsList_s.insert(std::pair<std::time_t,Notification*>(time,notification)); |
---|
90 | |
---|
91 | if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications. |
---|
92 | return true; |
---|
93 | |
---|
94 | bool all = false; |
---|
95 | if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationQueue. |
---|
96 | all = true; |
---|
97 | |
---|
98 | //!< Insert the notification in all queues that have its sender as target. |
---|
99 | for(std::map<NotificationQueue*,int>::iterator it = queueList_s.begin(); it != queueList_s.end(); it++) //!< Iterate through all queues. |
---|
100 | { |
---|
101 | std::set<std::string> set = it->first->getTargetsSet(); |
---|
102 | if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TDO: Make sure this works. |
---|
103 | { |
---|
104 | notificationLists_s[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationQueue. |
---|
105 | it->first->update(notification, time); //!< Update the queue. |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | COUT(3) << "Notification registered with the NotificationManager." << std::endl; |
---|
110 | |
---|
111 | return true; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | @brief |
---|
116 | Registers a NotificationQueue within the NotificationManager. |
---|
117 | @param queue |
---|
118 | The NotificationQueue to be registered. |
---|
119 | @return |
---|
120 | Returns true if successful. |
---|
121 | */ |
---|
122 | /*static*/ bool NotificationManager::registerQueue(NotificationQueue* queue) |
---|
123 | { |
---|
124 | NotificationManager::highestIndex_s += 1; |
---|
125 | int index = NotificationManager::highestIndex_s; |
---|
126 | |
---|
127 | queueList_s[queue] = index; //!< Add the NotificationQueue to the list of queues. |
---|
128 | |
---|
129 | std::set<std::string> set = queue->getTargetsSet(); //TDO: Works this? |
---|
130 | |
---|
131 | //! If all senders are the target of the queue, then the list of notification for that specific queue is te same as the list of all Notifications. |
---|
132 | if(set.find(ALL) != set.end()) |
---|
133 | { |
---|
134 | notificationLists_s[index] = &allNotificationsList_s; |
---|
135 | COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl; |
---|
136 | return true; |
---|
137 | } |
---|
138 | |
---|
139 | notificationLists_s[index] = new std::multimap<std::time_t,Notification*>; |
---|
140 | std::multimap<std::time_t,Notification*> map = *notificationLists_s[index]; |
---|
141 | |
---|
142 | //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue. |
---|
143 | for(std::multimap<std::time_t,Notification*>::iterator it = allNotificationsList_s.begin(); it != allNotificationsList_s.end(); it++) |
---|
144 | { |
---|
145 | if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target. |
---|
146 | { |
---|
147 | map.insert(std::pair<std::time_t,Notification*>(it->first, it->second)); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | queue->update(); //!< Update the queue. |
---|
152 | |
---|
153 | COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl; |
---|
154 | |
---|
155 | return true; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | @brief |
---|
160 | Fetches the Notifications for a specific NotificationQueue in a specified timeframe. |
---|
161 | @param queue |
---|
162 | The NotificationQueue the Notifications are fetched for. |
---|
163 | @param timeFrameStart |
---|
164 | The start time of the timeframe. |
---|
165 | @param timeFrameEnd |
---|
166 | The end time of the timeframe. |
---|
167 | @return |
---|
168 | Returns a time-ordered list of Notifications. |
---|
169 | @todo |
---|
170 | Make sure the map is deleted. |
---|
171 | */ |
---|
172 | /*static*/ std::multimap<std::time_t,Notification*>* NotificationManager::getNotifications(NotificationQueue* queue, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) |
---|
173 | { |
---|
174 | std::multimap<std::time_t,Notification*>* notifications = NotificationManager::notificationLists_s[NotificationManager::queueList_s[queue]]; |
---|
175 | |
---|
176 | if(notifications == NULL) |
---|
177 | return NULL; |
---|
178 | |
---|
179 | std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; |
---|
180 | itLowest = notifications->lower_bound(timeFrameStart); |
---|
181 | itHighest = notifications->upper_bound(timeFrameStart); |
---|
182 | |
---|
183 | std::multimap<std::time_t,Notification*>* map = new std::multimap<std::time_t,Notification*>(); |
---|
184 | |
---|
185 | for(it = itLowest; it != itHighest; it++) |
---|
186 | { |
---|
187 | map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); |
---|
188 | } |
---|
189 | |
---|
190 | if(map->size() == 0) |
---|
191 | { |
---|
192 | delete map; |
---|
193 | return NULL; |
---|
194 | } |
---|
195 | |
---|
196 | return map; |
---|
197 | } |
---|
198 | |
---|
199 | } |
---|