phonebookui/Phonebook2/Presentation/src/CPbk2PresentationContact.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
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 phonebook2 presentation level contact
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2PresentationContact.h"
       
    20 
       
    21 // Phonebook 2
       
    22 #include <CPbk2PresentationContactField.h>
       
    23 #include <CPbk2PresentationContactFieldCollection.h>
       
    24 #include <MPbk2FieldProperty.h>
       
    25 #include <MPbk2FieldProperty2.h>
       
    26 #include <CPbk2FieldPropertyArray.h>
       
    27 #include <Phonebook2PrivateCRKeys.h>
       
    28 
       
    29 // Virtual Phonebook
       
    30 #include <MVPbkFieldType.h>
       
    31 #include <MVPbkContactStore.h>
       
    32 #include <MVPbkContactStoreProperties.h>
       
    33 #include <MVPbkContactFieldUriData.h>
       
    34 #include <MVPbkStoreContact2.h>
       
    35 #include <TVPbkFieldVersitProperty.h>
       
    36 #include <VPbkVariant.hrh>
       
    37 #include <VPbkEng.rsg>
       
    38 
       
    39 // System includes
       
    40 #include <centralrepository.h>
       
    41 
       
    42 /// Unnamed namespace for local definitions
       
    43 namespace {
       
    44     // This is done locally here because we are not dependent from CommonUI
       
    45     TBool LocalVariationFeatureEnabled( TInt aFeatureFlag )
       
    46         {
       
    47         TInt lvFlags( KErrNotFound );
       
    48         TBool ret( EFalse );
       
    49 
       
    50         CRepository* key = NULL;
       
    51         // Read local variation flags
       
    52         TRAPD( err, key =
       
    53                 CRepository::NewL(TUid::Uid(KCRUidPhonebook)));
       
    54         // if NewL fails do not set iLVFlags, new query is made next time
       
    55         if ( err == KErrNone )
       
    56             {
       
    57             err = key->Get(KPhonebookLocalVariationFlags, lvFlags);
       
    58             if (err != KErrNone)
       
    59                 {
       
    60                 // If there were problems reading the flags,
       
    61                 // assume everything is off
       
    62                 lvFlags = 0;
       
    63                 }
       
    64             delete key;
       
    65             }
       
    66 
       
    67         if ( lvFlags != KErrNotFound )
       
    68             {
       
    69             ret = lvFlags & aFeatureFlag;
       
    70             }
       
    71 
       
    72         return ret;
       
    73         }
       
    74 
       
    75     TBool IsVariatedLocallyL( const MPbk2FieldProperty& aProperty )
       
    76         {
       
    77         // Check if "pager" field should be part of the template,
       
    78         // USA flagged feature
       
    79         // Read local variation flag for pager field
       
    80         TBool useInTemplate ( ETrue );
       
    81         if ( aProperty.FieldType().FieldTypeResId() ==
       
    82                     R_VPBK_FIELD_TYPE_PAGERNUMBER )
       
    83             {
       
    84             useInTemplate =
       
    85                     LocalVariationFeatureEnabled( EVPbkLVPagerInTemplate );
       
    86             }
       
    87 
       
    88         // Returns ETrue if field with aProperty should be added to contact.
       
    89         return useInTemplate;
       
    90         }
       
    91 
       
    92 } /// namespace
       
    93 
       
    94 
       
    95 // --------------------------------------------------------------------------
       
    96 // CPbk2PresentationContact::CPbk2PresentationContact
       
    97 // C++ default constructor can NOT contain any code, that
       
    98 // might leave.
       
    99 // --------------------------------------------------------------------------
       
   100 //
       
   101 CPbk2PresentationContact::CPbk2PresentationContact(
       
   102     MVPbkStoreContact& aStoreContact,
       
   103     const MPbk2FieldPropertyArray& aFieldProperties)
       
   104     :   iStoreContact(aStoreContact),
       
   105         iFieldProperties(aFieldProperties)
       
   106     {
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CPbk2PresentationContact::ConstructL
       
   111 // Symbian 2nd phase constructor can leave.
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 void CPbk2PresentationContact::ConstructL()
       
   115     {
       
   116     iCollection = CPbk2PresentationContactFieldCollection::NewL(
       
   117         iFieldProperties, iStoreContact.Fields(), *this);
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CPbk2PresentationContact::NewL
       
   122 // Two-phased constructor.
       
   123 // --------------------------------------------------------------------------
       
   124 //
       
   125 EXPORT_C CPbk2PresentationContact* CPbk2PresentationContact::NewL(
       
   126     MVPbkStoreContact& aStoreContact,
       
   127     const MPbk2FieldPropertyArray& aFieldProperties)
       
   128     {
       
   129     CPbk2PresentationContact* self =
       
   130         new( ELeave ) CPbk2PresentationContact(aStoreContact, aFieldProperties);
       
   131     CleanupStack::PushL( self );
       
   132     self->ConstructL();
       
   133     CleanupStack::Pop( self );
       
   134     return self;
       
   135     }
       
   136 
       
   137 
       
   138 // Destructor
       
   139 CPbk2PresentationContact::~CPbk2PresentationContact()
       
   140     {
       
   141     delete iCollection;
       
   142     }
       
   143 
       
   144 // --------------------------------------------------------------------------
       
   145 // CPbk2PresentationContact::IsFieldAdditionPossibleL
       
   146 // --------------------------------------------------------------------------
       
   147 //
       
   148 EXPORT_C TBool CPbk2PresentationContact::IsFieldAdditionPossibleL(
       
   149         const MVPbkFieldType& aType) const
       
   150     {
       
   151     TInt max = MaxNumberOfFieldL(aType);
       
   152     if (max == KVPbkStoreContactUnlimitedNumber)
       
   153         {
       
   154         return ETrue;
       
   155         }
       
   156     else
       
   157         {
       
   158         TInt cur = CurrentAmountOfFieldTypeInContact(aType, KNullDesC);
       
   159         return cur < max;
       
   160         }
       
   161     }
       
   162 
       
   163 // --------------------------------------------------------------------------
       
   164 // CPbk2PresentationContact::AddSupportedTemplateFieldsL
       
   165 // --------------------------------------------------------------------------
       
   166 //
       
   167 EXPORT_C void CPbk2PresentationContact::AddSupportedTemplateFieldsL()
       
   168     {
       
   169     // Adds fields that belongs to the template according to UI specification.
       
   170     // In addition checks that the store supports the field and that
       
   171     // there isn't already a same type of field in the contact.
       
   172     // Also locally variated features are checked.
       
   173     const MVPbkFieldTypeList& supportedTypes =
       
   174         iStoreContact.ParentStore().StoreProperties().SupportedFields();
       
   175     const TInt count = iFieldProperties.Count();
       
   176     for (TInt i = 0; i < count; ++i)
       
   177         {
       
   178         const MPbk2FieldProperty& prop = iFieldProperties.At( i );
       
   179         
       
   180         MPbk2FieldProperty2* fieldPropertyExtension =
       
   181             reinterpret_cast<MPbk2FieldProperty2*>(
       
   182                 const_cast<MPbk2FieldProperty&>( prop ).
       
   183                     FieldPropertyExtension(
       
   184                         KMPbk2FieldPropertyExtension2Uid ) );
       
   185         
       
   186         if ( fieldPropertyExtension != NULL ) 
       
   187             {
       
   188             if ( prop.Flags() & KPbk2FieldFlagTemplateField &&
       
   189                 supportedTypes.ContainsSame( prop.FieldType() ) &&
       
   190                 CurrentAmountOfFieldTypeInContact( prop.FieldType(),
       
   191                     fieldPropertyExtension->XSpName() ) == 0  &&
       
   192                 IsVariatedLocallyL( prop ) )
       
   193                 {
       
   194                 AddFieldL( CreateFieldLC( prop.FieldType() ),
       
   195                     fieldPropertyExtension->XSpName() );
       
   196                 CleanupStack::Pop(); // new field
       
   197                 }
       
   198             }
       
   199         }
       
   200     }
       
   201 // --------------------------------------------------------------------------
       
   202 // CPbk2PresentationContact::AvailableFieldsToAddL
       
   203 // --------------------------------------------------------------------------
       
   204 //
       
   205 EXPORT_C CArrayPtr<const MPbk2FieldProperty>*
       
   206     CPbk2PresentationContact::AvailableFieldsToAddL() const
       
   207     {
       
   208     const TInt count = iFieldProperties.Count();
       
   209     CArrayPtrFlat<const MPbk2FieldProperty>* propArray =
       
   210         new(ELeave) CArrayPtrFlat<const MPbk2FieldProperty>(count);
       
   211     CleanupStack::PushL(propArray);
       
   212 
       
   213     for (TInt i = 0; i < count; ++i)
       
   214         {
       
   215         const MPbk2FieldProperty& prop = iFieldProperties.At(i);
       
   216         if (IsFieldAdditionPossibleL(prop.FieldType()) &&
       
   217             prop.Flags() & KPbk2FieldFlagUserCanAddField)
       
   218             {
       
   219             propArray->AppendL(&prop);
       
   220             }
       
   221         }
       
   222 
       
   223     propArray->Compress();
       
   224     CleanupStack::Pop(propArray);
       
   225     return propArray;
       
   226     }
       
   227 
       
   228 // --------------------------------------------------------------------------
       
   229 // CPbk2PresentationContact::ParentObject
       
   230 // --------------------------------------------------------------------------
       
   231 //
       
   232 MVPbkObjectHierarchy& CPbk2PresentationContact::ParentObject() const
       
   233     {
       
   234     return iStoreContact.ParentObject();
       
   235     }
       
   236 
       
   237 // --------------------------------------------------------------------------
       
   238 // CPbk2PresentationContact::Fields
       
   239 // --------------------------------------------------------------------------
       
   240 //
       
   241 const MVPbkStoreContactFieldCollection& CPbk2PresentationContact::Fields() const
       
   242     {
       
   243     return *iCollection;
       
   244     }
       
   245 
       
   246 // --------------------------------------------------------------------------
       
   247 // CPbk2PresentationContact::IsSame
       
   248 // --------------------------------------------------------------------------
       
   249 //
       
   250 TBool CPbk2PresentationContact::IsSame(
       
   251     const MVPbkStoreContact& aOtherContact) const
       
   252     {
       
   253     const CPbk2PresentationContact* cnt =
       
   254         dynamic_cast<const CPbk2PresentationContact*>(&aOtherContact);
       
   255     if (cnt)
       
   256         {
       
   257         return iStoreContact.IsSame(cnt->iStoreContact);
       
   258         }
       
   259     return iStoreContact.IsSame(aOtherContact);
       
   260     }
       
   261 
       
   262 // --------------------------------------------------------------------------
       
   263 // CPbk2PresentationContact::CreateLinkLC
       
   264 // --------------------------------------------------------------------------
       
   265 //
       
   266 MVPbkContactLink* CPbk2PresentationContact::CreateLinkLC() const
       
   267     {
       
   268     return iStoreContact.CreateLinkLC();
       
   269     }
       
   270 
       
   271 // --------------------------------------------------------------------------
       
   272 // CPbk2PresentationContact::DeleteL
       
   273 // --------------------------------------------------------------------------
       
   274 //
       
   275 void CPbk2PresentationContact::DeleteL(MVPbkContactObserver& aObserver) const
       
   276     {
       
   277     iStoreContact.DeleteL(aObserver);
       
   278     }
       
   279 
       
   280 // --------------------------------------------------------------------------
       
   281 // CPbk2PresentationContact::Close
       
   282 // Does not destroy iStoreContact, since it is not owned
       
   283 // --------------------------------------------------------------------------
       
   284 //
       
   285 void CPbk2PresentationContact::Close() const
       
   286     {
       
   287     delete this;
       
   288     }
       
   289 
       
   290 // --------------------------------------------------------------------------
       
   291 // CPbk2PresentationContact::ParentStore
       
   292 // --------------------------------------------------------------------------
       
   293 //
       
   294 MVPbkContactStore& CPbk2PresentationContact::ParentStore() const
       
   295     {
       
   296     return iStoreContact.ParentStore();
       
   297     }
       
   298 
       
   299 // --------------------------------------------------------------------------
       
   300 // CPbk2PresentationContact::Fields
       
   301 // --------------------------------------------------------------------------
       
   302 //
       
   303 MVPbkStoreContactFieldCollection& CPbk2PresentationContact::Fields()
       
   304     {
       
   305     return *iCollection;
       
   306     }
       
   307 
       
   308 // --------------------------------------------------------------------------
       
   309 // CPbk2PresentationContact::CreateFieldLC
       
   310 // --------------------------------------------------------------------------
       
   311 //
       
   312 MVPbkStoreContactField* CPbk2PresentationContact::CreateFieldLC(
       
   313     const MVPbkFieldType& aFieldType) const
       
   314     {
       
   315     return iStoreContact.CreateFieldLC(aFieldType);
       
   316     }
       
   317 
       
   318 // --------------------------------------------------------------------------
       
   319 // CPbk2PresentationContact::AddFieldL
       
   320 // --------------------------------------------------------------------------
       
   321 //
       
   322 TInt CPbk2PresentationContact::AddFieldL(MVPbkStoreContactField* aField)
       
   323     {
       
   324     // The aField instance can not be used after AddFieldL
       
   325     TInt index = iStoreContact.AddFieldL(aField);
       
   326     MVPbkStoreContactFieldCollection& fields = iStoreContact.Fields();
       
   327     // Create a presistent store field for the mapping field
       
   328     const MVPbkStoreContactField& field = fields.FieldAt(index);
       
   329     TRAPD(res, iCollection->AddFieldMappingL(field, KNullDesC));
       
   330 
       
   331     if (res != KErrNone)
       
   332         {
       
   333         // If creation of mapping field failed, remove also the field
       
   334         // from the store
       
   335         iStoreContact.RemoveField(index);
       
   336         User::Leave(res);
       
   337         }
       
   338 
       
   339     return iCollection->FindFieldIndex(field);
       
   340     }
       
   341 
       
   342 // --------------------------------------------------------------------------
       
   343 // CPbk2PresentationContact::RemoveField
       
   344 // --------------------------------------------------------------------------
       
   345 //
       
   346 void CPbk2PresentationContact::RemoveField(TInt aIndex)
       
   347     {
       
   348     TInt storeIndex = iCollection->StoreIndexOfField(aIndex);
       
   349     iStoreContact.RemoveField(storeIndex);
       
   350     iCollection->RemoveFieldMapping(aIndex);
       
   351     }
       
   352 
       
   353 // --------------------------------------------------------------------------
       
   354 // CPbk2PresentationContact::RemoveAllFields
       
   355 // --------------------------------------------------------------------------
       
   356 //
       
   357 void CPbk2PresentationContact::RemoveAllFields()
       
   358     {
       
   359     iCollection->ResetFieldMappings();
       
   360     iStoreContact.RemoveAllFields();
       
   361     }
       
   362 
       
   363 // --------------------------------------------------------------------------
       
   364 // CPbk2PresentationContact::LockL
       
   365 // --------------------------------------------------------------------------
       
   366 //
       
   367 void CPbk2PresentationContact::LockL(MVPbkContactObserver& aObserver) const
       
   368     {
       
   369     iStoreContact.LockL(aObserver);
       
   370     }
       
   371 
       
   372 // --------------------------------------------------------------------------
       
   373 // CPbk2PresentationContact::CommitL
       
   374 // --------------------------------------------------------------------------
       
   375 //
       
   376 void CPbk2PresentationContact::CommitL(MVPbkContactObserver& aObserver) const
       
   377     {
       
   378     iStoreContact.CommitL(aObserver);
       
   379     }
       
   380 
       
   381 // --------------------------------------------------------------------------
       
   382 // CPbk2PresentationContact::GroupsJoinedLC
       
   383 // --------------------------------------------------------------------------
       
   384 //
       
   385 MVPbkContactLinkArray* CPbk2PresentationContact::GroupsJoinedLC() const
       
   386     {
       
   387     return iStoreContact.GroupsJoinedLC();
       
   388     }
       
   389 
       
   390 // --------------------------------------------------------------------------
       
   391 // CPbk2PresentationContact::Group
       
   392 // --------------------------------------------------------------------------
       
   393 //
       
   394 MVPbkContactGroup* CPbk2PresentationContact::Group()
       
   395     {
       
   396     return iStoreContact.Group();
       
   397     }
       
   398 
       
   399 // --------------------------------------------------------------------------
       
   400 // CPbk2PresentationContact::MaxNumberOfFieldL
       
   401 // In addition of checking the store maximum MaxNumberOfFieldL()
       
   402 // checks the multiplicity defined in the field property for
       
   403 // the given field type.
       
   404 // --------------------------------------------------------------------------
       
   405 //
       
   406 TInt CPbk2PresentationContact::MaxNumberOfFieldL(
       
   407         const MVPbkFieldType& aType ) const
       
   408     {
       
   409     // Get the store limits
       
   410     TInt res = iStoreContact.MaxNumberOfFieldL(aType);
       
   411 
       
   412     // If store has no limits or the number is bigger than one
       
   413     // -> check the UI limits
       
   414     const TInt oneFieldInContact = 1;
       
   415     if (res > oneFieldInContact || res == KVPbkStoreContactUnlimitedNumber)
       
   416         {
       
   417         const MPbk2FieldProperty* prop = iFieldProperties.FindProperty(aType);
       
   418         if (prop && prop->Multiplicity() == EPbk2FieldMultiplicityOne)
       
   419             {
       
   420             res = oneFieldInContact;
       
   421             }
       
   422         }
       
   423     return res;
       
   424     }
       
   425 
       
   426 
       
   427 // --------------------------------------------------------------------------
       
   428 // CPbk2PresentationContact::StoreContactExtension
       
   429 // --------------------------------------------------------------------------
       
   430 //
       
   431 TAny* CPbk2PresentationContact::StoreContactExtension(TUid aExtensionUid)
       
   432 	{
       
   433 	
       
   434 	if( aExtensionUid == KMVPbkStoreContactExtension2Uid )
       
   435 		return static_cast<MVPbkStoreContact2*>( this );
       
   436 	
       
   437 	return NULL;
       
   438 	}
       
   439 
       
   440 // CPbk2PresentationContact::PropertiesLC
       
   441 // --------------------------------------------------------------------------
       
   442 //
       
   443 MVPbkStoreContactProperties* CPbk2PresentationContact::PropertiesL() const
       
   444 	{
       
   445 	MVPbkStoreContact2* tempContact = 
       
   446 	    reinterpret_cast<MVPbkStoreContact2*>
       
   447 	        (iStoreContact.StoreContactExtension
       
   448 	            (KMVPbkStoreContactExtension2Uid));
       
   449 	
       
   450 	return tempContact->PropertiesL();
       
   451 	}
       
   452 
       
   453 // --------------------------------------------------------------------------
       
   454 // CPbk2PresentationContact::SetAsOwnL
       
   455 // --------------------------------------------------------------------------
       
   456 //
       
   457 void CPbk2PresentationContact::SetAsOwnL(
       
   458 		MVPbkContactObserver& aObserver) const
       
   459 	{
       
   460 	MVPbkStoreContact2* tempContact = 
       
   461 	    reinterpret_cast<MVPbkStoreContact2*>
       
   462 	        (iStoreContact.StoreContactExtension
       
   463 	            (KMVPbkStoreContactExtension2Uid));
       
   464 
       
   465 	tempContact->SetAsOwnL( aObserver );
       
   466 	}
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // CPbk2PresentationContact::MatchContactStore
       
   470 // -----------------------------------------------------------------------------
       
   471 //
       
   472 TBool CPbk2PresentationContact::MatchContactStore(
       
   473         const TDesC& aContactStoreUri) const
       
   474     {
       
   475     return iStoreContact.MatchContactStore(aContactStoreUri);
       
   476     }
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CPbk2PresentationContact::MatchContactStoreDomain
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 TBool CPbk2PresentationContact::MatchContactStoreDomain(
       
   483         const TDesC& aContactStoreDomain) const
       
   484     {
       
   485     return iStoreContact.MatchContactStoreDomain(aContactStoreDomain);
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CPbk2PresentationContact::CreateBookmarkLC
       
   490 // -----------------------------------------------------------------------------
       
   491 //
       
   492 MVPbkContactBookmark* CPbk2PresentationContact::CreateBookmarkLC() const
       
   493     {
       
   494     return iStoreContact.CreateBookmarkLC();
       
   495     }
       
   496 
       
   497 // -----------------------------------------------------------------------------
       
   498 // CPbk2PresentationContact::CurrentAmountOfFieldTypeInContact
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 TInt CPbk2PresentationContact::CurrentAmountOfFieldTypeInContact(
       
   502         const MVPbkFieldType& aType, const TDesC& aName ) const
       
   503     {
       
   504     const TInt maxMatchPriority = iStoreContact.ContactStore().
       
   505         StoreProperties().SupportedFields().MaxMatchPriority();
       
   506     const TInt count = iCollection->FieldCount();
       
   507     TInt res = 0;
       
   508     for ( TInt i = 0; i < count; ++i )
       
   509         {
       
   510         const MVPbkStoreContactField& field = iCollection->FieldAt(i);
       
   511         const MVPbkFieldType* type = NULL;
       
   512         for ( TInt j = 0; j < maxMatchPriority && !type; ++j )
       
   513             {
       
   514             type = field.MatchFieldType(j);
       
   515             }
       
   516         if ( (aType.IsSame( *type ) ) )
       
   517             {
       
   518             if (aName == KNullDesC)
       
   519                 {
       
   520                 ++res;
       
   521                 }
       
   522             else
       
   523                 {
       
   524                 TVPbkFieldVersitProperty prop;
       
   525                 prop.SetName(EVPbkVersitNameIMPP);
       
   526                 if (aType.Matches(prop, 0) &&
       
   527                         (MVPbkContactFieldUriData::Cast(field.FieldData()).
       
   528                         Scheme() == aName))
       
   529                     {
       
   530                     res++;
       
   531                     }
       
   532                 }
       
   533             }
       
   534         }
       
   535     return res;
       
   536     }
       
   537 
       
   538 // --------------------------------------------------------------------------
       
   539 // CPbk2PresentationContact::AddFieldL
       
   540 // --------------------------------------------------------------------------
       
   541 //
       
   542 EXPORT_C TInt CPbk2PresentationContact::AddFieldL(MVPbkStoreContactField* aField,
       
   543         const TDesC& aName)
       
   544     {
       
   545     // The aField instance can not be used after AddFieldL
       
   546     TInt index = iStoreContact.AddFieldL(aField);
       
   547     MVPbkStoreContactFieldCollection& fields = iStoreContact.Fields();
       
   548     // Create a presistent store field for the mapping field
       
   549     const MVPbkStoreContactField& field = fields.FieldAt(index);
       
   550     TRAPD(res, iCollection->AddFieldMappingL(field, aName));
       
   551 
       
   552     if (res != KErrNone)
       
   553         {
       
   554         // If creation of mapping field failed, remove also the field
       
   555         // from the store
       
   556         iStoreContact.RemoveField(index);
       
   557         User::Leave(res);
       
   558         }
       
   559 
       
   560     return iCollection->FindFieldIndex(field);
       
   561     }
       
   562 
       
   563 //  End of File