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