/*
	MailCacheSupport.h
*/
#ifndef _MAIL_CACHE_SUPPORT_H
#define _MAIL_CACHE_SUPPORT_H
#include <Binder.h>
#include <SmartArray.h>
#include <String.h>
#include <XMLParser.h>
#include "MimeMessage.h"

namespace Wagner {

class UserContainer {
	public:
								UserContainer();
								UserContainer(const UserContainer &inContainer);
								UserContainer(const char *user, const char *login, const char *password,
												const char *server, const char *port);
								~UserContainer();
								
		UserContainer &			operator=(const UserContainer &container);
	
		void					SetUserName(const char *name);
		void					SetLogin(const char *login);
		void					SetPassword(const char *password);
		void					SetServer(const char *server);
		void					SetPort(int port);
		
		const char *			GetUserName() const;
		const char *			GetLogin() const;
		const char *			GetPassword() const;
		const char *			GetServer() const;
		int						GetPort() const;
		
		bool					IsValid();
		void					Clear();
		void					PrintToStream();						

	private:
		BString fUserName;
		BString fLogin;
		BString fPassword;
		BString fServer;
		int fPort;
};

struct message_summary {
								message_summary() : fDirty(false) {
									// empty body
								};

								message_summary(const message_summary &inSummary) {
									*this = inSummary;
								};

		message_summary &		operator=(const message_summary &inSummary) {
									if (this != &inSummary) {
										fDirty = inSummary.fDirty;
										fMime = inSummary.fMime;
									}
									return *this;
								};
	// Member variables.	
	bool fDirty;
	MimeMessage fMime;
};

struct loaded_summary {
								loaded_summary() : fMessages(NULL) {
									Clear();
	 							};
																
								loaded_summary(const loaded_summary &inSummary) {
									*this = inSummary;
								};
								
		loaded_summary &		operator=(const loaded_summary &inSummary) {
									if (this != &inSummary) {
										fModified = inSummary.fModified;
										fDirty = inSummary.fDirty;
										fTotalMessages = inSummary.fTotalMessages;
										fUnseenMessages = inSummary.fUnseenMessages;
										fUid = inSummary.fUid;
										fName = inSummary.fName;
										fUser = inSummary.fUser;
										delete [] fMessages;
										fMessages = new message_summary[fTotalMessages];
										for (int32 i = 0; i < fTotalMessages; i++)
											fMessages[i].fMime = inSummary.fMessages[i].fMime;
										
									}
									return *this;
								};
															
								~loaded_summary() {
									delete [] fMessages;
									fMessages = NULL;
								};
	
		void					Clear() {
									fModified = false;
									fDirty = false;
									fTotalMessages = 0;
									fUnseenMessages = 0;
									fMailboxSize = 0;
									fMailboxQuota = 0;
									fUid = "";
									fName = "";
									fUser.Clear();
									delete [] fMessages;
									fMessages = NULL;
								};
	
	// Member variables
	bool fModified;			// Modified with respect to what we have on disk
	bool fDirty;			// Dirty with respect to what's on the server
	int32 fTotalMessages;
	int32 fUnseenMessages;
	int32 fMailboxSize;
	int32 fMailboxQuota;
	BString fUid;
	BString fName;
	UserContainer fUser;
	message_summary *fMessages;
};

class SummaryFileParseContext : public BXMLParseContext {
	public:
								SummaryFileParseContext(loaded_summary *summary);

		virtual status_t		OnStartTag(BString &name, BStringMap &attribute);

	private:
		loaded_summary *fSummary;
		int32 fIndex;
};

class MailboxNodeContainer : public BinderContainer {
	public:
								MailboxNodeContainer();
		virtual					~MailboxNodeContainer();
								// Binder virtuals
		virtual get_status_t	ReadProperty(const char *name, property &outProperty, const property_list &inArgs = empty_arg_list);
};

class MailBinderNode : public BinderContainer {
	public:
								MailBinderNode();
		virtual					~MailBinderNode();
								// Binder virtuals
		virtual put_status_t	WriteProperty(const char *name, const property &inProperty);
		virtual get_status_t	ReadProperty(const char *name, property &outProperty, const property_list &inArgs = empty_arg_list);
};

} // end Wagner namespace
#endif