/***************************************************************************
                         SP Mail project : Simple Powerful Mail
                          CMailFolders.cpp  -  description
                             -------------------
    begin                : 23 June 2003
    copyright            : (C) 2000-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <sptk3/CSmallPixmaps.h>
#include <sptk3/CStrings.h>

#include "CImapFolders.h"
#include "mailbox.xpm"

using namespace std;

Fl_Pixmap mailboxPixmap(mailbox_xpm);

CImapFolders::CImapFolders(CRegistry& registry)
: CTreeView("",180,SP_ALIGN_LEFT), m_registry(registry) {
	end();
}

CImapFolders::~CImapFolders() {
}

void CImapFolders::loadAccounts() {
	m_accountsMap.clear();
	CXmlNode* topic = m_registry.findFirst("accounts");
	if (topic) { 
		CXmlNode::iterator itor = topic->begin();
		CXmlNode::iterator iend = topic->end();
		for (; itor != iend; itor++) {
			CXmlNode& accnt = *(*itor);
			string type = accnt.attributes["type"];

			if (type == "IMAP") {
				string name     = accnt.attributes["name"];
				string server   = accnt.attributes["server"];
				string user     = accnt.attributes["user"];
				string password = accnt.attributes["password"];

				CImapAccount *imapAccount = new CImapAccount(name,server,user,password);
				std::map<std::string,CImapAccount *>::iterator itor = m_accountsMap.find(name);
				if (itor != m_accountsMap.end())
					delete itor->second;
				m_accountsMap[name] = imapAccount;
				imapAccount->loadFolders();
			}
		}
	}
	createFolders();
}

void CImapFolders::readAccounts() {
}

void CImapFolders::clear() {
	CTreeView::clear();
	std::map<std::string,CImapAccount *>::iterator itor = m_accountsMap.begin();
	for (; itor != m_accountsMap.end(); itor++)
		delete itor->second;
	m_accountsMap.clear();
}

void CImapFolders::createFolders() {
	CTreeView::clear();
	std::map<std::string,CImapAccount *>::iterator itor = m_accountsMap.begin();
	for (; itor != m_accountsMap.end(); itor++) {
		CImapAccount *imapAccount = itor->second;
		const CStrings& folders = imapAccount->folders();

		CTreeItem* node = addItem(imapAccount->name(),&mailboxPixmap);
		for (unsigned i = 0; i < folders.size(); i++) {
			vector<string> path;
			stringToStringVector(folders[i],path,"/");
			node->addPath(path,CSmallPixmaps::pixmaps[SXPM_FOLDER_OPENED],CSmallPixmaps::pixmaps[SXPM_FOLDER],CSmallPixmaps::pixmaps[SXPM_FOLDER_OPENED]);
		}
	}
	relayout();
}
