phonebookui/Phonebook2/UIServices/src/CPbk2ServerAppConnection.cpp
branchRCL_3
changeset 63 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2005-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:  Phonebook 2 UI Service server application connection.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2ServerAppConnection.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "MPbk2ConnectionObserver.h"
       
    23 #include "CPbk2ExitServiceMonitor.h"
       
    24 #include "CPbk2AcceptServiceMonitor.h"
       
    25 #include <Pbk2InternalUID.h>
       
    26 
       
    27 // System includes
       
    28 #include <coemain.h>
       
    29 
       
    30 // Debugging headers
       
    31 #include <Pbk2Debug.h>
       
    32 
       
    33 // --------------------------------------------------------------------------
       
    34 // CPbk2ServerAppConnection::CPbk2ServerAppConnection
       
    35 // --------------------------------------------------------------------------
       
    36 //
       
    37 CPbk2ServerAppConnection::CPbk2ServerAppConnection
       
    38         ( MPbk2ConnectionObserver& aObserver ) :
       
    39             CActive( EPriorityStandard ),
       
    40             iObserver( aObserver )
       
    41     {
       
    42     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
    43         "CPbk2ServerAppConnection::CPbk2ServerAppConnection()") );
       
    44     CActiveScheduler::Add( this );
       
    45     }
       
    46 
       
    47 // --------------------------------------------------------------------------
       
    48 // CPbk2ServerAppConnection::~CPbk2ServerAppConnection
       
    49 // --------------------------------------------------------------------------
       
    50 //
       
    51 CPbk2ServerAppConnection::~CPbk2ServerAppConnection()
       
    52     {
       
    53     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
    54         "CPbk2ServerAppConnection::~CPbk2ServerAppConnection()") );
       
    55     Cancel();
       
    56     delete iArrayResults;
       
    57     delete iFieldResults;
       
    58     DisconnectMonitors();
       
    59     DisconnectServerApplication();
       
    60     }
       
    61 
       
    62 // --------------------------------------------------------------------------
       
    63 // CPbk2ServerAppConnection::NewL
       
    64 // --------------------------------------------------------------------------
       
    65 //
       
    66 CPbk2ServerAppConnection* CPbk2ServerAppConnection::NewL
       
    67         ( MPbk2ConnectionObserver& aObserver )
       
    68     {
       
    69     CPbk2ServerAppConnection* self =
       
    70         new ( ELeave ) CPbk2ServerAppConnection( aObserver );
       
    71     return self;
       
    72     }
       
    73 
       
    74 // --------------------------------------------------------------------------
       
    75 // CPbk2ServerAppConnection::LaunchAssignL
       
    76 // --------------------------------------------------------------------------
       
    77 //
       
    78 void CPbk2ServerAppConnection::LaunchAssignL
       
    79         ( HBufC8* aConfigurationPackage, HBufC8* aDataPackage,
       
    80           HBufC8* aAssignInstructions )
       
    81     {
       
    82     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
    83         "CPbk2ServerAppConnection::LaunchAssignL()") );
       
    84     // Need to trap, because consumer freezes if ConnectL function leaves.
       
    85     // Reason for the freeze is that connection stays open.
       
    86     TRAPD( error, ConnectL() );
       
    87     if ( error == KErrNone )
       
    88         {
       
    89         iPbk2AppService.LaunchAssignL(
       
    90             iStatus,  aConfigurationPackage, aDataPackage,
       
    91             aAssignInstructions, iCanceled );
       
    92         SetActive();
       
    93         }
       
    94     else
       
    95         {
       
    96         DisconnectMonitors();
       
    97         DisconnectServerApplication();
       
    98         User::Leave( error );
       
    99         }
       
   100     }
       
   101 
       
   102 // --------------------------------------------------------------------------
       
   103 // CPbk2ServerAppConnection::LaunchAttributeAssignL
       
   104 // --------------------------------------------------------------------------
       
   105 //
       
   106 void CPbk2ServerAppConnection::LaunchAttributeAssignL
       
   107     ( HBufC8* aConfigurationPackage,
       
   108       TPbk2AttributeAssignData& aAttributePackage,
       
   109       HBufC8* aAssignInstructions )
       
   110     {
       
   111     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   112         "CPbk2ServerAppConnection::LaunchAttributeAssignL()") );
       
   113     ConnectL();
       
   114     iPbk2AppService.LaunchAttributeAssignL
       
   115         ( iStatus,  aConfigurationPackage, aAttributePackage,
       
   116           aAssignInstructions, iCanceled );
       
   117     SetActive();
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CPbk2ServerAppConnection::LaunchAttributeUnassignL
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 void CPbk2ServerAppConnection::LaunchAttributeUnassignL
       
   125     ( HBufC8* aConfigurationPackage,
       
   126       TPbk2AttributeAssignData aAttributePackage,
       
   127       HBufC8* aAssignInstructions )
       
   128     {
       
   129     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   130         "CPbk2ServerAppConnection::LaunchAttributeUnassignL()") );
       
   131     ConnectL();
       
   132     iPbk2AppService.LaunchAttributeUnassignL
       
   133         ( iStatus,  aConfigurationPackage, aAttributePackage,
       
   134           aAssignInstructions, iCanceled );
       
   135     SetActive();
       
   136     }
       
   137 
       
   138 // --------------------------------------------------------------------------
       
   139 // CPbk2ServerAppConnection::CancelAssign
       
   140 // --------------------------------------------------------------------------
       
   141 //
       
   142 void CPbk2ServerAppConnection::CancelAssign()
       
   143     {
       
   144     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   145         "CPbk2ServerAppConnection::CancelAssign()") );
       
   146     if ( iPbk2AppService.Handle() != KNullHandle )
       
   147         {
       
   148         iPbk2AppService.CancelAssign( iCanceled );
       
   149         }
       
   150     }
       
   151 
       
   152 // --------------------------------------------------------------------------
       
   153 // CPbk2ServerAppConnection::LaunchFetchL
       
   154 // --------------------------------------------------------------------------
       
   155 //
       
   156 void CPbk2ServerAppConnection::LaunchFetchL
       
   157         ( HBufC8* aConfigurationPackage, HBufC8* aFetchInstructions )
       
   158     {
       
   159     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   160         "CPbk2ServerAppConnection::LaunchFetchL()") );
       
   161     //AIW Cancel command uses CancelFetch and leaves AO active,
       
   162     //call here Cancel() to avoid using active AO (E32User-CBase 42)
       
   163     Cancel();
       
   164     // Need to trap, because consumer freezes if ConnectL function leaves.
       
   165     // Reason for the freeze is that connection stays open.
       
   166     TRAPD( error, ConnectL() );
       
   167 
       
   168     if ( error == KErrNone )
       
   169         {
       
   170         iPbk2AppService.LaunchFetchL
       
   171             ( iStatus, aConfigurationPackage,
       
   172               aFetchInstructions, iCanceled );
       
   173         SetActive();
       
   174         }
       
   175     else
       
   176         {
       
   177         DisconnectMonitors();
       
   178         DisconnectServerApplication();
       
   179         User::Leave( error );
       
   180         }
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CPbk2ServerAppConnection::CancelFetch
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 void CPbk2ServerAppConnection::CancelFetch()
       
   188     {
       
   189     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   190         "CPbk2ServerAppConnection::CancelFetch()") );
       
   191     if ( iPbk2AppService.Handle() != KNullHandle )
       
   192         {
       
   193         iPbk2AppService.CancelFetch( iCanceled );
       
   194         }
       
   195     }
       
   196 
       
   197 // --------------------------------------------------------------------------
       
   198 // CPbk2ServerAppConnection::RunL
       
   199 // --------------------------------------------------------------------------
       
   200 //
       
   201 void CPbk2ServerAppConnection::RunL()
       
   202     {
       
   203     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   204         "CPbk2ServerAppConnection::RunL(%d)"), iStatus.Int() );
       
   205     switch( iStatus.Int() )
       
   206         {
       
   207         case KErrNone:
       
   208             {
       
   209             GetResultsL();
       
   210             iObserver.OperationCompleteL( *iArrayResults, iExtraResultData,
       
   211                 *iFieldResults );
       
   212             DisconnectMonitors();
       
   213             DisconnectServerApplication();
       
   214             break;
       
   215             }
       
   216         case KErrCancel:            // FALLTHROUGH
       
   217         case KErrServerTerminated:
       
   218             {
       
   219             iObserver.OperationCanceledL();
       
   220             DisconnectMonitors();
       
   221             DisconnectServerApplication();
       
   222             break;
       
   223             }
       
   224         case KErrAbort:
       
   225             {
       
   226             // Abort is like completion with KErrCancel,
       
   227             // expect that we leave the server application alive
       
   228             // and wait for it to close itself down and send
       
   229             // HandleServerAppExit notification
       
   230             iObserver.OperationCanceledL();
       
   231             DisconnectMonitors();
       
   232             break;
       
   233             }
       
   234         default:
       
   235             {
       
   236             // Delegate to RunError
       
   237             User::Leave( iStatus.Int() );
       
   238             break;
       
   239             }
       
   240         };
       
   241     }
       
   242 
       
   243 // --------------------------------------------------------------------------
       
   244 // CPbk2ServerAppConnection::RunError
       
   245 // --------------------------------------------------------------------------
       
   246 //
       
   247 TInt CPbk2ServerAppConnection::RunError( TInt aError )
       
   248     {
       
   249     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   250         "CPbk2ServerAppConnection::RunError(%d)"), aError );
       
   251     TRAPD( err,  iObserver.OperationErrorL( aError ) );
       
   252     DisconnectMonitors();
       
   253     DisconnectServerApplication();
       
   254     return err;
       
   255     }
       
   256 
       
   257 // --------------------------------------------------------------------------
       
   258 // CPbk2ServerAppConnection::DoCancel
       
   259 // --------------------------------------------------------------------------
       
   260 //
       
   261 void CPbk2ServerAppConnection::DoCancel()
       
   262     {
       
   263     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   264         "CPbk2ServerAppConnection::DoCancel()") );
       
   265     // Cancels outstanding notification monitors
       
   266     if ( iExitServiceMonitor )
       
   267         {
       
   268         iExitServiceMonitor->Cancel();
       
   269         }
       
   270     if ( iAcceptServiceMonitor )
       
   271         {
       
   272         iAcceptServiceMonitor->Cancel();
       
   273         }
       
   274 
       
   275     // Cancels outstanding operations
       
   276     CancelAssign();
       
   277     CancelFetch();
       
   278     }
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CPbk2ServerAppConnection::HandleServerAppExit
       
   282 // --------------------------------------------------------------------------
       
   283 //
       
   284 void CPbk2ServerAppConnection::HandleServerAppExit( TInt aReason )
       
   285     {
       
   286     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING(
       
   287         "CPbk2ServerAppConnection::HandleServerAppExit(%d)"), aReason );
       
   288     TBool okToExit = EFalse;
       
   289 
       
   290     // Query the observer whether it is ok to exit application
       
   291     // (the consumer process)
       
   292     TRAPD( err,
       
   293         okToExit = iObserver.OkToExitL( aReason, EPbk2ExitApplication ) );
       
   294 
       
   295     if ( err != KErrNone )
       
   296         {
       
   297         CCoeEnv::Static()->HandleError( err );
       
   298         }
       
   299 
       
   300     if (okToExit)
       
   301         {
       
   302         MAknServerAppExitObserver::HandleServerAppExit( aReason );
       
   303         }
       
   304     else
       
   305         {
       
   306         DisconnectServerApplication();
       
   307         }
       
   308     }
       
   309 
       
   310 // --------------------------------------------------------------------------
       
   311 // CPbk2ServerAppConnection::ConnectL
       
   312 // --------------------------------------------------------------------------
       
   313 //
       
   314 void CPbk2ServerAppConnection::ConnectL()
       
   315     {
       
   316     if ( iPbk2AppService.Handle() == KNullHandle )
       
   317         {
       
   318         iPbk2AppService.ConnectChainedAppL
       
   319             ( TUid::Uid( KPbk2ServerAppUID3 ) );
       
   320 
       
   321         delete iExitAppServerMonitor;
       
   322         iExitAppServerMonitor = NULL;
       
   323         iExitAppServerMonitor = CApaServerAppExitMonitor::NewL
       
   324             ( iPbk2AppService, *this, CActive::EPriorityStandard );
       
   325 
       
   326         delete iExitServiceMonitor;
       
   327         iExitServiceMonitor = NULL;
       
   328         iExitServiceMonitor = CPbk2ExitServiceMonitor::NewL
       
   329             ( iPbk2AppService, iObserver );
       
   330 
       
   331         delete iAcceptServiceMonitor;
       
   332         iAcceptServiceMonitor = NULL;
       
   333         iAcceptServiceMonitor = CPbk2AcceptServiceMonitor::NewL
       
   334             ( iPbk2AppService, iObserver );
       
   335         }
       
   336     }
       
   337 
       
   338 // --------------------------------------------------------------------------
       
   339 // CPbk2ServerAppConnection::GetResultsL
       
   340 // --------------------------------------------------------------------------
       
   341 //
       
   342 void CPbk2ServerAppConnection::GetResultsL()
       
   343     {
       
   344     // Important to delete old results
       
   345     delete iArrayResults;
       
   346     iArrayResults = NULL;
       
   347     delete iFieldResults;
       
   348     iFieldResults = NULL;
       
   349 
       
   350     iExtraResultData = KErrNotSupported;
       
   351 
       
   352     // First get the result size
       
   353     TInt arraySize = 0;
       
   354     TInt fieldSize = 0;
       
   355     iPbk2AppService.GetResultSizeL( arraySize, fieldSize );
       
   356     iArrayResults = HBufC8::NewL( arraySize );
       
   357     iFieldResults = HBufC::NewL( fieldSize );
       
   358 
       
   359     // Then get the results
       
   360     iPbk2AppService.GetResultsL( *iArrayResults, iExtraResultData,
       
   361         *iFieldResults );
       
   362     }
       
   363 
       
   364 // --------------------------------------------------------------------------
       
   365 // CPbk2ServerAppConnection::DisconnectMonitors
       
   366 // --------------------------------------------------------------------------
       
   367 //
       
   368 void CPbk2ServerAppConnection::DisconnectMonitors()
       
   369     {
       
   370     delete iExitServiceMonitor;
       
   371     iExitServiceMonitor = NULL;
       
   372     delete iAcceptServiceMonitor;
       
   373     iAcceptServiceMonitor = NULL;
       
   374     }
       
   375 
       
   376 // --------------------------------------------------------------------------
       
   377 // CPbk2ServerAppConnection::DisconnectServerApplication
       
   378 // --------------------------------------------------------------------------
       
   379 //
       
   380 void CPbk2ServerAppConnection::DisconnectServerApplication()
       
   381     {
       
   382     delete iExitAppServerMonitor;
       
   383     iExitAppServerMonitor = NULL;
       
   384     iPbk2AppService.Close();
       
   385     }
       
   386 
       
   387 // End of File