phonebookui/Phonebook2/ServerApplication/src/CPbk2FetchService.cpp
changeset 0 e686773b3f54
child 18 d4f567ce2e7c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 application server fetch service.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2FetchService.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include "CPbk2EntryFetcher.h"
       
    23 #include "CPbk2ItemFetcher.h"
       
    24 
       
    25 // Virtual Phonebook
       
    26 #include <MVPbkContactLinkArray.h>
       
    27 
       
    28 // System includes
       
    29 #include <AiwContactSelectionDataTypes.h>
       
    30 #include <eikappui.h>
       
    31 #include <eikenv.h>
       
    32 #include <avkon.hrh>
       
    33 
       
    34 // Debug
       
    35 #include <Pbk2Debug.h>
       
    36 
       
    37 /// Unnamed namespace for local definitions
       
    38 namespace {
       
    39 
       
    40 _LIT(KPanicText, "CPbk2FetchService");
       
    41 
       
    42 enum TPanicCode
       
    43     {
       
    44     EInvalidRequest,
       
    45     EInvalidSelectionType
       
    46     };
       
    47 
       
    48 // RMessage IPC-argument slot order
       
    49 const TInt KResponseSlot = 0;
       
    50 const TInt KPackedResultsSlot = 0;
       
    51 const TInt KExtraResultsSlot = 1;
       
    52 const TInt KFieldContentSlot = 2;
       
    53 const TInt KResultArraySizeSlot = 0;
       
    54 const TInt KResultFieldSizeSlot = 1;
       
    55 const TInt KFetchInstructionsSlot = 1;
       
    56 
       
    57 const TInt KAcceptServiceSlot = 0;          // acception protocol
       
    58 const TInt KMarkedEntriesCountSlot = 0;     // acception protocol
       
    59 const TInt KSelectedLinksSlot = 1;          // acception protocol
       
    60 const TInt KSelectedContactSlot = 1;        // acception protocol
       
    61 
       
    62 const TInt KExitAcceptedSlot = 0;           // ok to exit protocol
       
    63 const TInt KExitCommandIdSlot = 1;          // ok to exit protocol
       
    64 const TInt KCompleteExitCommandIdSlot = 0;  // ok to exit protocol
       
    65 
       
    66 } /// namespace
       
    67 
       
    68 
       
    69 // --------------------------------------------------------------------------
       
    70 // CPbk2FetchService::CPbk2FetchService
       
    71 // --------------------------------------------------------------------------
       
    72 //
       
    73 CPbk2FetchService::CPbk2FetchService
       
    74         ( const RMessage2& aExitMsg, const RMessage2& aAcceptMsg ) :
       
    75             iExitMsg( aExitMsg), iAcceptMsg( aAcceptMsg )
       
    76     {
       
    77     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
    78         ("CPbk2FetchService::CPbk2FetchService()") );
       
    79     }
       
    80 
       
    81 // --------------------------------------------------------------------------
       
    82 // CPbk2FetchService::~CPbk2FetchService
       
    83 // --------------------------------------------------------------------------
       
    84 //
       
    85 CPbk2FetchService::~CPbk2FetchService()
       
    86     {
       
    87     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
    88         ("CPbk2FetchService::~CPbk2FetchService()") );
       
    89 
       
    90     if ( !iFetchCompleteMsg.IsNull() )
       
    91         {
       
    92         iFetchCompleteMsg.Complete( KErrServerTerminated );
       
    93         }
       
    94 
       
    95     delete iUiService;
       
    96     }
       
    97 
       
    98 // --------------------------------------------------------------------------
       
    99 // CPbk2FetchService::NewL
       
   100 // --------------------------------------------------------------------------
       
   101 //
       
   102 CPbk2FetchService* CPbk2FetchService::NewL
       
   103         ( const RMessage2& aExitMsg, const RMessage2& aAcceptMsg )
       
   104     {
       
   105     CPbk2FetchService* self =
       
   106             new ( ELeave ) CPbk2FetchService( aExitMsg, aAcceptMsg );
       
   107     return self;
       
   108     }
       
   109 
       
   110 // --------------------------------------------------------------------------
       
   111 // CPbk2FetchService::LaunchServiceL
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 void CPbk2FetchService::LaunchServiceL( const RMessage2& aMessage )
       
   115     {
       
   116     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   117         ("CPbk2FetchService::LaunchServiceL()") );
       
   118 
       
   119     if ( ( iFetchCompleteMsg.Handle() != KNullHandle ) &&
       
   120          ( iAcceptMsg.Handle() == KNullHandle ) )
       
   121         {
       
   122         aMessage.Panic( KPanicText(), EInvalidRequest );
       
   123         }
       
   124 
       
   125     TPbk2FetchType fetchType = FetchTypeL( aMessage );
       
   126 
       
   127     switch ( fetchType )
       
   128         {
       
   129         case EItemFetch:    // FALLTHROUGH
       
   130         case ECallItemFetch:
       
   131         case EMultipleItemFetch:
       
   132             {
       
   133             LaunchItemFetchL( aMessage, fetchType );
       
   134             break;
       
   135             }
       
   136         case EEntryFetch:   // FALLTHROUGH
       
   137         case EMultipleEntryFetch:
       
   138             {
       
   139             LaunchEntryFetchL( aMessage, fetchType );
       
   140             break;
       
   141             }
       
   142         default:
       
   143             {
       
   144             aMessage.Panic( KPanicText(), EInvalidRequest );
       
   145             break;
       
   146             }
       
   147         }
       
   148     }
       
   149 
       
   150 // --------------------------------------------------------------------------
       
   151 // CPbk2FetchService::CancelService
       
   152 // --------------------------------------------------------------------------
       
   153 //
       
   154 void CPbk2FetchService::CancelService( const RMessage2& aMessage )
       
   155     {
       
   156     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   157         ("CPbk2FetchService::CancelService()") );
       
   158 
       
   159     // If iFetchCompleteMsg is already completed do nothing.
       
   160     if ( !iFetchCompleteMsg.IsNull() )
       
   161         {
       
   162         iUiService->CancelService();
       
   163         }
       
   164     aMessage.Complete( KErrNone );
       
   165     }
       
   166 
       
   167 // --------------------------------------------------------------------------
       
   168 // CPbk2FetchService::GetResultsL
       
   169 // --------------------------------------------------------------------------
       
   170 //
       
   171 void CPbk2FetchService::GetResultsL( const RMessage2& aMessage )
       
   172     {
       
   173     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   174         ("CPbk2FetchService::GetResultsL()") );
       
   175 
       
   176     if ( iUiService )
       
   177         {
       
   178         MPbk2UiService::TServiceResults results;
       
   179         iUiService->ServiceResults(&results);
       
   180         if ( results.iLinkArray )
       
   181             {
       
   182             HBufC8* packedResults = results.iLinkArray->PackLC();
       
   183             TPckg<TInt> extraData(results.iExtraData);
       
   184             TRAP_IGNORE(
       
   185                 aMessage.WriteL(KPackedResultsSlot, *packedResults );
       
   186                 aMessage.WriteL(KExtraResultsSlot, extraData );
       
   187             );
       
   188             CleanupStack::PopAndDestroy(); // packedResults
       
   189             }
       
   190         
       
   191         if ( results.iFieldContent != NULL )
       
   192             {
       
   193             TRAP_IGNORE(
       
   194                 aMessage.WriteL( KFieldContentSlot, *results.iFieldContent );
       
   195             );
       
   196             }
       
   197         }
       
   198     aMessage.Complete( KErrNone );
       
   199     }
       
   200 
       
   201 // --------------------------------------------------------------------------
       
   202 // CPbk2FetchService::GetResultSizeL
       
   203 // --------------------------------------------------------------------------
       
   204 //
       
   205 void CPbk2FetchService::GetResultSizeL( const RMessage2& aMessage )
       
   206     {
       
   207     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   208         ("CPbk2FetchService::GetResultSizeL()") );
       
   209 
       
   210     if ( iUiService )
       
   211         {
       
   212         MPbk2UiService::TServiceResults results;
       
   213         iUiService->ServiceResults(&results);
       
   214         if ( results.iLinkArray )
       
   215             {
       
   216             HBufC8* packedResults = results.iLinkArray->PackLC();
       
   217             TPtr8 ptr = packedResults->Des();
       
   218             TPckg<TInt> buffer( ptr.Size() );
       
   219             TRAP_IGNORE( aMessage.WriteL(
       
   220                 KResultArraySizeSlot, buffer ) );
       
   221             CleanupStack::PopAndDestroy(); // packedResults
       
   222             }
       
   223 
       
   224         if ( results.iFieldContent != NULL )
       
   225             {
       
   226             TPckg<TInt> buffer( results.iFieldContent->Des().Length() );
       
   227             TRAP_IGNORE( aMessage.WriteL(
       
   228                 KResultFieldSizeSlot, buffer ) );
       
   229             }
       
   230         else
       
   231             {
       
   232             TPckg<TInt> buffer( 0 );
       
   233             TRAP_IGNORE( aMessage.WriteL(
       
   234                 KResultFieldSizeSlot, buffer ) );
       
   235             }
       
   236         }
       
   237     aMessage.Complete( KErrNone );
       
   238     }
       
   239 
       
   240 // --------------------------------------------------------------------------
       
   241 // CPbk2FetchService::TryExitServiceL
       
   242 // --------------------------------------------------------------------------
       
   243 //
       
   244 void CPbk2FetchService::TryExitServiceL( const RMessage2& aMessage )
       
   245     {
       
   246     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   247         ("CPbk2FetchService::TryExitServiceL()") );
       
   248 
       
   249     TBool okToExit = EFalse;
       
   250     TPckg<TBool> exitPkg( okToExit );
       
   251     aMessage.ReadL( KExitAcceptedSlot, exitPkg );
       
   252 
       
   253     if ( iUiService && okToExit )
       
   254         {
       
   255         TInt exitId = KErrNotFound;
       
   256         TPckg<TInt> commandIdPkg( exitId );
       
   257         aMessage.ReadL( KExitCommandIdSlot, commandIdPkg );
       
   258 
       
   259 
       
   260         iUiService->ExitServiceL( exitId );
       
   261         }
       
   262 
       
   263     aMessage.Complete( KErrNone );
       
   264     }
       
   265 
       
   266 // --------------------------------------------------------------------------
       
   267 // CPbk2FetchService::TryAcceptServiceL
       
   268 // --------------------------------------------------------------------------
       
   269 //
       
   270 void CPbk2FetchService::TryAcceptServiceL( const RMessage2& aMessage )
       
   271     {
       
   272     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   273         ("CPbk2FetchService::TryAcceptServiceL()") );
       
   274 
       
   275     TBool accepted = EFalse;
       
   276     TPckg<TBool> acceptedPkg( accepted );
       
   277     aMessage.ReadL( KAcceptServiceSlot, acceptedPkg );
       
   278 
       
   279     if ( iUiService && accepted )
       
   280         {
       
   281         HBufC8* buffer = HBufC8::NewLC(
       
   282             aMessage.GetDesMaxLengthL( KSelectedContactSlot ) );
       
   283         TPtr8 ptr = buffer->Des();
       
   284         aMessage.ReadL( KSelectedContactSlot, ptr );
       
   285         iUiService->AcceptDelayedContactsL( *buffer );
       
   286         CleanupStack::PopAndDestroy(); // buffer
       
   287         }
       
   288 
       
   289     aMessage.Complete( KErrNone );
       
   290     }
       
   291 
       
   292 // --------------------------------------------------------------------------
       
   293 // CPbk2FetchService::ServiceComplete
       
   294 // --------------------------------------------------------------------------
       
   295 //
       
   296 void CPbk2FetchService::ServiceComplete()
       
   297     {
       
   298     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   299         ("CPbk2FetchService::ServiceComplete()") );
       
   300 
       
   301     if ( !iFetchCompleteMsg.IsNull() )
       
   302         {
       
   303         TPckg<TBool> complete( EFalse );
       
   304         TRAP_IGNORE( iFetchCompleteMsg.WriteL( KResponseSlot, complete ) );
       
   305         iFetchCompleteMsg.Complete( KErrNone );
       
   306         }
       
   307     }
       
   308 
       
   309 // --------------------------------------------------------------------------
       
   310 // CPbk2FetchService::ServiceCanceled
       
   311 // --------------------------------------------------------------------------
       
   312 //
       
   313 void CPbk2FetchService::ServiceCanceled()
       
   314     {
       
   315     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   316         ("CPbk2FetchService::ServiceCanceled()") );
       
   317 
       
   318     if ( !iFetchCompleteMsg.IsNull() )
       
   319         {
       
   320         TPckg<TBool> cancel( ETrue );
       
   321         TRAP_IGNORE( iFetchCompleteMsg.WriteL( KResponseSlot, cancel ) );
       
   322         iFetchCompleteMsg.Complete( KErrCancel );
       
   323         }
       
   324     }
       
   325 
       
   326 // --------------------------------------------------------------------------
       
   327 // CPbk2FetchService::ServiceAborted
       
   328 // --------------------------------------------------------------------------
       
   329 //
       
   330 void CPbk2FetchService::ServiceAborted()
       
   331     {
       
   332     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   333         ("CPbk2FetchService::ServiceAborted()") );
       
   334 
       
   335     if ( !iFetchCompleteMsg.IsNull() )
       
   336         {
       
   337         iFetchCompleteMsg.Complete( KErrAbort );
       
   338 
       
   339         // Exit application
       
   340         CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   341         if ( eikonEnv )
       
   342             {
       
   343             CEikAppUi* appUi = eikonEnv->EikAppUi();
       
   344             MEikCommandObserver* cmdObs =
       
   345                 static_cast<MEikCommandObserver*>( appUi );
       
   346 
       
   347             // Dialog is closed so there is nothing to do if
       
   348             // ProcessCommandL leaves. Of course it shouldn't leave in
       
   349             // practice because it's exit command.
       
   350             TRAP_IGNORE( cmdObs->ProcessCommandL( EAknCmdExit ) );
       
   351             }
       
   352         }
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // CPbk2FetchService::ServiceError
       
   357 // --------------------------------------------------------------------------
       
   358 //
       
   359 void CPbk2FetchService::ServiceError( TInt aErrorCode )
       
   360     {
       
   361     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   362         ("CPbk2FetchService::ServiceError(%d)"), aErrorCode );
       
   363 
       
   364     TPckg<TInt> error( aErrorCode );
       
   365     TRAP_IGNORE( iFetchCompleteMsg.WriteL( KResponseSlot, error ) );
       
   366     iFetchCompleteMsg.Complete( aErrorCode );
       
   367     }
       
   368 
       
   369 // --------------------------------------------------------------------------
       
   370 // CPbk2FetchService::CompleteExitMessage
       
   371 // --------------------------------------------------------------------------
       
   372 //
       
   373 void CPbk2FetchService::CompleteExitMessage( TInt aExitCommandId )
       
   374     {
       
   375     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   376         ("CPbk2FetchService::CompleteExitMessage()") );
       
   377 
       
   378     TPckg<TInt> exitCommandId( aExitCommandId );
       
   379     if ( iUiService && iExitMsg.Handle() != KNullHandle )
       
   380         {
       
   381         TRAP_IGNORE( iExitMsg.WriteL(
       
   382             KCompleteExitCommandIdSlot, exitCommandId ) );
       
   383         iExitMsg.Complete( KErrNone );
       
   384         }
       
   385     }
       
   386 
       
   387 // --------------------------------------------------------------------------
       
   388 // CPbk2FetchService::CompleteAcceptMsg
       
   389 // --------------------------------------------------------------------------
       
   390 //
       
   391 void CPbk2FetchService::CompleteAcceptMsg
       
   392         ( const TDesC8& aMarkedEntries, const TDesC8& aLinkData )
       
   393     {
       
   394     PBK2_DEBUG_PRINT( PBK2_DEBUG_STRING
       
   395         ("CPbk2FetchService::CompleteAcceptMsg()") );
       
   396 
       
   397     if ( iUiService && iAcceptMsg.Handle() != KNullHandle )
       
   398         {
       
   399         // Verify that we have enough room in the buffer
       
   400         TInt maxSize = 0;
       
   401         TRAP_IGNORE( 
       
   402             maxSize = iAcceptMsg.GetDesMaxLengthL( KSelectedLinksSlot ) );
       
   403         TInt bufferSize = aLinkData.Size();
       
   404 
       
   405         if ( bufferSize > maxSize )
       
   406             {
       
   407             iAcceptMsg.Complete( KErrOverflow );
       
   408             }
       
   409         else
       
   410             {
       
   411             TRAP_IGNORE(
       
   412                 {
       
   413                 iAcceptMsg.WriteL( KMarkedEntriesCountSlot, aMarkedEntries );
       
   414                 iAcceptMsg.WriteL( KSelectedLinksSlot, aLinkData );                
       
   415                 });
       
   416             iAcceptMsg.Complete( KErrNone );
       
   417             }
       
   418         }
       
   419     }
       
   420 
       
   421 // --------------------------------------------------------------------------
       
   422 // CPbk2FetchService::LaunchEntryFetchL
       
   423 // --------------------------------------------------------------------------
       
   424 //
       
   425 void CPbk2FetchService::LaunchEntryFetchL
       
   426         ( const RMessage2& aMessage, TPbk2FetchType aFetchType )
       
   427     {
       
   428     delete iUiService;
       
   429     iUiService = NULL;
       
   430     iUiService = CPbk2EntryFetcher::NewL( aMessage, *this, aFetchType );
       
   431     iUiService->LaunchServiceL();
       
   432 
       
   433     iFetchCompleteMsg = aMessage;
       
   434     }
       
   435 
       
   436 // --------------------------------------------------------------------------
       
   437 // CPbk2FetchService::LaunchItemFetchL
       
   438 // --------------------------------------------------------------------------
       
   439 //
       
   440 void CPbk2FetchService::LaunchItemFetchL
       
   441         ( const RMessage2& aMessage, TPbk2FetchType aFetchType )
       
   442     {
       
   443     delete iUiService;
       
   444     iUiService = NULL;
       
   445     iUiService = CPbk2ItemFetcher::NewL( aMessage, *this, aFetchType );
       
   446     iUiService->LaunchServiceL();
       
   447 
       
   448     iFetchCompleteMsg = aMessage;
       
   449     }
       
   450 
       
   451 // --------------------------------------------------------------------------
       
   452 // CPbk2FetchService::FetchTypeL
       
   453 // --------------------------------------------------------------------------
       
   454 //
       
   455 TPbk2FetchType CPbk2FetchService::FetchTypeL( const RMessage2& aMessage )
       
   456     {
       
   457     TPbk2FetchType result = EInvalidType;
       
   458 
       
   459     TInt length = aMessage.GetDesLengthL( KFetchInstructionsSlot );
       
   460     if ( length > 0 )
       
   461         {
       
   462         HBufC8* dataBuf = HBufC8::NewLC( length );
       
   463         TPtr8 ptr = dataBuf->Des();
       
   464         aMessage.ReadL( KFetchInstructionsSlot, ptr );
       
   465 
       
   466         TAiwContactSelectionDataType dataType =
       
   467             TAiwContactSelectionDataBase::SelectionDataTypeFromBuffer( ptr );
       
   468 
       
   469         switch ( dataType )
       
   470             {
       
   471             case EAiwSingleEntrySelectionV1:        // FALLTHROUGH
       
   472             case EAiwSingleEntrySelectionV2:
       
   473                 {
       
   474                 result = EEntryFetch;
       
   475                 break;
       
   476                 }
       
   477             case EAiwMultipleEntrySelectionV1:      // FALLTHROUGH
       
   478             case EAiwMultipleEntrySelectionV2:
       
   479                 {
       
   480                 result = EMultipleEntryFetch;
       
   481                 break;
       
   482                 }
       
   483             case EAiwSingleItemSelectionV1:
       
   484                 {
       
   485                 TAiwSingleItemSelectionDataV1Pckg data;
       
   486                 data.Copy( ptr );
       
   487                 result = EItemFetch;
       
   488 
       
   489                 TAiwAddressSelectType addressSelectType =
       
   490                     data().AddressSelectType();
       
   491                 if ( addressSelectType == EAiwCallItemSelect
       
   492                      || addressSelectType == EAiwVoIPItemSelect )
       
   493                     {
       
   494                     result = ECallItemFetch;
       
   495                     }
       
   496                 break;
       
   497                 }
       
   498             case EAiwSingleItemSelectionV2:
       
   499                 {
       
   500                 result = EItemFetch;
       
   501                 break;
       
   502                 }
       
   503             case EAiwSingleItemSelectionV3:
       
   504                 {
       
   505                 TAiwSingleItemSelectionDataV3Pckg data;
       
   506                 data.Copy( ptr );
       
   507                 result = EItemFetch;
       
   508 
       
   509                 TAiwAddressSelectType addressSelectType =
       
   510                     data().AddressSelectType();
       
   511                 if ( addressSelectType == EAiwCallItemSelect
       
   512                      || addressSelectType == EAiwVoIPItemSelect )
       
   513                     {
       
   514                     result = ECallItemFetch;
       
   515                     }
       
   516                 break;
       
   517                 }
       
   518             case EAiwMultipleItemSelectionV1:
       
   519                 {
       
   520                 result = EMultipleItemFetch;
       
   521                 break;
       
   522                 }
       
   523             default:
       
   524                 {
       
   525                 aMessage.Panic( KPanicText, EInvalidSelectionType );
       
   526                 break;
       
   527                 }
       
   528             }
       
   529         CleanupStack::PopAndDestroy( dataBuf );
       
   530         }
       
   531     else
       
   532         {
       
   533         aMessage.Panic( KPanicText, EInvalidSelectionType );
       
   534         }
       
   535 
       
   536     return result;
       
   537     }
       
   538 
       
   539 // End of File