- Timestamp:
- Sep 10, 2010, 11:17:02 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/notifications/src/modules/notifications/NotificationManager.cc
r7398 r7399 39 39 #include "core/LuaState.h" 40 40 #include "util/ScopedSingletonManager.h" 41 41 42 #include "interfaces/NotificationListener.h" 43 42 44 #include "Notification.h" 43 45 #include "NotificationQueue.h" … … 56 58 ManageScopedSingleton(NotificationManager, ScopeID::Graphics, false); 57 59 58 //TODO: Make work.59 60 SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode); 60 61 … … 70 71 71 72 ModifyConsoleCommand("enterEditMode").setObject(this); 72 73 73 74 COUT(3) << "NotificatioManager created." << std::endl; 74 75 } … … 82 83 ModifyConsoleCommand("enterEditMode").setObject(NULL); 83 84 85 // Destroys all Notifications that have been registered with the NotificationManager. 84 86 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 85 87 it->second->destroy(); 86 88 87 89 COUT(3) << "NotificationManager destroyed." << std::endl; 88 90 } 89 91 92 /** 93 @brief 94 Is called before the object is destroyed. 95 */ 90 96 void NotificationManager::preDestroy(void) 91 97 { 98 // Destroys all NotificationQueues that have been registered with the NotificationManager. 92 99 for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); ) 93 100 { … … 109 116 bool NotificationManager::registerNotification(Notification* notification) 110 117 { 111 if(notification == NULL) // A NULL-Notification cannot be registered. 112 return false; 118 assert(notification); 113 119 114 120 std::time_t time = std::time(0); //TODO: Doesn't this expire? //!< Get current time. 115 121 122 // Add the Notification to the list that holds all Notifications. 116 123 this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification)); 117 124 … … 123 130 all = true; 124 131 125 // Insert the notification in all listeners that have its sender as target.126 for(std::map<NotificationListener*, int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all listeners.132 // Insert the Notification in all NotificationListeners that have its sender as target. 133 for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners. 127 134 { 128 135 std::set<std::string, NotificationListenerStringCompare> set = it->first->getTargetsSet(); 129 136 bool bAll = set.find(NotificationManager::ALL) != set.end(); 130 if(all || bAll || set.find(notification->getSender()) != set.end()) //TODO: Make sure this works. 137 // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target. 138 if(all || bAll || set.find(notification->getSender()) != set.end()) 131 139 { 132 140 if(!bAll) 133 { 134 this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the Notifications list of the current NotificationListener. 135 } 136 it->first->update(notification, time); // Update the listener. 137 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(notification); 138 if(counterIt == this->listenerCounter_.end()) 139 this->listenerCounter_[notification] = 1; 140 else 141 this->listenerCounter_[notification] = counterIt->second + 1; 141 this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener. 142 it->first->update(notification, time); // Update the NotificationListener. 142 143 } 143 144 } … … 150 151 /** 151 152 @brief 152 Unregisters a Notification within the NotificationManager .153 Unregisters a Notification within the NotificationManager for a given NotificationListener. 153 154 @param notification 154 155 A pointer to the Notification to be unregistered. … … 161 162 assert(listener); 162 163 163 // If the Notification was removed from the list of Notifications of the input NotificationListener, the counter for the Notification of the number of NotificationListeners it is present in is decremented. 164 if(this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second))) 165 this->listenerCounter_[notification] = this->listenerCounter_[notification] - 1; 164 // Remove the Notification from the list of Notifications of the input NotificationListener. 165 this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second)); 166 166 167 167 COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl; … … 170 170 /** 171 171 @brief 172 Helper method that removes an input notification form an input map.172 Helper method that removes an input Notification form an input map. 173 173 @param notification 174 A pointer to the notification to be removed.174 A pointer to the Notification to be removed. 175 175 @param map 176 The map the notification should be removed from.176 The map the Notification should be removed from. 177 177 @return 178 178 Returns true if successful. … … 199 199 The NotificationListener to be registered. 200 200 @return 201 Returns true if successful. 201 Returns true if successful. Fales if the NotificationListener is already registered. 202 202 */ 203 203 bool NotificationManager::registerListener(NotificationListener* listener) 204 204 { 205 assert(listener); 206 207 // If the NotificationListener is already registered. 208 if(this->listenerList_.find(listener) != this->listenerList_.end()) 209 return false; 210 205 211 this->highestIndex_ += 1; 206 int index = this->highestIndex_;207 208 this->listenerList_[listener] = index; // Add the NotificationListener to the list of listeners.212 unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely. 213 214 this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners. 209 215 210 216 std::set<std::string, NotificationListenerStringCompare> set = listener->getTargetsSet(); 211 217 212 // 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.218 // 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. 213 219 bool bAll = set.find(NotificationManager::ALL) != set.end(); 214 220 std::multimap<std::time_t, Notification*>* map; 215 221 if(bAll) 216 222 this->notificationLists_[index] = &this->allNotificationsList_; 223 // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners. 217 224 else 218 225 { … … 224 231 for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++) 225 232 { 226 if(bAll || set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current notification as target. 227 { 228 if(!bAll) 229 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 230 std::map<Notification*, unsigned int>::iterator counterIt = this->listenerCounter_.find(it->second); 231 if(counterIt == this->listenerCounter_.end()) 232 this->listenerCounter_[it->second] = 1; 233 else 234 this->listenerCounter_[it->second] = counterIt->second + 1; 235 } 233 if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target. 234 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); 236 235 } 237 236 … … 245 244 /** 246 245 @brief 247 Unregisters a NotificationListener withing the NotificationManager. 246 Unregisters a NotificationListener within the NotificationManager. 247 @param listener 248 The NotificationListener to be unregistered. 248 249 */ 249 250 void NotificationManager::unregisterListener(NotificationListener* listener) … … 251 252 assert(listener); 252 253 253 int identifier = this->listenerList_.find(listener)->second; 254 //TODO: Make unsigned int. 255 unsigned int identifier = this->listenerList_.find(listener)->second; 254 256 std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second; 255 257 256 // If the map is not the map of all notifications, make sure all Notifications are removed and delete it.258 // If the map is not the map of all Notifications, make sure all Notifications are unregistered. 257 259 std::multimap<std::time_t, Notification*>::iterator it = map->begin(); 258 260 if(map != &this->allNotificationsList_) … … 266 268 } 267 269 270 // Remove the NotificationListener from the list of NotificationListeners. 268 271 this->listenerList_.erase(listener); 272 // Remove the Notifications list that was associated with the input NotificationListener. 269 273 this->notificationLists_.erase(identifier); 270 274 … … 274 278 /** 275 279 @brief 276 Fetches the Notifications for a specific NotificationListener in a specified timeframe .280 Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map. 277 281 @param listener 278 282 The NotificationListener the Notifications are fetched for. 279 283 @param map 280 A multimap, in which the notifications are stored.284 A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated. 281 285 @param timeFrameStart 282 286 The start time of the timeframe. … … 286 290 Returns true if successful. 287 291 */ 288 bool NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) 289 { 290 if(listener == NULL || map == NULL) 291 return false; 292 293 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // The Notifications for the input NotificationListener. 294 295 if(notifications == NULL) // Returns false, if there are no Notifications. 296 return false; 292 void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd) 293 { 294 assert(listener); 295 assert(map); 296 297 std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener. 297 298 298 299 std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest; 300 // Iterators pointing to the bounds specified by the specified start and end times of the time frame. 299 301 itLowest = notifications->lower_bound(timeFrameStart); 300 302 itHighest = notifications->upper_bound(timeFrameEnd); 301 303 302 for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time Frame to the end of it. 303 map->insert(std::pair<std::time_t, Notification*>(it->first,it->second)); // Add the found Notifications to the map. 304 305 return true; 306 } 307 308 void NotificationManager::loadQueues(void) 309 { 310 new NotificationQueue("all"); 311 } 312 313 void NotificationManager::createQueue(const std::string& name) 314 { 315 new NotificationQueue(name); 316 } 317 318 NotificationQueue* NotificationManager::getQueue(const std::string & name) 319 { 320 std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); 321 if(it == this->queues_.end()) 322 return NULL; 323 324 return (*it).second; 325 } 326 327 bool NotificationManager::registerQueue(NotificationQueue* queue) 328 { 329 return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second; 330 } 331 332 void NotificationManager::unregisterQueue(NotificationQueue* queue) 333 { 334 this->queues_.erase(queue->getName()); 335 } 336 304 for(it = itLowest; it != itHighest; it++) // Iterate through the Notifications from the start of the time frame to the end of it. 305 map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map. 306 } 307 308 /** 309 @brief 310 Enters the edit mode of the NotificationLayer. 311 */ 337 312 void NotificationManager::enterEditMode(void) 338 313 { … … 342 317 } 343 318 319 /** 320 @brief 321 Registers a NotificationQueue. 322 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. 323 @param queue 324 A pointer to the NotificationQueue to be registered. 325 @return 326 Returns true if successful. If e.g. the a NotificationQueue with that name already exists this returns false. 327 */ 328 bool NotificationManager::registerQueue(NotificationQueue* queue) 329 { 330 return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second; 331 } 332 333 /** 334 @brief 335 Unregisters a NotificationQueue. 336 @param queue 337 A pointer to the NotificationQueue to be unregistered. 338 */ 339 void NotificationManager::unregisterQueue(NotificationQueue* queue) 340 { 341 this->queues_.erase(queue->getName()); 342 } 343 344 /** 345 @brief 346 Loads all the NotificationQueues that should exist. 347 */ 348 void NotificationManager::loadQueues(void) 349 { 350 new NotificationQueue("all"); 351 } 352 353 /** 354 @brief 355 Creates a new NotificationQueue. 356 This is used in lua. 357 @param name 358 The name of the new NotificationQueue. 359 */ 360 void NotificationManager::createQueue(const std::string& name) 361 { 362 new NotificationQueue(name); 363 } 364 365 /** 366 @brief 367 Get the NotificationQueue with the input name. 368 @param name 369 The name of the NotificationQueue. 370 @return 371 Returns a pointer to the NotificationQueue with the input name. Returns NULL if no NotificationQueue with such a name exists. 372 */ 373 NotificationQueue* NotificationManager::getQueue(const std::string & name) 374 { 375 std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.find(name); 376 // Returns NULL if no such NotificationQueue exists. 377 if(it == this->queues_.end()) 378 return NULL; 379 380 return (*it).second; 381 } 382 344 383 }
Note: See TracChangeset
for help on using the changeset viewer.