phonebookengines/VirtualPhonebook/VPbkSimStore/src/CViewContact.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002-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:  A virtual phonebook view contact implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CViewContact.h"
       
    22 
       
    23 #include <MVPbkFieldType.h>
       
    24 #include <CVPbkSimCntField.h>
       
    25 #include <CVPbkSimContact.h>
       
    26 #include "CContactStore.h"
       
    27 #include "CFieldTypeMappings.h"
       
    28 #include "CContactView.h"
       
    29 #include "CContact.h"
       
    30 #include "CContactOperationCallback.h"
       
    31 #include "CRemoteStore.h"
       
    32 #include "VPbkSimStoreError.h"
       
    33 
       
    34 namespace VPbkSimStore {
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // TViewContactFieldData::TViewContactFieldData
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 TViewContactFieldData::TViewContactFieldData( const TDesC& aData )
       
    45 :   iData( aData )
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // TViewContactFieldData::IsEmpty
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 TBool TViewContactFieldData::IsEmpty() const
       
    54     {
       
    55     return iData.Length() == 0;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // TViewContactFieldData::CopyL
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void TViewContactFieldData::CopyL( const MVPbkContactFieldData& /*aFieldData*/ )
       
    63     {
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // TViewContactFieldData::Text
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 TPtrC TViewContactFieldData::Text() const
       
    71     {
       
    72     return iData;
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // TViewContactFieldData::SetTextL
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void TViewContactFieldData::SetTextL( const TDesC& aText )
       
    80     {
       
    81     iData.Set( aText );
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // TViewContactFieldData::SetTextL
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 TInt TViewContactFieldData::MaxLength() const
       
    89     {
       
    90     return iData.Length();
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // TViewContactField::TViewContactField
       
    95 // C++ default constructor can NOT contain any code, that
       
    96 // might leave.
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 TViewContactField::TViewContactField( CViewContact& aParentContact,
       
   100     const MVPbkFieldType& aFieldType, const TDesC& aData )
       
   101 :   iParentContact( aParentContact ),
       
   102     iFieldType( aFieldType ),
       
   103     iFieldData( aData )
       
   104     {
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // TViewContactField::ParentContact
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 MVPbkBaseContact& TViewContactField::ParentContact() const
       
   112     {
       
   113     return iParentContact;
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // TViewContactField::MatchFieldType
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 const MVPbkFieldType* TViewContactField::MatchFieldType( 
       
   121     TInt /*aMatchPriority*/ ) const
       
   122     {
       
   123     return &iFieldType;
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // TViewContactField::BestMatchingFieldType
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 const MVPbkFieldType* TViewContactField::BestMatchingFieldType() const
       
   131     {
       
   132     return &iFieldType;
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // TViewContactField::ConstFieldData
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 const MVPbkContactFieldData& TViewContactField::FieldData() const
       
   140     {
       
   141     return iFieldData;
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // TViewContactField::IsSame
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 TBool TViewContactField::IsSame( 
       
   149     const MVPbkBaseContactField& aOther ) const
       
   150     {
       
   151     if ( &aOther.ParentContact() == &ParentContact() &&
       
   152          &aOther.FieldData() == &FieldData() )
       
   153         {
       
   154         return ETrue;
       
   155         }
       
   156     return EFalse;
       
   157     }
       
   158 
       
   159 
       
   160 // Destructor
       
   161 CViewContactFieldCollection::~CViewContactFieldCollection()
       
   162     {
       
   163     iFields.Close();
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CViewContactFieldCollection::SetParentContact
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CViewContactFieldCollection::SetParentContact( 
       
   171     CViewContact& aParentContact )
       
   172     {
       
   173     iParentContact = &aParentContact;
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CViewContactFieldCollection::ResetFields
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CViewContactFieldCollection::ResetFields()
       
   181     {
       
   182     iFields.Reset();
       
   183     }
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CViewContactFieldCollection::AppendFieldL
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CViewContactFieldCollection::AppendFieldL( TViewContactField& aNewField )
       
   190     {
       
   191     iFields.AppendL( aNewField );
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // CViewContactFieldCollection::ParentContact
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 MVPbkBaseContact& CViewContactFieldCollection::ParentContact() const
       
   199     {
       
   200     return *iParentContact;
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CViewContactFieldCollection::FieldCount
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 TInt CViewContactFieldCollection::FieldCount() const
       
   208     {
       
   209     return iFields.Count();
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CViewContactFieldCollection::FieldAt
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 const MVPbkBaseContactField& CViewContactFieldCollection::FieldAt( 
       
   217     TInt aIndex ) const
       
   218     {
       
   219     return iFields[aIndex];
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CViewContact::CViewContact
       
   224 // C++ default constructor can NOT contain any code, that
       
   225 // might leave.
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 CViewContact::CViewContact( CContactView& aView, 
       
   229     const MVPbkFieldTypeList& aSortOrder )
       
   230     :   iView( aView ),
       
   231         iSortOrder( &aSortOrder )
       
   232     {
       
   233     }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // CViewContact::ConstructL
       
   237 // Symbian 2nd phase constructor can leave.
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void CViewContact::ConstructL()
       
   241     {
       
   242     iAsyncOp = new( ELeave ) VPbkEngUtils::CVPbkAsyncOperation;
       
   243     iFieldCollection.SetParentContact( *this );
       
   244     }
       
   245     
       
   246 // -----------------------------------------------------------------------------
       
   247 // CViewContact::NewL
       
   248 // Two-phased constructor.
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 CViewContact* CViewContact::NewL( CContactView& aView,
       
   252     const MVPbkFieldTypeList& aSortOrder )
       
   253     {
       
   254     CViewContact* self = new( ELeave ) CViewContact( aView, aSortOrder );
       
   255     CleanupStack::PushL( self );
       
   256     self->ConstructL();
       
   257     CleanupStack::Pop(self);
       
   258     return self;
       
   259     }
       
   260 
       
   261 // Destructor
       
   262 CViewContact::~CViewContact()
       
   263     {
       
   264     delete iStoreOperation;
       
   265     delete iAsyncOp;    
       
   266     }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CViewContact::SetSimContactL
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CViewContact::SetSimContactL( MVPbkSimContact& aSimContact )
       
   273     {
       
   274     // View contact must have the same fields as in the sort order that was
       
   275     // given by VPbk client.
       
   276     iFieldCollection.ResetFields();
       
   277     CFieldTypeMappings& mappings = iView.Store().FieldTypeMappings();
       
   278     TInt count = aSimContact.FieldCount();
       
   279     TInt typeCount = iSortOrder->FieldTypeCount();
       
   280     // Loop all field types in the sort order and try to find fields
       
   281     // from the sim contact.
       
   282     for ( TInt i = 0; i < typeCount; ++i )
       
   283         {
       
   284         const CVPbkSimCntField* simField = NULL;
       
   285         const MVPbkFieldType& sortOrderType = iSortOrder->FieldTypeAt( i );
       
   286         for ( TInt j = 0; j < count; ++j )
       
   287             {
       
   288             const MVPbkFieldType* vpbkType = mappings.Match( *iSortOrder, 
       
   289                 aSimContact.ConstFieldAt( j ).Type() );
       
   290             if ( vpbkType && vpbkType == &sortOrderType )
       
   291                 {
       
   292                 simField = &aSimContact.ConstFieldAt( j );
       
   293                 break;
       
   294                 }
       
   295             }
       
   296 
       
   297         if ( simField )
       
   298             {
       
   299             // SIM field was found for the sort order type
       
   300             TViewContactField field( *this, sortOrderType, simField->Data() );
       
   301             iFieldCollection.AppendFieldL( field );
       
   302             }
       
   303         else
       
   304             {
       
   305             // SIM field was NOT found for the sort order type. Set empty data,
       
   306             TViewContactField field( *this, sortOrderType, KNullDesC );
       
   307             iFieldCollection.AppendFieldL( field );
       
   308             }
       
   309         }
       
   310 
       
   311     iSimContact = &aSimContact;
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CViewContact::SimIndex
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 TInt CViewContact::SimIndex() const
       
   319     {
       
   320     if ( iSimContact )
       
   321         {
       
   322         return iSimContact->SimIndex();
       
   323         }
       
   324     return KErrNotFound;
       
   325     }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CViewContact::SetSortOrder
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 void CViewContact::SetSortOrder( const MVPbkFieldTypeList& aSortOrder )
       
   332     {
       
   333     iSortOrder = &aSortOrder;
       
   334     }
       
   335 
       
   336 // -----------------------------------------------------------------------------
       
   337 // CViewContact::NativeContact
       
   338 // -----------------------------------------------------------------------------
       
   339 //
       
   340 const MVPbkSimContact* CViewContact::NativeContact() const
       
   341     {
       
   342     return iSimContact;
       
   343     }
       
   344     
       
   345 // -----------------------------------------------------------------------------
       
   346 // CViewContact::View
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 CContactView& CViewContact::View() const
       
   350     {
       
   351     return iView;
       
   352     }
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CViewContact::Fields
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 const MVPbkBaseContactFieldCollection& CViewContact::Fields() const
       
   359     {
       
   360     return iFieldCollection;
       
   361     }
       
   362 
       
   363 // -----------------------------------------------------------------------------
       
   364 // CViewContact::IsSame
       
   365 // -----------------------------------------------------------------------------
       
   366 //
       
   367 TBool CViewContact::IsSame( const MVPbkStoreContact& aOtherContact ) const
       
   368     {
       
   369     if ( &iView.ContactStore() == &aOtherContact.ContactStore() )
       
   370         {
       
   371         const CVPbkSimContact& simContact = 
       
   372             static_cast<const CContact&>( aOtherContact ).SimContact();
       
   373         return SimIndex() == simContact.SimIndex();
       
   374         }
       
   375     return EFalse;
       
   376     }
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CViewContact::IsSame
       
   380 // -----------------------------------------------------------------------------
       
   381 //
       
   382 TBool CViewContact::IsSame( const MVPbkViewContact& aOtherContact ) const
       
   383     {
       
   384     return aOtherContact.IsSame(*this, &iView.ContactStore());
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CViewContact::IsSame
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 TBool CViewContact::IsSame(const MVPbkViewContact& aOtherContact, 
       
   392                            const MVPbkContactStore* aContactStore) const
       
   393     {
       
   394     if (aContactStore == &iView.ContactStore())
       
   395         {
       
   396         return static_cast<const CViewContact&>(aOtherContact).SimIndex() == 
       
   397             SimIndex();
       
   398         }
       
   399     return EFalse;
       
   400     }
       
   401 
       
   402 // -----------------------------------------------------------------------------
       
   403 // CViewContact::IsSame
       
   404 // -----------------------------------------------------------------------------
       
   405 //
       
   406 TBool CViewContact::IsSame(const MVPbkStoreContact& aOtherContact, 
       
   407                            const MVPbkContactStore* aContactStore) const
       
   408     {
       
   409     if (aContactStore == &iView.ContactStore())
       
   410         {
       
   411         const CVPbkSimContact& simContact = 
       
   412             static_cast<const CContact&>( aOtherContact ).SimContact();
       
   413         return SimIndex() == simContact.SimIndex();
       
   414         }
       
   415     return EFalse;
       
   416     }
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CViewContact::CreateLinkLC
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 MVPbkContactLink* CViewContact::CreateLinkLC() const
       
   423     {
       
   424     return iView.Store().CreateLinkLC( SimIndex() );
       
   425     }
       
   426 
       
   427 // -----------------------------------------------------------------------------
       
   428 // CViewContact::DeleteL
       
   429 // -----------------------------------------------------------------------------
       
   430 //
       
   431 void CViewContact::DeleteL( MVPbkContactObserver& aObserver ) const
       
   432     {
       
   433     if ( iStoreOperation )
       
   434         {
       
   435         User::Leave( KErrInUse );
       
   436         }
       
   437         
       
   438     // From the client point of view the MVPbkStoreContact is constant but
       
   439     // implementation needs a non const contact.
       
   440     RVPbkStreamedIntArray indexArray;
       
   441     CleanupClosePushL( indexArray );
       
   442     indexArray.AppendIntL( SimIndex() );
       
   443     iStoreOperation = iView.Store().NativeStore().DeleteL( indexArray, 
       
   444         const_cast<CViewContact&>(*this));
       
   445     iObserver = &aObserver;
       
   446     CleanupStack::PopAndDestroy(); // indexArray 
       
   447     }
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CViewContact::MatchContactStore
       
   451 // -----------------------------------------------------------------------------
       
   452 //        
       
   453 TBool CViewContact::MatchContactStore(const TDesC& aContactStoreUri) const
       
   454     {
       
   455     return iView.MatchContactStore(aContactStoreUri);
       
   456     }
       
   457 
       
   458 // -----------------------------------------------------------------------------
       
   459 // CViewContact::MatchContactStoreDomain
       
   460 // -----------------------------------------------------------------------------
       
   461 //        
       
   462 TBool CViewContact::MatchContactStoreDomain(
       
   463         const TDesC& aContactStoreDomain) const
       
   464     {
       
   465     return iView.MatchContactStoreDomain(aContactStoreDomain);
       
   466     }
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CViewContact::CreateBookmarkLC
       
   470 // -----------------------------------------------------------------------------
       
   471 //
       
   472 MVPbkContactBookmark* CViewContact::CreateBookmarkLC() const
       
   473     {
       
   474     return iView.Store().CreateBookmarkLC( SimIndex() );
       
   475     }
       
   476     
       
   477 // -----------------------------------------------------------------------------
       
   478 // CViewContact::ParentView
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 MVPbkContactViewBase& CViewContact::ParentView() const
       
   482     {
       
   483     return iView;
       
   484     }
       
   485 
       
   486 // -----------------------------------------------------------------------------
       
   487 // CViewContact::ParentView
       
   488 // -----------------------------------------------------------------------------
       
   489 //
       
   490 void CViewContact::ReadL( MVPbkContactObserver& aObserver ) const
       
   491     {
       
   492     MVPbkContactObserver::TContactOpResult opResult;
       
   493     opResult.iExtension = NULL;
       
   494     opResult.iOpCode = MVPbkContactObserver::EContactRead;
       
   495     opResult.iStoreContact = iView.Store().ReadContactL( SimIndex() );
       
   496     
       
   497     CreateReadCallbackL( aObserver, opResult );
       
   498     }
       
   499 
       
   500 // -----------------------------------------------------------------------------
       
   501 // CViewContact::ReadAndLockL
       
   502 // -----------------------------------------------------------------------------
       
   503 //
       
   504 void CViewContact::ReadAndLockL( MVPbkContactObserver& aObserver ) const
       
   505     {
       
   506     MVPbkContactObserver::TContactOpResult opResult;
       
   507     opResult.iExtension = NULL;
       
   508     opResult.iOpCode = MVPbkContactObserver::EContactReadAndLock;
       
   509     CContact* cnt = iView.Store().ReadContactL( SimIndex() );
       
   510     cnt->SetLock( ETrue );
       
   511     opResult.iStoreContact = cnt;
       
   512     
       
   513     CreateReadCallbackL( aObserver, opResult );
       
   514     }
       
   515 
       
   516 // -----------------------------------------------------------------------------
       
   517 // CViewContact::Expandable
       
   518 // -----------------------------------------------------------------------------
       
   519 //
       
   520 MVPbkExpandable* CViewContact::Expandable() const
       
   521     {
       
   522     return NULL;
       
   523     }
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // CViewContact::ContactEventComplete
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 void CViewContact::ContactEventComplete( TEvent /*aEvent*/, 
       
   530     CVPbkSimContact* /*aContact*/ )
       
   531     {
       
   532     __ASSERT_DEBUG( iObserver, 
       
   533         Panic(EPreCond_CViewContact_ContactEventComplete));
       
   534     
       
   535     delete iStoreOperation;
       
   536     iStoreOperation = NULL;
       
   537     
       
   538     MVPbkContactObserver::TContactOpResult vpbkOpResult;
       
   539     vpbkOpResult.iStoreContact = NULL;
       
   540     vpbkOpResult.iExtension = NULL;
       
   541     vpbkOpResult.iOpCode = MVPbkContactObserver::EContactDelete;
       
   542     
       
   543     MVPbkContactObserver* observer = iObserver;
       
   544     iObserver = NULL;
       
   545     observer->ContactOperationCompleted( vpbkOpResult );
       
   546     }
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CViewContact::ContactEventError
       
   550 // -----------------------------------------------------------------------------
       
   551 //
       
   552 void CViewContact::ContactEventError( TEvent /*aEvent*/, 
       
   553     CVPbkSimContact* /*aContact*/, TInt aError )
       
   554     {
       
   555     __ASSERT_DEBUG( iObserver, Panic(EPreCond_CViewContact_ContactEventError));
       
   556     
       
   557     delete iStoreOperation;
       
   558     iStoreOperation = NULL;
       
   559     
       
   560     MVPbkContactObserver* observer = iObserver;
       
   561     iObserver = NULL;
       
   562     MVPbkContactObserver::TContactOp op = MVPbkContactObserver::EContactDelete;
       
   563     observer->ContactOperationFailed( op, aError, EFalse );
       
   564     } 
       
   565 
       
   566 // -----------------------------------------------------------------------------
       
   567 // CViewContact::CreateReadCallbackL
       
   568 // -----------------------------------------------------------------------------
       
   569 //
       
   570 void CViewContact::CreateReadCallbackL( MVPbkContactObserver& aObserver,
       
   571     MVPbkContactObserver::TContactOpResult& aOpResult ) const
       
   572     {
       
   573     CleanupDeletePushL( aOpResult.iStoreContact );
       
   574     CContactOperationCallback* callBack = 
       
   575         new( ELeave ) CContactOperationCallback( aOpResult, aObserver, 
       
   576         KErrNone );
       
   577     CleanupStack::Pop( aOpResult.iStoreContact );
       
   578     CleanupStack::PushL( callBack );
       
   579     iAsyncOp->CallbackL( callBack );
       
   580     CleanupStack::Pop( callBack );
       
   581     }
       
   582 } // namespace VPbkSimStore
       
   583 //  End of File