locationlandmarksrefappfors60/Src/LandmarksLmCategoriesModel.cpp
author Pat Downey <patd@symbian.org>
Wed, 01 Sep 2010 12:31:27 +0100
branchRCL_3
changeset 18 870918037e16
parent 0 522cd55cc3d7
permissions -rw-r--r--
Revert incorrect RCL_3 drop: Revision: 201033 Kit: 201035

/*
* Copyright (c) 2004-2005 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*       Implements the CLandmarksLmCategoriesModel class
*
*/



#include <eikenv.h>
#include <gulicon.h>
#include <AknIconArray.h>
#include <avkon.mbg>

#include <EPos_CPosLandmarkCategory.h>

#include "LandmarksUtils.h"
#include "LandmarksCommonData.h"
#include "LandmarksLmCategoriesModel.h"
#include "LandmarksApplicationEngine.h"


_LIT(KItemFormat, "\t%S");

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
CLandmarksLmCategoriesModel::CLandmarksLmCategoriesModel(
    RArray<TPosLmItemId>& aMarkedCategories)
:   iMarkedCategoryIds(aMarkedCategories)
	{
	}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void CLandmarksLmCategoriesModel::ConstructL(CLandmarksApplicationEngine& aEngine)
	{
    iCategories = aEngine.CategoriesL();
	}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
CLandmarksLmCategoriesModel* CLandmarksLmCategoriesModel::NewL(
    CLandmarksApplicationEngine& aEngine,
    RArray<TPosLmItemId>& aMarkedCategories)
    {
    CLandmarksLmCategoriesModel* self = new (ELeave) 
        CLandmarksLmCategoriesModel(aMarkedCategories);
    CleanupStack::PushL(self);
    self->ConstructL(aEngine);
    CleanupStack::Pop(self);
    return self;
    }

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
CLandmarksLmCategoriesModel::~CLandmarksLmCategoriesModel()
	{
    if (iCategories)
        {
        iCategories->ResetAndDestroy();
	    delete iCategories;
        }
	}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
void CLandmarksLmCategoriesModel::SelectCategoriesL(
    CArrayFixFlat<TInt>& aSelectedIndexes) 
    {
    // clear current list of selected categories
    iMarkedCategoryIds.Reset();

    for (TInt i = 0; i < aSelectedIndexes.Count(); i++)
        {
        TInt categoryIndex = aSelectedIndexes[i];

        // Fetch the id of the selected item.
        __ASSERT_ALWAYS((categoryIndex >= 0) && (categoryIndex < iCategories->Count()),
                        LandmarksUtils::Panic(KErrGeneral));
        TPosLmItemId selectedCategoryId = (*iCategories)[categoryIndex]->CategoryId();

        User::LeaveIfError(iMarkedCategoryIds.Append(selectedCategoryId));
        }
    }

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
CArrayFixFlat<TInt>* CLandmarksLmCategoriesModel::SelectedCategoriesL() 
    {
    CArrayFixFlat<TInt>* selectedItems = new (ELeave) CArrayFixFlat<TInt>(KGranularity);
    CleanupStack::PushL(selectedItems);

    for (TInt i = 0; i < iCategories->Count(); i++)
        {
        TPosLmItemId categoryId = (*iCategories)[i]->CategoryId();

		TInt index = iMarkedCategoryIds.Find(categoryId);
        if (KErrNotFound != index)
        	{
	        selectedItems->AppendL(i);
        	}
        }

	CleanupStack::Pop(selectedItems);        
	return selectedItems;        
    }

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
//
CDesCArray* CLandmarksLmCategoriesModel::FormattedCategoryNamesL()
    {
    // Granularity is set to be 8.
    CDesCArrayFlat* listItems = new (ELeave) CDesCArrayFlat(KGranularity);
    CleanupStack::PushL(listItems);

    for (TInt i = 0; i < iCategories->Count(); i++)
        {
        // Create buffer that will contain the name and mark status. 3 extra 
        // chars for tab and icon index.
        const TInt KExtraChars = 3; 
        TPtrC categoryName;
        User::LeaveIfError((*iCategories)[i]->GetCategoryName(categoryName));
        HBufC* formattedCategoryName = HBufC::NewLC(categoryName.Length() + KExtraChars);

        // format string with category
        formattedCategoryName->Des().Format(KItemFormat, &categoryName);

        listItems->AppendL(*formattedCategoryName);
        CleanupStack::PopAndDestroy(formattedCategoryName);
        }

    CleanupStack::Pop(listItems);

    iListItems = listItems;
    return iListItems;
    }