MainFrm.cpp
上传用户:weimei12
上传日期:2022-08-11
资源大小:185k
文件大小:14k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "SimpleMail.h"
- #include "MailMessage.h"
- #include "SmtpSendManger.h"
- #include "Pop3Message.h"
- #include "MailSaveMngr.h"
- #include "RecvProgressDlg.h"
- #include "PopReceiveManager.h"
- #include "MailManager.h"
- #include "LeftPaneView.h"
- #include "MainFrm.h"
- #include "SettingDlg.h"
- #include "ExpandEditCtrl.h"
- #include "SendDlg.h"
- #include "ListCtrlView.h"
- #include "EditCtrlView.h"
- #include "SplitterView.h"
- #include "RightPaneFrame.h"
- #include <Shlwapi.h>
- #include <windows.h>
- #include "MIMECode.h"
- #include "Base64.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- ON_WM_CREATE()
- ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
- ON_MESSAGE(WM_LIST_SELECTED, OnMailListSelected)
- ON_COMMAND(ID_MAIL_SETTING, &CMainFrame::OnMailSetting)
- ON_COMMAND(ID_MAIL_SEND, &CMainFrame::OnMailSend)
- ON_COMMAND(ID_MAIL_RECEIVE, &CMainFrame::OnMailReceive)
- ON_WM_CLOSE()
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_SHOWWINDOW()
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
- m_nOldMailCount = 0;
- m_bHaveSavedMail = FALSE;
- m_pProgDlg = NULL;
- m_pLeftPaneView = NULL;
- m_bIntialize = TRUE;
- memset(m_szSetFileName, 0, sizeof(m_szSetFileName));
- }
- CMainFrame::~CMainFrame()
- {
- DelIconFromTray();
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- ReadMailSetting();
- return 0;
- }
- BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
- CCreateContext* pContext)
- {
- if (!m_wndSplitter.CreateStatic(this, 1, 2))
- {
- TRACE0("Failed to create splitter windown");
- return FALSE;
- }
- // Get the client rect first for calculating left pane size
- CRect rect;
- GetClientRect(&rect);
- // create the left tree view first.
- if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftPaneView), CSize(rect.Width()/3, 0), pContext))
- {
- TRACE0("Failed to create left pane viewn");
- return FALSE;
- }
- // The right pane is a frame which and contain several different views.
- // The is can be set to active or non-active
- if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0), pContext))
- {
- TRACE0("Failed to create right pane framen");
- return FALSE;
- }
- m_pLeftPaneView = (CLeftPaneView*) m_wndSplitter.GetPane(0,0);
- m_pLeftPaneView->m_pRightPaneFrame = (CRightPaneFrame*) m_wndSplitter.GetPane(0, 1);
-
- // Set the left pane as the active view
- SetActiveView((CView*) m_wndSplitter.GetPane(0, 0));
- return TRUE;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.style &= ~FWS_ADDTOTITLE;
- return TRUE;
- }
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- // CMainFrame message handlers
- void CMainFrame::OnMailSetting()
- {
- // TODO: Add your command handler code here
- CSettingDlg dlg;
- dlg.m_strUserName = m_MailMgnr.GetUserName();
- dlg.m_strPassword = m_MailMgnr.GetPassword();
- dlg.m_strSmtpServer = m_MailMgnr.GetSmtpServer();
- dlg.m_strSendMail = m_MailMgnr.GetSendMail();
- dlg.m_strPopServer = m_MailMgnr.GetPopServer();
- dlg.m_bValidate = m_MailMgnr.GetValidate();
- if (IDOK == dlg.DoModal())
- {
- m_MailMgnr.SetUserName(dlg.m_strUserName);
- m_MailMgnr.SetPassword(dlg.m_strPassword);
- m_MailMgnr.SetSmtpServer(dlg.m_strSmtpServer);
- m_MailMgnr.SetSendMail(dlg.m_strSendMail);
- m_MailMgnr.SetPopServer(dlg.m_strPopServer);
- m_MailMgnr.SetValidate(dlg.m_bValidate);
- WritePrivateProfileString(_T("Setting"), _T("UserName"), dlg.m_strUserName, m_szSetFileName);
- WritePrivateProfileString(_T("Setting"), _T("Password"), dlg.m_strPassword, m_szSetFileName);
- WritePrivateProfileString(_T("Setting"), _T("SmtpServer"), dlg.m_strSmtpServer, m_szSetFileName);
- WritePrivateProfileString(_T("Setting"), _T("SendMail"), dlg.m_strSendMail, m_szSetFileName);
- WritePrivateProfileString(_T("Setting"), _T("Pop3Server"), dlg.m_strPopServer, m_szSetFileName);
- if (dlg.m_bValidate)
- {
- WritePrivateProfileString(_T("Setting"), _T("Validate"), _T("TRUE"), m_szSetFileName);
- }
- else
- {
- WritePrivateProfileString(_T("Setting"), _T("Validate"), _T("FALSE"), m_szSetFileName);
- }
- m_pLeftPaneView->UpdateRootName(dlg.m_strSendMail);
- }
- }
- void CMainFrame::OnMailSend()
- {
- // TODO: Add your command handler code here
- CSendDlg dlg;
- if (IDOK == dlg.DoModal())
- {
- if (m_MailMgnr.SendMailMessage(dlg.m_strRcps, dlg.m_strCCRcps,
- dlg.m_strBCCRcps, dlg.m_strTopic,
- dlg.m_strContent, dlg.m_arrAttachFiles) != HRESULT_SUCCESS)
- {
- AfxMessageBox(_T("发送邮件失败"));
- }
- }
- }
- void CMainFrame::OnMailReceive()
- {
- //Create the dialog to show the progress
- m_pProgDlg = new CRecvProgressDlg;
- m_pProgDlg->Create(IDD_DLG_PROGRESS);
- CRect rect;
- GetClientRect(&rect);
- ClientToScreen(&rect);
- m_pProgDlg->SetWindowPos(&CWnd::wndTop, rect.TopLeft().x + 250, rect.TopLeft().y + 100, 450, 180, SWP_SHOWWINDOW);
- m_pProgDlg->UpdateProgress(START_RECEIVING);
- //clear the history information relevant to mails
- CMailSaveMngr* pSaveMngr = NULL;
- pSaveMngr = m_MailMgnr.GetSaveMngr();
- UINT i = 0;
- for (; i < (UINT)m_arrMsgPos.GetSize(); i++)
- {
- pSaveMngr->DelMail(m_arrMsgPos[i]);
- }
- m_arrMsgPos.RemoveAll();
- m_arrMsgSize.RemoveAll();
- m_arrTopMail.RemoveAll();
- //receive mails
- if (m_MailMgnr.RecMail(m_pProgDlg, m_arrMsgPos, m_arrMsgSize) != HRESULT_SUCCESS)
- {
- m_pProgDlg->UpdateProgress(RECEIVE_ERRORS);
- return;
- }
- //display
- ReadAndDisplayMails(RECEIVE_MAILS);
- m_pProgDlg->UpdateProgress(RECEIVED_SUCCESS);
-
- }
- void CMainFrame::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- CFile file("SimpleMail.cfg", CFile::modeCreate | CFile::modeWrite);
- CArchive ar(&file, CArchive::store);
- ar << (int)m_arrMsgPos.GetSize();
- UINT i = 0;
- for (; i < (UINT)m_arrMsgPos.GetSize(); i++)
- {
- ar << m_arrMsgPos[i].nBlockIndex;
- ar << m_arrMsgSize[i];
- }
- CFrameWnd::OnClose();
- }
- void CMainFrame::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- }
- else
- {
- }
- }
- void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
- {
- // TODO: Add your message handler code here and/or call default
- if (SC_MINIMIZE == nID)
- {
- ShowWindow(SW_HIDE);
- AddIconToTray();
- return;
- }
- CFrameWnd::OnSysCommand(nID, lParam);
- }
- LRESULT CMainFrame::OnTrayNotification(WPARAM wParam,LPARAM lParam)
- {
- if (wParam != IDI_ICON)
- {
- return 0L;
- }
- if (LOWORD(lParam) == WM_LBUTTONDBLCLK)
- {
- this->OnTrayOpen();
- }
- return 1;
- }
- LRESULT CMainFrame::OnMailListSelected(WPARAM wParam, LPARAM lParam)
- {
- CMailSaveMngr* pSaveMngr = NULL;
- pSaveMngr = m_MailMgnr.GetSaveMngr();
- UINT nCount = (UINT)m_arrMsgSize.GetSize();
- TCHAR * pszMail = NULL;
- pszMail = new TCHAR[m_arrMsgSize[nCount - wParam - 1]];
- int n = m_arrMsgSize[nCount - wParam - 1];
- pSaveMngr->LoadMail(m_arrMsgPos[nCount - wParam - 1], pszMail, m_arrMsgSize[nCount - wParam - 1]);
- CPop3Message MailMess;
- MailMess.SetMailContent(pszMail);
- CEditCtrlView* pEditView = NULL;
- pEditView = m_pLeftPaneView->m_pRightPaneFrame->m_pSplitterView->m_pEditCtrlView;
- pEditView->DisplayText(MailMess.GetMailContent());
- return 1;
- }
- ///<summary>
- /// Add the icon to the system tray
- ///</summary>
- void CMainFrame::AddIconToTray()
- {
- NOTIFYICONDATA tnd;
- tnd.cbSize=sizeof(NOTIFYICONDATA);
- tnd.hWnd = this->m_hWnd;
- tnd.uID = IDI_ICON;
- tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
- tnd.uCallbackMessage = WM_ICON_NOTIFY;
- tnd.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_ICON));
- _tcscpy_s(tnd.szTip, 128, "SimpleMail");
- Shell_NotifyIcon(NIM_ADD, &tnd);
- }
- ///<summary>
- /// Delete the icon from the system tray
- ///</summary>
- void CMainFrame::DelIconFromTray()
- {
- NOTIFYICONDATA tnid;
- tnid.cbSize = sizeof(NOTIFYICONDATA);
- tnid.hWnd = this->m_hWnd;
- tnid.uID = IDI_ICON;
- tnid.uFlags = 0;
- Shell_NotifyIcon(NIM_DELETE, &tnid);
- }
- ///<summary>
- /// Open the program when the user click the icon in the tray
- ///</summary>
- void CMainFrame::OnTrayOpen()
- {
- AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOWNORMAL);
- this->SetForegroundWindow();
- DelIconFromTray();
- }
- ///<summary>
- /// Read the setting from the disk,such as the user name, the smtp server
- ///</summary>
- void CMainFrame::ReadMailSetting()
- {
- GetModuleFileName(NULL, m_szSetFileName, _MAX_PATH);
- PathRemoveFileSpec(m_szSetFileName);
- _tcscat_s(m_szSetFileName, _MAX_PATH, SETTING_FILE_NAME);
- if (PathFileExists(m_szSetFileName))
- {
- TCHAR szTemp[SETTING_MAX_LEN] = {0};
- GetPrivateProfileString(_T("Setting"), _T("UserName"), _T("Test"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- m_MailMgnr.SetUserName(szTemp);
- GetPrivateProfileString(_T("Setting"), _T("Password"), _T("Test"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- m_MailMgnr.SetPassword(szTemp);
- GetPrivateProfileString(_T("Setting"), _T("SmtpServer"), _T("smtp.163.com"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- m_MailMgnr.SetSmtpServer(szTemp);
- GetPrivateProfileString(_T("Setting"), _T("SendMail"), _T("Test@163.com"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- m_MailMgnr.SetSendMail(szTemp);
- GetPrivateProfileString(_T("Setting"), _T("Pop3Server"), _T("pop3.163.com"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- m_MailMgnr.SetPopServer(szTemp);
- GetPrivateProfileString(_T("Setting"), _T("Validate"), _T("TRUE"), szTemp, SETTING_MAX_LEN, m_szSetFileName);
- if ('T' == szTemp[0])
- {
- m_MailMgnr.SetValidate(TRUE);
- }
- else
- {
- m_MailMgnr.SetValidate(FALSE);
- }
- }
- else
- {
- m_MailMgnr.SetUserName(_T("Test"));
- m_MailMgnr.SetPassword(_T("Test"));
- m_MailMgnr.SetSmtpServer(_T("smtp.163.com"));
- m_MailMgnr.SetSendMail(_T("Test@163.com"));
- m_MailMgnr.SetPopServer(_T("pop3.163.com"));
- }
- }
- ///<summary>
- /// Read the former mails saved on the disk
- ///</summary>
- void CMainFrame::ReadFormerMails()
- {
- CMailSaveMngr* pSaveMngr = NULL;
- pSaveMngr = m_MailMgnr.GetSaveMngr();
- m_bHaveSavedMail = pSaveMngr->InitMailTmpFile();
- if (m_bHaveSavedMail)
- {// if there are any mails saved on the disk, load the info from the config file
-
- TCHAR szCfg[_MAX_PATH] = {0};
- GetModuleFileName(NULL, szCfg, _MAX_PATH);
- PathRemoveFileSpec(szCfg);
- _tcscat_s(szCfg, _MAX_PATH, MAIL_CONFIG_FILE);
- //make sure that the config file exist, in case we open it before it creates
- if (!PathFileExists(szCfg))
- {
- return;
- }
- CFile file(szCfg, CFile::modeRead);
- CArchive ar(&file, CArchive::load);
- UINT nSize = 0;
- ar >> nSize;
- m_nOldMailCount = nSize;
- UINT i = 0;
- for (; i < nSize; i++)
- {
- int nBlockIndex = 0;
- ar >> nBlockIndex;
- MAILPOS tmpPos;
- tmpPos.nBlockIndex = nBlockIndex;
- m_arrMsgPos.Add(tmpPos);
- int nSize = 0;
- ar >> nSize;
- m_arrMsgSize.Add(nSize);
- }
- //let the former mails display on the interface
- ReadAndDisplayMails(INITIAL_PROGRAM);
- }
- }
- ///<summary>
- /// Read mails fromdatabase and display
- ///</summary>
- void CMainFrame::ReadAndDisplayMails(const UINT nState)
- {
- //Read from the database
- UINT nCount = 0;
- nCount = (UINT)m_arrMsgPos.GetCount();
- CMailSaveMngr* pSaveMngr = NULL;
- pSaveMngr = m_MailMgnr.GetSaveMngr();
- TCHAR* pMail = NULL;
- pMail = new TCHAR[MAIL_TOP_TO_READ];
- UINT i = 0;
- for (; i < nCount; i++)
- {
- pSaveMngr->LoadMail(m_arrMsgPos[i], pMail, MAIL_TOP_TO_READ);
- CPop3Message MailMessage;
- MailMessage.SetTopMailContent(pMail);
- m_arrTopMail.Add(MailMessage);
- }
- delete[] pMail;
- //update the display in the list view
- CListCtrlView* pListView = NULL;
- pListView = m_pLeftPaneView->m_pRightPaneFrame->m_pSplitterView->m_pListCtrlView;
- pListView->m_listCtrl.DeleteAllItems();
- CString strFrom, strSubject, strDate, strSize;
- i = 0;
- for (; i < nCount; i++)
- {
- strFrom = m_arrTopMail[i].GetMailFrom();
- strSubject = m_arrTopMail[i].GetMailSubject();
- strDate = m_arrTopMail[i].GetMailDate();
- strSize.Empty();
- strSize.Format(_T("%6.2f K"), m_arrMsgSize[i] / 1024.0);
- pListView->InsertRowItem(strFrom, strSubject, strDate, strSize);
- }
- //update the display in the tree view
- if (RECEIVE_MAILS == nState)
- {
- //There are definitely many policies to determine the amount of new mails.
- //However, I just use this simple criteria
- UINT nNewMailCount = 0;
- if (nCount > m_nOldMailCount)
- {
- nNewMailCount = nCount - m_nOldMailCount;
- }
- else if (nCount < m_nOldMailCount)
- {
- nNewMailCount = nCount;
- }
- if (0 != nNewMailCount)
- {
- CString strRootName;
- strRootName.Format("%s(%d)", m_MailMgnr.GetSendMail(), nNewMailCount);
- m_pLeftPaneView->UpdateRootName(strRootName);
- }
- }
- m_nOldMailCount = nCount;
- }
- void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- CFrameWnd::OnShowWindow(bShow, nStatus);
- if (m_bIntialize)
- {
- ReadFormerMails();
- CString strSendName;
- strSendName = m_MailMgnr.GetSendMail();
- if (!strSendName.IsEmpty())
- {
- m_pLeftPaneView->UpdateRootName(strSendName);
- }
- m_bIntialize = FALSE;
- }
- }