predictivesearch/PcsUtils/src/CPsData.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Utility class to hold data for predictive search.
       
    15 *                Used to marshal data between the client, server and data
       
    16 *                plugins.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <s32mem.h>
       
    23 #include <collate.h>
       
    24 
       
    25 #include "CPsData.h"
       
    26 #include "CPcsDefs.h"
       
    27 
       
    28 // ============================== MEMBER FUNCTIONS ============================
       
    29 
       
    30 
       
    31 // ----------------------------------------------------------------------------
       
    32 // CPsData::NewL
       
    33 // Two Phase constructor
       
    34 // ----------------------------------------------------------------------------
       
    35 EXPORT_C CPsData* CPsData::NewL()
       
    36 {
       
    37 	CPsData* self = new (ELeave) CPsData();	
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop();
       
    41     return self;
       
    42 	
       
    43 }
       
    44 
       
    45 // ----------------------------------------------------------------------------
       
    46 // CPsData::CPsData
       
    47 // Default constructor
       
    48 // ----------------------------------------------------------------------------
       
    49 CPsData::CPsData()
       
    50 {
       
    51 }
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // CPsData::ConstructL
       
    55 // Two phase construction
       
    56 // ----------------------------------------------------------------------------
       
    57 void CPsData::ConstructL()
       
    58 {
       
    59     iData = new (ELeave) RPointerArray<HBufC>(1);
       
    60     iGc = new (ELeave) RPointerArray<HBufC>(1);
       
    61     iDataExtension = NULL;
       
    62 }
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CPsData::~CPsData
       
    66 // Destructor
       
    67 // ----------------------------------------------------------------------------
       
    68 EXPORT_C CPsData::~CPsData()
       
    69 {
       
    70 	if ( iData )
       
    71 	{
       
    72 		iData->ResetAndDestroy();
       
    73 		delete iData;
       
    74 	}
       
    75 	if(iDataExtension)
       
    76     {
       
    77 		delete iDataExtension;
       
    78 		iDataExtension = NULL;
       
    79     }
       
    80     if(iIntDataExt && iIntDataExt->Count())
       
    81     {
       
    82     	
       
    83     	iIntDataExt->Close();
       
    84     	delete iIntDataExt;
       
    85     }
       
    86     if ( iGc )
       
    87     {
       
    88     	iGc->ResetAndDestroy();
       
    89     	delete iGc;
       
    90     }
       
    91 }
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // CPsData::Id
       
    95 // Returns the unique id
       
    96 // ----------------------------------------------------------------------------
       
    97 EXPORT_C  TInt CPsData::Id() const
       
    98 {
       
    99     return iId;
       
   100 }
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 // CPsData::SetId
       
   104 // Sets the unique id
       
   105 // ----------------------------------------------------------------------------
       
   106 EXPORT_C void CPsData::SetId (const TInt aId)
       
   107 {
       
   108 	iId = aId;
       
   109 }
       
   110 
       
   111 // ----------------------------------------------------------------------------
       
   112 // CPsData::UriId
       
   113 // Returns the unique URI id
       
   114 // ----------------------------------------------------------------------------
       
   115 EXPORT_C  TUint8 CPsData::UriId() const
       
   116 {
       
   117     return iUriId;
       
   118 }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // CPsData::SetUriId
       
   122 // Sets the unique URI id
       
   123 // ----------------------------------------------------------------------------
       
   124 EXPORT_C void CPsData::SetUriId(const TUint8 aUriId)
       
   125 {
       
   126 	iUriId = aUriId;
       
   127 }
       
   128 
       
   129 // ----------------------------------------------------------------------------
       
   130 // CPsData::Data1
       
   131 // Returns the data1
       
   132 // ----------------------------------------------------------------------------
       
   133 EXPORT_C HBufC* CPsData::Data(TInt aIndex) const
       
   134 {
       
   135     if ( aIndex >= iData->Count() )    
       
   136          return NULL;
       
   137     
       
   138     return (*iData)[aIndex];
       
   139 }
       
   140 
       
   141 // ----------------------------------------------------------------------------
       
   142 // CPsData::SetData1L
       
   143 // Sets the data1
       
   144 // ----------------------------------------------------------------------------
       
   145 EXPORT_C void CPsData::SetDataL(TInt aIndex, const TDesC& aData)
       
   146 {   
       
   147     // Every call to insert makes the array to grow.
       
   148     // This check is required to optimize memory.
       
   149     if ( aIndex < iData->Count() )
       
   150     {
       
   151        //delete (*iData)[aIndex];     // Commented as it is delaying the contact caching as well as search time     
       
   152        iGc->Append((*iData)[aIndex]); // Instead append to garbage collector for cleanup later
       
   153        (*iData)[aIndex] = aData.AllocL();
       
   154     }
       
   155     else 
       
   156     {  
       
   157        iData->InsertL( aData.AllocL(), aIndex);
       
   158     }
       
   159 }
       
   160 
       
   161 // ----------------------------------------------------------------------------
       
   162 // CPsData::ExternalizeL
       
   163 // Writes 'this' to aStream
       
   164 // ----------------------------------------------------------------------------
       
   165 EXPORT_C void CPsData::ExternalizeL(RWriteStream& aStream) const
       
   166 {
       
   167     aStream.WriteInt16L(iId); // Write unique ID to the stream
       
   168     
       
   169     aStream.WriteUint8L(iUriId); // Write URI ID to the stream
       
   170  
       
   171     aStream.WriteUint8L(iDataMatches); // Write data matches attribute 
       
   172     
       
   173     aStream.WriteUint8L(iData->Count()); // Number of data elements
       
   174 
       
   175 	for ( int index = 0; index < iData->Count(); index++ )
       
   176 	{
       
   177 	    HBufC* data = (*iData)[index];
       
   178 	    if ( data ) // Write data to the stream (or a NULL descriptor)
       
   179 	    {
       
   180 	    	aStream.WriteInt16L( data->Size() ); // data size
       
   181 			aStream << *data; // data
       
   182 	    }
       
   183 	    else
       
   184 	    {
       
   185 	    	aStream.WriteInt16L( 0 );
       
   186 	    	aStream << KNullDesC;
       
   187 	    }	    	    	
       
   188 	}
       
   189 	
       
   190 	if ( iDataExtension )
       
   191     {
       
   192         HBufC8* dataExt = (HBufC8*)iDataExtension;
       
   193     	aStream.WriteUint8L( dataExt->Size() );
       
   194     	aStream << *dataExt;
       
   195     }
       
   196     else 
       
   197     {
       
   198     	aStream.WriteUint8L( 0 );
       
   199     	aStream << KNullDesC8;	    	
       
   200     }
       
   201     
       
   202     TInt intDataExtCount = 0;
       
   203     if (iIntDataExt)
       
   204     {
       
   205         intDataExtCount = iIntDataExt->Count();
       
   206     }
       
   207     if(intDataExtCount)
       
   208     {
       
   209     	aStream.WriteInt32L(intDataExtCount);
       
   210     	for(TInt i =0 ; i < intDataExtCount;  i++ )
       
   211 	  	    aStream.WriteInt32L((*iIntDataExt)[i]);
       
   212     }
       
   213     else
       
   214     {
       
   215     	aStream.WriteInt32L(0);
       
   216     	
       
   217     }
       
   218 }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // CPsData::InternalizeL
       
   222 // Initializes 'this' with the contents of aStream
       
   223 // ----------------------------------------------------------------------------
       
   224 EXPORT_C void CPsData::InternalizeL(RReadStream& aStream)
       
   225 {
       
   226     // Read unique id
       
   227     TInt tmp = aStream.ReadInt16L();
       
   228     iId = tmp;
       
   229     
       
   230     // Read the URI Id
       
   231     iUriId = aStream.ReadUint8L();    
       
   232     
       
   233     // Read data matches
       
   234     iDataMatches = aStream.ReadUint8L();
       
   235 
       
   236     // Read number of data elements
       
   237     TInt szNumElements = aStream.ReadUint8L();
       
   238     
       
   239     for ( int index = 0; index < szNumElements; index++ )
       
   240     {
       
   241         // Read data size
       
   242 	    TInt szData = aStream.ReadInt16L();
       
   243     	    
       
   244     	HBufC* data =  HBufC::NewL(aStream, szData);
       
   245 	    iData->InsertL(data, index);	 
       
   246     }
       
   247 	
       
   248 	// Read data extension size
       
   249 	TUint8 szDataExt = aStream.ReadUint8L();
       
   250     if(iDataExtension)
       
   251     {
       
   252         delete iDataExtension;
       
   253         iDataExtension = NULL;
       
   254     }
       
   255     
       
   256     HBufC8* dataExt =  HBufC8::NewL(aStream, szDataExt);
       
   257     iDataExtension = dataExt;
       
   258     
       
   259     TInt intDataExtCount = aStream.ReadInt32L();
       
   260     if(intDataExtCount)
       
   261     {
       
   262         if(!iIntDataExt)
       
   263 		{
       
   264 		 iIntDataExt = new (ELeave) RArray<TInt> (1);	
       
   265 		}
       
   266 	 
       
   267     	for(TInt i =0 ; i < intDataExtCount;  i++ )
       
   268     	{
       
   269     	    TInt tempDataExt = aStream.ReadInt32L();
       
   270     	    iIntDataExt->Append(tempDataExt);
       
   271     	}
       
   272     }
       
   273     else
       
   274     {
       
   275     	// Do Nothing since there are no elements
       
   276     }
       
   277 }
       
   278 
       
   279 // ----------------------------------------------------------------------------
       
   280 // CPsData::CompareByData
       
   281 // Compare function
       
   282 // ----------------------------------------------------------------------------
       
   283 EXPORT_C TInt CPsData::CompareByData( const CPsData& aObject1, 
       
   284                                       const CPsData& aObject2 )
       
   285 {
       
   286 	// Get the standard method
       
   287     TCollationMethod meth = *Mem::CollationMethodByIndex( 0 );
       
   288     meth.iFlags |= TCollationMethod::EIgnoreNone;
       
   289     meth.iFlags |= TCollationMethod::EFoldCase;
       
   290 
       
   291     HBufC* str1;
       
   292     HBufC* str2 ;
       
   293 
       
   294     TInt compareRes(0);
       
   295     for ( int cnt1 = 0, cnt2 = 0;
       
   296           (cnt1 < aObject1.DataElementCount()) && (cnt2 < aObject2.DataElementCount());
       
   297           cnt1++, cnt2++ )
       
   298         {
       
   299         if ( aObject1.Data(cnt1) )
       
   300             {
       
   301             //data1 += aObject1.Data(i)->Des();
       
   302             str1 =  aObject1.Data(cnt1)->Des().AllocL();    
       
   303             str1->Des().TrimAll();
       
   304             }	
       
   305         else
       
   306             {
       
   307             str1 = HBufC::NewL(2);
       
   308             str1->Des().Copy(KNullDesC);
       
   309             }
       
   310         CleanupStack::PushL( str1 );
       
   311         if ( aObject2.Data(cnt2) )
       
   312             {
       
   313             str2 = aObject2.Data(cnt2)->Des().AllocL();
       
   314             str2->Des().TrimAll();
       
   315             }	     
       
   316         else
       
   317             {
       
   318             str2 = HBufC::NewL(2);
       
   319             str2->Des().Copy(KNullDesC);
       
   320             }
       
   321         CleanupStack::PushL( str2 );
       
   322         compareRes = str1->Des().CompareC( str2->Des(), 3, &meth );
       
   323         CleanupStack::PopAndDestroy(str2);
       
   324         CleanupStack::PopAndDestroy(str1);
       
   325         if( !compareRes )
       
   326             {
       
   327             break;
       
   328             }
       
   329         }
       
   330     return compareRes;
       
   331 }
       
   332 
       
   333 // ----------------------------------------------------------------------------
       
   334 // CPsData::CompareById
       
   335 // Compare function
       
   336 // ----------------------------------------------------------------------------
       
   337 EXPORT_C TBool CPsData::CompareById(const CPsData& aObject1, const CPsData& aObject2)
       
   338 {
       
   339     // Both Id() and UriId() must match if data objects represent the same contact.    
       
   340     // Values of Id() are guaranteed to be unique only within one store. 
       
   341     if ( aObject1.Id() == aObject2.Id() && aObject1.UriId() == aObject2.UriId() )  
       
   342         {      
       
   343             return ETrue;       
       
   344         }        
       
   345     return EFalse;
       
   346 }
       
   347 
       
   348 
       
   349 // ----------------------------------------------------------------------------
       
   350 // CPsData::IsDataMatch
       
   351 // Return TRUE if data is matched for predictive search
       
   352 // ----------------------------------------------------------------------------		
       
   353 EXPORT_C TBool CPsData::IsDataMatch(TInt aIndex)
       
   354 {
       
   355     __ASSERT_DEBUG( aIndex < 8, User::Panic( _L("CPsData"), KErrOverflow ) );
       
   356     TUint8 val = 1 << aIndex;
       
   357     return (iDataMatches & val);
       
   358 }
       
   359 
       
   360 // ----------------------------------------------------------------------------
       
   361 // CPsData:DataMatch
       
   362 // Return the data match attribute
       
   363 // ----------------------------------------------------------------------------		
       
   364 EXPORT_C TUint8 CPsData::DataMatch()
       
   365 {
       
   366     return iDataMatches;
       
   367 }
       
   368 
       
   369 // ----------------------------------------------------------------------------
       
   370 // CPsData::SetData1Match
       
   371 // 
       
   372 // ----------------------------------------------------------------------------
       
   373 EXPORT_C void CPsData::SetDataMatch(TInt aIndex)
       
   374 {
       
   375     __ASSERT_DEBUG( aIndex < 8, User::Panic( _L("CPsData"), KErrOverflow ) );
       
   376     TUint8 val = 1 << aIndex;
       
   377     iDataMatches |= val;
       
   378 }
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // CPsData::ClearDataMatches
       
   382 // 
       
   383 // ----------------------------------------------------------------------------
       
   384 EXPORT_C void CPsData::ClearDataMatches()
       
   385 {
       
   386 	iDataMatches = 0x0;
       
   387 }
       
   388 
       
   389 // ----------------------------------------------------------------------------
       
   390 // CPsData::DataElementCount
       
   391 // 
       
   392 // ----------------------------------------------------------------------------
       
   393 EXPORT_C TInt CPsData::DataElementCount() const
       
   394 {
       
   395 	return (iData->Count());
       
   396 }
       
   397 
       
   398 
       
   399 // ----------------------------------------------------------------------------
       
   400 // CPsData::DataExtension
       
   401 // 
       
   402 // ----------------------------------------------------------------------------
       
   403 EXPORT_C TAny* CPsData::DataExtension() const
       
   404 {
       
   405 	return iDataExtension;
       
   406 }
       
   407 
       
   408 // ----------------------------------------------------------------------------
       
   409 // CPsData::SetDataExtension
       
   410 // 
       
   411 // ----------------------------------------------------------------------------
       
   412 EXPORT_C void CPsData::SetDataExtension(TAny* aDataExt)
       
   413 {
       
   414 	iDataExtension = aDataExt;
       
   415 }
       
   416 
       
   417 // ----------------------------------------------------------------------------
       
   418 // CPsData::AddIntDataExtL
       
   419 // 
       
   420 // ----------------------------------------------------------------------------
       
   421 EXPORT_C void CPsData::AddIntDataExtL(TInt aDataExt)
       
   422 {	if(!iIntDataExt)
       
   423 	{
       
   424 		 iIntDataExt = new (ELeave) RArray<TInt> (1);	
       
   425 	}
       
   426 	
       
   427 	iIntDataExt->Append(aDataExt);
       
   428 }
       
   429 
       
   430 // ----------------------------------------------------------------------------
       
   431 // CPsData::RemoveIntDataExt
       
   432 // 
       
   433 // ----------------------------------------------------------------------------
       
   434 EXPORT_C void CPsData::RemoveIntDataExt(TInt aIndex)
       
   435 {
       
   436 	if(iIntDataExt && (iIntDataExt->Count() > aIndex))
       
   437 	{
       
   438 		iIntDataExt->Remove(aIndex);
       
   439 	}
       
   440 }
       
   441 
       
   442 // ----------------------------------------------------------------------------
       
   443 // CPsData::IntDataExt
       
   444 // 
       
   445 // ----------------------------------------------------------------------------
       
   446 EXPORT_C void CPsData::IntDataExt(RArray<TInt>& aDataExtArray)
       
   447 {   
       
   448 	aDataExtArray.Reset();
       
   449 	if(iIntDataExt)
       
   450 	{
       
   451 		for(TInt i =0 ; i < iIntDataExt->Count(); i++ )
       
   452 		  	aDataExtArray.Insert((*iIntDataExt)[i],0);
       
   453 	}
       
   454 }