/**
Utility method used to write text and binary blob fields into write streams. The write
streams will be used to persist the blob informations in contact database.
Provides a mechanism to get information from a contact item and store them in the
right blob fields within contact database. Template can be NULL. 

@param		aTextHeader reference to a write stream in which text header will be written
@param		aTextValues reference to a write stream in which text values will be written.
			From the caller point of view this reference should be a reference to a RSqlParamWriteStream instance
@param		aBinaryHeader reference to a write stream in which binary header will be written
@param		aBinaryValues reference to a write stream in which binary values will be written.
@param		aItem Contact item to be filled with information from text blob field.
@param      aSysTemplate System template item.
*/		
void TCntPersistenceUtility::WriteBlobL(CEmbeddedStore& aTextEmbeddedStore, RWriteStream& aTextValues, CEmbeddedStore& aBinaryEmbeddedStore, CEmbeddedStore& aBinaryEmbeddedBlobStore, const CContactItem& aItem, const CContactTemplate* aSysTemplate)
	{
	CContactItemFieldSet& fieldSet = aItem.CardFields();
	CContactItemFieldSet* textFieldSet = CContactItemFieldSet::NewLC();
	CContactItemFieldSet* binaryFieldSet = CContactItemFieldSet::NewLC();
	
	for(TInt i = 0; i < fieldSet.Count(); ++i)
		{
		CContactItemField* item	= CContactItemField::NewL((aItem.CardFields())[i]);
		CleanupStack::PushL(item);
		if(item->StorageType() == KStorageTypeText)
			{
			textFieldSet->AddL(*item);	
			}
		else
			{
			binaryFieldSet->AddL(*item);	
			}	
		CleanupStack::Pop(item);	
		}
	
	TStreamId rootId = textFieldSet->StoreL(aTextEmbeddedStore, aSysTemplate, aTextValues, aBinaryEmbeddedBlobStore, NULL);// *textEmbeddedBlobStore); 
	aTextEmbeddedStore.SetRootL(rootId);
	aTextEmbeddedStore.CommitL();

	rootId = binaryFieldSet->StoreL(aBinaryEmbeddedStore, aSysTemplate, aTextValues, aBinaryEmbeddedBlobStore, NULL); 
	aBinaryEmbeddedStore.SetRootL(rootId);
	aBinaryEmbeddedStore.CommitL();
	aBinaryEmbeddedBlobStore.CommitL();
		
	CleanupStack::PopAndDestroy(2, textFieldSet);  //binaryFieldSet, textFieldSet
	}
/**
Utility method used to read text fields from blob and fill item field set,Provides a mechanism to restore a 
contact item field set from text blob field within contact database.
Blob informations are stored based on contact item field. At restore, a reference to a contact item field set
is provided. For every contact item field in the item field set, a text restore is made.

@param		aFieldSet Reference to CContactItemFieldSet. Item field set that has to be filled with informations from blob
@param		aHeaderStore Stream store containing the header informations
@param		aId Root id for the header stream store
@param		aValuesStore Read stream used to read text fields from text blob
@param		aViewDef View definition indicating which fields have to be read from blob
@param		aTemplate Template indicating if current field set should be filled based on a template
*/	
void TCntPersistenceUtility::RestoreTextL(CContactItemFieldSet& aFieldSet, CStreamStore& aHeaderStore, TStreamId aId, HBufC* textStream, const CContactItemViewDef& aViewDef, const CContactItem* aTemplate)
	{
	const TBool KIncludeFields = ( aViewDef.Use() == CContactItemViewDef::EIncludeFields );
	
	if (KIncludeFields && aViewDef.Count() == 0)
		{
		// If view definition does not contain any field we don't do anything (don't read from 
		// blob). We simply return from method without doing anything.
		// This is not an error condition
		return;	
		}
		
	RStoreReadStream stream;
	stream.OpenLC(aHeaderStore,aId);
	
	TCardinality fieldCount;
	stream>>fieldCount;
	
	TInt textFieldIndex=0;

	for (TInt ii = 0; ii < fieldCount; ++ii)
		{
		// Restore text for every CContactItemField in provided CContactItemFieldSet.
		CContactItemField* field = CContactItemField::NewLC();
		
		if(aTemplate)
		    {
    		field->RestoreFieldTypesL(stream, &(aTemplate->CardFields()));
		    }
		else
		    {
    		field->RestoreFieldTypesL(stream, NULL);
		    }    
		    
		ASSERT(field->StorageType() == KStorageTypeText);

		TBool fieldDefined = ETrue;
		if(!aViewDef.MatchesAll())
			{
			fieldDefined = (aViewDef.Find(field->ContentType()) != KErrNotFound);
			}

		if ((!((fieldDefined && KIncludeFields) || (!fieldDefined && !KIncludeFields))) || 
			(field->IsHidden() && aViewDef.Mode() == CContactItemViewDef::EMaskHiddenFields))
			{
    		CleanupStack::PopAndDestroy(field); 	
			}
		else
		    {
    		field->RestoreTextL(textStream, textFieldIndex);
			aFieldSet.AddL(*field);
    		CleanupStack::Pop(field); 	
		    }	
		    
        ++textFieldIndex;
		}
	CleanupStack::PopAndDestroy(&stream);	
	}
Exemple #3
0
static void PhonebookWriteReqL(PktBuf &in, PktBuf &out)
{
	TUint16 memtype, ns, type, subtype;
	TUint32 location;
	CContactItem *item;
	CContactItemFieldSet *fieldset;
	CContactItemField *field;
	TBuf<64> name;
	TPtrC family_name, given_name;
	int i, s;

	g->InitPhoneBookL();

	in >> memtype;
	in >> location;

	if (memtype != GN_MT_ME) {
		out << (TUint16)GNAPPLET_MSG_PHONEBOOK_WRITE_RESP;
		out << (TUint16)GN_ERR_INVALIDMEMORYTYPE;
		return;
	}

	fieldset = CContactItemFieldSet::NewLC();
	in >> ns;
	for (i = 0; i < ns; i++) {
		in >> type >> subtype >> name;
		switch (type) {
		case GN_PHONEBOOK_ENTRY_Name:
			for (s = 0; s < name.Length() && name[s] != ' '; s++) ;
			if (s >= 1 && s < name.Length()) {
				family_name.Set(name.Ptr(), s);
				given_name.Set(name.Ptr() + s + 1, name.Length() - s - 1);
			} else {
				family_name.Set(name);
			}
			if (family_name.Length() > 0) {
				field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldFamilyName);
				//field->SetLabelL(_L("alma"));
				field->SetMapping(KUidContactFieldVCardMapUnusedN);
				field->TextStorage()->SetTextL(family_name);
				fieldset->AddL(*field);
			}
			if (given_name.Length() > 0) {
				field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldGivenName);
				//field->SetLabelL(_L("alma"));
				field->SetMapping(KUidContactFieldVCardMapUnusedN);
				field->TextStorage()->SetTextL(given_name);
				fieldset->AddL(*field);
			}
			break;
		case GN_PHONEBOOK_ENTRY_Email:
			field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldEMail);
			field->SetMapping(KUidContactFieldVCardMapEMAILINTERNET);
			field->TextStorage()->SetTextL(name);
			fieldset->AddL(*field);
			break;
		case GN_PHONEBOOK_ENTRY_Note:
			field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldNote);
			field->SetMapping(KUidContactFieldVCardMapNOTE);
			field->TextStorage()->SetTextL(name);
			fieldset->AddL(*field);
			break;
		case GN_PHONEBOOK_ENTRY_Number:
			if (subtype == GN_PHONEBOOK_NUMBER_Fax) {
				field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldFax);
				field->SetMapping(KUidContactFieldVCardMapTEL);
				field->AddFieldTypeL(KUidContactFieldVCardMapFAX);
			} else {
				field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldPhoneNumber);
				field->SetMapping(KUidContactFieldVCardMapTEL);
				switch (subtype) {
				case GN_PHONEBOOK_NUMBER_Home:
					field->AddFieldTypeL(KUidContactFieldVCardMapHOME);
					break;
				case GN_PHONEBOOK_NUMBER_Mobile:
					field->AddFieldTypeL(KUidContactFieldVCardMapCELL);
					break;
				case GN_PHONEBOOK_NUMBER_Work:
					field->AddFieldTypeL(KUidContactFieldVCardMapWORK);
					break;
				}
			}
			field->TextStorage()->SetTextL(name);
			fieldset->AddL(*field);
			break;
		case GN_PHONEBOOK_ENTRY_Ringtone:
			//field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldRingTone);
			//field->TextStorage()->SetTextL(name);
			//fieldset->AddL(*field);
			break;
		case GN_PHONEBOOK_ENTRY_Group:
			//field = CContactItemField::NewL(KStorageTypeText,
			//field->TextStorage()->SetTextL(name);
			//fieldset->AddL(*field);
			break;
		case GN_PHONEBOOK_ENTRY_URL:
			field = CContactItemField::NewL(KStorageTypeText, KUidContactFieldUrl);
			field->SetMapping(KUidContactFieldVCardMapURL);
			field->TextStorage()->SetTextL(name);
			fieldset->AddL(*field);
			break;
		default:
			break;
		}
	}
	in.FinishL();

	TRAPD(error, item = g->cdb->OpenContactL(location));
	if (error == KErrNone) {
		item->UpdateFieldSet(fieldset);
		CleanupStack::Pop(fieldset);
		CleanupStack::PushL(item);
		g->cdb->CommitContactL(*item);
	} else {
		item = CContactCard::NewL();
		item->UpdateFieldSet(fieldset);
		CleanupStack::Pop(fieldset);
		CleanupStack::PushL(item);
		location = g->cdb->AddNewContactL(*item);
	}
	CleanupStack::Pop(item);

	out << (TUint16)GNAPPLET_MSG_PHONEBOOK_WRITE_RESP;
	out << (TUint16)GN_ERR_NONE;
	out << memtype;
	out << location;
	cons->Printf(_L("write: %d\n"), location);
}