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