phonebookui/Phonebook/View/src/PbkIconInfo.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *       Provides methods for Phonebook Icon info class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "PbkIconInfo.h"
       
    22 #include <barsread.h>
       
    23 #include <coemain.h>
       
    24 #include <gulicon.h>
       
    25 #include <PbkView.rsg>
       
    26 #include <AknsUtils.h>
       
    27 #include <aknenv.h>
       
    28 
       
    29 // unnamed namespace
       
    30 namespace {
       
    31 
       
    32 /**
       
    33  * Creates a resource reader.
       
    34  * 
       
    35  * @aReader will contain filled reader
       
    36  * @aResourceId resource to be loaded
       
    37  * @aCoeEnv coe env to be used for resource reading
       
    38  */
       
    39 void CreateReaderLC
       
    40         (TResourceReader& aReader, 
       
    41         TInt aResourceId, 
       
    42         CCoeEnv* aCoeEnv = NULL)
       
    43     {
       
    44     CCoeEnv* coeEnv = aCoeEnv;
       
    45     if (!coeEnv)
       
    46         {
       
    47         coeEnv = CCoeEnv::Static();
       
    48         }
       
    49     coeEnv->CreateResourceReaderLC(aReader, aResourceId);
       
    50     }
       
    51 }
       
    52 
       
    53 // ==================== MEMBER FUNCTIONS ====================
       
    54 
       
    55 EXPORT_C TPbkIconId TPbkIconInfo::PbkIconInfoId() const 
       
    56     { 
       
    57     return iPbkIconInfoId; 
       
    58     }
       
    59 
       
    60 EXPORT_C const TDesC& TPbkIconInfo::MbmFileName() const 
       
    61     { 
       
    62     return *iMbmFileName; 
       
    63     }
       
    64 
       
    65 EXPORT_C TInt TPbkIconInfo::IconId() const 
       
    66     { 
       
    67     return iIconId;
       
    68     }
       
    69 
       
    70 EXPORT_C TInt TPbkIconInfo::MaskId() const 
       
    71     { 
       
    72     return iMaskId;
       
    73     }
       
    74 
       
    75 EXPORT_C TAknsItemID TPbkIconInfo::SkinId() const
       
    76     {
       
    77     return iSkinId;
       
    78     }
       
    79 
       
    80 TAknsItemID TPbkIconInfo::ColorId() const
       
    81     {
       
    82     return iColorId;
       
    83     }
       
    84 
       
    85 TInt TPbkIconInfo::ColorIndex() const
       
    86     {
       
    87     return iColorIndex;
       
    88     }
       
    89 
       
    90 TInt TPbkIconInfo::DefaultColorIndex() const
       
    91     {
       
    92     return iDefaultColorIndex;
       
    93     }
       
    94 
       
    95 inline TPbkIconInfo::TPbkIconInfo()
       
    96     {
       
    97     // Nothing needs to be intialised here.
       
    98     // All fields are intialised by CPbkIconInfoContainer::ConstructL() before
       
    99     // this object can be used.
       
   100     }
       
   101 
       
   102 inline TPbkIconInfo::~TPbkIconInfo()
       
   103     {
       
   104     // Destroy() handles releasing resources of this class
       
   105     }
       
   106 
       
   107 inline void TPbkIconInfo::ReadFromResourceLC(TResourceReader& aReader)
       
   108     {
       
   109     iPbkIconInfoId = static_cast<TPbkIconId>(aReader.ReadInt8());
       
   110 	HBufC* buf = aReader.ReadHBufCL();
       
   111     CleanupStack::PushL(buf);
       
   112     iMbmFileName = buf;
       
   113     iIconId = aReader.ReadInt32();
       
   114     iMaskId = aReader.ReadInt32();
       
   115     TInt skinIdMajor = aReader.ReadInt32();
       
   116     TInt skinIdMinor = aReader.ReadInt32();
       
   117     iSkinId.Set(skinIdMajor, skinIdMinor);
       
   118     TInt colorIdMajor = aReader.ReadInt32();
       
   119     TInt colorIdMinor = aReader.ReadInt32();
       
   120     iColorId.Set(colorIdMajor, colorIdMinor);
       
   121     iColorIndex = aReader.ReadInt32();
       
   122     iDefaultColorIndex = aReader.ReadInt32();
       
   123     }
       
   124 
       
   125 inline void TPbkIconInfo::Destroy()
       
   126     {
       
   127     // delete data members
       
   128     delete iMbmFileName;
       
   129     iMbmFileName = NULL;
       
   130     }
       
   131 
       
   132 inline CPbkIconInfoContainer::CPbkIconInfoContainer()
       
   133     {
       
   134     // CBase::operator new(TLeave) resets member data
       
   135     }
       
   136 
       
   137 inline void CPbkIconInfoContainer::ConstructL(TResourceReader& aReader)
       
   138     {
       
   139     DoAppendIconsFromResourceL(aReader);
       
   140     }
       
   141 
       
   142 EXPORT_C CPbkIconInfoContainer* CPbkIconInfoContainer::NewL
       
   143         (CCoeEnv* aCoeEnv /*=NULL*/)
       
   144     {
       
   145     return NewL(R_PBK_ICON_INFO_ARRAY, aCoeEnv);
       
   146     }
       
   147 
       
   148 EXPORT_C CPbkIconInfoContainer* CPbkIconInfoContainer::NewL
       
   149         (TResourceReader& aReader)
       
   150     {
       
   151     CPbkIconInfoContainer* self = new(ELeave) CPbkIconInfoContainer;
       
   152     CleanupStack::PushL(self);
       
   153     self->ConstructL(aReader);
       
   154     CleanupStack::Pop(self);
       
   155     return self;
       
   156     }
       
   157 
       
   158 EXPORT_C CPbkIconInfoContainer* CPbkIconInfoContainer::NewL
       
   159         (TInt aResourceId, CCoeEnv* aCoeEnv /*=NULL*/)
       
   160     {
       
   161     CPbkIconInfoContainer* self = new(ELeave) CPbkIconInfoContainer;
       
   162     CleanupStack::PushL(self);
       
   163     TResourceReader reader;
       
   164     CreateReaderLC(reader, aResourceId, aCoeEnv);
       
   165     self->ConstructL(reader);
       
   166     CleanupStack::PopAndDestroy(); // reader (buffer)
       
   167     CleanupStack::Pop(self);
       
   168     return self;
       
   169     }
       
   170 
       
   171 CPbkIconInfoContainer::~CPbkIconInfoContainer()
       
   172     {
       
   173     // close and delete data members
       
   174     for (TInt i = iIconArray.Count()-1; i >= 0; --i)
       
   175         {
       
   176         iIconArray[i].Destroy();
       
   177         }
       
   178     iIconArray.Close();
       
   179     }
       
   180 
       
   181 EXPORT_C const TPbkIconInfo* CPbkIconInfoContainer::Find
       
   182         (TPbkIconId aIconId) const
       
   183     {
       
   184     const TInt count = iIconArray.Count();
       
   185     for (TInt i = 0; i < count; ++i)
       
   186         {
       
   187         if (iIconArray[i].iPbkIconInfoId == aIconId)
       
   188             {
       
   189             return &iIconArray[i];
       
   190             }
       
   191         }
       
   192     return NULL;
       
   193     }
       
   194 
       
   195 EXPORT_C CGulIcon* CPbkIconInfoContainer::LoadBitmapL
       
   196         (TPbkIconId aIconId) const
       
   197     {
       
   198     const TPbkIconInfo* iconInfo = Find(aIconId);
       
   199     if (iconInfo)
       
   200         {
       
   201         CGulIcon* icon = CGulIcon::NewLC();
       
   202         icon->SetBitmapsOwnedExternally(EFalse);
       
   203 
       
   204         CFbsBitmap* bitmap = NULL;
       
   205         CFbsBitmap* mask = NULL;
       
   206 
       
   207         PbkIconUtils::CreateIconL(AknsUtils::SkinInstance(), bitmap, mask, *iconInfo);
       
   208 
       
   209         icon->SetBitmap(bitmap);
       
   210         icon->SetMask(mask);
       
   211 
       
   212         CleanupStack::Pop(); // icon
       
   213 
       
   214         return icon;
       
   215         }
       
   216     return NULL;
       
   217     }
       
   218 
       
   219 EXPORT_C void CPbkIconInfoContainer::AppendIconsFromResourceL
       
   220         (TInt aResourceId)
       
   221     {
       
   222     TResourceReader reader;
       
   223     CreateReaderLC(reader, aResourceId);
       
   224     DoAppendIconsFromResourceL(reader);
       
   225     CleanupStack::PopAndDestroy(); // reader
       
   226     }
       
   227 
       
   228 /**
       
   229  * Appends icon infor to this container.
       
   230  *
       
   231  * @param aReader resource to be added to this info container
       
   232  */
       
   233 void CPbkIconInfoContainer::DoAppendIconsFromResourceL
       
   234         (TResourceReader& aReader)
       
   235     {
       
   236     // read from resource
       
   237     TInt count = aReader.ReadInt16();
       
   238     while (count-- > 0)
       
   239         {
       
   240         TPbkIconInfo iconInfo;
       
   241         iconInfo.ReadFromResourceLC(aReader);
       
   242         
       
   243         TPbkIconInfo* info = const_cast<TPbkIconInfo*>
       
   244             (Find(iconInfo.PbkIconInfoId()));
       
   245         if ( !info )
       
   246             {
       
   247             // add if icon is not found
       
   248             User::LeaveIfError(iIconArray.Append(iconInfo));
       
   249             }
       
   250         else
       
   251             {
       
   252             // if icon is found, update the data
       
   253             info->iIconId = iconInfo.iIconId;
       
   254             info->iMaskId = iconInfo.iMaskId;
       
   255             info->iSkinId = iconInfo.iSkinId;
       
   256 
       
   257             delete info->iMbmFileName;
       
   258             info->iMbmFileName = iconInfo.iMbmFileName;
       
   259             }
       
   260 
       
   261         CleanupStack::Pop(); // iconInfo
       
   262         }
       
   263     }
       
   264 
       
   265 EXPORT_C void PbkIconUtils::CreateIconL(
       
   266         MAknsSkinInstance* aSkin, 
       
   267         CFbsBitmap*& aBitmap, 
       
   268         CFbsBitmap*& aMask, 
       
   269         const TPbkIconInfo& aIconInfo)
       
   270     {
       
   271     CreateIconLC(aSkin, aBitmap, aMask, aIconInfo);
       
   272     CleanupStack::Pop(2); // bitmap + mask
       
   273     }
       
   274 
       
   275 EXPORT_C void PbkIconUtils::CreateIconLC(
       
   276         MAknsSkinInstance* aSkin, 
       
   277         CFbsBitmap*& aBitmap, 
       
   278         CFbsBitmap*& aMask, 
       
   279         const TPbkIconInfo& aIconInfo)
       
   280     {
       
   281     if (aIconInfo.ColorId() == KAknsIIDNone)
       
   282         {
       
   283         AknsUtils::CreateIconLC
       
   284             (aSkin, aIconInfo.SkinId(), aBitmap, aMask, 
       
   285              aIconInfo.MbmFileName(), 
       
   286              aIconInfo.IconId(), aIconInfo.MaskId());
       
   287         }
       
   288     else
       
   289         {
       
   290         AknsUtils::CreateColorIconLC
       
   291             (aSkin, aIconInfo.SkinId(), 
       
   292              aIconInfo.ColorId(), aIconInfo.ColorIndex(),
       
   293              aBitmap, aMask, 
       
   294              aIconInfo.MbmFileName(), 
       
   295              aIconInfo.IconId(), aIconInfo.MaskId(),
       
   296              AKN_LAF_COLOR_STATIC(aIconInfo.DefaultColorIndex()));
       
   297         }
       
   298     }
       
   299 
       
   300 
       
   301 // End of File