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

📄 svexinfo.cpp

📁 symbian下读取服务的程序
💻 CPP
字号:
// Copyright (c) 2006 Murray Read, All rights reserved

#include "SvexInfoPriv.h"
#include "SvexUtil.h"
#include "SvexService.hrh"


//
// TSvexAppServiceInfo
//
TSvexAppServiceInfo::TSvexAppServiceInfo(TUid aAppUid, TUid aServiceUid, const TApaAppServiceInfo& aInfo)
: iAppUid(aAppUid), iServiceUid(aServiceUid), iInfo(aInfo)
	{
	}



//
// MSvexAppServiceInfoArray
//
MSvexAppServiceInfoArray::~MSvexAppServiceInfoArray()
	{
	}


//
// CSvexAppInfo
//
EXPORT_C CSvexAppInfo::~CSvexAppInfo()
	{
	delete iInfo;
	}

EXPORT_C TUid CSvexAppInfo::AppUid() const
	{
	return iUid;
	}

EXPORT_C const TApaAppCaption& CSvexAppInfo::AppCaption() const
	{
	return iCaption;
	}

TInt CSvexAppInfo::MsasiCount() const
	{
	return iArray.Count();
	}

TSvexAppServiceInfo CSvexAppInfo::MsasiAt(TInt aIndex) const
	{
	// use apparc's array to get the app's service info
	const TApaAppServiceInfo& info = iArray[aIndex];
	return TSvexAppServiceInfo(iUid, info.Uid(), info);
	}

CSvexAppInfo::CSvexAppInfo(TUid aAppUid, const TApaAppCaption& aAppCaption, CApaAppServiceInfoArray* aInfo)
: iUid(aAppUid), iInfo(aInfo), iArray(aInfo->Array()), iCaption(aAppCaption)
	{
	}



//
// CSvexServiceInfo
//
EXPORT_C TUid CSvexServiceInfo::ServiceUid() const
	{
	return iUid;
	}

CSvexServiceInfo::CSvexServiceInfo(TUid aServiceUid)
:iUid(aServiceUid)
	{
	}



//
// CSvexDirectServiceInfo
//
EXPORT_C CSvexDirectServiceInfo::~CSvexDirectServiceInfo()
	{
	delete iInfo;
	}

TInt CSvexDirectServiceInfo::MsasiCount() const
	{
	return iArray.Count();
	}

TSvexAppServiceInfo CSvexDirectServiceInfo::MsasiAt(TInt aIndex) const
	{
	// use apparc's array to get the service's app info
	const TApaAppServiceInfo& info = iArray[aIndex];
	return TSvexAppServiceInfo(info.Uid(), ServiceUid(), info);
	}

CSvexDirectServiceInfo::CSvexDirectServiceInfo(TUid aServiceUid, CApaAppServiceInfoArray* aInfo)
: CSvexServiceInfo(aServiceUid), iInfo(aInfo), iArray(aInfo->Array())
	{
	}



//
// CSvexInfoBase
//
EXPORT_C CSvexInfoBase* CSvexInfoBase::NewL()
	{
	CSvexInfoBase* self = NewLC();
	CleanupStack::Pop(self);
	return self;
	}

EXPORT_C CSvexInfoBase* CSvexInfoBase::NewLC()
	{
	CSvexInfoBase* self = new(ELeave) CSvexInfoBase;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

EXPORT_C CSvexInfoBase::~CSvexInfoBase()
	{
	delete iServiceInfoService;
	iLs.Close();
	}

EXPORT_C CSvexServiceInfo* CSvexInfoBase::DirectServiceInfoLC(TUid aServiceUid) const
	{
	// get service info directly from apparc
	CApaAppServiceInfoArray* apaInfo = iLs.GetServiceImplementationsLC(aServiceUid);
	CSvexDirectServiceInfo* service = new(ELeave) CSvexDirectServiceInfo(aServiceUid, apaInfo);
	CleanupStack::Pop(apaInfo);		// now owned by service
	CleanupStack::PushL(service);
	return service;
	}

EXPORT_C CSvexAppInfo* CSvexInfoBase::DirectAppInfoLC(TUid aAppUid) const
	{
	TApaAppInfo appInfo;
	User::LeaveIfError(iLs.GetAppInfo(appInfo, aAppUid));
	return DirectAppInfoLC(appInfo);
	}

CSvexAppInfo* CSvexInfoBase::DirectAppInfoLC(const TApaAppInfo& aAppInfo) const
	{
	// get app info directly from apparc
	TUid appUid = aAppInfo.iUid;
	CApaAppServiceInfoArray* services = iLs.GetAppServicesLC(appUid);
	CSvexAppInfo* app = new(ELeave) CSvexAppInfo(appUid, aAppInfo.iCaption, services);
	CleanupStack::Pop(services);	// now owned by app
	CleanupStack::PushL(app);
	return app;
	}

CSvexInfoBase::CSvexInfoBase()
	{
	}

void CSvexInfoBase::ConstructL()
	{
	User::LeaveIfError(iLs.Connect());
	}

EXPORT_C TApaAppCaption CSvexInfoBase::AppCaption(TUid aAppUid) const
	{
	// get app caption directly from apparc
	TApaAppInfo appInfo;
	iLs.GetAppInfo(appInfo, aAppUid);
	return appInfo.iCaption;
	}

EXPORT_C const CSvexServiceInfo& CSvexInfoBase::ServiceInfoServiceL() const
	{
	// get Service Info Service directly from apparc, cache the result
	if (!iServiceInfoService)
		CleanupStack::Pop(iServiceInfoService = DirectServiceInfoLC(TUid::Uid(KSvexServiceInfoServiceUid)));
	return *iServiceInfoService;
	}

EXPORT_C RApaLsSession& CSvexInfoBase::LsSession() const
	{
	return iLs;
	}


//
// CSvexInfo
//
EXPORT_C CSvexInfo* CSvexInfo::NewL()
	{
	CSvexInfo* self = NewLC();
	CleanupStack::Pop(self);
	return self;
	}

EXPORT_C CSvexInfo* CSvexInfo::NewLC()
	{
	CSvexInfo* self = new(ELeave) CSvexInfo;
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

EXPORT_C CSvexInfo::~CSvexInfo()
	{
	CleanupInfo();
	}

// GetAllInfoL() collects information about all apps and services
// registered with apparc. We can't directly ask what services
// are available, only apps. So the strategy is to ask for all
// apps, find out what services they support and build the
// service lists from that.
void CSvexInfo::GetAllInfoL()
	{
	// remove any existing data
	CleanupInfo();
	// protect against errors in this function
	CleanupStack::PushL(TCleanupItem(CleanupInfoCallback, this));
	
	// go through all apps
	User::LeaveIfError(iLs.GetAllApps());
	
	// count the apps
	TInt appCount;
	User::LeaveIfError(iLs.AppCount(appCount));
	
	// reserve space first to make the insertions efficient
	iAppUids.ReserveL(appCount);
	iAppMap.ReserveL(appCount);
	
	// loop through all the apps
	TApaAppInfo appInfo;
	TInt err;
	while ((err = iLs.GetNextApp(appInfo)) == KErrNone)
		{
		// apps without services will generate errors, ignore them.
		TRAP_IGNORE(AddAppL(appInfo));
		}
	User::LeaveIfError(err);

	// Finally colapse the services & build the service UID array
	TInt servCount = iServiceMap.Count();
	// reserve enough space for all the UIDs
	iServiceUids.ReserveL(servCount);
	// iterate through the service map
	THashMapIter<TInt, CSvexServiceInfo*> servIter(iServiceMap);
	for (CSvexServiceInfo* const* pServ = servIter.NextValue(); pServ; pServ = servIter.NextValue())
		{
		CSvexBuildServiceInfo* const service = (CSvexBuildServiceInfo*)(*pServ);
		// colapse the service array
		service->MakeArrayL();
		// Add the service UID
		iServiceUids.AppendL(service->ServiceUid());
		}

	CleanupStack::Pop();	// cleanup item
	}

void CSvexInfo::AddAppL(const TApaAppInfo& aAppInfo)
	{
	// get the app's services
	CSvexAppInfo* app = DirectAppInfoLC(aAppInfo);

	// add the app to the app map
	TUid appUid = aAppInfo.iUid;
	User::LeaveIfError(iAppMap.Insert(appUid.iUid, app));
	CleanupStack::Pop(app);	// now owned by iAppMap

	// add the UID to the apps UID list
	iAppUids.AppendL(appUid);
	
	// Now add the app to the services map
	TInt servCount = app->MsasiCount();
	for (TInt ii=0; ii<servCount; ii++)
		{
		const TApaAppServiceInfo& serv = app->MsasiAt(ii).iInfo;
		TUid servUid = serv.Uid();
		// get the service Info in which to place this app
		CSvexBuildServiceInfo* servInfo;
		CSvexBuildServiceInfo** pServInfo = (CSvexBuildServiceInfo**)(iServiceMap.Find(servUid.iUid));
		if (pServInfo)
			{
			servInfo = *pServInfo;
			}
		else
			{
			// need to add a new service info
			servInfo = new(ELeave) CSvexBuildServiceInfo(servUid);
			CleanupStack::PushL(servInfo);
			User::LeaveIfError(iServiceMap.Insert(servUid.iUid, servInfo));
			CleanupStack::Pop(servInfo);	// now owned by iServiceMap
			}
		servInfo->AddAppL(appUid, serv);
		}
	}

EXPORT_C const RArray<TUid>& CSvexInfo::AppUids() const
	{
	return iAppUids;
	}

EXPORT_C const CSvexAppInfo& CSvexInfo::AppInfo(TUid aAppUid) const
	{
	// get the app info from the recorded data
	CSvexAppInfo const * const * ppInfo = iAppMap.Find(aAppUid.iUid);
	__ASSERT_ALWAYS(ppInfo && *ppInfo, Panic(ESvexPanicBadAppUid));
	return **ppInfo;
	}

EXPORT_C const RArray<TUid>& CSvexInfo::ServiceUids() const
	{
	return iServiceUids;
	}

EXPORT_C const CSvexServiceInfo& CSvexInfo::ServiceInfo(TUid aServiceUid) const
	{
	// get the service info from the recorded data
	CSvexServiceInfo const * const * ppInfo = iServiceMap.Find(aServiceUid.iUid);
	__ASSERT_ALWAYS(ppInfo && *ppInfo, Panic(ESvexPanicBadServiceUid));
	return **ppInfo;
	}

CSvexInfo::CSvexInfo()
	{
	}

void CSvexInfo::ConstructL()
	{
	CSvexInfoBase::ConstructL();
	GetAllInfoL();
	}

void CSvexInfo::CleanupInfo()
	{
	// iterate through the app map, deleting CSvexAppInfo objects
	THashMapIter<TInt, CSvexAppInfo*> appIter(iAppMap);
	CSvexAppInfo* const* pApp;
	do	{
		pApp = appIter.NextValue();	// returns NULL at end
		if (pApp)
			delete *pApp;
		} while(pApp);
	iAppMap.Close();
	
	// iterate through the service map, deleting CSvexServiceInfo objects
	THashMapIter<TInt, CSvexServiceInfo*> servIter(iServiceMap);
	CSvexServiceInfo* const* pServ;
	do	{
		pServ = servIter.NextValue();	// returns NULL at end
		if (pServ)
			delete *pServ;
		} while(pServ);
	iServiceMap.Close();
	
	// close the hashMaps
	iAppUids.Close();
	iServiceUids.Close();	
	}

void CSvexInfo::CleanupInfoCallback(TAny* aThis)
	{
	((CSvexInfo*)aThis)->CleanupInfo();
	}

EXPORT_C TApaAppCaption CSvexInfo::AppCaption(TUid aAppUid) const
	{
	return AppInfo(aAppUid).AppCaption();
	}

EXPORT_C const CSvexServiceInfo& CSvexInfo::ServiceInfoServiceL() const
	{
	// get it locally if available, otherwise go to apparc
	if (iServiceMap.Find(KSvexServiceInfoServiceUid))
		return ServiceInfo(TUid::Uid(KSvexServiceInfoServiceUid));
	else
		return CSvexInfoBase::ServiceInfoServiceL();
	}


//
// CSvexBuildServiceInfo
//
CSvexBuildServiceInfo::~CSvexBuildServiceInfo()
	{
	// delete any items in the list
	SSvexAppServiceInfoList* ptr = iFirst;
	while (ptr)
		{
		SSvexAppServiceInfoList* next = ptr->iNext;
		delete ptr;
		ptr = next;
		}
	// close the array
	iArray.Close();
	}

void CSvexBuildServiceInfo::AddAppL(TUid aAppUid, const TApaAppServiceInfo& aServInfo)
	{
	// Add items to the list, we're not going to add them to the
	// array till we have all the items, and we know how many
	// there are.
	SSvexAppServiceInfoList* elem = new(ELeave) SSvexAppServiceInfoList;
	elem->iInfo.iAppUid = aAppUid;
	elem->iInfo.iInfo = &aServInfo;
	elem->iNext = iFirst;
	iFirst = elem;
	iCount++;
	}

TInt CSvexBuildServiceInfo::MsasiCount() const
	{
	return iArray.Count();
	}

TSvexAppServiceInfo CSvexBuildServiceInfo::MsasiAt(TInt aIndex) const
	{
	const SSvexAppServiceInfo& info = iArray[aIndex];
	return TSvexAppServiceInfo(info.iAppUid, ServiceUid(), *info.iInfo);
	}

CSvexBuildServiceInfo::CSvexBuildServiceInfo(TUid aServiceUid)
: CSvexServiceInfo(aServiceUid)
	{
	}

void CSvexBuildServiceInfo::MakeArrayL()
	{
	// Now that we have all the items, reserve space in the
	// array, remove items from the list and append them to
	// the array
	if (iFirst)
		{
		iArray.ReserveL(iCount);
		SSvexAppServiceInfoList* ptr = iFirst;
		while (ptr)
			{
			iArray.AppendL(ptr->iInfo);
			SSvexAppServiceInfoList* next = ptr->iNext;
			delete ptr;
			ptr = next;
			}
 		iFirst = NULL;
		}
	}


⌨️ 快捷键说明

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