predictivesearch/PcsUtils/src/CPsData.cpp
changeset 0 e686773b3f54
child 15 e8e3147d53eb
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 // ============================== MEMBER FUNCTIONS ============================
       
    28 
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // CPsData::NewL
       
    32 // Two Phase constructor
       
    33 // ----------------------------------------------------------------------------
       
    34 EXPORT_C CPsData* CPsData::NewL()
       
    35 {
       
    36 	CPsData* self = new (ELeave) CPsData();	
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop();
       
    40     return self;
       
    41 	
       
    42 }
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // CPsData::CPsData
       
    46 // Default constructor
       
    47 // ----------------------------------------------------------------------------
       
    48 CPsData::CPsData()
       
    49 {
       
    50 }
       
    51 
       
    52 // ----------------------------------------------------------------------------
       
    53 // CPsData::ConstructL
       
    54 // Two phase construction
       
    55 // ----------------------------------------------------------------------------
       
    56 void CPsData::ConstructL()
       
    57 {
       
    58     iData = new (ELeave) RPointerArray<HBufC>(1);
       
    59     iGc = new (ELeave) RPointerArray<HBufC>(1);
       
    60     iDataExtension = NULL;
       
    61 }
       
    62 
       
    63 // ----------------------------------------------------------------------------
       
    64 // CPsData::~CPsData
       
    65 // Destructor
       
    66 // ----------------------------------------------------------------------------
       
    67 EXPORT_C CPsData::~CPsData()
       
    68 {
       
    69 	if ( iData )
       
    70 	{
       
    71 		iData->ResetAndDestroy();
       
    72 		delete iData;
       
    73 	}
       
    74 	if(iDataExtension)
       
    75     {
       
    76 		delete iDataExtension;
       
    77 		iDataExtension = NULL;
       
    78     }
       
    79     if(iIntDataExt && iIntDataExt->Count())
       
    80     {
       
    81     	
       
    82     	iIntDataExt->Close();
       
    83     	delete iIntDataExt;
       
    84     }
       
    85     if ( iGc )
       
    86     {
       
    87     	iGc->ResetAndDestroy();
       
    88     	delete iGc;
       
    89     }
       
    90 }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // CPsData::Id
       
    94 // Returns the unique id
       
    95 // ----------------------------------------------------------------------------
       
    96 EXPORT_C  TInt CPsData::Id() const
       
    97 {
       
    98     return iId;
       
    99 }
       
   100 
       
   101 // ----------------------------------------------------------------------------
       
   102 // CPsData::SetId
       
   103 // Sets the unique id
       
   104 // ----------------------------------------------------------------------------
       
   105 EXPORT_C void CPsData::SetId (const TInt aId)
       
   106 {
       
   107 	iId = aId;
       
   108 }
       
   109 
       
   110 // ----------------------------------------------------------------------------
       
   111 // CPsData::UriId
       
   112 // Returns the unique URI id
       
   113 // ----------------------------------------------------------------------------
       
   114 EXPORT_C  TUint8 CPsData::UriId() const
       
   115 {
       
   116     return iUriId;
       
   117 }
       
   118 
       
   119 // ----------------------------------------------------------------------------
       
   120 // CPsData::SetUriId
       
   121 // Sets the unique URI id
       
   122 // ----------------------------------------------------------------------------
       
   123 EXPORT_C void CPsData::SetUriId(const TUint8 aUriId)
       
   124 {
       
   125 	iUriId = aUriId;
       
   126 }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 // CPsData::Data1
       
   130 // Returns the data1
       
   131 // ----------------------------------------------------------------------------
       
   132 EXPORT_C HBufC* CPsData::Data(TInt aIndex) const
       
   133 {
       
   134     if ( aIndex >= iData->Count() )    
       
   135          return NULL;
       
   136     
       
   137     return (*iData)[aIndex];
       
   138 }
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CPsData::SetData1L
       
   142 // Sets the data1
       
   143 // ----------------------------------------------------------------------------
       
   144 EXPORT_C void CPsData::SetDataL(TInt aIndex, const TDesC& aData)
       
   145 {   
       
   146     // Every call to insert makes the array to grow.
       
   147     // This check is required to optimize memory.
       
   148     if ( aIndex < iData->Count() )
       
   149     {
       
   150        //delete (*iData)[aIndex];     // Commented as it is delaying the contact caching as well as search time     
       
   151        iGc->Append((*iData)[aIndex]); // Instead append to garbage collector for cleanup later
       
   152        (*iData)[aIndex] = aData.AllocL();
       
   153     }
       
   154     else 
       
   155     {  
       
   156        iData->InsertL( aData.AllocL(), aIndex);
       
   157     }
       
   158 }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CPsData::ExternalizeL
       
   162 // Writes 'this' to aStream
       
   163 // ----------------------------------------------------------------------------
       
   164 EXPORT_C void CPsData::ExternalizeL(RWriteStream& aStream) const
       
   165 {
       
   166     aStream.WriteInt16L(iId); // Write unique ID to the stream
       
   167     
       
   168     aStream.WriteUint8L(iUriId); // Write URI ID to the stream
       
   169  
       
   170     aStream.WriteUint8L(iDataMatches); // Write data matches attribute 
       
   171     
       
   172     aStream.WriteUint8L(iData->Count()); // Number of data elements
       
   173 
       
   174 	for ( int index = 0; index < iData->Count(); index++ )
       
   175 	{
       
   176 	    HBufC* data = (*iData)[index];
       
   177 	    if ( data ) // Write data to the stream (or a NULL descriptor)
       
   178 	    {
       
   179 	    	aStream.WriteInt16L( data->Size() ); // data size
       
   180 			aStream << *data; // data
       
   181 	    }
       
   182 	    else
       
   183 	    {
       
   184 	    	aStream.WriteInt16L( 0 );
       
   185 	    	aStream << KNullDesC;
       
   186 	    }	    	    	
       
   187 	}
       
   188 	
       
   189 	if ( iDataExtension )
       
   190     {
       
   191         HBufC8* dataExt = (HBufC8*)iDataExtension;
       
   192     	aStream.WriteUint8L( dataExt->Size() );
       
   193     	aStream << *dataExt;
       
   194     }
       
   195     else 
       
   196     {
       
   197     	aStream.WriteUint8L( 0 );
       
   198     	aStream << KNullDesC8;	    	
       
   199     }
       
   200     
       
   201     TInt intDataExtCount = 0;
       
   202     if (iIntDataExt)
       
   203     {
       
   204         intDataExtCount = iIntDataExt->Count();
       
   205     }
       
   206     if(intDataExtCount)
       
   207     {
       
   208     	aStream.WriteInt32L(intDataExtCount);
       
   209     	for(TInt i =0 ; i < intDataExtCount;  i++ )
       
   210 	  	    aStream.WriteInt32L((*iIntDataExt)[i]);
       
   211     }
       
   212     else
       
   213     {
       
   214     	aStream.WriteInt32L(0);
       
   215     	
       
   216     }
       
   217 }
       
   218 
       
   219 // ----------------------------------------------------------------------------
       
   220 // CPsData::InternalizeL
       
   221 // Initializes 'this' with the contents of aStream
       
   222 // ----------------------------------------------------------------------------
       
   223 EXPORT_C void CPsData::InternalizeL(RReadStream& aStream)
       
   224 {
       
   225     // Read unique id
       
   226     TInt tmp = aStream.ReadInt16L();
       
   227     iId = tmp;
       
   228     
       
   229     // Read the URI Id
       
   230     iUriId = aStream.ReadUint8L();    
       
   231     
       
   232     // Read data matches
       
   233     iDataMatches = aStream.ReadUint8L();
       
   234 
       
   235     // Read number of data elements
       
   236     TInt szNumElements = aStream.ReadUint8L();
       
   237     
       
   238     for ( int index = 0; index < szNumElements; index++ )
       
   239     {
       
   240         // Read data size
       
   241 	    TInt szData = aStream.ReadInt16L();
       
   242     	    
       
   243     	HBufC* data =  HBufC::NewL(aStream, szData);
       
   244 	    iData->InsertL(data, index);	 
       
   245     }
       
   246 	
       
   247 	// Read data extension size
       
   248 	TUint8 szDataExt = aStream.ReadUint8L();
       
   249     if(iDataExtension)
       
   250     {
       
   251         delete iDataExtension;
       
   252         iDataExtension = NULL;
       
   253     }
       
   254     
       
   255     HBufC8* dataExt =  HBufC8::NewL(aStream, szDataExt);
       
   256     iDataExtension = dataExt;
       
   257     
       
   258     TInt intDataExtCount = aStream.ReadInt32L();
       
   259     if(intDataExtCount)
       
   260     {
       
   261         if(!iIntDataExt)
       
   262 		{
       
   263 		 iIntDataExt = new (ELeave) RArray<TInt> (1);	
       
   264 		}
       
   265 	 
       
   266     	for(TInt i =0 ; i < intDataExtCount;  i++ )
       
   267     	{
       
   268     	    TInt tempDataExt = aStream.ReadInt32L();
       
   269     	    iIntDataExt->Append(tempDataExt);
       
   270     	}
       
   271     }
       
   272     else
       
   273     {
       
   274     	// Do Nothing since there are no elements
       
   275     }
       
   276 }
       
   277 
       
   278 // ----------------------------------------------------------------------------
       
   279 // CPsData::CompareByData
       
   280 // Compare function
       
   281 // ----------------------------------------------------------------------------
       
   282 EXPORT_C TInt CPsData::CompareByData( const CPsData& aObject1, 
       
   283                                       const CPsData& aObject2 )
       
   284 {
       
   285 	// Get the standard method
       
   286     TCollationMethod meth = *Mem::CollationMethodByIndex( 0 );
       
   287     meth.iFlags |= TCollationMethod::EIgnoreNone;
       
   288     meth.iFlags |= TCollationMethod::EFoldCase;
       
   289 
       
   290     HBufC* str1;
       
   291     HBufC* str2 ;
       
   292 
       
   293     TInt compareRes(0);
       
   294     for ( int cnt1 = 0, cnt2 = 0;
       
   295           (cnt1 < aObject1.DataElementCount()) && (cnt2 < aObject2.DataElementCount());
       
   296           cnt1++, cnt2++ )
       
   297         {
       
   298         if ( aObject1.Data(cnt1) )
       
   299             {
       
   300             //data1 += aObject1.Data(i)->Des();
       
   301             str1 =  aObject1.Data(cnt1)->Des().AllocL();    
       
   302             str1->Des().TrimAll();
       
   303             }	
       
   304         else
       
   305             {
       
   306             str1 = HBufC::NewL(2);
       
   307             str1->Des().Copy(KNullDesC);
       
   308             }
       
   309         CleanupStack::PushL( str1 );
       
   310         if ( aObject2.Data(cnt2) )
       
   311             {
       
   312             str2 = aObject2.Data(cnt2)->Des().AllocL();
       
   313             str2->Des().TrimAll();
       
   314             }	     
       
   315         else
       
   316             {
       
   317             str2 = HBufC::NewL(2);
       
   318             str2->Des().Copy(KNullDesC);
       
   319             }
       
   320         CleanupStack::PushL( str2 );
       
   321         compareRes = str1->Des().CompareC( str2->Des(), 3, &meth );
       
   322         CleanupStack::PopAndDestroy(str2);
       
   323         CleanupStack::PopAndDestroy(str1);
       
   324         if( !compareRes )
       
   325             {
       
   326             break;
       
   327 			}
       
   328         }
       
   329     return compareRes;
       
   330 }
       
   331 
       
   332 // ----------------------------------------------------------------------------
       
   333 // CPsData::CompareById
       
   334 // Compare function
       
   335 // ----------------------------------------------------------------------------
       
   336 EXPORT_C TBool CPsData::CompareById(const CPsData& aObject1, const CPsData& aObject2)
       
   337 {
       
   338 	if(aObject1.Id() == aObject2.Id())
       
   339 	{
       
   340 		return ETrue;
       
   341 	}
       
   342 	
       
   343 	return EFalse;
       
   344 }
       
   345 				     
       
   346 
       
   347 // ----------------------------------------------------------------------------
       
   348 // CPsData::IsDataMatch
       
   349 // Return TRUE if data is matched for predictive search
       
   350 // ----------------------------------------------------------------------------		
       
   351 EXPORT_C TBool CPsData::IsDataMatch(TInt aIndex)
       
   352 {	
       
   353     TReal val;
       
   354 	Math::Pow(val, 2, aIndex);
       
   355 
       
   356 	return(iDataMatches & (TUint8)val);
       
   357 }
       
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // CPsData:DataMatch
       
   361 // Return the data match attribute
       
   362 // ----------------------------------------------------------------------------		
       
   363 EXPORT_C TUint8 CPsData::DataMatch()
       
   364 {
       
   365     return iDataMatches;
       
   366 }
       
   367 
       
   368 // ----------------------------------------------------------------------------
       
   369 // CPsData::SetData1Match
       
   370 // 
       
   371 // ----------------------------------------------------------------------------
       
   372 EXPORT_C void CPsData::SetDataMatch(TInt aIndex)
       
   373 {	
       
   374     TReal val;
       
   375 	Math::Pow(val, 2, aIndex);
       
   376 
       
   377 	iDataMatches |= (TUint8)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 }