📄 phonelistbox.cpp
字号:
/*
Filename : PhoneListBox.cpp
Part of : Phone Navigator
Description : Implementation of phone list box of PhoneNavigator
Version : 3.2
This example is only to be used with PC Connectivity API version 3.2
Compability ("as is") with future versions is not quaranteed.
Copyright (c) 2007 Nokia Corporation.
This material, including but not limited to documentation and any related
computer programs, is protected by intellectual property rights of Nokia
Corporation and/or its licensors.
All rights are reserved. Reproducing, modifying, translating, or
distributing any or all of this material requires the prior written consent
of Nokia Corporation. Nokia Corporation retains the right to make changes
to this material at any time without notice. A copyright license is hereby
granted to download and print a copy of this material for personal use only.
No other license to any other intellectual property rights is granted. The
material is provided "as is" without warranty of any kind, either express or
implied, including without limitation, any warranty of non-infringement,
merchantability and fitness for a particular purpose. In no event shall
Nokia Corporation be liable for any direct, indirect, special, incidental,
or consequential loss or damages, including but not limited to, lost profits
or revenue,loss of use, cost of substitute program, or loss of data or
equipment arising out of the use or inability to use the material, even if
Nokia Corporation has been advised of the likelihood of such damages occurring.
*/
#include "stdafx.h"
#include "PhoneNavigator.h"
#include "PhoneListBox.h"
#include "DeviceInfoDlg.h"
#include "RenameDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//===================================================================
// CPhoneListBox
//===================================================================
// Constructor
//
//===================================================================
CPhoneListBox::CPhoneListBox():CListBox()
{
m_pLabel = NULL;
TRACE(L"CPhoneListBox::CPhoneListBox(): Calling CONAOpenDM...");
DWORD dwResult = CONAOpenDM(&m_hDMHandle);
if(dwResult != CONA_OK)
{
TRACE(L"CPhoneListBox::CPhoneListBox(): CONAOpenDM failed\n");
ErrorMessageDlg(L"CPhoneListBox::CPhoneListBox(): CONAOpenDM failed", dwResult);
}
else
{
TRACE(L"CPhoneListBox::CPhoneListBox(): CONAOpenDM succeeded\n");
}
dwResult = CONARegisterDMNotifyIF(m_hDMHandle, API_REGISTER, this);
if(dwResult != CONA_OK)
{
TRACE(L"CPhoneListBox::CPhoneListBox(): StartListening failed\n");
ErrorMessageDlg(L"CPhoneListBox::CPhoneListBox(): StartListening failed", dwResult);
}
else
{
TRACE(L"CPhoneListBox::CPhoneListBox(): StartListening succeeded\n");
}
}
//===================================================================
// Destructor
//
//===================================================================
CPhoneListBox::~CPhoneListBox()
{
TRACE(L"CPhoneListBox::~CPhoneListBox()\n");
DWORD dwResult = CONACloseDM(m_hDMHandle);
if(dwResult != CONA_OK)
{
TRACE(L"CPhoneListBox::~CPhoneListBox(): CONACloseDM failed\n");
ErrorMessageDlg(L"CPhoneListBox::~CPhoneListBox(): CONACloseDM failed", dwResult);
}
else
{
TRACE(L"CPhoneListBox::~CPhoneListBox(): CONACloseDM succeeded\n");
}
}
BEGIN_MESSAGE_MAP(CPhoneListBox, CListBox)
//{{AFX_MSG_MAP(CPhoneListBox)
ON_WM_DESTROY()
ON_WM_LBUTTONDBLCLK()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//===================================================================
// Init
//
// Sets member variable m_pLabel
//
//===================================================================
void CPhoneListBox::Init(CStatic* pLabel)
{
// Set static control to write status information
m_pLabel = pLabel;
ListAllPhones();
}
//===================================================================
// GetCurrentSN
//
// Returns serial number of currently selected phone
//
//===================================================================
CString CPhoneListBox::GetCurrentSN()
{
CString strRetVal = L"";
// phone list is shown
int index = GetCurSel();
if(index != LB_ERR)
{ // there is a selected item
CONAPI_DEVICE *pDevice = (CONAPI_DEVICE*)GetItemDataPtr(index);
if(pDevice)
{ // copying serial number to strRetVal
strRetVal = CString(pDevice->pstrSerialNumber);
}
}
return strRetVal;
}
//===================================================================
// AddPhone
//
// Adds phone defined in CONAPI_DEVICE struct
// to list box
//
//===================================================================
int CPhoneListBox::AddPhone(CONAPI_DEVICE* pDevice)
{
TRACE(L"CPhoneListBox::AddPhone(): Begin\n");
// Write content of pDevice to output window:
TRACE(L"CPhoneListBox::AddPhone(): CONAPI_DEVICE\n");
TRACE(L"\tSerial number: %s\n", pDevice->pstrSerialNumber);
TRACE(L"\tFriendly name: %s\n", pDevice->pstrFriendlyName);
TRACE(L"\tModel: %s\n", pDevice->pstrModel);
TRACE(L"\tManufacturer: %s\n", pDevice->pstrManufacturer);
TRACE(L"\tNumberOfItems: %d\n", pDevice->dwNumberOfItems);
for(DWORD i = 0; i < pDevice->dwNumberOfItems; i++)
{
TRACE(L"\t\tConnection %d, DeviceID: %d\n", i + 1, pDevice->pItems[i].dwDeviceID);
TRACE(L"\t\tConnection %d, DeviceName: %s\n", i + 1, pDevice->pItems[i].pstrDeviceName);
if(pDevice->pItems[i].dwMedia == API_MEDIA_BLUETOOTH)
TRACE(L"\t\tConnection %d, DeviceName : %s\n", i + 1, pDevice->pItems[i].pstrDeviceName);
TRACE(L"\t\tConnection %d, State: %d\n", i + 1, pDevice->pItems[i].dwState);
}
CString strPhoneinfotxt;
strPhoneinfotxt = pDevice->pstrFriendlyName;
int index = AddString(strPhoneinfotxt);
// Copy pDevice to item data of listbox item:
CONAPI_DEVICE *pDeviceCopy = new CONAPI_DEVICE;
pDeviceCopy->pstrSerialNumber = new WCHAR[MAX_PATH];
pDeviceCopy->pstrFriendlyName = new WCHAR[MAX_PATH];
pDeviceCopy->pstrModel = new WCHAR[MAX_PATH];
pDeviceCopy->pstrManufacturer = new WCHAR[MAX_PATH];
pDeviceCopy->dwNumberOfItems = pDevice->dwNumberOfItems;
pDeviceCopy->pItems = 0;
wcsncpy_s(pDeviceCopy->pstrSerialNumber, MAX_PATH, pDevice->pstrSerialNumber, MAX_PATH);
wcsncpy_s(pDeviceCopy->pstrFriendlyName, MAX_PATH, pDevice->pstrFriendlyName, MAX_PATH);
wcsncpy_s(pDeviceCopy->pstrModel, MAX_PATH, pDevice->pstrModel, MAX_PATH);
wcsncpy_s(pDeviceCopy->pstrManufacturer, MAX_PATH, pDevice->pstrManufacturer, MAX_PATH);
SetItemDataPtr(index, pDeviceCopy);
TRACE(L"CPhoneListBox::AddPhone(): End\n");
return index;
}
//===================================================================
// ListAllPhones
//
// Adds all connected phones to list box
//
//===================================================================
void CPhoneListBox::ListAllPhones()
{
TRACE(L"CPhoneListBox::ListAllPhones(): Begin\n");
DWORD dwDeviceCount = 0;
DWORD dwIndex = 0;
CONAPI_DEVICE* pDevices = NULL;
CString strText = L"";
CSize maxSize(0,0), textSize(0,0);
ResetContent();
// Querying count of connected devices
DWORD dwResult = CONAGetDeviceCount(m_hDMHandle, &dwDeviceCount);
if(dwResult == CONA_OK)
{
if(dwDeviceCount > 0)
{
// Get list of currently connected devices
pDevices = new CONAPI_DEVICE[dwDeviceCount];
dwResult = CONAGetDevices(m_hDMHandle, &dwDeviceCount, pDevices);
if(dwResult == CONA_OK)
{
// Add each device to the phone list box
while ( dwIndex < dwDeviceCount )
{
int idx = AddPhone(&pDevices[dwIndex]);
// Update horizontal scrollbar width
GetText(idx, strText);
textSize = GetDC()->GetOutputTextExtent(strText);
if (maxSize.cx < textSize.cx)
{
SetHorizontalExtent(textSize.cx);
maxSize = textSize;
}
dwIndex++;
}
// CONAGetDevices allocates memory for the member variables in
// CONAPI_DEVICE and it is callers responsibility to free it...
dwResult = CONAFreeDeviceStructure(dwDeviceCount, pDevices);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ListAllPhones(): CONAFreeDeviceStructure failed!", dwResult);
}
// Select first item
SetCurSel(0);
}
else
{ // CONAGetDevices failed
ErrorMessageDlg(L"CPhoneListBox::ListAllPhones(): CONAGetDevices failed!", dwResult);
}
// freeing allocated memory
delete[] pDevices;
pDevices = NULL;
}
}
else
{ // CONAGetDeviceCount failed
ErrorMessageDlg(L"CPhoneListBox::ListAllPhones(): CONAGetDeviceCount failed!", dwResult);
}
if(m_pLabel)
{
m_pLabel->SetWindowText(L"Connected phones:");
}
TRACE(L"CPhoneListBox::ListAllPhones(): End\n");
}
//===================================================================
// ResetContent
//
// Freeing allocated memory
//
//===================================================================
void CPhoneListBox::ResetContent()
{
for(int i = 0; i < GetCount(); i++)
{
// contains phones --> let's delete CONAPI_DEVICE struct
CONAPI_DEVICE* pDevice = (CONAPI_DEVICE*)GetItemDataPtr(i);
if(pDevice)
{
delete[] pDevice->pstrSerialNumber;
delete[] pDevice->pstrFriendlyName;
delete[] pDevice->pstrModel;
delete[] pDevice->pstrManufacturer;
delete pDevice;
pDevice = NULL;
}
}
CListBox::ResetContent();
}
//===================================================================
// ShowCurrentDeviceInfo
//
// Shows a general device info dialog
//===================================================================
void CPhoneListBox::ShowCurrentDeviceInfo()
{
if (GetCurSel() != LB_ERR)
{
// Get selected device info
CONAPI_DEVICE_GEN_INFO* pInfo = NULL;
DWORD dwResult = CONAGetDeviceInfo(
m_hDMHandle,
GetCurrentSN(),
CONAPI_DEVICE_GENERAL_INFO,
(LPVOID*)&pInfo);
if (dwResult == CONA_OK)
{
// Create and show the general device info dialog
CDeviceInfoDlg dlg(pInfo);
dlg.DoModal();
}
else
{
ErrorMessageDlg(L"CPhoneListBox::ShowCurrentDeviceInfo(): CONAGetDeviceInfo failed!", dwResult);
}
if (pInfo != NULL)
{
// Release allocated resources
dwResult = CONAFreeDeviceInfoStructure(CONAPI_DEVICE_GENERAL_INFO, (LPVOID)pInfo);
if (dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ShowCurrentDeviceInfo(): CONAFreeDeviceInfoStructure failed!", dwResult);
}
}
}
else
{
AfxMessageBox(L"Please select a phone.");
}
}
void CPhoneListBox::RenameCurrentPhone()
{
TRACE(L"CPhoneListBox::RenameCurrentPhone(): Begin\n");
BOOL bIsFile = TRUE; // selected item is file - not folder
if(GetCurSel() != LB_ERR)
{
CRenameDlg dlg;
CONAPI_DEVICE* pDevice = (CONAPI_DEVICE*)GetItemDataPtr(GetCurSel());
dlg.m_strNewName = pDevice->pstrFriendlyName;
if(dlg.DoModal() == IDOK)
{
WCHAR szNewName[MAX_PATH] = {0};
wcsncpy_s(szNewName, dlg.m_strNewName, MAX_PATH);
TRACE(L"CPhoneListBox::RenameCurrentPhone(): %s --> %s, Calling RenameFriendlyName(%s, %s)...\n", dlg.m_strNewName, szNewName, pDevice->pstrSerialNumber, szNewName);
DWORD dwResult = CONARenameFriendlyName(m_hDMHandle, pDevice->pstrSerialNumber, szNewName);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::RenameCurrentPhone(): RenameFriendlyName failed!", dwResult);
}
else
{
ListAllPhones();
}
}
}
else
{
AfxMessageBox(L"Please select phone to be renamed.");
}
TRACE(L"CPhoneListBox::RenameCurrentPhone(): End\n");
}
//===================================================================
// CPhoneListBox message handlers
//===================================================================
// OnDestroy
//
// Called before destruction
//
//===================================================================
void CPhoneListBox::OnDestroy()
{
ResetContent();
CListBox::OnDestroy();
}
//===================================================================
// OnLButtonDblClk
//
// Called when user doubleclicks list item
//
//===================================================================
void CPhoneListBox::OnLButtonDblClk(UINT nFlags, CPoint point)
{
ShowCurrentDeviceInfo();
CListBox::OnLButtonDblClk(nFlags, point);
}
//===================================================================
// CCONADeviceNotify implementation
void CPhoneListBox::OnDeviceListUpdated()
{
TRACE(L"CPhoneListBox::DeviceListUpdated()\n");
ListAllPhones();
}
void CPhoneListBox::OnDeviceAdded(const WCHAR* pstrSN, DWORD dwStatus)
{
DWORD event = GET_CONAPI_CB_STATUS(dwStatus);
DWORD info = GET_CONAPI_CB_INFO(dwStatus);
DWORD infodata = GET_CONAPI_CB_INFO_DATA(dwStatus);
VERIFY(event == CONAPI_DEVICE_ADDED);
VERIFY(info == CONAPI_CONNECTION_ADDED);
TRACE(L"CPhoneListBox::DeviceAdded(%s, %i), %i\n", pstrSN, dwStatus, infodata);
ListAllPhones();
}
void CPhoneListBox::OnDeviceRemoved(const WCHAR* pstrSN, DWORD dwStatus)
{
TRACE(L"CPhoneListBox::DeviceRemoved(%s, %i)\n", pstrSN, dwStatus);
ListAllPhones();
}
void CPhoneListBox::OnDeviceUpdated(const WCHAR* pstrSN, DWORD dwStatus)
{
TRACE(L"CPhoneListBox::DeviceUpdated(%s, %i)\n", pstrSN, dwStatus);
ListAllPhones();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -