- Timestamp:
- Sep 10, 2010, 11:17:02 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/notifications/src/modules/notifications/NotificationQueue.cc
r7398 r7399 41 41 #include "core/LuaState.h" 42 42 #include "util/Convert.h" 43 #include "util/SubString.h" 44 43 45 #include "Notification.h" 44 46 … … 49 51 @brief 50 52 Constructor. Creates and initializes the object. 53 @param name 54 The name of the new NotificationQueue. 55 @param senders 56 The senders that are targets of this NotificationQueue, i.e. the names of senders whose Notifications this NotificationQueue displays. 57 The senders need to be seperated by commas. 58 @param size 59 The size (the maximum number of displayed Notifications) of this NotificationQueue. 60 @param displayTime 61 The time during which a Notification is (at most) displayed. 51 62 */ 52 63 NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, unsigned int displayTime) … … 56 67 RegisterRootObject(NotificationQueue); 57 68 58 this->initialize(); 59 69 // Initialize. 70 this->size_ = 0; 71 this->tickTime_ = 0.0; 72 73 // Sets the input values. 60 74 this->setTargets(senders); 61 75 this->name_ = name; … … 63 77 this->setDisplayTime(displayTime); 64 78 79 //TODO: Destroy if registration fails? 80 81 // Register the NotificationQueue with the NotificationManager. 65 82 bool queueRegistered = NotificationManager::getInstance().registerQueue(this); 66 83 this->registered_ = true; 67 if(!queueRegistered) 84 if(!queueRegistered) // If the registration has failed. 68 85 { 69 86 this->registered_ = false; … … 72 89 } 73 90 74 this->create(); 75 91 this->create(); // Creates the NotificationQueue in lua. 92 93 // register the NotificationQueue as NotificationListener with the NotificationManager. 76 94 bool listenerRegistered = NotificationManager::getInstance().registerListener(this); 77 if(!listenerRegistered) 95 if(!listenerRegistered) // If the registration has failed. 78 96 { 79 97 this->registered_ = false; 98 // Remove the NotificationQueue in lua. 99 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")"); 80 100 NotificationManager::getInstance().unregisterQueue(this); 81 101 COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl; … … 94 114 this->targets_.clear(); 95 115 96 if(this->registered_) 116 if(this->registered_) // If the 97 117 { 98 118 this->clear(); 99 119 120 // Unregister with the NotificationManager. 100 121 NotificationManager::getInstance().unregisterListener(this); 101 122 NotificationManager::getInstance().unregisterQueue(this); 102 123 124 // Remove the NotificationQueue in lua. 103 125 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() + "\")"); 104 126 } 105 }106 107 /**108 @brief109 Initializes the object.110 Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager.111 */112 void NotificationQueue::initialize(void)113 {114 this->size_ = 0;115 this->tickTime_ = 0.0;116 127 } 117 128 … … 139 150 140 151 std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin(); 141 while(it != this->ordering_.upper_bound(&this->timeLimit_)) // Iterate through all elements whose creation time is smaller than the current time minus the display time. 152 // Iterate through all elements whose creation time is smaller than the current time minus the display time. 153 while(it != this->ordering_.upper_bound(&this->timeLimit_)) 142 154 { 143 155 NotificationContainer* temp = *it; 144 156 it++; 145 this->remove(temp); 157 this->remove(temp); // Remove the Notifications that have expired. 146 158 } 147 159 … … 160 172 161 173 std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>; 162 if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) // Get the Notifications sent in the interval form now to minus the display time. 163 { 164 COUT(1) << "NotificationQueue update failed due to undetermined cause." << std::endl; 165 return; 166 } 174 // Get the Notifications sent in the interval from now to now minus the display time. 175 NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_); 167 176 168 177 if(!notifications->empty()) 169 178 { 170 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++)// Add all Notifications.171 {179 // Add all Notifications. 180 for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) 172 181 this->push(it->second, it->first); 173 }174 182 } 175 183 176 184 delete notifications; 177 185 178 COUT( 3) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; //TODO: Level 4.186 COUT(4) << "NotificationQueue '" << this->getName() << "' updated." << std::endl; 179 187 } 180 188 … … 191 199 this->push(notification, time); 192 200 193 COUT( 3) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; //TODO: Level 4.194 } 195 196 /** 197 @brief 198 Adds a Notification to the NotificationQueue.201 COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl; 202 } 203 204 /** 205 @brief 206 Adds (pushes) a Notification to the NotificationQueue. 199 207 It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI. 200 208 @param notification 201 The Notification .209 The Notification to be pushed. 202 210 @param time 203 The time .211 The time when the Notification has been sent. 204 212 */ 205 213 void NotificationQueue::push(Notification* notification, const std::time_t & time) … … 216 224 217 225 this->ordering_.insert(container); 226 // Insert the Notification at the begin of the list (vector, actually). 218 227 this->notifications_.insert(this->notifications_.begin(), container); 219 228 229 // Push the Notification to the GUI. 220 230 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")"); 221 231 } … … 223 233 /** 224 234 @brief 225 Removes the least recently added Notification form the NotificationQueue.235 Removes (pops) the least recently added Notification form the NotificationQueue. 226 236 */ 227 237 void NotificationQueue::pop(void) … … 230 240 this->ordering_.erase(container); 231 241 this->notifications_.pop_back(); 242 232 243 this->size_--; 244 233 245 delete container; 246 247 // Pops the Notification from the GUI. 234 248 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")"); 235 249 } … … 237 251 /** 238 252 @brief 239 Removes the Notification that is stored in the input container.253 Removes the Notification that is stored in the input NotificationContainer. 240 254 @param container 241 255 The NotificationContainer with the Notification to be removed. … … 244 258 { 245 259 std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), container); 260 // Get the index at which the Notification is. 246 261 std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin (); 247 262 this->ordering_.erase(container); 248 263 this->notifications_.erase(it); 264 249 265 this->size_--; 266 250 267 delete container; 268 269 // Removes the Notification from the GUI. 251 270 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")"); 252 271 } … … 254 273 /** 255 274 @brief 256 Clears the queue by removing all Notifications.275 Clears the NotificationQueue by removing all NotificationContainers. 257 276 */ 258 277 void NotificationQueue::clear(void) 259 278 { 260 279 this->ordering_.clear(); 280 // Delete all NotificationContainers in the list. 261 281 for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++) 262 282 delete *it; 263 283 264 284 this->notifications_.clear(); 285 265 286 this->size_ = 0; 287 288 // Clear the NotificationQueue in the GUI. 266 289 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")"); 267 290 } … … 272 295 @param name 273 296 The name to be set. 274 @return 275 returns true if successful. 276 */ 277 bool NotificationQueue::setName(const std::string& name) 297 */ 298 void NotificationQueue::setName(const std::string& name) 278 299 { 279 300 this->name_ = name; 280 return true;281 301 } 282 302 … … 286 306 @param size 287 307 The size to be set. 288 @return289 Returns true if successful.290 308 */ 291 309 void NotificationQueue::setMaxSize(unsigned int size) … … 293 311 if(this->maxSize_ == size) 294 312 return; 295 313 296 314 this->maxSize_ = size; 297 this->sizeChanged(); 298 } 299 300 /** 301 @brief 302 Adjusts the NotificationQueue, when the maximum size has changed. 303 */ 304 void NotificationQueue::sizeChanged(void) 305 { 306 GUIManager::getInstance().getLuaState()->doString("NotificationLayer.changeSize(\"" + this->getName() + "\", " + multi_cast<std::string>(this->getSize()) + ")"); 307 this->update(); 315 316 if(this->registered_) 317 this->update(); 308 318 } 309 319 … … 329 339 /** 330 340 @brief 331 Produces all targets concatinated as string, with kommas (',') as seperators.341 Produces all targets of the NotificationQueue concatinated as string, with kommas (',') as seperators. 332 342 @return 333 343 Returns the targets as a string. … … 337 347 std::stringstream stream; 338 348 bool first = true; 339 for(std::set<std::string, NotificationListenerStringCompare>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) // Iterate through the set of targets. 349 // Iterate through the set of targets. 350 for(std::set<std::string, NotificationListenerStringCompare>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) 340 351 { 341 352 if(!first) 342 stream << ',';353 stream << ", "; 343 354 else 344 355 first = false; … … 351 362 /** 352 363 @brief 353 Sets the targets of the queue.364 Sets the targets of the NotificationQueue. 354 365 The targets are the senders whose Notifications are displayed in this queue. 355 366 @param targets 356 367 Accepts a string of targets, each seperated by commas (','), spaces are ignored. 357 @return 358 Returns true if successful. 359 */ 360 bool NotificationQueue::setTargets(const std::string & targets) 368 */ 369 void NotificationQueue::setTargets(const std::string & targets) 361 370 { 362 371 this->targets_.clear(); 363 372 364 //TODO: Do with SubString. 365 std::string* pTemp; 366 unsigned int index = 0; 367 while(index < targets.size()) // Go through the string, character by character until the end is reached. 368 { 369 pTemp = new std::string(); 370 while(index < targets.size() && targets[index] != ',' && targets[index] != ' ') 371 { 372 *pTemp += targets[index]; 373 index++; 374 } 375 index++; 376 this->targets_.insert(*pTemp); 377 } 373 SubString string = SubString(targets, ",", " ", false); 374 for(unsigned int i = 0; i < string.size(); i++) 375 this->targets_.insert(string[i]); 378 376 379 377 if(this->registered_) … … 382 380 NotificationManager::getInstance().registerListener(this); 383 381 } 384 385 return true;386 382 } 387 383
Note: See TracChangeset
for help on using the changeset viewer.