📄 phonelistbox.cpp
字号:
/*
Filename : PhoneListBox.cpp
Part of : File Browser
Description : Implementation of File Browser's phone list box
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 "FileBrowser.h"
#include "FileBrowserDlg.h"
#include "PhoneListBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
const CString g_strPhoneRoot = L"\\\\"; // '\\' means phone root
//===================================================================
// CPhoneListBox
//===================================================================
// Constructor
//
//===================================================================
CPhoneListBox::CPhoneListBox():CListBox()
{
m_wState = PHONELIST_STATE_PHONELIST;
m_hFS = NULL;
m_strCurrentSN = L"";
m_strCurrentFolder = L"";
m_pLabel = NULL;
TRACE(L"CPhoneListBox::CPhoneListBox(): Calling CONAOpenDM...");
DWORD dwResult = CONAOpenDM(&m_hDM);
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_hDM, API_REGISTER, this);
if(dwResult != CONA_OK)
{
TRACE(L"CPhoneListBox::CPhoneListBox(): CONARegisterDMNotifyIF() failed\n");
ErrorMessageDlg(L"CPhoneListBox::CPhoneListBox(): CONARegisterDMNotifyIF() failed", dwResult);
}
else
{
TRACE(L"CPhoneListBox::CPhoneListBox(): CONARegisterDMNotifyIF() succeeded\n");
}
}
//===================================================================
// Destructor
//
//===================================================================
CPhoneListBox::~CPhoneListBox()
{
TRACE(L"CPhoneListBox::~CPhoneListBox()\n");
DWORD dwResult = CONARegisterDMNotifyIF(m_hDM, API_UNREGISTER, this);
if(dwResult != CONA_OK)
{
TRACE(L"CPhoneListBox::~CPhoneListBox(): CONARegisterDMNotifyIF() failed\n");
ErrorMessageDlg(L"CPhoneListBox::~CPhoneListBox(): CONARegisterDMNotifyIF() failed", dwResult);
}
else
{
TRACE(L"CPhoneListBox::~CPhoneListBox(): CONARegisterDMNotifyIF() 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;
}
//===================================================================
// PostNcDestroy
//
// Overridden for cleanup
//
//===================================================================
void CPhoneListBox::PostNcDestroy()
{
// Close phone file system handle
CloseFS(m_hFS);
// Close device management handle
DWORD dwResult = CONACloseDM(m_hDM);
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");
}
__super::PostNcDestroy();
}
//===================================================================
// PhoneItemDblClicked
//
// Called, when user has doubleclicked phone item
// If item is [..], shows parent folder
// If item is [foldername], shows subfolder
//
//===================================================================
void CPhoneListBox::PhoneItemDblClicked()
{
CString strSelectedTxt, strCurrentFolder;
int index = GetCurSel();
if(index != LB_ERR)
{
GetText(index, strSelectedTxt);
if(strSelectedTxt == L"[..]")
{ // go up in folder tree
if((strCurrentFolder = GetSelectedFolder()) == g_strPhoneRoot)
{ // root folder --> show phone list
ResetContent();
m_wState = PHONELIST_STATE_PHONELIST;
ListAllPhones();
}
else
{
// getting parent folder of strCurrentFolder:
if(strCurrentFolder != g_strPhoneRoot)
{
strCurrentFolder = strCurrentFolder.Left(strCurrentFolder.ReverseFind('\\'));
if(strCurrentFolder == L"\\")
{ // move to root folder
strCurrentFolder = g_strPhoneRoot;
}
}
ShowPhoneFolder(strCurrentFolder);
}
}
else if(strSelectedTxt[0] == '[')
{ // selected item is folder
ShowPhoneFolder(GetSelectedFolder());
}
else
{ // selected item is file
TRACE(L"CPhoneListBox::PhoneItemDblClicked(): Double clicked file %s --> ignoring...\n", strSelectedTxt);
}
}
else
{
TRACE(L"CPhoneListBox::PhoneItemDblClicked(): No list box item selected.\n");
}
}
//===================================================================
// ShowFolders
//
// Adds all found folders to listbox by using CONAFindNextFolder.
//
//===================================================================
void CPhoneListBox::ShowFolders(FINDHANDLE hFindHandle)
{
CONAPI_FOLDER_INFO FolderInfo = {0};
DWORD dwResult = 0;
while((dwResult = CONAFindNextFolder(hFindHandle, &FolderInfo)) == CONA_OK)
{
CTime time = CTime(FolderInfo.tFolderTime);
TRACE(L"Folder: %s\t%s\tModified: %s\tLabel: %s\tMemory type: %s\n",
FolderInfo.pstrName,
Permissions2String(FolderInfo.dwAttributes),
time.Format(L"%d.%m.%Y %H:%M:%S"),
FolderInfo.pstrLabel,
FolderInfo.pstrMemoryType);
int index = AddString(L"[" + CString(FolderInfo.pstrName) + L"]");
// Setting folder name as itemdata
CString* pItemData = new CString(FolderInfo.pstrName);
SetItemDataPtr(index, pItemData);
dwResult = CONAFreeFolderInfoStructure(&FolderInfo);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ShowFolders(): CONAFreeFolderinfoStructure failed", dwResult);
}
}
if(dwResult != ECONA_ALL_LISTED)
{
ErrorMessageDlg(L"CPhoneListBox::ShowFolders(): CONAFindNextFolder failed!", dwResult);
}
}
//===================================================================
// ShowFolders
//
// Adds all found files to listbox by using CONAFindNextFile.
//
//===================================================================
void CPhoneListBox::ShowFiles(FINDHANDLE hFindHandle)
{
CONAPI_FILE_INFO FileInfo = {0};
DWORD dwResult = 0;
while((dwResult = CONAFindNextFile(hFindHandle, &FileInfo)) == CONA_OK)
{
CTime time = CTime(FileInfo.tFileTime);
CString strAttributes = L"";
TRACE(L"File: %s\t%s\tSize: %d\tModified: %s\tMIMEType: %s\n",
FileInfo.pstrName,
Permissions2String(FileInfo.dwAttributes),
FileInfo.dwFileSize,
time.Format(L"%d.%m.%Y %H:%M:%S"),
CString(FileInfo.pstrMIMEType));
int index = AddString(FileInfo.pstrName);
// Setting file name as itemdata
CString* pItemData = new CString(FileInfo.pstrName);
SetItemDataPtr(index, pItemData);
dwResult = CONAFreeFileInfoStructure(&FileInfo);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ShowFiles(): CONAFreeFileInfoStructure failed!", dwResult);
}
}
if(dwResult != ECONA_ALL_LISTED)
{
ErrorMessageDlg(L"CPhoneListBox::ShowFiles(): CONAFindNextFile failed!", dwResult);
}
}
//===================================================================
// ShowPhoneFolder
//
// Adds all files and folders to listbox by using
// functions CONAFindBegin and CONAFindEnd.
//
//===================================================================
void CPhoneListBox::ShowPhoneFolder(CString strFolder)
{
TRACE(L"CPhoneListBox::ShowPhoneFolder(): Begin\n\tstrFolder = %s\n", strFolder);
FINDHANDLE hFind = NULL;
DWORD dwMedia = API_MEDIA_ALL;
DWORD dwDeviceID = 0;
DWORD dwFindOptions = 0;
if(((CFileBrowserDlg*)GetParent())->GetCacheOption())
{
dwFindOptions |= CONA_FIND_USE_CACHE;
}
DWORD dwResult = CONAFindBegin(m_hFS, dwFindOptions, &hFind, strFolder);
if(dwResult == CONA_OK)
{
ResetContent();
AddString(L"[..]");
ShowFolders(hFind);
ShowFiles(hFind);
m_strCurrentFolder = strFolder;
dwResult = CONAFindEnd(hFind);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ShowPhoneFolder(): CONAFindEnd failed!", dwResult);
}
}
else
{
CString strErrortxt;
strErrortxt.Format(L"CPhoneListBox::ShowPhoneFolder(): CONAFindBegin(%s) failed!\nm_hFS = 0x%X, hFind = 0x%X", strFolder, m_hFS, hFind);
ErrorMessageDlg(strErrortxt, dwResult);
ListAllPhones();
}
if(m_pLabel)
{
CONAPI_DEVICE Device = {0};
dwResult = CONAGetDevice(m_hDM, m_strCurrentSN, &Device);
if(dwResult == CONA_OK)
{
CString mFN = Device.pstrFriendlyName;
m_pLabel->SetWindowText(mFN + L":" + strFolder);
dwResult = CONAFreeDeviceStructure(1, &Device);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::ShowPhoneFolder(): CONAFreeDeviceStructure failed!", dwResult);
}
}
}
TRACE(L"CPhoneListBox::ShowPhoneFolder(): End\n");
}
//===================================================================
// GetCurrentSN
//
// Returns serial number of currently selected phone
//
//===================================================================
CString CPhoneListBox::GetCurrentSN()
{
CString strRetVal = L"";
if(m_wState == PHONELIST_STATE_PHONELIST)
{ // 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);
}
}
}
else
{ // user is browsing phone content
strRetVal = m_strCurrentSN;
}
return strRetVal;
}
//===================================================================
// GetState
//
// Returns current state of listbox
// (PHONELIST_STATE_PHONELIST or PHONELIST_STATE_PHONECONTENT)
//
//===================================================================
DWORD CPhoneListBox::GetState()
{
return m_wState;
}
//===================================================================
// GetFreeMemoryString
//
// Returns string containing free memory of "DEV"
// This function demonstrates use of functions
// CONARefreshDeviceMemoryValues, CONAGetMemoryTypes and
// CONAGetMemoryValues
//
//===================================================================
CString CPhoneListBox::GetFreeMemoryString(CONAPI_DEVICE* pDevice)
{
TRACE(L"CPhoneListBox::GetFreeMemoryString(): Begin\n");
CString strRetVal = L"";
CString strTmp = L"";
WCHAR* pstrMemoryTypes = NULL;
DWORD dwMedia = pDevice->pItems->dwMedia;
DWORD dwDeviceID = 0;
__int64 dwFreeMem = -1, dwTotalMem = -1, dwUsedMem = -1;
FSHANDLE hFS = OpenFS(pDevice->pstrSerialNumber);
if(!hFS)
{
return strRetVal;
}
// refreshing memory values
DWORD dwResult = CONARefreshDeviceMemoryValues(hFS);
if(dwResult != CONA_OK)
{
ErrorMessageDlg(L"CPhoneListBox::GetFreeMemoryString(): CONARefreshDeviceMemoryValues failed!", dwResult);
}
// Getting memory types of connected device
dwResult = CONAGetMemoryTypes(hFS, &pstrMemoryTypes);
if(dwResult == CONA_OK)
{
// go through memory type list:
WCHAR* separator = L",";
wchar_t* next_token;
bool bFirst = true;
WCHAR* token = wcstok_s(pstrMemoryTypes, separator, &next_token);
while(token != NULL)
{
dwResult = CONAGetMemoryValues(hFS, token, &dwFreeMem, &dwTotalMem, &dwUsedMem);
if(dwResult == CONA_OK)
{
TRACE(L"CPhoneListBox::GetFreeMemoryString(): %s: free %0.2f MB used %0.2f/%0.2f MB\n",
token, (double)dwFreeMem/1024/1024, (double)dwUsedMem/1024/1024, (double)dwTotalMem/1024/1024);
// let's show memory values of the memory type
if(bFirst)
{
bFirst = false;
strTmp.Format(L"%s: ", token);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -