locationpickerservice/src/locationpickerdatamanager_p.cpp
changeset 15 13ae750350c9
child 17 0f22fb80ebba
equal deleted inserted replaced
0:522cd55cc3d7 15:13ae750350c9
       
     1 /*
       
     2 * Copyright (c) 2010 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: LocationPickerDataManager private implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <e32std.h>
       
    20 #include <lbsposition.h>
       
    21 #include <EPos_CPosLandmark.h>
       
    22 #include <EPos_CPosLandmarkDatabase.h>
       
    23 #include <EPos_CPosLmCategoryManager.h>
       
    24 #include <EPos_CPosLandmarkCategory.h>
       
    25 #include <EPos_CPosLandmarkSearch.h>
       
    26 #include <EPos_CPosLmCategoryCriteria.h>
       
    27 #include <HbIcon>
       
    28 
       
    29 #include "locationpickerdatamanager_p.h"
       
    30 
       
    31 
       
    32 // ----------------------------------------------------------------------------
       
    33 // LocationPickerDataManagerPrivate::LocationPickerDataManagerPrivate()
       
    34 // ----------------------------------------------------------------------------
       
    35 
       
    36 LocationPickerDataManagerPrivate::LocationPickerDataManagerPrivate() :
       
    37         mModel( NULL ),
       
    38         mViewType( ELocationPickerAllView ),
       
    39         mIterator(NULL),
       
    40         mLandmarkDb(NULL),
       
    41         mLmCategoryManager(NULL),
       
    42         mLandmarkSearch(NULL)
       
    43 {
       
    44 }
       
    45 
       
    46 // ----------------------------------------------------------------------------
       
    47 // LocationPickerDataManagerPrivate::LocationPickerDataManagerPrivate()
       
    48 // ----------------------------------------------------------------------------
       
    49 
       
    50 LocationPickerDataManagerPrivate::LocationPickerDataManagerPrivate( QStandardItemModel &aModel, TViewType aViewType ) :
       
    51         mModel( &aModel ),
       
    52         mViewType( aViewType ),
       
    53         mIterator(NULL),
       
    54         mLandmarkDb(NULL),
       
    55         mLmCategoryManager(NULL),
       
    56         mLandmarkSearch(NULL)
       
    57 {
       
    58 }
       
    59 
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // LocationPickerDataManagerPrivate::~LocationPickerDataManagerPrivate()
       
    63 // ----------------------------------------------------------------------------
       
    64 LocationPickerDataManagerPrivate::~LocationPickerDataManagerPrivate()
       
    65 {
       
    66     // delete the member variables;
       
    67 
       
    68     if( mIterator )
       
    69         delete mIterator;
       
    70     if ( mLandmarkDb )
       
    71         delete mLandmarkDb;
       
    72     if( mLmCategoryManager )
       
    73         delete mLmCategoryManager;
       
    74     if( mLandmarkSearch )
       
    75         delete mLandmarkSearch;
       
    76 }
       
    77 
       
    78 // ----------------------------------------------------------------------------
       
    79 // LocationPickerDataManagerPrivate::populateModel()
       
    80 // ----------------------------------------------------------------------------
       
    81 bool LocationPickerDataManagerPrivate::populateModel( quint32 aCollectionId )
       
    82 {
       
    83     bool retValue = false;
       
    84     TRAP_IGNORE( retValue = populateModelL( aCollectionId ) );
       
    85     return retValue;
       
    86 }
       
    87 
       
    88 // ----------------------------------------------------------------------------
       
    89 // LocationPickerDataManagerPrivate::populateModel()
       
    90 // ----------------------------------------------------------------------------
       
    91 bool LocationPickerDataManagerPrivate::populateModelL( quint32 aCollectionId )
       
    92 {
       
    93     // Handle to the landmark database
       
    94     mLandmarkDb = NULL;
       
    95 
       
    96     //Open and intialize Landmark DB
       
    97     mLandmarkDb = CPosLandmarkDatabase::OpenL();
       
    98     ExecuteAndDeleteLD( mLandmarkDb->InitializeL() );
       
    99 
       
   100     switch( mViewType )
       
   101     {
       
   102         case ELocationPickerAllView:
       
   103         case ELocationPickerSearchView:
       
   104              {
       
   105                  // Create an iterator for iterating the landmarks in the database
       
   106                  mIterator = mLandmarkDb->LandmarkIteratorL();
       
   107 
       
   108                  if( ( mIterator == NULL ) || (mIterator->NumOfItemsL() == 0) )
       
   109                  {
       
   110                      // no items in the landmark database, so return false.
       
   111                      return false;
       
   112                  }
       
   113                  CleanupStack::PushL(mIterator);
       
   114                  populateLandmarksL();
       
   115                  CleanupStack::Pop( mIterator );
       
   116              }
       
   117              break;
       
   118         case ELocationPickerCollectionListView:
       
   119              {
       
   120 
       
   121                  // Create category manager for landmarks
       
   122                  mLmCategoryManager = CPosLmCategoryManager::NewL( *mLandmarkDb );
       
   123 
       
   124                  if( !mLmCategoryManager )
       
   125                  {
       
   126                      return false;
       
   127                  }
       
   128                  // Create an iterator for iterating the referenced categories in the database
       
   129                  mIterator = mLmCategoryManager->ReferencedCategoryIteratorL();
       
   130 
       
   131                  if( ( mIterator == NULL ) || (mIterator->NumOfItemsL() == 0) )
       
   132                  {
       
   133                      // no items in the landmark database, so return false.
       
   134                      return false;
       
   135                  }
       
   136                  CleanupStack::PushL(mIterator);
       
   137                  populateCollectionsL();
       
   138                  CleanupStack::Pop( mIterator );
       
   139 
       
   140              }
       
   141              break;
       
   142 
       
   143         case ELocationPickerCollectionContentView:
       
   144              {
       
   145 
       
   146                  // create a search object.
       
   147                  mLandmarkSearch = CPosLandmarkSearch::NewL( *mLandmarkDb );
       
   148                  CleanupStack::PushL( mLandmarkSearch );
       
   149 
       
   150                  // Create the search criterion
       
   151                  CPosLmCategoryCriteria* criteria = CPosLmCategoryCriteria::NewLC();
       
   152                  criteria->SetCategoryItemId( aCollectionId );
       
   153 
       
   154                  // Start the search and execute it at once.
       
   155                  ExecuteAndDeleteLD( mLandmarkSearch->StartLandmarkSearchL( *criteria ) );
       
   156                  CleanupStack::PopAndDestroy( criteria );
       
   157 
       
   158                  // Retrieve an iterator to access the matching landmarks.
       
   159                  mIterator = mLandmarkSearch->MatchIteratorL();
       
   160                  if( ( mIterator == NULL ) || (mIterator->NumOfItemsL() == 0) )
       
   161                  {
       
   162                      // no landmarks in this collection
       
   163                      CleanupStack::Pop(mLandmarkSearch);
       
   164                      return false;
       
   165                  }
       
   166                  CleanupStack::PushL( mIterator );
       
   167                  populateLandmarksL();
       
   168                  CleanupStack::Pop( mIterator );
       
   169                  CleanupStack::Pop(mLandmarkSearch);
       
   170              }
       
   171              break;
       
   172     }
       
   173     return true;
       
   174 }
       
   175 
       
   176 // ----------------------------------------------------------------------------
       
   177 // LocationPickerDataManagerPrivate::populateLandmarksL()
       
   178 // ----------------------------------------------------------------------------
       
   179 
       
   180 void LocationPickerDataManagerPrivate::populateLandmarksL()
       
   181 {
       
   182     // Read each landmark in the database and copy to the model.
       
   183     TPosLmItemId lmId;
       
   184     while ((lmId = mIterator->NextL()) != KPosLmNullItemId )
       
   185     {
       
   186         CPosLandmark* readLandmark = mLandmarkDb->ReadLandmarkLC(lmId );
       
   187 
       
   188         if( readLandmark )
       
   189         {
       
   190             QString lmAddressLine1(" ");
       
   191             QString lmAddressLine2("");
       
   192 
       
   193             TPtrC tempStr;
       
   194             TInt retStatus;
       
   195 
       
   196             // Copy landmark name in string 1
       
   197             retStatus = readLandmark->GetLandmarkName( tempStr );
       
   198             if( retStatus == KErrNone && tempStr.Length() > 0)
       
   199             {
       
   200                 lmAddressLine1 = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   201             }
       
   202 
       
   203             // create address line 2
       
   204             bool addressEmtpy = true; // used to check if address line 2 is empty
       
   205 
       
   206             // get street
       
   207             retStatus = readLandmark->GetPositionField( EPositionFieldStreet, tempStr );
       
   208             if( retStatus == KErrNone && tempStr.Length() )
       
   209             {
       
   210                 lmAddressLine2 = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   211                 addressEmtpy = EFalse;
       
   212             }
       
   213 
       
   214             // Get city
       
   215             retStatus =readLandmark->GetPositionField( EPositionFieldCity, tempStr );
       
   216             if( retStatus == KErrNone && tempStr.Length() )
       
   217             {
       
   218                 if( !addressEmtpy )
       
   219                 {
       
   220                     lmAddressLine2 = lmAddressLine2 + KSeparator;
       
   221                     lmAddressLine2 = lmAddressLine2 + KSpace;
       
   222                     lmAddressLine2 = lmAddressLine2 + QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   223                 }
       
   224                 else
       
   225                 {
       
   226                     lmAddressLine2 = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   227                     addressEmtpy = EFalse;
       
   228                 }
       
   229             }
       
   230             // Get State
       
   231             retStatus =readLandmark->GetPositionField( EPositionFieldState, tempStr );
       
   232             if( retStatus == KErrNone && tempStr.Length() )
       
   233             {
       
   234                 if( !addressEmtpy )
       
   235                 {
       
   236                     lmAddressLine2 = lmAddressLine2 + KSeparator;
       
   237                     lmAddressLine2 = lmAddressLine2 + KSpace;
       
   238                     lmAddressLine2 = lmAddressLine2 + QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   239                 }
       
   240                 else
       
   241                 {
       
   242                     lmAddressLine2 = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   243                     addressEmtpy = EFalse;
       
   244                 }
       
   245             }
       
   246 
       
   247             // get country
       
   248             retStatus =readLandmark->GetPositionField( EPositionFieldCountry, tempStr );
       
   249             if( retStatus == KErrNone && tempStr.Length() )
       
   250             {
       
   251                 if( !addressEmtpy )
       
   252                 {
       
   253                     lmAddressLine2 = lmAddressLine2 + KSeparator;
       
   254                     lmAddressLine2 = lmAddressLine2 + KSpace;
       
   255                     lmAddressLine2 = lmAddressLine2 + QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   256                 }
       
   257                 else
       
   258                 {
       
   259                     lmAddressLine2 = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   260                     addressEmtpy = EFalse;
       
   261                 }
       
   262             }
       
   263 
       
   264             // get contact address type
       
   265             QString contactAddressType;
       
   266             retStatus = readLandmark->GetLandmarkDescription( tempStr );
       
   267             if( retStatus == KErrNone && tempStr.Length() > 0)
       
   268             {
       
   269                 contactAddressType = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   270             }
       
   271 
       
   272             // set icons based on contact address type
       
   273             QVariantList icons;;
       
   274             if( contactAddressType == KContactHome )
       
   275             {
       
   276                 icons << HbIcon(KDummyImage) << HbIcon(KContactHomeIcon);
       
   277             }
       
   278             else if( contactAddressType == KContactWork )
       
   279             {
       
   280                 icons << HbIcon(KDummyImage) << HbIcon(KContactWorkIcon);
       
   281             }
       
   282             else
       
   283             {
       
   284                 icons << HbIcon(KDummyImage) << HbIcon(KContactPrefIcon);
       
   285             }
       
   286 
       
   287 
       
   288             // create a list item and set to model
       
   289             QStringList addressData;
       
   290             addressData << lmAddressLine1 << lmAddressLine2;
       
   291             QStandardItem *modelItem = new QStandardItem();
       
   292             modelItem->setData(QVariant(addressData), Qt::DisplayRole);
       
   293             modelItem->setData( icons, Qt::DecorationRole );
       
   294             mModel->appendRow( modelItem );
       
   295 
       
   296             CleanupStack::PopAndDestroy( readLandmark );
       
   297         }
       
   298    }
       
   299 }
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 // LocationPickerDataManagerPrivate::populateCollectionsL()
       
   303 // ----------------------------------------------------------------------------
       
   304 
       
   305 void LocationPickerDataManagerPrivate::populateCollectionsL()
       
   306 {
       
   307     // Read each categpry in the database and copy to the model.
       
   308     TPosLmItemId lmId;
       
   309     while ((lmId = mIterator->NextL()) != KPosLmNullItemId )
       
   310     {
       
   311         CPosLandmarkCategory* readCategory = mLmCategoryManager->ReadCategoryLC(lmId );
       
   312 
       
   313         if( readCategory )
       
   314         {
       
   315             QString categoryName("");
       
   316 
       
   317             TPtrC tempStr;
       
   318             TInt retStatus;
       
   319 
       
   320             retStatus = readCategory->GetCategoryName( tempStr );
       
   321             if( retStatus == KErrNone && tempStr.Length() > 0)
       
   322             {
       
   323                 categoryName = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   324                 if(categoryName == KContactsString)
       
   325                 {
       
   326                     categoryName = KContactsCollection;
       
   327                 }
       
   328             }
       
   329 
       
   330             // create a list item and copy to model
       
   331 
       
   332             QString iconPath;
       
   333             QStandardItem *modelItem = new QStandardItem();
       
   334             modelItem->setData(QVariant(categoryName), Qt::DisplayRole);
       
   335             modelItem->setData( QIcon (KCollectionsContacts), Qt::DecorationRole );
       
   336             mModel->appendRow( modelItem );
       
   337 
       
   338             CleanupStack::PopAndDestroy( readCategory );
       
   339         }
       
   340     }
       
   341 }
       
   342 
       
   343 // ----------------------------------------------------------------------------
       
   344 // LocationPickerDataManagerPrivate::getData()
       
   345 // ----------------------------------------------------------------------------
       
   346 
       
   347 void LocationPickerDataManagerPrivate::getData( int aIndex, quint32& aValue )
       
   348 {
       
   349     aValue = 0;
       
   350     RArray<TPosLmItemId> idArray;
       
   351     TRAPD( err, mIterator->GetItemIdsL( idArray, aIndex, 1 ) );
       
   352     if( err == KErrNone)
       
   353         aValue = (quint32) idArray[0];
       
   354 }
       
   355 
       
   356 // ----------------------------------------------------------------------------
       
   357 // LocationPickerDataManagerPrivate::getLocationItem()
       
   358 // ----------------------------------------------------------------------------
       
   359 
       
   360 void LocationPickerDataManagerPrivate::getLocationItem( quint32 aLmId, QLocationPickerItem& aItem )
       
   361 {
       
   362     TRAPD( err, getLocationItemL( aLmId, aItem ) );
       
   363     if( err != KErrNone )
       
   364         aItem.mIsValid = false;
       
   365 }
       
   366 
       
   367 // ----------------------------------------------------------------------------
       
   368 // LocationPickerDataManagerPrivate::getLocationItem()
       
   369 // ----------------------------------------------------------------------------
       
   370 
       
   371 void LocationPickerDataManagerPrivate::getLocationItemL( quint32 aLmId, QLocationPickerItem& aItem )
       
   372 {
       
   373     //Open and intialize Landmark DB
       
   374     CPosLandmarkDatabase* landmarkDb = NULL;
       
   375     landmarkDb = CPosLandmarkDatabase::OpenL();
       
   376 
       
   377     ExecuteAndDeleteLD( landmarkDb->InitializeL() );
       
   378 
       
   379     CPosLandmark* readLandmark = landmarkDb->ReadLandmarkLC(aLmId );
       
   380 
       
   381     if( readLandmark )
       
   382     {
       
   383         // get position and store
       
   384         TLocality position;
       
   385         readLandmark->GetPosition( position );
       
   386         aItem.mLatitude = position.Latitude();
       
   387         aItem.mLongitude = position.Longitude();
       
   388 
       
   389         // get address fields
       
   390         TPtrC tempStr;
       
   391         TInt retStatus;
       
   392 
       
   393         // Copy landmark name in string 1
       
   394         retStatus = readLandmark->GetLandmarkName( tempStr );
       
   395         if( retStatus == KErrNone && tempStr.Length() > 0)
       
   396         {
       
   397             aItem.mName = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   398         }
       
   399 
       
   400         // get street
       
   401         retStatus = readLandmark->GetPositionField( EPositionFieldStreet, tempStr );
       
   402         if( retStatus == KErrNone && tempStr.Length() )
       
   403         {
       
   404             aItem.mStreet = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   405         }
       
   406 
       
   407         // Get city
       
   408         retStatus =readLandmark->GetPositionField( EPositionFieldCity, tempStr );
       
   409         if( retStatus == KErrNone && tempStr.Length() )
       
   410         {
       
   411             aItem.mCity = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   412         }
       
   413         // Get State
       
   414         retStatus =readLandmark->GetPositionField( EPositionFieldState, tempStr );
       
   415         if( retStatus == KErrNone && tempStr.Length() )
       
   416         {
       
   417             aItem.mState = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   418         }
       
   419 
       
   420         // get country
       
   421         retStatus =readLandmark->GetPositionField( EPositionFieldCountry, tempStr );
       
   422         if( retStatus == KErrNone && tempStr.Length() )
       
   423         {
       
   424               aItem.mCountry = QString( (QChar*)tempStr.Ptr(), tempStr.Length());
       
   425         }
       
   426     }
       
   427     aItem.mIsValid = true;
       
   428     CleanupStack::PopAndDestroy(readLandmark);
       
   429     delete landmarkDb;
       
   430 }