predictivesearch/PcsUtils/src/CPsSettings.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 store settings for predictive search.
       
    15 *                Used to set the desired data stores to search and 
       
    16 *                the display fields for predictive search.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <s32mem.h>
       
    23 #include "CPsSettings.h"
       
    24 
       
    25 // ============================== MEMBER FUNCTIONS ============================
       
    26 
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CPsSettings::NewL
       
    30 // Two Phase constructor
       
    31 // ----------------------------------------------------------------------------
       
    32 EXPORT_C CPsSettings* CPsSettings::NewL()
       
    33 {
       
    34 	CPsSettings* self = new (ELeave) CPsSettings();	
       
    35     CleanupStack::PushL(self);
       
    36     self->ConstructL();
       
    37     CleanupStack::Pop();
       
    38     return self;
       
    39 }
       
    40 
       
    41 // ----------------------------------------------------------------------------
       
    42 // CPsSettings::CPsSettings
       
    43 // Default constructor
       
    44 // ----------------------------------------------------------------------------
       
    45 CPsSettings::CPsSettings()
       
    46 {
       
    47 }
       
    48 
       
    49 // ----------------------------------------------------------------------------
       
    50 // CPsSettings::ConstructL
       
    51 // Two phase construction
       
    52 // ----------------------------------------------------------------------------
       
    53 void CPsSettings::ConstructL()
       
    54 {
       
    55    iMaxResults = -1;
       
    56    iSortType = EAlphabetical;
       
    57 }
       
    58 
       
    59 
       
    60 // ----------------------------------------------------------------------------
       
    61 // CPsSettings::CloneL
       
    62 // Returns the clone.
       
    63 // ------------------------------------------------------------------------
       
    64 EXPORT_C CPsSettings*  CPsSettings::CloneL() const
       
    65 {
       
    66     CPsSettings* self = CPsSettings::NewL();	
       
    67     CleanupStack::PushL(self);
       
    68     // Set the Max reslts
       
    69 	self->SetMaxResults(iMaxResults);
       
    70 	self->SetSortType(iSortType);
       
    71     
       
    72     // Copy the search uris
       
    73     RPointerArray<TDesC> newUris;
       
    74 
       
    75     for(TInt i =0; i < iSearchUri.Count() ; i++)
       
    76 	{
       
    77 		newUris.Append(iSearchUri[i]->AllocL());
       
    78 	}
       
    79     self->SetSearchUrisL(newUris);
       
    80     self->SetSearchUrisL(iSearchUri);
       
    81     
       
    82     // Set the display fields
       
    83     self->SetDisplayFieldsL(iDisplayFields);
       
    84      
       
    85     newUris.ResetAndDestroy();
       
    86     
       
    87     CleanupStack::Pop();
       
    88     return self;
       
    89 }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CPsSettings::~CPsSettings
       
    93 // Destructor
       
    94 // ----------------------------------------------------------------------------
       
    95 EXPORT_C CPsSettings::~CPsSettings()
       
    96 {
       
    97 	iSearchUri.ResetAndDestroy();
       
    98 	iDisplayFields.Reset();
       
    99 }
       
   100 
       
   101 // ----------------------------------------------------------------------------
       
   102 // CPsSettings::SetSearchUrisL
       
   103 // Sets the list of URIs to search from.
       
   104 // ----------------------------------------------------------------------------
       
   105 EXPORT_C void CPsSettings::SetSearchUrisL(const RPointerArray<TDesC>& aSearchUri)
       
   106 {   
       
   107 	iSearchUri.ResetAndDestroy();
       
   108     for(TInt i =0 ; i < aSearchUri.Count();i++)
       
   109 	{
       
   110 		const HBufC* uriToAppend = aSearchUri[i]->AllocL();
       
   111 		iSearchUri.Append(uriToAppend);
       
   112 	}
       
   113 }
       
   114 
       
   115 // ----------------------------------------------------------------------------
       
   116 // CPsSettings::SetDisplayFieldsL
       
   117 // Sets the list of fields to display.
       
   118 // ----------------------------------------------------------------------------
       
   119 EXPORT_C void CPsSettings::SetDisplayFieldsL(const RArray<TInt>& aDisplayFields)
       
   120 {
       
   121 	iDisplayFields.Reset();
       
   122     for(TInt i =0 ; i < aDisplayFields.Count();i++)
       
   123 	{
       
   124 		iDisplayFields.Append(aDisplayFields[i]);
       
   125 	}
       
   126 }
       
   127 
       
   128 // ----------------------------------------------------------------------------
       
   129 // CPsSettings::SetMaxResults
       
   130 // Sets the number of elements (contacts) to be given to client.
       
   131 // ----------------------------------------------------------------------------
       
   132 EXPORT_C void CPsSettings::SetMaxResults(const TInt aMaxResults)
       
   133 {
       
   134 	if(aMaxResults >= -1)
       
   135 	{
       
   136 		iMaxResults = aMaxResults;
       
   137 	}
       
   138 	else
       
   139 	{
       
   140 		iMaxResults = -1;
       
   141 	}	
       
   142 }
       
   143 
       
   144 // ----------------------------------------------------------------------------
       
   145 // CPsSettings::SetSortType
       
   146 // Sets the sort type for the search
       
   147 // ----------------------------------------------------------------------------
       
   148 EXPORT_C void CPsSettings::SetSortType(const TSortType aSortType)
       
   149 {
       
   150 	iSortType = aSortType;
       
   151 }
       
   152 
       
   153 // CPsSettings::SearchUrisL
       
   154 // Returns the list of URIs configured to search from.
       
   155 // ----------------------------------------------------------------------------
       
   156 EXPORT_C void CPsSettings:: SearchUrisL(RPointerArray<TDesC>& aSearchUri) const
       
   157 {
       
   158     //Cleanup already existing memory
       
   159     for(TInt i =0 ; i < iSearchUri.Count();i++)
       
   160 	{
       
   161 		const HBufC* uriToAppend =iSearchUri[i]->AllocL();
       
   162 		aSearchUri.Append(uriToAppend);
       
   163 	}
       
   164 }
       
   165 
       
   166 // ----------------------------------------------------------------------------
       
   167 // CPsSettings::DisplayFieldsL
       
   168 // Returns the list of fields to display.
       
   169 // ----------------------------------------------------------------------------
       
   170 EXPORT_C void CPsSettings:: DisplayFieldsL(RArray<TInt>& aDisplayFields) const
       
   171 {
       
   172     for(TInt i =0 ; i < iDisplayFields.Count();i++)
       
   173 	{
       
   174 		aDisplayFields.Append(iDisplayFields[i]);
       
   175 	}
       
   176 }
       
   177 
       
   178 // ----------------------------------------------------------------------------
       
   179 // CPsSettings::MaxResults
       
   180 // Returns the number of elements (contacts) to be given to client.
       
   181 // ----------------------------------------------------------------------------
       
   182 EXPORT_C TInt CPsSettings::MaxResults() const
       
   183 {
       
   184 	return iMaxResults;
       
   185 }
       
   186 
       
   187 // ----------------------------------------------------------------------------
       
   188 // CPsSettings::GetSortType
       
   189 // Returns the sort type based on which results will be sorted.
       
   190 // ----------------------------------------------------------------------------
       
   191 EXPORT_C TSortType CPsSettings::GetSortType() const
       
   192 {
       
   193 	return iSortType;
       
   194 }
       
   195 
       
   196 // ----------------------------------------------------------------------------
       
   197 // CPsSettings::GetGroupIdsL
       
   198 // Returns the array  groupId from the URIs specified in the settings
       
   199 // ----------------------------------------------------------------------------
       
   200 EXPORT_C void CPsSettings::GetGroupIdsL(RArray<TInt>& aGroupIdArray) 
       
   201 {
       
   202     aGroupIdArray.Reset();
       
   203     
       
   204     // The format for groups URI is "cntdb:://group.gdb/group?=<groupId>"
       
   205     // We use "=" to identify if it is  a group URI
       
   206     TChar equalToCharacter('=');
       
   207     
       
   208     // Parse through all the URIs
       
   209     for ( TInt i =0; i < iSearchUri.Count(); i++ ) 
       
   210     {	    	
       
   211     	TInt offset = iSearchUri[i]->Locate(equalToCharacter);
       
   212 		
       
   213 		if ( offset != KErrNotFound )
       
   214 		{
       
   215 			// Parse and Extract the group name
       
   216 			TInt len = iSearchUri[i]->Length();
       
   217 			HBufC* grpIdBuf = iSearchUri[i]->Right(len - offset - 1).AllocL();
       
   218 
       
   219 			// Convert to decimal 
       
   220 			TInt grpIdVal = 0;
       
   221 			TLex16 myDocId(*grpIdBuf);
       
   222 			TInt err = myDocId.Val(grpIdVal);
       
   223 			 	
       
   224 			// Append to aGroupIdArray, if conversion is successful
       
   225 			if( KErrNone == err)
       
   226 			{
       
   227 				aGroupIdArray.Append(grpIdVal);
       
   228 			}
       
   229 				   
       
   230 			// Cleanup memory
       
   231 			delete   grpIdBuf;
       
   232 			grpIdBuf = NULL;
       
   233 		}
       
   234 	}		
       
   235 }
       
   236 
       
   237 // ----------------------------------------------------------------------------
       
   238 // CPsSettings::ExternalizeL
       
   239 // Writes 'this' to aStream
       
   240 // ----------------------------------------------------------------------------
       
   241 EXPORT_C void CPsSettings::ExternalizeL(RWriteStream& aStream) const
       
   242 {     
       
   243     aStream.WriteUint8L(iSearchUri.Count()); // Number of databse URIs
       
   244 
       
   245 	for ( int index = 0; index < iSearchUri.Count(); index++ )
       
   246 	{
       
   247 	    HBufC* uri = iSearchUri[index]->AllocL();
       
   248 	    if ( uri ) // Write uri to the stream (or a NULL descriptor)
       
   249 	    {
       
   250 	    	aStream.WriteUint8L( uri->Size() ); // uri size
       
   251 			aStream << *uri; // uri
       
   252 	    }
       
   253 	    else
       
   254 	    {
       
   255 	    	aStream.WriteUint8L( 0 );
       
   256 	    	aStream << KNullDesC8;
       
   257 	    }
       
   258 	    delete uri;
       
   259 	    uri = NULL;	    	
       
   260 	}
       
   261 	
       
   262     aStream.WriteUint8L(iDisplayFields.Count()); // Number of display fields
       
   263 	for ( int index = 0; index < iDisplayFields.Count(); index++ )
       
   264 	{
       
   265 		aStream.WriteInt16L(iDisplayFields[index]);
       
   266 	}
       
   267 
       
   268     aStream.WriteInt16L(iMaxResults); // Number of contacts that will be displayed to the client
       
   269     aStream.WriteInt8L(iSortType); // Set the sort type
       
   270 }
       
   271 
       
   272 // ----------------------------------------------------------------------------
       
   273 // CPsSettings::InternalizeL
       
   274 // Initializes 'this' with the contents of aStream
       
   275 // ----------------------------------------------------------------------------
       
   276 EXPORT_C void CPsSettings::InternalizeL(RReadStream& aStream)
       
   277 {
       
   278     // Read number of database URIs
       
   279     TInt szNumUri = aStream.ReadUint8L();
       
   280     
       
   281     for ( int index = 0; index < szNumUri; index++ )
       
   282     {
       
   283         // Read uri size
       
   284 	    TUint8 szUri = aStream.ReadUint8L();
       
   285     	    
       
   286     	HBufC* uri =  HBufC::NewL(aStream, szUri);
       
   287 	    iSearchUri.InsertL(uri, index);   
       
   288     }
       
   289     
       
   290     // Read number of display fields
       
   291     TInt szNumFields = aStream.ReadUint8L();
       
   292     for ( int index = 0; index < szNumFields; index++ )
       
   293     {
       
   294     	iDisplayFields.InsertL(aStream.ReadInt16L(), index);
       
   295     }
       
   296     
       
   297     // Read Number of contacts that will be displayed to the client
       
   298     iMaxResults = aStream.ReadInt16L();
       
   299     iSortType = (TSortType)aStream.ReadInt8L();
       
   300 }