phonebookui/Phonebook2/UIControls/src/CPbk2StoreInfoItemBuilder.cpp
branchRCL_3
changeset 20 f4a778e096c2
child 21 9da50d567e3c
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     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 class for building store info items
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CPbk2StoreInfoItemBuilder.h"
       
    20 
       
    21 // Phonebook2
       
    22 #include "MPbk2StoreInfoUiItem.h"
       
    23 #include <CPbk2StoreInfoItemArray.h>
       
    24 #include <CPbk2StoreInfoItem.h>
       
    25 #include <CPbk2StorePropertyArray.h>
       
    26 #include <RPbk2LocalizedResourceFile.h>
       
    27 #include <Pbk2DataCaging.hrh>
       
    28 #include <CPbk2StoreProperty.h>
       
    29 #include <Pbk2UIControls.rsg>
       
    30 #include <CPbk2StorePropertyContentStoreName.h>
       
    31 #include <Pbk2StoreProperty.hrh>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <MVPbkContactStoreInfo.h>
       
    35 #include <MVPbkContactStore.h>
       
    36 #include <MVPbkContactStoreList.h>
       
    37 #include <CVPbkContactManager.h>
       
    38 #include <TVPbkContactStoreUriPtr.h>
       
    39 #include <CVPbkContactStoreUriArray.h>
       
    40 
       
    41 // System includes
       
    42 #include <barsread.h>
       
    43 #include <coemain.h>
       
    44 #include <StringLoader.h>
       
    45 #include <avkon.rsg>
       
    46 
       
    47 // CONSTANTS
       
    48 
       
    49 /// Unnamed namespace for local definitions
       
    50 namespace {
       
    51 
       
    52 #ifdef _DEBUG
       
    53 
       
    54 enum TPanicCode
       
    55     {
       
    56     EBuildSingleStoreItemsL_PreCond
       
    57     };
       
    58 
       
    59 void Panic(TInt aReason)
       
    60     {
       
    61     _LIT(KPanicText, "CPbk2StoreInfoItemBuilder");
       
    62     User::Panic(KPanicText, aReason);
       
    63     }
       
    64 
       
    65 #endif // _DEBUG
       
    66 
       
    67 const TInt KMaxIntSize = 16;
       
    68 const TInt KKByte = 1024;
       
    69 typedef TInt (MVPbkContactStoreInfo::*StoreInfoFunction)() const;
       
    70 
       
    71 // Create mappings that doesn't interfere with definitions
       
    72 // in Pbk2StoreProperty.hrh -> they start from 0x01
       
    73 // There is no "max number of contact" item in the UI but
       
    74 // this is needed to calculate free slots
       
    75 const TUint32 KPbk2StoreInfoMaxNumberOfContacts = 0x10000000;
       
    76 const TUint32 KPbk2StoreInfoLastMapping = 0xffffffff;
       
    77 
       
    78 /// Function mapping definitions
       
    79 struct TPbk2StoreInfoFunctionMapping
       
    80     {
       
    81     TUint32 iType;
       
    82     StoreInfoFunction iInfoFunctionL;
       
    83     };
       
    84 
       
    85 /// Mappings for MVPbkContactStoreInfo functions that return TInt
       
    86 static const TPbk2StoreInfoFunctionMapping KInfoFunctionMappings[] =
       
    87     {
       
    88         {
       
    89         KPbk2StoreInfoMaxNumberOfContacts,
       
    90         &MVPbkContactStoreInfo::MaxNumberOfContactsL
       
    91         },
       
    92         {
       
    93         KPbk2StoreInfoNumberOfContacts,
       
    94         &MVPbkContactStoreInfo::NumberOfContactsL
       
    95         },
       
    96         {
       
    97         KPbk2StoreInfoNumberOfGroups,
       
    98         &MVPbkContactStoreInfo::NumberOfGroupsL
       
    99         },
       
   100         {
       
   101         KPbk2StoreInfoLastMapping,
       
   102         NULL
       
   103         }
       
   104     };
       
   105 
       
   106 /**
       
   107  * Gets info data.
       
   108  *
       
   109  * @param aType     Type of info.
       
   110  * @param aInfo     Contact store info interface.
       
   111  * @return  Requested info data.
       
   112  */
       
   113 TInt GetInfoDataL( TUint32 aType, const MVPbkContactStoreInfo& aInfo )
       
   114     {
       
   115     TInt ret = KErrNotFound;
       
   116 
       
   117     for ( TInt i = 0;
       
   118         KInfoFunctionMappings[i].iType != KPbk2StoreInfoLastMapping; ++i)
       
   119         {
       
   120         if (KInfoFunctionMappings[i].iType & aType)
       
   121             {
       
   122             ret = (aInfo.*KInfoFunctionMappings[i].iInfoFunctionL)();
       
   123             }
       
   124         }
       
   125 
       
   126     if ( ret == KErrNotFound )
       
   127         {
       
   128         if (aType & KPbk2StoreInfoReservedMemory)
       
   129             {
       
   130             ret = I64LOW((aInfo.ReservedMemoryL() + KKByte/2) / KKByte);
       
   131             }
       
   132         else if (aType & KPbk2StoreInfoFreeMemory)
       
   133             {
       
   134             ret = I64LOW(aInfo.FreeMemoryL() / KKByte);
       
   135             }
       
   136         }
       
   137 
       
   138     return ret;
       
   139     }
       
   140 
       
   141 /**
       
   142  * Store info UI item representation.
       
   143  */
       
   144 NONSHARABLE_CLASS(CUiItem) : public CBase, public MPbk2StoreInfoUiItem
       
   145     {
       
   146     public: // Destruction
       
   147 
       
   148         /**
       
   149          * Destructor.
       
   150          */
       
   151         ~CUiItem();
       
   152 
       
   153     public: // Interface
       
   154 
       
   155         /**
       
   156          * Sets heading text.
       
   157          *
       
   158          * @param aText     The text to set.
       
   159          */
       
   160         void SetHeadingText(
       
   161                 TDesC* aText );
       
   162 
       
   163         /**
       
   164          * Sets item text.
       
   165          *
       
   166          * @param aText     The text to set.
       
   167          */
       
   168         void SetItemText(
       
   169                 TDesC* aText );
       
   170 
       
   171     public: // From MPbk2StoreInfoUiItem
       
   172         const TDesC& HeadingText() const;
       
   173         const TDesC& ItemText() const;
       
   174 
       
   175     private: // Data
       
   176         /// Own: Heading text
       
   177         TDesC* iHeadingText;
       
   178         /// Own: Item
       
   179         TDesC* iItem;
       
   180     };
       
   181 
       
   182 // --------------------------------------------------------------------------
       
   183 // CUiItem::~CUiItem
       
   184 // --------------------------------------------------------------------------
       
   185 //
       
   186 CUiItem::~CUiItem()
       
   187     {
       
   188     delete iHeadingText;
       
   189     delete iItem;
       
   190     }
       
   191 
       
   192 // --------------------------------------------------------------------------
       
   193 // CUiItem::SetHeadingText
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 void CUiItem::SetHeadingText( TDesC* aText )
       
   197     {
       
   198     iHeadingText = aText;
       
   199     }
       
   200 
       
   201 // --------------------------------------------------------------------------
       
   202 // CUiItem::SetItemText
       
   203 // --------------------------------------------------------------------------
       
   204 //
       
   205 void CUiItem::SetItemText( TDesC* aText )
       
   206     {
       
   207     iItem = aText;
       
   208     }
       
   209 
       
   210 // --------------------------------------------------------------------------
       
   211 // CUiItem::HeadingText
       
   212 // --------------------------------------------------------------------------
       
   213 //
       
   214 const TDesC& CUiItem::HeadingText() const
       
   215     {
       
   216     return *iHeadingText;
       
   217     }
       
   218 
       
   219 // --------------------------------------------------------------------------
       
   220 // CUiItem::ItemText
       
   221 // --------------------------------------------------------------------------
       
   222 //
       
   223 const TDesC& CUiItem::ItemText() const
       
   224     {
       
   225     return *iItem;
       
   226     }
       
   227 
       
   228 } /// namespace
       
   229 
       
   230 // --------------------------------------------------------------------------
       
   231 // CPbk2StoreInfoItemBuilder::CPbk2StoreInfoItemBuilder
       
   232 // --------------------------------------------------------------------------
       
   233 //
       
   234 CPbk2StoreInfoItemBuilder::CPbk2StoreInfoItemBuilder
       
   235         ( CVPbkContactManager& aContactManager,
       
   236           CPbk2StorePropertyArray& aStoreProperties ) :
       
   237             iContactManager( aContactManager ),
       
   238             iStoreProperties( aStoreProperties )
       
   239     {
       
   240     }
       
   241 
       
   242 // --------------------------------------------------------------------------
       
   243 // CPbk2StoreInfoItemBuilder::~CPbk2StoreInfoItemBuilder
       
   244 // --------------------------------------------------------------------------
       
   245 //
       
   246 CPbk2StoreInfoItemBuilder::~CPbk2StoreInfoItemBuilder()
       
   247     {
       
   248     delete iSingleStoreItemDefs;
       
   249     delete iAllStoresItemDefs;
       
   250     }
       
   251 
       
   252 // --------------------------------------------------------------------------
       
   253 // CPbk2StoreInfoItemBuilder::NewL
       
   254 // --------------------------------------------------------------------------
       
   255 //
       
   256 CPbk2StoreInfoItemBuilder* CPbk2StoreInfoItemBuilder::NewL
       
   257         ( CVPbkContactManager& aContactManager,
       
   258           CPbk2StorePropertyArray& aStoreProperties )
       
   259     {
       
   260     CPbk2StoreInfoItemBuilder* self = new( ELeave ) CPbk2StoreInfoItemBuilder
       
   261         ( aContactManager, aStoreProperties );
       
   262     CleanupStack::PushL(self);
       
   263     self->ConstructL();
       
   264     CleanupStack::Pop(self);
       
   265     return self;
       
   266     }
       
   267 
       
   268 // --------------------------------------------------------------------------
       
   269 // CPbk2StoreInfoItemBuilder::ConstructL
       
   270 // --------------------------------------------------------------------------
       
   271 //
       
   272 void CPbk2StoreInfoItemBuilder::ConstructL()
       
   273     {
       
   274     TResourceReader reader;
       
   275     CCoeEnv::Static()->CreateResourceReaderLC
       
   276         (reader, R_PBK2_SINGLE_STORE_INFO_ITEMS);
       
   277     iSingleStoreItemDefs = CPbk2StoreInfoItemArray::NewL(reader);
       
   278     CleanupStack::PopAndDestroy(); // reader
       
   279 
       
   280     CCoeEnv::Static()->CreateResourceReaderLC
       
   281         (reader, R_PBK2_ALL_STORES_INFO_ITEMS);
       
   282     iAllStoresItemDefs = CPbk2StoreInfoItemArray::NewL(reader);
       
   283     CleanupStack::PopAndDestroy(); // reader
       
   284     }
       
   285 
       
   286 // --------------------------------------------------------------------------
       
   287 // CPbk2StoreInfoItemBuilder::BuildSingleStoreItemsL
       
   288 // --------------------------------------------------------------------------
       
   289 //
       
   290 CArrayPtr<MPbk2StoreInfoUiItem>*
       
   291         CPbk2StoreInfoItemBuilder::BuildSingleStoreItemsL
       
   292             ( const TVPbkContactStoreUriPtr& aUriPtr )
       
   293     {
       
   294     MVPbkContactStore* store =
       
   295         iContactManager.ContactStoresL().Find(aUriPtr);
       
   296     __ASSERT_DEBUG(store, Panic(EBuildSingleStoreItemsL_PreCond));
       
   297 
       
   298     const TInt granularity = 10;
       
   299     CArrayPtrFlat<MPbk2StoreInfoUiItem>* results =
       
   300         new(ELeave) CArrayPtrFlat<MPbk2StoreInfoUiItem>(granularity);
       
   301     CleanupStack::PushL(results);
       
   302     const CPbk2StoreProperty* prop = iStoreProperties.FindProperty(aUriPtr);
       
   303     const TInt count = iSingleStoreItemDefs->Count();
       
   304     for (TInt i = 0; i < count; ++i)
       
   305         {
       
   306         const CPbk2StoreInfoItem& item = iSingleStoreItemDefs->At(i);
       
   307         if (prop->MemoryInfoTypes() & item.ItemType())
       
   308             {
       
   309             MPbk2StoreInfoUiItem* uiItem =
       
   310                 CreateSingleStoreUiItemLC(*store, *prop, item);
       
   311             results->AppendL(uiItem);
       
   312             CleanupStack::Pop(); // uiItem
       
   313             }
       
   314         }
       
   315     CleanupStack::Pop(results);
       
   316     return results;
       
   317     }
       
   318 
       
   319 // --------------------------------------------------------------------------
       
   320 // CPbk2StoreInfoItemBuilder::BuildSingleStoreItemsL
       
   321 // --------------------------------------------------------------------------
       
   322 //
       
   323 CArrayPtr<MPbk2StoreInfoUiItem>*
       
   324         CPbk2StoreInfoItemBuilder::BuildAllStoresItemsL
       
   325             ( CVPbkContactStoreUriArray& aStoreUris )
       
   326     {
       
   327     const TInt granularity = 10;
       
   328     CArrayPtrFlat<MPbk2StoreInfoUiItem>* results =
       
   329         new(ELeave) CArrayPtrFlat<MPbk2StoreInfoUiItem>(granularity);
       
   330     CleanupStack::PushL(results);
       
   331 
       
   332     const TInt storeCount = aStoreUris.Count();
       
   333     const TInt itemCount = iAllStoresItemDefs->Count();
       
   334     for (TInt i = 0; i < itemCount; ++i)
       
   335         {
       
   336         TInt sumValue = KErrNotFound;
       
   337         const CPbk2StoreInfoItem& item = iAllStoresItemDefs->At(i);
       
   338         for (TInt j = 0; j < storeCount; ++j)
       
   339             {
       
   340             // Get store property
       
   341             const CPbk2StoreProperty* prop =
       
   342                 iStoreProperties.FindProperty(aStoreUris[j]);
       
   343             // Get store
       
   344             MVPbkContactStore* store =
       
   345                 iContactManager.ContactStoresL().Find(aStoreUris[j]);
       
   346 
       
   347             if (prop->MemoryInfoTypes() & item.ItemType())
       
   348                 {
       
   349                 if (item.Flags() & KPbk2StoreInfoOneItemForEachStore)
       
   350                     {
       
   351                     // Create own item for each store
       
   352                     MPbk2StoreInfoUiItem* uiItem =
       
   353                         CreateSingleStoreUiItemLC(*store, *prop, item);
       
   354                     results->AppendL(uiItem);
       
   355                     CleanupStack::Pop(); // uiItem
       
   356                     }
       
   357                 else if (sumValue == KErrNotFound)
       
   358                     {
       
   359                     // Init sum value with first stores value
       
   360                     sumValue = GetInfoDataL(item.ItemType(),
       
   361                         store->StoreInfo());
       
   362                     }
       
   363                 else
       
   364                     {
       
   365                     // Add to sumValue
       
   366                     sumValue += GetInfoDataL(item.ItemType(),
       
   367                         store->StoreInfo());
       
   368                     }
       
   369                 }
       
   370 
       
   371             }
       
   372 
       
   373         if (sumValue != KErrNotFound)
       
   374             {
       
   375             MPbk2StoreInfoUiItem* uiItem =
       
   376                 CreateUiItemLC(NULL, item, sumValue);
       
   377             results->AppendL(uiItem);
       
   378             CleanupStack::Pop(); // uiItem
       
   379             }
       
   380         }
       
   381 
       
   382     CleanupStack::Pop(results);
       
   383     return results;
       
   384     }
       
   385 
       
   386 // --------------------------------------------------------------------------
       
   387 // CPbk2StoreInfoItemBuilder::CreateSingleStoreUiItemLC
       
   388 // --------------------------------------------------------------------------
       
   389 //
       
   390 MPbk2StoreInfoUiItem* CPbk2StoreInfoItemBuilder::CreateSingleStoreUiItemLC
       
   391         ( MVPbkContactStore& aStore, const CPbk2StoreProperty& aProperty,
       
   392           const CPbk2StoreInfoItem& aItem )
       
   393     {
       
   394     TInt value = 0;
       
   395     const MVPbkContactStoreInfo& info = aStore.StoreInfo();
       
   396     if (aItem.ItemType() & KPbk2StoreInfoFreeLocations)
       
   397         {
       
   398         value = GetInfoDataL(KPbk2StoreInfoMaxNumberOfContacts, info) -
       
   399             GetInfoDataL(KPbk2StoreInfoNumberOfContacts, info);
       
   400         }
       
   401     else
       
   402         {
       
   403         value = GetInfoDataL(aItem.ItemType(), info);
       
   404         }
       
   405 
       
   406     return CreateUiItemLC(&aProperty, aItem, value);
       
   407     }
       
   408 
       
   409 // --------------------------------------------------------------------------
       
   410 // CPbk2StoreInfoItemBuilder::CreateUiItemLC
       
   411 // --------------------------------------------------------------------------
       
   412 //
       
   413 MPbk2StoreInfoUiItem* CPbk2StoreInfoItemBuilder::CreateUiItemLC
       
   414         ( const CPbk2StoreProperty* aProperty,
       
   415           const CPbk2StoreInfoItem& aItem, TInt aIntValue )
       
   416     {
       
   417     CUiItem* uiItem = new(ELeave) CUiItem;
       
   418     CleanupStack::PushL(uiItem);
       
   419 
       
   420     HBufC* itemValue = FormatUiItemValueL(aItem, aIntValue);
       
   421     uiItem->SetItemText(itemValue); // takes ownership
       
   422 
       
   423     HBufC* heading = CreateHeadingL(aProperty, aItem, aIntValue);
       
   424     uiItem->SetHeadingText( heading ); // takes ownership
       
   425 
       
   426     return uiItem;
       
   427     }
       
   428 
       
   429 // --------------------------------------------------------------------------
       
   430 // CPbk2StoreInfoItemBuilder::FormatUiItemValueL
       
   431 // --------------------------------------------------------------------------
       
   432 //
       
   433 HBufC* CPbk2StoreInfoItemBuilder::FormatUiItemValueL
       
   434         ( const CPbk2StoreInfoItem& aItem,  TInt aValue )
       
   435     {
       
   436     HBufC* result = NULL;
       
   437 
       
   438     TUint memoryItemTypes =
       
   439         KPbk2StoreInfoFreeMemory | KPbk2StoreInfoReservedMemory;
       
   440     if (aItem.ItemType() & memoryItemTypes)
       
   441         {
       
   442         result = StringLoader::LoadL(R_QTN_SIZE_KB, aValue);
       
   443         }
       
   444     else
       
   445         {
       
   446         TBuf<KMaxIntSize> intBuf;
       
   447         intBuf.Num(aValue);
       
   448         result = intBuf.AllocL();
       
   449         }
       
   450 
       
   451     return result;
       
   452     }
       
   453 
       
   454 // --------------------------------------------------------------------------
       
   455 // CPbk2StoreInfoItemBuilder::CreateHeadingL
       
   456 // --------------------------------------------------------------------------
       
   457 //
       
   458 HBufC* CPbk2StoreInfoItemBuilder::CreateHeadingL
       
   459         ( const CPbk2StoreProperty* aProperty,
       
   460           const CPbk2StoreInfoItem& aItem, TInt aValue )
       
   461     {
       
   462     HBufC* retVal = NULL;
       
   463 
       
   464     const TInt singleValue = 1;
       
   465 
       
   466     if ( aItem.TextType() == EPbk2StoreInfoStoreName && aProperty )
       
   467         {
       
   468         CPbk2Content* content = aProperty->RetrieveContentLC
       
   469             ( EPbk2MemInfoContacts );
       
   470 
       
   471         CPbk2StorePropertyContentStoreName* propContent =
       
   472             dynamic_cast<CPbk2StorePropertyContentStoreName*>( content );
       
   473 
       
   474         HBufC* storeName = NULL;
       
   475         if (propContent && propContent->StoreName() != KNullDesC())
       
   476             {
       
   477             storeName = propContent->StoreName().AllocL();
       
   478             }
       
   479         else
       
   480             {
       
   481             // Backup plan -> use store name
       
   482             storeName = aProperty->StoreName().AllocL();
       
   483             }
       
   484 
       
   485         CleanupStack::PopAndDestroy( content );
       
   486         retVal = storeName;
       
   487         }
       
   488 
       
   489     else
       
   490         {
       
   491         retVal = aItem.ItemTextPlural().AllocL();
       
   492         }
       
   493 
       
   494     return retVal;
       
   495     }
       
   496 
       
   497 // End of File