www.pudn.com > doc2pdf-0_7_1.rar > doc2pdfDlg.cpp


// doc2pdfDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "doc2pdf.h" 
#include "doc2pdfDlg.h" 
#include "doc2pdfSetupDlg.h" 
 
#include "doc2pdf_pop3.h" 
#include "doc2pdf_converter.h" 
#include "doc2pdf_smtp.h" 
#include "doc2pdf_log.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define	DOC2PDF_MAIL_TIMER_ID		0xbeeff00d 
#define	DOC2PDF_DIRECTORY_TIMER_ID	0xbeeffeed 
#define DOC2PDF_MAIL_TIMER_INTERVAL	300000			// 5 minutes 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}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) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDoc2pdfDlg dialog 
 
CDoc2pdfDlg::CDoc2pdfDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDoc2pdfDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDoc2pdfDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	m_MailProcessThread = NULL; 
	m_DirProcessThread = NULL; 
} 
 
void CDoc2pdfDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDoc2pdfDlg) 
	DDX_Control(pDX, IDC_STATUS_STATIC, m_Status); 
	DDX_Control(pDX, IDC_STOP_BUTTON, m_StopButton); 
	DDX_Control(pDX, IDC_START_BUTTON, m_StartButton); 
	DDX_Control(pDX, IDC_SETUP_BUTTON, m_SetupButton); 
	DDX_Control(pDX, ID_QUIT_BUTTON, m_QuitButton); 
	DDX_Control(pDX, IDC_LOGLIST, m_LogList); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDoc2pdfDlg, CDialog) 
	//{{AFX_MSG_MAP(CDoc2pdfDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton) 
	ON_BN_CLICKED(ID_QUIT_BUTTON, OnQuitButton) 
	ON_BN_CLICKED(IDC_SETUP_BUTTON, OnSetupButton) 
	ON_BN_CLICKED(IDC_STOP_BUTTON, OnStopButton) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDoc2pdfDlg message handlers 
 
BOOL CDoc2pdfDlg::OnInitDialog() 
{ 
	Doc2pdfRegistry	registry; 
	CString			logfilename; 
	CString			title; 
	 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	registry.GetLogFilename(logfilename); 
	if(logfilename.GetLength() == 0) 
	{ 
		MessageBox("Doc2pdf has not been configured.  Please click OK and fill all the fields of the Doc2pdf Setup dialog.","Doc2pdf Not Configured",MB_ICONINFORMATION); 
		OnSetupButton(); 
	} 
 
	title.Format("Doc2pdf (v%s)",DOC2PDF_VERSION); 
	SetWindowText(title); 
	 
	theLog.Init(&m_LogList,logfilename); 
	 
	if(stricmp(theApp.m_lpCmdLine,"/start") == 0) 
	{ 
		OnStartButton();		 
	} 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CDoc2pdfDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CDoc2pdfDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
HCURSOR CDoc2pdfDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CDoc2pdfDlg::OnStartButton()  
{ 
	Doc2pdfRegistry	registry; 
	DWORD			mailInterval; 
	DWORD			dirInterval; 
	int numMailboxes = registry.GetAllMailboxIds(NULL); 
	int numDirectories = registry.GetAllDirectoryIds(NULL); 
		 
	if(numMailboxes <= 0 && numDirectories <= 0) 
	{ 
		MessageBox("Doc2pdf has not been configured.  Please click OK and fill all the fields of the Doc2pdf Setup dialog.","Doc2pdf Not Configured",MB_ICONINFORMATION); 
		OnSetupButton();	 
	} 
	else 
	{ 
		if ( numDirectories > 0 ){ 
			registry.GetDirectoryPollInterval(dirInterval); 
			dirInterval *= 1000; 
 
			if(CDialog::SetTimer(DOC2PDF_DIRECTORY_TIMER_ID,dirInterval,NULL)) 
			{ 
				m_StopButton.EnableWindow(TRUE); 
				m_StartButton.EnableWindow(FALSE); 
				m_SetupButton.EnableWindow(FALSE); 
				m_Status.SetWindowText("Idle ..."); 
				OnTimer(DOC2PDF_DIRECTORY_TIMER_ID); 
			} 
			else 
			{ 
				MessageBox("Could not create timer","Timer Creation Error", MB_ICONERROR);; 
				return; 
			}	 
 
		} 
		if ( numMailboxes > 0 ){ 
			registry.GetMailboxPollInterval(mailInterval); 
			mailInterval *= 60000; 
 
			if(CDialog::SetTimer(DOC2PDF_MAIL_TIMER_ID,mailInterval,NULL)) 
			{ 
				m_StopButton.EnableWindow(TRUE); 
				m_StartButton.EnableWindow(FALSE); 
				m_SetupButton.EnableWindow(FALSE); 
				m_Status.SetWindowText("Idle ..."); 
				OnTimer(DOC2PDF_MAIL_TIMER_ID); 
			} 
			else 
			{ 
				MessageBox("Could not create timer","Timer Creation Error", MB_ICONERROR);; 
				return; 
			}	 
		} 
	} 
} 
 
 
void CDoc2pdfDlg::OnStopButton()  
{ 
	// Stop the timer 
	CDialog::KillTimer(DOC2PDF_MAIL_TIMER_ID); 
	CDialog::KillTimer(DOC2PDF_DIRECTORY_TIMER_ID); 
	 
	m_StopButton.EnableWindow(FALSE); 
	m_StartButton.EnableWindow(TRUE);	 
	m_SetupButton.EnableWindow(TRUE); 
	m_Status.SetWindowText("Stopped ..."); 
} 
 
 
void CDoc2pdfDlg::OnQuitButton()  
{ 
	OnStopButton(); 
	CDialog::OnOK(); 
} 
 
DWORD __stdcall DirProcessThread(void* pthis) 
{ 
	Doc2pdfRegistry		registry; 
	Doc2pdfConverter	converter; 
 
	Doc2pdfDirectoryInfo	dirinfo; 
	CStringList			directories; 
	POSITION			pos; 
	CString				spooldir; 
	CString				ghostscriptdir; 
	CString				output; 
 
	registry.GetSpoolDir(spooldir); 
	registry.GetGhostscriptDir(ghostscriptdir); 
	registry.GetAllDirectoryIds(&directories);	 
	pos = directories.GetHeadPosition(); 
	//for each directory 
	while(pos) 
	{ 
		registry.GetDirectoryInfo(directories.GetAt(pos),dirinfo); 
		 
		converter.Init(ghostscriptdir); 
 
		WIN32_FIND_DATA wfd; 
		HANDLE h = FindFirstFile( dirinfo.m_Sourcedir + "*", &wfd ); 
		if ( h != INVALID_HANDLE_VALUE ){ 
			do{ 
				CString filename = wfd.cFileName; 
				filename.MakeLower(); 
				if ( !(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
					filename.Right(4) != ".pdf" && 
					(wfd.nFileSizeHigh > 0 || wfd.nFileSizeLow > 0) && 
					!(wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) )	{ 
					//do something with the file if appropriate 
					Doc2pdfAttachment attachment; 
					attachment.m_Encoding = PLAINFILE; 
					attachment.m_Converted = FALSE; 
					attachment.m_Name = dirinfo.m_Sourcedir + filename; 
					attachment.m_Path = attachment.m_Name; 
					theLog.Log( attachment.m_Path ); 
					if( converter.ConvertAttachment( attachment, output ) == 0 ){ 
						//file has been converted						 
						theLog.Log( output ); 
 
						CString fn = attachment.m_Path; 
						CString basefn = attachment.m_Path.Right( attachment.m_Path.GetLength() - attachment.m_Path.ReverseFind('\\') - 1); 
						CString moveTo = dirinfo.m_Destdir + basefn; 
						CString ctext = "Moving file from " + fn + " to " + moveTo;						 
						theLog.Log( ctext ); 
						if ( CopyFile( fn, moveTo, false ) ){; 
							DeleteFile( fn ); 
						} 
						else { 
							theLog.Log( "Could not move PDF to destination directory" ); 
						} 
					} 
					else { 
						//file not converted for some reason - log? 
						theLog.Log( output ); 
					}					 
				} 
			}while( FindNextFile(h, &wfd) ); 
		} 
 
		directories.GetNext(pos); 
	} 
 
	CDoc2pdfDlg* pThis = (CDoc2pdfDlg*)pthis; 
	pThis->m_DirProcessThread = NULL; 
	pThis->m_Status.SetWindowText("Idle"); 
	pThis->m_StopButton.EnableWindow(TRUE); 
 
	return 0; 
} 
 
DWORD __stdcall MailProcessThread(void* pthis) 
{ 
	int					count; 
	Doc2pdfEmailList	emaillist; 
	Doc2pdfRegistry		registry; 
	Doc2pdfPop3Reader	reader; 
	Doc2pdfConverter	converter; 
	Doc2pdfSmtpSender	sender; 
	Doc2pdfMailboxInfo	mailboxinfo; 
	CStringList			mailboxes; 
	POSITION			pos; 
	CString				spooldir; 
	CString				ghostscriptdir; 
 
	registry.GetSpoolDir(spooldir); 
	registry.GetGhostscriptDir(ghostscriptdir); 
	registry.GetAllMailboxIds(&mailboxes);	 
	pos = mailboxes.GetHeadPosition(); 
	while(pos) 
	{ 
		registry.GetMailboxInfo(mailboxes.GetAt(pos),mailboxinfo); 
		 
		reader.Init(mailboxinfo.m_Pop3host, 
			        mailboxinfo.m_Pop3user, 
					mailboxinfo.m_Pop3passwd, 
					spooldir); 
		converter.Init(ghostscriptdir); 
		 
		sender.Init(mailboxinfo.m_Smtphost,mailboxinfo.m_Myaddress); 
 
		count = reader.ReadAllEmail(emaillist); 
		if(count) 
		{ 
			if(converter.ConvertAllEmail(emaillist)) 
			{ 
				sender.SendAllEmail(emaillist); 
			} 
 
			reader.DeleteAllEmail(count); 
		} 
		 
		emaillist.Empty(); 
 
		mailboxes.GetNext(pos); 
	} 
 
	CDoc2pdfDlg* pThis = (CDoc2pdfDlg*)pthis; 
	pThis->m_MailProcessThread = NULL; 
	pThis->m_Status.SetWindowText("Idle"); 
	pThis->m_StopButton.EnableWindow(TRUE); 
 
	return 0; 
} 
 
void CDoc2pdfDlg::OnTimer(UINT nIDEvent)  
{ 
	DWORD tid; 
	 
	if(nIDEvent == DOC2PDF_MAIL_TIMER_ID) 
	{		 
		while( m_DirProcessThread != NULL ){ 
			//wait for it to finish 
		} 
 
		if(m_MailProcessThread == NULL) 
		{ 
			m_Status.SetWindowText("Scanning mailboxes ..."); 
			m_MailProcessThread = CreateThread(NULL,0,MailProcessThread,this,0,&tid); 
			if(m_MailProcessThread == NULL) 
			{ 
				// Log here? 
			} 
			else 
			{ 
				m_Status.SetWindowText("Working ..."); 
				m_StopButton.EnableWindow(FALSE); 
 
			} 
		} 
		else 
		{ 
			// We're overloaded 
		} 
	} 
	else if(nIDEvent == DOC2PDF_DIRECTORY_TIMER_ID) 
	{		 
		if(m_MailProcessThread == NULL && m_DirProcessThread == NULL) 
		{ 
			m_Status.SetWindowText("Scanning directories ..."); 
			m_DirProcessThread = CreateThread(NULL,0,DirProcessThread,this,0,&tid); 
			if(m_DirProcessThread == NULL) 
			{ 
				// Log here? 
			} 
			else 
			{ 
				m_Status.SetWindowText("Working ..."); 
				m_StopButton.EnableWindow(FALSE); 
 
			} 
		} 
		else 
		{ 
			// We're overloaded 
		} 
	} 
	else 
	{ 
		CDialog::OnTimer(nIDEvent); 
	}	 
} 
 
void CDoc2pdfDlg::OnSetupButton()  
{ 
	CDoc2pdfSetupDlg setupdlg; 
	 
	setupdlg.DoModal(); 
}