Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutoriallevel2/src/modules/notifications/NotificationManager.cc @ 8448

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

Extending NotificationQueueCEGUI.

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