locationlandmarksrefappfors60/Src/LandmarksCategoriesModel.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2004-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:  
       
    15 *       Implements the CLandmarksCategoriesModel class and some help functions
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <eikenv.h>
       
    22 #include <gulicon.h>
       
    23 #include <AknsUtils.h>
       
    24 #include <AknsItemID.h>
       
    25 
       
    26 #include <EPos_CPosLandmarkCategory.h>
       
    27 
       
    28 #include <lmrefapp.mbg>
       
    29 #include "LandmarksCommonData.h"
       
    30 #include "LandmarksCategoriesModel.h"
       
    31 
       
    32 // ============================= LOCAL FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // DefaultIconIndex returns the icon index of the appropriate default icon.
       
    36 // User defined categories have different default icons than global ones.
       
    37 //
       
    38 // Returns: an icon index
       
    39 // Param:   aCategory the category to investigate
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 TInt DefaultIconIndex(CPosLandmarkCategory& aCategory)
       
    43     {
       
    44     // Global categories have different default icon than user-defined.
       
    45     TBool isGlobal = aCategory.GlobalCategory() != KPosLmNullGlobalCategory;
       
    46 
       
    47     if (isGlobal)
       
    48         {
       
    49         return KDefaultGlobalIconIndex;
       
    50         }
       
    51     else
       
    52         {
       
    53         return KDefaultUserIconIndex;
       
    54         }
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // ResetAndDestroyIcons
       
    59 // Resets and destroys an array of icons. This function is used as a 
       
    60 // TCleanupOperation.
       
    61 // Params: aAny: The icons array to reset ands destroy
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void ResetAndDestroyIcons(TAny* aAny)
       
    65     {
       
    66     CIconList* icons = reinterpret_cast <CIconList*> (aAny);
       
    67     for (TInt i = 0; i < icons->Count(); i++)
       
    68         {
       
    69         delete &icons[i];
       
    70         }
       
    71     icons->Reset();
       
    72     }
       
    73 
       
    74 // ============================ MEMBER FUNCTIONS ===============================
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 CLandmarksCategoriesModel::CLandmarksCategoriesModel()
       
    80 	{
       
    81 	}
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CLandmarksCategoriesModel::ConstructL()
       
    87 	{
       
    88 	// The list box model uses MDesCArray interface class to take in text items.
       
    89 	iListItems = new(ELeave) CDesCArraySeg(KGranularity);
       
    90 	}
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 CLandmarksCategoriesModel* CLandmarksCategoriesModel::NewL()
       
    96     {
       
    97     CLandmarksCategoriesModel* self = 
       
    98         new (ELeave) CLandmarksCategoriesModel();
       
    99     CleanupStack::PushL(self);
       
   100     self->ConstructL();
       
   101     CleanupStack::Pop(self);
       
   102     return self;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 CLandmarksCategoriesModel::~CLandmarksCategoriesModel()
       
   109 	{
       
   110 	delete iListItems;
       
   111 	}
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CLandmarksCategoriesModel::SetItemIds(RArray<TPosLmItemId>* aItemIds)
       
   117     {
       
   118     iItemIds = aItemIds;
       
   119 
       
   120     // Empty descriptor model
       
   121     iListItems->Reset();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 CDesCArraySeg* CLandmarksCategoriesModel::ItemList()
       
   128     {
       
   129     return iListItems;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CLandmarksCategoriesModel::SetCurrentItem(TInt aIndex) 
       
   136     {
       
   137     iCurrentItem = aIndex;
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 TPosLmItemId CLandmarksCategoriesModel::CurrentItemId()
       
   144     {
       
   145     if (iCurrentItem < 0 || iCurrentItem > iItemIds->Count() - 1)
       
   146         {
       
   147         return KPosLmNullItemId;
       
   148         }
       
   149 
       
   150     return (*iItemIds)[iCurrentItem];
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 CIconList* CLandmarksCategoriesModel::CreateIconListL()
       
   157     {
       
   158     CIconList* iconList = new (ELeave) CArrayPtrFlat<CGulIcon>(KGranularity);
       
   159     CleanupStack::PushL(TCleanupItem(ResetAndDestroyIcons, iconList));
       
   160     
       
   161     // Create the global category icon consisting of its image and mask.
       
   162     TAknsItemID aknsItemId ={0,0};
       
   163     CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
       
   164         KLandmarksMbmFileName, 
       
   165         EMbmLmrefappCategory_global, 
       
   166         EMbmLmrefappCategory_global_mask);
       
   167     CleanupStack::PushL(icon);
       
   168     icon->SetBitmapsOwnedExternally(EFalse); 
       
   169     iconList->AppendL(icon);
       
   170     CleanupStack::Pop(icon);
       
   171 
       
   172     // Create the user defined category icon consisting of its image and mask.
       
   173     icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
       
   174         KLandmarksMbmFileName, 
       
   175         EMbmLmrefappCategory_user, 
       
   176         EMbmLmrefappCategory_user_mask);
       
   177     CleanupStack::PushL(icon);
       
   178     icon->SetBitmapsOwnedExternally(EFalse); 
       
   179     iconList->AppendL(icon);
       
   180     CleanupStack::Pop(icon);
       
   181     
       
   182     CleanupStack::Pop(); // iconList
       
   183 
       
   184     // initialze iIconList. We don't need to delete any possible previous 
       
   185     // iIconList since ownership is transferred to calling object.
       
   186     iIconList = iconList;
       
   187 
       
   188     return iconList;
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // Refreshes a part of the model. Those categories that have been read from the 
       
   193 // database overwrites old categories. The model is refreshed incrementally 
       
   194 // from top to bottom.
       
   195 // (other items were commented in a header).
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 void CLandmarksCategoriesModel::RepopulateModelL(
       
   199     CArrayPtr<CPosLandmarkCategory>* aCategories)
       
   200     {
       
   201     TInt nrOfReadItems = aCategories->Count();
       
   202 
       
   203     // Format and insert the read categories
       
   204     for (TInt i = 0; i < nrOfReadItems; i++)
       
   205         {
       
   206         // Format name and icon
       
   207         CPosLandmarkCategory* category = (*aCategories)[i];
       
   208         HBufC* formattedName = FormatListItemLC(*category);
       
   209 
       
   210         // Insert formatted descriptor into model
       
   211         iListItems->AppendL(*formattedName);
       
   212 
       
   213         CleanupStack::PopAndDestroy(formattedName);
       
   214         }
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // Formats a list item to the following format "X\tcategoryName"
       
   219 // where X = icon index
       
   220 // (other items were commented in a header).
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 HBufC* CLandmarksCategoriesModel::FormatListItemLC(CPosLandmarkCategory& aCategory)
       
   224     {
       
   225     // Extract name
       
   226     TPtrC name;
       
   227     aCategory.GetCategoryName(name);
       
   228     // 10 extra chars for icon index and column separator
       
   229     const TInt KExtraChars = 10;
       
   230     HBufC* formattedNameBuf = HBufC::NewLC(name.Length() + KExtraChars);
       
   231     TPtr formattedNamePtr = formattedNameBuf->Des();
       
   232 
       
   233     // Extract icon info
       
   234     TPtrC mbmIconFile;
       
   235     TInt iconIndex, maskIndex;
       
   236     TInt res = aCategory.GetIcon(mbmIconFile, iconIndex, maskIndex);
       
   237 
       
   238     // Format list item
       
   239     if (res == KErrNone)
       
   240         {
       
   241         // Create icon and append it to icon array. 
       
   242         TRAPD(err, AppendIconL(mbmIconFile, iconIndex, maskIndex));
       
   243         if (err == KErrNone)
       
   244             {
       
   245             // Append the array index where the icon is appended
       
   246             formattedNamePtr.AppendNum(iIconList->Count() - 1);
       
   247             }
       
   248         else // Unable to create icon, use appropriate default icon.
       
   249             {
       
   250             formattedNamePtr.AppendNum(DefaultIconIndex(aCategory));
       
   251             }
       
   252         }
       
   253     else // res == KErrNotFound
       
   254         {
       
   255         formattedNamePtr.AppendNum(DefaultIconIndex(aCategory));
       
   256         }
       
   257     formattedNamePtr.Append(KTab);
       
   258     formattedNamePtr.Append(name);
       
   259 
       
   260     return formattedNameBuf;
       
   261     }
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 void CLandmarksCategoriesModel::AppendIconL(
       
   267     const TDesC& aMbmFile, 
       
   268     TInt aIconOffset, 
       
   269     TInt aMaskOffset)
       
   270     {
       
   271     TAknsItemID aknsItemId = {0,0};
       
   272     CGulIcon* icon = AknsUtils::CreateGulIconL(NULL, aknsItemId,
       
   273         aMbmFile, 
       
   274         aIconOffset, 
       
   275         aMaskOffset);
       
   276     CleanupStack::PushL(icon);
       
   277     icon->SetBitmapsOwnedExternally(EFalse);
       
   278     iIconList->AppendL(icon);
       
   279     CleanupStack::Pop(icon);
       
   280     }
       
   281 
       
   282