root/AccountView.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. IMPLEMENT_DYNCREATE
  2. CAccountView
  3. DoDataExchange
  4. BEGIN_MESSAGE_MAP
  5. Dump
  6. OnSize
  7. OnInitialUpdate
  8. OnSelchangeAccount
  9. PreCreateWindow
  10. PreTranslateMessage
  11. RenameAccount
  12. CreateNewAccount
  13. GetAccountNum
  14. SetStatusBarProgress
  15. SetStatusBarText
  16. GetCurrentAccountName
  17. IsFetching
  18. SetFetchStatus
  19. GetButtonStatus
  20. SetButtonStatus
  21. GetFirstAccountName

/*
 * Copyright (C) 2002-2003 chik, s.hiranaka
 * For license terms, see the file COPYING in this directory.
 */

// AccountView.cpp
//

#include "stdafx.h"
#include "pochy.h"
#include "AccountView.h"
#include "MainFrm.h"
#include "lib.h"
#include "direct.h" // for _mkdir
#include "SettingAccountDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAccountView

IMPLEMENT_DYNCREATE(CAccountView, CFormView)

CAccountView::CAccountView()
        : CFormView(CAccountView::IDD)
{
        //{{AFX_DATA_INIT(CAccountView)
                // メモ: ClassWizard はこの位置にメンバの初期化処理を追加します
        //}}AFX_DATA_INIT
}

CAccountView::~CAccountView()
{
}

void CAccountView::DoDataExchange(CDataExchange* pDX)
{
        CFormView::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAccountView)
        DDX_Control(pDX, IDC_COMBO_ACCOUNT, m_combo_account);
        //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAccountView, CFormView)
        //{{AFX_MSG_MAP(CAccountView)
        ON_WM_SIZE()
        ON_CBN_SELCHANGE(IDC_COMBO_ACCOUNT, OnSelchangeAccount)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAccountView 診断

#ifdef _DEBUG
void CAccountView::AssertValid() const
{
        CFormView::AssertValid();
}

void CAccountView::Dump(CDumpContext& dc) const
{
        CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CAccountView メッセージ ハンドラ

void CAccountView::OnSize(UINT nType, int cx, int cy) 
{
        CFormView::OnSize(nType, cx, cy);

        CRect rect1;
        this->GetWindowRect(rect1);
        ScreenToClient(&rect1);

        CRect rect2;
        CWnd *wnd;
        wnd = this->GetDlgItem(IDC_COMBO_ACCOUNT);
        if(wnd != NULL){
                // adjust size of accountview.
                wnd->MoveWindow(rect1);
        }
}

void CAccountView::OnInitialUpdate() 
{
        CFormView::OnInitialUpdate();

        CFileFind ff;
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        BOOL b = ff.FindFile(app->m_app_path+"\\*.*");
        int index;
        CString message;
        ACCOUNT_STRUCT as;
        m_data.RemoveAll();

        while(b){
                b = ff.FindNextFile();
                if(ff.IsDirectory() && !ff.IsDots()){
                        if(g_is_there(app->m_app_path + "\\" + ff.GetFileName() + "\\account.ini")){
                                index = m_combo_account.AddString(ff.GetFileName());
                                switch(index){
                                case CB_ERR:
                                        message.Format("%sの情報取得中にエラーが発生しました", ff.GetFileName());
                                        AfxMessageBox(message);
                                        return;
                                case CB_ERRSPACE:
                                        message.Format("%dの情報取得中にメモリが足りなくなりました", ff.GetFileName());
                                        AfxMessageBox(message);
                                        return;
                                }
                                if(m_combo_account.SetItemData(index, index) == CB_ERR){
                                        message.Format("%sの情報取得中にエラーが発生しました", ff.GetFileName());
                                        AfxMessageBox(message);
                                        return;
                                }else{
                                        m_combo_account.SetItemData(index, index);
                                        as.account_name = ff.GetFileName();
                                        as.button_fetchmail = TRUE;
                                        as.button_setting = TRUE;
                                        as.button_retrieve = TRUE;
                                        as.status_pgbar_current = 0;
                                        as.status_pgbar_upper = 100;
                                        as.status_text = "";
                                        as.is_fetching = FALSE;
                                        m_data.Add(as);
                                }
                        }
                }
        }

        // if account does not exist, make new one.
        if(0 == m_combo_account.GetCount())
                CreateNewAccount("NewAccount0");

        // display default account.
        // need to clarify whether or not that account do exist.
        CString account = app->GetProfileString("DefaultMailBox", "BoxName");
        if(g_is_there(app->m_app_path+"\\"+account+"\\account.ini")){
                index = m_combo_account.FindStringExact(-1, account);
                m_combo_account.SetCurSel(index);
        }else{
                index = 0;
                m_combo_account.SetCurSel(index);
                m_combo_account.GetLBText(index, account);
        }
        m_index = index;
        m_account = account;

        // change document name to current mail box name.
    CString title;
    title.Format("%s - %s", account, "pochy");
        AfxGetApp()->m_pMainWnd->SetWindowText(title);
}

void CAccountView::OnSelchangeAccount()
{
        int index = m_combo_account.GetCurSel();
        CString account;
        m_combo_account.GetLBText(index, account);
        m_account = account;
        m_index = index;

        CWaitCursor wait_cursor; // turn cursor into hourglass.
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        CMainFrame *mf = (CMainFrame*)app->m_pMainWnd;

        CFolderView *fv = mf->m_pTreeV;
        CSummaryView *sv = mf->m_pListV;
        CTextView *tv = mf->m_pTextV;

        fv->ConstructTree(account);

        // if selected account is under popping, current status of popping is displaied on statusbar.
        mf->SetStatusBarProgress(m_data[GetAccountNum(account)].status_pgbar_current, m_data[GetAccountNum(account)].status_pgbar_upper);
        mf->SetStatusBarText(0, m_data[GetAccountNum(account)].status_text);

        // reply、transfer、header、gpg button at toolbar is invalidated.
        mf->m_button_reply = FALSE;
        mf->m_button_transfer = FALSE;
        mf->m_button_info = FALSE;
        mf->m_button_gpg_dec = FALSE;

        // if multipartview is showing, hide.
        sv->Clear();
        tv->Clear();

        // change document name to current mail box name
    CString title;
    title.Format("%s - %s", account, "pochy");
        AfxGetApp()->m_pMainWnd->SetWindowText(title);

        // clear password cache
        app->m_pop_passphrase.Empty();
}

BOOL CAccountView::PreCreateWindow(CREATESTRUCT& cs)
{
        cs.style &= ~(WS_VSCROLL | WS_HSCROLL);
        return CFormView::PreCreateWindow(cs);
}

BOOL CAccountView::PreTranslateMessage(MSG* pMsg)
{
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        CMainFrame *mf = (CMainFrame*)app->m_pMainWnd;
        if(pMsg->message == WM_CHAR){
                if(mf->Char(pMsg->wParam))
                        return TRUE;
        }
        return CFormView::PreTranslateMessage(pMsg);
}

void CAccountView::RenameAccount(const char *org, const char *dst)
{
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        int index = m_combo_account.FindStringExact(-1, org);
        m_data.RemoveAt(index);
        m_combo_account.DeleteString(index);

        index = m_combo_account.AddString(dst); // after addstring adding something, data must be sorted.
        ACCOUNT_STRUCT as;
        as.account_name = dst;
        as.button_fetchmail = TRUE;
        as.button_setting = TRUE;
        as.button_retrieve = TRUE;
        as.status_pgbar_current = 0;
        as.status_pgbar_upper = 100;
        as.status_text = "";
        as.is_fetching = FALSE;
        m_data.Add(as);

        m_combo_account.SetCurSel(index);
        OnSelchangeAccount();
}

void CAccountView::CreateNewAccount(const char *name)
{
        CPochyApp *app = (CPochyApp*)AfxGetApp();

        _mkdir(app->m_app_path+"\\"+name);
        _mkdir(app->m_app_path+"\\"+name+"\\inbox");
        _mkdir(app->m_app_path+"\\"+name+"\\outbox");
        _mkdir(app->m_app_path+"\\"+name+"\\draft");
        _mkdir(app->m_app_path+"\\"+name+"\\trash");

        g_fcreate(app->m_app_path+"\\"+name+"\\list");
        g_fcreate(app->m_app_path+"\\"+name+"\\inbox\\list");
        g_fcreate(app->m_app_path+"\\"+name+"\\outbox\\list");
        g_fcreate(app->m_app_path+"\\"+name+"\\draft\\list");
        g_fcreate(app->m_app_path+"\\"+name+"\\trash\\list");
        g_fcreate(app->m_app_path+"\\"+name+"\\account.ini");

        CString message;
        ACCOUNT_STRUCT as;
        int index = m_combo_account.AddString(name);
        switch(index){
        case CB_ERR:
                message.Format("%sの情報取得中にエラーが発生しました", name);
                AfxMessageBox(message);
                return;
        case CB_ERRSPACE:
                message.Format("%dの情報取得中にメモリが足りなくなりました", name);
                AfxMessageBox(message);
                return;
        }
        if(m_combo_account.SetItemData(index, index) == CB_ERR){
                message.Format("%sの情報取得中にエラーが発生しました", name);
                AfxMessageBox(message);
                return;
        }else{
                as.account_name = name;
                as.button_fetchmail = TRUE;
                as.button_setting = TRUE;
                as.button_retrieve = TRUE;
                as.status_pgbar_current = 0;
                as.status_pgbar_upper = 100;
                as.status_text = "";
                as.is_fetching = FALSE;
                m_data.Add(as);
        }
        m_combo_account.SetCurSel(index);
        m_account = name;
        m_index = index;

//      CSettingAccountDlg sad;
//      sad.DoModal();
}

int CAccountView::GetAccountNum(CString account)
{
        for(int i=0; i<m_data.GetSize(); i++){
                if(account == m_data[i].account_name)
                        return i;
        }       
        return -1;
}

void CAccountView::SetStatusBarProgress(int current, int upper, CString account)
{
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        CMainFrame *mf = (CMainFrame*)app->m_pMainWnd;

        if(m_account == account){
                mf->SetStatusBarProgress(current, upper);
        }
        m_data[GetAccountNum(account)].status_pgbar_current = current;
        m_data[GetAccountNum(account)].status_pgbar_upper = upper;
}

void CAccountView::SetStatusBarText(LPCSTR text, CString account)
{
        CPochyApp* app = (CPochyApp*)AfxGetApp();
        CMainFrame *mf = (CMainFrame*)app->m_pMainWnd;

        if(m_account == account){
                mf->SetStatusBarText(0, text);
        }
        m_data[GetAccountNum(account)].status_text = text;
}

char* CAccountView::GetCurrentAccountName()
{
        return m_data[m_index].account_name.GetBuffer(0);
}

BOOL CAccountView::IsFetching(char *account_name)
{
        CString account = account_name;
        int index = GetAccountNum(account);
        return m_data[index].is_fetching;
}

void CAccountView::SetFetchStatus(char *account_name, BOOL status)
{
        CString account = account_name;
        int index = GetAccountNum(account);
        m_data[index].is_fetching = status;
}

BOOL CAccountView::GetButtonStatus(char *account, int id)
{
/*
        ID_DRAFT, TBSTYLE_BUTTON
        ID_FETCH_MAIL, TBSTYLE_BUTTON
        ID_REPLY, TBSTYLE_BUTTON
        ID_TRANSFER, TBSTYLE_BUTTON
        ID_ADDRESS, TBSTYLE_BUTTON
        ID_GPG, TBSTYLE_BUTTON
        ID_RETRIEVE, TBSTYLE_BUTTON
        ID_SETTING, TBSTYLE_BUTTON
        ID_ALL_HEADER, TBSTYLE_BUTTON
  */
        CString account_name = account;
        int index = this->GetAccountNum(account_name);
        switch(id){
        case ID_DRAFT:
                return TRUE;
        case ID_FETCH_MAIL:
                return m_data[index].button_fetchmail;
        case ID_REPLY:
                return TRUE;
        case ID_TRANSFER:
                return TRUE;
        case ID_ADDRESS:
                return TRUE;
        case ID_GPG:
                return TRUE;
        case ID_RETRIEVE:
                return m_data[index].button_retrieve;
        case ID_SETTING:
                return m_data[index].button_setting;
        case ID_INFO:
                return TRUE;
        default:
                return TRUE;
        }
}

void CAccountView::SetButtonStatus(char *account, int id, BOOL status)
{
        CString account_name = account;
        int index = this->GetAccountNum(account_name);

        switch(id){
        case ID_DRAFT:
                break;
        case ID_FETCH_MAIL:
                m_data[index].button_fetchmail = status;
                break;
        case ID_REPLY:
                break;
        case ID_TRANSFER:
                break;
        case ID_ADDRESS:
                break;
        case ID_GPG:
                break;
        case ID_RETRIEVE:
                m_data[index].button_retrieve = status;
                break;
        case ID_SETTING:
                m_data[index].button_setting = status;
        case ID_INFO:
                break;
//      case ID_FILTER:
//              m_data[index].button_filter = status;
        default:
                break;
        }
}

CString CAccountView::GetFirstAccountName()
{
        CString account;
        m_combo_account.GetLBText(0, account);

        return account;
}

/* [<][>][^][v][top][bottom][index][help] */