wvuing/wvuieng/EngSrc/CCASearchManager.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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:  This class offers the search services
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "CCARequest.h"
       
    21 #include "CCARequestMapper.h"
       
    22 #include "CCASearchData.h"
       
    23 #include "CCASearchManager.h"
       
    24 #include "MCASearchObserverInterface.h"
       
    25 #include "PublicEngineDefinitions.h"
       
    26 #include "PrivateEngineDefinitions.h"
       
    27 #include "ImpsCSPAllErrors.h"
       
    28 #include "MCAImpsFactory.h"
       
    29 
       
    30 #include "ChatDebugPrint.h"
       
    31 
       
    32 // ================= MEMBER FUNCTIONS =======================
       
    33 
       
    34 // Two-phased constructor.
       
    35 CCASearchManager* CCASearchManager::NewL(
       
    36     MCAImpsFactory* aIMPSFactory,
       
    37     CCARequestMapper*   aRequestMapper )
       
    38     {
       
    39     CCASearchManager* self = new ( ELeave ) CCASearchManager(
       
    40         aIMPSFactory,
       
    41         aRequestMapper );
       
    42 
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop( self );
       
    46 
       
    47     return self;
       
    48     }
       
    49 
       
    50 // Destructor
       
    51 CCASearchManager::~CCASearchManager()
       
    52     {
       
    53     delete iOwnGrouplistCreatorId;
       
    54     delete iResults;
       
    55     delete iCreatedGroups;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // CCASearchManager::SetSearchSupported
       
    60 // ---------------------------------------------------------
       
    61 //
       
    62 void CCASearchManager::SetSearchSupported( TBool aSearchSupported )
       
    63     {
       
    64     iSearchSupported = aSearchSupported;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // CCASearchManager::StartSearchL
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 EXPORT_C TInt CCASearchManager::StartSearchL(
       
    72     const CSearchPairs& aPairs,
       
    73     TInt          aSearchLimit,
       
    74     MCASearchObserverInterface* aObserver )
       
    75     {
       
    76     CHAT_DP_TXT( "CCASearchManager::StartSearchL()...starts" );
       
    77 
       
    78     if ( !iSearchSupported )
       
    79         {
       
    80         User::Leave( ESearchNotSupported );
       
    81         }
       
    82 
       
    83     // Set data
       
    84     iSearchLimit = aSearchLimit;
       
    85     iState = ESearchStartRequested;
       
    86 
       
    87     // Create the request
       
    88     CHAT_DP_FUNC_DP( "StartSearchL", "Creating request" );
       
    89     TInt opId = iImpsFundClient->SearchFirstL( aPairs,
       
    90                                                aSearchLimit );
       
    91     CCARequest* request = iRequestMapper->CreateRequestL( opId, EFalse );
       
    92 
       
    93     // remember the observer (synchronous, so only one observer used)
       
    94     iObserver = aObserver;
       
    95 
       
    96     CHAT_DP_FUNC_DP( "StartSearchL", "Waiting for completion" );
       
    97 
       
    98     request->StartWait();
       
    99 
       
   100     // execution continues from here
       
   101     TInt errorCode( KErrNone );
       
   102     errorCode = request->ErrorCode();
       
   103 
       
   104     iRequestMapper->RemoveRequest( request );
       
   105     iObserver = NULL;
       
   106 
       
   107     CHAT_DP_TXT( "CCASearchManager::StartSearchL()...over" );
       
   108 
       
   109     return errorCode;
       
   110     }
       
   111 
       
   112 
       
   113 // ---------------------------------------------------------
       
   114 // CCASearchManager::EndSearchL
       
   115 // ---------------------------------------------------------
       
   116 //
       
   117 EXPORT_C TInt CCASearchManager::EndSearchL()
       
   118     {
       
   119     CHAT_DP_TXT( "CCASearchManager::EndSearchL()...starts" );
       
   120 
       
   121     if ( !iSearchSupported )
       
   122         {
       
   123         User::Leave( ESearchNotSupported );
       
   124         }
       
   125 
       
   126     // Create the request and start waiting
       
   127     CCARequest* request = iRequestMapper->CreateRequestL(
       
   128                               iImpsFundClient->StopSearchL( iSearchID ),
       
   129                               EFalse );
       
   130 
       
   131     request->StartWait();
       
   132 
       
   133     TInt errorCode( KErrNone );
       
   134 
       
   135     // Execution continues from here
       
   136     errorCode = request->ErrorCode();
       
   137     CHAT_DP( D_CHAT_LIT( "EndSearchL() error code %d" ), errorCode );
       
   138 
       
   139     iRequestMapper->RemoveRequest( request );
       
   140     iObserver = NULL;
       
   141 
       
   142     CHAT_DP_TXT( "CCASearchManager::EndSearchL()...over" );
       
   143 
       
   144     return errorCode;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------
       
   148 // CCASearchManager::SearchNextL
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 EXPORT_C TInt CCASearchManager::SearchNextL( TInt aIndex,
       
   152                                              MCASearchObserverInterface* aObserver )
       
   153     {
       
   154     CHAT_DP_FUNC_ENTER( "SearchNextL" );
       
   155     CHAT_DP( D_CHAT_LIT( "SearchNextL index is %d" ), aIndex );
       
   156 
       
   157     if ( !iSearchSupported )
       
   158         {
       
   159         User::Leave( ESearchNotSupported );
       
   160         }
       
   161 
       
   162     if ( aIndex >= SearchDataInterface()->HowManyResultsAvailable() &&
       
   163          iResults->NoMoreResultsAvailable() )
       
   164         {
       
   165         CHAT_DP_TXT( "!!!NO MORE RESULTS!!!" );
       
   166         iObserver = NULL;
       
   167         User::Leave( ERequestedResultsButNotAvailable );
       
   168         }
       
   169 
       
   170     if ( iResults->WeHaveData( aIndex ) )
       
   171         {
       
   172         CHAT_DP_TXT( "   We have results" );
       
   173         CHAT_DP_FUNC_DONE( "SearchNextL" );
       
   174         iObserver = NULL;
       
   175         return KErrNone;
       
   176         }
       
   177     else
       
   178         {
       
   179         CHAT_DP_TXT( "   We do not have results" );
       
   180         CHAT_DP( D_CHAT_LIT( "Next index %d" ), iResults->NextSearchIndex() );
       
   181 
       
   182         // Start waiting
       
   183         TInt opId = iImpsFundClient->SearchNextL( iSearchID,
       
   184                                                   iResults->NextSearchIndex() );
       
   185         CCARequest* request = iRequestMapper->CreateRequestL( opId, EFalse );
       
   186 
       
   187         // remember the observer for this operation
       
   188         iObserver = aObserver;
       
   189 
       
   190         request->StartWait();
       
   191 
       
   192         // execution continues from here
       
   193         TInt errorCode( KErrNone );
       
   194         errorCode = request->ErrorCode();
       
   195 
       
   196         iRequestMapper->RemoveRequest( request );
       
   197 
       
   198         CHAT_DP_FUNC_DONE( "SearchNextL" );
       
   199         iObserver = NULL;
       
   200 
       
   201         return errorCode;
       
   202         }
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------
       
   206 // CCASearchManager::SearchDataInterface
       
   207 // ---------------------------------------------------------
       
   208 //
       
   209 EXPORT_C MCASearchData* CCASearchManager::SearchDataInterface() const
       
   210     {
       
   211     return iResults;
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------
       
   215 // CCASearchManager::HandleSearchL
       
   216 // ---------------------------------------------------------
       
   217 //
       
   218 void CCASearchManager::HandleSearchL( TInt aId,
       
   219                                       TInt aSearchId,
       
   220                                       TInt aIndex,
       
   221                                       TInt aResultsFound,
       
   222                                       TBool aCompleted,
       
   223                                       TImpsSearchResultType aType,
       
   224                                       MDesCArray* aResults ,
       
   225                                       TImpsCspIdentifier& /* aCspId */ )
       
   226     {
       
   227     // NOTES about response API
       
   228     // 1. After the search has been started 2nd search and from that point on
       
   229     //    the aSearchId is not available (see SCR)
       
   230     CHAT_DP_FUNC_ENTER( "HandleSearchL" );
       
   231     CHAT_DP( D_CHAT_LIT( "   aIndex = %d" ), aIndex );
       
   232     CHAT_DP( D_CHAT_LIT( "   aSearchId = %d" ), aSearchId );
       
   233     CHAT_DP( D_CHAT_LIT( "   iSearchID = %d" ), iSearchID );
       
   234 
       
   235     TInt leave( KErrNone );
       
   236 
       
   237     // Find the request
       
   238     CCARequest* request =
       
   239         const_cast<CCARequest*>( iRequestMapper->FindRequest( aId ) );
       
   240 
       
   241     if ( request )
       
   242         {
       
   243         if ( iState == ESearchStartRequested )
       
   244             {
       
   245             iResults->ClearResults();
       
   246             }
       
   247 
       
   248         iResults->UpdateMaximumPossibleResults( aResultsFound );
       
   249 
       
   250         TRAP( leave, SaveResultsL( aIndex, aCompleted, aType, aResults ) );
       
   251         if ( leave != KErrNone )
       
   252             {
       
   253             CHAT_DP_FUNC_DP( "HandleSearchL", "Got an error" );
       
   254 
       
   255             if ( iObserver )
       
   256                 {
       
   257                 iObserver->HandleSearchError( EECouldntSaveSearchResults );
       
   258                 }
       
   259             }
       
   260         else
       
   261             {
       
   262             if ( iState == ESearchStartRequested )
       
   263                 {
       
   264                 // Update search ID for the first and only time
       
   265                 iSearchID = aSearchId;
       
   266 
       
   267                 // Update state
       
   268                 iState = ESearchGoingOn;
       
   269                 }
       
   270             }
       
   271         }
       
   272 
       
   273     // If code added/changed before HandleRequest, then make sure that
       
   274     // code does not Leave before HandleRequest, because request
       
   275     // response waiting does not stop (and waitnote) before calling
       
   276     // HandleRequest
       
   277     CHAT_DP_FUNC_DP( "HandleSearchL", "Handling request" );
       
   278     TInt status( KErrNone );
       
   279     if ( leave )
       
   280         {
       
   281         status = leave;
       
   282         }
       
   283     iRequestMapper->HandleRequest( aId, status );
       
   284 
       
   285     if ( iObserver )
       
   286         {
       
   287         CHAT_DP_FUNC_DP( "HandleSearchL", "Invoking observer" );
       
   288         iObserver->HandleSearchFinished();
       
   289         }
       
   290 
       
   291     // leave on system errors
       
   292     if ( leave > Imps_ERROR_BASE )
       
   293         {
       
   294         User::LeaveIfError( leave );
       
   295         }
       
   296 
       
   297     CHAT_DP_FUNC_DONE( "HandleSearchL" );
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------
       
   301 // CCASearchManager::HandleSearchStoppedL
       
   302 // ---------------------------------------------------------
       
   303 //
       
   304 void CCASearchManager::HandleSearchStoppedL( TInt aId,
       
   305                                              TInt /* aSearchId */,
       
   306                                              TImpsCspIdentifier& /* aCspId */ )
       
   307     {
       
   308     CHAT_DP_FUNC_ENTER( "HandleSearchStoppedL" );
       
   309 
       
   310     iResults->ClearResults();
       
   311 
       
   312     iState = ENoActivity;
       
   313 
       
   314     // If code added/changed before HandleRequest, then make sure that
       
   315     // code does not Leave before HandleRequest, because request
       
   316     // response waiting does not stop (and waitnote) before calling
       
   317     // HandleRequest
       
   318 
       
   319     iRequestMapper->HandleRequest( aId, KErrNone );
       
   320 
       
   321     iObserver = NULL;
       
   322     CHAT_DP_FUNC_DONE( "HandleSearchStoppedL" );
       
   323     }
       
   324 
       
   325 // C++ default constructor can NOT contain any code, that
       
   326 // might leave.
       
   327 //
       
   328 CCASearchManager::CCASearchManager(
       
   329     MCAImpsFactory* aIMPSFactory,
       
   330     CCARequestMapper*   aRequestMapper )
       
   331         :   iImpsFactory( aIMPSFactory ),
       
   332         iState( ENoActivity ),
       
   333         iRequestMapper( aRequestMapper )
       
   334     {
       
   335     }
       
   336 
       
   337 // Symbian OS default constructor can leave.
       
   338 void CCASearchManager::ConstructL()
       
   339     {
       
   340     CHAT_DP_TXT( "CCASearchManager::ConstructL...starts" );
       
   341 
       
   342     iImpsFundClient = iImpsFactory->CreateFundClientL();
       
   343 
       
   344     iResults = CCASearchData::NewL();
       
   345     iCreatedGroups = new( ELeave ) CDesCArrayFlat( KArrayGranularity );
       
   346 
       
   347     CHAT_DP_TXT( "CCASearchManager::ConstructL...ends" );
       
   348     }
       
   349 
       
   350 // ---------------------------------------------------------
       
   351 // CCASearchManager::SaveResultsL
       
   352 // ---------------------------------------------------------
       
   353 //
       
   354 void CCASearchManager::SaveResultsL( TInt aIndex,
       
   355                                      TBool aCompleted,
       
   356                                      TImpsSearchResultType aType,
       
   357                                      MDesCArray* aResults )
       
   358     {
       
   359     CHAT_DP_TXT( "CCASearchManager::SaveResultsL...starts" );
       
   360 
       
   361     iResults->UpdateDataL(
       
   362         aIndex,
       
   363         aCompleted,
       
   364         aType,
       
   365         aResults,
       
   366         iSearchLimit );
       
   367 
       
   368     CHAT_DP_TXT( "CCASearchManager::SaveResultsL...over" );
       
   369     }
       
   370 
       
   371 //  End of File