Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7417 was 7417, checked in by rgrieder, 14 years ago

Removed StringCompare again.

  • Property svn:eol-style set to native
File size: 14.2 KB
RevLine 
[2280]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
[2911]29/**
[7403]30    @file NotificationManager.cc
[2911]31    @brief Implementation of the NotificationManager class.
32*/
33
[2280]34#include "NotificationManager.h"
35
[7403]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"
[2911]41
[5619]42#include "interfaces/NotificationListener.h"
[2280]43
[7403]44#include "Notification.h"
45#include "NotificationQueue.h"
46
47#include "ToluaBindNotifications.h"
48
[2435]49namespace orxonox
50{
[2280]51
[6417]52    const std::string NotificationManager::ALL("all");
53    const std::string NotificationManager::NONE("none");
[2911]54
[7403]55    // Register tolua_open function when loading the library.
56    DeclareToluaInterface(Notifications);
[2911]57
[7403]58    ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false);
59
60    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
61
[2911]62    /**
63    @brief
64        Constructor. Registers the Object.
65    */
66    NotificationManager::NotificationManager()
[2280]67    {
[2911]68        RegisterRootObject(NotificationManager);
69
70        this->highestIndex_ = 0;
[7403]71
72        ModifyConsoleCommand("enterEditMode").setObject(this);
73
74        COUT(3) << "NotificatioManager created." << std::endl;
[2280]75    }
76
[2911]77    /**
78    @brief
79        Destructor.
80    */
[2280]81    NotificationManager::~NotificationManager()
82    {
[7403]83        ModifyConsoleCommand("enterEditMode").setObject(NULL);
[7163]84
[7403]85        COUT(3) << "NotificationManager destroyed." << std::endl;
[2280]86    }
[2501]87
[2911]88    /**
89    @brief
[7403]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
[5619]106        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
[2911]107    @param notification
108        The Notification to be registered.
109    @return
110        Returns true if successful.
111    */
112    bool NotificationManager::registerNotification(Notification* notification)
113    {
[7403]114        assert(notification);
[5619]115
[5748]116        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
[5619]117
[7403]118        // Add the Notification to the list that holds all Notifications.
119        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
[5619]120
[7403]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.
[2911]122            return true;
[5619]123
[2911]124        bool all = false;
[7403]125        if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
[2911]126            all = true;
[5619]127
[7403]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.
[2435]130        {
[7417]131            const std::set<std::string>& set = it->first->getTargetsSet();
[7403]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())
[2280]135            {
[7403]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.
[2435]139            }
140        }
[5619]141
[7403]142        COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl;
[5619]143
[2280]144        return true;
145    }
[5619]146
[2911]147    /**
148    @brief
[7403]149        Unregisters a Notification within the NotificationManager for a given NotificationListener.
[7163]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
[7403]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));
[7163]162
[7403]163        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
[7163]164    }
165
166    /**
167    @brief
[7403]168        Helper method that removes an input Notification form an input map.
[7163]169    @param notification
[7403]170        A pointer to the Notification to be removed.
[7163]171    @param map
[7403]172        The map the Notification should be removed from.
[7163]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
[5619]193        Registers a NotificationListener within the NotificationManager.
194    @param listener
195        The NotificationListener to be registered.
[2911]196    @return
[7403]197        Returns true if successful.  Fales if the NotificationListener is already registered.
[2911]198    */
[5619]199    bool NotificationManager::registerListener(NotificationListener* listener)
[2280]200    {
[7403]201        assert(listener);
202
203        // If the NotificationListener is already registered.
204        if(this->listenerList_.find(listener) != this->listenerList_.end())
205            return false;
206
[2911]207        this->highestIndex_ += 1;
[7403]208        unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
[5619]209
[7403]210        this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
[5619]211
[7417]212        const std::set<std::string>& set = listener->getTargetsSet();
[5619]213
[7403]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
[2284]221        {
[7403]222            this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
223            map = this->notificationLists_[index];
[2284]224        }
[5619]225
[7403]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++)
[2911]228        {
[7403]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));
[2911]231        }
[2501]232
[7403]233        listener->update(); // Update the listener.
[5619]234
[7163]235        COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
[5619]236
[2911]237        return true;
[2280]238    }
[5619]239
[2911]240    /**
241    @brief
[7403]242        Unregisters a NotificationListener within the NotificationManager.
243    @param listener
244        The NotificationListener to be unregistered.
[7163]245    */
246    void NotificationManager::unregisterListener(NotificationListener* listener)
247    {
248        assert(listener);
249
[7403]250        unsigned int identifier = this->listenerList_.find(listener)->second;
[7163]251        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
252
[7403]253        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
[7163]254        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
[7403]255        if(map != &this->allNotificationsList_)
[7163]256        {
[7403]257            while(it != map->end())
258            {
259                this->unregisterNotification(it->second, listener);
260                it = map->begin();
261            }
262            delete map;
[7163]263        }
264
[7403]265        // Remove the NotificationListener from the list of NotificationListeners.
[7163]266        this->listenerList_.erase(listener);
[7403]267        // Remove the Notifications list that was associated with the input NotificationListener.
[7163]268        this->notificationLists_.erase(identifier);
269
270        COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl;
271    }
272
273    /**
274    @brief
[7403]275        Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
[5619]276    @param listener
277        The NotificationListener the Notifications are fetched for.
[2911]278    @param map
[7403]279        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
[2911]280    @param timeFrameStart
281        The start time of the timeframe.
282    @param timeFrameEnd
283        The end time of the timeframe.
284    @return
285        Returns true if successful.
286    */
[7403]287    void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
[2346]288    {
[7403]289        assert(listener);
290        assert(map);
[2501]291
[7403]292        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
[5619]293
[2911]294        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
[7403]295        // Iterators pointing to the bounds specified by the specified start and end times of the time frame.
[2911]296        itLowest = notifications->lower_bound(timeFrameStart);
[7403]297        itHighest = notifications->upper_bound(timeFrameEnd);
[5619]298
[7403]299        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it.
300            map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
301    }
[5619]302
[7403]303    /**
304    @brief
305        Enters the edit mode of the NotificationLayer.
306    */
307    void NotificationManager::enterEditMode(void)
308    {
309        GUIManager::getInstance().hideGUI("NotificationLayer");
310        GUIManager::getInstance().showGUI("NotificationLayer", false, false);
311        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()");
[2346]312    }
[2280]313
[7403]314    /**
315    @brief
316        Registers a NotificationQueue.
317        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.
318    @param queue
319        A pointer to the NotificationQueue to be registered.
320    @return
321        Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false.
322    */
323    bool NotificationManager::registerQueue(NotificationQueue* queue)
324    {
325        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
326    }
327
328    /**
329    @brief
330        Unregisters a NotificationQueue.
331    @param queue
332        A pointer to the NotificationQueue to be unregistered.
333    */
334    void NotificationManager::unregisterQueue(NotificationQueue* queue)
335    {
336        this->queues_.erase(queue->getName());
337    }
338
339    /**
340    @brief
341        Loads all the NotificationQueues that should exist.
342    */
343    void NotificationManager::loadQueues(void)
344    {
345        new NotificationQueue("all");
346    }
347
348    /**
349    @brief
350        Creates a new NotificationQueue.
351        This is used in lua.
352    @param name
353        The name of the new NotificationQueue.
354    */
355    void NotificationManager::createQueue(const std::string& name)
356    {
357        new NotificationQueue(name);
358    }
359
360    /**
361    @brief
362        Get the NotificationQueue with the input name.
363    @param name
364        The name of the NotificationQueue.
365    @return
366        Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists.
367    */
368    NotificationQueue* NotificationManager::getQueue(const std::string & name)
369    {
370        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name);
371        // Returns NULL if no such NotificationQueue exists.
372        if(it == this->queues_.end())
373            return NULL;
374
375        return (*it).second;
376    }
377
[2280]378}
Note: See TracBrowser for help on using the repository browser.