phonebookengines/contactsmodel/src/CNTFLDST.CPP
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <cntdef.h>
       
    17 #include <cntfldst.h>
       
    18 #include <txtetext.h>
       
    19 #include <confndr.h>
       
    20 #include <conlist.h>
       
    21 
       
    22 
       
    23 #include "CNTSTD.H"
       
    24 #include <s32mem.h>
       
    25 
       
    26 const TUid KUidEtextToText={0x100040C8};
       
    27 const TUid KUidTextToEtext={0x100040C7};
       
    28 
       
    29 
       
    30 //
       
    31 // class CContactFieldStorage
       
    32 //
       
    33 
       
    34 
       
    35 // for BC-proofing
       
    36 EXPORT_C void CContactFieldStorage::CContactFieldStorage_Reserved1() 
       
    37 	{  
       
    38 	}
       
    39 
       
    40 
       
    41 
       
    42 //
       
    43 // class CContactTextField
       
    44 //
       
    45 
       
    46 // for BC-proofing
       
    47 EXPORT_C /*virtual*/ void CContactTextField::CContactFieldStorage_Reserved1()
       
    48 	{
       
    49 	}
       
    50 
       
    51 EXPORT_C CContactTextField::~CContactTextField()
       
    52 /** Deletes the text owned by the object, prior to its destruction. */
       
    53     {
       
    54     delete iText;
       
    55     }
       
    56 
       
    57 void CContactTextField::InternalizeL(RReadStream& aStream)
       
    58     {
       
    59     TInt maxlen =  aStream.ReadInt32L();
       
    60     if (maxlen > 0)
       
    61     	iText =  HBufC::NewL(aStream, maxlen);
       
    62 	}
       
    63 
       
    64 void CContactTextField::InternalizeL(HBufC* aTextStream,TInt aTextFieldIndex)
       
    65     {
       
    66 	TInt pos=0;
       
    67 	TPtrC des= *aTextStream;
       
    68 	TInt totalLen=des.Length();
       
    69 	// remove left part
       
    70 	for(TInt i=1;i<=aTextFieldIndex && pos<totalLen;i++)
       
    71 		{
       
    72 		TInt offset=des.Right(totalLen-pos).Locate(KTextStreamSeparator);
       
    73 		if (offset != KErrNotFound)
       
    74 			pos+=offset+1;
       
    75 		else
       
    76 			break;
       
    77 		}
       
    78 	// remove right part
       
    79 	if (totalLen>pos)
       
    80 		{
       
    81 		TInt len=des.Right(totalLen-pos).Locate(KTextStreamSeparator);
       
    82 		if (len>0)
       
    83 			iText=des.Mid(pos,len).AllocL();
       
    84 		}
       
    85 	}
       
    86 
       
    87 void CContactTextField::ExternalizeL(RWriteStream& aStream) const
       
    88     {
       
    89 	// Don't attempt to externalise an empty fieldset.
       
    90 	if (iText == NULL)
       
    91 		{
       
    92 		aStream.WriteInt32L(0);	
       
    93 		return;
       
    94 		}
       
    95 
       
    96     TInt maxlen = iText->Length();
       
    97     aStream.WriteInt32L(maxlen);
       
    98     if(maxlen)
       
    99     	{
       
   100     	aStream << *iText;
       
   101     	}    
       
   102    }
       
   103 
       
   104 void CContactTextField::ExternalizeL(RWriteStream& aStream,TBool aIsInlineBlob,TInt /*aTextFieldIndex*/) const
       
   105     {
       
   106 	
       
   107 	if (iText)
       
   108       aStream.WriteL(iText->Des());          
       
   109 	if (aIsInlineBlob)
       
   110 		{
       
   111 		TBuf<1> buffer;
       
   112 		buffer.Format(_L("%c"),KTextStreamSeparator);
       
   113 		aStream.WriteL(buffer); 
       
   114 		}
       
   115   }
       
   116 
       
   117 
       
   118 TStreamId CContactTextField::StoreL(CStreamStore& aStore) const
       
   119     {
       
   120 
       
   121     RStoreWriteStream stream;
       
   122     TStreamId id=stream.CreateLC(aStore);
       
   123 	ExternalizeL(stream);
       
   124     stream.CommitL();
       
   125     CleanupStack::PopAndDestroy();	// stream
       
   126     return id;
       
   127     }
       
   128 
       
   129 void CContactTextField::RestoreL(CStreamStore& ,RReadStream& aStream)
       
   130     {
       
   131 	InternalizeL(aStream);
       
   132 	}
       
   133 
       
   134 EXPORT_C void CContactTextField::SetTextL(const TDesC& aText)
       
   135 /** Sets the field text. 
       
   136 
       
   137 This function allocates a new HBufC descriptor, deleting any existing one, 
       
   138 and copies the new text into it. The function can leave.
       
   139 
       
   140 @param aText The new field text. */
       
   141 	{
       
   142 	HBufC* text=aText.AllocL();
       
   143 	delete iText;
       
   144 	iText=text;
       
   145 	}
       
   146 
       
   147 EXPORT_C void CContactTextField::SetText(HBufC *aHbuf)
       
   148 /** Sets the field text. 
       
   149 
       
   150 The text field object takes ownership of the specified descriptor.
       
   151 The function cannot leave.
       
   152 
       
   153 @param aHbuf The new field text. */
       
   154 	{
       
   155 	delete iText;
       
   156 	iText=aHbuf;
       
   157 	}
       
   158 
       
   159 // zzz this should be binned or tested (and re-written)
       
   160 EXPORT_C void CContactTextField::SetTextArray(MDesCArray* anArray)
       
   161 /** Sets the field text from a descriptor array. Each descriptor 
       
   162 in the array is appended to the text field storage. They are separated by 
       
   163 paragraph delimiters (CEditableText::EParagraphDelimiter). Any existing text 
       
   164 is replaced.
       
   165 
       
   166 @param anArray Pointer to array of descriptors to set as the field text. */
       
   167     {
       
   168     TInt count = anArray->MdcaCount();
       
   169     delete (iText);
       
   170 	TInt size=0;
       
   171 	TInt ii=0;
       
   172     for (;ii<count;ii++)
       
   173     	{
       
   174     	size+=anArray->MdcaPoint(ii).Size();
       
   175     	if (ii<(count-1))
       
   176 	    	size++; //for semicolon
       
   177 		}
       
   178     iText=HBufC::NewL(size);
       
   179     for (ii=0;ii<count;ii++)
       
   180     	{
       
   181     	TPtrC temp(anArray->MdcaPoint(ii));
       
   182     	TPtr ptr = iText->Des();
       
   183     	ptr.Append(temp);
       
   184     	if (ii<(count-1))
       
   185 			ptr.Append(CEditableText::EParagraphDelimiter);
       
   186     	}
       
   187     }
       
   188 
       
   189 EXPORT_C TPtrC CContactTextField::Text() const
       
   190 /** Gets a copy of the text which is stored in the field.
       
   191 
       
   192 @return Descriptor pointing to a copy of the field data. */
       
   193     {
       
   194     TPtrC text;
       
   195     if (iText)
       
   196     	text.Set(*iText);
       
   197     return text;
       
   198     }
       
   199 
       
   200 TBool CContactTextField::IsFull() const
       
   201 	{
       
   202 	return (iText && iText->Size()!=0);
       
   203 	}
       
   204 
       
   205 HBufC *CContactTextField::EncodeL(const TDesC& aText,TUid aConversion) const
       
   206 	{
       
   207 	HBufC8* text=HBufC8::NewLC(aText.Length()*2);
       
   208 	TPtr8 ptr = text->Des();
       
   209 	TInt i;
       
   210 	for (i=0; i < aText.Length(); i++)
       
   211 		{
       
   212 		ptr.Append(aText[i] & 0x00FF);
       
   213 		ptr.Append((aText[i] >> 8) & 0x00FF);
       
   214 		}
       
   215 	CCnaConverterList* convList=CCnaConverterList::NewLC(); 
       
   216 	CConverterBase* conv = convList->NewConverterL(aConversion); 
       
   217 	if (!conv)
       
   218 		{
       
   219 		CleanupStack::PopAndDestroy();          // convList 
       
   220 		User::Leave(KErrNotSupported);
       
   221 		}
       
   222 	CleanupStack::PushL(conv);
       
   223 	CBufFlat* decodeBuffer = CBufFlat::NewL(256); 
       
   224 	CleanupStack::PushL(decodeBuffer);
       
   225 	CBufFlat* encodedBuffer = CBufFlat::NewL(256); 
       
   226 	CleanupStack::PushL(encodedBuffer);
       
   227 	decodeBuffer->InsertL(0,ptr);
       
   228 	RBufReadStream readStream;
       
   229 	RBufWriteStream writeStream;
       
   230 	readStream.Open(*decodeBuffer);
       
   231 	writeStream.Open(*encodedBuffer);
       
   232 	conv->ConvertObjectL(readStream, writeStream); 
       
   233 	readStream.Close();
       
   234 	TInt size=encodedBuffer->Size();
       
   235 	HBufC* writeBuf=HBufC::NewLC(size); 
       
   236 	TPtr resulttext = writeBuf->Des();
       
   237 	for(i = 0; i < (size - 1); i += 2)
       
   238 		{
       
   239 		resulttext.Append((encodedBuffer->Ptr(0)[i + 1] << 8) | 
       
   240 			encodedBuffer->Ptr(0)[i]);
       
   241 		}
       
   242 
       
   243 	writeStream.CommitL();
       
   244 	writeStream.Close();
       
   245 	CleanupStack::Pop(); // writebuf
       
   246 	CleanupStack::PopAndDestroy(2); // buffers 
       
   247 	CleanupStack::PopAndDestroy(2); //conv+convList 
       
   248 	CleanupStack::PopAndDestroy();  //text
       
   249 	return (writeBuf);
       
   250 	}
       
   251 
       
   252 
       
   253 EXPORT_C TPtrC CContactTextField::StandardTextLC() const
       
   254 /** Converts a copy of the text stored in the field from Symbian editable text 
       
   255 format into plain text and returns it as a pointer descriptor.
       
   256 
       
   257 @return Descriptor pointing to a copy of the field data as plain text. */
       
   258     {
       
   259     TPtrC text;
       
   260     if (iText)
       
   261 		text.Set(*iText);
       
   262 	HBufC *hbuf=EncodeL(text,KUidEtextToText);
       
   263 	CleanupStack::PushL(hbuf);
       
   264 	return(hbuf->Des());
       
   265 	}
       
   266 
       
   267 EXPORT_C void CContactTextField::SetStandardTextL(const TDesC& aText)
       
   268 /** Converts a text string from plain text into Symbian editable text, and sets 
       
   269 this as the text which is stored in the field.  The text is truncated to
       
   270 KCntMaxTextFieldLength characters if necessary.
       
   271 
       
   272 @param aText The new field data. */
       
   273 	{
       
   274 	// Make a copy of aText and truncate if necessary.
       
   275 	TPtr truncText(const_cast<TUint16*>(aText.Ptr()),KCntMaxTextFieldLength);
       
   276 	truncText.SetLength(aText.Length()>KCntMaxTextFieldLength ? KCntMaxTextFieldLength : aText.Length());
       
   277 
       
   278 	HBufC* text=EncodeL(truncText,KUidTextToEtext);
       
   279 	delete iText;
       
   280 	iText=text;
       
   281 	}
       
   282 
       
   283 EXPORT_C void CContactTextField::SetStandardTextArray(MDesCArray* anArray)
       
   284 /** Converts an array of text strings from plain text into Symbian editable text, 
       
   285 appends them to a single descriptor, separating them with the new line character, 
       
   286 and sets this as the text which is stored in the field. Any existing field 
       
   287 text is replaced.  The text is truncated to KCntMaxTextFieldLength characters if
       
   288 necessary.
       
   289 
       
   290 @param anArray Array of descriptors to set as the field data. */
       
   291     {
       
   292     TInt count = anArray->MdcaCount();
       
   293     HBufC *txt=HBufC::NewL(0);
       
   294 	CleanupStack::PushL(txt);
       
   295     for (TInt ii=0;ii<count;ii++)
       
   296     	{
       
   297 		TInt mdcaLen=anArray->MdcaPoint(ii).Size();
       
   298 		if (mdcaLen>0)
       
   299 			{
       
   300     		if (txt->Length()>0)
       
   301 				txt->Des().Append(TChar('\n'));
       
   302     		txt=txt->ReAllocL(txt->Length()+mdcaLen+1);	// +1 In case we add '\n'
       
   303 			CleanupStack::Pop();	// txt(old value)
       
   304 			CleanupStack::PushL(txt);
       
   305     		TPtrC temp(anArray->MdcaPoint(ii));
       
   306     		txt->Des().Append(temp);
       
   307 			}
       
   308     	}
       
   309 
       
   310 	// Truncate if necessary.
       
   311 	if (txt->Length()>KCntMaxTextFieldLength)
       
   312 		{
       
   313 		txt->Des().SetLength(KCntMaxTextFieldLength);
       
   314 		}
       
   315 		
       
   316 	HBufC *text=EncodeL(txt->Des(),KUidTextToEtext);
       
   317 	CleanupStack::PopAndDestroy();	// txt
       
   318 	delete iText;
       
   319     iText=text;
       
   320     }
       
   321 
       
   322 //
       
   323 // class CContactDateField
       
   324 //
       
   325 
       
   326 
       
   327 // for BC-proofing
       
   328 EXPORT_C /*virtual*/ void CContactDateField::CContactFieldStorage_Reserved1()
       
   329 	{
       
   330 	}
       
   331 
       
   332 CContactDateField::CContactDateField()
       
   333 	{
       
   334 	iTime=Time::NullTTime();
       
   335 	}
       
   336 
       
   337 TStreamId CContactDateField::StoreL(CStreamStore& aStore) const
       
   338     {
       
   339     RStoreWriteStream stream;
       
   340     TStreamId id=stream.CreateLC(aStore);
       
   341 	ExternalizeL(stream);
       
   342     stream.CommitL();
       
   343     CleanupStack::PopAndDestroy();	// stream
       
   344     return id;
       
   345     }
       
   346 
       
   347 void CContactDateField::RestoreL(CStreamStore& ,RReadStream& aStream)
       
   348 	{
       
   349 	InternalizeL(aStream);
       
   350 	}
       
   351 
       
   352 void CContactDateField::InternalizeL( RReadStream& aStream)
       
   353     {
       
   354     TInt64 timevalue;
       
   355     aStream>>timevalue;
       
   356 	iTime=TTime(timevalue);
       
   357     }
       
   358 
       
   359 void CContactDateField::ExternalizeL(RWriteStream& aStream) const
       
   360     {
       
   361 	aStream<<iTime.Int64();
       
   362     }
       
   363 
       
   364 EXPORT_C void CContactDateField::SetTime(TTime aTime)
       
   365 /** Sets the date/time stored in the field from a TTime value.
       
   366 
       
   367 @param aTime The field's new date/time value. */
       
   368     {
       
   369     iTime=aTime;
       
   370     }
       
   371 
       
   372 EXPORT_C void CContactDateField::SetTime(TDateTime aDateTime)
       
   373 /** Sets the date/time stored in the field from a TDateTime value.
       
   374 
       
   375 @param aDateTime The field's new date/time value. */
       
   376     {
       
   377     iTime=TTime(aDateTime);
       
   378     }
       
   379 
       
   380 EXPORT_C TTime CContactDateField::Time() const
       
   381 /** Gets the date/time stored in the field as a TTime value.
       
   382 
       
   383 @return The field's date/time value. */
       
   384     {
       
   385     return iTime;
       
   386     }
       
   387 
       
   388 TBool CContactDateField::IsFull() const
       
   389 	{
       
   390 	return(iTime!=Time::NullTTime());
       
   391 	}
       
   392 
       
   393 //
       
   394 // class CContactStoreField
       
   395 //
       
   396 
       
   397 // for BC-proofing
       
   398 EXPORT_C /*virtual*/ void CContactStoreField::CContactFieldStorage_Reserved1()
       
   399 	{
       
   400 	}
       
   401 
       
   402 CContactStoreField::~CContactStoreField()
       
   403 	{
       
   404 	delete iThing;
       
   405 	}
       
   406 
       
   407 TStreamId CContactStoreField::StoreL(CStreamStore& aStore) const
       
   408     {
       
   409     RStoreWriteStream stream;
       
   410     TStreamId id=stream.CreateLC(aStore);
       
   411 	ExternalizeL(stream);
       
   412     stream.CommitL();
       
   413     CleanupStack::PopAndDestroy();	// stream
       
   414     return id;
       
   415     }
       
   416 
       
   417 void CContactStoreField::RestoreL(CStreamStore&, RReadStream& aStream)
       
   418 	{
       
   419 	InternalizeL(aStream);
       
   420 	}
       
   421 
       
   422 void CContactStoreField::InternalizeL(RReadStream& aStream)
       
   423     {
       
   424     TInt32 length;
       
   425     aStream>>length;
       
   426     if (iThing)
       
   427     	{
       
   428     	delete (iThing);
       
   429     	}
       
   430     if (length)
       
   431     	{
       
   432 		// stream the HBufC descriptor in from the the stream store
       
   433     	iThing=HBufC8::NewLC(aStream,length);
       
   434     	CleanupStack::Pop();// buf
       
   435     	}
       
   436     }
       
   437 
       
   438 void CContactStoreField::ExternalizeL(RWriteStream& aStream) const
       
   439     {
       
   440     if (IsFull())
       
   441     	{
       
   442     	aStream<<TInt32(iThing->Size());
       
   443     	aStream<<*iThing;
       
   444     	}
       
   445     else
       
   446     	{
       
   447     	aStream<<TInt32(0);
       
   448     	}
       
   449     }
       
   450 
       
   451 
       
   452 EXPORT_C void CContactStoreField::SetThingL(const TDesC8& aDes)
       
   453 /** Sets the field data from an 8-bit descriptor.
       
   454 
       
   455 @param aDes 8-bit descriptor containing the binary data to copy into the 
       
   456 field. */
       
   457     {
       
   458     if (iThing)
       
   459     	{
       
   460     	delete (iThing);
       
   461     	}
       
   462 	iThing = aDes.AllocL();
       
   463     }
       
   464 
       
   465 EXPORT_C void CContactStoreField::SetThingL(const HBufC8& aBuf)
       
   466 /** Sets the field data from an 8-bit heap descriptor.
       
   467 
       
   468 @param aDes 8-bit heap descriptor containing the binary data to copy into the field. */
       
   469     {
       
   470     if (iThing)
       
   471     	{
       
   472     	delete (iThing);
       
   473     	}
       
   474 	iThing = aBuf.AllocL();
       
   475     }
       
   476 
       
   477 EXPORT_C void CContactStoreField::SetThingL(const CBufBase* aBuf)
       
   478 /** Sets the field data from a buffer.
       
   479 
       
   480 @param aBuf Buffer containing the binary data to copy into the field. */
       
   481 	{
       
   482 	TInt len=aBuf->Size();
       
   483 	HBufC8* buf=HBufC8::NewLC(len);
       
   484 	TPtr8 ptr=buf->Des();
       
   485 	TInt pos=0;
       
   486 	while (pos<len)
       
   487 		{
       
   488 		ptr.Append(CONST_CAST(CBufBase*,aBuf)->Ptr(pos));
       
   489 		pos=ptr.Length();
       
   490 		}
       
   491 	CleanupStack::Pop(buf);
       
   492     if (iThing)
       
   493     	{
       
   494     	delete (iThing);
       
   495     	}
       
   496 	iThing=buf;
       
   497 	}
       
   498 
       
   499 EXPORT_C HBufC8* CContactStoreField::Thing() const
       
   500 /** Gets a pointer to the binary data stored in the field.
       
   501 
       
   502 @return Pointer to the binary data stored in the field. */
       
   503     {
       
   504     return iThing;
       
   505     }
       
   506 
       
   507 TBool CContactStoreField::IsFull() const
       
   508 	{
       
   509 	return (iThing && iThing->Size()!=0);
       
   510 	}
       
   511 
       
   512 //
       
   513 // class CContactAgentField
       
   514 //
       
   515 
       
   516 // for BC-proofing
       
   517 EXPORT_C /*virtual*/ void CContactAgentField::CContactFieldStorage_Reserved1()
       
   518 	{
       
   519 	}
       
   520 
       
   521 TStreamId CContactAgentField::StoreL(CStreamStore& aStore) const
       
   522     {
       
   523     RStoreWriteStream stream;
       
   524     TStreamId id=stream.CreateLC(aStore);
       
   525 	ExternalizeL(stream);
       
   526     stream.CommitL();
       
   527     CleanupStack::PopAndDestroy();	// stream
       
   528     return id;
       
   529     }
       
   530 
       
   531 void CContactAgentField::RestoreL(CStreamStore& ,RReadStream& aStream)
       
   532     {
       
   533 	InternalizeL(aStream);
       
   534 	}
       
   535 
       
   536 void CContactAgentField::InternalizeL( RReadStream& aStream)
       
   537     {
       
   538     iAgentId=TContactItemId(aStream.ReadInt32L());
       
   539     }
       
   540 
       
   541 void CContactAgentField::ExternalizeL(RWriteStream& aStream) const
       
   542     {
       
   543 	aStream.WriteInt32L(iAgentId);
       
   544     }
       
   545 
       
   546 EXPORT_C void CContactAgentField::SetAgentId(TContactItemId aId)
       
   547 /** 
       
   548 * Sets the agent ID field data.
       
   549 * @param aId The agent ID. Must be > 0 otherwise it won't be set.
       
   550 */
       
   551     {
       
   552     if (aId > 0)
       
   553         {
       
   554         iAgentId = aId;
       
   555         }
       
   556     }
       
   557 
       
   558 EXPORT_C TContactItemId CContactAgentField::Value() const
       
   559 /** Gets the agent ID field data.
       
   560 
       
   561 @return The agent ID. */
       
   562     {
       
   563     return iAgentId;
       
   564     }
       
   565 
       
   566 TBool CContactAgentField::IsFull() const
       
   567 	{
       
   568 	return (iAgentId!=KNullContactId);
       
   569 	}