/***************************************************************************
 *   Copyright (C) 2004 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/ 
#ifndef EVAUSERSETTING_H
#define EVAUSERSETTING_H
 
#include "evafriendlist.h"
#include "evauserinfo.h"
#include "evaqunlist.h"
#include <list>
#include <string>
#include <stack>
#include <inttypes.h>
#include <qdatetime.h>
#include <qsize.h>
#include <kshortcut.h>
// this class processes saving & loading buddy list, message recording & user settings
class EvaUserSetting
{
public:
	EvaUserSetting(const int id);
	~EvaUserSetting();
	typedef struct{
	Q_UINT32 sender;
	QString sNick;
	Q_UINT32 receiver;
	QString rNick;
	Q_UINT8  type; // 0 auto reply,  1 normal
	QString  message;
	QDateTime time;
	Q_UINT8   fontSize;
	Q_UINT8   flag; // start from right.  bit 0: u, bit 1: i, bit 2: b
	Q_UINT8   blue;
	Q_UINT8   green;
	Q_UINT8   red;
	} chatMessage;
	
	typedef struct{
	Q_UINT16  messageType;
	Q_UINT8  type;
	Q_UINT32 from;
	Q_UINT32 to;
	Q_UINT32 internalQunID;
	QString  message;
	} sysMessage;
	
	// save Qun information method	
	const bool saveQunList(QObject *receiver, QunList &list);
	const bool loadQunList(QObject *receiver);
	
	// the group names include user creating ones and the first one "buddy list"
	const bool saveBuddyList(QObject *receiver, std::list<std::string> groups, ContactInfo &myInfo, FriendList &list,
				unsigned short extraInfo, std::string sig, unsigned int sigTime);
	const bool loadBuddyList(QObject * receiver);
	
	const bool saveMessage(const int buddy, const int sender, QString sNick, 
			const int receiver, QString rNick,
			const bool isNormal, 
			const QString message, 
			const QDateTime time, const char fontSize, 
			const bool u, const bool i, const bool b, 
			const char blue, const char green, const char red, const bool isQunMsg = false);
	
	std::list<chatMessage> getMessages( const int buddy, const int page, const bool isQunMsg = false); // page number starts from 0
	std::list<chatMessage> getMessages(const int buddy, const int page, const QDateTime starttime, const QDateTime endtime, 
						const bool isQunMsg = false);
	
	const bool saveSysMessage(const short msgType, const uint8_t type, const int fromQQ, const int toQQ, 
				const QString message, const int internalQunID = 0);
	std::list<sysMessage> getSysMessages( const int page); // page number starts from 0
	sysMessage getLastSysMessage();
	
	const bool saveSettings();
	const bool loadSettings();
	void loadDefaultSettings();
	
	void setThemeDir(const QString &dir) { themeDir = dir; }
	void setSoundDir(const QString &dir) { soundDir = dir; }
	void setPageSize(const int size) { pageSize = size; }
	void setFaceSize(const QSize &size) { faceSize = size; }
	void setShowSystemBroadcastEnabled(const bool show) { showQQBroadcast = show; }
	void setShowMessageTipEnabled( const bool show) { showMessageTipWindow = show; }
	void setShowBudyOnlineNotifyEnabled( const bool show) { showBudyOnlineNotifyWindow = show; }
	void setShowOnlineEnabled( const bool show) { showOnlineUsers = show; }
	void setSendKeyEnterEnabled(const bool ok) { isSendKeyEnter = ok; }
	void setSoundEnabled(const bool play) { playSound = play; }
	void setAutoReplyEnabled(const bool reply) { autoReply = reply; }
	
	const QString &getThemeDir() const { return themeDir; }
	const QString &getSoundDir() const { return soundDir; }
	const int getPageSize() const { return pageSize; }
	const QSize &getFaceSize() const { return faceSize; }
	const bool isShowSystemBroadcastEnabled() const { return showQQBroadcast; }
	const bool isShowMessageTipEnabled() const { return showMessageTipWindow;}
	const bool isShowBudyOnlineNotifyEnabled() const { return showBudyOnlineNotifyWindow;}
	const bool isShowOnlineEnabled() const { return showOnlineUsers;}
	const bool isSendKeyEnterEnabled() const { return isSendKeyEnter; }
	const bool isSoundEnabled() const { return playSound;}
	const bool isAutoReplyEnabled() const { return autoReply; }
	
	const int getAutoReplySelectedIndex() const { return autoSelectedIndex; }
	const QString &getSelectedAutoReply();
	const QString &getQuickReplyMessageAt(const int index);
	void addAutoReplyMessage(const QString &message);
	void addQuickReplyMessage(const QString &message);
	
	void setAutoReplySelectedIndex(const int index) { autoSelectedIndex = index; }
	void setAutoReplyList(std::list<QString> list) { autoList = list;}
	void setQuickReplyList(std::list<QString> list) { quickList = list;}
	const std::list<QString> &getAutoReplyList();
	const std::list<QString> &getQuickReplyList();

	const QString getPictureCacheDir() const { return pictureCacheDir; }
	void setPictureCacheDir(const QString path) { pictureCacheDir = path; }
	
	const QString getUserHeadDir() const { return userHeadDir; }
	void setUserHeadDir(const QString path) { userHeadDir = path; }	
	
	const KShortcut &getMessageShortcut() const { return shortcut; }
	void setMessageShortcut( const KShortcut &key) { shortcut = key; }
	
	static bool isVersionCorrect(const QString fileName);
// protected:
// 	virtual void customEvent( QCustomEvent *e);
private:
	const QString getUserHomeDir();
	const QString getEvaSettingDir();
	const QString getEvaUserDir();
	const bool isDirExisted(const QString &dir);
	bool createDefaultDirs();
	int qqNum;
	bool isUserDirExisted;
	QString sysSettingDir;
	QString pictureCacheDir;
	QString userHeadDir;
	
	QString themeDir;
	QString soundDir;
	int pageSize;	
	QSize faceSize;
	bool showQQBroadcast;
	bool showMessageTipWindow;
	bool showBudyOnlineNotifyWindow;
	bool showOnlineUsers;
	bool isSendKeyEnter;
	bool playSound;
	bool autoReply;
	KShortcut shortcut;

	int autoSelectedIndex;
	std::list<QString>autoList;
	std::list<QString>quickList;

	
	QString buddyListFileName;
	QString qunListFileName;
	QString chatMsgFileName;
	QString qunMsgFileName;
	QString sysMsgFileName;
	QString userProfileFileName;
	
	std::stack<sysMessage> sysStack;

};

#endif
 
