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