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