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

📄 scadnpage.cpp

📁 agsm a gsm sim manager for browsing, managing, and manipulating the phonebook,SMS on the SIM card. I
💻 CPP
字号:
// ScAdnPage.cpp : implementation file
//

#include "stdafx.h"
#include "agsm2.h"
#include "ScAdnPage.h"
#include "agsm2Dlg.h"
#include "PcScCtrl.h"
#include "Helper.h"
#include "PBDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CScAdnPage property page

IMPLEMENT_DYNCREATE(CScAdnPage, CScPropertyPage)

CScAdnPage::CScAdnPage() : CScPropertyPage(CScAdnPage::IDD)
{
	//{{AFX_DATA_INIT(CScAdnPage)
	//}}AFX_DATA_INIT
}

CScAdnPage::CScAdnPage(UINT  nIDCaption ) : CScPropertyPage(CScAdnPage::IDD,nIDCaption )
{
	//{{AFX_DATA_INIT(CScAdnPage)
	//}}AFX_DATA_INIT
}


CScAdnPage::~CScAdnPage()
{
}

void CScAdnPage::DoDataExchange(CDataExchange* pDX)
{
	CScPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScAdnPage)
	DDX_Control(pDX, IDC_LIST_ADN, m_AdnListCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScAdnPage, CScPropertyPage)
	//{{AFX_MSG_MAP(CScAdnPage)
	ON_WM_CONTEXTMENU()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_ADN, OnDblclkListAdn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScAdnPage message handlers

BOOL CScAdnPage::OnInitDialog() 
{
	CScPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_AdnListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT|
									LVS_EX_GRIDLINES|
									LVS_EX_HEADERDRAGDROP);
	CString pos((LPCSTR)IDS_STRING_POS);
	CString name((LPCSTR)IDS_STRING_NAME);
	CString phone((LPCSTR)IDS_STRING_PHONE);
	m_AdnListCtrl.InsertColumn(0,pos,LVCFMT_LEFT,40);
	m_AdnListCtrl.InsertColumn(1,name,LVCFMT_LEFT,100);
	m_AdnListCtrl.InsertColumn(2,phone,LVCFMT_LEFT,200);
	char buf[8];
	for(int i=1; i<256; i++)
	{
		sprintf(buf,"%d",i);
		m_AdnListCtrl.AddItem(i-1,0,buf);
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CScAdnPage::OnApply() 
{
	// TODO: Add your specialized code here and/or call the base class
	SC_OPERATION curOp = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_CurOperation;
	TRACE(_T("CScAdnPage::OnApply %d\n"),curOp);
	switch(curOp)
	{
		case SC_READ:
			ReadRecord();
			break;
		case SC_WRITE:
			WriteRecord();
			break;
		case SC_MOVEUP:
			m_AdnListCtrl.MoveUp();
			break;
		case SC_MOVEDOWN:
			m_AdnListCtrl.MoveDown();
			break;
		case SC_IMPORT:
			break;
		case SC_EXPORT:
			break;
		case SC_PRINT:
			break;
		default:
			break;
	}
	return CScPropertyPage::OnApply();
}

void CScAdnPage::ReadRecord(int n)
{
	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader;
	int from = 0, to = 0, i;
	try{
		BeginWaitCursor();
		SelectCurFile();
		CProgressCtrl &ReadProgress = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_RWProgress;

		if(n == -1)//read all records
		{
			ReadProgress.SetRange(0,(SHORT)m_NumOfRecs);
			ReadProgress.ShowWindow(SW_SHOW);
			m_AdnListCtrl.DeleteAllItems();
			from =1; to = m_NumOfRecs;
			for(i =from; i<= to; i++)
			{
				ReadRecordN(i,TRUE);
				ReadProgress.SetPos(i);
			}
			//Beep(1000,50);
			ReadProgress.ShowWindow(SW_HIDE);
		}else if(n == -2){
			int nSelected = m_AdnListCtrl.GetSelectedCount();
			int nItem = -1;
			ReadProgress.SetRange(0,(SHORT)nSelected);
			ReadProgress.ShowWindow(SW_SHOW);
			for (i=1; i<=nSelected; i++)
			{
				nItem = m_AdnListCtrl.GetNextItem(nItem, LVNI_SELECTED);
				ASSERT(nItem != -1);
				ReadRecordN(nItem+1,FALSE);
				ReadProgress.SetPos(i);
			}
			ReadProgress.ShowWindow(SW_HIDE);

		}else{
			ReadRecordN(n,FALSE);
		}
		EndWaitCursor();
	}
	catch(LONG e)
	{
		if((e & 0xfffff000) == 0x6000 || (e & 0xfffff000) == 0x9000)
			AfxMessageBox(ScReader.FormatErrMsg(e));
		else
			Helper::ShowLastError(e);
	}
}

void CScAdnPage::WriteRecord(int n)
{
	TRACE("Write Record\n");
	int iRet;
	if(n == -1)
		iRet = AfxMessageBox(IDS_STRING128,MB_YESNO);   
	else
		iRet = AfxMessageBox(IDS_STRING129,MB_YESNO); 
	if(iRet == IDNO)
		return;

	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader;
	try{
		BeginWaitCursor();
		SelectCurFile();
		CProgressCtrl &ReadProgress = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_RWProgress;
		int itemCount = m_AdnListCtrl.GetItemCount();
		int from = 0, to = 0, i;

		if(n == -1)//write all records
		{
			ReadProgress.SetRange(0,(SHORT)m_NumOfRecs);
			ReadProgress.ShowWindow(SW_SHOW);
			from = 1; to = m_NumOfRecs;
			for(i=from; i <= to && i<=itemCount; i++)
			{
				WriteRecordN(i,FALSE);
				ReadProgress.SetPos(i);
			}
			//Beep(1000,50);
			//((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_globalStatusStr = _T("成功写入");
			ReadProgress.ShowWindow(SW_HIDE);
			//((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->UpdateData(FALSE);
		}else if( n == -2){//selected 
			int nSelected = m_AdnListCtrl.GetSelectedCount();
			int nItem = -1;
			ReadProgress.SetRange(0,(SHORT)nSelected);
			ReadProgress.ShowWindow(SW_SHOW);
			for (i=1; i<=nSelected; i++)
			{
				nItem = m_AdnListCtrl.GetNextItem(nItem, LVNI_SELECTED);
				ASSERT(nItem != -1);
				WriteRecordN(nItem+1,FALSE);
				ReadProgress.SetPos(i);
			}
			ReadProgress.ShowWindow(SW_HIDE);
		}else{//individual
			WriteRecordN(n,FALSE);
		}
		EndWaitCursor();
	}catch(LONG e)
	{
		if((e & 0xfffff000) == 0x6000 || (e & 0xfffff000) == 0x9000)
			AfxMessageBox(ScReader.FormatErrMsg(e));
		else
			Helper::ShowLastError(e);
	}
}

void CScAdnPage::OnContextMenu(CWnd* pWnd, CPoint point) 
{
	// TODO: Add your message handler code here
	TRACE("OnContextMenu\n");
	CMenu menu;
	VERIFY(menu.LoadMenu(IDR_POPUP_MENU));
	CMenu* pPopup = menu.GetSubMenu(0);
	ASSERT(pPopup != NULL);
	if(m_AdnListCtrl.GetSelectedCount() == 0)
	{
		pPopup->EnableMenuItem(ID_MENUITEM_EDIT,MF_GRAYED);
		pPopup->EnableMenuItem(ID_MENUITEM_CUT,MF_GRAYED);
		pPopup->EnableMenuItem(ID_MENUITEM_COPY,MF_GRAYED);
		pPopup->EnableMenuItem(ID_MENUITEM_DELETE,MF_GRAYED);
	}else{
		POSITION pos = m_AdnListCtrl.GetFirstSelectedItemPosition();
		TRACE("HotItem %d\n",pos);
		if(pos)
		{
			if( m_AdnListCtrl.GetItemText((int)pos-1,1).IsEmpty() ||
				m_AdnListCtrl.GetItemText((int)pos-1,2).IsEmpty() )
			{
				pPopup->EnableMenuItem(ID_MENUITEM_CUT,MF_GRAYED);
				pPopup->EnableMenuItem(ID_MENUITEM_COPY,MF_GRAYED);
				pPopup->EnableMenuItem(ID_MENUITEM_DELETE,MF_GRAYED);
			}
		}
	}
	if(CanPaste() == FALSE)
	{
		pPopup->EnableMenuItem(ID_MENUITEM_PASTE,MF_GRAYED);
	}
 
	pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd());

}


void CScAdnPage::OnDblclkListAdn(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	TRACE("OnDblclkListAdn\n");
	EditRecord();
	*pResult = 0;
}

void CScAdnPage::Paste()
{
	if ( !OpenClipboard() )
	{
		AfxMessageBox( "Cannot open the Clipboard" );
		return;
	}
	/* get text from the clipboard */
	HANDLE hClipData;
	LPVOID lpClipData;
	if (!(hClipData = GetClipboardData(CF_TEXT))) {
		AfxMessageBox( "Cannot get Clipboard data" );
		CloseClipboard();
		return;
	}
	if (!(lpClipData = GlobalLock(hClipData))) {
		AfxMessageBox( "Out of Memory" );
		CloseClipboard();
		return;
	}
	int len = strlen((char *)lpClipData)+1;
	char *buf = (char *)malloc(len);
	char *buf_base = buf;
	memcpy(buf,lpClipData,len);
	int ret;
	char buf1[16],buf2[16],buf3[64];
	UINT nSelectedCount = m_AdnListCtrl.GetSelectedCount();
	POSITION pos = m_AdnListCtrl.GetFirstSelectedItemPosition();
	UINT nPasted = 0;
	while(*buf)
	{
		ret = sscanf(buf,"%3[0-9],%14[^,],%40[0-9*#]",buf1,buf2,buf3);
		if(ret != 3)
			ret = sscanf(buf,"%3[0-9],%14[^,],%40[+0-9*#]",buf1,buf2,buf3);
		if(ret == 3)
		{
			if(nSelectedCount == 0)
			{
				int i = m_AdnListCtrl.GetItemCount();
				sprintf(buf1,"%d",i);
				m_AdnListCtrl.AddItem(i,0,buf1);
				m_AdnListCtrl.AddItem(i,1,buf2);
				m_AdnListCtrl.AddItem(i,2,buf3);
				
			}else{
				if(nPasted < nSelectedCount)
				{
//					AddItem((int)(pos+nPasted-1),0,buf1);
					m_AdnListCtrl.AddItem((int)(pos+nPasted-1),1,buf2);
					m_AdnListCtrl.AddItem((int)(pos+nPasted-1),2,buf3);
					nPasted++;
				}else
					break;
			}
			buf = strstr(buf,"\r\n");
			if(buf)
				buf += 2;
			else
				break;
		}else
			break;
	}
	free(buf_base);
	GlobalUnlock(hClipData);
	CloseClipboard();

}

BOOL CScAdnPage::CanPaste()
{
	if ( !OpenClipboard() )
	{
		AfxMessageBox( "Cannot open the Clipboard" );
		return FALSE;
	}
	/* get text from the clipboard */
	HANDLE hClipData;
	LPVOID lpClipData;
	if (!(hClipData = GetClipboardData(CF_TEXT))) {
		//AfxMessageBox( "Cannot get Clipboard data" );
		CloseClipboard();
		return FALSE;
	}
	if (!(lpClipData = GlobalLock(hClipData))) {
		AfxMessageBox( "Out of Memory" );
		CloseClipboard();
		return FALSE;
	}
	int len = strlen((char *)lpClipData)+1;
	char *buf = (char *)malloc(len);
	memcpy(buf,lpClipData,len);
	char buf1[16],buf2[16],buf3[64];
	int ret = sscanf(buf,"%3[0-9],%14[^,],%40[0-9*#]",buf1,buf2,buf3);
	if(ret != 3)
		ret = sscanf(buf,"%3[0-9],%14[^,],+%40[0-9*#]",buf1,buf2,buf3);
	free(buf);
	GlobalUnlock(hClipData);
	CloseClipboard();
	return ret == 3;
}

void CScAdnPage::EditRecord()
{
	POSITION pos = m_AdnListCtrl.GetFirstSelectedItemPosition();
	TRACE("HotItem %d\n",pos);
	if(pos)
	{
		CPBDialog dlg;
		dlg.m_editName = m_AdnListCtrl.GetItemText((int)pos-1,1);
		dlg.m_editPhone = m_AdnListCtrl.GetItemText((int)pos-1,2);
		dlg.DoModal();
		TRACE("name=%s\n",dlg.m_editName);
		TRACE("phone=%s\n",dlg.m_editPhone);
		m_AdnListCtrl.AddItem((int)pos-1,1,dlg.m_editName);
		m_AdnListCtrl.AddItem((int)pos-1,2,dlg.m_editPhone);
	}

}

BOOL CScAdnPage::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	HACCEL hAccelTable = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_hAccelTable;
	TranslateAccelerator(GetSafeHwnd(), hAccelTable, pMsg);
	return CScPropertyPage::PreTranslateMessage(pMsg);
}

CScListCtrl* CScAdnPage::GetListCtrl()
{
	return &m_AdnListCtrl;
}

void CScAdnPage::SelectCurFile()
{
	RESAPDU resApdu;
	resApdu.data = m_pResData;
	resApdu.len = sizeof(m_pResData);
	CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader;

	try{
		ScReader.m_bGetRes = FALSE;
		ScReader.Select(0x3f00,resApdu);

		resApdu.len = sizeof(m_pResData);
		ScReader.Select(0x7f10,resApdu);

		ScReader.m_bGetRes = TRUE;
		resApdu.len = sizeof(m_pResData);
		//http://support.microsoft.com/kb/q141487/
		//CString title((LPCSTR)IDS_STRING_LND);
		
		//if(m_psp.pszTitle && title == m_psp.pszTitle )//LND
		//	ScReader.Select(0x6f44,resApdu);
		//else
		//	ScReader.Select(0x6f3a,resApdu);
		ScReader.Select(m_FileId,resApdu);

		m_FileSize = (m_pResData[2]<<8)|m_pResData[3];
		m_Sof = (StructOfFile)m_pResData[13];
		m_RecordLen = m_pResData[14];
		m_NumOfRecs = m_FileSize/m_RecordLen;
	}catch (LONG)
	{
		throw;
	}
}

void CScAdnPage::ReadRecordN(int n, BOOL newRecord)
{
	try{
		int nItem = n - 1;
		CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader;
		LPBYTE pRecData = (LPBYTE)malloc(m_RecordLen+2);
		RESAPDU resApdu;
		resApdu.data = pRecData;
		resApdu.len = m_RecordLen+2;
		ScReader.ReadRecord((UCHAR)n,RRM_Absolute,resApdu);
		CString cstrName = Helper::Gsm11EncStrToCStr(pRecData,(int)(m_RecordLen-14));
		CString phonenum = Helper::PhoneNumToAscii(&pRecData[m_RecordLen-15+2],
							pRecData[m_RecordLen-15+1]);
		CString cstrNum;
		cstrNum.Format("%d",n);
		if(newRecord)
			m_AdnListCtrl.AddItem(nItem,0,cstrNum);
		m_AdnListCtrl.AddItem(nItem,1,cstrName);
		m_AdnListCtrl.AddItem(nItem,2,phonenum);
		void *ItemData = (void *)((SCLIST_ITEMDATA *)m_AdnListCtrl.GetItemData(nItem))->ItemData;
		if(ItemData)
			free(ItemData);
		((SCLIST_ITEMDATA *)m_AdnListCtrl.GetItemData(nItem))->ItemData = (DWORD)pRecData;
		((SCLIST_ITEMDATA *)m_AdnListCtrl.GetItemData(nItem))->dirty = FALSE;
		m_AdnListCtrl.Update(nItem);
	}catch(LONG){
		throw;
	}
}

void CScAdnPage::WriteRecordN(int n,BOOL newRecord)
{
	try{
		int nItem = n-1;
		CPcScCtrl &ScReader = ((CAgsm2Dlg *)(AfxGetApp()->m_pMainWnd))->m_SCReader;
		LPBYTE pRecData = (LPBYTE)malloc(m_RecordLen);

		CString strName = m_AdnListCtrl.GetItemText(nItem,1);
		CString strPhoneNum = m_AdnListCtrl.GetItemText(nItem,2);
		TRACE("=============Write to card %d================\n",n);
		TRACE("name=%s\n",strName);
		TRACE("phone=%s\n",strPhoneNum);
		Helper::CStrToGsm11EncStr(strName,pRecData,m_RecordLen-14);
		Helper::CStrToCalledPartyBcd(strPhoneNum,pRecData+m_RecordLen-14,14);
		if(m_Sof == EF_CYCLIC)
			ScReader.UpdateRecord(0,RRM_PreviousRecord,pRecData,(UCHAR)m_RecordLen);
		else
			ScReader.UpdateRecord(n,RRM_Absolute,pRecData,(UCHAR)m_RecordLen);
		((SCLIST_ITEMDATA *)m_AdnListCtrl.GetItemData(nItem))->dirty = FALSE;
		free(pRecData);
	}catch(LONG)
	{
		throw;
	}
}

void CScAdnPage::SetFileId(USHORT id)
{
	m_FileId = id;

}

⌨️ 快捷键说明

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