📄 phonelistbox.cs
字号:
//Filename : PhoneListBox.cs
//Part of : Phone Navigator C# example
//Description : Implementation of phone navigator'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.
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
namespace CSFileBrowser.NET
{
using PCCSErrors;
using PCCAPIUtils;
using CONADefinitions;
using CONADeviceManagement;
using CONAFileSystem;
//===================================================================
// PhoneListBox
public class PhoneListBox : System.Windows.Forms.ListBox
{
// "\\" means phone root
const string g_strPhoneRoot = "\\\\";
private bool bDisposed = false;
private int m_wState;
private string m_strCurrentFolder;
private string m_strCurrentSN;
private string m_strCurrentFriendlyName;
private string[] ItemData;
// Device management handle
private int m_hDMHandle;
private CONADefinitions.DeviceNotifyCallbackDelegate pfnCallBack;
private string[] m_strSerialNumbers;
private string[] m_strFriendlyNames;
[MarshalAs(UnmanagedType.LPWStr)]
string pstrMemoryTypes;
//===================================================================
// Constructor
//
//===================================================================
public PhoneListBox()
{
int ret;
// Initialize Device Management APi
int iRet = CONADeviceManagement.DMAPI_Initialize(CONADeviceManagement.DMAPI_VERSION_32, 0);
if (iRet != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("DMAPI_Initialize", iRet);
m_strCurrentFolder = "";
// Get Device management handle
ret = CONADeviceManagement.CONAOpenDM(ref m_hDMHandle);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONAOpenDM", ret);
// Register device notification callback function
Common.pDeviceCallBack = Common.DeviceNotifyCallback;
ret = CONADeviceManagement.CONARegisterNotifyCallback(m_hDMHandle, CONADefinitions.API_REGISTER, Common.pDeviceCallBack);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONARegisterNotifyCallback", ret);
}
//===================================================================
// Destructor
//
//===================================================================
protected override void Dispose(bool disposing)
{
int ret;
if (!bDisposed)
{
if (m_hDMHandle != 0)
{
// Unregister device notification callback function
ret = CONADeviceManagement.CONARegisterNotifyCallback(m_hDMHandle, CONADefinitions.API_UNREGISTER, Common.pDeviceCallBack);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONARegisterNotifyCallback", ret);
// Close device management handle
ret = CONADeviceManagement.CONACloseDM(m_hDMHandle);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONACloseDM", ret);
}
// Terminate Device Management APi
int iRet = CONADeviceManagement.DMAPI_Terminate(0);
if (iRet != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("DMAPI_Terminate", iRet);
base.Dispose(disposing);
}
bDisposed = true;
}
//===================================================================
// PhoneListBox_DoubleClick
//
// Called when user doubleclicks list item
// If state = PHONE_LIST
// Show phone file system root
// Else
// If item is [..], shows parent folder (if root change to phone list)
// If item is [foldername], shows subfolder
//
//===================================================================
protected override void OnDoubleClick(EventArgs e)
{
if (m_wState == Common.PHONELIST_STATE_PHONELIST)
{
m_strCurrentSN = GetCurrentSN();
m_strCurrentFriendlyName = GetCurrentFriendlyName();
if (m_strCurrentSN.Length > 0)
{
ResetContent();
// removing phones
m_wState = Common.PHONELIST_STATE_PHONECONTENT;
ShowPhoneFolder(g_strPhoneRoot);
}
}
else if (m_wState == Common.PHONELIST_STATE_PHONECONTENT)
{
string strSelectedTxt;
string strCurrentFolder;
int index = SelectedIndex;
if (index != -1)
{
strSelectedTxt = Items[SelectedIndex].ToString();
if (strSelectedTxt == "[..]")
{
// go up in folder tree
strCurrentFolder = GetSelectedFolder();
if (strCurrentFolder == g_strPhoneRoot)
{
// root folder --> show phone list
ResetContent();
m_strCurrentFolder = "";
m_wState = Common.PHONELIST_STATE_PHONELIST;
ListAllPhones();
}
else
{
// getting parent folder of strCurrentFolder:
if (strCurrentFolder != g_strPhoneRoot)
{
strCurrentFolder = strCurrentFolder.Substring(0, strCurrentFolder.LastIndexOf("\\"));
if (strCurrentFolder == "\\")
{
// move to root folder
strCurrentFolder = g_strPhoneRoot;
}
}
ShowPhoneFolder(strCurrentFolder);
}
}
else if (strSelectedTxt.Substring(0, 1) == "[")
{
// selected item is folder
ShowPhoneFolder(GetSelectedFolder());
}
else
{
// selected item is file
Trace.WriteLine("PhoneListBox::PhoneItemDblClicked(): Double clicked file %s --> ignoring...\\n", strSelectedTxt);
}
}
}
}
//===================================================================
// ShowFolders
//
// Adds all found folders to listbox by using CONAFindNextFolder.
//
//===================================================================
public void ShowFolders(int hFindHandle)
{
CONADefinitions.CONAPI_FOLDER_INFO FolderInfo;
int iResult = 0;
// Allocate memory for buffer
IntPtr Buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CONADefinitions.CONAPI_FOLDER_INFO)));
iResult = CONAFileSystem.CONAFindNextFolder(hFindHandle, Buffer);
while (iResult == PCCSErrors.CONA_OK)
{
// Copy data from buffer
FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
int index = this.Items.Add("[" + FolderInfo.pstrName + "]");
// Setting folder name as itemdata
Array.Resize(ref ItemData, Items.Count + 1);
ItemData[index] = FolderInfo.pstrName;
iResult = CONAFileSystem.CONAFreeFolderInfoStructure(Buffer);
if (iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowFolders(): CONAFreeFolderInfoStructure failed", iResult);
}
iResult = CONAFileSystem.CONAFindNextFolder(hFindHandle, Buffer);
}
if (iResult != PCCSErrors.ECONA_ALL_LISTED & iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowFolders(): CONAFindNextFolder failed!", iResult);
}
}
//===================================================================
// ShowFiles
//
// Adds all found files to listbox by using CONAFindNextFile.
//
//===================================================================
public void ShowFiles(int hFindHandle)
{
CONADefinitions.CONAPI_FILE_INFO FileInfo;
int iResult;
// Allocate memory for buffer
IntPtr Buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CONADefinitions.CONAPI_FILE_INFO)));
iResult = CONAFileSystem.CONAFindNextFile(hFindHandle, Buffer);
while (iResult == PCCSErrors.CONA_OK)
{
// Copy data from buffer
FileInfo = (CONADefinitions.CONAPI_FILE_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FILE_INFO));
int index = this.Items.Add(FileInfo.pstrName);
// Setting file name as itemdata
Array.Resize(ref ItemData, Items.Count + 1);
ItemData[index] = FileInfo.pstrName;
iResult = CONAFileSystem.CONAFreeFileInfoStructure(Buffer);
if (iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowFiles(): CONAFreeFileInfoStructure failed!", iResult);
}
iResult = CONAFileSystem.CONAFindNextFile(hFindHandle, Buffer);
}
if (iResult != PCCSErrors.ECONA_ALL_LISTED & iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowFiles(): CONAFindNextFile failed!", iResult);
}
}
//===================================================================
// ShowPhoneFolder
//
// Adds all files and folders to listbox by using
// functions CONAFindBegin and CONAFindEnd.
//
//===================================================================
public void ShowPhoneFolder(string strFolder)
{
int hFS = 0;
int hFind = 0;
int iMedia = CONADefinitions.API_MEDIA_ALL;
int iDeviceID = 0;
int iResult = CONAFileSystem.CONAOpenFS(m_strCurrentSN, ref iMedia, ref hFS, ref iDeviceID);
if (iResult == PCCSErrors.CONA_OK)
{
int iFindOtions = 0;
if (Common.MainForm.UseCache())
iFindOtions = CONADefinitions.CONA_FIND_USE_CACHE;
iResult = CONAFileSystem.CONAFindBegin(hFS, iFindOtions, ref hFind, strFolder);
if (iResult == PCCSErrors.CONA_OK)
{
ResetContent();
this.Items.Add("[..]");
ShowFolders(hFind);
ShowFiles(hFind);
m_strCurrentFolder = strFolder;
iResult = CONAFileSystem.CONAFindEnd(hFind);
if (iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFindEnd failed!", iResult);
}
}
else
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFindBegin() failed!", iResult);
ListAllPhones();
}
iResult = CONAFileSystem.CONACloseFS(hFS);
if (iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONACloseFS failed!", iResult);
}
}
else
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAOpenFS failed!", iResult);
}
CONADefinitions.CONAPI_DEVICE Device;
// Allocate memory for buffer
IntPtr Buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CONADefinitions.CONAPI_DEVICE)));
iResult = CONADeviceManagement.CONAGetDevice(m_hDMHandle, m_strCurrentSN, Buffer);
if (iResult == PCCSErrors.CONA_OK)
{
// Copy data from buffer
Device = (CONADefinitions.CONAPI_DEVICE)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_DEVICE));
string mFN = Device.pstrFriendlyName;
Common.MainForm.LBL_PhoneFiles.Text = mFN + ":" + strFolder;
iResult = CONADeviceManagement.CONAFreeDeviceStructure(1, Buffer);
if (iResult != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFreeDeviceStructure failed!", iResult);
}
}
}
//===================================================================
// GetCurrentSN
//
// Returns serial number of currently selected phone
//
//===================================================================
public string GetCurrentSN()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -