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

📄 phonelistbox.cpp

📁 以前做NOKIA手机与PC通信时所参考的源代码,里面包括两个程序,一个是手机文件夹浏览源码,另一个手机SIS安装程序.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				else
				{
					strTmp.Format(L", %s: ", token);
				}
                if(dwFreeMem != -1)
                    strTmp.Format(strTmp + L"Free %0.2f MB", (double)dwFreeMem/1024/1024);
                if(dwTotalMem != -1 && dwUsedMem != -1)
                    strTmp.Format(strTmp + L", used %0.2f/%0.2f MB", 
						(double)dwUsedMem/1024/1024, (double)dwTotalMem/1024/1024);
				strRetVal += strTmp;
            }
            else
            { // CONAGetMemoryValues failed
                ErrorMessageDlg(L"CPhoneListBox::GetFreeMemoryString(): CONAGetMemoryValues failed!", dwResult);
            } 
            token = wcstok_s(NULL, separator, &next_token);
        }
        // pstrMemoryTypes was allocated by CONAGetMemoryTypes
        // It must be de-allocated
		dwResult = CONAFreeString(pstrMemoryTypes);
        if(dwResult != CONA_OK)
        {
            ErrorMessageDlg(L"CPhoneListBox::GetFreeMemoryString(): CONAFreeString failed!", dwResult);
        }
    }
    else
    { // CONAGetMemoryTypes failed
        ErrorMessageDlg(L"CPhoneListBox::GetFreeMemoryString(): CONAGetMemoryTypes failed!", dwResult);
    }
	if(pDevice->pstrSerialNumber == m_strCurrentSN)
	{
		m_hFS = hFS;
	}
	else
	{
		CloseFS(hFS);
	}
	TRACE(L"CPhoneListBox::GetFreeMemoryString(): End\n");
    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;
    CString strFreeMemoryStr = GetFreeMemoryString(pDevice);

    strPhoneinfotxt.Format(L"%s (%s)", 
        pDevice->pstrFriendlyName,
        strFreeMemoryStr);
    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();
    m_wState = PHONELIST_STATE_PHONELIST;

    // Querying count of connected devices
    DWORD dwResult = CONAGetDeviceCount(m_hDM, &dwDeviceCount);
    if(dwResult == CONA_OK)
    {
        if(dwDeviceCount > 0)
        {
		    // Get list of currently connected devices
		    pDevices = new CONAPI_DEVICE[dwDeviceCount];
		    dwResult = CONAGetDevices(m_hDM, &dwDeviceCount, pDevices);
		    if(dwResult == CONA_OK)
		    {
			    // Add each device to the phone list box
			    while ( dwIndex < dwDeviceCount )
			    {
					CString strSN = pDevices[dwIndex].pstrSerialNumber;
					if(dwIndex == 0)
					{
						m_strCurrentSN = strSN;
					}
                    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);
                }
		    }
            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(GetCount() > 0)
	{
		SetCurSel(0);
	}
    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++)
    { 
        if(m_wState == PHONELIST_STATE_PHONELIST)
        { // 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;
            }
        }
        else if(m_wState == PHONELIST_STATE_PHONECONTENT)
        { // contains phone content --> let's delete file/folder name
            CString* pstrName = (CString*)GetItemDataPtr(i);
            if(pstrName)
            {
	            delete pstrName;
                pstrName = NULL;
            }
        }
    }
    CListBox::ResetContent();
}

//===================================================================
// GetSelectedFolder
//
// Returns currently selected folder
// 
//===================================================================
CString CPhoneListBox::GetSelectedFolder()
{
    CString strRetVal = L"";
    // Let's check if there is phone folder selected
    int index = GetCurSel();
    if(m_wState == PHONELIST_STATE_PHONECONTENT && index != LB_ERR)
    { // there is a selected item
        strRetVal = m_strCurrentFolder;
        CString strSelectedTxt;
        GetText(index, strSelectedTxt);
        if(strSelectedTxt[0] == '[' && strSelectedTxt != L"[..]")
        { // folder [directoryname]
            if(strRetVal.Right(1) != L"\\")
                strRetVal += L"\\";
            strRetVal += *((CString*)GetItemDataPtr(index));
        }            
    }
    return strRetVal;
}

//===================================================================
// GetCurrentFolder
//
// Returns folder, the content of which is currently shown 
// 
//===================================================================
CString CPhoneListBox::GetCurrentFolder()
{
    return m_strCurrentFolder;
}

//===================================================================
// GetCurrentFile
//
// Returns selected file. Return empty string if no file is selected.
// 
//===================================================================
CString CPhoneListBox::GetCurrentFile()
{
    CString strRetVal = L"";
    int index = GetCurSel();
    if(m_wState == PHONELIST_STATE_PHONECONTENT && index != LB_ERR)
    { // there is a selected item
        CString strSelectedTxt;
        GetText(index, strSelectedTxt);
        if(strSelectedTxt[0] != '[')
        { // selected item is file
            strRetVal = *((CString*)GetItemDataPtr(index));
        }            
    }
    return strRetVal;
}

//===================================================================
// OpenFS
//
// Opens phone file system handle.
// 
//===================================================================
FSHANDLE CPhoneListBox::OpenFS(CString strSN)
{
	DWORD dwMedia = API_MEDIA_ALL;
	FSHANDLE hFS = NULL;
	DWORD dwDeviceID = 0;        
	DWORD dwResult = CONAOpenFS(strSN, &dwMedia, &hFS, &dwDeviceID);
	if(dwResult != CONA_OK)
	{
		ErrorMessageDlg(L"CPhoneListBox::OpenFS(): CONAOpenFS failed!", dwResult);
		return NULL;
	}    
	return hFS;
}

//===================================================================
// CloseFS
//
// Closes phone file system handle.
// 
//===================================================================
void CPhoneListBox::CloseFS(FSHANDLE hFS)
{
	if(hFS)
	{
		DWORD dwResult = CONACloseFS(hFS);
		if(dwResult != CONA_OK)
		{
			ErrorMessageDlg(L"CPhoneListBox::CloseFS(): CONACloseFS failed!", dwResult);
		}
	}
}

//===================================================================
// 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) 
{    
    switch(m_wState)
    {
    case PHONELIST_STATE_PHONELIST:
		{
			CString strCurrentSN = GetCurrentSN();
			if(!strCurrentSN.IsEmpty())
			{
				ResetContent(); // removing phones and cleaning memory
				if(strCurrentSN != m_strCurrentSN)
				{
					m_strCurrentSN = strCurrentSN;
					CloseFS(m_hFS);
					m_hFS = OpenFS(m_strCurrentSN);
				}
				if(m_hFS)
				{
					m_wState = PHONELIST_STATE_PHONECONTENT;
					ShowPhoneFolder(g_strPhoneRoot);
				}
			}
        }
        break;
    case PHONELIST_STATE_PHONECONTENT:
        PhoneItemDblClicked();
        break;
    }
	
	CListBox::OnLButtonDblClk(nFlags, point);
}

//===================================================================
// CCONADeviceNotify implementation

void CPhoneListBox::OnDeviceListUpdated()
{
    TRACE(L"CPhoneListBox::DeviceListUpdated()\n");
    if(m_wState == PHONELIST_STATE_PHONELIST)
    {
        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)\n", pstrSN, dwStatus);
    if(m_wState == PHONELIST_STATE_PHONELIST)
    {
        ListAllPhones();
    }
}

void CPhoneListBox::OnDeviceRemoved(const WCHAR* pstrSN, DWORD dwStatus)
{
    TRACE(L"CPhoneListBox::DeviceRemoved(%s, %i)\n", pstrSN, dwStatus);
    if(m_wState == PHONELIST_STATE_PHONELIST)
    {
        ListAllPhones();
    }
    else
    {
        if(CString(pstrSN) == GetCurrentSN())
        {
            AfxMessageBox(L"Connection to device lost!");
            ResetContent();
            m_wState = PHONELIST_STATE_PHONELIST;            
            ListAllPhones();
        }
    }
}

void CPhoneListBox::OnDeviceUpdated(const WCHAR* pstrSN, DWORD dwStatus)
{
    TRACE(L"CPhoneListBox::DeviceUpdated(%s, %i)\n", pstrSN, dwStatus);
    if(m_wState == PHONELIST_STATE_PHONELIST)
    {
        ListAllPhones();
    }
}

⌨️ 快捷键说明

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