creator/engine/src/creator_landmark.cpp
branchRCL_3
changeset 22 fad26422216a
parent 21 b3cee849fa46
child 23 f8280f3bfeb7
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <EPos_CPosLandmarkDatabase.h>
       
    20 #include <EPos_CPosLmCategoryManager.h>
       
    21 #include <lbsposition.h>
       
    22 
       
    23 #include "engine.h"
       
    24 #include "enginewrapper.h"
       
    25 #include "creator_landmark.h" 
       
    26 #include "creator_traces.h"
       
    27 
       
    28 _LIT(KAccommodation, "accommodation");
       
    29 _LIT(KBusiness, "business");
       
    30 _LIT(KCommunication, "communication");
       
    31 _LIT(KEducational, "educational");
       
    32 _LIT(KEntertainment, "entertainment");
       
    33 _LIT(KFoodAndBeverage, "food");
       
    34 _LIT(KGeographical, "geographical");
       
    35 _LIT(KOutdoor, "outdoor");
       
    36 _LIT(KPeople, "people");
       
    37 _LIT(KPublic, "public");
       
    38 _LIT(KReligious, "religious");
       
    39 _LIT(KShopping, "shopping");
       
    40 _LIT(KSightseeing, "sightseeing");
       
    41 _LIT(KSports, "sports");
       
    42 _LIT(KTransport, "transport");
       
    43 
       
    44 
       
    45 /**
       
    46  * Class mapping the strings to global landmark categories
       
    47  */
       
    48 class CategoryMapping
       
    49 {
       
    50 public:
       
    51     CategoryMapping(TUint aId, const TDesC& aCategoryStr)
       
    52     : 
       
    53     iGlobalId(aId),
       
    54     iStr(aCategoryStr)
       
    55     {}
       
    56 
       
    57     TPosLmGlobalCategory iGlobalId;
       
    58     const TDesC& iStr;
       
    59 };
       
    60 
       
    61 /**
       
    62  * Global landmark categories
       
    63  */
       
    64 CategoryMapping LandmarkCategories[] =    {
       
    65         CategoryMapping(3000, KAccommodation), // Accommodation
       
    66         CategoryMapping(6000, KBusiness), // Business
       
    67         CategoryMapping(9000, KCommunication), // Communication
       
    68         CategoryMapping(12000, KEducational), // Educational institute
       
    69         CategoryMapping(15000, KEntertainment), // Entertainment
       
    70         CategoryMapping(18000, KFoodAndBeverage), // Food & Beverage
       
    71         CategoryMapping(21000, KGeographical), // Geographical area
       
    72         CategoryMapping(24000, KOutdoor), // Outdoor activities        
       
    73         CategoryMapping(27000, KPeople), // People
       
    74         CategoryMapping(30000, KPublic), // Public service
       
    75         CategoryMapping(33000, KReligious), // Religious places
       
    76         CategoryMapping(36000, KShopping), // Shopping
       
    77         CategoryMapping(39000, KSightseeing), // Sightseeing
       
    78         CategoryMapping(42000, KSports), // Sports
       
    79         CategoryMapping(45000, KTransport)  // Transport
       
    80 };
       
    81 
       
    82 /**
       
    83  * Return landmark global id
       
    84  */ 
       
    85 TPosLmGlobalCategory CCreatorLandmarks::GetCategoryIdL(const TDesC& aCategoryStr)
       
    86     {
       
    87     TUint numOfCategories = sizeof(LandmarkCategories)/sizeof(CategoryMapping);
       
    88     for( TUint i = 0; i < numOfCategories; ++i )
       
    89         {
       
    90         if( LandmarkCategories[i].iStr == aCategoryStr )
       
    91             return LandmarkCategories[i].iGlobalId;
       
    92         }
       
    93     
       
    94     return 0;
       
    95     }
       
    96 
       
    97 /**
       
    98  *
       
    99  */ 
       
   100 CLandmarkParameters::CLandmarkParameters()
       
   101     {
       
   102     LOGSTRING("Creator: CLandmarkParameters::CLandmarkParameters");
       
   103     }
       
   104 
       
   105 /**
       
   106  *
       
   107  */
       
   108 CLandmarkParameters::~CLandmarkParameters()
       
   109     {
       
   110     LOGSTRING("Creator: CLandmarkParameters::~CLandmarkParameters");
       
   111     delete iName;    
       
   112     delete iStreet;
       
   113     delete iCity;    
       
   114     delete iState;
       
   115     delete iCountry;
       
   116     delete iPostCode;    
       
   117     delete iDescription;
       
   118     delete iPhonenumber;
       
   119     delete iUrl;
       
   120     iCategories.Close();
       
   121     }
       
   122 
       
   123 /**
       
   124  *
       
   125  */
       
   126 void CLandmarkParameters::SetRandomNameL(CCreatorEngine& aEngine)
       
   127     {    
       
   128     TPtrC name(aEngine.RandomString(CCreatorEngine::ELandmarkName));
       
   129     delete iName;
       
   130     iName = 0;
       
   131     iName = HBufC::NewL(name.Length());
       
   132     iName->Des().Copy(name);
       
   133     }
       
   134 
       
   135 /**
       
   136  *
       
   137  */
       
   138 void CLandmarkParameters::SetRandomUrlL(CCreatorEngine& aEngine)
       
   139     {
       
   140     _LIT(KUrlPrefix, "http://www.");
       
   141     _LIT(KUrlPostfix, ".com");
       
   142     TPtrC company(aEngine.RandomString(CCreatorEngine::ECompany));
       
   143     delete iUrl;
       
   144     iUrl = 0;
       
   145     iUrl = HBufC::NewL(KUrlPrefix().Length()+company.Length()+KUrlPostfix().Length());
       
   146     iUrl->Des() = KUrlPrefix;
       
   147     iUrl->Des().Append( company );
       
   148     iUrl->Des().Append( KUrlPostfix );
       
   149     }
       
   150 
       
   151 /**
       
   152  *
       
   153  */
       
   154 void CLandmarkParameters::AddRandomCategoryL(CCreatorEngine& aEngine)
       
   155     {
       
   156     TUint numOfCategories = sizeof(LandmarkCategories)/sizeof(CategoryMapping);
       
   157     iCategories.AppendL(LandmarkCategories[aEngine.RandomNumber(0, numOfCategories-1)].iGlobalId);
       
   158     }
       
   159 
       
   160 /**
       
   161  *
       
   162  */
       
   163 void CLandmarkParameters::SetRandomStreetL(CCreatorEngine& aEngine)
       
   164     {
       
   165     TPtrC street(aEngine.RandomString(CCreatorEngine::EAddress));
       
   166     delete iStreet;
       
   167     iStreet = 0;
       
   168     iStreet = HBufC::NewL(street.Length());
       
   169     iStreet->Des().Copy(street);
       
   170     }
       
   171  
       
   172 /**
       
   173  *
       
   174  */
       
   175 void CLandmarkParameters::SetRandomCityL(CCreatorEngine& aEngine)
       
   176     {
       
   177     TPtrC city(aEngine.RandomString(CCreatorEngine::ECity));
       
   178     delete iCity;
       
   179     iCity = 0;
       
   180     iCity = HBufC::NewL(city.Length());
       
   181     iCity->Des().Copy(city);
       
   182     }
       
   183 
       
   184 /**
       
   185  *
       
   186  */
       
   187 void CLandmarkParameters::SetRandomStateL(CCreatorEngine& aEngine)
       
   188     {
       
   189     TPtrC state(aEngine.RandomString(CCreatorEngine::EState));
       
   190     delete iState;
       
   191     iState = 0;
       
   192     iState = HBufC::NewL(state.Length());
       
   193     iState->Des().Copy(state);
       
   194     }
       
   195 
       
   196 /**
       
   197  *
       
   198  */
       
   199 void CLandmarkParameters::SetRandomCountryL(CCreatorEngine& aEngine)
       
   200     {
       
   201     TPtrC country(aEngine.RandomString(CCreatorEngine::ECountry));
       
   202     delete iCountry;
       
   203     iCountry = 0;
       
   204     iCountry = HBufC::NewL(country.Length());
       
   205     iCountry->Des().Copy(country);
       
   206     }
       
   207 
       
   208 /**
       
   209  *
       
   210  */
       
   211 void CLandmarkParameters::SetRandomPostCodeL(CCreatorEngine& aEngine)
       
   212     {
       
   213     TPtrC po(aEngine.RandomString(CCreatorEngine::EPostcode));
       
   214     delete iPostCode;
       
   215     iPostCode = 0;
       
   216     iPostCode = HBufC::NewL(po.Length());
       
   217     iPostCode->Des().Copy(po);
       
   218     }
       
   219 
       
   220 /**
       
   221  *
       
   222  */
       
   223 void CLandmarkParameters::SetRandomLatitudeL(CCreatorEngine& aEngine)
       
   224     {
       
   225     iLatitude = aEngine.RandomNumber(-89, 89); // Degrees
       
   226     TReal64 random = aEngine.RandomNumber(0, 9999); // Desimals 
       
   227     iLatitude +=  random / 10000.0;
       
   228     }
       
   229 
       
   230 /**
       
   231  *
       
   232  */
       
   233 void CLandmarkParameters::SetRandomLongitudeL(CCreatorEngine& aEngine)
       
   234     {
       
   235     TInt minDegrees = -179;
       
   236     TInt maxDegrees = 179;
       
   237     TInt minDecimal = 0;
       
   238     TInt maxDecimal = 9999;
       
   239     TReal64 divider = 10000.0;
       
   240     iLongitude = aEngine.RandomNumber(minDegrees, maxDegrees); // Degrees
       
   241     TReal64 random = aEngine.RandomNumber(minDecimal, maxDecimal); 
       
   242     iLongitude += random  / divider;
       
   243     }
       
   244 
       
   245 /**
       
   246  *
       
   247  */
       
   248 void CLandmarkParameters::SetRandomPositionAccuracyL(CCreatorEngine& aEngine)
       
   249     {
       
   250     this->iPositionAccuracy = aEngine.RandomNumber(0,5);
       
   251     }
       
   252 
       
   253 /**
       
   254  *
       
   255  */
       
   256 void CLandmarkParameters::SetRandomAltitudeL(CCreatorEngine& aEngine)
       
   257     {
       
   258     this->iAltitude = aEngine.RandomNumber(0,120);
       
   259     }
       
   260 
       
   261 /**
       
   262  *
       
   263  */
       
   264 void CLandmarkParameters::SetRandomAltitudeAccuracyL(CCreatorEngine& aEngine)
       
   265     {
       
   266     this->iAltitudeAccuracy = aEngine.RandomNumber(0,5);
       
   267     }
       
   268 
       
   269 /**
       
   270  *
       
   271  */
       
   272 void CLandmarkParameters::SetRandomDescriptionL(CCreatorEngine& aEngine)
       
   273     {
       
   274     delete iDescription;
       
   275     iDescription = 0;
       
   276     TPtrC desc(aEngine.RandomString(CCreatorEngine::ELandmarkDescription));
       
   277     iDescription = HBufC::NewL(desc.Length());
       
   278     iDescription->Des().Copy(desc);
       
   279     }
       
   280 
       
   281 /**
       
   282  *
       
   283  */
       
   284 void CLandmarkParameters::SetRandomPhoneNumberL(CCreatorEngine& aEngine)
       
   285     {
       
   286     delete iPhonenumber;
       
   287     iPhonenumber = 0;
       
   288     TPtrC phone(aEngine.RandomString(CCreatorEngine::EPhoneNumber));
       
   289     iPhonenumber = HBufC::NewL(phone.Length());
       
   290     iPhonenumber->Des().Copy(phone);
       
   291     }
       
   292 
       
   293 /**
       
   294  *
       
   295  */
       
   296 CCreatorLandmarks::CCreatorLandmarks()
       
   297     {
       
   298     }
       
   299 
       
   300 /**
       
   301  *
       
   302  */
       
   303 CCreatorLandmarks* CCreatorLandmarks::NewL(CCreatorEngine* aEngine)
       
   304     {
       
   305     CCreatorLandmarks* self = CCreatorLandmarks::NewLC(aEngine);
       
   306     CleanupStack::Pop(); // self
       
   307     return self;
       
   308     }
       
   309 
       
   310 /**
       
   311  *
       
   312  */
       
   313 CCreatorLandmarks* CCreatorLandmarks::NewLC(CCreatorEngine* aEngine)
       
   314     {
       
   315     CCreatorLandmarks* self = new (ELeave) CCreatorLandmarks;
       
   316     CleanupStack::PushL(self);
       
   317     self->ConstructL(aEngine);
       
   318     return self;
       
   319     }
       
   320 
       
   321 /**
       
   322  *
       
   323  */
       
   324 void CCreatorLandmarks::ConstructL(CCreatorEngine* aEngine)
       
   325     {
       
   326     LOGSTRING("Creator: CCreatorLandmarks::ConstructL");
       
   327     iEngine = aEngine;
       
   328     iLandmarkDb = CPosLandmarkDatabase::OpenL();
       
   329     if( iLandmarkDb->IsInitializingNeeded() )
       
   330         {
       
   331         ExecuteAndDeleteLD(iLandmarkDb->InitializeL());
       
   332         }
       
   333     }
       
   334 
       
   335 /**
       
   336  *
       
   337  */
       
   338 CCreatorLandmarks::~CCreatorLandmarks()
       
   339     {
       
   340     LOGSTRING("Creator: CCreatorLandmarks::~CCreatorLandmarks");
       
   341     if ( iEngine && iEntryIds.Count() )
       
   342         {
       
   343         TRAP_IGNORE( iEngine->WriteEntryIdsToStoreL( iEntryIds, KUidDictionaryUidLandmarks ) );
       
   344         }
       
   345     iEntryIds.Reset();
       
   346     iEntryIds.Close();
       
   347     
       
   348     delete iLandmarkDb;
       
   349     delete iLmOperation;
       
   350     delete iParameters;
       
   351     }
       
   352 
       
   353 /**
       
   354  *
       
   355  */
       
   356 TBool CCreatorLandmarks::AskDataFromUserL(TInt aCommand)
       
   357     {
       
   358     LOGSTRING("Creator: CCreatorLandmarks::AskDataFromUserL");
       
   359     
       
   360     CCreatorModuleBase::AskDataFromUserL( aCommand );
       
   361     
       
   362     if ( aCommand == ECmdDeleteLandmarks )
       
   363         {
       
   364         return iEngine->GetEngineWrapper()->YesNoQueryDialog( _L("Delete all Landmarks?"), this, ECreatorModuleDelete );
       
   365         }
       
   366     else if ( aCommand ==  ECmdDeleteCreatorLandmarks )
       
   367         {
       
   368         return iEngine->GetEngineWrapper()->YesNoQueryDialog( _L("Delete all Landmarks created with Creator?"), this, ECreatorModuleDelete );
       
   369         }
       
   370     return iEngine->GetEngineWrapper()->EntriesQueryDialog(&iEntriesToBeCreated, _L("How many landmarks to create?"), EFalse, this, ECreatorModuleStart );    
       
   371     }
       
   372 
       
   373 /**
       
   374  * Convert global category id to local category id
       
   375  */
       
   376 TPosLmItemId CCreatorLandmarks::GetCategoryIdL( TPosLmGlobalCategory aGlobalCategoryID )
       
   377 {
       
   378     CPosLmCategoryManager* categoryManager = CPosLmCategoryManager::NewL( *iLandmarkDb );
       
   379     CleanupStack::PushL( categoryManager );
       
   380 
       
   381     // Find ID of the given global category in given database
       
   382     TPosLmItemId id = categoryManager->GetGlobalCategoryL( aGlobalCategoryID );
       
   383     CleanupStack::PopAndDestroy(); // categoryManager
       
   384     return id;
       
   385 }
       
   386 
       
   387 /**
       
   388  *
       
   389  */
       
   390 TInt CCreatorLandmarks::CreateLandmarkEntryL(CLandmarkParameters *aParameters)
       
   391     {
       
   392     LOGSTRING("Creator: CCreatorLandmarks::CreateLandmarkEntryL");
       
   393     TInt err = KErrNone;
       
   394     // clear any existing parameter definations
       
   395     delete iParameters;
       
   396     iParameters = NULL;
       
   397         
       
   398     CLandmarkParameters* parameters = aParameters;
       
   399         
       
   400     // random data needed if no predefined data available
       
   401     if (!parameters)
       
   402         {
       
   403         iParameters = new(ELeave) CLandmarkParameters;
       
   404         parameters = iParameters;
       
   405             
       
   406         TBuf<160> company = iEngine->RandomString(CCreatorEngine::ECompany);
       
   407 
       
   408         parameters->SetRandomNameL(*iEngine);
       
   409         parameters->SetRandomUrlL(*iEngine);
       
   410         parameters->AddRandomCategoryL(*iEngine);
       
   411         parameters->SetRandomStreetL(*iEngine);
       
   412         parameters->SetRandomCityL(*iEngine);
       
   413         parameters->SetRandomStateL(*iEngine);
       
   414         parameters->SetRandomCountryL(*iEngine);
       
   415         parameters->SetRandomPostCodeL(*iEngine);
       
   416         parameters->SetRandomLatitudeL(*iEngine);
       
   417         parameters->SetRandomLongitudeL(*iEngine);
       
   418         parameters->SetRandomPositionAccuracyL(*iEngine);
       
   419         parameters->SetRandomAltitudeL(*iEngine);
       
   420         parameters->SetRandomAltitudeAccuracyL(*iEngine);
       
   421         parameters->SetRandomDescriptionL(*iEngine);
       
   422         parameters->SetRandomPhoneNumberL(*iEngine);
       
   423         }
       
   424     
       
   425     CPosLandmark* newLandmark = CPosLandmark::NewLC();        
       
   426     TCoordinate crd(parameters->iLatitude, parameters->iLongitude, parameters->iAltitude);
       
   427     TLocality loc(crd, parameters->iPositionAccuracy, parameters->iAltitudeAccuracy);
       
   428     newLandmark->SetPositionL(loc);
       
   429     
       
   430     // Add categories
       
   431     for( TInt i = 0; i < parameters->iCategories.Count(); ++i )
       
   432         {
       
   433         newLandmark->AddCategoryL(GetCategoryIdL(parameters->iCategories[i]));
       
   434         }
       
   435     if( parameters->iName )    
       
   436         newLandmark->SetLandmarkNameL(parameters->iName->Des());    
       
   437     if( parameters->iCountry )
       
   438         newLandmark->SetPositionFieldL(EPositionFieldCountry, parameters->iCountry->Des());
       
   439     if( parameters->iCity )
       
   440         newLandmark->SetPositionFieldL(EPositionFieldCity, parameters->iCity->Des());
       
   441     if( parameters->iPostCode )
       
   442         newLandmark->SetPositionFieldL(EPositionFieldPostalCode, parameters->iPostCode->Des());
       
   443     if( parameters->iState )
       
   444         newLandmark->SetPositionFieldL(EPositionFieldState, parameters->iState->Des());
       
   445     if( parameters->iStreet )
       
   446         newLandmark->SetPositionFieldL(EPositionFieldStreet, parameters->iStreet->Des());
       
   447     if( parameters->iPhonenumber )
       
   448         newLandmark->SetPositionFieldL(EPositionFieldBuildingTelephone, parameters->iPhonenumber->Des());
       
   449     if( parameters->iDescription )
       
   450         newLandmark->SetLandmarkDescriptionL(parameters->iDescription->Des());    
       
   451     if( parameters->iUrl )
       
   452         newLandmark->SetPositionFieldL(EPositionFieldMediaLinksStart, parameters->iUrl->Des());
       
   453     
       
   454     TRAP(err, iLandmarkDb->AddLandmarkL(*newLandmark));
       
   455     
       
   456     // id has been generated, store it for being able to delete
       
   457     // entries created only with Creator
       
   458     iEntryIds.Append( newLandmark->LandmarkId() );
       
   459 
       
   460     ExecuteAndDeleteLD(iLandmarkDb->CompactL());
       
   461     
       
   462     CleanupStack::PopAndDestroy(newLandmark);
       
   463     return err;
       
   464     }
       
   465 
       
   466 //----------------------------------------------------------------------------
       
   467 void CCreatorLandmarks::DeleteAllL()
       
   468     {
       
   469     LOGSTRING("Creator: CCreatorLandmarks::DeleteAllL");
       
   470     ExecuteAndDeleteLD( iLandmarkDb->RemoveAllLandmarksL() );
       
   471     ExecuteAndDeleteLD( iLandmarkDb->CompactL() );
       
   472 
       
   473     // reset must be done here, because iEntryIds is stored in destructor
       
   474     iEntryIds.Reset();
       
   475     
       
   476     // all entries deleted, remove the Landmarks related registry
       
   477     iEngine->RemoveStoreL( KUidDictionaryUidLandmarks );
       
   478     }
       
   479 
       
   480 //----------------------------------------------------------------------------
       
   481 void CCreatorLandmarks::DeleteAllCreatedByCreatorL()
       
   482     {
       
   483     LOGSTRING("Creator: CCreatorLandmarks::DeleteAllCreatedByCreatorL");
       
   484     iEntryIds.Reset();
       
   485     
       
   486     // fetch ids of entries created by Creator
       
   487     iEngine->ReadEntryIdsFromStoreL( iEntryIds, KUidDictionaryUidLandmarks );
       
   488     
       
   489     // delete entries
       
   490     TRAP_IGNORE(
       
   491         ExecuteAndDeleteLD( iLandmarkDb->RemoveLandmarksL( iEntryIds ) );
       
   492         ExecuteAndDeleteLD( iLandmarkDb->CompactL() );
       
   493         );
       
   494 
       
   495     // reset must be done here, because iEntryIds is stored in destructor
       
   496     iEntryIds.Reset();
       
   497     
       
   498     // all entries deleted, remove the Landmarks related registry
       
   499     iEngine->RemoveStoreL( KUidDictionaryUidLandmarks );
       
   500     }
       
   501 
       
   502