javaextensions/pim/javapimloc/src.s60/cpimlocalizationdata.cpp
changeset 21 2a9601315dfc
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2004-2006 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:  Base class for all localization data objects.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // CLASS HEADER
       
    20 #include "cpimlocalizationdata.h"
       
    21 
       
    22 // INTERNAL INCLUDES
       
    23 #include "cpimlabelprovider.h"
       
    24 #include "pimpanics.h"
       
    25 
       
    26 // EXTERNAL INCLUDES
       
    27 #include <bautils.h>
       
    28 #include <barsc.h>
       
    29 #include <barsread.h>
       
    30 
       
    31 // UNNAMED LOCAL NAMESPACE
       
    32 namespace
       
    33 {
       
    34 // Number of resource links in the PIM_LIST resource structure
       
    35 const TInt KPIMNumListResourceLinks = 3;
       
    36 }
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 // -----------------------------------------------------------------------------
       
    40 // CPIMLocalizationData::NewL
       
    41 // Two-phased constructor.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CPIMLocalizationData* CPIMLocalizationData::NewL(
       
    45     RResourceFile& aResourceFile,
       
    46     TResourceReader& aReader,
       
    47     TInt aSubType)
       
    48 {
       
    49     CPIMLocalizationData* self =
       
    50         CPIMLocalizationData::NewLC(aResourceFile, aReader, aSubType);
       
    51     CleanupStack::Pop(self);
       
    52     return self;
       
    53 }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CPIMLocalizationData::NewL
       
    57 // Two-phased constructor. Created item is left to the cleanup stack
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CPIMLocalizationData* CPIMLocalizationData::NewLC(
       
    61     RResourceFile& aResourceFile,
       
    62     TResourceReader& aReader,
       
    63     TInt aSubType)
       
    64 {
       
    65     CPIMLocalizationData* self = new(ELeave) CPIMLocalizationData(aSubType);
       
    66     CleanupStack::PushL(self);
       
    67     self->ConstructFromResourceL(aResourceFile, aReader);
       
    68     return self;
       
    69 }
       
    70 
       
    71 // Destructor
       
    72 CPIMLocalizationData::~CPIMLocalizationData()
       
    73 {
       
    74     iFields.ResetAndDestroy();
       
    75     iAttributes.ResetAndDestroy();
       
    76     delete iListName;
       
    77 }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CPIMLocalizationData::GetFieldLabelL
       
    81 // Provides a string label associated with the given field. The caller
       
    82 // takes ownership of the returned object.
       
    83 // Returns: String label for the field. The label is locale specific.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 HBufC* CPIMLocalizationData::GetFieldLabelL(TPIMField aField)
       
    87 {
       
    88     TInt count(iFields.Count());
       
    89     for (TInt i(0); i < count; i++)
       
    90     {
       
    91         // Search through each label provider and ask field label. Null means
       
    92         // that the provider does not have label for this id. Try next
       
    93         HBufC* label = iFields[ i ]->LabelL(aField);
       
    94         if (label)
       
    95         {
       
    96             // Found label for this id, return it
       
    97             return label;
       
    98         }
       
    99     }
       
   100     return NULL;
       
   101 }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CPIMLocalizationData::GetAttributeLabelL
       
   105 // Provides a string label associated with the given attribute. The caller
       
   106 // takes ownership of the returned object.
       
   107 // Returns: String label for the attribute. The label is locale specific.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 HBufC* CPIMLocalizationData::GetAttributeLabelL(TPIMAttribute aAttribute)
       
   111 {
       
   112     TInt count(iAttributes.Count());
       
   113     for (TInt i(0); i < count; i++)
       
   114     {
       
   115         // Search through each label provider and ask attribute. Null means
       
   116         // that the provider does not have label for this id. Try next
       
   117         HBufC* label = iAttributes[ i ]->LabelL(aAttribute);
       
   118         if (label)
       
   119         {
       
   120             // Found label for this id, return it
       
   121             return label;
       
   122         }
       
   123     }
       
   124     return NULL;
       
   125 }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CPIMLocalizationData::GetArrayElementLabelL
       
   129 // Provides a string label associated with the given array element. The caller
       
   130 // takes ownership of the returned object.
       
   131 // Returns: String label for the array element. The label is locale specific.
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 HBufC* CPIMLocalizationData::GetArrayElementLabelL(
       
   135     TPIMField aStringArrayField,
       
   136     TPIMArrayElement aArrayElement)
       
   137 {
       
   138     TInt count(iFields.Count());
       
   139     for (TInt i(0); i < count; i++)
       
   140     {
       
   141         // Search through each label provider and ask attribute label. Null means
       
   142         // that the provider does not have label for this id. Try next
       
   143         HBufC* label = iFields[ i ]->LabelL(aStringArrayField, aArrayElement);
       
   144         if (label)
       
   145         {
       
   146             // Found label for this id, return it
       
   147             return label;
       
   148         }
       
   149     }
       
   150     return NULL;
       
   151 }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // CPIMLocalizationData::GetListNameL
       
   155 // Provides the name of the list. The caller
       
   156 // takes ownership of the returned object.
       
   157 // Returns: the list name
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 HBufC* CPIMLocalizationData::GetListNameL()
       
   161 {
       
   162     __ASSERT_DEBUG(iListName,
       
   163                    User::Panic(KPIMPanicCategory, EPIMPanicGeneral));
       
   164     return iListName->AllocL();
       
   165 }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CPIMLocalizationData::CPIMLocalizationData
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 CPIMLocalizationData::CPIMLocalizationData(TInt aSubType)
       
   172         : iSubType(aSubType)
       
   173 {
       
   174 }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CPIMLocalizationData::ConstructFromResourceL
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CPIMLocalizationData::ConstructFromResourceL(
       
   181     RResourceFile& aResourceFile,
       
   182     TResourceReader& aReader)
       
   183 {
       
   184     TInt listCount(aReader.ReadInt16());
       
   185 
       
   186     __ASSERT_DEBUG(listCount > iSubType,
       
   187                    User::Panic(KPIMPanicCategory, EPIMPanicGeneral));
       
   188 
       
   189     // Find the correct resource structure for the requested sub-type. If
       
   190     // the list type is incorrect the reader skips the incorrect resource
       
   191     // structure and tries the next one until no lists can be processed.
       
   192     for (TInt i(1); i <= listCount; i++)
       
   193     {
       
   194         TInt listType(aReader.ReadInt8());
       
   195         if (listType == iSubType)
       
   196         {
       
   197             ReadListInfoFromResourceL(aResourceFile, aReader);
       
   198             break;
       
   199         }
       
   200         else if (i < listCount)
       
   201         {
       
   202             // Advance in resource file since this wasn't the list which
       
   203             // was requested. Currently there are three LLINK:s to skip
       
   204             aReader.Advance(sizeof(TInt32) * KPIMNumListResourceLinks);
       
   205         }
       
   206     }
       
   207 }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CPIMLocalizationData::ReadListInfoFromResourceL
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 void CPIMLocalizationData::ReadListInfoFromResourceL(
       
   214     RResourceFile& aResourceFile,
       
   215     TResourceReader& aReader)
       
   216 {
       
   217     TInt32 listNameLabelId(aReader.ReadInt32());
       
   218     TInt32 fieldsId(aReader.ReadInt32());
       
   219     TInt32 attrsId(aReader.ReadInt32());
       
   220 
       
   221     // Read label from resource
       
   222     aReader.SetBuffer(aResourceFile.AllocReadLC(listNameLabelId));
       
   223     iListName = aReader.ReadHBufCL();
       
   224     // Resource buffer is not needed anymore
       
   225     CleanupStack::PopAndDestroy(); // AllocReadLC()
       
   226 
       
   227     // Read fields from resource. Fields must be specified in the resource
       
   228     // file since empty lists are not allowed.
       
   229     aReader.SetBuffer(aResourceFile.AllocReadLC(fieldsId));
       
   230 
       
   231     TInt count(aReader.ReadInt16());
       
   232     for (TInt i(0); i < count; i++)
       
   233     {
       
   234         CPIMLabelProvider* provider = CPIMLabelProvider::NewLC(aReader);
       
   235         iFields.AppendL(provider);
       
   236         CleanupStack::Pop(provider);
       
   237     }
       
   238     // Resource buffer is not needed anymore
       
   239     CleanupStack::PopAndDestroy(); // AllocReadLC()
       
   240 
       
   241     // Read attributes from resource. Do nothing if attributes not specified
       
   242     if (attrsId != 0)
       
   243     {
       
   244         // Create buffer to the attributes lists
       
   245         aReader.SetBuffer(aResourceFile.AllocReadLC(attrsId));
       
   246 
       
   247         count = aReader.ReadInt16();
       
   248         for (TInt i(0); i < count; i++)
       
   249         {
       
   250             CPIMLabelProvider* provider = CPIMLabelProvider::NewLC(aReader);
       
   251             iAttributes.AppendL(provider);
       
   252             CleanupStack::Pop(provider);
       
   253         }
       
   254         // Resource buffer is not needed anymore
       
   255         CleanupStack::PopAndDestroy(); // AllocReadLC()
       
   256     }
       
   257 }
       
   258 
       
   259 // End of file