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

📄 btserviceslistview.cpp

📁 蓝牙通迅服务,挺有用的东东,希望对大家有所帮助
💻 CPP
字号:
// BtServicesListView.cpp
//
// Copyright (c) 2003 Symbian Ltd.	All rights reserved.
//

#include <eikmenub.h>
#include <eiklabel.h>
#include <eikcmbut.h>
#include <eiktxlbm.h>
#include <eikchlst.h>
#include <eikedwin.h>

#include <qbtselectdlg.hrh>
#include <btservices.rsg>
#include "btserviceslistview.h"
#include "btservicesapp.h"
#include "btserviceseng.h"
#include "btservices.hrh"

static const TUint32 uuids[11] = {0x100,0x1,0x3,0x8,0x1000,0x1001,0x1002,0x1101,0x1105,0x1106,0x1108};

CBtServicesListView::CBtServicesListView(CBtServicesEng& aEngine)
: iEngine(aEngine)
	{
	}

CBtServicesListView::~CBtServicesListView()
	{
	delete iListBox;
	delete iSelectButton;
	delete iCancelButton;
	delete iRefreshButton;
	delete iDeviceLabel;
	delete iUUIDLabel;
	}

CBtServicesListView* CBtServicesListView::NewL(CBtServicesEng& aEngine)
	{
	CBtServicesListView* self = new(ELeave) CBtServicesListView(aEngine);
	CleanupStack::PushL(self);
	self->ConstructL();
	CleanupStack::Pop(self);
	return self;
	}

void CBtServicesListView::ConstructL()
	{
	iEngine.SetView(*this);
	CreateWindowL();

	// The views should be made invisible until ViewActivated has been called
	MakeVisible(EFalse);

	TRect rect = iEikonEnv->EikAppUi()->ClientRect();
	SetRect(rect);

	const TInt KLayoutWindowWidth = 208;
	const TInt KLayoutWindowHeight = 254;
	const TInt KLayoutStandardMargin = 8;
	const TInt KLayoutButtonHeight = 22;
	const TInt KLayoutStandardButtonWidth = 76;
	const TInt KLayoutWideButtonWidth = 108;
	const TInt KLayoutLabelHeight = 16;

	iListBox = new(ELeave) CEikTextListBox();
	iListBox->ConstructL(this, 0);
	iListBox->Model()->SetItemTextArray(iEngine.GetTextArray());
	iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
	iListBox->SetExtent(TPoint(KLayoutStandardMargin,KLayoutStandardMargin + 2 * KLayoutLabelHeight), TSize(KLayoutWindowWidth - 2 * KLayoutStandardMargin,KLayoutWindowHeight - 4 * KLayoutStandardMargin - 2 * KLayoutButtonHeight - 2 * KLayoutLabelHeight));
	iListBox->CreateScrollBarFrameL();
	iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	iListBox->SetFocus(ETrue);
	iListBox->SetListBoxObserver(this);

	iSelectButton = new(ELeave) CEikCommandButton();
	iSelectButton->SetContainerWindowL(*this);
	iSelectButton->SetTextL(*(iEikonEnv->AllocReadResourceLC(R_BUTTON_TEXT_SELECT)));
	CleanupStack::PopAndDestroy();	
	iSelectButton->SetExtent(TPoint(KLayoutStandardMargin,KLayoutWindowHeight - KLayoutStandardMargin - KLayoutButtonHeight), TSize(KLayoutWideButtonWidth, KLayoutButtonHeight));
	iSelectButton->SetObserver(this);

	iCancelButton = new(ELeave) CEikCommandButton();
	iCancelButton->SetContainerWindowL(*this);
	iCancelButton->SetTextL(*(iEikonEnv->AllocReadResourceLC(R_BUTTON_TEXT_CANCEL)));
	CleanupStack::PopAndDestroy();	
	iCancelButton->SetExtent(TPoint(2 * KLayoutStandardMargin + KLayoutWideButtonWidth, KLayoutWindowHeight - KLayoutStandardMargin - KLayoutButtonHeight), TSize(KLayoutStandardButtonWidth,KLayoutButtonHeight));
	iCancelButton->SetObserver(this);
	iCancelButton->SetDimmed(ETrue);

	iRefreshButton = new(ELeave) CEikCommandButton();
	iRefreshButton->SetContainerWindowL(*this);
	iRefreshButton->SetTextL(*(iEikonEnv->AllocReadResourceLC(R_BUTTON_TEXT_REFRESH)));
	CleanupStack::PopAndDestroy();	
	iRefreshButton->SetExtent(TPoint(2 * KLayoutStandardMargin + KLayoutWideButtonWidth, KLayoutWindowHeight - 2 * KLayoutStandardMargin - 2 * KLayoutButtonHeight), TSize(KLayoutStandardButtonWidth,KLayoutButtonHeight));
	iRefreshButton->SetObserver(this);
	iRefreshButton->SetDimmed(ETrue);

	iDeviceLabel = new(ELeave) CEikLabel();
	iDeviceLabel->SetContainerWindowL(*this);
	iDeviceLabel->SetExtent(TPoint(2 * KLayoutStandardMargin, KLayoutStandardMargin), TSize(KLayoutWindowWidth - 4 * KLayoutStandardMargin, KLayoutLabelHeight));
	iDeviceLabel->SetTextL(*(iEikonEnv->AllocReadResourceLC(R_LABEL_TEXT_DEVICE)));
	CleanupStack::PopAndDestroy();	

	iUUIDLabel = new(ELeave) CEikLabel();
	iUUIDLabel->SetContainerWindowL(*this);
	iUUIDLabel->SetExtent(TPoint(2 * KLayoutStandardMargin, KLayoutStandardMargin + KLayoutLabelHeight), TSize(KLayoutWindowWidth - 4 * KLayoutStandardMargin, KLayoutLabelHeight));
	HBufC* buf = HBufC::NewLC(KMaxDescriptorLength);
	TPtr uuidBuffer = buf->Des();
	uuidBuffer.Append(*(iEikonEnv->AllocReadResourceLC(R_LABEL_TEXT_UUIDFILTER)));
	CleanupStack::PopAndDestroy();
	iEngine.AppendUUIDText(uuidBuffer, TUUID(0)); // 0 means to use member variable (iUUIDFilter) of CBtServicesEng
	iUUIDLabel->SetTextL(uuidBuffer);
	CleanupStack::PopAndDestroy();

	SetComponentsToInheritVisibility();
	ActivateL();
	}

TVwsViewId CBtServicesListView::ViewId() const 
	{
	return TVwsViewId(KUidBtServices, KUidBtServicesListView);
	}

void CBtServicesListView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/,const TDesC8& /*aCustomMessage*/)
	{
	MEikAppUiFactory* factory = iEikonEnv->AppUiFactory();
	factory->MenuBar()->ChangeMenuBarL(0, R_BTSERVICES_LIST_MENUBAR, EFalse);
	MakeVisible(ETrue);
	}

void CBtServicesListView::ViewDeactivated()
	{
	MakeVisible(EFalse);
	}

CEikTextListBox& CBtServicesListView::ListBox() const
	{
	return *iListBox;
	}

CEikonEnv* CBtServicesListView::GetEikonEnv() const
	{
	return iEikonEnv;
	}

void CBtServicesListView::SelectDeviceL()
	{
	TInt err = iEngine.BluetoothReady();
	if (err != KErrNone)
		{
		_LIT(KMsgBluetoothNotReady,"Bluetooth Not Ready");
		_LIT(KMsgBlankLine,"");
		iEikonEnv->InfoWinL(KMsgBluetoothNotReady,KMsgBlankLine);
		return;
		}

	TBTDevAddr deviceAddress(0);
	TBTDeviceName deviceName;
	TBTDeviceClass deviceClass;
	
	CQBTUISelectDialog* dialog = new (ELeave) CQBTUISelectDialog(deviceAddress, deviceName, deviceClass, CQBTUISelectDialog::EQBTDeviceFilterAll);
	if(dialog->LaunchSingleSelectDialogLD() == EBTDeviceSelected)
		{
		_LIT(KDeviceLabelFormatString,"Device: %S");
		HBufC* buf = HBufC::NewLC(KMaxDescriptorLength);
		TPtr labelText = buf->Des();
		labelText.Format(KDeviceLabelFormatString,&deviceName);
		iDeviceLabel->SetTextL(labelText);
		CleanupStack::PopAndDestroy();
		iEngine.NewDeviceSelectedL(deviceAddress,deviceName,deviceClass);
		}
	}

void CBtServicesListView::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
	{
	if (aEventType == EEventStateChanged)
		{
		if (aControl == iSelectButton) // select a new device
			{
			iEikonEnv->BusyMsgCancel();
			iEngine.CancelSdpAgent();
			iEngine.DeleteSdpAgent();
			SelectDeviceL();
			}
		else if (aControl == iCancelButton && !iCancelButton->IsDimmed()) // cancel search (button is only un-dimmed when search is active)
			{
			iEikonEnv->BusyMsgCancel();
			TGulAlignment position(EHRightVTop);
			_LIT(KMsgCancelled,"Cancelled");
			iEikonEnv->InfoMsgWithAlignmentAndDuration(position,KMsgCancelled,KCancelMsgDuration);

			iEngine.CancelSdpAgent();
			DimCancelButton(ETrue);
			DimRefreshButton(EFalse);
			}
		else if (aControl == iRefreshButton && !iRefreshButton->IsDimmed()) // refresh display (button is dimmed if a search is in progress)
			{
			iEngine.GetDeviceServicesL();
			}
		}
	}

void CBtServicesListView::ConvertMenuChoiceToButtonL(const TInt aButton)
	{
	switch (aButton)
		{
	case EBtServicesRefreshList:
		HandleControlEventL(iRefreshButton,EEventStateChanged);
		break;
	case EBtServicesNewDevice:
		HandleControlEventL(iSelectButton,EEventStateChanged);
		break;
	default:
		break;
		}
	}

void CBtServicesListView::HandleListBoxEventL(CEikListBox* aListBox,TListBoxEvent /* aEventType */)
	{
	if (aListBox == iListBox) // just to check, shouldn't be any other list box, and no need to do anything if it is (no "else")
		{
		if (aListBox->CurrentItemIndex() != -1 && aListBox->Model()->NumberOfItems() != 0 && !iEngine.GettingAllAttributes()) // check that an item is selected (!=-1), there is at least one item, and that we're not already getting service attributes
			{
			iEikonEnv->BusyMsgCancel();
			iEngine.GetAttributesL(iListBox->CurrentItemIndex());
			}
		}
	}

void CBtServicesListView::Draw(const TRect& /* aRect */) const
	{
	CWindowGc& gc = SystemGc(); // Get graphics context
	gc.Clear();
	}

void CBtServicesListView::DimCancelButton(TBool aDimmed)
	{
	iCancelButton->SetDimmed(aDimmed);
	iCancelButton->DrawNow();
	}

void CBtServicesListView::DimRefreshButton(TBool aDimmed)
	{
	iRefreshButton->SetDimmed(aDimmed);
	iRefreshButton->DrawNow();
	}

TBool CBtServicesListView::RefreshButtonDimmed() const
	{
	return iRefreshButton->IsDimmed();
	}

void CBtServicesListView::SetUUIDTextL(const TDesC& aBuf)
	{
	iUUIDLabel->SetTextL(aBuf);
	}

TInt CBtServicesListView::CountComponentControls() const
	{
	return EMyControlCount;
	}

CCoeControl* CBtServicesListView::ComponentControl(TInt aIndex) const
	{
	switch (aIndex)
		{
	case EMyListBox:
		return iListBox;
	case EMySelectButton:
		return iSelectButton;
	case EMyCancelButton:
		return iCancelButton;
	case EMyDeviceLabel:
		return iDeviceLabel;
	case EMyRefreshButton:
		return iRefreshButton;
	case EMyUUIDLabel:
		return iUUIDLabel;
	default:
		return NULL;
		}
	}

void CBtServicesListView::DisplayDeviceInfoDialogL(const TBTDeviceName& aName, const TBTDevAddr& aAddress,const TBTDeviceClass& aClass)
	{
	CEikDialog* dialog = new(ELeave) CDeviceInfoDialog(aName, aAddress, aClass);
	(void) dialog->ExecuteLD(R_BTSERVICES_INFO_DIALOG);
	}

void CBtServicesListView::DisplayAttributeDialogL(CDesCArrayFlat* aArray, const TDesC& aDeviceName, const TDesC& aServiceName)
	{
	CEikDialog* dialog = new(ELeave) CAttributeDisplayDialog(aArray, aDeviceName, aServiceName);
	(void) dialog->ExecuteLD(R_BTSERVICES_ATTRIBUTES_DIALOG);
	}

TBool CBtServicesListView::DisplayUUIDSettingsDialogL(TUint32& aUUIDFilter)
	{
	CEikDialog* dialog = new(ELeave) CUUIDSettingsDialog(aUUIDFilter);
	return dialog->ExecuteLD(R_BTSERVICES_UUID_DIALOG);
	}

//////////////////////////////////////////
// CAttributeDisplayDialog functions
//////////////////////////////////////////

CAttributeDisplayDialog::CAttributeDisplayDialog(CDesCArrayFlat* aArray, const TDesC& aDeviceName, const TDesC& aServiceName)
: iArray(aArray), iDeviceName(aDeviceName), iServiceName(aServiceName)
	{
	}

void CAttributeDisplayDialog::PreLayoutDynInitL()
	{
	CEikTextListBox* listBox = static_cast<CEikTextListBox*>(Control(EBtAttributesListBox ));
	listBox->Model()->SetItemTextArray(iArray);
	listBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
	listBox->CreateScrollBarFrameL();
	listBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
	CEikLabel* deviceLabel = static_cast<CEikLabel*>(Control(EBtAttributesDeviceNameLabel));
	_LIT(KDevicePrefix,"Device: ");
	iDeviceName.Insert(0,KDevicePrefix);
	deviceLabel->SetTextL(iDeviceName);
	CEikLabel* serviceLabel = static_cast<CEikLabel*>(Control(EBtAttributesServiceLabel));
	_LIT(KServicePrefix,"Service: ");
	iServiceName.Insert(0,KServicePrefix);
	serviceLabel->SetTextL(iServiceName);
	}

//////////////////////////////////////////
// CUUIDSettingsDialog functions
//////////////////////////////////////////

CUUIDSettingsDialog::CUUIDSettingsDialog(TUint32& aUUIDFilter)
: iUUIDFilter(aUUIDFilter)
	{
	}

void CUUIDSettingsDialog::PreLayoutDynInitL()
	{
	CEikChoiceList* choiceList = static_cast<CEikChoiceList*>(Control( EUUIDDialogChoiceList));
	for (TInt i = 0; i < 11; i++)
		{
		if (uuids[i] == iUUIDFilter)
			{
			choiceList->SetCurrentItem(i);
			}
		}
	CEikEdwin* editor = static_cast<CEikEdwin*>(Control(EUUIDDialogNumberEditor));
	TBuf<10> numberBuf;
	numberBuf.AppendNum(iUUIDFilter,EHex);
	editor->SetTextL(&numberBuf);
	editor->DrawNow();
	}

void CUUIDSettingsDialog::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
	{
	CEikChoiceList* choiceList = static_cast<CEikChoiceList*>(Control( EUUIDDialogChoiceList ));
	CEikEdwin* editor = static_cast<CEikEdwin*>(Control(EUUIDDialogNumberEditor));

	if (aEventType == EEventStateChanged && static_cast<CCoeControl*>(choiceList) == aControl)
		{
		TInt index = choiceList->CurrentItem();
		iUUIDFilter = uuids[index];

		TBuf<KMaxUUIDTextBufferLength> numberBuf;
		numberBuf.AppendNum(iUUIDFilter,EHex);
		editor->SetTextL(&numberBuf);
		editor->DrawNow();
		}
	CEikDialog::HandleControlEventL( aControl, aEventType );
	}

TBool CUUIDSettingsDialog::OkToExitL(TInt /* aButtonID */)
	{
	CEikEdwin* editor = static_cast<CEikEdwin*>(Control(EUUIDDialogNumberEditor));
	TBuf<KMaxUUIDTextBufferLength> buf;
	editor->GetText(buf);
	TLex lex(buf);
	TInt err = lex.Val(iUUIDFilter, EHex);
	return (err == KErrNone);
	}

///////////////////////////////
// CDeviceInfoDialog functions
///////////////////////////////

CDeviceInfoDialog::CDeviceInfoDialog(const TBTDeviceName& aName, const TBTDevAddr& aAddress,const TBTDeviceClass& aClass)
: iDeviceName(aName), iDeviceAddress(aAddress), iDeviceClass(aClass)
	{
	}

void CDeviceInfoDialog::PreLayoutDynInitL()
	{
	CEikLabel* nameLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogName));
	_LIT(KNamePrefix,"Name: ");
	iDeviceName.Insert(0,KNamePrefix);
	nameLabel->SetTextL(iDeviceName);
	CEikLabel* addrLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogAddr));
	_LIT(KAddrFormat,"Address: 0x");
	HBufC* buf = HBufC::NewLC(KMaxDescriptorLength);
	TPtr desc = buf->Des();
	iDeviceAddress.GetReadable(desc);
	desc.Insert(0, KAddrFormat);
	addrLabel->SetTextL(desc);
	CEikLabel* classLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogClass));
	_LIT(KClassFormat,"Class ID: 0x%08x");
	desc.Format(KClassFormat,iDeviceClass.DeviceClass());
	classLabel->SetTextL(desc);
	classLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogServiceClass));
	_LIT(KServiceClassFormat,"    Service Class: 0x%04x");
	desc.Format(KServiceClassFormat,iDeviceClass.MajorServiceClass());
	classLabel->SetTextL(desc);
	classLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogMajorClass));
	_LIT(KMajorClassFormat,"    Major Class: 0x%02x");
	desc.Format(KMajorClassFormat,iDeviceClass.MajorDeviceClass());
	classLabel->SetTextL(desc);
	classLabel = static_cast<CEikLabel*>(Control(EBtDeviceInfoDialogMinorClass));
	_LIT(KMinorClassFormat,"    Minor Class: 0x%02x");
	desc.Format(KMinorClassFormat,iDeviceClass.MinorDeviceClass());
	classLabel->SetTextL(desc);
	CleanupStack::PopAndDestroy();
	}

⌨️ 快捷键说明

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