/***************************************************************************
 *   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 <list>
#include <string>
#include <stack>
#include <inttypes.h>
#include <qdatetime.h>
#include <qsize.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_UINT8  type;
	Q_UINT32 from;
	Q_UINT32 to;
	QString  message;
	} sysMessage;
	
	// the group names include user creating ones and the first one "buddy list"
	const bool saveBuddyList(std::list<std::string> groupNames, ContactInfo &myInfo, FriendList &list);
	const bool loadBuddyList(std::list<std::string> &groupNames, ContactInfo &myInfo, FriendList &list);
	
	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);
	
	std::list<chatMessage> getMessages( const int buddy, const int page); // page number starts from 0
	std::list<chatMessage> getMessages(const int buddy, const int page, const QDateTime starttime, const QDateTime endtime);
	
	const bool saveSysMessage(const uint8_t type, const int fromQQ, const int toQQ, const QString message);
	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();
private:
	const QString getUserHomeDir();
	const QString getEvaSettingDir();
	const QString getEvaUserDir();
	const bool isDirExisted(const QString &dir);
	int qqNum;
	bool isUserDirExisted;
	QString sysSettingDir;

	QString themeDir;
	QString soundDir;
	int pageSize;	
	QSize faceSize;
	bool showQQBroadcast;
	bool showMessageTipWindow;
	bool showBudyOnlineNotifyWindow;
	bool showOnlineUsers;
	bool isSendKeyEnter;
	bool playSound;
	bool autoReply;

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

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

#endif
 
