mobilemessaging/postcard/postcardsrc/PostcardContact.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2006 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:  
       
    15 *           Postcard application's interface to contacts (Virtual Phonebook
       
    16 *           and Phonebook2).
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include <s32file.h>
       
    25 #include <aknenv.h>
       
    26 #include <AiwServiceHandler.h>
       
    27 #include <AiwContactSelectionDataTypes.h>
       
    28 #include <CPbk2StoreConfiguration.h>
       
    29 #include <VPbkContactStoreUris.h>
       
    30 #include <CVPbkContactStoreUriArray.h>
       
    31 #include <TVPbkContactStoreUriPtr.h>
       
    32 #include <CVPbkContactLinkArray.h>
       
    33 #include <TVPbkFieldVersitProperty.h>
       
    34 #include <MVPbkStoreContact.h>
       
    35 #include <contactmatcher.h>
       
    36 #include <akninputblock.h>
       
    37 
       
    38 #include <Postcard.rsg>
       
    39 #include "PostcardContact.h"
       
    40 #include "PostcardPanic.h"
       
    41 
       
    42 // ================= Static Constant Data ===================
       
    43 
       
    44 // Table to convert from TLocation to virtual phonebook field type parameter
       
    45 const TVPbkFieldTypeParameter CPostcardContact::iLocToFieldTypeParam[] =
       
    46     {
       
    47     EVPbkVersitParamPREF,
       
    48     EVPbkVersitParamHOME,
       
    49     EVPbkVersitParamWORK
       
    50     };
       
    51 
       
    52 // Table to convert from TAddressField to virtual phonebook subfield type
       
    53 const TVPbkSubFieldType CPostcardContact::iAddrFieldToSubFieldType[] =
       
    54     {
       
    55     EVPbkVersitSubFieldStreet,
       
    56     EVPbkVersitSubFieldExtendedAddress,
       
    57     EVPbkVersitSubFieldPostalCode,
       
    58     EVPbkVersitSubFieldLocality,
       
    59     EVPbkVersitSubFieldRegion,
       
    60     EVPbkVersitSubFieldCountry
       
    61     };
       
    62 
       
    63 // Table to convert from TPostcardControls to TAddressField
       
    64 const CPostcardContact::TAddressField CPostcardContact::iControlIdToAddrField[] =
       
    65     {
       
    66     CPostcardContact::EAddressFieldExtendedAddress,
       
    67     CPostcardContact::EAddressFieldStreet,
       
    68     CPostcardContact::EAddressFieldPostalCode,
       
    69     CPostcardContact::EAddressFieldLocality,
       
    70     CPostcardContact::EAddressFieldRegion,
       
    71     CPostcardContact::EAddressFieldCountry
       
    72     };
       
    73 
       
    74 // ================= MEMBER FUNCTIONS =======================
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 //  Two-phased constructor
       
    78 // ---------------------------------------------------------
       
    79 CPostcardContact* CPostcardContact::NewL( RFs& aFs )
       
    80     {
       
    81     CPostcardContact* self = new (ELeave) CPostcardContact( aFs );
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL();
       
    84     CleanupStack::Pop( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------
       
    89 //  Default destructor
       
    90 // ---------------------------------------------------------
       
    91 CPostcardContact::~CPostcardContact()
       
    92     {
       
    93     delete iStoreContact;
       
    94     delete iContactMatcher;
       
    95     delete iUriArray;
       
    96     delete iAiwServiceHandler;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CPostcardContact
       
   101 // ---------------------------------------------------------
       
   102 CPostcardContact::CPostcardContact( RFs& aFs ) : iFs( aFs )
       
   103     {
       
   104     }
       
   105 
       
   106 
       
   107 // ---------------------------------------------------------
       
   108 // ConstructL
       
   109 // ---------------------------------------------------------
       
   110 void CPostcardContact::ConstructL( )
       
   111     {
       
   112 #ifdef __WINS__
       
   113     // Use default contact database store 
       
   114     iUriArray = CVPbkContactStoreUriArray::NewL();
       
   115     TVPbkContactStoreUriPtr uriPtr(VPbkContactStoreUris::DefaultCntDbUri());
       
   116     iUriArray->AppendL(uriPtr);
       
   117 #else
       
   118     // Get store configuration from phonebook2
       
   119     CPbk2StoreConfiguration* storeConfiguration =
       
   120         CPbk2StoreConfiguration::NewL();
       
   121     CleanupStack::PushL(storeConfiguration);
       
   122 
       
   123     iUriArray = storeConfiguration->CurrentConfigurationL();
       
   124     CleanupStack::PopAndDestroy( storeConfiguration ); // storeConfiguration
       
   125 #endif
       
   126 
       
   127     // Create contact matcher
       
   128     iContactMatcher = CContactMatcher::NewL( &iFs );
       
   129     iContactMatcher->OpenStoreL(*iUriArray);
       
   130     
       
   131     iShutdown = EFalse;
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------
       
   135 // HasLocationL
       
   136 // ---------------------------------------------------------
       
   137 TBool CPostcardContact::HasLocationL(TLocation aLocation) const
       
   138     {
       
   139     // A small buffer to see if the contact has any fields
       
   140     // defined for the location aLocation
       
   141     const TInt KBufLen = 8;
       
   142     TBuf<KBufLen> contactString;
       
   143 
       
   144     GetContactStringL(contactString, aLocation);
       
   145     return contactString.Length() > 0;
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------
       
   149 // GetContactStringL
       
   150 // ---------------------------------------------------------
       
   151 void CPostcardContact::GetContactStringL(TDes& aBuffer,
       
   152     TLocation aAddressLocation) const
       
   153     {
       
   154     static const TAddressField addrFields[] =
       
   155         {
       
   156         EAddressFieldStreet,
       
   157         EAddressFieldExtendedAddress,
       
   158         EAddressFieldPostalCode,
       
   159         EAddressFieldLocality,
       
   160         EAddressFieldRegion,
       
   161         EAddressFieldCountry
       
   162         };
       
   163     const TInt KNumAddrFields = sizeof(addrFields) / sizeof(addrFields[0]);
       
   164 
       
   165     _LIT(KPostcardAddressSelectionSeparator, ", ");
       
   166     TPtrC fieldSeparator(KPostcardAddressSelectionSeparator);
       
   167 
       
   168     aBuffer.Zero();
       
   169     TInt bufferRemaining = aBuffer.MaxLength();
       
   170 
       
   171     // Read address fields from the contact. Add field to buffer if there
       
   172     // is space for the separator and at least one more character.
       
   173     for(TInt i = 0; i < KNumAddrFields && bufferRemaining > fieldSeparator.Length(); i++)
       
   174         {
       
   175         HBufC* fieldText = GetAddressFieldLC(aAddressLocation, addrFields[i]);
       
   176         if (fieldText->Length() > 0)
       
   177             {
       
   178             // Add the separator if this is not the first non-empty field
       
   179             if (aBuffer.Length() > 0)
       
   180                 {
       
   181                 aBuffer.Append(fieldSeparator);
       
   182                 bufferRemaining -= fieldSeparator.Length();
       
   183                 }
       
   184 
       
   185             if (bufferRemaining > fieldText->Length())
       
   186                 {
       
   187                 aBuffer.Append(*fieldText);
       
   188                 }
       
   189             else
       
   190                 {
       
   191                 aBuffer.Append(fieldText->Left(bufferRemaining));
       
   192                 }
       
   193             bufferRemaining = aBuffer.MaxLength() - aBuffer.Length();
       
   194             }
       
   195         CleanupStack::PopAndDestroy( fieldText );  // fieldText
       
   196         }
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // GetAddressFieldLC
       
   201 // ---------------------------------------------------------
       
   202 HBufC* CPostcardContact::GetAddressFieldLC(TLocation aLocation,
       
   203     TAddressField aAddressField) const
       
   204     {
       
   205     return GetContactFieldLC(EVPbkVersitNameADR,
       
   206         AddrFieldToSubFieldType(aAddressField),
       
   207         LocToFieldTypeParam(aLocation));
       
   208     }
       
   209 
       
   210 // ---------------------------------------------------------
       
   211 // FetchContactL
       
   212 // ---------------------------------------------------------
       
   213 void CPostcardContact::FetchContactL()
       
   214     {
       
   215     // Fetch contact from Phonebook using AIW
       
   216 
       
   217     if (!iAiwServiceHandler)
       
   218         {
       
   219         // Create AIW service handler
       
   220         iAiwServiceHandler = CAiwServiceHandler::NewL();
       
   221         iAiwServiceHandler->AttachL(R_POSTCARD_SINGLE_ENTRY_FETCH_INTEREST);
       
   222         }
       
   223 
       
   224 
       
   225     // A new contact is selected. Delete contact in case FetchContactL()
       
   226     // was called before.
       
   227     delete iStoreContact;
       
   228     iStoreContact = NULL;
       
   229 
       
   230     CAiwGenericParamList& inList = iAiwServiceHandler->InParamListL();
       
   231     InitAiwContactFetchParamL( inList );
       
   232 
       
   233     iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, inList,
       
   234         iAiwServiceHandler->OutParamListL(), 0, this);
       
   235 
       
   236     // Assign an input blocker, which will call cancel to phonebook
       
   237     // e.g. if Postcard is closed via FSW.
       
   238     CAknInputBlock* inputBlock = CAknInputBlock::NewCancelHandlerLC( this );
       
   239 
       
   240     iActSchedWait.Start(); // wait ExecuteServiceCmdL() to complete
       
   241 
       
   242     CleanupStack::PopAndDestroy( inputBlock );
       
   243     
       
   244     if ( iShutdown )
       
   245         {
       
   246         // This leave will be catched in AppUi's DoEditAddressL function.
       
   247         User::Leave( KLeaveExit );
       
   248         }
       
   249     }
       
   250 
       
   251 // ---------------------------------------------------------
       
   252 // FetchContactL
       
   253 // ---------------------------------------------------------
       
   254 void CPostcardContact::FetchContactL(RFile& aFile)
       
   255     {
       
   256     __ASSERT_DEBUG( !iStoreContact, Panic(EPostcardPanicCoding));
       
   257 
       
   258     // Using streams would be more elegant but Phonebook2 impletes it this
       
   259     // way. Writes the descriptor to a file directly without a length.
       
   260     TInt fileSize;
       
   261     User::LeaveIfError( aFile.Size( fileSize ) );
       
   262     HBufC8* buf = HBufC8::NewLC( fileSize );
       
   263     TPtr8 ptr = buf->Des();
       
   264     User::LeaveIfError( aFile.Read( ptr ) );
       
   265     CVPbkContactLinkArray* linkArray = CVPbkContactLinkArray::NewLC(
       
   266         *buf, iContactMatcher->GetContactStoresL());
       
   267     if( linkArray->Count() == 0 )
       
   268         {
       
   269         User::Leave( KErrNotFound );
       
   270         }
       
   271     iContactMatcher->GetStoreContactL( linkArray->At( 0 ), &iStoreContact );
       
   272     CleanupStack::PopAndDestroy( 2, buf ); // linkArray, buf
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------
       
   276 // HandleNotifyL
       
   277 // ---------------------------------------------------------
       
   278 TInt CPostcardContact::HandleNotifyL(TInt /*aCmdId*/, TInt aEventId,
       
   279     CAiwGenericParamList& aEventParamList,
       
   280     const CAiwGenericParamList& /*aInParamList*/)
       
   281     {
       
   282 
       
   283     switch(aEventId)
       
   284         {
       
   285         case KAiwEventOutParamCheck: // fall through
       
   286         case KAiwEventQueryExit:// fall through
       
   287             // It is always ok for us to exit. Edited postcard will be
       
   288             // saved to drafts.
       
   289             return ETrue;
       
   290 
       
   291         case KAiwEventCompleted:
       
   292             // Fetch completed with or without a contact
       
   293             TRAP_IGNORE(HandleAiwEventCompletedL(aEventParamList));
       
   294             // Leave in HandleAiwEventCompletedL() is not progated further.
       
   295             // After that the Postcard wouldn't respond to UI events. Even when
       
   296             // AsyncStop() is called. Maybe Phonebook cannot handle a leave
       
   297             // from here properly.
       
   298 
       
   299             // Fallthrough
       
   300         case KAiwEventCanceled:
       
   301             // Fetch was cancelled by user or red key pressed
       
   302             // Fallthrough
       
   303         case KAiwEventError:
       
   304             // Fetch is complete, there was an error
       
   305             if (iActSchedWait.IsStarted())
       
   306                 {
       
   307                 // End nested active scheduler loop
       
   308                 iActSchedWait.AsyncStop();
       
   309                 }
       
   310             break;
       
   311         default:
       
   312             Panic(EPostcardPanicAiwEvent);
       
   313         }
       
   314 
       
   315     return KErrNone;
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------
       
   319 // HandleAiwEventCompleted
       
   320 // ---------------------------------------------------------
       
   321 void CPostcardContact::HandleAiwEventCompletedL(
       
   322     CAiwGenericParamList& aEventParamList)
       
   323     {
       
   324     // Single item fetch. The result should be a contact link array with
       
   325     // 1 contact links and one contact field.
       
   326     __ASSERT_ALWAYS(aEventParamList.Count() == 2,
       
   327         Panic(EPostcardPanicAiwEventParam));
       
   328     const TAiwGenericParam& param = aEventParamList[0];
       
   329     __ASSERT_ALWAYS(param.SemanticId() == EGenericParamContactLinkArray,
       
   330         Panic(EPostcardPanicAiwEventParam));
       
   331     __ASSERT_DEBUG(param.Value().TypeId() == EVariantTypeDesC8,
       
   332         Panic(EPostcardPanicAiwEventParam));
       
   333     TPtrC8 contactLinks = param.Value().AsData();
       
   334     CVPbkContactLinkArray* linkArray = CVPbkContactLinkArray::NewLC(
       
   335         contactLinks, iContactMatcher->GetContactStoresL() );
       
   336     TInt linkCount = linkArray->Count();
       
   337     __ASSERT_DEBUG(linkCount <= 1, Panic(EPostcardPanicAiwEventParam));
       
   338     if (linkCount)  // user selected a contact
       
   339         {
       
   340         iContactMatcher->GetStoreContactL( linkArray->At( 0 ), &iStoreContact );
       
   341         }
       
   342     CleanupStack::PopAndDestroy( linkArray );  // linkArray
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------
       
   346 // InitAiwContactFetchParamL
       
   347 // ---------------------------------------------------------
       
   348 void CPostcardContact::InitAiwContactFetchParamL(
       
   349     CAiwGenericParamList& aParamList ) const
       
   350     {
       
   351     TAiwGenericParam param( EGenericParamContactSelectionData );
       
   352     TAiwSingleEntrySelectionDataV1 selectionData;
       
   353     param.Value().Set( TAiwSingleEntrySelectionDataV1Pckg( selectionData ) );
       
   354     aParamList.AppendL( param );
       
   355 
       
   356     param.Reset();
       
   357     param.SetSemanticId( EGenericParamContactStoreUriArray );
       
   358     param.Value().Set( *iUriArray->PackLC() );
       
   359     aParamList.AppendL( param );
       
   360     CleanupStack::PopAndDestroy(); // packed uriArray
       
   361     }
       
   362 
       
   363 // ---------------------------------------------------------
       
   364 // GetNameLC()
       
   365 // ---------------------------------------------------------
       
   366 HBufC* CPostcardContact::GetNameLC() const
       
   367     {
       
   368     // GetNameL() returns NULL if the contact has no name defined
       
   369     HBufC* nameText = iContactMatcher->GetNameL(iStoreContact->Fields());
       
   370     if (nameText)
       
   371         {
       
   372         CleanupStack::PushL(nameText);        
       
   373         }
       
   374     else
       
   375         {
       
   376         nameText = HBufC::NewLC(0);
       
   377         }
       
   378     return nameText;
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------
       
   382 // GetContactFieldLC
       
   383 // ---------------------------------------------------------
       
   384 HBufC* CPostcardContact::GetContactFieldLC(TVPbkFieldTypeName aFieldType,
       
   385     TVPbkSubFieldType aSubFieldType,
       
   386     TVPbkFieldTypeParameter aFieldTypeParameter) const
       
   387     {
       
   388     TVPbkFieldVersitProperty prop;
       
   389     
       
   390     prop.SetName(aFieldType);
       
   391     prop.SetSubField(aSubFieldType);
       
   392 
       
   393     TVPbkFieldTypeParameters fieldTypeParameters;
       
   394     fieldTypeParameters.Add(aFieldTypeParameter);
       
   395     prop.SetParameters(fieldTypeParameters);
       
   396 
       
   397     // Doesn't take ownership
       
   398     const MVPbkFieldType* fieldType =
       
   399         iContactMatcher->FieldTypes().FindMatch(prop, 0);
       
   400 
       
   401     if (fieldType)
       
   402         {
       
   403 	    TPtrC src =
       
   404             iContactMatcher->GetFieldDataTextL(*iStoreContact, *fieldType);
       
   405         HBufC *data = HBufC::NewLC(src.Length());
       
   406         *data = src;
       
   407         data->Des().Trim();
       
   408         return data;
       
   409         }
       
   410     else
       
   411         {
       
   412         return HBufC::NewLC(0);
       
   413         }
       
   414     }
       
   415 
       
   416 // ---------------------------------------------------------
       
   417 // AknInputBlockCancel
       
   418 // ---------------------------------------------------------
       
   419 void CPostcardContact::AknInputBlockCancel()
       
   420     {
       
   421     if ( iActSchedWait.IsStarted() )
       
   422         {
       
   423         // Cancels currently outstanding AIW request to phonebook.
       
   424         TRAPD( error, iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdSelect, 
       
   425                                                    iAiwServiceHandler->InParamListL(), 
       
   426                                                    iAiwServiceHandler->OutParamListL(),
       
   427                                                    KAiwOptCancel,
       
   428                                                    this ) );
       
   429         if ( error == KErrNone )
       
   430             {
       
   431             iShutdown = ETrue;
       
   432             }
       
   433         }
       
   434     }
       
   435 
       
   436 // End of File