Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9699 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 18.1 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/LuaState.h"
39#include "util/ScopedSingletonManager.h"
[2911]40
[5619]41#include "interfaces/NotificationListener.h"
[2280]42
[7403]43#include "NotificationQueue.h"
[8706]44#include "NotificationQueueCEGUI.h"
[7403]45
[2435]46namespace orxonox
47{
[2280]48
[7488]49    ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
[7403]50
[2911]51    /**
52    @brief
53        Constructor. Registers the Object.
54    */
55    NotificationManager::NotificationManager()
[2280]56    {
[9667]57        RegisterObject(NotificationManager);
[2911]58
[8858]59        orxout(internal_info, context::notifications) << "NotificatioManager created." << endl;
[2280]60    }
61
[2911]62    /**
63    @brief
64        Destructor.
65    */
[2280]66    NotificationManager::~NotificationManager()
67    {
[7488]68        // Destroys all Notifications.
69        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it!= this->allNotificationsList_.end(); it++)
70            it->second->destroy();
71        this->allNotificationsList_.clear();
72
[8858]73        orxout(internal_info, context::notifications) << "NotificationManager destroyed." << endl;
[2280]74    }
[2501]75
[2911]76    /**
77    @brief
[7403]78        Is called before the object is destroyed.
79    */
80    void NotificationManager::preDestroy(void)
81    {
82        // Destroys all NotificationQueues that have been registered with the NotificationManager.
[8079]83        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin();
84        while(it != this->queues_.end())
85        {
[8706]86            it->second->destroy();
[8079]87            it = this->queues_.begin();
88        }
[7489]89
[7403]90        this->queues_.clear();
91    }
92
[7484]93    /**
94    @brief
[8706]95        Creates and registers a Notification with the input message from the input sender.
96        This is called by the NotificationListener, whenever a new notification arrives.
[7484]97    @param message
[8706]98        The message of the new Notification.
[7484]99    @param sender
[8706]100        The name of the entity (of the collective) that sent the new Notification.
101    @param type
102        The type of the new Notification.
103    @return
104        Returns true if successful.
[7484]105    */
[8706]106    bool NotificationManager::registerNotification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
[7474]107    {
[8706]108        // TODO: Do something with the type.
109        Notification* notification = new Notification(message, sender, type);
110        return this->registerNotification(notification);
111    }
112
113    /**
114    @brief
115        Executes the input command from the input sender.
116        This is called by the NotificationListener, whenever a new command arrives.
117    @param command
118        The command to be executed,
119    @param sender
120        The The name of the entity (of the collective) that sent the command.
121    @return
122        Returns true if the command was successfully executed.
123    */
124    bool NotificationManager::executeCommand(notificationCommand::Value command, const std::string& sender)
125    {
126        bool commandExecuted = false;
127        if(command == notificationCommand::clear)
[7474]128        {
[8706]129            if(this->commandClear(sender))
130                commandExecuted = true;
[7474]131        }
[8706]132
133        if(commandExecuted)
[8858]134            orxout(internal_info, context::notifications) << "Notification command \"" << NotificationListener::command2Str(command) << "\" executed." << endl;
[8706]135
136        return commandExecuted;
137    }
138
139    /**
140    @brief
141        The clear command. Clears all NotificationQueues that have its sender as a target.
142    @param sender
143        The sender of the clear command.
144    @return
145        Returns true if the command was successfully executed by at least one NotificationQueue, false if it was not executed.
146    */
147    bool NotificationManager::commandClear(const std::string& sender)
148    {
149        bool all = (sender == NotificationListener::ALL);
150        bool executed = false;
151        // Clear all NotificationQueues that have the input sender as target.
152        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
[7474]153        {
[8706]154            const std::set<std::string>& set = it->second->getTargetsSet();
155            // If either the sender is 'all', the NotificationQueue has as target all or the NotificationQueue has the input sender as a target.
156            if(all || set.find(NotificationListener::ALL) != set.end() || set.find(sender) != set.end())
157                executed = it->second->tidy() || executed;
[7474]158        }
[8706]159
160        return executed;
[7474]161    }
[8706]162   
[7403]163    /**
164    @brief
[8706]165        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender.
[2911]166    @param notification
167        The Notification to be registered.
168    @return
169        Returns true if successful.
170    */
171    bool NotificationManager::registerNotification(Notification* notification)
172    {
[7403]173        assert(notification);
[5619]174
[7552]175        std::time_t time = std::time(0); // Get current time.
[5619]176
[7403]177        // Add the Notification to the list that holds all Notifications.
178        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
[5619]179
[8706]180        if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
[2911]181            return true;
[5619]182
[8706]183        // If all are the sender, then the Notifications is added to every NotificationQueue.
184        bool all = (notification->getSender() == NotificationListener::ALL);
[5619]185
[8706]186        // Insert the Notification in all NotificationQueues that have its sender as target.
187        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
[2435]188        {
[8706]189            const std::set<std::string>& set = it->second->getTargetsSet();
190            bool bAll = set.find(NotificationListener::ALL) != set.end();
191            // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target.
[7403]192            if(all || bAll || set.find(notification->getSender()) != set.end())
[2280]193            {
[7403]194                if(!bAll)
[8706]195                    this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue.
196                it->second->update(notification, time); // Update the NotificationQueue.
[2435]197            }
198        }
[5619]199
[8858]200        orxout(verbose, context::notifications) << "Notification (&" << notification << ") registered with the NotificationManager." << endl;
[5619]201
[2280]202        return true;
203    }
[5619]204
[2911]205    /**
206    @brief
[8706]207        Unregisters a Notification within the NotificationManager for a given NotificationQueue.
[7163]208    @param notification
209        A pointer to the Notification to be unregistered.
[8706]210    @param queue
211        A pointer to the NotificationQueue the Notification is unregistered for.
[7163]212    */
[8706]213    void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue)
[7163]214    {
215        assert(notification);
[8706]216        assert(queue);
[7163]217
[8706]218        // Remove the Notification from the list of Notifications of the input NotificationQueue.
219        this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second));
[7163]220
[8858]221        orxout(verbose, context::notifications) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << endl;
[7163]222    }
223
224    /**
225    @brief
[7403]226        Helper method that removes an input Notification form an input map.
[7163]227    @param notification
[7403]228        A pointer to the Notification to be removed.
[7163]229    @param map
[7403]230        The map the Notification should be removed from.
[7163]231    @return
232        Returns true if successful.
233    */
234    bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map)
235    {
236        // Iterates through all items in the map until the Notification is found.
237        //TODO: Do more efficiently?
238        for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++)
239        {
240            if(it->second == notification)
241            {
242                map.erase(it);
243                return true;
244            }
245        }
246        return false;
247    }
248
249    /**
250    @brief
[8706]251        Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map.
252    @param queue
253        The NotificationQueue the Notifications are fetched for.
254    @param map
255        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
256    @param timeFrameStart
257        The start time of the timeframe.
258    @param timeFrameEnd
259        The end time of the timeframe.
[2911]260    @return
[8706]261        Returns true if successful.
[2911]262    */
[8706]263    void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
[2280]264    {
[8706]265        assert(queue);
266        assert(map);
[7403]267
[8706]268        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
269
270        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
271        // Iterators pointing to the bounds specified by the specified start and end times of the time frame.
272        itLowest = notifications->lower_bound(timeFrameStart);
273        itHighest = notifications->upper_bound(timeFrameEnd);
274
275        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it.
276            map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
277    }
278
279    /**
280    @brief
281        Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map.
282    @param queue
283        The NotificationQueue the Notifications are fetched for.
284    @param map
285        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
286    @param numberOfNotifications
287        The number of newest Notifications to be got.
288    @return
289        Returns true if successful.
290    */
291    void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)
292    {
293        assert(queue);
294        assert(map);
295
296        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
297
298        if(!notifications->empty()) // If the list of Notifications is not empty.
299        {
300            std::multimap<std::time_t,Notification*>::iterator it = notifications->end();
301            for(int i = 0; i < numberOfNotifications; i++) // Iterate through the Notifications from the newest until we have the specified number of notifications.
302            {
303                it--;
304                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
305                if(it == notifications->begin())
306                    break;
307            }
308        }
309    }
310
311    /**
312    @brief
313        Registers a NotificationQueue.
314        This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
315    @param queue
316        A pointer to the NotificationQueue to be registered.
317    @return
318        Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false.
319    */
320    bool NotificationManager::registerQueue(NotificationQueue* queue)
321    {
322        assert(queue);
323
324        // If the NotificationQueue is already registered.
325        if(this->queues_.find(queue->getName()) != this->queues_.end())
[7403]326            return false;
327
[8706]328        this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues.
[5619]329
[8706]330        const std::set<std::string>& set = queue->getTargetsSet();
[5619]331
[8706]332        // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications.
333        bool bAll = set.find(NotificationListener::ALL) != set.end();
[7504]334        std::multimap<std::time_t, Notification*>* map = NULL;
[7403]335        if(bAll)
[8706]336            this->notificationLists_[queue->getName()] = &this->allNotificationsList_;
337        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues.
[7403]338        else
[2284]339        {
[8706]340            this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>;
341            map = this->notificationLists_[queue->getName()];
[2284]342        }
[5619]343
[8706]344        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
[7403]345        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
[2911]346        {
[7403]347            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
348                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
[2911]349        }
[2501]350
[8706]351        queue->update(); // Update the queue.
352       
[8858]353        orxout(verbose, context::notifications) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << endl;
[2911]354        return true;
[2280]355    }
[5619]356
[2911]357    /**
358    @brief
[8706]359        Unregisters a NotificationQueue.
360    @param queue
361        A pointer to the NotificationQueue to be unregistered.
[7163]362    */
[8706]363    void NotificationManager::unregisterQueue(NotificationQueue* queue)
[7163]364    {
[8706]365        assert(queue);
[7163]366
[8706]367        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second;
[7163]368
[7403]369        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
[7163]370        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
[7403]371        if(map != &this->allNotificationsList_)
[7163]372        {
[7403]373            while(it != map->end())
374            {
[8706]375                this->unregisterNotification(it->second, queue);
[7403]376                it = map->begin();
377            }
378            delete map;
[7163]379        }
380
[8706]381        // Remove the NotificationQueue from the list of NotificationQueues.
382        this->queues_.erase(queue->getName());
383        // Remove the Notifications list that was associated with the input NotificationQueue.
384        this->notificationLists_.erase(queue->getName());
385       
[8858]386        orxout(verbose, context::notifications) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << endl;
[7163]387    }
388
389    /**
390    @brief
[8706]391        Get the NotificationQueue with the input name.
392    @param name
393        The name of the NotificationQueue.
[2911]394    @return
[8706]395        Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists.
[2911]396    */
[8706]397    NotificationQueue* NotificationManager::getQueue(const std::string & name)
[2346]398    {
[8706]399        std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name);
400        // Returns NULL if no such NotificationQueue exists.
401        if(it == this->queues_.end())
402            return NULL;
[2501]403
[8706]404        return (*it).second;
[7403]405    }
[5619]406
[7403]407    /**
408    @brief
[8706]409        Loads all the NotificationQueues that should exist.
[7403]410    */
[8706]411    void NotificationManager::loadQueues(void)
[7403]412    {
[8706]413        /*NotificationQueueCEGUI* allQueue = new NotificationQueueCEGUI("all");
414        allQueue->setDisplaySize(Vector2(0.5, 0));
415        allQueue->setPosition(Vector4(0.0, 10, 0.3, 0));
416
417        NotificationQueueCEGUI* infoQueue = new NotificationQueueCEGUI("info", "gameinfo", 1, -1);
418        infoQueue->setPosition(Vector4(0.2, 0, 0.8, 0));
419        infoQueue->setFontSize(24);
420        infoQueue->setFontColor(Vector4(1.0, 1.0, 0.0, 0.8));
421        infoQueue->setAlignment("HorzCentred");
422        infoQueue->setDisplaySize(Vector2(0.6, 0.0));*/
[2346]423    }
[2280]424
[8706]425    // Notification class
426
[7403]427    /**
428    @brief
[8706]429        Constructor. Creates a Notification with the input message and sender.
430    @param message
431        The message of the Notification.
432    @param sender
433        The sender of the Notification.
434    @param type
[7403]435
436    */
[8706]437    Notification::Notification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
[7403]438    {
[8706]439        this->initialize();
440        this->message_ = message;
441        this->sender_ = sender;
442        this->type_ = type;
[7403]443    }
444
445    /**
446    @brief
[8706]447        Destructor.
[7403]448    */
[8706]449    Notification::~Notification()
[7403]450    {
451
452    }
453
454    /**
455    @brief
[8706]456        Registers the object and sets some default values.
[7403]457    */
[8706]458    void Notification::initialize(void)
[7403]459    {
[8706]460        this->message_.clear();
461        this->sender_ = NotificationListener::NONE;
[7403]462    }
463
[2280]464}
Note: See TracBrowser for help on using the repository browser.