www.pudn.com > MiniFox.rar > MiniFox.cpp


// MiniFox.cpp : Defines the class behaviors for the application. 
// 
 
#include "stdafx.h" 
#include "MiniFox.h" 
 
#include "MainFrm.h" 
#include "MiniFoxDoc.h" 
#include "MiniFoxView.h" 
#include "AccTreeData.h" 
 
#include "Wizard.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// Global data and function: 
CTypedPtrList< CObList, CAccProfile* > gAccList; 
bool CreateAcc( CFile& rAccFile, CWizard::AccInfo& rAccInfo ); 
 
///////////////////////////////////////////////////////////////////////////// 
// CMiniFoxApp 
 
BEGIN_MESSAGE_MAP(CMiniFoxApp, CWinApp) 
	//{{AFX_MSG_MAP(CMiniFoxApp) 
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
	// Standard file based document commands 
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) 
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) 
	// Standard print setup command 
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMiniFoxApp construction 
 
CMiniFoxApp::CMiniFoxApp() 
{ 
	// TODO: add construction code here, 
	// Place all significant initialization in InitInstance 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only CMiniFoxApp object 
 
CMiniFoxApp theApp; 
 
///////////////////////////////////////////////////////////////////////////// 
// CMiniFoxApp initialization 
 
BOOL CMiniFoxApp::InitInstance() 
{ 
	AfxEnableControlContainer(); 
 
	// Standard initialization 
	// If you are not using these features and wish to reduce the size 
	//  of your final executable, you should remove from the following 
	//  the specific initialization routines you do not need. 
 
#ifdef _AFXDLL 
	Enable3dControls();			// Call this when using MFC in a shared DLL 
#else 
	Enable3dControlsStatic();	// Call this when linking to MFC statically 
#endif 
 
	// Change the registry key under which our settings are stored. 
	// TODO: You should modify this string to be something appropriate 
	// such as the name of your company or organization. 
	SetRegistryKey(_T("Local AppWizard-Generated Applications")); 
 
	LoadStdProfileSettings();  // Load standard INI file options (including MRU) 
 
	// Register the application's document templates.  Document templates 
	//  serve as the connection between documents, frame windows and views. 
 
	CSingleDocTemplate* pDocTemplate; 
	pDocTemplate = new CSingleDocTemplate( 
		IDR_MAINFRAME, 
		RUNTIME_CLASS(CMiniFoxDoc), 
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window 
		RUNTIME_CLASS(CMiniFoxView)); 
	AddDocTemplate(pDocTemplate); 
 
/* ===================================================================== 
	The code below is to examin whether the account.cfg file has 
	been created, that's whether there exists at least one account. 
	If no account exists, a new account should be created. This 
	application will help user create an account by wizard. 
   =====================================================================*/ 
 
// Firstly, check the existence of the file named account.cfg. 
	CFileException ex; 
	CFile accountsFile; 
	if ( !accountsFile.Open(ACCCFGFILENAME, CFile::modeReadWrite, &ex) && 
		ex.m_cause == CFileException::fileNotFound ) 
	// The account configure file does not exist. Thus create it and create an account: 
	{ 
		CWizard wizard; 
		int nRespond = wizard.DoModal(); 
		if( nRespond == IDOK ) 
		{ 
			if( !accountsFile.Open( ACCCFGFILENAME, CFile::modeCreate | 
				CFile::modeReadWrite, &ex) ) // Not successfully created.  
			{ 
				TRACE0( "Cannot create file:Account.cfg" ); 
				return false; 
			} 
			// Since "Account.cfg" file has been successfully created, store some 
				// data about the new account into this file: 
			if( !CreateAcc( accountsFile, wizard.accInfo ) ) 
			{ 
				AfxMessageBox( "The account hasn't been successfully created.", 
					MB_OK | MB_APPLMODAL | MB_ICONINFORMATION ); 
			} 
		} 
	} 
	else	// Configure file already exists. 
	{ 
		CArchive ar( &accountsFile, CArchive::load ); 
		gAccList.Serialize( ar ); 
		ar.Close(); 
	} 
	// Check out the configure file: 
	accountsFile.Close(); // Every time creating a new account, this file is to be opened. 
 
/*=======================================================================================*/ 
	// Parse command line for standard shell commands, DDE, file open 
	CCommandLineInfo cmdInfo; 
	ParseCommandLine(cmdInfo); 
 
	// Dispatch commands specified on the command line 
	if (!ProcessShellCommand(cmdInfo)) 
		return FALSE; 
 
	// The one and only window has been initialized, so show and update it. 
	m_pMainWnd->ShowWindow(SW_SHOW); 
	m_pMainWnd->UpdateWindow(); 
 
	return TRUE; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
// App command to run the dialog 
void CMiniFoxApp::OnAppAbout() 
{ 
	CAboutDlg aboutDlg; 
	aboutDlg.DoModal(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMiniFoxApp message handlers 
 
 
///////////////////////////////////////////////////////////////////////////// 
// Global function definition: 
bool CreateAcc( CFile& rAccFile, CWizard::AccInfo& rAccInfo ) 
{ 
// Create an object to represent the new account, and initialize its 
	// data members with default values: 
	CAccProfile* pAccInfo = new CAccProfile; 
	pAccInfo->m_nNewBoxCount	= 4;	// common boxes 
	pAccInfo->m_strAccName		= rAccInfo.m_strAccName; 
	if( ( pAccInfo->m_strMailPath = rAccInfo.m_strMailPath ) == 
		_T( "<默认>" ) ) 
		pAccInfo->m_strMailPath = DEFMAILPATH;	// Default mail path. 
	if ( pAccInfo->m_strMailPath.Right ( 1 ) != _T ("\\")) 
		pAccInfo->m_strMailPath += _T ( "\\" ); 
	 
// Create files: 
	CFileException ex; 
	CFile mailFile; 
	CString mailFilePath = pAccInfo->m_strMailPath; 
	CString strTmpFileName; 
	// "Account.stg" file: 
	strTmpFileName = mailFilePath + ACCSTGFILENAME; 
	if ( !mailFile.Open( strTmpFileName, CFile::modeCreate | 
		CFile::modeReadWrite, &ex ) ) 
	{ 
		delete pAccInfo; 
		TRACE( "Cannot create file: %s", strTmpFileName ); 
		return false; 
	} 
	// Store some data into "Account.stg" file: 
	// TODO:.................................... 
	mailFile.Close(); 
 
	// "*.IND" & "*.BOX" file: 
	CString mailFileNameArray[ ] = { 
		ININDFILENAME, 
		INBOXFILENAME, 
 
		OUTINDFILENAME, 
		OUTBOXFILENAME, 
 
		SENTINDFILENAME, 
		SENTBOXFILENAME, 
 
		TRASHINDFILENAME, 
		TRASHBOXFILENAME 
	}; 
 
	int nFilesCount = sizeof( mailFileNameArray ) / sizeof ( CString ); 
	for ( int i=0; im_strBoxName	= strBoxName; 
		pBox->m_strFileName	= strFileName; 
 
		// Add this box object into the child-box list of the account object: 
		pAccInfo->m_boxList.AddTail( pBox ); 
	} 
	gAccList.AddTail( pAccInfo ); 
 
	CArchive ar( &rAccFile, CArchive::store ); 
	gAccList.Serialize( ar ); 
	ar.Close(); 
	// The file should be closed outside of this scope. 
	return true; 
}