Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/notifications/NotificationManager.cc @ 7403

Last change on this file since 7403 was 7403, checked in by dafrick, 14 years ago

Merged notifications branch back to trunk.

  • Property svn:eol-style set to native
File size: 14.3 KB
Line 
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 "NotificationManager.h"
35
36#include "core/command/ConsoleCommand.h"
37#include "core/CoreIncludes.h"
38#include "core/GUIManager.h"
39#include "core/LuaState.h"
40#include "util/ScopedSingletonManager.h"
41
42#include "interfaces/NotificationListener.h"
43
44#include "Notification.h"
45#include "NotificationQueue.h"
46
47#include "ToluaBindNotifications.h"
48
49namespace orxonox
50{
51
52    const std::string NotificationManager::ALL("all");
53    const std::string NotificationManager::NONE("none");
54
55    // Register tolua_open function when loading the library.
56    DeclareToluaInterface(Notifications);
57
58    ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false);
59
60    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
61
62    /**
63    @brief
64        Constructor. Registers the Object.
65    */
66    NotificationManager::NotificationManager()
67    {
68        RegisterRootObject(NotificationManager);
69
70        this->highestIndex_ = 0;
71
72        ModifyConsoleCommand("enterEditMode").setObject(this);
73
74        COUT(3) << "NotificatioManager created." << std::endl;
75    }
76
77    /**
78    @brief
79        Destructor.
80    */
81    NotificationManager::~NotificationManager()
82    {
83        ModifyConsoleCommand("enterEditMode").setObject(NULL);
84
85        COUT(3) << "NotificationManager destroyed." << std::endl;
86    }
87
88    /**
89    @brief
90        Is called before the object is destroyed.
91    */
92    void NotificationManager::preDestroy(void)
93    {
94        // Destroys all NotificationQueues that have been registered with the NotificationManager.
95        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); )
96        {
97            NotificationQueue* queue = (*it).second;
98            it++;
99            queue->destroy();
100        }
101        this->queues_.clear();
102    }
103
104    /**
105    @brief
106        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
107    @param notification
108        The Notification to be registered.
109    @return
110        Returns true if successful.
111    */
112    bool NotificationManager::registerNotification(Notification* notification)
113    {
114        assert(notification);
115
116        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
117
118        // Add the Notification to the list that holds all Notifications.
119        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
120
121        if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
122            return true;
123
124        bool all = false;
125        if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
126            all = true;
127
128        // Insert the Notification in all NotificationListeners that have its sender as target.
129        for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
130        {
131            std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet();
132            bool bAll = set.find(NotificationManager::ALL) != set.end();
133            // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
134            if(all || bAll || set.find(notification->getSender()) != set.end())
135            {
136                if(!bAll)
137                    this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
138                it->first->update(notification, time); // Update the NotificationListener.
139            }
140        }
141
142        COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl;
143
144        return true;
145    }
146
147    /**
148    @brief
149        Unregisters a Notification within the NotificationManager for a given NotificationListener.
150    @param notification
151        A pointer to the Notification to be unregistered.
152    @param listener
153        A pointer to the NotificationListener the Notification is unregistered for.
154    */
155    void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
156    {
157        assert(notification);
158        assert(listener);
159
160        // Remove the Notification from the list of Notifications of the input NotificationListener.
161        this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
162
163        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
164    }
165
166    /**
167    @brief
168        Helper method that removes an input Notification form an input map.
169    @param notification
170        A pointer to the Notification to be removed.
171    @param map
172        The map the Notification should be removed from.
173    @return
174        Returns true if successful.
175    */
176    bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map)
177    {
178        // Iterates through all items in the map until the Notification is found.
179        //TODO: Do more efficiently?
180        for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++)
181        {
182            if(it->second == notification)
183            {
184                map.erase(it);
185                return true;
186            }
187        }
188        return false;
189    }
190
191    /**
192    @brief
193        Registers a NotificationListener within the NotificationManager.
194    @param listener
195        The NotificationListener to be registered.
196    @return
197        Returns true if successful.  Fales if the NotificationListener is already registered.
198    */
199    bool NotificationManager::registerListener(NotificationListener* listener)
200    {
201        assert(listener);
202
203        // If the NotificationListener is already registered.
204        if(this->listenerList_.find(listener) != this->listenerList_.end())
205            return false;
206
207        this->highestIndex_ += 1;
208        unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
209
210        this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
211
212        std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet();
213
214        // If all senders are the target of the NotificationListener, then the list of Notifications for that specific NotificationListener is the same as the list of all Notifications.
215        bool bAll = set.find(NotificationManager::ALL) != set.end();
216        std::multimap<std::time_t, Notification*>* map;
217        if(bAll)
218            this->notificationLists_[index] = &this->allNotificationsList_;
219        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
220        else
221        {
222            this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
223            map = this->notificationLists_[index];
224        }
225
226        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
227        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
228        {
229            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
230                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
231        }
232
233        listener->update(); // Update the listener.
234
235        COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
236
237        return true;
238    }
239
240    /**
241    @brief
242        Unregisters a NotificationListener within the NotificationManager.
243    @param listener
244        The NotificationListener to be unregistered.
245    */
246    void NotificationManager::unregisterListener(NotificationListener* listener)
247    {
248        assert(listener);
249
250        //TODO: Make unsigned int.
251        unsigned int identifier = this->listenerList_.find(listener)->second;
252        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
253
254        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
255        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
256        if(map != &this->allNotificationsList_)
257        {
258            while(it != map->end())
259            {
260                this->unregisterNotification(it->second, listener);
261                it = map->begin();
262            }
263            delete map;
264        }
265
266        // Remove the NotificationListener from the list of NotificationListeners.
267        this->listenerList_.erase(listener);
268        // Remove the Notifications list that was associated with the input NotificationListener.
269        this->notificationLists_.erase(identifier);
270
271        COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl;
272    }
273
274    /**
275    @brief
276        Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
277    @param listener
278        The NotificationListener the Notifications are fetched for.
279    @param map
280        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
281    @param timeFrameStart
282        The start time of the timeframe.
283    @param timeFrameEnd
284        The end time of the timeframe.
285    @return
286        Returns true if successful.
287    */
288    void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
289    {
290        assert(listener);
291        assert(map);
292
293        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
294
295        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
296        // Iterators pointing to the bounds specified by the specified start and end times of the time frame.
297        itLowest = notifications->lower_bound(timeFrameStart);
298        itHighest = notifications->upper_bound(timeFrameEnd);
299
300        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it.
301            map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
302    }
303
304    /**
305    @brief
306        Enters the edit mode of the NotificationLayer.
307    */
308    void NotificationManager::enterEditMode(void)
309    {
310        GUIManager::getInstance().hideGUI("NotificationLayer");
311        GUIManager::getInstance().showGUI("NotificationLayer", false, false);
312        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()");
313    }
314
315    /**
316    @brief
317        Registers a NotificationQueue.
318        This makes sure that the NotificationQueue can be attained through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
319    @param queue
320        A pointer to the NotificationQueue to be registered.
321    @return
322        Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false.
323    */
324    bool NotificationManager::registerQueue(NotificationQueue* queue)
325    {
326        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
327    }
328
329    /**
330    @brief
331        Unregisters a NotificationQueue.
332    @param queue
333        A pointer to the NotificationQueue to be unregistered.
334    */
335    void NotificationManager::unregisterQueue(NotificationQueue* queue)
336    {
337        this->queues_.erase(queue->getName());
338    }
339
340    /**
341    @brief
342        Loads all the NotificationQueues that should exist.
343    */
344    void NotificationManager::loadQueues(void)
345    {
346        new NotificationQueue("all");
347    }
348
349    /**
350    @brief
351        Creates a new NotificationQueue.
352        This is used in lua.
353    @param name
354        The name of the new NotificationQueue.
355    */
356    void NotificationManager::createQueue(const std::string& name)
357    {
358        new NotificationQueue(name);
359    }
360
361    /**
362    @brief
363        Get the NotificationQueue with the input name.
364    @param name
365        The name of the NotificationQueue.
366    @return
367        Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists.
368    */
369    NotificationQueue* NotificationManager::getQueue(const std::string & name)
370    {
371        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name);
372        // Returns NULL if no such NotificationQueue exists.
373        if(it == this->queues_.end())
374            return NULL;
375
376        return (*it).second;
377    }
378
379}
Note: See TracBrowser for help on using the repository browser.