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