PECengine/ListLibrary2/AttrListSrc/CPEngAttributeListManager.cpp
branchRCL_3
changeset 17 a941bc465d9f
parent 0 094583676ce7
equal deleted inserted replaced
16:6ca72c0fe49a 17:a941bc465d9f
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Attribute list manager
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include    <e32std.h>
       
    20 #include "CPEngAttributeListManager.h"
       
    21 #include "CPEngAttributeListItem.h"
       
    22 #include "CPEngAttributeListModel.h"
       
    23 
       
    24 #include "PEngListLibTools.h"
       
    25 
       
    26 #include "MPEngStorageManager.h"
       
    27 #include "PEngStorageGlobals.h"
       
    28 #include "PEngStorageManager.h"
       
    29 #include "MPEngListLibFactory.h"
       
    30 #include "CPEngSessionSlotId.h"
       
    31 
       
    32 #include "PEngAttrLibFactory.h"
       
    33 #include "MPEngPresenceAttrManager.h"
       
    34 
       
    35 
       
    36 // CONSTANTS
       
    37 // flag of the default attribute list + 3 counts, of 3 arrays
       
    38 const TInt KIntEntrySize( 16 );
       
    39 
       
    40 // storage ID of the attribute lists
       
    41 _LIT( KAttributeListsStoreID, "AttributeListsStore" );
       
    42 
       
    43 //Default granurality for ID lists
       
    44 const TInt KAttributeListIdGranurality = 3;
       
    45 
       
    46 const TInt KWVIDMaxSize = 50;
       
    47 
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CPEngAttributeListManager::CPEngAttributeListManager()
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CPEngAttributeListManager::CPEngAttributeListManager(
       
    56     MPEngListLibFactory& aFactory )
       
    57         : CPEngStoreEntry( EPEngMixedPermanentVersionCon ),
       
    58         iAccessCount( 1 ), // init count to 1
       
    59         iFactory( aFactory ),
       
    60         iAttributeLists( KAttributeListIdGranurality ),
       
    61         iDeleteAttrListContactIDs( KAttributeListIdGranurality ),
       
    62         iDeleteAttrListContactLists( KAttributeListIdGranurality ),
       
    63         iDeleteDefaultAttrList( EFalse )
       
    64     {
       
    65     iSize = KIntEntrySize;
       
    66     }
       
    67 
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CPEngAttributeListManager::ConstructL()
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 void CPEngAttributeListManager::ConstructL(
       
    74     const CPEngSessionSlotId& aSessionSlot )
       
    75     {
       
    76     iSessionId = aSessionSlot.CloneL();
       
    77     MPEngStorageManager* storageManager =
       
    78         PEngStorageManager::GetStorageManagerL( *iSessionId );
       
    79     CleanupClosePushL( *storageManager );
       
    80     CPEngStoreEntry::BaseConstructL( *storageManager );
       
    81     CleanupStack::PopAndDestroy();
       
    82 
       
    83     iAttributeManager = PEngAttrLibFactory::AttributeManagerInstanceL(
       
    84                             *iSessionId );
       
    85     iStorageManager->RetrieveL( *this );
       
    86     }
       
    87 
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CPEngAttributeListManager::NewL()
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 CPEngAttributeListManager* CPEngAttributeListManager::NewL(
       
    94     MPEngListLibFactory& aFactory,
       
    95     const CPEngSessionSlotId& aSessionSlot )
       
    96     {
       
    97     CPEngAttributeListManager* self = NewLC( aFactory, aSessionSlot );
       
    98     CleanupStack::Pop();
       
    99     return self;
       
   100     }
       
   101 
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CPEngAttributeListManager::NewLC()
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CPEngAttributeListManager* CPEngAttributeListManager::NewLC(
       
   108     MPEngListLibFactory& aFactory,
       
   109     const CPEngSessionSlotId& aSessionSlot )
       
   110     {
       
   111     CPEngAttributeListManager* self =
       
   112         new( ELeave ) CPEngAttributeListManager( aFactory );
       
   113 
       
   114     CleanupClosePushL( *self );
       
   115     self->ConstructL( aSessionSlot );
       
   116 
       
   117     return self;
       
   118     }
       
   119 
       
   120 
       
   121 //   Destructor
       
   122 CPEngAttributeListManager::~CPEngAttributeListManager()
       
   123     {
       
   124     delete iSessionId;
       
   125     if ( iAttributeManager )
       
   126         {
       
   127         iAttributeManager->Close();
       
   128         }
       
   129 
       
   130     iAttributeLists.ResetAndDestroy();
       
   131     iDeleteAttrListContactIDs.Reset();
       
   132     iDeleteAttrListContactLists.Reset();
       
   133     }
       
   134 
       
   135 
       
   136 
       
   137 // =============================================================================
       
   138 // =============== From MPEngAttributeListManager ==============================
       
   139 // =============================================================================
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CPEngAttributeListManager::SessionId()
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 const CPEngSessionSlotId& CPEngAttributeListManager::SessionId() const
       
   146     {
       
   147     return *iSessionId;
       
   148     }
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CPEngAttributeListManager::Open()
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CPEngAttributeListManager::Open()
       
   156     {
       
   157     iAccessCount++;
       
   158     }
       
   159 
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CPEngAttributeListManager::Close()
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CPEngAttributeListManager::Close()
       
   166     {
       
   167     iAccessCount--;
       
   168     if ( iAccessCount == 0 )
       
   169         {
       
   170         iFactory.RemoveAttrListManager( this );
       
   171         delete this;
       
   172         }
       
   173     }
       
   174 
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CPEngAttributeListManager::CreateEmptyAttributeListL()
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 MPEngAttributeList2* CPEngAttributeListManager::CreateEmptyAttributeListL()
       
   181     {
       
   182     return CPEngAttributeListModel::NewL( iAttributeManager );
       
   183     }
       
   184 
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CPEngAttributeListManager::GetDefaultAttributeListL()
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 MPEngAttributeList2* CPEngAttributeListManager::GetDefaultAttributeListL(
       
   191     TPEngAttributeListsSet2 aAttrListsSet )
       
   192     {
       
   193     switch ( aAttrListsSet )
       
   194         {
       
   195         case EPEngNetworkAttributeLists:
       
   196             {
       
   197             TInt index ( FindCurrentDefaultAttributeList() );
       
   198             if ( index == KErrNotFound )
       
   199                 {
       
   200                 User::Leave( KErrNotFound );
       
   201                 }
       
   202 
       
   203             // get list of attributes and create model with it
       
   204             return CPEngAttributeListModel::NewL( iAttributeManager,
       
   205                                                   iAttributeLists[index]->PresenceAttributes() );
       
   206             }
       
   207 
       
   208         case EPEngLocalAttributeLists:
       
   209             {
       
   210             TInt index ( FindNewDefaultAttributeList() );
       
   211             if ( index == KErrNotFound )
       
   212                 {
       
   213                 index = FindCurrentDefaultAttributeList();
       
   214                 if ( index == KErrNotFound )
       
   215                     {
       
   216                     User::Leave( KErrNotFound );
       
   217                     }
       
   218                 }
       
   219 
       
   220             // get list of attributes and create model with it
       
   221             return CPEngAttributeListModel::NewL( iAttributeManager,
       
   222                                                   iAttributeLists[index]->PresenceAttributes() );
       
   223             }
       
   224         default:
       
   225             {
       
   226             User::Leave( KErrNotSupported );
       
   227             break;
       
   228             }
       
   229         }
       
   230 
       
   231 
       
   232     // just for compiler
       
   233     return NULL;
       
   234     }
       
   235 
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CPEngAttributeListManager::SetAsDefaultAttributeListL()
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 void CPEngAttributeListManager::SetAsDefaultAttributeListL(
       
   242     MPEngAttributeList2& aAttrList )
       
   243     {
       
   244     // set delete default attribute list to EFalse
       
   245     iDeleteDefaultAttrList = EFalse;
       
   246     CPEngAttributeListModel* model =
       
   247         static_cast< CPEngAttributeListModel* >( &aAttrList );
       
   248     // first check if there is not any new default flag already and remove it
       
   249     TInt index ( FindNewDefaultAttributeList() );
       
   250     if ( index != KErrNotFound )
       
   251         {
       
   252         // set new default to zero and if attribute lists is no more needed
       
   253         // delete it (0 == zero contacts in attr list)
       
   254         if ( 0 == iAttributeLists[ index ]->SetNewDefault( EFalse ) )
       
   255             {
       
   256             // attr. list empty delete it
       
   257             iSize -= iAttributeLists[ index ]->SizeOfEntry();
       
   258             delete iAttributeLists[ index ];
       
   259             iAttributeLists.Remove( index );
       
   260             }
       
   261         }
       
   262 
       
   263     // now find new one and set it, if it does not exist, create it
       
   264     index = FindAttributeList( model->PresenceAttributes() );
       
   265     if ( index != KErrNotFound )
       
   266         {
       
   267         // check if it is not already default, if not then set flag for new
       
   268         // otherwise there is no need to do anything
       
   269         if ( !iAttributeLists[ index ]->CurrentlyDefault() )
       
   270             {
       
   271             // it exists, just set the flag
       
   272             iAttributeLists[ index ]->SetNewDefault( ETrue );
       
   273             }
       
   274         }
       
   275     else
       
   276         {
       
   277         // it does not exist, create it and insert it in the array
       
   278         // with parameter new Default on
       
   279         CPEngAttributeListItem* newListItem =
       
   280             CPEngAttributeListItem::NewLC( model->PresenceAttributes() );
       
   281 
       
   282         // set it as new Default
       
   283         newListItem->SetNewDefault( ETrue );
       
   284         iAttributeLists.AppendL( newListItem );
       
   285         CleanupStack::Pop(); // newListItem
       
   286         iSize += newListItem->SizeOfEntry() ;
       
   287         }
       
   288 
       
   289 
       
   290     // store changes
       
   291     StoreChangesL();
       
   292     }
       
   293 
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CPEngAttributeListManager::DeleteDefaultAttributeListL()
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 void CPEngAttributeListManager::DeleteDefaultAttributeListL()
       
   300     {
       
   301     // check if there is somewhere new default attribute list
       
   302     TInt err ( FindNewDefaultAttributeList() );
       
   303     if ( err != KErrNotFound )
       
   304         {
       
   305         // set new default to zero and if attribute lists is no more needed
       
   306         // delete it (0 == zero contacts in attr list)
       
   307         if ( 0 == iAttributeLists[ err ]->SetNewDefault( EFalse ) )
       
   308             {
       
   309             // attr. list empty delete it
       
   310             delete iAttributeLists[ err ];
       
   311             iAttributeLists.Remove( err );
       
   312             }
       
   313         }
       
   314 
       
   315     // set flag to delete default attr list
       
   316     iDeleteDefaultAttrList = ETrue;
       
   317     StoreChangesL();
       
   318     }
       
   319 
       
   320 
       
   321 // -----------------------------------------------------------------------------
       
   322 // CPEngAttributeListManager::GetAttributeListForUserL()
       
   323 // -----------------------------------------------------------------------------
       
   324 //
       
   325 MPEngAttributeList2* CPEngAttributeListManager::GetAttributeListForUserL(
       
   326     const TDesC&  aContactId,
       
   327     TPEngAttributeListsSet2 aAttrListsSet )
       
   328     {
       
   329     return GetAttributeListForContactL(
       
   330                aContactId,
       
   331                aAttrListsSet,
       
   332                CPEngAttributeListItem::ECurrentContactIDs,
       
   333                CPEngAttributeListItem::ENewContactIDs );
       
   334     }
       
   335 
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CPEngAttributeListManager::GetAttributeListForContactListL()
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 MPEngAttributeList2* CPEngAttributeListManager::GetAttributeListForContactListL(
       
   342     const TDesC& aContactList,
       
   343     TPEngAttributeListsSet2 aAttrListsSet )
       
   344     {
       
   345     return GetAttributeListForContactL(
       
   346                aContactList,
       
   347                aAttrListsSet,
       
   348                CPEngAttributeListItem::ECurrentContactLists,
       
   349                CPEngAttributeListItem::ENewContactLists );
       
   350     }
       
   351 
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CPEngAttributeListManager::AttachAttributeListToUserL()
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void CPEngAttributeListManager::AttachAttributeListToUserL(
       
   358     const TDesC& aContactId,
       
   359     MPEngAttributeList2& aList )
       
   360     {
       
   361     AttachAttributeListToContactL(
       
   362         aContactId,
       
   363         aList,
       
   364         CPEngAttributeListItem::ECurrentContactIDs,
       
   365         CPEngAttributeListItem::ENewContactIDs,
       
   366         iDeleteAttrListContactIDs );
       
   367     }
       
   368 
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CPEngAttributeListManager::AttachAttributeListToContactListL()
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CPEngAttributeListManager::AttachAttributeListToContactListL(
       
   375     const TDesC& aContactList,
       
   376     MPEngAttributeList2& aList )
       
   377     {
       
   378     AttachAttributeListToContactL(
       
   379         aContactList,
       
   380         aList,
       
   381         CPEngAttributeListItem::ECurrentContactLists,
       
   382         CPEngAttributeListItem::ENewContactLists,
       
   383         iDeleteAttrListContactLists );
       
   384     }
       
   385 
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CPEngAttributeListManager::DeleteAttributeListFromUserL()
       
   389 // -----------------------------------------------------------------------------
       
   390 //
       
   391 void CPEngAttributeListManager::DeleteAttributeListFromUserL(
       
   392     const TDesC& aContactId )
       
   393     {
       
   394     DeleteAttributeListFromContactL(
       
   395         aContactId,
       
   396         CPEngAttributeListItem::ENewContactIDs,
       
   397         iDeleteAttrListContactIDs );
       
   398     }
       
   399 
       
   400 
       
   401 // -----------------------------------------------------------------------------
       
   402 // CPEngAttributeListManager::DeleteAttributeListFromContactListL()
       
   403 // -----------------------------------------------------------------------------
       
   404 //
       
   405 void CPEngAttributeListManager::DeleteAttributeListFromContactListL(
       
   406     const TDesC& aContactList )
       
   407     {
       
   408     DeleteAttributeListFromContactL(
       
   409         aContactList,
       
   410         CPEngAttributeListItem::ENewContactLists,
       
   411         iDeleteAttrListContactLists );
       
   412     }
       
   413 
       
   414 
       
   415 
       
   416 // -----------------------------------------------------------------------------
       
   417 // CPEngAttributeListManager::ExternalizeL()
       
   418 // -----------------------------------------------------------------------------
       
   419 //
       
   420 void CPEngAttributeListManager::ExternalizeL( RWriteStream& aStream,
       
   421                                               TPEngStorageType aStorageType ) const
       
   422     {
       
   423     if ( !( aStorageType & StorageType() ) )
       
   424         {
       
   425         User::Leave( KErrGeneral );
       
   426         }
       
   427 
       
   428     // write delete default list flag
       
   429     aStream.WriteInt32L( iDeleteDefaultAttrList );
       
   430     aStream.WriteInt32L( iSynchronized );
       
   431 
       
   432 
       
   433     // externalize list of to be delete wv IDs's attribute lists
       
   434     TInt count ( iDeleteAttrListContactIDs.Count() );
       
   435     aStream.WriteInt32L( count );
       
   436     for ( TInt i( 0 ) ; i < count ; i++ )
       
   437         {
       
   438         aStream << iDeleteAttrListContactIDs.MdcaPoint( i );
       
   439         }
       
   440 
       
   441 
       
   442     // externalize list of to be deleted contact lists's attribute lists
       
   443     count = iDeleteAttrListContactLists.Count();
       
   444     aStream.WriteInt32L( count );
       
   445     for ( TInt ii( 0 ) ; ii < count ; ii++ )
       
   446         {
       
   447         aStream << iDeleteAttrListContactLists.MdcaPoint( ii );
       
   448         }
       
   449 
       
   450 
       
   451     // externalize attribute lists
       
   452     count = iAttributeLists.Count();
       
   453     aStream.WriteInt32L( count );
       
   454     for ( TInt x ( 0 ) ;  x < count ; x++ )
       
   455         {
       
   456         iAttributeLists[x]->ExternalizeL( aStream );
       
   457         }
       
   458     }
       
   459 
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CPEngAttributeListManager::InternalizeL()
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CPEngAttributeListManager::InternalizeL( RReadStream& aStream,
       
   466                                               TPEngStorageType aStorageType )
       
   467     {
       
   468     if ( !( aStorageType & StorageType() ) )
       
   469         {
       
   470         User::Leave( KErrGeneral );
       
   471         }
       
   472 
       
   473 
       
   474     iSize = KIntEntrySize;
       
   475     iDeleteDefaultAttrList =  aStream.ReadInt32L();
       
   476 
       
   477     // read synchronize flag
       
   478     // member value isn't updated from stored data
       
   479     // ==> full sync is performed each time when attribute lists
       
   480     //     are used for the first time during the session
       
   481     //     (if member variable would be updated from persisted data, sync would hapen
       
   482     //      only when atttribute lists are updated for the first time to account.)
       
   483     aStream.ReadInt32L();
       
   484 
       
   485 
       
   486     // internalize list of to be delete wv IDs's attribute lists
       
   487     TInt count ( aStream.ReadInt32L() );
       
   488     iDeleteAttrListContactIDs.Reset();
       
   489     for ( TInt i( 0 ) ; i < count ; i++ )
       
   490         {
       
   491         // use max size as double of KWVIDMaxSize
       
   492         HBufC16* newContact = HBufC16::NewLC( aStream, 2 * KWVIDMaxSize );
       
   493         iDeleteAttrListContactIDs.InsertIsqL( *newContact, ECmpFolded );
       
   494         iSize += newContact->Size();
       
   495         CleanupStack::PopAndDestroy(); // newContact
       
   496         }
       
   497 
       
   498 
       
   499     // internalize list of to be deleted contact lists's attribute lists
       
   500     count = aStream.ReadInt32L();
       
   501     iDeleteAttrListContactLists.Reset();
       
   502     for ( TInt ii( 0 ) ; ii < count ; ii++ )
       
   503         {
       
   504         // use max size as double of KWVIDMaxSize
       
   505         HBufC16* newContact = HBufC16::NewLC( aStream, 2 * KWVIDMaxSize );
       
   506         iDeleteAttrListContactLists.InsertIsqL( *newContact, ECmpFolded );
       
   507         iSize += newContact->Size();
       
   508         CleanupStack::PopAndDestroy(); // newContact
       
   509         }
       
   510 
       
   511     // reset array of the lists
       
   512     iAttributeLists.ResetAndDestroy();
       
   513 
       
   514 
       
   515     // first read  number of the Attribute lists
       
   516     count = aStream.ReadInt32L();
       
   517     for ( TInt x ( 0 ) ;  x < count ; x++ )
       
   518         {
       
   519         CPEngAttributeListItem* newLists =
       
   520             CPEngAttributeListItem::NewLC( aStream );
       
   521         iAttributeLists.AppendL( newLists );
       
   522         CleanupStack::Pop(); // newLists
       
   523         iSize += newLists->SizeOfEntry();
       
   524         }
       
   525     }
       
   526 
       
   527 
       
   528 // -----------------------------------------------------------------------------
       
   529 // CPEngAttributeListManager::StorageId()
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 const TDesC& CPEngAttributeListManager::StorageId() const
       
   533     {
       
   534     return KAttributeListsStoreID;
       
   535     }
       
   536 
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CPEngAttributeListManager::EntrySize()
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 TUint32 CPEngAttributeListManager::EntrySize() const
       
   543     {
       
   544     return iSize;
       
   545     }
       
   546 
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CPEngAttributeListManager::HandleSIDsChangeL()
       
   550 // -----------------------------------------------------------------------------
       
   551 //
       
   552 void CPEngAttributeListManager::HandleSIDsChangeL(
       
   553     CPtrCArray& /* aChangedSIDs */ )
       
   554     {
       
   555     iStorageManager->RetrieveL( *this );
       
   556     }
       
   557 
       
   558 
       
   559 // -----------------------------------------------------------------------------
       
   560 // CPEngAttributeListManager::HandleSIDNotifyError()
       
   561 // -----------------------------------------------------------------------------
       
   562 //
       
   563 void CPEngAttributeListManager::HandleSIDNotifyError(
       
   564     TInt /* aError */ )
       
   565     {
       
   566     }
       
   567 
       
   568 
       
   569 // -----------------------------------------------------------------------------
       
   570 // CPEngAttributeListManager::FindAttributeList()
       
   571 // -----------------------------------------------------------------------------
       
   572 //
       
   573 TInt CPEngAttributeListManager::FindAttributeList(
       
   574     const RArray<TUint32>& aAttributeList ) const
       
   575     {
       
   576 
       
   577     // go through all attribute lists and look for the same attribute list
       
   578     TInt count ( iAttributeLists.Count() );
       
   579     for ( TInt x ( 0 ) ; x < count ; x++ )
       
   580         {
       
   581         if ( CompareAttributeLists( iAttributeLists[x]->PresenceAttributes() ,
       
   582                                     aAttributeList ) )
       
   583             {
       
   584             // Lists of presence attributes matches, return index of
       
   585             // the Attribute List Item
       
   586             return x;
       
   587             }
       
   588         }
       
   589 
       
   590     // no list of attributes which matches was found
       
   591     return KErrNotFound;
       
   592     }
       
   593 
       
   594 
       
   595 // -----------------------------------------------------------------------------
       
   596 // CPEngAttributeListManager::FindCurrentDefaultAttributeList()
       
   597 // -----------------------------------------------------------------------------
       
   598 //
       
   599 TInt CPEngAttributeListManager::FindCurrentDefaultAttributeList() const
       
   600     {
       
   601     return FindDefaultAttrList( &CPEngAttributeListItem::CurrentlyDefault );
       
   602     }
       
   603 
       
   604 
       
   605 // -----------------------------------------------------------------------------
       
   606 // CPEngAttributeListManager::FindNewDefaultAttributeList()
       
   607 // -----------------------------------------------------------------------------
       
   608 //
       
   609 TInt CPEngAttributeListManager::FindNewDefaultAttributeList() const
       
   610     {
       
   611     return FindDefaultAttrList( &CPEngAttributeListItem::NewDefault );
       
   612     }
       
   613 
       
   614 
       
   615 // -----------------------------------------------------------------------------
       
   616 // CPEngAttributeListManager::StoreChangesL()
       
   617 // -----------------------------------------------------------------------------
       
   618 //
       
   619 TInt CPEngAttributeListManager::StoreChangesL()
       
   620     {
       
   621     TRAPD( err, iStorageManager->StoreL( *this ) );
       
   622     if ( err == KErrAccessDenied )
       
   623         {
       
   624         CPtrCArray* arr = NULL;
       
   625         HandleSIDsChangeL( *arr );
       
   626         }
       
   627     User::LeaveIfError( err );
       
   628     return err;
       
   629     }
       
   630 
       
   631 
       
   632 // -----------------------------------------------------------------------------
       
   633 // CPEngAttributeListManager::CompareAttributeLists()
       
   634 // -----------------------------------------------------------------------------
       
   635 //
       
   636 TBool CPEngAttributeListManager::CompareAttributeLists(
       
   637     const RArray<TUint32>&  aListA ,
       
   638     const RArray<TUint32>&  aListB ) const
       
   639     {
       
   640     TInt count( aListA.Count() );
       
   641     if ( count !=  aListB.Count() )
       
   642         {
       
   643         // they do not even have same count of the presence attributes so
       
   644         // they cannot be same
       
   645         return EFalse;
       
   646         }
       
   647 
       
   648     // array are in order without duplicates, so we can easily go through
       
   649     for ( TInt x( 0 ) ; x < count ; x++ )
       
   650         {
       
   651         if ( aListA[x] != aListB[x] )
       
   652             {
       
   653             // do not match -> Attribute lists are not same
       
   654             return EFalse;
       
   655             }
       
   656         }
       
   657 
       
   658     // it went all fine, then Attribute lists are same
       
   659     return ETrue;
       
   660     }
       
   661 
       
   662 
       
   663 // -----------------------------------------------------------------------------
       
   664 // CPEngAttributeListManager::FindItemInArray()
       
   665 // -----------------------------------------------------------------------------
       
   666 //
       
   667 TInt CPEngAttributeListManager::FindItemInArray( const CDesCArray& aArray,
       
   668                                                  const TDesC& aItem ) const
       
   669     {
       
   670     TInt position( 0 );
       
   671     if ( aArray.FindIsq( aItem, position, ECmpFolded ) == KErrNone )
       
   672         {
       
   673         return position;
       
   674         }
       
   675 
       
   676     return KErrNotFound;
       
   677     }
       
   678 
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // CPEngAttributeListManager::FindContactInAttrLists()
       
   682 // -----------------------------------------------------------------------------
       
   683 //
       
   684 TInt CPEngAttributeListManager::FindContactInAttrLists(
       
   685     const TDesC& aContact,
       
   686     CPEngAttributeListItem::TPEngAttrListItemArrays aArray ) const
       
   687     {
       
   688     // go through all attribute lists and look for the contact in Current ones
       
   689     TInt count ( iAttributeLists.Count() );
       
   690     for ( TInt x ( 0 ) ; x < count ; x++ )
       
   691         {
       
   692         if ( KErrNotFound != FindItemInArray(
       
   693                  iAttributeLists[x]->ArrayOfContacts( aArray ),
       
   694                  aContact ) )
       
   695             {
       
   696             // contact was found, return index
       
   697             return x;
       
   698             }
       
   699         }
       
   700 
       
   701     // contact was not found in any of the lists
       
   702     return KErrNotFound;
       
   703     }
       
   704 
       
   705 
       
   706 // -----------------------------------------------------------------------------
       
   707 // CPEngAttributeListManager::FindDefaultAttrList()
       
   708 // -----------------------------------------------------------------------------
       
   709 //
       
   710 TInt CPEngAttributeListManager::FindDefaultAttrList(
       
   711     ItemDefaultSet aDefaultSetCall ) const
       
   712     {
       
   713     TInt count ( iAttributeLists.Count() );
       
   714     for ( TInt x ( 0 ) ; x < count ; x++ )
       
   715         {
       
   716         if ( ( iAttributeLists[x]->*aDefaultSetCall )() )
       
   717             {
       
   718             return x;
       
   719             }
       
   720         }
       
   721 
       
   722     // no default list
       
   723     return KErrNotFound;
       
   724     }
       
   725 
       
   726 
       
   727 // -----------------------------------------------------------------------------
       
   728 // CPEngAttributeListManager::GetAttributeListForContactL()
       
   729 // -----------------------------------------------------------------------------
       
   730 //
       
   731 MPEngAttributeList2* CPEngAttributeListManager::GetAttributeListForContactL(
       
   732     const TDesC& aContact,
       
   733     TPEngAttributeListsSet2 aAttrListsSet,
       
   734     CPEngAttributeListItem::TPEngAttrListItemArrays aArrayCurrent,
       
   735     CPEngAttributeListItem::TPEngAttrListItemArrays aArrayNew )
       
   736     {
       
   737     switch ( aAttrListsSet )
       
   738         {
       
   739         case EPEngNetworkAttributeLists:
       
   740             {
       
   741             TInt index( FindContactInAttrLists( aContact, aArrayCurrent ) );
       
   742             if ( index == KErrNotFound )
       
   743                 {
       
   744                 User::Leave( KErrNotFound );
       
   745                 }
       
   746 
       
   747             // get list of attributes and create model with it
       
   748             return CPEngAttributeListModel::NewL(
       
   749                        iAttributeManager,
       
   750                        iAttributeLists[index]->PresenceAttributes() );
       
   751             }
       
   752 
       
   753 
       
   754         case EPEngLocalAttributeLists:
       
   755             {
       
   756             TInt index( FindContactInAttrLists( aContact, aArrayNew ) );
       
   757             if ( index == KErrNotFound )
       
   758                 {
       
   759                 //try to have a look in the published ones
       
   760                 index = FindContactInAttrLists( aContact, aArrayCurrent );
       
   761                 if ( index == KErrNotFound )
       
   762                     {
       
   763                     User::Leave( KErrNotFound );
       
   764                     }
       
   765                 }
       
   766 
       
   767             // get list of attributes and create model with it
       
   768             return CPEngAttributeListModel::NewL(
       
   769                        iAttributeManager,
       
   770                        iAttributeLists[index]->PresenceAttributes() );
       
   771             }
       
   772 
       
   773         default:
       
   774             {
       
   775             User::Leave( KErrNotSupported );
       
   776             break;
       
   777             }
       
   778         }
       
   779 
       
   780     // just for compiler
       
   781     return NULL;
       
   782     }
       
   783 
       
   784 
       
   785 // -----------------------------------------------------------------------------
       
   786 // CPEngAttributeListManager::AttachAttributeListToContactL()
       
   787 // -----------------------------------------------------------------------------
       
   788 //
       
   789 void CPEngAttributeListManager::AttachAttributeListToContactL(
       
   790     const TDesC& aContact,
       
   791     MPEngAttributeList2& aList,
       
   792     CPEngAttributeListItem::TPEngAttrListItemArrays aArrayCurrent,
       
   793     CPEngAttributeListItem::TPEngAttrListItemArrays aArrayNew,
       
   794     CDesCArray& aToDeleteContacts )
       
   795     {
       
   796     TBool need2Store( EFalse );
       
   797     CPEngAttributeListModel* model =
       
   798         static_cast< CPEngAttributeListModel* >( &aList );
       
   799 
       
   800     // If defined contact is defined for some other Attribute list,
       
   801     // it can be ignored, since WV server will remove that connection
       
   802     // and link it to the new attribute list what has to be checked
       
   803     // it that contact is not in new part of any attribute lists
       
   804     // and as well delete part, form there it has to be deleted
       
   805 
       
   806     // first look in the new contacts of the attribute list
       
   807     TInt index( FindContactInAttrLists( aContact, aArrayNew ) );
       
   808     if ( index != KErrNotFound )
       
   809         {
       
   810         need2Store = ETrue;
       
   811         // it was found in new, remove it from there
       
   812         TInt count ( iAttributeLists[index]->DeleteContactFromList( aContact,
       
   813                                                                     aArrayNew ) );
       
   814 
       
   815         // check if there is still some contact in this attribute list,
       
   816         // if not delete it
       
   817         if ( count == 0 )
       
   818             {
       
   819             // delete attribute list
       
   820             iSize -= iAttributeLists[ index ]->SizeOfEntry();
       
   821             delete iAttributeLists[ index ];
       
   822             iAttributeLists.Remove( index );
       
   823             }
       
   824         }
       
   825 
       
   826     // now look in delete contacts of the attribute list
       
   827     index = FindItemInArray( aToDeleteContacts, aContact );
       
   828     if ( index != KErrNotFound )
       
   829         {
       
   830         need2Store = ETrue;
       
   831         // it was found in delete ones, remove it, it won't be neccesarry
       
   832         aToDeleteContacts.Delete( index );
       
   833         iSize -= aContact.Size();
       
   834         }
       
   835 
       
   836 
       
   837     // now find if list of attributes already somewhere exists
       
   838     TInt listIndex ( FindAttributeList( model->PresenceAttributes() ) );
       
   839     if ( listIndex != KErrNotFound )
       
   840         {
       
   841         // attribute list already exists, check current
       
   842         // if it does not exists already, if not add it to the new ones
       
   843 
       
   844         if ( KErrNotFound != FindItemInArray(
       
   845                  iAttributeLists[listIndex]->ArrayOfContacts( aArrayCurrent ),
       
   846                  aContact ) )
       
   847             {
       
   848             // contact is in current, no sense to do anything,
       
   849             if ( need2Store )
       
   850                 {
       
   851                 User::LeaveIfError( StoreChangesL() );
       
   852                 }
       
   853             return;
       
   854             }
       
   855         else
       
   856             {
       
   857             // not in current, add it to the new contact lists
       
   858             iAttributeLists[ listIndex ]->AddContactToListL( aContact,
       
   859                                                              aArrayNew );
       
   860             iSize += aContact.Size();
       
   861             }
       
   862 
       
   863         // now store changes to the store in any case
       
   864         User::LeaveIfError( StoreChangesL() );
       
   865         return;
       
   866         }
       
   867 
       
   868 
       
   869     // Attribute lists does not exist, create it
       
   870     CPEngAttributeListItem* newListItem = CPEngAttributeListItem::NewLC(
       
   871                                               model->PresenceAttributes() );
       
   872 
       
   873     // add contact to the new ones and append it to the array
       
   874     newListItem->AddContactToListL( aContact, aArrayNew );
       
   875     iAttributeLists.AppendL( newListItem );
       
   876     iSize += newListItem->SizeOfEntry() ;
       
   877     CleanupStack::Pop(); // newListItem
       
   878     User::LeaveIfError( StoreChangesL() );
       
   879     }
       
   880 
       
   881 
       
   882 // -----------------------------------------------------------------------------
       
   883 // CPEngAttributeListManager::DeleteAttributeListFromContactL()
       
   884 // -----------------------------------------------------------------------------
       
   885 //
       
   886 void CPEngAttributeListManager::DeleteAttributeListFromContactL(
       
   887     const TDesC& aContact,
       
   888     CPEngAttributeListItem::TPEngAttrListItemArrays aArrayNew,
       
   889     CDesCArray& aToDeleteContacts )
       
   890     {
       
   891     // there is now check if contact is in current, does not matter,
       
   892     // just try if it is in new, and delete if from there and add it to the
       
   893     // delete contacts queue
       
   894     TInt indexNew ( FindContactInAttrLists( aContact, aArrayNew ) );
       
   895     if ( indexNew != KErrNotFound )
       
   896         {
       
   897         TInt count ( iAttributeLists[indexNew]->DeleteContactFromList( aContact,
       
   898                                                                        aArrayNew ) );
       
   899         // check if there is still some contact in this attribute list,
       
   900         // if not delete it
       
   901         if ( count == 0 )
       
   902             {
       
   903             // delete attribute list, it's empty
       
   904             iSize -= iAttributeLists[ indexNew ]->SizeOfEntry();
       
   905             delete iAttributeLists[ indexNew ];
       
   906             iAttributeLists.Remove( indexNew );
       
   907             }
       
   908         }
       
   909 
       
   910     // now add contact to the delete contact lists queue,
       
   911     // if it's already there silently ignore
       
   912     TRAPD( e, aToDeleteContacts.InsertIsqL( aContact, ECmpFolded ) );
       
   913     iSize += aContact.Size();
       
   914     NListLibTools::LeaveIfMajorErrorL( e );
       
   915     User::LeaveIfError( StoreChangesL() );
       
   916     }
       
   917 
       
   918 //  End of File
       
   919 
       
   920 
       
   921 
       
   922 
       
   923 
       
   924 
       
   925 
       
   926 
       
   927 
       
   928 
       
   929 
       
   930 
       
   931 
       
   932 
       
   933 
       
   934 
       
   935 
       
   936 
       
   937 
       
   938 
       
   939