/***************************************************************************
                         SP Mail project : Simple Powerful Mail
                          CMailFolders.cpp  -  description
                             -------------------
    begin                : 23 June 2003
    copyright            : (C) 2000-2005 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();
   m_registry.topic("Accounts");
   int accountNumber = m_registry["account number"];
   for (int i = 0; i < accountNumber; i++) {
      m_registry.topic("Account " +int2string(i));

      string type = m_registry["type"].c_str();

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

         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();
}
