Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem5/src/orxonox/overlays/notifications/NotificationManager.cc @ 2949

Last change on this file since 2949 was 2858, checked in by dafrick, 16 years ago

Done some documenting, and resolved some (possible) issues. So the Notifications are about finished…

  • Property svn:eol-style set to native
File size: 7.6 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
[2779]29/**
30    @file NotificationManager.cc
31    @brief Implementation of the NotificationManager class.
32*/
33
[2280]34#include "OrxonoxStableHeaders.h"
35#include "NotificationManager.h"
36
37#include "core/CoreIncludes.h"
38
[2779]39#include <set>
40
[2280]41#include "Notification.h"
42#include "NotificationQueue.h"
43
[2435]44namespace orxonox
45{
[2280]46
[2779]47    const std::string NotificationManager::ALL = "all";
48    const std::string NotificationManager::NONE = "none";
49
[2785]50    NotificationManager* NotificationManager::singletonRef_s = NULL;
[2779]51
52    /**
53    @brief
54        Constructor. Registers the Object.
55    */
[2788]56    NotificationManager::NotificationManager()
[2280]57    {
[2785]58        RegisterRootObject(NotificationManager);
59
[2786]60        assert(singletonRef_s == 0);
61        singletonRef_s = this;
62
[2785]63        this->highestIndex_ = 0;
[2280]64    }
65
[2779]66    /**
67    @brief
68        Destructor.
69    */
[2280]70    NotificationManager::~NotificationManager()
71    {
72    }
[2785]73
74    /**
75    @brief
76        Returns the current (and single) instance of the NotificationManager. Creates one, if there isn't one to begin with.
77    @return
78        Returns a reference to the single instance of the NotificationManager.
79    */
80    /*static*/ NotificationManager & NotificationManager::getInstance()
81    {
[2786]82        assert(singletonRef_s);
[2785]83        return *singletonRef_s;
84    }
[2779]85   
86    /**
87    @brief
88        Registers a Notification within the NotificationManager and makes sure that the Notification is displayed in all the NotificationQueues associated with its sender.
89    @param notification
90        The Notification to be registered.
91    @return
92        Returns true if successful.
93    */
[2785]94    bool NotificationManager::registerNotification(Notification* notification)
[2280]95    {
[2779]96   
97        if(notification == NULL) //!< A NULL-Notification cannot be registered.
98            return false;
99       
100        std::time_t time = std::time(0); //TDO: Doesn't this expire? //!< Get current time.
101       
[2785]102        this->allNotificationsList_.insert(std::pair<std::time_t,Notification*>(time,notification));
[2779]103       
104        if(notification->getSender() == NONE) //!< If the sender has no specific name, then the Notification is only added to the list of all Notifications.
105            return true;
106       
107        bool all = false;
108        if(notification->getSender() == ALL) //!< If all are the sender, then the Notifications is added to every NotificationQueue.
109            all = true;
110       
111        //!< Insert the notification in all queues that have its sender as target.
[2785]112        for(std::map<NotificationQueue*,int>::iterator it = this->queueList_.begin(); it != this->queueList_.end(); it++) //!< Iterate through all queues.
[2435]113        {
[2779]114            std::set<std::string> set = it->first->getTargetsSet();
115            if(all || set.find(notification->getSender()) != set.end() || set.find(ALL) != set.end()) //TDO: Make sure this works.
[2280]116            {
[2785]117                this->notificationLists_[it->second]->insert(std::pair<std::time_t,Notification*>(time,notification)); //!< Insert the Notification in the Notifications list of the current NotificationQueue.
[2779]118                it->first->update(notification, time); //!< Update the queue.
[2435]119            }
120        }
[2779]121       
[2783]122        COUT(3) << "Notification registered with the NotificationManager." << std::endl;
[2779]123       
[2280]124        return true;
125    }
[2779]126   
127    /**
128    @brief
129        Registers a NotificationQueue within the NotificationManager.
130    @param queue
131        The NotificationQueue to be registered.
132    @return
133        Returns true if successful.
134    */
[2785]135    bool NotificationManager::registerQueue(NotificationQueue* queue)
[2280]136    {
[2785]137        this->highestIndex_ += 1;
138        int index = this->highestIndex_;
[2779]139       
[2785]140        this->queueList_[queue] = index; //!< Add the NotificationQueue to the list of queues.
[2779]141       
142        std::set<std::string> set = queue->getTargetsSet(); //TDO: Works this?
143       
144        //! 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.
145        if(set.find(ALL) != set.end())
[2284]146        {
[2785]147            this->notificationLists_[index] = &this->allNotificationsList_;
[2783]148            COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl;
[2779]149            return true;
[2284]150        }
[2779]151       
[2785]152        this->notificationLists_[index] = new std::multimap<std::time_t,Notification*>;
153        std::multimap<std::time_t,Notification*> map = *this->notificationLists_[index];
[2779]154       
155        //! Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
[2785]156        for(std::multimap<std::time_t,Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
[2779]157        {
158            if(set.find(it->second->getSender()) != set.end()) //!< Checks whether the overlay has the sender of the current notification as target.
159            {
160                map.insert(std::pair<std::time_t,Notification*>(it->first, it->second));
161            }
162        }
163       
164        queue->update(); //!< Update the queue.
[2783]165
166        COUT(3) << "NotificationQueue registered with the NotificationManager." << std::endl;
[2779]167       
168        return true;
[2280]169    }
[2779]170   
171    /**
172    @brief
173        Fetches the Notifications for a specific NotificationQueue in a specified timeframe.
174    @param queue
175        The NotificationQueue the Notifications are fetched for.
[2858]176    @param map
177        A multimap, in which the notifications are stored.
[2779]178    @param timeFrameStart
179        The start time of the timeframe.
180    @param timeFrameEnd
181        The end time of the timeframe.
182    @return
[2858]183        Returns true if successful.
[2779]184    */
[2858]185    bool NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
[2346]186    {
[2858]187        if(queue == NULL || map == NULL)
188            return false;
189
190        std::multimap<std::time_t,Notification*>* notifications = this->notificationLists_[this->queueList_[queue]]; //!< The Notifications for the input NotificationQueue.
[2779]191       
[2858]192        if(notifications == NULL) //!< Returns NULL, if there are no Notifications.
193            return true;
[2779]194   
195        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
196        itLowest = notifications->lower_bound(timeFrameStart);
197        itHighest = notifications->upper_bound(timeFrameStart);
198       
[2858]199        for(it = itLowest; it != itHighest; it++) //!< Iterate through the Notifications from the start of the time Frame to the end of it.
[2349]200        {
[2858]201            map->insert(std::pair<std::time_t,Notification*>(it->first,it->second)); //!< Add the found Notifications to the map.
[2349]202        }
[2779]203       
[2858]204        return true;
[2346]205    }
[2280]206
207}
Note: See TracBrowser for help on using the repository browser.