www.pudn.com > fanccMSNr.src.rar > MessengerModel.hpp


#pragma once 
#include  
#include  
#include  
#include  
using namespace std; 
 
#include "MessageDispatcher.hpp" 
#include "ChatterModel.hpp" 
#include "MessengerListener.hpp" 
#include "MIMEMessage.hpp" 
#include "User.hpp" 
 
namespace poral { 
 
	/** 
	* Manages connection of dispatch server and notification server. 
	*/ 
	class MessengerModel:  
		public MessageListener			// interface 
	{ 
		// Type and property definitions  ///////////////////// 
	public: 
		typedef enum { 
			NOT_CONNECTED, 
			CONNECTING, 
			CONNECTED 
		} Status; 
 
		/** FL list members. */ 
		vector users; 
		/** Map of status code to displayable name. */ 
		Properties statusName; 
	protected: 
		Status status; 
		/** Should refrenced by getTransactionID() */ 
		int transactionID; 
		/** My information. */ 
		User my; 
		string password; 
 
		/** Contains companion id to talk to if startChatWith() called, or equals "" if otherwise. */ 
		string companionID; 
 
		MessageDispatcher *messageDispatcher; 
		list listeners; 
		// TODO: Change type to list and set allocator 
		list chatters; 
 
	public: 
		// Constructors and destructors ///////////////////// 
		MessengerModel(MessageDispatcher &md): 
		companionID("") { 
			status=NOT_CONNECTED; 
			messageDispatcher=&md; 
			transactionID=0; 
		} 
		virtual ~MessengerModel(void); 
 
		// Getter and setters ///////////////////// 
		Status getStatus() const { 
			return status; 
		} 
		MessageDispatcher *getMessageDispatcher() const { 
			return messageDispatcher; 
		} 
		int getTransactionID() { 
			return transactionID++; 
		} 
		const User &getMy() const { 
			return my; 
		} 
		void addMessengerListener(MessengerListener &listener); 
		void removeMessengerListener(const MessengerListener &listener); 
 
		/** 
		* Creates ChatterModel. 
		* The new ChatterModel allocated to inside structure of MessengerModel. 
		* You should call removeChatterModel() when the model needed no more. 
		* ChatterModel will valid until only MessengerModel deleted. 
		*/ 
		ChatterModel &createChatterModel(); 
		void removeChatterModel(const ChatterModel &model); 
 
		// Operators ///////////////////// 
		void login(const string &id, const string &password); 
		void cancelLogin(); 
		void logout(); 
		void setStatus(const string &status); 
		void setScreenName(const string &screenName); 
		void blockUser(bool block, const string &userID); 
		void removeUser(const string &userID); 
		void addUser(const string &userID); 
		void startChatWith(const string &companionID); 
		/** Send SYN message to synchronize user information. */ 
		void synchronizeUser(); 
 
		/** Searches user by id. */ 
		vector::iterator findUser(const string &id); 
		/** Searches chatter by companion id. */ 
		list::iterator findChatter(const string &companionID); 
 
		// Implementation of MessageListener. ///////////////////// 
		// Shoud not call these mehtods. 
		virtual void connected(); 
		virtual void messageArrived(const Message &message); 
		virtual void failed(); 
 
	protected: 
		/**  
		* Adds a new user or modify the information if it exists.  
		* @return newly allocated or modified object. 
		*/ 
		User &addUser(const string &status, const string &ID, const string &screenName); 
 
		// Fire functions //////////////////// 
 
		/** Fires MessengerModel::messengerUpdate() */ 
		void fireMessengerUpdate(MessengerListener::Event event); 
		/** Fires MessengerModel::userUpdate() */ 
		void fireUserUpdate(const User &user, MessengerListener::UserEvent event); 
		/** Fires MessengerModel::chatterViewRequested() */ 
		void fireChatterViewRequested(ChatterModel &model); 
 
		/**  
		* Change model status to disconnected.  
		* 
		* @param closed Indicates messageDispatcher has been closed. 
		*               disconnect() does not call messageDispatcher->close(); 
		*				when this variable set to true. 
		*/ 
		void disconnect(MessengerListener::Event event, bool closed=false); 
 
	private: 
		/** Used privately in messageArrived() */ 
		void MIMEMessageArrived(const MIMEMessage &message); 
 
		friend class ChatterModel; 
	}; 
}