root/pochy.cpp

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

DEFINITIONS

This source file includes following definitions.
  1. BEGIN_MESSAGE_MAP
  2. InitInstance
  3. CAboutDlg
  4. DoDataExchange
  5. BEGIN_MESSAGE_MAP
  6. CreateDraftFrame
  7. ExitInstance
  8. OnInitDialog

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

// pochy.cpp :
//

#include "stdafx.h"
#include "Pochy.h"
#include "MainFrm.h"
#include "PochyDoc.h"
#include "MailEditView.h"
#include "lib.h"
#include "Gpg.h"
#include "DraftFrame.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPochyApp

BEGIN_MESSAGE_MAP(CPochyApp, CWinApp)
        //{{AFX_MSG_MAP(CPochyApp)
        ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
        //}}AFX_MSG_MAP
        // 標準のファイル基本ドキュメント コマンド
        ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
        ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
        // 標準の印刷セットアップ コマンド
        ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPochyApp クラスの構築

CPochyApp::CPochyApp()
{
        CGpg gpg;
        m_gpg_enable = gpg.Check();
        m_gpg_passphrase.Empty();
        m_pop_passphrase.Empty();
        // toolbarのreply、transfer、header、gpgボタンを無効にする
}

/////////////////////////////////////////////////////////////////////////////
// 唯一の CPochyApp オブジェクト

CPochyApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CPochyApp クラスの初期化

BOOL CPochyApp::InitInstance()
{
        CString path;
        int point;

        // 二重起動の防止と既に起動されていた場合はforefroudにする
        HANDLE mutex = CreateMutex(NULL, TRUE, "GPG_Mailer_Pochy_!");
        if(!mutex)
                return FALSE;
        if(GetLastError() == ERROR_ALREADY_EXISTS){
                HWND hwnd = GetWindow(GetDesktopWindow(), GW_CHILD);
                while(hwnd){
                        if(GetProp(hwnd, "GPG_Mailer_Pochy_!")){
                                if(IsIconic(hwnd))
                                        ShowWindow(hwnd, SW_RESTORE);
                                SetForegroundWindow(GetLastActivePopup(hwnd));
                                if(strlen(this->m_lpCmdLine) > 0){
                                        char mailto[512];
                                        strcpy(mailto, this->m_lpCmdLine);
                                        COPYDATASTRUCT cds; 
                                        memset(&cds,0,sizeof(cds)); 
                                        cds.dwData = WM_MAILTO;
                                        cds.cbData = sizeof(mailto);
                                        cds.lpData = mailto;
                                        ::SendMessage(hwnd, WM_COPYDATA, NULL, (LPARAM)&cds);
                                }
                                return FALSE;
                        }
                        hwnd = GetWindow(hwnd, GW_HWNDNEXT);
                }
                return FALSE;
        }
        
        // 標準的な初期化処理
        // もしこれらの機能を使用せず、実行ファイルのサイズを小さく
        // したければ以下の特定の初期化ルーチンの中から不必要なもの
        // を削除してください。
//      if(!AfxSocketInit()){
//              AfxMessageBox("ソケットの初期化に失敗しました。");
//              return FALSE;
//      }

#ifdef _AFXDLL
        Enable3dControls();             // 共有 DLL の中で MFC を使用する場合にはここを呼び出してください。
#else
        Enable3dControlsStatic();       // MFC と静的にリンクしている場合にはここを呼び出してください。
#endif

        // 設定が保存される下のレジストリ キーを変更します。
        // TODO: この文字列を、会社名または所属など適切なものに
        // 変更してください。

        // 現在位置のフルパスを取得し、setting.iniへのパスを作成
        GetModuleFileName(NULL, path.GetBuffer(1024), 1024); // -> 1024よりも長いパスが返されると落ちるので要注意
        path.ReleaseBuffer();
        point = path.ReverseFind('\\');
        path = path.Left(point);
        m_app_path = path;
        path += "\\setting.ini";

        // if some file that is required for pochy is missing, create those file. 
        // you run pochy first time, this may be occured.
        if(!g_is_there(this->m_app_path+"\\address.csv")){
                g_fcreate(this->m_app_path+"\\address.csv");
        }

        // レジストリではなく、pochy.exeと同じフォルダ内のiniファイルにセッティングを保存
        free((void *)m_pszProfileName);
        m_pszProfileName = _tcsdup(path.GetBuffer(0));  //はまった
        LoadStdProfileSettings();  // 標準の INI ファイルのオプションをロードします (MRU を含む)

        // アプリケーション用のドキュメント テンプレートを登録します。ドキュメント テンプレート
        //  はドキュメント、フレーム ウィンドウとビューを結合するために機能します。
        pDoc1Template = new CSingleDocTemplate(
                IDR_MAINFRAME,
                RUNTIME_CLASS(CPochyDoc),
                RUNTIME_CLASS(CMainFrame),       // メイン SDI フレーム ウィンドウ
                RUNTIME_CLASS(CTextView));
        AddDocTemplate(pDoc1Template);

        // iniファイルの wndPlace Zoom=1 の場合ウィンドウを最大化して起動
        if(this->GetProfileInt("wndPlace", "Zoom", 1) == 1) m_nCmdShow = SW_MAXIMIZE;

        // DDE、file open など標準のシェル コマンドのコマンドラインを解析します。
        CCommandLineInfo cmdInfo;
        ParseCommandLine(cmdInfo);

        // always open as new file
        cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew;

        // コマンドラインでディスパッチ コマンドを指定します。
        if (!ProcessShellCommand(cmdInfo))
                return FALSE;

        // メイン ウィンドウが初期化されたので、表示と更新を行います。
        this->m_pMainWnd->ShowWindow(SW_SHOW);
        this->m_pMainWnd->UpdateWindow();

        // ウィンドウのプロパティリストにユニークな情報を追加しとく
        // 二重起動の際にこのウィンドウを探すのに使うから
        SetProp(this->m_pMainWnd->GetSafeHwnd(), "GPG_Mailer_Pochy_!", (HANDLE)1);

        CString mailto = this->m_lpCmdLine;
        if(!mailto.IsEmpty() && mailto.Find("mailto:") == 0){
                // getting default account name
                CString def_account = this->GetProfileString("DefaultMailBox", "BoxName");

                this->m_me.Initialize();

                // setting To
                if(mailto.Find("?", 7) != -1){ // mailto:hoge@hoge.com?subject=PGP%20Key&Body=Please%20send%20keyとか
                                // subject, body is not implemented.
                        this->m_me.SetTo(mailto.Mid(7, mailto.Find("?", 7)-7));
                }else{
                        this->m_me.SetTo(mailto.Mid(7));
                }

                if(!def_account.IsEmpty()){
                        // setting From
                        CString path = this->m_app_path+"\\"+def_account+"\\account.ini";
                        CString address;
                        GetPrivateProfileString("mailbox", "address", "", address.GetBuffer(BUF_LENGTH), BUF_LENGTH, path);
                        address.ReleaseBuffer();
                        this->m_me.SetFrom(address);

                        // setting message-id
                        this->m_me.SetMessageID(g_gen_msgid(address));

                        // getting signature.
                        CString signature;
                        g_file2cstring(this->m_app_path+"\\"+def_account+"\\signature", signature);
                        // set main text.
                        this->m_me.AddText(signature);
                        // draftframe
                        this->CreateDraftFrame(def_account);
                }else{
                        CString hostname;
                        char *tmp = hostname.GetBuffer(255);
                        DWORD size=255;
                        ::GetUserName(tmp, &size);
                        hostname.ReleaseBuffer();
                        this->m_me.SetFrom("");
                        this->m_me.AddText("");
                        this->m_me.SetMessageID(g_gen_msgid(hostname));
                        // draftframe
                        this->CreateDraftFrame(((CMainFrame *)this->m_pMainWnd)->m_pAcntV->GetFirstAccountName());
                }
        }
        return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// アプリケーションのバージョン情報で使われる CAboutDlg ダイアログ

class CAboutDlg : public CDialog
{
public:
        CAboutDlg();

// ダイアログ データ
        //{{AFX_DATA(CAboutDlg)
        enum { IDD = IDD_ABOUTBOX };
        CStatic m_copyright;
        CStatic m_version;
        //}}AFX_DATA

        // ClassWizard 仮想関数のオーバーライドを生成します。
        //{{AFX_VIRTUAL(CAboutDlg)
        protected:
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV のサポート
        //}}AFX_VIRTUAL

// インプリメンテーション
protected:
        //{{AFX_MSG(CAboutDlg)
        virtual BOOL OnInitDialog();
        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
        //{{AFX_DATA_INIT(CAboutDlg)
        //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CAboutDlg)
        DDX_Control(pDX, IDC_APPABOUT_COPY, m_copyright);
        DDX_Control(pDX, IDC_ABOUTBOX_VERSION, m_version);
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
        //{{AFX_MSG_MAP(CAboutDlg)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// ダイアログを実行するためのアプリケーション コマンド
void CPochyApp::OnAppAbout()
{
        CAboutDlg aboutDlg;
        aboutDlg.DoModal();
}

void CPochyApp::CreateDraftFrame(CString account)
{
/*      CSingleDocTemplate* pDoc2Template;
        pDoc2Template = new CSingleDocTemplate(
                IDR_MAINFRAME,
                RUNTIME_CLASS(CPochyDoc),
                RUNTIME_CLASS(CDraftFrame),
                RUNTIME_CLASS(CMailEditView));
        AddDocTemplate(pDoc2Template);

        ASSERT(pDoc2Template != NULL);
        ASSERT_KINDOF(CDocTemplate, pDoc2Template);

//      pTmpWnd = m_pMainWnd;
//      m_pMainWnd = NULL;
        m_pDraftWnd = new CDraftFrame;

        pDoc2Template->OpenDocumentFile(NULL, FALSE);

//      ASSERT_KINDOF(CDraftFrame, m_pMainWnd);
        ASSERT_KINDOF(CDraftFrame, this->m_pDraftWnd);
//      CDraftFrame *df = static_cast<CDraftFrame*>(m_pMainWnd);
        CDraftFrame *df = static_cast<CDraftFrame*>(this->m_pDraftWnd);
//      m_pMainWnd = pTmpWnd;
*/

        CDocument *doc = (CDocument*)((CFrameWnd *)AfxGetMainWnd())->GetActiveDocument();

        // create doc template
        CSingleDocTemplate DocTemplate(
                IDR_MAINFRAME,
                NULL,
                RUNTIME_CLASS(CDraftFrame),
                RUNTIME_CLASS(CMailEditView));

        // create draft frame
        CFrameWnd *fw = DocTemplate.CreateNewFrame(doc, NULL);
        if(fw == NULL){
                TRACE("cannot create draftframe");
                return;
        }

        CDraftFrame *df = static_cast<CDraftFrame*>(fw);

        if(this->GetProfileInt("DraftWndPlace", "Zoom", 0) == 1)
                df->ShowWindow(SW_MAXIMIZE);
        else
                df->ShowWindow(SW_SHOW);

        char title[512];
        if(account.GetLength() > 504){
                account = account.Left(504);
        }
        wsprintf(title, "%s - draft", account);
        COPYDATASTRUCT cds;
        memset(&cds,0,sizeof(cds));
        cds.dwData = WM_TITLE;
        cds.cbData = sizeof(title);
        cds.lpData = title;
        ::SendMessage(df->m_hWnd, WM_COPYDATA, NULL, (LPARAM)&cds);

        df->m_account = account;
        m_draft_array.Add(df);
        df->UpdateWindow();
}

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

int CPochyApp::ExitInstance() 
{       
//      if(m_pszProfileName){
//              delete ((void*)m_pszProfileName);
//              m_pszProfileName = NULL;
//      }

        // プロパティリストからエントリを削除する
        RemoveProp(m_pMainWnd->GetSafeHwnd(), "GPG_Mailer_Pochy_!");

        return CWinApp::ExitInstance();
}

BOOL CAboutDlg::OnInitDialog() 
{
        CDialog::OnInitDialog();
        
        // TODO: この位置に初期化の補足処理を追加してください
        this->m_version.SetWindowText(VERSION);
        this->m_copyright.SetWindowText(COPYRIGHT);
        return TRUE;  // コントロールにフォーカスを設定しないとき、戻り値は TRUE となります
                      // 例外: OCX プロパティ ページの戻り値は FALSE となります
}

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