phoneengine/PhoneCntFinder/ContactService/src/tphcntaiwserviceobserver.cpp
changeset 0 5f000ab63145
equal deleted inserted replaced
-1:000000000000 0:5f000ab63145
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Observer for Phonebook AIW services.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tphcntaiwserviceobserver.h"
       
    20 #include "mphcntservicerequestparam.h"
       
    21 #include "mphcntserviceresult.h"
       
    22 
       
    23 // Phonebook2 AIW service constants
       
    24 const TInt KServiceTermiationAllowed = 0; 
       
    25 const TInt KProcessTerminationAllowed = 1;
       
    26 
       
    27 // Phone Process Uid.
       
    28 const TInt KPhoneProcess = 0x100058B3;
       
    29 const TBool KDoNotAllowProcessTermination = EFalse;
       
    30 const TBool KAllowProcessTermination = ETrue;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Constructor
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 TPhCntAiwServiceObserver::TPhCntAiwServiceObserver(
       
    39     MPhCntAiwServiceCompleteObserver& aObserver) : 
       
    40     iResult( NULL ),
       
    41     iParams( NULL ),
       
    42     iObserver( aObserver ),
       
    43     iAiwServiceShuttingDown( EFalse )
       
    44     {
       
    45     }
       
    46    
       
    47 // ---------------------------------------------------------------------------
       
    48 // Sets params and result
       
    49 // ---------------------------------------------------------------------------
       
    50 // 
       
    51 void TPhCntAiwServiceObserver::SetRequestAndResult( 
       
    52     MPhCntServiceRequestParam* aParams,
       
    53     MPhCntServiceResult* aResult )
       
    54     {
       
    55     iParams = aParams;
       
    56     iResult = aResult;
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // From MAiwNotifyCallback
       
    61 // Handles the Phonebooks AIW callbacks.
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 TInt TPhCntAiwServiceObserver::HandleNotifyL(
       
    65     TInt aCmdId,
       
    66     TInt aEventId,
       
    67     CAiwGenericParamList& aEventParamList,
       
    68     const CAiwGenericParamList& /*aInParamList*/ )
       
    69 	{
       
    70 	TInt response = KErrNone;
       
    71 	if( iResult && iParams )
       
    72 	    {
       
    73 	    if( aCmdId == iParams->Command() ) 
       
    74 	        {
       
    75 	        switch( aEventId )
       
    76 	            {
       
    77 	            // User canceled the service.
       
    78 	            case KAiwEventCanceled:
       
    79 	                // if AIW service is shutting down (ie user has pressed end key), 
       
    80 	                // do not indicate that response has received yet, because
       
    81 	                // we still have to wait KAiwEventQueryExit.
       
    82 	                if( !iAiwServiceShuttingDown )
       
    83 	                    {
       
    84 	                    ResponseReceived( KErrCancel );
       
    85 	                    }
       
    86 	                break;
       
    87 	            
       
    88 	            // Service provider had an internal error.
       
    89 	            case KAiwEventError:
       
    90 	                ResponseReceived( ParseErrorCode( aEventParamList ) );
       
    91 	                break;
       
    92 	                
       
    93 	            // Service provider stopped from some reason.
       
    94 	            case KAiwEventStopped:
       
    95 	                ResponseReceived( KErrGeneral );
       
    96 	                break;
       
    97 	            
       
    98 	            case KAiwEventCompleted:
       
    99 	                iResult->ParseResult( aEventParamList );
       
   100 	                ResponseReceived( KErrNone );
       
   101 	                break;
       
   102 	                
       
   103 	            // We are not interested to check the client selection.
       
   104 	            case KAiwEventOutParamCheck: 
       
   105 	                response = ETrue;
       
   106 	                break;
       
   107 	                
       
   108 	                
       
   109 	            // QUERY EXIT is three phase operation.
       
   110 	            // First time when KAiwEventQueryExit comes, Phonebook AIW service
       
   111 	            // provider is asking if service is allowed to be terminated (KServiceTermiationAllowed).
       
   112 	            // After that service request is completed with event KAiwEventCanceled.
       
   113 	            // Last phase is KAiwEventQueryExit event with query to allow process termination 
       
   114 	            // (KProcessTerminationAllowed).
       
   115 	            case KAiwEventQueryExit:
       
   116 
       
   117 	                TInt queryType;
       
   118                     if( !FindFirstTInt32Value( 
       
   119 	                        aEventParamList, EGenericParamApplication, queryType ) )
       
   120                         {
       
   121                         if( queryType == KServiceTermiationAllowed )
       
   122                             { 
       
   123                             // AIW service is always allowed to exit. After this
       
   124                             // AIW service provider cancels the outstanding
       
   125                             // request with event KAiwEventCanceled.
       
   126                             response = iAiwServiceShuttingDown = ETrue;
       
   127                             }
       
   128                         else if( queryType == KProcessTerminationAllowed )
       
   129                             {
       
   130                             if ( RunningInPhoneAppProcess() )
       
   131                                 {
       
   132                                 // Phone app process is not allowed to be 
       
   133                                 // terminated.
       
   134                                 response = KDoNotAllowProcessTermination;    
       
   135                                 }
       
   136                             else
       
   137                                 {
       
   138                                 response = KAllowProcessTermination;
       
   139                                 }                          
       
   140                             ResponseReceived( KErrCancel );
       
   141                             }
       
   142                         }
       
   143                     break;
       
   144 	              
       
   145 	            // Other events are not handled and we wait for
       
   146 	            // next event.
       
   147                 default:
       
   148                     break;
       
   149 	            }           
       
   150 	        }
       
   151 	    }
       
   152 	    
       
   153 	return response;
       
   154 	}
       
   155 	
       
   156 // ---------------------------------------------------------------------------
       
   157 // Takes error code from generic param list.
       
   158 // ---------------------------------------------------------------------------
       
   159 //	
       
   160 TInt TPhCntAiwServiceObserver::ParseErrorCode( 
       
   161     const CAiwGenericParamList& aEventParamList )
       
   162     {
       
   163     TInt errorValue = KErrGeneral;
       
   164     FindFirstTInt32Value( aEventParamList, EGenericParamError, errorValue );
       
   165     return errorValue;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // Takes integer value from aEventParamList.
       
   170 // ---------------------------------------------------------------------------
       
   171 //	    
       
   172 TInt TPhCntAiwServiceObserver::FindFirstTInt32Value( 
       
   173     const CAiwGenericParamList& aEventParamList, 
       
   174     TGenericParamId aParameterId,
       
   175     TInt& aValue ) const
       
   176     {
       
   177     TInt err( KErrNotFound );
       
   178     TInt index( 0 );
       
   179     const TAiwGenericParam* param =
       
   180         aEventParamList.FindFirst( index, aParameterId );
       
   181     if ( index != KErrNotFound && param )
       
   182         {
       
   183         aValue = param->Value().AsTInt32();
       
   184         err = KErrNone;
       
   185         }
       
   186     return err;
       
   187     }
       
   188 
       
   189 // ---------------------------------------------------------------------------
       
   190 // Notifies observer.
       
   191 // ---------------------------------------------------------------------------
       
   192 //
       
   193 void TPhCntAiwServiceObserver::ResponseReceived( TInt aError )
       
   194     {
       
   195     iObserver.ServiceRequestComplete( aError );
       
   196     iResult = NULL;
       
   197     iParams = NULL;
       
   198     iAiwServiceShuttingDown = EFalse;
       
   199     }
       
   200     
       
   201 // ---------------------------------------------------------------------------
       
   202 // Checks if running in phone process. The handling of KAiwEventQueryExit
       
   203 // is done differently if running in phone process.
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 TBool TPhCntAiwServiceObserver::RunningInPhoneAppProcess() const
       
   207     {
       
   208     RProcess currentProcess;
       
   209 	TBool phoneProcess( EFalse );
       
   210 	TUid processID = currentProcess.Identity();
       
   211 	if ( processID.iUid == KPhoneProcess )
       
   212         {
       
   213         phoneProcess = ETrue;    
       
   214         }
       
   215     return phoneProcess;
       
   216     }
       
   217