predictivesearch/PcsUtils/src/CPsQuery.cpp
changeset 0 e686773b3f54
child 6 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 the search sequence for predictive search.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <s32mem.h>
       
    21 #include "CPsQuery.h"
       
    22 #include "CPsQueryItem.h"
       
    23 #include "CPcsDebug.h"
       
    24 
       
    25 // ============================== MEMBER FUNCTIONS ============================
       
    26 
       
    27 
       
    28 // ----------------------------------------------------------------------------
       
    29 // CPsQuery::NewL
       
    30 // Two Phase constructor
       
    31 // ----------------------------------------------------------------------------
       
    32 EXPORT_C CPsQuery* CPsQuery::NewL()
       
    33 {
       
    34 	CPsQuery* self = new (ELeave) CPsQuery();	
       
    35     CleanupStack::PushL(self);
       
    36     self->ConstructL();
       
    37     CleanupStack::Pop();
       
    38     return self;
       
    39 	
       
    40 }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // CPsQuery::CPsQuery
       
    44 // Default constructor
       
    45 // ----------------------------------------------------------------------------
       
    46 CPsQuery::CPsQuery()
       
    47 {
       
    48 }
       
    49 
       
    50 // ----------------------------------------------------------------------------
       
    51 // CPsQuery::ConstructL
       
    52 // Two phase construction
       
    53 // ----------------------------------------------------------------------------
       
    54 void CPsQuery::ConstructL()
       
    55 { 
       
    56 }
       
    57 
       
    58 // ----------------------------------------------------------------------------
       
    59 // CPsQuery::~CPsQuery
       
    60 // Destructor
       
    61 // ----------------------------------------------------------------------------
       
    62 EXPORT_C CPsQuery::~CPsQuery()
       
    63 {
       
    64 	Reset();
       
    65 }
       
    66 
       
    67 // ----------------------------------------------------------------------------
       
    68 // CPsQuery::InsertL
       
    69 // Add a query item to the search sequence
       
    70 // ----------------------------------------------------------------------------
       
    71 EXPORT_C void CPsQuery::InsertL(const CPsQueryItem& aQueryItem, TInt aIndex)
       
    72 {   
       
    73 	if( (aIndex >= 0) 
       
    74 	   &&  (aIndex <= (iSearchQuery.Count() + 1)) 
       
    75 	   && (this->Count() < KPsQueryMaxLen) )
       
    76 	{
       
    77 		iSearchQuery.Insert(&aQueryItem, aIndex);		
       
    78 	}    
       
    79 }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // CPsQuery::AppendL
       
    83 // Appends a query item to the search sequence
       
    84 // ----------------------------------------------------------------------------
       
    85 EXPORT_C void CPsQuery::AppendL(const CPsQueryItem& aQueryItem)
       
    86 {
       
    87 	if(this->Count() < KPsQueryMaxLen )
       
    88 		iSearchQuery.AppendL(&aQueryItem);
       
    89 }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CPsQuery::GetItemAtL
       
    93 // Returns the query item at the specified index
       
    94 // ----------------------------------------------------------------------------
       
    95 EXPORT_C CPsQueryItem& CPsQuery:: GetItemAtL(TInt aIndex)
       
    96 {    
       
    97 	if ( aIndex < 0 && aIndex >= iSearchQuery.Count()) 
       
    98 	{
       
    99 	    User::Leave(KErrArgument);		
       
   100 	}
       
   101 	
       
   102 	return (*(iSearchQuery[aIndex]));
       
   103 }
       
   104 
       
   105 // ----------------------------------------------------------------------------
       
   106 // CPsQuery::QueryAsStringL
       
   107 // Returns the search sequence
       
   108 // ----------------------------------------------------------------------------
       
   109 EXPORT_C TDesC& CPsQuery:: QueryAsStringLC()
       
   110 {
       
   111 	HBufC* query = HBufC::NewLC(KPsQueryMaxLen);	
       
   112 
       
   113     for(TInt arrayIndex =0 ; arrayIndex < iSearchQuery.Count();arrayIndex++)
       
   114     {
       
   115     	query->Des().Append((iSearchQuery[arrayIndex])->Character());
       
   116     }
       
   117     
       
   118     return *query;
       
   119 }
       
   120 
       
   121 // ----------------------------------------------------------------------------
       
   122 // CPsQuery::Remove
       
   123 // Deletes the query item at the specified index
       
   124 // ----------------------------------------------------------------------------
       
   125 EXPORT_C void CPsQuery:: Remove(TInt aIndex)
       
   126 {
       
   127 	if (aIndex >= 0 && aIndex <= (iSearchQuery.Count() - 1))
       
   128 	{
       
   129 	    delete iSearchQuery[aIndex];
       
   130 		iSearchQuery.Remove(aIndex);		
       
   131 	}   
       
   132 }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // CPsQuery::Reset
       
   136 // Deletes the entire search query
       
   137 // ----------------------------------------------------------------------------
       
   138 EXPORT_C void CPsQuery:: Reset()
       
   139 {
       
   140 	for ( TInt arrayIndex = 0; arrayIndex < iSearchQuery.Count(); arrayIndex++ )
       
   141 	{
       
   142 		delete iSearchQuery[arrayIndex];
       
   143 	}   
       
   144     iSearchQuery.Reset();  
       
   145 }
       
   146 
       
   147 // ----------------------------------------------------------------------------
       
   148 // CPsQuery::KeyboardModeL
       
   149 // Returns the keyboard input mode for the search query
       
   150 // ----------------------------------------------------------------------------
       
   151 EXPORT_C TKeyboardModes CPsQuery:: KeyboardModeL()
       
   152 {
       
   153 	TKeyboardModes keyboardMode = EModeUndefined;
       
   154     
       
   155     if(iSearchQuery.Count() > 0)
       
   156     {
       
   157     	keyboardMode = iSearchQuery[0]->Mode();
       
   158 	    for(TInt arrayIndex =1 ; arrayIndex < iSearchQuery.Count() ;arrayIndex++)
       
   159 	    {
       
   160 	        if(keyboardMode != iSearchQuery[arrayIndex]->Mode())
       
   161 	        {
       
   162 	        	keyboardMode = EModeUndefined;
       
   163 	        	break;	
       
   164 	        }
       
   165 	    }
       
   166     }
       
   167 	
       
   168     return keyboardMode;
       
   169 }
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // CPsQuery::Count
       
   173 // Returns the length of the query
       
   174 // ----------------------------------------------------------------------------
       
   175 EXPORT_C TInt CPsQuery:: Count()
       
   176 {
       
   177 	return (iSearchQuery.Count());
       
   178 }
       
   179 
       
   180 // ----------------------------------------------------------------------------
       
   181 // Deprecated: Use CPcsDebug::PrintQuery 
       
   182 // CPsQuery::PrintQuery
       
   183 // Prints the query as array of query items (query items cannot be spaces)
       
   184 // Used only for debugging
       
   185 // ----------------------------------------------------------------------------
       
   186 EXPORT_C void CPsQuery:: PrintQuery()
       
   187 {
       
   188     PRINT1 ( _L("CPsQuery::PrintQuery: Number of items : %d"), iSearchQuery.Count()); 
       
   189 	for(TInt i = 0; i < iSearchQuery.Count(); i++)
       
   190 	{
       
   191 		TUint inputKey = iSearchQuery[i]->Character().GetUpperCase();
       
   192 		TBuf<2> buffer;
       
   193 		buffer.Format(_L("%c"), inputKey);
       
   194 	    switch(iSearchQuery[i]->Mode())
       
   195 	    {
       
   196 	    	case EItut:
       
   197 	    	    PRINT2 ( _L("Character at index %d: '%S' (ITU-T)"), i, &buffer);
       
   198 	    	    break;
       
   199 	    	case EQwerty:
       
   200                 PRINT2 ( _L("Character at index %d: '%S' (QWERTY)"), i, &buffer);
       
   201 	    	    break;	    	
       
   202 	    	default:
       
   203                 PRINT2 ( _L("Character at index %d: '%S' (mode=?)"), i, &buffer);
       
   204 	    	    break;
       
   205 	    }
       
   206 	}
       
   207 }
       
   208 
       
   209 // ----------------------------------------------------------------------------
       
   210 // CPsQuery::ExternalizeL
       
   211 // Writes 'this' to aStream
       
   212 // ----------------------------------------------------------------------------
       
   213 EXPORT_C void CPsQuery::ExternalizeL(RWriteStream& aStream) const
       
   214 {	
       
   215     aStream.WriteUint8L(iSearchQuery.Count()); // Number of query items
       
   216 	for ( int index = 0; index < iSearchQuery.Count(); index++ )
       
   217 	{
       
   218 		(iSearchQuery[index])->ExternalizeL(aStream);
       
   219 	}
       
   220 }
       
   221 
       
   222 // ----------------------------------------------------------------------------
       
   223 // CPsQuery::InternalizeL
       
   224 // Initializes 'this' with the contents of aStream
       
   225 // ----------------------------------------------------------------------------
       
   226 EXPORT_C void CPsQuery::InternalizeL(RReadStream& aStream)
       
   227 {
       
   228     // Read number of query items
       
   229     TInt numQueryItems = aStream.ReadUint8L();
       
   230     
       
   231     // Internalize each item
       
   232     for ( int index = 0; index < numQueryItems; index++ )
       
   233     {
       
   234         CPsQueryItem *item = CPsQueryItem::NewL();
       
   235         item->InternalizeL(aStream);
       
   236         this->AppendL(*item);
       
   237     }
       
   238 }
       
   239