Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Doing some documentation.

  • Property svn:eol-style set to native
File size: 15.5 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/GUIManager.h"
39#include "core/LuaState.h"
40#include "network/Host.h"
41#include "network/NetworkFunction.h"
42#include "util/ScopedSingletonManager.h"
43
44#include "interfaces/NotificationListener.h"
45
46#include "Notification.h"
47#include "NotificationQueue.h"
48
49#include "ToluaBindNotifications.h"
50
51namespace orxonox
52{
53
54    const std::string NotificationManager::ALL("all");
55    const std::string NotificationManager::NONE("none");
56
57    // Register tolua_open function when loading the library.
58    DeclareToluaInterface(Notifications);
59
60    ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false);
61
62    // Setting console command to enter the edit mode.
63    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
64
65    registerStaticNetworkFunction(NotificationManager::sendNotification);
66
67    /**
68    @brief
69        Constructor. Registers the Object.
70    */
71    NotificationManager::NotificationManager()
72    {
73        RegisterRootObject(NotificationManager);
74
75        this->highestIndex_ = 0;
76
77        ModifyConsoleCommand("enterEditMode").setObject(this);
78
79        COUT(3) << "NotificatioManager created." << std::endl;
80    }
81
82    /**
83    @brief
84        Destructor.
85    */
86    NotificationManager::~NotificationManager()
87    {
88        ModifyConsoleCommand("enterEditMode").setObject(NULL);
89
90        COUT(3) << "NotificationManager destroyed." << std::endl;
91    }
92
93    /**
94    @brief
95        Is called before the object is destroyed.
96    */
97    void NotificationManager::preDestroy(void)
98    {
99        // Destroys all NotificationQueues that have been registered with the NotificationManager.
100        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); )
101        {
102            NotificationQueue* queue = (*it).second;
103            it++;
104            queue->destroy();
105        }
106        this->queues_.clear();
107    }
108
109    /**
110    @brief
111        Sends a Notification with the specified message to the specified client from the specified sender.
112    @param message
113        The message that should be sent.
114    @param clientId
115        The id of the client the notification should be sent to.
116    @param sender
117        The sender that sent the notification.
118    */
119    /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender)
120    {
121        // If we're in standalone mode or we're already no the right client we create and send the Notification.
122        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
123        {
124            Notification* notification = new Notification(message);
125            notification->send(sender);
126        }
127        // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network.
128        else if(GameMode::isServer())
129        {
130            callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender);
131        }
132    }
133
134    /**
135    @brief
136        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
137    @param notification
138        The Notification to be registered.
139    @return
140        Returns true if successful.
141    */
142    bool NotificationManager::registerNotification(Notification* notification)
143    {
144        assert(notification);
145
146        std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time.
147
148        // Add the Notification to the list that holds all Notifications.
149        this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
150
151        if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
152            return true;
153
154        bool all = false;
155        if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
156            all = true;
157
158        // Insert the Notification in all NotificationListeners that have its sender as target.
159        for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
160        {
161            const std::set<std::string>& set = it->first->getTargetsSet();
162            bool bAll = set.find(NotificationManager::ALL) != set.end();
163            // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
164            if(all || bAll || set.find(notification->getSender()) != set.end())
165            {
166                if(!bAll)
167                    this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
168                it->first->update(notification, time); // Update the NotificationListener.
169            }
170        }
171
172        COUT(4) << "Notification (&" << notification << ") registered with the NotificationManager." << std::endl;
173
174        return true;
175    }
176
177    /**
178    @brief
179        Unregisters a Notification within the NotificationManager for a given NotificationListener.
180    @param notification
181        A pointer to the Notification to be unregistered.
182    @param listener
183        A pointer to the NotificationListener the Notification is unregistered for.
184    */
185    void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
186    {
187        assert(notification);
188        assert(listener);
189
190        // Remove the Notification from the list of Notifications of the input NotificationListener.
191        this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
192
193        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
194    }
195
196    /**
197    @brief
198        Helper method that removes an input Notification form an input map.
199    @param notification
200        A pointer to the Notification to be removed.
201    @param map
202        The map the Notification should be removed from.
203    @return
204        Returns true if successful.
205    */
206    bool NotificationManager::removeNotification(Notification* notification, std::multimap<std::time_t, Notification*>& map)
207    {
208        // Iterates through all items in the map until the Notification is found.
209        //TODO: Do more efficiently?
210        for(std::multimap<std::time_t, Notification*>::iterator it = map.begin(); it != map.end(); it++)
211        {
212            if(it->second == notification)
213            {
214                map.erase(it);
215                return true;
216            }
217        }
218        return false;
219    }
220
221    /**
222    @brief
223        Registers a NotificationListener within the NotificationManager.
224    @param listener
225        The NotificationListener to be registered.
226    @return
227        Returns true if successful.  Fales if the NotificationListener is already registered.
228    */
229    bool NotificationManager::registerListener(NotificationListener* listener)
230    {
231        assert(listener);
232
233        // If the NotificationListener is already registered.
234        if(this->listenerList_.find(listener) != this->listenerList_.end())
235            return false;
236
237        this->highestIndex_ += 1;
238        unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
239
240        this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
241
242        const std::set<std::string>& set = listener->getTargetsSet();
243
244        // 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.
245        bool bAll = set.find(NotificationManager::ALL) != set.end();
246        std::multimap<std::time_t, Notification*>* map;
247        if(bAll)
248            this->notificationLists_[index] = &this->allNotificationsList_;
249        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
250        else
251        {
252            this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
253            map = this->notificationLists_[index];
254        }
255
256        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
257        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
258        {
259            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
260                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
261        }
262
263        listener->update(); // Update the listener.
264
265        COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
266
267        return true;
268    }
269
270    /**
271    @brief
272        Unregisters a NotificationListener within the NotificationManager.
273    @param listener
274        The NotificationListener to be unregistered.
275    */
276    void NotificationManager::unregisterListener(NotificationListener* listener)
277    {
278        assert(listener);
279
280        unsigned int identifier = this->listenerList_.find(listener)->second;
281        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
282
283        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
284        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
285        if(map != &this->allNotificationsList_)
286        {
287            while(it != map->end())
288            {
289                this->unregisterNotification(it->second, listener);
290                it = map->begin();
291            }
292            delete map;
293        }
294
295        // Remove the NotificationListener from the list of NotificationListeners.
296        this->listenerList_.erase(listener);
297        // Remove the Notifications list that was associated with the input NotificationListener.
298        this->notificationLists_.erase(identifier);
299
300        COUT(4) << "NotificationListener unregistered with the NotificationManager." << std::endl;
301    }
302
303    /**
304    @brief
305        Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
306    @param listener
307        The NotificationListener the Notifications are fetched for.
308    @param map
309        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
310    @param timeFrameStart
311        The start time of the timeframe.
312    @param timeFrameEnd
313        The end time of the timeframe.
314    @return
315        Returns true if successful.
316    */
317    void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
318    {
319        assert(listener);
320        assert(map);
321
322        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
323
324        std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
325        // Iterators pointing to the bounds specified by the specified start and end times of the time frame.
326        itLowest = notifications->lower_bound(timeFrameStart);
327        itHighest = notifications->upper_bound(timeFrameEnd);
328
329        for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it.
330            map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
331    }
332
333    /**
334    @brief
335        Enters the edit mode of the NotificationLayer.
336    */
337    void NotificationManager::enterEditMode(void)
338    {
339        GUIManager::getInstance().hideGUI("NotificationLayer");
340        GUIManager::getInstance().showGUI("NotificationLayer", false, false);
341        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()");
342    }
343
344    /**
345    @brief
346        Registers a NotificationQueue.
347        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.
348    @param queue
349        A pointer to the NotificationQueue to be registered.
350    @return
351        Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false.
352    */
353    bool NotificationManager::registerQueue(NotificationQueue* queue)
354    {
355        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
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        this->queues_.erase(queue->getName());
367    }
368
369    /**
370    @brief
371        Loads all the NotificationQueues that should exist.
372    */
373    void NotificationManager::loadQueues(void)
374    {
375        new NotificationQueue("all");
376    }
377
378    /**
379    @brief
380        Creates a new NotificationQueue.
381        This is used in lua.
382    @param name
383        The name of the new NotificationQueue.
384    */
385    void NotificationManager::createQueue(const std::string& name)
386    {
387        new NotificationQueue(name);
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}
Note: See TracBrowser for help on using the repository browser.