⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 非常好用的VC++源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "afxinet.h"
#include "Toller.h"

#include "MainFrm.h"
#include "Splash.h"

#include "sysoptionpage.h"
#include "ftpoptionpage.h"
#include "pollingpage.h"

#include "userlistdlg.h"

#include "tollerinfo.h"
#include "tolldef.h"

#include "reportformatdlg.h"

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


#define ID_POLLTIMER 1000
#define MMSOFHOUR	 3600000

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_DATE, OnUpdateDate)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIME, OnUpdateTime)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_OPTION, OnOption)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	ON_COMMAND(ID_SPECIAL_USER, OnSpecialUser)
	ON_WM_TIMER()
	ON_COMMAND(ID_TEST_COLLECT, OnTestCollect)
	ON_COMMAND(ID_TEST_UPDATEDATABASE, OnTestUpdatedatabase)
	ON_COMMAND(ID_FTP_CONNECT, OnFtpConnect)
	ON_COMMAND(ID_DEBUG_QUERY, OnDebugQuery)
	ON_COMMAND(ID_SYSTEM_REPORT, OnSystemReport)
	ON_COMMAND(ID_DNLOAD_LOGFILE, OnDnloadLogfile)
	ON_COMMAND(ID_MANUAL_UPDATE, OnManualUpdate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	sLogFileName=_T("c:\\toller\\proxylog\\access");
	sDatabaseName=_T("c:\\toller\\database\\toller.dbf");

	nUpdateAtHour=5;
	sLastDate="11/27/1997";

	m_nUpdateLocalLogWhen=0;//on Sunday update local log.

	m_nTollFromDate=1;//on 1st of each month,we update the toller database.
	
	// FTP-specific initialization follows...
	m_sUserName		 =_T("");	
	m_sPassword		 =_T("");

	m_pFtpConnection = NULL;
	m_pInetSession	 = NULL;

	m_nCostPerKB	 = 20;
}

//toller cleanup work
CMainFrame::~CMainFrame()
{
	FreeTollerList();
	SpecialUserList.RemoveAll();

	// clean up any objets that are still lying around

	if (m_pFtpConnection != NULL)
	{
		m_pFtpConnection->Close();
		delete m_pFtpConnection;
	}

	if (m_pInetSession != NULL)
	{
		m_pInetSession->Close();
		delete m_pInetSession;
	}
}

//
//Draw the status line with specified text string
//
void CMainFrame::StatusText(CString aString)
{
	SetMessageText((LPCTSTR)aString);

	CWnd* pBarWnd = GetMessageBar();
	if (pBarWnd != NULL)
		pBarWnd->UpdateWindow();

}

void CMainFrame::LoadSetting()
{
	CWinApp* pApp=AfxGetApp();

	sLogFileFTPAddr=pApp->GetProfileString("System","LogFileFTPAddr","");
	sFTPServerName=pApp->GetProfileString("System","FTPServerName","proxy.njupt.edu.cn");

	m_sUserName=pApp->GetProfileString("System","FTPUserName","");	
	m_sPassword=pApp->GetProfileString("System","FTPPassword","");

	//When will the database be updated?
	nUpdateAtHour=pApp->GetProfileInt("System","UpdateAtHour",5);
	sLastDate=pApp->GetProfileString("System","LastDate","11/27/1997");

	//default is 0 that means Sunday
	m_nUpdateLocalLogWhen=pApp->GetProfileInt("System","UpdateLocalLogWhen",0);
	m_nTollFromDate=pApp->GetProfileInt("System","TollFromDate",1);
	m_nCostPerKB   =pApp->GetProfileInt("System","CostPerKB",20);

	nServerUpdateLogAt=pApp->GetProfileInt("System","ServerUpdateLogAt",3);
}

void CMainFrame::LoadTollerDatabase()
{
	tollerList.RemoveAll();
	
	sDatabaseName="c:\\toller\\database\\toller.dbf";

	CFile file;
	if(!file.Open(sDatabaseName,CFile::modeRead))
	{
		MessageBeep(0);
		return;
	}

	TOLLERINFO ti;
	UINT nBytesRead;

	while(TRUE)
	{
		nBytesRead=file.Read(&ti,sizeof(TOLLERINFO));
		if(nBytesRead==0)break;//reach the end of the file
		tollerList.AddTail(new CTollerInfo(&ti));
		TRACE("Toller LoginName=%s\n",ti.szLoginName);
	}

	file.Close();

}

//clear sub total information
//清除昨日国际流量信息,因为月流量进行累加而昨日流量不作累加,
//因此在收集昨日的计费信息到数据库之前,必须首先清除昨日国际流量
//在Collect被调用前调用
void CMainFrame::ClearSubTotal()
{
	POSITION pos;
	CTollerInfo* pti;

	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		return;
	}

	while(pos!=NULL)
	{
		pti=(CTollerInfo*)tollerList.GetNext(pos);
		pti->nSubTotal=0;
	}
}


BOOL CMainFrame::Query(CString sLoginName,
			   CTollerInfo& tollerInfo)
{
	POSITION pos;
	CTollerInfo* pti;

	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		return FALSE;
	}

	while(pos!=NULL)
	{
		pti=(CTollerInfo*)tollerList.GetNext(pos);
		if(pti->GetLoginName()==sLoginName)
		{
			tollerInfo.Put(*pti);
			//sDebugInfo+="  Find loginName matched.";
			return TRUE;
		}
	}
	return FALSE;//can't find the matched loginName in the list.
}

//
//generate report as HTML format
//
void   CMainFrame::ReportAsHTML(CString& sFileName)
{
	CFile file;
	if(!file.Open(sFileName,CFile::modeCreate|CFile::modeWrite))
	{
	   MessageBeep(0);
	   return;
	}
	
	CString str;
	str="<html><head><title>校园网计费清单</title></head><body><center><b><i><font size=+3>校园网计费清单</font></i></b></center><p>";
	file.Write(str,str.GetLength());

	str="<center><table><caption><align=left>";
	file.Write(str,str.GetLength());

	CTime time=CTime::GetCurrentTime();

	str=time.Format("截止日期:%Y年%m月%d日</caption>");

	file.Write(str,str.GetLength());

	str="<tr align=\"left\"><td><b>用户注册名</b></td><td><b>本月国际流入量小计</b></td><td><b>昨日通信流量</b></td><td><b>本月费用小计</b></td></tr>";

	file.Write(str,str.GetLength());

	POSITION pos;
	CTollerInfo* pti;
	CString sRow;

	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		return ;
	}


	while(pos!=NULL)
	{
		pti=(CTollerInfo*)tollerList.GetNext(pos);
		sRow=FormatHTMLTabRow(pti);		
		file.Write(sRow,sRow.GetLength());
	}
	
	str="</table></body></html>";		
	file.Write(str,str.GetLength());
	file.Close();
}

//
//format a tollerinfo into a HTML table row
//
CString CMainFrame::FormatHTMLTabRow(CTollerInfo* pti)
{
	CString str;
	CString sFee;
	
	CString sSubTotal("");
	CString sTotal("");
	CString sIPAddr;//=CString(tollerInfo.szIPAddr);
	sIPAddr+=pti->szIPAddr;
	CString sLoginName(pti->szLoginName);

	sSubTotal.Format("%dk",pti->nSubTotal/1000);
	sTotal.Format("%dk",pti->nTotal/1000);
		
	int     nFee;
	nFee	=pti->nTotal/1000*m_nCostPerKB/1000;//0.02yuan per kilo bytes
	sFee.Format("%d元",nFee);
	
	str="<tr><td>"+sLoginName+"</td>"
		 +"<td>"+sTotal+"</td>"
		 +"<td>"+sSubTotal+"</td>"
		 +"<td>"+sFee+"</td>"
		 +"</tr>";

	return str;
}

//
//format a tollerinfo into a Text table row
//
CString CMainFrame::FormatTextTabRow(CTollerInfo* pti)
{
	CString str;
	CString sIPAddr(pti->szIPAddr);
	CString sLoginName(pti->szLoginName);
	CString sFee;
	int	nFee;
	//m_nCostPerKB unit=0.1 Fen,so divide by 1000
	nFee=pti->nTotal*m_nCostPerKB/1000/1000;
	sFee.Format("%d+*",nFee);
	
	str.Format(
		"%s,%s,%d,%d,%s\r\n",
		sIPAddr,
		sLoginName,
		pti->nTotal,
		pti->nSubTotal,
		sFee);

	return str;
}

//
//generate report as Text format
//
void   CMainFrame::ReportAsText(CString& sFileName)
{
	CFile file;
	if(!file.Open(sFileName,CFile::modeCreate|CFile::modeWrite))
	{
	   AfxMessageBox("Error in writing report file");
	   return;
	}
	/*
	CString str;
	str="校园网计费清单\r\n";
	file.Write(str,str.GetLength());

	str="IP地址\t注册用户名\t本月通信流量\t昨日通信流量\t费用\r\n";
	file.Write(str,str.GetLength());

	*/
	POSITION pos;
	CTollerInfo* pti;
	CString sRow;

	if((pos=tollerList.GetHeadPosition())==NULL)
	{
		return;
	}


	while(pos!=NULL)
	{
		pti=(CTollerInfo*)tollerList.GetNext(pos);
		sRow=FormatTextTabRow(pti);		
		file.Write(sRow,sRow.GetLength());
	}

	file.Close();
}



void CMainFrame::SaveSetting()
{
	CWinApp* pApp=AfxGetApp();

	pApp->WriteProfileString("System","LogFileFTPAddr",sLogFileFTPAddr);
	pApp->WriteProfileString("System","FTPServerName",sFTPServerName);

	pApp->WriteProfileString("System","FTPUserName",m_sUserName);	
	pApp->WriteProfileString("System","FTPPassword",m_sPassword);

	pApp->WriteProfileInt("System","UpdateAtHour",nUpdateAtHour);
	pApp->WriteProfileInt("System","UpdateLocalLogWhen",m_nUpdateLocalLogWhen);
	pApp->WriteProfileInt("System","TollFromDate",m_nTollFromDate);
	pApp->WriteProfileInt("System","CostPerKB",m_nCostPerKB);


	pApp->WriteProfileString("System","LastDate",sLastDate);

	pApp->WriteProfileInt("System","ServerUpdateLogAt",nServerUpdateLogAt);
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// CG: The following block was inserted by 'Status Bar' component.
	{	
		// Create an array for status bar indicators
		UINT pIndicators[3] = { ID_SEPARATOR };
		if (!m_wndStatusBar.Create(this) ||
			!InitStatusBar(pIndicators, 1, 60))
		{
			TRACE0("Failed to create Status Bar\n");
			return -1;
		}

		// TODO: If you have a call to EnableDocking() and DockControlBar()
		// before this line, move them to right after this comment.  This
		// insures that the docking area remains above the status bar.
	}

	// CG: The following line was added by the Splash Screen component.
	CSplashWnd::ShowSplashScreen(this);

	LoadSetting();

	InitSpecialUserList();

	LoadSpecialUserList();

	InitSuffixList();

	LoadSuffixList();

	LoadCERNIPPrefixList();

	LoadTollerDatabase();

	ClearSubTotal();

	InitInternetSession();

	//start the polling timer
	SetTimer(ID_POLLTIMER,MMSOFHOUR,NULL);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	UINT ClassStyle=CS_VREDRAW|CS_HREDRAW;
	cs.style=cs.style&(~FWS_ADDTOTITLE);
	cs.lpszClass = AfxRegisterWndClass(ClassStyle,
		AfxGetApp()->LoadStandardCursor(IDC_ARROW), 
		(HBRUSH)(COLOR_WINDOW+1),//for brush
		AfxGetApp()->LoadIcon(IDR_MAINFRAME));
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnUpdateDate(CCmdUI* pCmdUI)
{
	// CG: This function was inserted by 'Status Bar' component.

	// Get current date and format it
	CTime time = CTime::GetCurrentTime();
	CString strDate = time.Format(_T("%x"));

	// BLOCK: compute the width of the date string
	CSize size;
	{
		HGDIOBJ hOldFont = NULL;
		HFONT hFont = (HFONT)m_wndStatusBar.SendMessage(WM_GETFONT);
		CClientDC dc(NULL);
		if (hFont != NULL) 
			hOldFont = dc.SelectObject(hFont);
		size = dc.GetTextExtent(strDate);
		if (hOldFont != NULL) 
			dc.SelectObject(hOldFont);
	}

	// Update the pane to reflect the current date
	UINT nID, nStyle;
	int nWidth;
	m_wndStatusBar.GetPaneInfo(m_nDatePaneNo, nID, nStyle, nWidth);
	m_wndStatusBar.SetPaneInfo(m_nDatePaneNo, nID, nStyle, size.cx);
	pCmdUI->SetText(strDate);
	pCmdUI->Enable(TRUE);

}

void CMainFrame::OnUpdateTime(CCmdUI* pCmdUI)
{
	// CG: This function was inserted by 'Status Bar' component.

	// Get current date and format it
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format(_T("%X"));

	// BLOCK: compute the width of the date string
	CSize size;
	{
		HGDIOBJ hOldFont = NULL;
		HFONT hFont = (HFONT)m_wndStatusBar.SendMessage(WM_GETFONT);
		CClientDC dc(NULL);
		if (hFont != NULL) 
			hOldFont = dc.SelectObject(hFont);
		size = dc.GetTextExtent(strTime);
		if (hOldFont != NULL) 
			dc.SelectObject(hOldFont);
	}

	// Update the pane to reflect the current time
	UINT nID, nStyle;
	int nWidth;
	m_wndStatusBar.GetPaneInfo(m_nTimePaneNo, nID, nStyle, nWidth);
	m_wndStatusBar.SetPaneInfo(m_nTimePaneNo, nID, nStyle, size.cx);
	pCmdUI->SetText(strTime);
	pCmdUI->Enable(TRUE);

}

BOOL CMainFrame::InitStatusBar(UINT *pIndicators, int nSize, int nSeconds)
{
	// CG: This function was inserted by 'Status Bar' component.

	// Create an index for the DATE pane
	m_nDatePaneNo = nSize++;
	pIndicators[m_nDatePaneNo] = ID_INDICATOR_DATE;
	// Create an index for the TIME pane
	m_nTimePaneNo = nSize++;
	nSeconds = 1;
	pIndicators[m_nTimePaneNo] = ID_INDICATOR_TIME;

	// TODO: Select an appropriate time interval for updating
	// the status bar when idling.
	m_wndStatusBar.SetTimer(0x1000, nSeconds * 1000, NULL);

	return m_wndStatusBar.SetIndicators(pIndicators, nSize);

}

void CMainFrame::OnOption() 
{
	// TODO: Add your command handler code here
	CPropertySheet dialog(IDS_OPTION);
	CSysOptionPage page1;//for IP address range list editing
	CFtpOptionPage page2;
	CPollingPage   page3;
	
	page2.m_sFtpServerName		=sFTPServerName;
	page2.m_sLogFileLocation	=sLogFileFTPAddr;
	page2.m_sUserName			=m_sUserName;
	page2.m_sPassword			=m_sPassword;

	page3.m_uInterval			=nUpdateAtHour;
	page3.m_nServerUpdateLogAt	=nServerUpdateLogAt;
	page3.m_nUpdateLocalLogWhen =m_nUpdateLocalLogWhen;
	page3.m_nTollFromDate		=m_nTollFromDate;
	page3.m_uCostPerKB			=m_nCostPerKB;

	
	dialog.AddPage(&page1);
	dialog.AddPage(&page2);
	dialog.AddPage(&page3);

	if(dialog.DoModal()==IDOK)
	{ 
		m_sUserName			 =page2.m_sUserName;
		m_sPassword			 =page2.m_sPassword;
		sFTPServerName		 =page2.m_sFtpServerName;
		sLogFileFTPAddr		 =page2.m_sLogFileLocation;
		
		nUpdateAtHour		 =page3.m_uInterval;
		nServerUpdateLogAt	 =page3.m_nServerUpdateLogAt;
		m_nUpdateLocalLogWhen=page3.m_nUpdateLocalLogWhen;
		m_nTollFromDate		 =page3.m_nTollFromDate;
		m_nCostPerKB		 =page3.m_uCostPerKB;

		SaveSetting();
		SaveTollSetting();
		SaveCERNIPPrefixList();
		SaveSuffixList();
	}
}

void CMainFrame::OnDestroy() 
{

	CMDIFrameWnd::OnDestroy();

}

void CMainFrame::OnClose() 
{
	SaveSetting();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -