Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11815 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

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