locationlandmarksrefappfors60/Src/LandmarksCategoriesContainer.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:  Implements the CLandmarksCategoriesContainer class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <aknsfld.h>
       
    21 #include <AknQueryDialog.h>
       
    22 #include <eikclbd.h>
       
    23 #include <eikmenub.h>
       
    24 
       
    25 #include <EPos_CPosLandmarkCategory.h>
       
    26 
       
    27 #include <lmrefapp.rsg>
       
    28 #include "LandmarksCategoriesContainer.h"
       
    29 #include "LandmarksCommonData.h"
       
    30 #include "LandmarksListbox.h"
       
    31 #include "LandmarksListBoxModel.h"
       
    32 #include "LandmarksApplicationEngine.h"
       
    33 #include "LandmarksCategoriesModel.h"
       
    34 
       
    35 // ============================= LOCAL FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // ResetAndDestroyCategories
       
    39 // Resets and destroys an array of categories. This function is used as a 
       
    40 // TCleanupOperation in CLandmarksCategoriesModel.
       
    41 // Params: aAny: The categories array to reset ands destroy
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 void ResetAndDestroyCategories(TAny* aAny)
       
    45     {
       
    46     CArrayPtr<CPosLandmarkCategory>* categories = 
       
    47         reinterpret_cast <CArrayPtr<CPosLandmarkCategory>*> (aAny);
       
    48     categories->ResetAndDestroy();
       
    49     }
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CLandmarksCategoriesContainer::CLandmarksCategoriesContainer(
       
    57     CAknView& aView,
       
    58     CLandmarksApplicationEngine& aEngine, 
       
    59     CEikMenuBar& aMenuBar)
       
    60 :   CLandmarksContainerBase(aView, aEngine), 
       
    61     iMenuBar(aMenuBar)
       
    62     {
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 void CLandmarksCategoriesContainer::ConstructL(
       
    69     const TRect& aRect)
       
    70     {
       
    71     // Create listbox and findbox.
       
    72     CLandmarksContainerBase::ConstructL(aRect);
       
    73 
       
    74     // Start observer db events.
       
    75     iEngine.AddDbObserverL(this);
       
    76 
       
    77     // Create data model.
       
    78     iModel = CLandmarksCategoriesModel::NewL();
       
    79 
       
    80     // Create a descriptor array that will contain the landmark names and 
       
    81     // icon indeces.
       
    82     CLandmarksListBoxModel* listBoxModel = iListBox->Model();
       
    83     listBoxModel->SetItemTextArray(iModel->ItemList());
       
    84     listBoxModel->SetOwnershipType(ELbmDoesNotOwnItemArray);
       
    85 
       
    86     // Create an icon array the will be displayed in the listbox.
       
    87 
       
    88     CIconList* icons = iModel->CreateIconListL();
       
    89     iListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
       
    90 
       
    91     // Observe listbox for state changes.
       
    92     iListBox->SetObserver(this);
       
    93 
       
    94     // Observe findbox for state changes.
       
    95     iFindBox->SetObserver(this);
       
    96 
       
    97     // Populate listbox with categories and icons.
       
    98     StartUpdateListBoxL();
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 CLandmarksCategoriesContainer::~CLandmarksCategoriesContainer()
       
   105     {
       
   106     delete iModel;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 void CLandmarksCategoriesContainer::DeleteCategoryL()
       
   113     {
       
   114     // Format query.
       
   115     HBufC* queryFormat = 
       
   116         iCoeEnv->AllocReadResourceAsDes16LC(R_LMREFAPP_DELETE_CATEGORY);
       
   117 
       
   118     CPosLandmarkCategory* category = iEngine.CategoryLC(CurrentCategoryId());
       
   119     TPtrC name;
       
   120     category->GetCategoryName(name);
       
   121     
       
   122     HBufC* query = HBufC::NewLC(name.Length() + queryFormat->Length());
       
   123     query->Des().Format(*queryFormat, &name);
       
   124 
       
   125     // Launch dialog.
       
   126     CAknQueryDialog* queryDialog = CAknQueryDialog::NewL();
       
   127     if (queryDialog->ExecuteLD(R_LMREFAPP_GENERAL_QUERY, *query))
       
   128         {
       
   129         // Delete category if positive response.
       
   130         iEngine.DeleteCategoryL(category->CategoryId());
       
   131         }
       
   132     CleanupStack::PopAndDestroy(3, queryFormat);
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 TPosLmItemId CLandmarksCategoriesContainer::CurrentCategoryId()
       
   139     {
       
   140     return iModel->CurrentItemId();
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TBool CLandmarksCategoriesContainer::IsGlobalL()
       
   147     {
       
   148     CPosLandmarkCategory* category = iEngine.CategoryLC(CurrentCategoryId());
       
   149     TBool isGlobal = (category->GlobalCategory() != KPosLmNullGlobalCategory);
       
   150     CleanupStack::PopAndDestroy(category);
       
   151 
       
   152     return isGlobal;
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TKeyResponse CLandmarksCategoriesContainer::OfferKeyEventL(
       
   159     const TKeyEvent& aKeyEvent, TEventCode aType)
       
   160     {
       
   161 	TKeyResponse response = EKeyWasNotConsumed;
       
   162 
       
   163     switch (aKeyEvent.iCode)
       
   164         {
       
   165         case EKeyUpArrow:
       
   166         case EKeyDownArrow:
       
   167             // Scrolling the listbox. Forward to iListBox.
       
   168             response = iListBox->OfferKeyEventL(aKeyEvent, aType);
       
   169             break;
       
   170 
       
   171         case EKeyDevice3:
       
   172             // Select key pressed. Display menu.
       
   173             iMenuBar.SetMenuTitleResourceId(R_LMREFAPP_CATEGORIES_MENUBAR);
       
   174             iMenuBar.TryDisplayMenuBarL();
       
   175             response = EKeyWasConsumed;
       
   176             break;
       
   177 
       
   178         case EKeyBackspace:
       
   179             if (iFindBox->TextLength() == 0 &&
       
   180                 IsItemSelected() &&
       
   181                 !IsGlobalL())
       
   182                 {
       
   183                 // If there is no text in the search field backspace will 
       
   184                 // delete a category.
       
   185                 DeleteCategoryL();
       
   186                 response = EKeyWasConsumed;
       
   187                 break;
       
   188                 }
       
   189             // erase character in find box
       
   190         default:
       
   191             // Forward to findbox.
       
   192             response =  iFindBox->OfferKeyEventL(aKeyEvent, aType);
       
   193             break;
       
   194         }
       
   195 
       
   196     return response;
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 void CLandmarksCategoriesContainer::NotifyOperationProgressL(
       
   203     TOperation aOperation, 
       
   204     TInt /*aProgress*/, 
       
   205     TInt aErrorCode)
       
   206     {
       
   207     switch (aOperation)
       
   208         {
       
   209         case ECategorySearch:
       
   210             HandleSearchReadyL(aErrorCode);
       
   211             break;
       
   212 
       
   213         case ECategoryRead:
       
   214             HandleReadReadyL(aErrorCode);
       
   215             break;
       
   216 
       
   217         default:
       
   218             break;
       
   219         }
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // -----------------------------------------------------------------------------
       
   224 //
       
   225 void CLandmarksCategoriesContainer::NotifyDbEventL(
       
   226     TPosLmEvent& aEvent, 
       
   227     TInt aErrorCode)
       
   228     {
       
   229     if (aErrorCode == KErrNone)
       
   230         {
       
   231         switch (aEvent.iEventType)
       
   232             {
       
   233             case EPosLmEventUnknownChanges:
       
   234             case EPosLmEventLandmarkUnknownChanges:
       
   235             case EPosLmEventCategoryUnknownChanges:
       
   236             // Any of the events below might have cancelled an ongoing 
       
   237             // categories search/read operation => we must restart refreshing listbox
       
   238             case EPosLmEventLandmarkCreated:
       
   239             case EPosLmEventLandmarkDeleted:
       
   240             case EPosLmEventLandmarkUpdated:
       
   241             case EPosLmEventCategoryCreated:
       
   242             case EPosLmEventCategoryDeleted:
       
   243             case EPosLmEventCategoryUpdated:
       
   244                 StartUpdateListBoxL();
       
   245                 break;
       
   246             case EPosLmEventNewDefaultDatabaseLocation:
       
   247                 // Not supported by this application
       
   248                 break;
       
   249             case EPosLmEventMediaRemoved:
       
   250                 // Not supported by this application
       
   251                 break;
       
   252             default:
       
   253                 // Ignore other non-category events
       
   254                 break;
       
   255             }
       
   256         }
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 void CLandmarksCategoriesContainer::HandleControlEventL(
       
   263     CCoeControl* aControl, 
       
   264     TCoeEvent aEventType)
       
   265     {
       
   266 	if (aEventType == EEventStateChanged && aControl == iListBox)
       
   267 		{
       
   268 		// Take the index of the current item in the list
       
   269 		TInt itemIndex = iListBox->CurrentItemIndex();
       
   270 
       
   271 		// Set the current item in the model object regarding to the list box.
       
   272 		iModel->SetCurrentItem(itemIndex);
       
   273 		}
       
   274 
       
   275 	if (aEventType == EEventStateChanged && aControl == iFindBox)
       
   276 		{
       
   277         // Fetch filter from search field
       
   278         TInt searchTextLength = iFindBox->TextLength();
       
   279         HBufC* filterBuf = HBufC::NewLC(searchTextLength);
       
   280         TPtr filter = filterBuf->Des();
       
   281         iFindBox->GetSearchText(filter);
       
   282 
       
   283         // If filter has changed repopulate list box. (It is necessary to check
       
   284         // if filter has changed since each filter change generates several 
       
   285         // EEventStateChanged events for find box)
       
   286         if (filter != *iOldFilter)
       
   287             {
       
   288             // Filter has changed. Start filtering.
       
   289             StartUpdateListBoxL();
       
   290             }
       
   291 
       
   292         CleanupStack::PopAndDestroy(filterBuf);
       
   293 		}
       
   294     }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 void CLandmarksCategoriesContainer::StartUpdateListBoxL()
       
   300     {
       
   301     // Fetch filter from search field
       
   302     TInt searchTextLength = iFindBox->TextLength();
       
   303     const TInt KExtraChars = 2; // 2 chars wildcards
       
   304     HBufC* filterBuf = HBufC::NewLC(searchTextLength + KExtraChars);
       
   305     TPtr filter = filterBuf->Des();
       
   306     iFindBox->GetSearchText(filter);
       
   307 
       
   308     // Find out if there is any prevoius result to limit the search to
       
   309     TInt oldFilterLength = iOldFilter->Length();
       
   310     TBool searchOnlyInPreviousMatches = 
       
   311         // If previous filter is empty there is no search results
       
   312         oldFilterLength > 0 && 
       
   313 
       
   314         // If chars have been removed from filter we abandon previous results
       
   315         filter.Length() > oldFilterLength && 
       
   316 
       
   317         // There must exist at least one previous match
       
   318         IsItemSelected();
       
   319 
       
   320     // old filter used - now we can update it
       
   321     delete iOldFilter;
       
   322     iOldFilter = NULL;
       
   323     iOldFilter = filter.AllocL();
       
   324 
       
   325 	// Start to asynchronously populate the listbox model according to filter
       
   326     if (filter.Length() == 0)
       
   327         {
       
   328         iEngine.StartSearchingCategoriesL(this);
       
   329         }
       
   330     else
       
   331         {
       
   332         filter.Append(KWildCard);
       
   333         iEngine.StartSearchingCategoriesL(
       
   334             filter, 
       
   335             searchOnlyInPreviousMatches, 
       
   336             this);
       
   337         }
       
   338 
       
   339     CleanupStack::PopAndDestroy(filterBuf);
       
   340     }
       
   341 
       
   342 // -----------------------------------------------------------------------------
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 void CLandmarksCategoriesContainer::HandleSearchReadyL(TInt aErrorCode)
       
   346     {
       
   347 
       
   348     // Delete old icon array and add new
       
   349 	CColumnListBoxData* columnData = iListBox->ItemDrawer()->ColumnData();
       
   350     CArrayPtr<CGulIcon>* iconList = columnData->IconArray();
       
   351     iconList->ResetAndDestroy();
       
   352     delete iconList;
       
   353     columnData->SetIconArray(iModel->CreateIconListL());
       
   354 
       
   355     if (aErrorCode == KErrNone)
       
   356         {
       
   357         // Update model
       
   358         iModel->SetItemIds(iEngine.FetchCategorySearchResult());
       
   359         iListBox->HandleItemRemovalL();
       
   360 
       
   361         iEngine.StartReadingCategoriesL(KNrOfItemsDisplayed, this);
       
   362         iReadingFirstPage = ETrue;
       
   363         }
       
   364     else if (aErrorCode == KErrNotFound)
       
   365         {
       
   366         // Update model (indicate no item is selected)
       
   367         iModel->SetItemIds(NULL);
       
   368         iModel->SetCurrentItem(iListBox->CurrentItemIndex());
       
   369 
       
   370         // Update the listbox
       
   371         iListBox->HandleItemRemovalL();
       
   372         iListBox->DrawDeferred();
       
   373         }
       
   374     }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // -----------------------------------------------------------------------------
       
   378 //
       
   379 void CLandmarksCategoriesContainer::HandleReadReadyL(
       
   380     TInt aErrorCode)
       
   381     {
       
   382     // Update model with new categories
       
   383     CArrayPtr<CPosLandmarkCategory>* categories = iEngine.FetchCategoriesLC();
       
   384     CleanupStack::PushL(TCleanupItem(ResetAndDestroyCategories, categories));
       
   385     iModel->RepopulateModelL(categories);
       
   386     CleanupStack::PopAndDestroy(2, categories);
       
   387 
       
   388     if (aErrorCode == KErrNone)
       
   389         {
       
   390         if (iReadingFirstPage)
       
   391             {
       
   392             // First page ready
       
   393             iReadingFirstPage = EFalse;
       
   394 
       
   395             // Add the first page
       
   396             iListBox->HandleItemAdditionL();
       
   397 
       
   398             // Set the first item selected by default. We must do this since 
       
   399             // HandleItemRemovalL in HandleSearchReadyL resets the listbox.
       
   400             iModel->SetCurrentItem(0);
       
   401             iListBox->SetCurrentItemIndex(0);
       
   402             }
       
   403         else
       
   404             {
       
   405             iListBox->HandleItemAdditionL();
       
   406             }
       
   407         }
       
   408     }
       
   409 
       
   410