www.pudn.com > SMS.rar > MobileAssistantView.cpp


// MobileAssistantView.cpp : CMobileAssistantView 类的实现 
// 
 
#include "stdafx.h" 
#include "MobileAssistant.h" 
 
#include "MobileAssistantDoc.h" 
#include "MobileAssistantView.h" 
#include "MainFrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 
 
LRESULT Proc_ReceivedSMS ( SM_PARAM *pSMRecv, WPARAM wParam, LPARAM lParam ) 
{ 
	CMobileAssistantView *pMobileAssistantView = reinterpret_cast(wParam); 
	ASSERT ( pMobileAssistantView ); 
	return pMobileAssistantView->Proc_ReceivedSMS ( pSMRecv ); 
} 
 
void Proc_IncomingFromGSM ( LPCTSTR lpszCallerID ) 
{ 
	Log ( L_NORMAL, _T("New call from [%s]"), GET_SAFE_STRING(lpszCallerID) ); 
} 
 
// CMobileAssistantView 
 
IMPLEMENT_DYNCREATE(CMobileAssistantView, CFormView) 
 
BEGIN_MESSAGE_MAP(CMobileAssistantView, CFormView) 
	ON_COMMAND(ID_BUTTON_SendSMS, OnBUTTONSendSMS) 
	ON_WM_DESTROY() 
	ON_COMMAND(ID_BUTTON_OpenPort, OnBUTTONOpenPort) 
	ON_COMMAND(ID_BUTTON_Clear, OnBUTTONClear) 
	ON_COMMAND(ID_BUTTON_Exit, OnBUTTONExit) 
	ON_COMMAND(ID_BUTTON_RecvSMS, OnBUTTONRecvSMS) 
	ON_COMMAND(ID_BUTTON_ReadSMS, OnBUTTONReadSMS) 
	ON_BN_CLICKED(IDC_BUTTON_AddPhoneBook, OnBUTTONAddPhoneBook) 
	ON_WM_SIZE() 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDC_BUTTON_ReadOnePhoneBook, OnBUTTONReadOnePhoneBook) 
	ON_BN_CLICKED(IDC_BUTTON_ReadPhoneBook, OnBUTTONReadPhoneBook) 
	ON_MESSAGE(WM_SHOWLOG,OnWM_SHOWLOG) 
END_MESSAGE_MAP() 
 
// CMobileAssistantView 构造/析构 
 
CMobileAssistantView::CMobileAssistantView() 
	: CFormView(CMobileAssistantView::IDD) 
	, m_nComNo ( 1 ) 
{ 
	//{{AFX_DATA_INIT(CMobileAssistantView) 
	m_csReceiverMobileNO = _T(""); 
	m_csSMSContent = _T(""); 
	m_csBaudRate = _T("9600"); 
	m_csSMSC = _T(""); 
	m_csLocalCountryCode = _T(""); 
	m_csLocalAreaCode = _T(""); 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
	m_MobileControl.m_bMustRegistered = FALSE; 
 
	{	//d 
	}	//d 
} 
 
CMobileAssistantView::~CMobileAssistantView() 
{ 
} 
 
void CMobileAssistantView::DoDataExchange(CDataExchange* pDX) 
{ 
	CFormView::DoDataExchange(pDX); 
	DDX_Text(pDX, IDC_EDIT_ReceiverMobileNO, m_csReceiverMobileNO); 
	DDX_Text(pDX, IDC_EDIT_SMSContent, m_csSMSContent); 
	DDX_CBString(pDX, IDC_COMBO_BaudRate, m_csBaudRate); 
	DDX_Text(pDX, IDC_EDIT_SMSC, m_csSMSC); 
	DDX_Text(pDX, IDC_EDIT_LocalCountryCode, m_csLocalCountryCode); 
	DDX_Text(pDX, IDC_EDIT_LocalAreaCode, m_csLocalAreaCode); 
} 
 
BOOL CMobileAssistantView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: 在此处通过修改 
	//  CREATESTRUCT cs 来修改窗口类或样式 
 
	return CFormView::PreCreateWindow(cs); 
} 
 
void CMobileAssistantView::OnInitialUpdate() 
{ 
	CFormView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
 
	Set_RecvLogMsgHwnd ( GetSafeHwnd() ); 
	SetCtrlValue (); 
	this->SetForegroundWindow (); 
	SetTimer ( 1, 1000, NULL ); 
 
	Log ( L_VERBOSE, _T("\r\n%s"), NOTE_HwMobile ); 
} 
 
 
// CMobileAssistantView 诊断 
 
#ifdef _DEBUG 
void CMobileAssistantView::AssertValid() const 
{ 
	CFormView::AssertValid(); 
} 
 
void CMobileAssistantView::Dump(CDumpContext& dc) const 
{ 
	CFormView::Dump(dc); 
} 
 
CMobileAssistantDoc* CMobileAssistantView::GetDocument() const // 非调试版本是内联的 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMobileAssistantDoc))); 
	return (CMobileAssistantDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
 
// CMobileAssistantView 消息处理程序 
 
void CMobileAssistantView::Align() 
{ 
	if ( !GetDlgItem(IDC_EDIT_Log) || !::IsWindow ( GetDlgItem(IDC_EDIT_Log)->GetSafeHwnd() ) ) 
		return; 
 
	CRect rcClient; 
	CWnd *pWndParent = GetParent (); 
	GetWindowRect ( &rcClient ); 
	pWndParent->ScreenToClient ( &rcClient ); 
	MoveWindow ( &rcClient ); 
 
	CRect rc; 
	GetDlgItem(IDC_EDIT_Log)->GetWindowRect ( &rc ); 
	ScreenToClient ( &rc ); 
 
	GetClientRect ( &rcClient ); 
	CRect rcLog = ::hwMoveWindow ( this, GetDlgItem(IDC_EDIT_Log), 
		0, rc.top, rcClient.right, rcClient.bottom ); 
 
} 
 
void CMobileAssistantView::SetCtrlValue() 
{ 
	m_csReceiverMobileNO = AfxGetApp()->GetProfileString ( _T("SMS"), _T("ReceiverMobileNO"), _T("13823310105;13510631301;") ); 
	m_csLocalCountryCode = AfxGetApp()->GetProfileString ( _T("SMS"), _T("LocalCountryCode"), _T("+86") ); 
	m_csLocalAreaCode = AfxGetApp()->GetProfileString ( _T("SMS"), _T("LocalAreaCode"), _T("0755") ); 
	m_csSMSContent = AfxGetApp()->GetProfileString ( _T("SMS"), _T("SMSContent"), _T("hello!") ); 
	m_csBaudRate = AfxGetApp()->GetProfileString ( _T("SMS"), _T("BaudRate"), _T("9600") ); 
	m_nComNo = AfxGetApp()->GetProfileInt ( _T("SMS"), _T("COM"), 1 ); 
 
	CComboBox *pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_COM); 
	ASSERT ( pcombo ); 
	for ( int i=0; i<16; i++ ) 
	{ 
		CString csCOM; 
		csCOM.Format ( _T("COM%d"), i+1 ); 
		pcombo->AddString ( csCOM ); 
	} 
	if ( m_nComNo > 0 ) 
		pcombo->SetCurSel ( m_nComNo-1 ); 
 
	pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_BaudRate); 
	ASSERT ( pcombo ); 
	pcombo->AddString ( _T("1200") ); 
	pcombo->AddString ( _T("2400") ); 
	pcombo->AddString ( _T("4800") ); 
	pcombo->AddString ( _T("9600") ); 
	pcombo->AddString ( _T("19200") ); 
	pcombo->AddString ( _T("38400") ); 
	pcombo->AddString ( _T("57600") ); 
	pcombo->AddString ( _T("115200") ); 
 
	pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_PhoneBook); 
	pcombo->AddString ( _T("FD - 固定拨号电话本") ); 
	pcombo->AddString ( _T("SM - SIM 卡电话本") ); 
	pcombo->AddString ( _T("ON - 本机电话号码列表") ); 
	pcombo->AddString ( _T("ME - 手机电话本") ); 
	pcombo->AddString ( _T("LD - 最近已拨电话(SIM)") ); 
	pcombo->AddString ( _T("MC - 最近来电未接电话本") ); 
	pcombo->AddString ( _T("RC - 最近来电已接电话本") ); 
	pcombo->AddString ( _T("DC - 最近拨号电话本") ); 
	pcombo->SetCurSel ( 0 ); 
 
	UpdateData ( FALSE ); 
} 
 
void CMobileAssistantView::GetCtrlValue() 
{ 
	UpdateData ( TRUE ); 
	CComboBox *pcombo = (CComboBox*) GetDlgItem(IDC_COMBO_COM); 
	ASSERT ( pcombo ); 
	m_nComNo = pcombo->GetCurSel () + 1; 
	m_csReceiverMobileNO.TrimLeft(); m_csReceiverMobileNO.TrimRight(); 
	m_csSMSContent.TrimLeft(); m_csSMSContent.TrimRight(); 
} 
 
void CMobileAssistantView::OnDestroy() 
{ 
	GetCtrlValue (); 
	AfxGetApp()->WriteProfileString ( _T("SMS"), _T("ReceiverMobileNO"), m_csReceiverMobileNO ); 
	AfxGetApp()->WriteProfileString ( _T("SMS"), _T("LocalCountryCode"), m_csLocalCountryCode ); 
	AfxGetApp()->WriteProfileString ( _T("SMS"), _T("LocalAreaCode"), m_csLocalAreaCode ); 
	AfxGetApp()->WriteProfileString ( _T("SMS"), _T("SMSContent"), m_csSMSContent ); 
	AfxGetApp()->WriteProfileString ( _T("SMS"), _T("BaudRate"), m_csBaudRate ); 
	AfxGetApp()->WriteProfileInt ( _T("SMS"), _T("COM"), m_nComNo ); 
 
	CFormView::OnDestroy(); 
} 
 
void CMobileAssistantView::SetMobileParameter () 
{ 
	GetCtrlValue (); 
 
	g_MobileParameter.bRecvSMSEnable = TRUE; 
	g_MobileParameter.bSendSMSEnable = TRUE; 
	g_MobileParameter.bAllowEchoSMS = TRUE; 
	lstrcpyn(g_MobileParameter.szLocalCountryCode, m_csLocalCountryCode, COUNT(g_MobileParameter.szLocalCountryCode) ); 
	lstrcpyn(g_MobileParameter.szLocalAreaCode, m_csLocalAreaCode, COUNT(g_MobileParameter.szLocalAreaCode) ); 
 
	m_MobileControl.SetParameter ( &g_MobileParameter, ::Proc_ReceivedSMS, WPARAM(this), NULL, 
		_T("HAHA:\n"), _T("\nSent by BiKe") ); 
	m_MobileControl.m_Proc_IncomingFromGSM = ::Proc_IncomingFromGSM; 
} 
 
void CMobileAssistantView::OnBUTTONOpenPort()  
{ 
	if ( ( (CMobileAssistantApp*)AfxGetApp() )->m_bMustTerminate ) 
	{ 
		PostQuitMessage (0); 
		return; 
	} 
	ToolBarEnableButton ( ID_BUTTON_OpenPort, FALSE ); 
	Log ( L_NORMAL, _T("Connect to SMS module. Please wait for a moment ...") ); 
	 
	SetMobileParameter (); 
	SetMobileNOHead (); 
	if ( !m_MobileControl.Init ( TRUE, TRUE, g_hEvt_TerminateSys, m_nComNo, atoi(CMultiByteString(m_csBaudRate).GetBuffer()) ) ) 
	{ 
		ToolBarEnableButton ( ID_BUTTON_OpenPort, TRUE ); 
	} 
	else 
	{ 
	} 
} 
 
void CMobileAssistantView::OnBUTTONClear()  
{ 
	if ( !GetDlgItem(IDC_EDIT_Log) || !::IsWindow ( GetDlgItem(IDC_EDIT_Log)->GetSafeHwnd() ) ) 
		return; 
	GetDlgItem(IDC_EDIT_Log)->SetWindowText ( _T("") ); 
} 
 
void CMobileAssistantView::OnBUTTONExit()  
{ 
	AfxGetMainWnd ()-> PostMessage ( WM_CLOSE ); 
} 
 
LRESULT Proc_SendSMSEnd ( ENUM_ERROR_TYPE eResult, SM_PARAM *psm, WPARAM wParam, LPARAM lParam ) 
{ 
	CMobileAssistantView *pMobileAssistantView = reinterpret_cast ( wParam ); 
	ASSERT ( pMobileAssistantView ); 
	return pMobileAssistantView->Proc_SendSMSEnd ( eResult, psm, wParam, lParam ); 
} 
 
BOOL CMobileAssistantView::Proc_SendSMSEnd ( ENUM_ERROR_TYPE eResult, SM_PARAM *psm, WPARAM wParam, LPARAM lParam ) 
{ 
	ASSERT ( psm ); 
	Log ( L_NORMAL, _T("Send SMS [%s] to [%s] %s"), GetCompatibleString(psm->TP_UD_Byte,FALSE), GetCompatibleString(psm->TPA,FALSE), 
		(eResult==ENUM_ERROR_TYPE_SUCCESS)?_T("successfully"):g_pErrorTypeDesc[eResult] ); 
	return TRUE; 
} 
 
void CMobileAssistantView::OnBUTTONSendSMS()  
{ 
	SetMobileParameter (); 
 
	if ( m_csReceiverMobileNO.GetLength () < 1|| m_csSMSContent.GetLength () < 1 ) 
	{ 
		AfxMessageBox ( _T("Enter error") ); 
		return; 
	} 
 
	CStringArray StrAry; 
	PartStringAndAddToStrAry ( m_csReceiverMobileNO, StrAry, _T(';') ); 
	for ( int i=0; iGetSize(); i++ ) 
	{ 
		t_PhoneBook &PhoneBook = pAry_PhoneBook->GetAt ( i ); 
		Log ( L_NORMAL, _T("电话本信息:NO.%d - [%s] - %s - %s"), PhoneBook.nIndex, 
			PhoneBook.szPhoneBookName, PhoneBook.szName, PhoneBook.szPhoneNO ); 
	} 
 
	HwDbgLog ( L_DEBUG, _T("读取电话本已经全部结束, 共 %d 条纪录"), pAry_PhoneBook->GetSize() ); 
	DELETE_ARRAY ( &pAry_PhoneBook ); 
	EnableWindow ( TRUE ); 
} 
 
BOOL CMobileAssistantView::Proc_ReceivedSMS ( SM_PARAM *pSMRecv ) 
{ 
	ASSERT ( pSMRecv ); 
	if ( !pSMRecv ) return FALSE; 
	char szRecvTime[DATETIME_TYPE_LENGTH+1] = {0}; 
	m_MobileControl.Convert_SCTS2Standard ( pSMRecv->TP_SCTS, szRecvTime, sizeof(szRecvTime) ); 
	Log ( L_NORMAL, _T("\r\nReceived SMS : NO.%d - %s - [%s]\r\n%s\r\n"), pSMRecv->index, GetCompatibleString(pSMRecv->TPA,FALSE), 
		GetCompatibleString(szRecvTime,FALSE), GetCompatibleString(pSMRecv->TP_UD_Byte,FALSE) ); 
	return TRUE; 
} 
 
void CMobileAssistantView::OnBUTTONAddPhoneBook()  
{ 
	EnableWindow ( FALSE ); 
	CString csName, csPhoneNO, csPhoneBook; 
	GetDlgItemText ( IDC_EDIT_Name, csName ); 
	GetDlgItemText ( IDC_EDIT_PhoneNO, csPhoneNO ); 
	GetDlgItemText ( IDC_COMBO_PhoneBook, csPhoneBook ); 
	int nSpacePos = csPhoneBook.Find ( _T(" "), 0 ); 
	if ( nSpacePos < 0 ) nSpacePos = csPhoneBook.GetLength(); 
	csPhoneBook = csPhoneBook.Left (nSpacePos); 
 
	if ( csName.GetLength () < 1 || csPhoneNO.GetLength () < 1 || csPhoneBook.GetLength () < 1 ) 
	{ 
		AfxMessageBox ( _T("Enter Phone Datum Error") ); 
		EnableWindow ( TRUE ); 
		return; 
	} 
 
	if ( !m_MobileControl.InsertPhoneBook ( csName, csPhoneNO, csPhoneBook ) ) 
	{ 
		AfxMessageBox ( _T("Insert PhoneBook Failed") ); 
	} 
	EnableWindow ( TRUE ); 
} 
 
void CMobileAssistantView::OnSize(UINT nType, int cx, int cy)  
{ 
	CFormView::OnSize(nType, cx, cy); 
	 
	Align (); 
} 
 
BOOL CMobileAssistantView::ToolBarEnableButton(int nID, BOOL bEnable) 
{ 
	CToolBarCtrl &ToolBarCtrl = ( (CMainFrame*)AfxGetMainWnd() )->m_wndToolBar.GetToolBarCtrl (); 
	return ToolBarCtrl.EnableButton ( nID, bEnable ); 
} 
 
void CMobileAssistantView::OnTimer(UINT nIDEvent)  
{ 
#ifdef _IS_HOWA_MOBILE_MODULE_TOOLS 
	if ( m_csSMSC.GetLength() <= 0 ) 
	{ 
		CString csSMSC = m_MobileControl.RetrievesSMSC (); 
		if ( csSMSC.GetLength() > 0 ) 
		{ 
			KillTimer ( nIDEvent ); 
			UpdateData ( TRUE ); 
			m_csSMSC = csSMSC; 
			UpdateData ( FALSE ); 
		} 
	} 
#endif 
	CFormView::OnTimer(nIDEvent); 
} 
 
void CMobileAssistantView::SetMobileNOHead() 
{ 
	t_Ary_MobileNOHead Ary_MobileNOHead; 
	Ary_MobileNOHead.SetSize ( 0, 10*sizeof(t_MobileNOHead) ); 
	t_MobileNOHead MobileNOHead = {0}; 
	memset ( &MobileNOHead, 0, sizeof(t_MobileNOHead) ); 
 
	// 大陆全球通 
	lstrcpy ( MobileNOHead.szCountryCode, _T("0086") );					// 国家代码 
	lstrcpy ( MobileNOHead.szAreaCode, _T("") );						// 地区代码 
	lstrcpy ( MobileNOHead.szMobileNoHead, _T("13") );					// 手机字头 
	lstrcpy ( MobileNOHead.szPrepositiveNO, _T("") );					// 前置号码 
	MobileNOHead.dwMobileNOFormat = SMS_MobileNO_Format_CountryCode;	// 字头格式 
	lstrcpy ( MobileNOHead.szDesc, _T("大陆全球通") );					// 描述 
	Ary_MobileNOHead.Add ( MobileNOHead ); 
	memset ( &MobileNOHead, 0, sizeof(t_MobileNOHead) ); 
 
	// 台湾全球通 
	lstrcpy ( MobileNOHead.szCountryCode, _T("00886") );				// 国家代码 
	lstrcpy ( MobileNOHead.szAreaCode, _T("") );						// 地区代码 
	lstrcpy ( MobileNOHead.szMobileNoHead, _T("09") );					// 手机字头 
	lstrcpy ( MobileNOHead.szPrepositiveNO, _T("") );					// 前置号码 
	MobileNOHead.dwMobileNOFormat = SMS_MobileNO_Format_CountryCode;	// 字头格式 
	lstrcpy ( MobileNOHead.szDesc, _T("台湾全球通") );					// 描述 
	Ary_MobileNOHead.Add ( MobileNOHead ); 
	memset ( &MobileNOHead, 0, sizeof(t_MobileNOHead) ); 
 
	m_MobileControl.ConfigParameter ( &Ary_MobileNOHead ); 
} 
 
void CMobileAssistantView::EnableWindow(BOOL bEnable) 
{ 
	GetDlgItem(IDC_COMBO_COM)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_COMBO_BaudRate)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_LocalCountryCode)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_LocalAreaCode)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_ReceiverMobileNO)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_SMSContent)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_Name)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_EDIT_PhoneNO)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_COMBO_PhoneBook)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_BUTTON_ReadOnePhoneBook)->EnableWindow ( bEnable ); 
	GetDlgItem(IDC_BUTTON_AddPhoneBook)->EnableWindow ( bEnable ); 
} 
 
LRESULT CMobileAssistantView::OnWM_SHOWLOG(WPARAM wParam, LPARAM lParam) 
{ 
	::OnWM_SHOWLOG ( (CRichEditCtrl*)GetDlgItem(IDC_EDIT_Log), wParam, lParam ); 
	return 0L; 
}