PECengine/PresenceManager2/SrcSearch/CPEngSearchControl.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2004 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 handls search operation
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "CPEngSearchControl.h"
       
    20 #include "CPEngSearchResultEntry.h"
       
    21 #include "CPEngNWSessionSlotStorageProxy.h"
       
    22 #include "MPEngAdvTransactionStatus2.h"
       
    23 #include "CPEngTransactionStatus.h"
       
    24 
       
    25 #include "CPEngSearchSessionHandler.h"
       
    26 #include "CPEngSearchHandler.h"
       
    27 #include "CPEngSearchTransaction2Imp.h"
       
    28 #include "PEngWVSearchPanics.h"
       
    29 
       
    30 #include <CPEngSearchTransaction2.h>
       
    31 #include <MPEngSearchTransactionObserver2.h>
       
    32 #include <CPEngSearchCriteria2.h>
       
    33 #include <PEngPresenceEngineConsts2.h>
       
    34 #include <CPEngNWSessionSlotNotifier2.h>
       
    35 #include <CPEngNWSessionSlotEvent2.h>
       
    36 
       
    37 
       
    38 
       
    39 //MACROS
       
    40 #define RETURN_ERR_IF_NOT_READY()               \
       
    41         {                                           \
       
    42         if( IsActive() )                            \
       
    43             {                                       \
       
    44             return KErrInUse;                       \
       
    45             }                                       \
       
    46         \
       
    47         if( iSearchState != EPEngSrch_Searching )   \
       
    48             {                                       \
       
    49             return KErrNotReady;                    \
       
    50             }                                       \
       
    51         }                                           \
       
    52      
       
    53 
       
    54 
       
    55 // ============================ MEMBER FUNCTIONS ===============================
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CPEngSearchControl::NewL()
       
    59 // -----------------------------------------------------------------------------
       
    60 CPEngSearchControl* CPEngSearchControl::NewL(
       
    61     TInt aPriority,
       
    62     const CPEngNWSessionSlotID2& aNWSessionSlotID,
       
    63     const RPointerArray< CPEngSearchCriteria2 >& aCriterias,
       
    64     TInt aSearchLimit,
       
    65     MPEngSearchTransactionObserver2& aObserver )
       
    66     {
       
    67     CPEngSearchControl* self = new ( ELeave ) CPEngSearchControl( aPriority,
       
    68                                                                   aObserver );
       
    69     CleanupStack::PushL( self );
       
    70     self->ConstructL( aPriority,
       
    71                       aNWSessionSlotID,
       
    72                       aCriterias,
       
    73                       aSearchLimit );
       
    74     CleanupStack::Pop( self );
       
    75 
       
    76     return self;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CPEngSearchControl::~CPEngSearchControl()
       
    81 // -----------------------------------------------------------------------------
       
    82 CPEngSearchControl::~CPEngSearchControl()
       
    83     {
       
    84     Cancel();
       
    85 
       
    86     if ( iSlotNotifier )
       
    87         {
       
    88         iSlotNotifier->RemoveObserver( *this );
       
    89         }
       
    90     delete iSlotNotifier;
       
    91 
       
    92     delete iSearchHandler;
       
    93     delete iSearchSessionHandler;
       
    94 
       
    95     delete iTransactionStatus;
       
    96     delete iSearchResult;
       
    97 
       
    98     if ( iOwner )
       
    99         {
       
   100         iOwner->HandleSearchDestruction( this );
       
   101         }
       
   102     }
       
   103 
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CPEngSearchControl::CPEngSearchControl()
       
   107 // -----------------------------------------------------------------------------
       
   108 CPEngSearchControl::CPEngSearchControl( TInt aPriority,
       
   109                                         MPEngSearchTransactionObserver2& aObserver )
       
   110         : CActive( aPriority ),
       
   111         iObserver( aObserver ),
       
   112         iTransactionOp( KErrNotFound ),
       
   113         iCancelling( EFalse ),
       
   114         iSearchState( EPEngSrch_Idle ),
       
   115         iDelayedErr( KErrNone )
       
   116     {
       
   117     CActiveScheduler::Add( this );
       
   118     }
       
   119 
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // CPEngSearchControl::ConstructL()
       
   123 // -----------------------------------------------------------------------------
       
   124 void CPEngSearchControl::ConstructL( TInt aPriority,
       
   125                                      const CPEngNWSessionSlotID2& aNWSessionSlotID,
       
   126                                      const RPointerArray< CPEngSearchCriteria2 >& aCriterias,
       
   127                                      TInt aSearchLimit )
       
   128     {
       
   129     ValidateSearchCriteriasL( aCriterias );
       
   130     iSearchResult = CPEngSearchResultEntry::NewL();
       
   131 
       
   132     iSearchSessionHandler = CPEngSearchSessionHandler::NewL( aPriority,
       
   133                                                              aNWSessionSlotID );
       
   134     iSearchHandler = CPEngSearchHandler::NewL( aPriority,
       
   135                                                iSearchSessionHandler->ImpsEngine(),
       
   136                                                *iSearchResult,
       
   137                                                aCriterias,
       
   138                                                aSearchLimit );
       
   139 
       
   140     iSlotNotifier = CPEngNWSessionSlotNotifier2::NewL( aPriority );
       
   141     User::LeaveIfError( iSlotNotifier->AddObserver( *this ) );
       
   142     User::LeaveIfError( iSlotNotifier->Start( aNWSessionSlotID ) );
       
   143 
       
   144     iTransactionStatus = CPEngTransactionStatus::NewL();
       
   145     }
       
   146 
       
   147 
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CPEngSearchControl::SetOwner()
       
   151 // -----------------------------------------------------------------------------
       
   152 void CPEngSearchControl::SetOwner( CPEngSearchTransaction2Imp& aOwner )
       
   153     {
       
   154     iOwner = &aOwner;
       
   155     }
       
   156 
       
   157 
       
   158 // -----------------------------------------------------------------------------
       
   159 // CPEngSearchControl::IssueSearch()
       
   160 // -----------------------------------------------------------------------------
       
   161 void CPEngSearchControl::IssueSearch()
       
   162     {
       
   163     iDelayedErr = KErrNone;
       
   164     iTransactionOp = EPEngTransOpNetworkSearchStart;
       
   165 
       
   166     TRAPD( err, iSearchSessionHandler->OpenCspSessionL( iStatus ) );
       
   167     HandleStepIssued( EPEngSrch_OpeningCspSession, err );
       
   168     }
       
   169 
       
   170 
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CPEngSearchControl::IssueContinueSearchFromNetwork()
       
   174 // -----------------------------------------------------------------------------
       
   175 TInt CPEngSearchControl::IssueContinueSearchFromNetwork( TInt aContinueIndex )
       
   176     {
       
   177     RETURN_ERR_IF_NOT_READY();
       
   178 
       
   179     iDelayedErr = KErrNone;
       
   180     iTransactionOp = EPEngTransOpNetworkSearchContinue;
       
   181 
       
   182     TRAPD( err, iSearchHandler->ContinueSearchL( aContinueIndex, iStatus ) );
       
   183     HandleStepIssued( EPEngSrch_Searching,
       
   184                       err );
       
   185 
       
   186     return KErrNone;
       
   187     }
       
   188 
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CPEngSearchControl::IssueStopSearchFromNetwork()
       
   192 // -----------------------------------------------------------------------------
       
   193 TInt CPEngSearchControl::IssueStopSearchFromNetwork()
       
   194     {
       
   195     RETURN_ERR_IF_NOT_READY();
       
   196 
       
   197     iDelayedErr = KErrNone;
       
   198     iTransactionOp = EPEngTransOpNetworkSearchStop;
       
   199 
       
   200     TRAPD( err, iSearchHandler->StopSearchL( iStatus ) );
       
   201     HandleStepIssued( EPEngSrch_StoppingSearch, err );
       
   202 
       
   203     return KErrNone;
       
   204     }
       
   205 
       
   206 
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CPEngSearchControl::RunL()
       
   210 // -----------------------------------------------------------------------------
       
   211 void CPEngSearchControl::RunL()
       
   212     {
       
   213     TInt err = iStatus.Int();
       
   214 
       
   215     switch ( iSearchState )
       
   216         {
       
   217         case EPEngSrch_OpeningCspSession:
       
   218             {
       
   219             //Session open failed - close session
       
   220             if ( err != KErrNone )
       
   221                 {
       
   222                 iDelayedErr = err;
       
   223                 TRAPD( err, iSearchSessionHandler->CloseCspSessionL( iStatus ) );
       
   224                 HandleStepIssued( EPEngSrch_ClosingCspSession, err );
       
   225                 }
       
   226 
       
   227             else
       
   228                 {
       
   229                 TRAPD( err, iSearchHandler->SearchL( iStatus ) );
       
   230                 HandleStepIssued( EPEngSrch_Searching, err );
       
   231                 }
       
   232             break;
       
   233             }
       
   234 
       
   235         case EPEngSrch_Searching:
       
   236             {
       
   237             DoNotifyObserver( err );
       
   238             break;
       
   239             }
       
   240 
       
   241         case EPEngSrch_StoppingSearch:
       
   242             {
       
   243             iDelayedErr = err;
       
   244             TRAPD( err, iSearchSessionHandler->CloseCspSessionL( iStatus ) );
       
   245             HandleStepIssued( EPEngSrch_ClosingCspSession, err );
       
   246             break;
       
   247             }
       
   248 
       
   249         case EPEngSrch_ClosingCspSession:
       
   250             {
       
   251             DoNotifyObserver( err );
       
   252             delete this;
       
   253             break;
       
   254             }
       
   255 
       
   256         default:
       
   257             {
       
   258             SearchPanicOrLeaveL( EPEngSearchControlRunStateMismatch, KErrGeneral );
       
   259             break;
       
   260             }
       
   261         }
       
   262     }
       
   263 
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CPEngSearchControl::RunError()
       
   267 // -----------------------------------------------------------------------------
       
   268 TInt CPEngSearchControl::RunError( TInt /*aError*/ )
       
   269     {
       
   270     delete this;
       
   271     return KErrNone;
       
   272     }
       
   273 
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // CPEngSearchControl::DoCancel()
       
   277 // -----------------------------------------------------------------------------
       
   278 //
       
   279 void CPEngSearchControl::DoCancel()
       
   280     {
       
   281     delete iSearchHandler;
       
   282     iSearchHandler = NULL;
       
   283 
       
   284     delete iSearchSessionHandler;
       
   285     iSearchSessionHandler = NULL;
       
   286 
       
   287     DoNotifyObserver( KErrCancel );
       
   288     }
       
   289 
       
   290 
       
   291 // -----------------------------------------------------------------------------
       
   292 // CPEngSearchControl::Cancel()
       
   293 // -----------------------------------------------------------------------------
       
   294 void CPEngSearchControl::Cancel()
       
   295     {
       
   296     if ( iCancelling )
       
   297         {
       
   298         return;
       
   299         }
       
   300 
       
   301     iCancelling = ETrue;
       
   302     CActive::Cancel();
       
   303     iCancelling = EFalse;
       
   304     }
       
   305 
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CPEngSearchControl::HandleNWSessionSlotChangeL()
       
   309 // From MPEngNWSessionSlotObserver2
       
   310 // -----------------------------------------------------------------------------
       
   311 void CPEngSearchControl::HandleNWSessionSlotChangeL(
       
   312     CPEngNWSessionSlotNotifier2& /*aNotifier*/,
       
   313     CPEngNWSessionSlotEvent2& aEvent )
       
   314     {
       
   315     if ( ( aEvent.Event() == EPEngEventAppNWPresenceSessionClosed ) ||
       
   316          ( aEvent.Event() == EPEngEventNWSessionClosedByServer ) )
       
   317         {
       
   318         iSlotNotifier->RemoveObserver( *this );
       
   319         AsyncTerminateSearch();
       
   320         }
       
   321     }
       
   322 
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CPEngSearchControl::HandleNWSessionSlotError()
       
   326 // From MPEngNWSessionSlotObserver2
       
   327 // -----------------------------------------------------------------------------
       
   328 void CPEngSearchControl::HandleNWSessionSlotError(
       
   329     TInt /*aError*/,
       
   330     CPEngNWSessionSlotNotifier2& /*aNotifier*/ )
       
   331     {
       
   332     iSlotNotifier->RemoveObserver( *this );
       
   333     AsyncTerminateSearch();
       
   334     }
       
   335 
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CPEngSearchControl::AsyncTerminateSearch()
       
   339 // -----------------------------------------------------------------------------
       
   340 void CPEngSearchControl::AsyncTerminateSearch()
       
   341     {
       
   342     //Called when slots network session is closed
       
   343     //==> Search operation must release owned WV Engine sessions
       
   344 
       
   345     //This might be called when:
       
   346     // 1. WV Engine session open is pending -> cancel open
       
   347     // 2. Session open, search request pending on WV Engine side
       
   348     // 3. Session open, no pending search request
       
   349     // 4. Session open, search stop request pending on WV Engine side
       
   350     // 5. Session open, session close pending on WV Engine side
       
   351 
       
   352     //Cancel any pending operation
       
   353     if ( IsActive() )
       
   354         {
       
   355         Cancel();
       
   356         }
       
   357 
       
   358     //And terminate the search
       
   359     HandleStepIssued( EPEngSrch_ClosingCspSession, KErrCancel );
       
   360     }
       
   361 
       
   362 
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CPEngSearchControl::ValidateSearchCriteriasL()
       
   366 // -----------------------------------------------------------------------------
       
   367 void CPEngSearchControl::ValidateSearchCriteriasL(
       
   368     const RPointerArray< CPEngSearchCriteria2 >& aCriterias )
       
   369     {
       
   370     //Validate search elements & search values
       
   371     const TInt criteriaCount( aCriterias.Count() );
       
   372     if ( criteriaCount == 0 )
       
   373         {
       
   374         User::Leave( KErrArgument );
       
   375         }
       
   376 
       
   377     TInt ii;
       
   378     TInt jj;
       
   379 
       
   380     //Verify all values
       
   381     for ( ii = 0; ii < criteriaCount; ii++ )
       
   382         {
       
   383 
       
   384         if ( aCriterias[ii]->SearchValue().Length() == 0 )
       
   385             {
       
   386             User::Leave( KErrArgument );
       
   387             }
       
   388 
       
   389         if ( aCriterias[ii]->Element() == KPEngNullSearchElement )
       
   390             {
       
   391             User::Leave( KErrArgument );
       
   392             }
       
   393         }
       
   394 
       
   395 
       
   396     //Chechk  duplicates
       
   397     for ( ii = 0; ii < criteriaCount - 1; ii++ )
       
   398         {
       
   399         for ( jj = ii + 1 ; jj < criteriaCount; jj++ )
       
   400             {
       
   401             if ( aCriterias[ii]->Element() == aCriterias[jj]->Element() )
       
   402                 {
       
   403                 User::Leave( KErrAlreadyExists );
       
   404                 }
       
   405             }
       
   406         }
       
   407     }
       
   408 
       
   409 
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CPEngSearchControl::DoNotifyObserver()
       
   413 // -----------------------------------------------------------------------------
       
   414 void CPEngSearchControl::DoNotifyObserver( TInt aErr )
       
   415     {
       
   416     //Don't notify client if no client issued transaction pending
       
   417     if ( iTransactionOp == KErrNotFound )
       
   418         {
       
   419         return;
       
   420         }
       
   421 
       
   422     //Determine which error to report client
       
   423     if ( iDelayedErr != KErrNone )
       
   424         {
       
   425         iTransactionStatus->SetStatus( iDelayedErr );
       
   426         }
       
   427     else
       
   428         {
       
   429         iTransactionStatus->SetStatus( aErr );
       
   430         }
       
   431 
       
   432     CPEngSearchTransaction2& transInterface = iOwner->Interface();
       
   433 
       
   434 
       
   435     //Notify client
       
   436     TRAPD( err, iObserver.HandleSearchTransactionCompleteL( *iTransactionStatus,
       
   437                                                             transInterface,
       
   438                                                             iSearchResult->Interface(),
       
   439                                                             iTransactionOp ) );
       
   440 
       
   441     if ( err != KErrNone )
       
   442         {
       
   443         iObserver.HandleSearchTransactionError( err,
       
   444                                                 transInterface,
       
   445                                                 iTransactionOp );
       
   446         }
       
   447 
       
   448     iTransactionOp = KErrNotFound;
       
   449     }
       
   450 
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // CPEngSearchControl::HandleStepIssued()
       
   454 // -----------------------------------------------------------------------------
       
   455 void CPEngSearchControl::HandleStepIssued( TPEngSearchState aState,
       
   456                                            TInt aErr )
       
   457     {
       
   458     iSearchState = aState;
       
   459     if ( aErr != KErrNone )
       
   460         {
       
   461         TRequestStatus* tmpStatus = &iStatus;
       
   462         User::RequestComplete( tmpStatus, aErr );
       
   463         }
       
   464 
       
   465     SetActive();
       
   466     }
       
   467 
       
   468 
       
   469 
       
   470 
       
   471 // End of file