Index: code/branches/tutoriallevel3/src/modules/notifications/NotificationManager.cc
===================================================================
--- code/branches/tutoriallevel3/src/modules/notifications/NotificationManager.cc	(revision 8452)
+++ code/branches/tutoriallevel3/src/modules/notifications/NotificationManager.cc	(revision 8453)
@@ -36,33 +36,17 @@
 #include "core/command/ConsoleCommand.h"
 #include "core/CoreIncludes.h"
-#include "core/GUIManager.h"
 #include "core/LuaState.h"
-#include "network/Host.h"
-#include "network/NetworkFunction.h"
 #include "util/ScopedSingletonManager.h"
 
 #include "interfaces/NotificationListener.h"
 
-#include "Notification.h"
 #include "NotificationQueue.h"
-
-#include "ToluaBindNotifications.h"
+#include "NotificationQueueCEGUI.h"
 
 namespace orxonox
 {
 
-    const std::string NotificationManager::ALL("all");
-    const std::string NotificationManager::NONE("none");
-
-    // Register tolua_open function when loading the library.
-    DeclareToluaInterface(Notifications);
-
     ManageScopedSingleton(NotificationManager, ScopeID::Root, false);
 
-    // Setting console command to enter the edit mode.
-    SetConsoleCommand("enterEditMode", &NotificationManager::enterEditMode);
-
-    registerStaticNetworkFunction(NotificationManager::sendNotification);
-
     /**
     @brief
@@ -73,8 +57,4 @@
         RegisterRootObject(NotificationManager);
 
-        this->highestIndex_ = 0;
-
-        ModifyConsoleCommand("enterEditMode").setObject(this);
-
         COUT(3) << "NotificatioManager created." << std::endl;
     }
@@ -86,6 +66,4 @@
     NotificationManager::~NotificationManager()
     {
-        ModifyConsoleCommand("enterEditMode").setObject(NULL);
-
         // Destroys all Notifications.
         for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it!= this->allNotificationsList_.end(); it++)
@@ -106,5 +84,5 @@
         while(it != this->queues_.end())
         {
-            it->second->destroy(true);
+            it->second->destroy();
             it = this->queues_.begin();
         }
@@ -115,33 +93,75 @@
     /**
     @brief
-        Sends a Notification with the specified message to the specified client from the specified sender.
+        Creates and registers a Notification with the input message from the input sender.
+        This is called by the NotificationListener, whenever a new notification arrives.
     @param message
-        The message that should be sent.
-    @param clientId
-        The id of the client the notification should be sent to.
+        The message of the new Notification.
     @param sender
-        The sender that sent the notification.
-    @param isLocal
-        If this is set to true (false is default), then the Notification is sent to the client where this function is executed, meaning the Notification is sent locally.
-    */
-    /*static*/ void NotificationManager::sendNotification(const std::string& message, unsigned int clientId, const std::string& sender, bool isLocal)
-    {
-        // If we're in standalone mode or we're already no the right client we create and send the Notification.
-        if(GameMode::isStandalone() || isLocal || Host::getPlayerID() == clientId)
-        {
-            Notification* notification = new Notification(message, sender);
-            if(NotificationManager::getInstance().registerNotification(notification))
-                COUT(3) << "Notification \"" << notification->getMessage() << "\" sent." << std::endl;
-        }
-        // If we're on the server (and the server is not the intended recipient of the Notification) we send it over the network.
-        else if(GameMode::isServer())
-        {
-            callStaticNetworkFunction(NotificationManager::sendNotification, clientId, message, clientId, sender);
-        }
-    }
-
-    /**
-    @brief
-        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationListeners associated with its sender.
+        The name of the entity (of the collective) that sent the new Notification.
+    @param type
+        The type of the new Notification.
+    @return
+        Returns true if successful.
+    */
+    bool NotificationManager::registerNotification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
+    {
+        // TODO: Do something with the type.
+        Notification* notification = new Notification(message, sender, type);
+        return this->registerNotification(notification);
+    }
+
+    /**
+    @brief
+        Executes the input command from the input sender.
+        This is called by the NotificationListener, whenever a new command arrives.
+    @param command
+        The command to be executed,
+    @param sender
+        The The name of the entity (of the collective) that sent the command.
+    @return
+        Returns true if the command was successfully executed.
+    */
+    bool NotificationManager::executeCommand(notificationCommand::Value command, const std::string& sender)
+    {
+        bool commandExecuted = false;
+        if(command == notificationCommand::clear)
+        {
+            if(this->commandClear(sender))
+                commandExecuted = true;
+        }
+
+        if(commandExecuted)
+            COUT(3) << "Notification command \"" << NotificationListener::command2Str(command) << "\" executed." << endl;
+
+        return commandExecuted;
+    }
+
+    /**
+    @brief
+        The clear command. Clears all NotificationQueues that have its sender as a target.
+    @param sender
+        The sender of the clear command.
+    @return
+        Returns true if the command was successfully executed by at least one NotificationQueue, false if it was not executed.
+    */
+    bool NotificationManager::commandClear(const std::string& sender)
+    {
+        bool all = (sender == NotificationListener::ALL);
+        bool executed = false;
+        // Clear all NotificationQueues that have the input sender as target.
+        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
+        {
+            const std::set<std::string>& set = it->second->getTargetsSet();
+            // If either the sender is 'all', the NotificationQueue has as target all or the NotificationQueue has the input sender as a target.
+            if(all || set.find(NotificationListener::ALL) != set.end() || set.find(sender) != set.end())
+                executed = it->second->tidy() || executed;
+        }
+
+        return executed;
+    }
+    
+    /**
+    @brief
+        Registers a Notification within the NotificationManager and makes sure that the Notification is sent to all the NotificationQueues associated with its sender.
     @param notification
         The Notification to be registered.
@@ -158,22 +178,21 @@
         this->allNotificationsList_.insert(std::pair<std::time_t, Notification*>(time, notification));
 
-        if(notification->getSender() == NotificationManager::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
+        if(notification->getSender() == NotificationListener::NONE) // If the sender has no specific name, then the Notification is only added to the list of all Notifications.
             return true;
 
-        bool all = false;
-        if(notification->getSender() == NotificationManager::ALL) // If all are the sender, then the Notifications is added to every NotificationListener.
-            all = true;
-
-        // Insert the Notification in all NotificationListeners that have its sender as target.
-        for(std::map<NotificationListener*, unsigned int>::iterator it = this->listenerList_.begin(); it != this->listenerList_.end(); it++) // Iterate through all NotificationListeners.
-        {
-            const std::set<std::string>& set = it->first->getTargetsSet();
-            bool bAll = set.find(NotificationManager::ALL) != set.end();
-            // If either the Notification has as sender 'all', the NotificationListener displays all Notifications or the NotificationListener has the sender of the Notification as target.
+        // If all are the sender, then the Notifications is added to every NotificationQueue.
+        bool all = (notification->getSender() == NotificationListener::ALL);
+
+        // Insert the Notification in all NotificationQueues that have its sender as target.
+        for(std::map<const std::string, NotificationQueue*>::iterator it = this->queues_.begin(); it != this->queues_.end(); it++) // Iterate through all NotificationQueues.
+        {
+            const std::set<std::string>& set = it->second->getTargetsSet();
+            bool bAll = set.find(NotificationListener::ALL) != set.end();
+            // If either the Notification has as sender 'all', the NotificationQueue displays all Notifications or the NotificationQueue has the sender of the Notification as target.
             if(all || bAll || set.find(notification->getSender()) != set.end())
             {
                 if(!bAll)
-                    this->notificationLists_[it->second]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationListener.
-                it->first->update(notification, time); // Update the NotificationListener.
+                    this->notificationLists_[it->second->getName()]->insert(std::pair<std::time_t, Notification*>(time, notification)); // Insert the Notification in the notifications list of the current NotificationQueue.
+                it->second->update(notification, time); // Update the NotificationQueue.
             }
         }
@@ -186,19 +205,19 @@
     /**
     @brief
-        Unregisters a Notification within the NotificationManager for a given NotificationListener.
+        Unregisters a Notification within the NotificationManager for a given NotificationQueue.
     @param notification
         A pointer to the Notification to be unregistered.
-    @param listener
-        A pointer to the NotificationListener the Notification is unregistered for.
-    */
-    void NotificationManager::unregisterNotification(Notification* notification, NotificationListener* listener)
+    @param queue
+        A pointer to the NotificationQueue the Notification is unregistered for.
+    */
+    void NotificationManager::unregisterNotification(Notification* notification, NotificationQueue* queue)
     {
         assert(notification);
-        assert(listener);
-
-        // Remove the Notification from the list of Notifications of the input NotificationListener.
-        this->removeNotification(notification, *(this->notificationLists_.find(this->listenerList_.find(listener)->second)->second));
-
-        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from listener (&" << listener << ")" << std::endl;
+        assert(queue);
+
+        // Remove the Notification from the list of Notifications of the input NotificationQueue.
+        this->removeNotification(notification, *(this->notificationLists_.find(queue->getName())->second));
+
+        COUT(4) << "Notification (&" << notification << ") unregistered with the NotificationManager from NotificationQueue " << queue->getName() << "." << std::endl;
     }
 
@@ -230,89 +249,7 @@
     /**
     @brief
-        Registers a NotificationListener within the NotificationManager.
-    @param listener
-        The NotificationListener to be registered.
-    @return
-        Returns true if successful.  Fales if the NotificationListener is already registered.
-    */
-    bool NotificationManager::registerListener(NotificationListener* listener)
-    {
-        assert(listener);
-
-        // If the NotificationListener is already registered.
-        if(this->listenerList_.find(listener) != this->listenerList_.end())
-            return false;
-
-        this->highestIndex_ += 1;
-        unsigned int index = this->highestIndex_; // An identifier that identifies each registered NotificationListener uniquely.
-
-        this->listenerList_[listener] = index; // Add the NotificationListener to the list of NotificationListeners.
-
-        const std::set<std::string>& set = listener->getTargetsSet();
-
-        // 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.
-        bool bAll = set.find(NotificationManager::ALL) != set.end();
-        std::multimap<std::time_t, Notification*>* map = NULL;
-        if(bAll)
-            this->notificationLists_[index] = &this->allNotificationsList_;
-        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationListeners.
-        else
-        {
-            this->notificationLists_[index] = new std::multimap<std::time_t, Notification*>;
-            map = this->notificationLists_[index];
-        }
-
-        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationListener.
-        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
-        {
-            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
-                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
-        }
-
-        listener->update(); // Update the listener.
-
-        COUT(4) << "NotificationListener registered with the NotificationManager." << std::endl;
-
-        return true;
-    }
-
-    /**
-    @brief
-        Unregisters a NotificationListener within the NotificationManager.
-    @param listener
-        The NotificationListener to be unregistered.
-    */
-    void NotificationManager::unregisterListener(NotificationListener* listener)
-    {
-        assert(listener);
-
-        unsigned int identifier = this->listenerList_.find(listener)->second;
-        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(identifier)->second;
-
-        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
-        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
-        if(map != &this->allNotificationsList_)
-        {
-            while(it != map->end())
-            {
-                this->unregisterNotification(it->second, listener);
-                it = map->begin();
-            }
-            delete map;
-        }
-
-        COUT(4) << "NotificationListener '" << identifier << "' unregistered with the NotificationManager." << std::endl;
-
-        // Remove the NotificationListener from the list of NotificationListeners.
-        this->listenerList_.erase(listener);
-        // Remove the Notifications list that was associated with the input NotificationListener.
-        this->notificationLists_.erase(identifier);
-    }
-
-    /**
-    @brief
-        Fetches the Notifications for a specific NotificationListener in a specified timeframe and stores them in the input map.
-    @param listener
-        The NotificationListener the Notifications are fetched for.
+        Fetches the Notifications for a specific NotificationQueue in a specified timeframe and stores them in the input map.
+    @param queue
+        The NotificationQueue the Notifications are fetched for.
     @param map
         A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
@@ -324,10 +261,10 @@
         Returns true if successful.
     */
-    void NotificationManager::getNotifications(NotificationListener* listener, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
-    {
-        assert(listener);
+    void NotificationManager::getNotifications(NotificationQueue* queue, std::multimap<std::time_t,Notification*>* map, const std::time_t & timeFrameStart, const std::time_t & timeFrameEnd)
+    {
+        assert(queue);
         assert(map);
 
-        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[this->listenerList_[listener]]; // All the Notifications for the input NotificationListener.
+        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
 
         std::multimap<std::time_t,Notification*>::iterator it, itLowest, itHighest;
@@ -342,13 +279,31 @@
     /**
     @brief
-        Enters the edit mode of the NotificationLayer.
-    */
-    void NotificationManager::enterEditMode(void)
-    {
-        if(GameMode::showsGraphics())
-        {
-            GUIManager::getInstance().hideGUI("NotificationLayer");
-            GUIManager::getInstance().showGUI("NotificationLayer", false, false);
-            GUIManager::getInstance().getLuaState()->doString("NotificationLayer.enterEditMode()");
+        Fetches the newest Notifications for a specific NotificationQueue and stores them in the input map.
+    @param queue
+        The NotificationQueue the Notifications are fetched for.
+    @param map
+        A pointer to a multimap, in which the notifications are stored. The map needs to have been allocated.
+    @param numberOfNotifications
+        The number of newest Notifications to be got.
+    @return
+        Returns true if successful.
+    */
+    void NotificationManager::getNewestNotifications(NotificationQueue* queue, std::multimap<std::time_t, Notification*>* map, int numberOfNotifications)
+    {
+        assert(queue);
+        assert(map);
+
+        std::multimap<std::time_t, Notification*>* notifications = this->notificationLists_[queue->getName()]; // All the Notifications for the input NotificationQueue.
+
+        if(!notifications->empty()) // If the list of Notifications is not empty.
+        {
+            std::multimap<std::time_t,Notification*>::iterator it = notifications->end();
+            for(int i = 0; i < numberOfNotifications; i++) // Iterate through the Notifications from the newest until we have the specified number of notifications.
+            {
+                it--;
+                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second)); // Add the found Notifications to the map.
+                if(it == notifications->begin())
+                    break;
+            }
         }
     }
@@ -357,5 +312,5 @@
     @brief
         Registers a NotificationQueue.
-        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.
+        This makes sure that the NotificationQueue can be accessed through lua by name. It also makes sure that the NotificationQueue is destroyed upon destruction of the NotificationManager.
     @param queue
         A pointer to the NotificationQueue to be registered.
@@ -365,6 +320,37 @@
     bool NotificationManager::registerQueue(NotificationQueue* queue)
     {
+        assert(queue);
+
+        // If the NotificationQueue is already registered.
+        if(this->queues_.find(queue->getName()) != this->queues_.end())
+            return false;
+
+        this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)); // Add the NotificationQueue to the list of NotificationQueues.
+
+        const std::set<std::string>& set = queue->getTargetsSet();
+
+        // If all senders are the target of the NotificationQueue, then the list of Notifications for that specific NotificationQueue is the same as the list of all Notifications.
+        bool bAll = set.find(NotificationListener::ALL) != set.end();
+        std::multimap<std::time_t, Notification*>* map = NULL;
+        if(bAll)
+            this->notificationLists_[queue->getName()] = &this->allNotificationsList_;
+        // Else a new list (resp. multimap) is created and added to the list of Notification lists for NotificationQueues.
+        else
+        {
+            this->notificationLists_[queue->getName()] = new std::multimap<std::time_t, Notification*>;
+            map = this->notificationLists_[queue->getName()];
+        }
+
+        // Iterate through all Notifications to determine whether any of them should belong to the newly registered NotificationQueue.
+        for(std::multimap<std::time_t, Notification*>::iterator it = this->allNotificationsList_.begin(); it != this->allNotificationsList_.end(); it++)
+        {
+            if(!bAll && set.find(it->second->getSender()) != set.end()) // Checks whether the listener has the sender of the current Notification as target.
+                map->insert(std::pair<std::time_t, Notification*>(it->first, it->second));
+        }
+
+        queue->update(); // Update the queue.
+        
         COUT(4) << "NotificationQueue '" << queue->getName() << "' registered with the NotificationManager." << std::endl;
-        return this->queues_.insert(std::pair<const std::string, NotificationQueue*>(queue->getName(), queue)).second;
+        return true;
     }
 
@@ -377,27 +363,26 @@
     void NotificationManager::unregisterQueue(NotificationQueue* queue)
     {
+        assert(queue);
+
+        std::multimap<std::time_t, Notification*>* map = this->notificationLists_.find(queue->getName())->second;
+
+        // If the map is not the map of all Notifications, make sure all Notifications are unregistered.
+        std::multimap<std::time_t, Notification*>::iterator it = map->begin();
+        if(map != &this->allNotificationsList_)
+        {
+            while(it != map->end())
+            {
+                this->unregisterNotification(it->second, queue);
+                it = map->begin();
+            }
+            delete map;
+        }
+
+        // Remove the NotificationQueue from the list of NotificationQueues.
+        this->queues_.erase(queue->getName());
+        // Remove the Notifications list that was associated with the input NotificationQueue.
+        this->notificationLists_.erase(queue->getName());
+        
         COUT(4) << "NotificationQueue '" << queue->getName() << "' unregistered with the NotificationManager." << std::endl;
-        this->queues_.erase(queue->getName());
-    }
-
-    /**
-    @brief
-        Loads all the NotificationQueues that should exist.
-    */
-    void NotificationManager::loadQueues(void)
-    {
-        new NotificationQueue("all");
-    }
-
-    /**
-    @brief
-        Creates a new NotificationQueue.
-        This is used in lua.
-    @param name
-        The name of the new NotificationQueue.
-    */
-    void NotificationManager::createQueue(const std::string& name)
-    {
-        new NotificationQueue(name);
     }
 
@@ -420,3 +405,60 @@
     }
 
+    /**
+    @brief
+        Loads all the NotificationQueues that should exist.
+    */
+    void NotificationManager::loadQueues(void)
+    {
+        NotificationQueueCEGUI* allQueue = new NotificationQueueCEGUI("all");
+        allQueue->setDisplaySize(Vector2(0.5, 0));
+        allQueue->setPosition(Vector4(0.0, 10, 0.3, 0));
+
+        NotificationQueueCEGUI* infoQueue = new NotificationQueueCEGUI("info", "gameinfo", 1, -1);
+        infoQueue->setPosition(Vector4(0.2, 0, 0.8, 0));
+        infoQueue->setFontSize(24);
+        infoQueue->setFontColor(Vector4(1.0, 1.0, 0.0, 0.8));
+        infoQueue->setAlignment("HorzCentred");
+        infoQueue->setDisplaySize(Vector2(0.6, 0.0));
+    }
+
+    // Notification class
+
+    /**
+    @brief
+        Constructor. Creates a Notification with the input message and sender.
+    @param message
+        The message of the Notification.
+    @param sender
+        The sender of the Notification.
+    @param type
+
+    */
+    Notification::Notification(const std::string& message, const std::string& sender, notificationMessageType::Value type)
+    {
+        this->initialize();
+        this->message_ = message;
+        this->sender_ = sender;
+        this->type_ = type;
+    }
+
+    /**
+    @brief
+        Destructor.
+    */
+    Notification::~Notification()
+    {
+
+    }
+
+    /**
+    @brief
+        Registers the object and sets some default values.
+    */
+    void Notification::initialize(void)
+    {
+        this->message_.clear();
+        this->sender_ = NotificationListener::NONE;
+    }
+
 }
