Index: code/trunk/src/modules/notifications/NotificationQueue.cc
===================================================================
--- code/trunk/src/modules/notifications/NotificationQueue.cc	(revision 7401)
+++ code/trunk/src/modules/notifications/NotificationQueue.cc	(revision 7403)
@@ -28,5 +28,5 @@
 
 /**
-    @file
+    @file NotificationQueue.cc
     @brief Implementation of the NotificationQueue class.
 */
@@ -34,94 +34,104 @@
 #include "NotificationQueue.h"
 
+#include <map>
+#include <sstream>
+
+#include "core/CoreIncludes.h"
+#include "core/GUIManager.h"
+#include "core/LuaState.h"
 #include "util/Convert.h"
-#include "core/CoreIncludes.h"
-#include "core/XMLPort.h"
-#include "NotificationOverlay.h"
-#include "NotificationManager.h"
+#include "util/SubString.h"
+
+#include "Notification.h"
 
 namespace orxonox
 {
 
-    CreateFactory(NotificationQueue);
-
-    const std::string NotificationQueue::DEFAULT_FONT("VeraMono");
-    const Vector2 NotificationQueue::DEFAULT_POSITION(0.0,0.0);
-    const float NotificationQueue::DEFAULT_FONT_SIZE = 0.025f;
-
     /**
     @brief
         Constructor. Creates and initializes the object.
-    */
-    NotificationQueue::NotificationQueue(BaseObject* creator) : OverlayGroup(creator)
+    @param name
+        The name of the new NotificationQueue.
+    @param senders
+        The senders that are targets of this NotificationQueue, i.e. the names of senders whose Notifications this NotificationQueue displays.
+        The senders need to be seperated by commas.
+    @param size
+        The size (the maximum number of displayed Notifications) of this NotificationQueue.
+    @param displayTime
+        The time during which a Notification is (at most) displayed.
+    */
+    NotificationQueue::NotificationQueue(const std::string& name, const std::string& senders, unsigned int size, unsigned int displayTime)
     {
         this->registered_ = false;
 
-        RegisterObject(NotificationQueue);
-        this->initialize();
-    }
-
-    /**
-    @brief
-        Destructor.
-    */
-    NotificationQueue::~NotificationQueue()
-    {
-        this->targets_.clear();
-        this->clear();
-
-        if(this->registered_)
-            NotificationManager::getInstance().unregisterListener(this);
-    }
-
-    /**
-    @brief
-        Initializes the object.
-        Registers the object, initializes variables, sets default values and registers the queue with the NotificationManager.
-    */
-    void NotificationQueue::initialize(void)
-    {
+        RegisterRootObject(NotificationQueue);
+
+        // Initialize.
         this->size_ = 0;
         this->tickTime_ = 0.0;
 
-        NotificationManager::getInstance().registerListener(this);
+        // Sets the input values.
+        this->setTargets(senders);
+        this->name_ = name;
+        this->maxSize_ = size;
+        this->setDisplayTime(displayTime);
+
+        //TODO: Destroy if registration fails?
+
+        // Register the NotificationQueue with the NotificationManager.
+        bool queueRegistered = NotificationManager::getInstance().registerQueue(this);
         this->registered_ = true;
-    }
-
-    /**
-    @brief
-        Sets the defaults.
-    */
-    void NotificationQueue::setDefaults(void)
-    {
-        this->setMaxSize(DEFAULT_SIZE);
-        this->setNotificationLength(DEFAULT_LENGTH);
-        this->setDisplayTime(DEFAULT_DISPLAY_TIME);
-        this->setPosition(DEFAULT_POSITION);
-
-        this->setTargets(NotificationManager::ALL);
-
-        this->setFontSize(DEFAULT_FONT_SIZE);
-        this->setFont(DEFAULT_FONT);
-    }
-
-    /**
-    @brief
-        Method for creating a NotificationQueue object through XML.
-    */
-    void NotificationQueue::XMLPort(Element& xmlelement, XMLPort::Mode mode)
-    {
-        SUPER(NotificationQueue, XMLPort, xmlelement, mode);
-
-        this->setDefaults();
-
-        XMLPortParam(NotificationQueue, "maxSize", setMaxSize, getMaxSize, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "notificationLength", setNotificationLength, getNotificationLength, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "displayTime", setDisplayTime, getDisplayTime, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "targets", setTargets, getTargets, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "font", setFont, getFont, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "fontSize", setFontSize, getFontSize, xmlelement, mode);
-        XMLPortParam(NotificationQueue, "position", setPosition, getPosition, xmlelement, mode);
+        if(!queueRegistered) // If the registration has failed.
+        {
+            this->registered_ = false;
+            COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl;
+            return;
+        }
+
+        this->create(); // Creates the NotificationQueue in lua.
+
+        // register the NotificationQueue as NotificationListener with the NotificationManager.
+        bool listenerRegistered = NotificationManager::getInstance().registerListener(this);
+        if(!listenerRegistered) // If the registration has failed.
+        {
+            this->registered_ = false;
+            // Remove the NotificationQueue in lua.
+            GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() +  "\")");
+            NotificationManager::getInstance().unregisterQueue(this);
+            COUT(1) << "Error: Notification Queue '" << this->getName() << "' could not be registered." << std::endl;
+            return;
+        }
 
         COUT(3) << "NotificationQueue '" << this->getName() << "' created." << std::endl;
+    }
+
+    /**
+    @brief
+        Destructor.
+    */
+    NotificationQueue::~NotificationQueue()
+    {
+        this->targets_.clear();
+
+        if(this->registered_) // If the 
+        {
+            this->clear();
+
+            // Unregister with the NotificationManager.
+            NotificationManager::getInstance().unregisterListener(this);
+            NotificationManager::getInstance().unregisterQueue(this);
+
+            // Remove the NotificationQueue in lua.
+            GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeQueue(\"" + this->getName() +  "\")");
+        }
+    }
+
+    /**
+    @brief
+        Creates the NotificationQueue in lua.
+    */
+    void NotificationQueue::create(void)
+    {
+        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.createQueue(\"" + this->getName() +  "\", " + multi_cast<std::string>(this->getMaxSize()) + ")");
     }
 
@@ -134,19 +144,19 @@
     void NotificationQueue::tick(float dt)
     {
-        this->tickTime_ += dt; //!< Add the time interval that has passed to the time counter.
-        if(this->tickTime_ >= 1.0) //!< If the time counter is greater than 1s all Notifications that have expired are removed, if it is smaller we wait to the next tick.
-        {
-            this->timeLimit_.time = std::time(0)-this->displayTime_; //!< Container containig the current time.
-
-            std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it;
-            it = this->containers_.begin();
-            while(it != this->containers_.upper_bound(&this->timeLimit_)) //!< Iterate through all elements whose creation time is smaller than the current time minus the display time.
+        this->tickTime_ += dt; // Add the time interval that has passed to the time counter.
+        if(this->tickTime_ >= 1.0) // If the time counter is greater than 1s all Notifications that have expired are removed, if it is smaller we wait to the next tick.
+        {
+            this->timeLimit_.time = std::time(0)-this->displayTime_; // Container containig the current time.
+
+            std::multiset<NotificationContainer*, NotificationContainerCompare>::iterator it = this->ordering_.begin();
+            // Iterate through all elements whose creation time is smaller than the current time minus the display time.
+            while(it != this->ordering_.upper_bound(&this->timeLimit_))
             {
-                this->removeContainer(*it);
-                this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize())));
-                it = this->containers_.begin(); //TODO: Needed?
+                NotificationContainer* temp = *it;
+                it++;
+                this->remove(temp); // Remove the Notifications that have expired.
             }
 
-            this->tickTime_ = 0.0f; //!< Reset time counter.
+            this->tickTime_ = this->tickTime_ - (int)this->tickTime_; // Reset time counter.
         }
     }
@@ -155,5 +165,5 @@
     @brief
         Updates the NotificationQueue.
-        Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting the in the queue.
+        Updates by clearing the queue and requesting all relevant Notifications from the NotificationManager and inserting them into the queue.
     */
     void NotificationQueue::update(void)
@@ -161,17 +171,13 @@
         this->clear();
 
-        std::multimap<std::time_t,Notification*>* notifications = new std::multimap<std::time_t,Notification*>;
-        if(!NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_)) //!< Get the Notifications sent in the interval form now to minus the display time.
-        {
-            COUT(1) << "NotificationQueue update failed due to undetermined cause." << std::endl;
-            return;
-        }
-
-        if(notifications->empty())
-            return;
-
-        for(std::multimap<std::time_t,Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++) //!> Add all Notifications.
-        {
-            this->addNotification(it->second, it->first);
+        std::multimap<std::time_t, Notification*>* notifications = new std::multimap<std::time_t, Notification*>;
+        // Get the Notifications sent in the interval from now to now minus the display time.
+        NotificationManager::getInstance().getNotifications(this, notifications, this->displayTime_);
+
+        if(!notifications->empty())
+        {
+            // Add all Notifications.
+            for(std::multimap<std::time_t, Notification*>::iterator it = notifications->begin(); it != notifications->end(); it++)
+                this->push(it->second, it->first);
         }
 
@@ -191,15 +197,106 @@
     void NotificationQueue::update(Notification* notification, const std::time_t & time)
     {
-        this->addNotification(notification, time);
-
-        std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it;
-        while(this->getSize() > this->getMaxSize())
-        {
-            it = this->containers_.begin();
-            this->removeContainer(*it);
-            this->scroll(Vector2(0.0f,-(1.1f*this->getFontSize())));
-        }
-
-        COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notifications has been added." << std::endl;
+        this->push(notification, time);
+
+        COUT(4) << "NotificationQueue '" << this->getName() << "' updated. A new Notification has been added." << std::endl;
+    }
+
+    /**
+    @brief
+        Adds (pushes) a Notification to the NotificationQueue.
+        It inserts it into the storage containers, creates a corresponding container and pushes the Notification message to the GUI.
+    @param notification
+        The Notification to be pushed.
+    @param time
+        The time when the Notification has been sent.
+    */
+    void NotificationQueue::push(Notification* notification, const std::time_t & time)
+    {
+        NotificationContainer* container = new NotificationContainer;
+        container->notification = notification;
+        container->time = time;
+
+        // If the maximum size of the NotificationQueue has been reached the last (least recently added) Notification is removed.
+        if(this->getSize() >= this->getMaxSize())
+            this->pop();
+
+        this->size_++;
+
+        this->ordering_.insert(container);
+        // Insert the Notification at the begin of the list (vector, actually).
+        this->notifications_.insert(this->notifications_.begin(), container);
+
+        // Push the Notification to the GUI.
+        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.pushNotification(\"" + this->getName() + "\", \"" + notification->getMessage() + "\")");
+    }
+
+    /**
+    @brief
+        Removes (pops) the least recently added Notification form the NotificationQueue.
+    */
+    void NotificationQueue::pop(void)
+    {
+        NotificationContainer* container = this->notifications_.back();
+        this->ordering_.erase(container);
+        this->notifications_.pop_back();
+
+        this->size_--;
+
+        delete container;
+
+        // Pops the Notification from the GUI.
+        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.popNotification(\"" + this->getName() + "\")");
+    }
+
+    /**
+    @brief
+        Removes the Notification that is stored in the input NotificationContainer.
+    @param container
+        The NotificationContainer with the Notification to be removed.
+    */
+    void NotificationQueue::remove(NotificationContainer* container)
+    {
+        std::vector<NotificationContainer*>::iterator it = std::find(this->notifications_.begin(), this->notifications_.end(), container);
+        // Get the index at which the Notification is.
+        std::vector<NotificationContainer*>::difference_type index = it - this->notifications_.begin ();
+        this->ordering_.erase(container);
+        this->notifications_.erase(it);
+
+        this->size_--;
+
+        delete container;
+
+        // Removes the Notification from the GUI.
+        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.removeNotification(\"" + this->getName() + "\", " + multi_cast<std::string>(index) + ")");
+    }
+
+    /**
+    @brief
+        Clears the NotificationQueue by removing all NotificationContainers.
+    */
+    void NotificationQueue::clear(void)
+    {
+        this->ordering_.clear();
+        // Delete all NotificationContainers in the list.
+        for(std::vector<NotificationContainer*>::iterator it = this->notifications_.begin(); it != this->notifications_.end(); it++)
+            delete *it;
+
+        this->notifications_.clear();
+
+        this->size_ = 0;
+
+        // Clear the NotificationQueue in the GUI.
+        GUIManager::getInstance().getLuaState()->doString("NotificationLayer.clearQueue(\"" + this->getName() + "\")");
+    }
+
+    /**
+    @brief
+        Sets the name of the NotificationQueue.
+    @param name
+        The name to be set.
+    */
+    void NotificationQueue::setName(const std::string& name)
+    {
+        this->name_ = name;
     }
 
@@ -209,31 +306,14 @@
     @param size
         The size to be set.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::setMaxSize(int size)
-    {
-        if(size < 0)
-            return false;
+    */
+    void NotificationQueue::setMaxSize(unsigned int size)
+    {
+        if(this->maxSize_ == size)
+            return;
+
         this->maxSize_ = size;
-        this->update();
-        return true;
-    }
-
-    /**
-    @brief
-        Sets the maximum number of characters a Notification message displayed by this queue is allowed to have.
-    @param length
-        The length to be set.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::setNotificationLength(int length)
-    {
-        if(length < 0)
-            return false;
-        this->notificationLength_ = length;
-        this->update();
-        return true;
+
+        if(this->registered_)
+            this->update();
     }
 
@@ -246,210 +326,60 @@
         Returns true if successful.
     */
-    bool NotificationQueue::setDisplayTime(int time)
-    {
-        if(time < 0)
-            return false;
+    void NotificationQueue::setDisplayTime(unsigned int time)
+    {
+        if(this->displayTime_ == time)
+            return;
+
         this->displayTime_ = time;
-        this->update();
-        return true;
-    }
-
-    /**
-    @brief
-        Produces all targets concatinated as string, with kommas (',') as seperators.
-    @param string
-        Pointer to a string which will be used by the method to fill with the concatination of the targets.
+
+        if(this->registered_)
+            this->update();
+    }
+
+    /**
+    @brief
+        Produces all targets of the NotificationQueue concatinated as string, with kommas (',') as seperators.
     @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::getTargets(std::string* string) const
-    {
-        if(string == NULL)
-        {
-            COUT(4) << "Input string must have memory allocated." << std::endl;
-            return false;
-        }
-        string->clear();
+        Returns the targets as a string.
+    */
+    const std::string& NotificationQueue::getTargets(void) const
+    {
+        std::stringstream stream;
         bool first = true;
-        for(std::set<std::string>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++) //!< Iterate through the set of targets.
+        // Iterate through the set of targets.
+        for(std::set<std::string, NotificationListenerStringCompare>::const_iterator it = this->targets_.begin(); it != this->targets_.end(); it++)
         {
             if(!first)
-            {
-                *string += ',';
-            }
+                stream << ", ";
             else
-            {
                 first = false;
-            }
-            *string += *it;
-        }
-
-        return true;
-    }
-
-    /**
-    @brief
-        Sets the targets of the queue.
+            stream << *it;
+        }
+
+        return *(new std::string(stream.str()));
+    }
+
+    /**
+    @brief
+        Sets the targets of the NotificationQueue.
         The targets are the senders whose Notifications are displayed in this queue.
     @param targets
         Accepts a string of targets, each seperated by commas (','), spaces are ignored.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::setTargets(const std::string & targets)
+    */
+    void NotificationQueue::setTargets(const std::string & targets)
     {
         this->targets_.clear();
 
-        std::string* pTemp;
-        unsigned int index = 0;
-        while( index < targets.size() ) //!< Go through the string, character by character until the end is reached.
-        {
-            pTemp = new std::string();
-            while(index < targets.size() && targets[index] != ',' && targets[index] != ' ')
-            {
-                *pTemp += targets[index];
-                index++;
-            }
-            index++;
-            this->targets_.insert(*pTemp);
-        }
-
-        return true;
-    }
-
-    /**
-    @brief
-        Sets the font size.
-    @param size
-        The font size.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::setFontSize(float size)
-    {
-        if(size <= 0)
-            return false;
-        this->fontSize_ = size;
-        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font size for each overlay.
-        {
-            it->second->overlay->setFontSize(size);
-        }
-        return true;
-    }
-
-    /**
-    @brief
-        Sets the font.
-    @param font
-        The font.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::setFont(const std::string & font)
-    {
-        this->font_ = font;
-        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); it++) //!< Set the font for each overlay.
-        {
-            it->second->overlay->setFont(font);
-        }
-        return true;
-    }
-
-    /**
-    @brief
-        Scrolls the NotificationQueue, meaning all NotificationOverlays are moved the input vector.
-    @param pos
-        The vector the NotificationQueue is scrolled.
-    */
-    void NotificationQueue::scroll(const Vector2 pos)
-    {
-        for (std::map<Notification*, NotificationOverlayContainer*>::iterator it = this->overlays_.begin(); it != this->overlays_.end(); ++it) //!< Scroll each overlay.
-        {
-            it->second->overlay->scroll(pos);
-        }
-    }
-
-    /**
-    @brief
-        Aligns all the Notifications to the position of the NotificationQueue.
-    */
-    void NotificationQueue::positionChanged(void)
-    {
-        int counter = 0;
-        for (std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin(); it != this->containers_.end(); it++) //!< Set the position for each overlay.
-        {
-            (*it)->overlay->setPosition(this->getPosition());
-            (*it)->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*counter));
-            counter++;
-        }
-    }
-
-    /**
-    @brief
-        Adds a Notification, to the queue.
-        It inserts it into the storage containers, creates an corresponding overlay and a container.
-    @param notification
-        The Notification.
-    @param time
-        The time.
-    */
-    void NotificationQueue::addNotification(Notification* notification, const std::time_t & time)
-    {
-        NotificationOverlayContainer* container = new NotificationOverlayContainer;
-        container->overlay = new NotificationOverlay(this, notification);
-        container->notification = notification;
-        container->time = time;
-        std::string timeString = std::ctime(&time);
-        timeString.erase(timeString.length()-1);
-        const std::string& addressString = multi_cast<std::string>(reinterpret_cast<unsigned long>(notification));
-        container->name = "NotificationOverlay(" + timeString + ")&" + addressString;
-
-        this->containers_.insert(container);
-        this->overlays_[notification] = container;
-        this->addElement(container->overlay);
-        this->size_= this->size_+1;
-
-        container->overlay->scroll(Vector2(0.0f,(1.1f*this->getFontSize())*(this->getSize()-1)));
-    }
-
-    /**
-    @brief
-        Removes a container from the queue.
-    @param container
-        A pointer to the container.
-    @return
-        Returns true if successful.
-    */
-    bool NotificationQueue::removeContainer(NotificationOverlayContainer* container)
-    {
-        if(this->size_ == 0) //!< You cannot remove anything if the queue is empty.
-            return false;
-
-        // Unregister the NotificationQueue with the NotificationManager.
-        NotificationManager::getInstance().unregisterNotification(container->notification, this);
-
-        this->removeElement(container->overlay);
-        this->containers_.erase(container);
-        this->overlays_.erase(container->notification);
-        container->overlay->destroy();
-        delete container;
-        this->size_= this->size_-1;
-
-        return true;
-    }
-
-    /**
-    @brief
-        Clears the queue by removing all containers.
-    */
-    void NotificationQueue::clear(void)
-    {
-        std::multiset<NotificationOverlayContainer*, NotificationOverlayContainerCompare>::iterator it = this->containers_.begin();
-        while(it != this->containers_.end())
-        {
-            this->removeContainer(*it);
-            it = this->containers_.begin();
+        SubString string = SubString(targets, ",", " ", false);
+        for(unsigned int i = 0; i < string.size(); i++)
+            this->targets_.insert(string[i]);
+
+        if(this->registered_)
+        {
+            NotificationManager::getInstance().unregisterListener(this);
+            NotificationManager::getInstance().registerListener(this);
         }
     }
 
 }
+
