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

📄 engine.cpp

📁 《UIQ 3 The Complete Guide》书的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// Engine.cpp - The data processing and provision engine for a signed app
//
// Copyright (C) UIQ Technology AB, 2007
//
// This material is provided "as is" without any warranty to its performance or functionality. 
// In no event shall UIQ Technology be liable for any damages whatsoever arising out of the
// use or inabilty to use this material. 
//

#include "Engine.h"
#include "SignedApp.hrh"

//////////////////////////////////////////////////////////////////////////////////
const TInt KAppCategoryEntryStreamVersion=1;
void TAppCategoryEntry::ExternalizeL(RWriteStream& aStream) const
	{
	aStream.WriteUint8L(KAppCategoryEntryStreamVersion);

	// iCategoryId
	aStream.WriteInt32L(iCategoryId);

	// iCategoryName
	aStream.WriteUint8L(iCategoryName.Length());
	aStream.WriteL((TText*)(iCategoryName.Ptr()),iCategoryName.Length());
	}

void TAppCategoryEntry::InternalizeL(RReadStream& aStream)
	{
	TInt version=aStream.ReadUint8L();
	if (version<=KAppCategoryEntryStreamVersion)
		{
		// iCategoryId
		iCategoryId=aStream.ReadInt32L();

		// iCategoryName
		iCategoryName.SetLength(aStream.ReadUint8L());
		aStream.ReadL((TText*)(iCategoryName.Ptr()),iCategoryName.Length());
		}
	else
		User::Leave(KErrNotSupported);
	}

//////////////////////////////////////////////////////////////////////////////////
void TFolderEntry::Construct(const TEntry& aItem)
//
// Construct an entry from the TEntry. 
//
	{
	// by default the item belongs to the unfiled category
	iCategory=EAppCategoryUnfiled;

	// take a copy of the relevant property
	iAttributes=aItem.iAtt;
	iSize=aItem.iSize;
	iModified=aItem.iModified;
	iUidType=aItem.iType;
	iName=aItem.iName;
	}

const TInt KFolderEntryStreamVersion=1;
void TFolderEntry::ExternalizeL(RWriteStream& aStream) const
//
// We have a number of fields we might want to save. However in this case most of the
// fields are derived from the contents of a folder on disk. When we come to build the content 
// of a folder on disk - say 1 week after previous time - it is entirely possible that other apps
// have mofidied the folder content - especically if we can view public folders. 
// Our only reasonable course of action for this particular application is attempt to re-assign 
// category information. We do this based on name.
//
	{
	aStream.WriteUint8L(KFolderEntryStreamVersion);

	// iCategory
	aStream.WriteInt32L(iCategory);

	// iName
	aStream.WriteUint16L(iName.Length());
	aStream.WriteL((TText*)(iName.Ptr()),iName.Length());
	}

void TFolderEntry::InternalizeL(RReadStream& aStream)
//
// See comments on ExternalizeL()
//
	{
	TInt version=aStream.ReadUint8L();
	if (version<=KFolderEntryStreamVersion)
		{
		// iCategory
		iCategory=aStream.ReadInt32L();

		// iName
		iName.SetLength(aStream.ReadUint16L());
		aStream.ReadL((TText*)(iName.Ptr()),iName.Length());
		}
	else
		User::Leave(KErrNotSupported);
	}

//////////////////////////////////////////////////////////////////////////////////
typedef union
	{
	TAny* thisIsAPointerToTheObject;
	TInt *whichWeConvertToOneOfThese;
	} CONVERT_POINTER_TYPES;

class TKeyEntryList : public TKeyArrayFix
	{
public:
	TKeyEntryList(RArray<TFolderEntry>* aFolderEntryList,const TInt aSortType,const TInt aDescending);
	TInt Compare(TInt aLeft,TInt aRight) const;
protected:
	RArray<TFolderEntry>* iFolderEntryList;
	TInt iSortOrder;
	};

TKeyEntryList::TKeyEntryList(RArray<TFolderEntry>* aFolderEntryList,const TInt aSortType,const TInt aDescending) :
	TKeyArrayFix(0,(TKeyCmpNumeric)aSortType),iFolderEntryList(aFolderEntryList),iSortOrder(aDescending)
	{}

TInt TKeyEntryList::Compare(
//
// Primary comparison function for comparing items in a FolderEntryList
// At(X) returns a pointer to the element at array index X. The type is returned as a TAny* (void*)
// as the array classes can hold arbitary types.
//
	TInt aLeft,
	TInt aRight) const // one of these can be -1, meaning not a real VA entry, e.g. when InsertIsq()
	{
	// how you might convert what is At() the position indicated by aLeft from a TAny* to the
	// array content type with a cast (our array is an array of TInts so conv TAny* to TInt* )
	TFolderEntry& leftEntry=(*iFolderEntryList)[*static_cast<TInt*>(At(aLeft))];

	// how you might convert what is At() the position indicated by aRight from a TAny* to the
	// array content type without using casts... 
	CONVERT_POINTER_TYPES conv;
	conv.thisIsAPointerToTheObject=At(aRight);
	TFolderEntry& rightEntry=(*iFolderEntryList)[(*conv.whichWeConvertToOneOfThese)];

	// left item comes before right item
	TInt ret=(-1);

	// the original SortType passed gets stored in the iCmpType field - which we override
	switch (iCmpType)
		{
	// sort by file size, if they are the same order by name
	case EFolderEntrySortBySize:
		ret=leftEntry.EntrySize()-rightEntry.EntrySize();
		if (ret==0)			
			ret=leftEntry.EntryName().CompareF(rightEntry.EntryName());
		break;

	// sort by name
	case EFolderEntrySortByName:
		ret=leftEntry.EntryName().CompareF(rightEntry.EntryName());
		break;

	// sort by last modified date, if they are the same order by name
	case EFolderEntrySortByModified:
		if (leftEntry.EntryModified()>rightEntry.EntryModified())
			ret=1;
		else if (leftEntry.EntryModified()==rightEntry.EntryModified())
			ret=leftEntry.EntryName().CompareF(rightEntry.EntryName());
		break;

	// by type = effectivly group together entries belonging to same category.
	case EFolderEntrySortByType:
		if (leftEntry.EntryCategory()>rightEntry.EntryCategory())
			ret=1;
		else if (leftEntry.EntryCategory()<rightEntry.EntryCategory())
			ret=-1;
		else // same category, 2nd order key is by name
			ret=leftEntry.EntryName().CompareF(rightEntry.EntryName());
		break;

	default:
		break;		
		}
	if (iSortOrder)
		ret=(-ret); // simply reverse the result to get descending order sorts
	return(ret);	
	}


//////////////////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
void CAppEngine::Panic(const TInt aReason) const
	{
	_LIT(KAppEngine,"AppEngine");
	User::Panic(KAppEngine,aReason);
	}

void CAppEngine::__DbgTestInvariant() const
//
// Called by the __TEST_INVARIANT macro for us to perform some self testing
//
	{
	// In our model we should have 
	// i) a set of TFolderEntry objects stored in the iFolderEntryList
	// ii) an index onto these objects
	// iii) the index may not refer to all entries, however all entries should be sorted
	// iv) all entries in the index should refer to an existing iFolderEntryList element

	// check that the order of items indicated by our index and the current sort 
	// are self consistent, if not we panic
	TInt i,j;
	TInt count=iFolderEntryIndex->Count();
	for (i=0;i<(count-1);i++)
		{
		const TFolderEntry& entry1=Entry(i);
		for (j=i+1;j<count;j++)
			{
			TInt ret=(-1);
			const TFolderEntry& entry2=Entry(j);
			switch (iSortType)
				{
			case EFolderEntrySortBySize:
				ret=entry1.EntrySize()-entry2.EntrySize();
				if (!ret)
					ret=entry1.EntryName().CompareF(entry2.EntryName());
				break;

			case EFolderEntrySortByName:
				ret=entry1.EntryName().CompareF(entry2.EntryName());
				break;

			case EFolderEntrySortByModified:
				if (entry1.EntryModified()>entry2.EntryModified())
					ret=1;
				else if (entry1.EntryModified()==entry2.EntryModified())
					ret=entry1.EntryName().CompareF(entry2.EntryName());
				break;

			case EFolderEntrySortByType:
				break;
			default:
				Panic(0); // an unknown sortMode
				break;
				}
			if (!ret) // 2nd order sorts should ensure this is never possible
				Panic(1);
			if (iSortOrder)
				ret=(-ret);
			if (ret>=0) // first item MUST come before 2nd item, else sort is broken
				Panic(2);
			}
		}

	// validate that the iCurrentEntry is within a valid range of values
	if (count>0)
		{
		if (iCurrentEntry<0 || iCurrentEntry>=count)
			Panic(3);
		}
	else if (iCurrentEntry>=0)
		Panic(4);


	// the iCurrentCategory should exist in the list of all categories
	count=CategoryListCount();
	for (i=0;i<count;i++)
		{
		if (CategoryListAt(i).iCategoryId==iCurrentCategory)
			break;
		}
	if (i>=count)
		Panic(5); // couldnt find iCurrentCategory within our list of categories


	// validate that all entries belong to existing categories - but not the 'All' category
	TInt entryListCount=EntryListCount();
	for (i=0;i<entryListCount;i++)
		{
		const TFolderEntry& entry=EntryListAt(i);
		for (j=1;j<count;j++)
			{ // j=1 == skip the 'All' category
			if (entry.EntryCategory()==CategoryListAt(j).iCategoryId)
				break;
			}
		if (j>=count)
			Panic(6); // that entry belongs to non-existant category
		}

	// check all entries in the index refer to valid folderEntries
	count=iFolderEntryIndex->Count();
	for (i=0;i<count;i++)
		{
		if (iFolderEntryIndex->At(i)>=entryListCount)
			Panic(7); // entry in index refers to an item outside the list of entries
		}

	}

#endif // _DEBUG

CAppEngine::~CAppEngine()
	{
	if (iFolderEntryList)
		iFolderEntryList->Close();
	delete(iFolderEntryList);

	if (iCategoryList)
		iCategoryList->Close();
	delete(iCategoryList);

	delete(iFolderEntryIndex);
	}

CAppEngine::CAppEngine(RFs& aFs,const TInt aZoomLevel) :
	iFs(aFs),iZoomLevel(aZoomLevel)
	{}

void CAppEngine::ConstructL()
//
// Perform any one time construction.
//
	{
	iCategoryList=new(ELeave)RArray<TAppCategoryEntry>(4);
	}

void CAppEngine::SetDefaultPathL(const TDesC& aPath)
// 
// Set the application default path to that indicated. Build the list of files in that folder
// in preparation for the UI to display them.
//
	{
	BuildDirectoryListL(aPath);
	}

⌨️ 快捷键说明

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