📄 wsengine.cpp
字号:
//
//============================================================================
// Name : CWSEngine from CWSEngine.h
// Part of : WSExample
// Created : 10/08/2005 by Forum Nokia
// Implementation notes: Engine uses city and country names to keep track of
// cities and countries.
//
// Version : 1.0
// Copyright: Nokia Corporation
//============================================================================
//
#include <e32base.h>
#include <t32wld.h>
#include <badesca.h>
#include "WSEngine.h"
// ---------------------------------------------------------
// CWSEngine::NewL()
//
// ---------------------------------------------------------
CWSEngine* CWSEngine::NewL()
{
CWSEngine* self = CWSEngine::NewLC();
CleanupStack::Pop( self );
return self;
}
// ---------------------------------------------------------
// CWSEngine::NewLC()
//
// ---------------------------------------------------------
CWSEngine* CWSEngine::NewLC()
{
CWSEngine* self = new( ELeave ) CWSEngine();
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
// ---------------------------------------------------------
// CWSEngine::~CWSEngine()
// closes world server connection
// ---------------------------------------------------------
CWSEngine::~CWSEngine()
{
iWS.Close();
}
// ---------------------------------------------------------
// CWSEngine::CWSEngine()
//
// ---------------------------------------------------------
CWSEngine::CWSEngine()
{
}
// ---------------------------------------------------------
// CWSEngine::ConstructL()
// Connect to world server.
// !!!!!!!!!!!!!!!!!!!!! Under the emulator of S60 sdk 2.1
// ! This is where the execution
// ! ends in emulator with error code KErrServerTerminated
// ! but still it works fine on actual devices.
// ---------------------------------------------------------
void CWSEngine::ConstructL()
{
TInt err;
TLocale locale;
err = iWS.Connect();
// NOTE: Connecting to the world server seems to reset some values
// (such as the time zone) in the used locale. To get around
// this TLocale locale was constructed before connecting to
// the world server and thus it contains the initial, correct
// values. After connecting to world server we just write the
// correct locale values back to use.
locale.Set();
if( err != KErrNone )
{
User::Leave( err );
}
iWS.SetDistanceUnits( EWldKilometers );
iCurrentUnit = EWldKilometers;
}
// ---------------------------------------------------------
// CWSEngine::FindFirst()
// Fills aCityData with the information of the city
// matching to aCityName.
// ---------------------------------------------------------
TInt CWSEngine::FindFirst( const TDesC& aCityName, TCityData& aCityData )
{
TInt err;
TWorldId cityId;
err = iWS.FindCity( cityId, aCityName );
if( err == KErrNone )
{
err = iWS.CityData( aCityData, cityId );
}
return err;
}
// ---------------------------------------------------------
// CWSEngine::FindFirst()
// Fills aCountryData with the information of the country
// matching to aCountryName.
// ---------------------------------------------------------
TInt CWSEngine::FindFirst( const TDesC& aCountryName,
TCountryData& aCountryData )
{
TInt err;
TWorldId countryId;
err = iWS.FindCountry( countryId, aCountryName );
if( err == KErrNone )
{
err = iWS.CountryData( aCountryData, countryId );
}
return err;
}
// ---------------------------------------------------------
// CWSEngine::ListCitiesL()
// Fills aCityListReturn with the names of the cities of
// whose name match to aSearchPattern.
// ---------------------------------------------------------
void CWSEngine::ListCitiesL( const TDesC& aSearchPattern,
CDesC16ArrayFlat* aCityListReturn )
{
TWorldId worldID;
TCityData cityData;
aCityListReturn->Reset();
if( iWS.FirstCity( worldID ) == KErrNone )
{
do
{
if( iWS.CityData( cityData, worldID ) == KErrNone )
{
if( cityData.iCity.FindF( aSearchPattern ) != KErrNotFound )
{
aCityListReturn->AppendL( cityData.iCity );
}
}
} while( iWS.NextCity( worldID ) == KErrNone );
}
}
// ---------------------------------------------------------
// CWSEngine::ListCountriesL()
// Fills aCountryListReturn with the names of the countries
// of whose name match to aSearchPattern.
// ---------------------------------------------------------
void CWSEngine::ListCountriesL( const TDesC& aSearchPattern,
CDesC16ArrayFlat* aCountryListReturn )
{
TWorldId worldID;
TCountryData countryData;
aCountryListReturn->Reset();
if( iWS.FirstCountry( worldID ) == KErrNone )
{
do
{
if( iWS.CountryData( countryData, worldID ) == KErrNone )
{
if( countryData.iCountry.FindF( aSearchPattern )
!= KErrNotFound )
{
aCountryListReturn->AppendL( countryData.iCountry );
}
}
} while( iWS.NextCountry( worldID ) == KErrNone );
}
}
// ---------------------------------------------------------
// CWSEngine::Sunlight()
// Fills aSunrise and aSunset with corresponding
// information about city pointed by aCityData.
// ---------------------------------------------------------
TInt CWSEngine::Sunlight( const TCityData& aCityData, TTime& aSunrise,
TTime& aSunset )
{
TWorldId worldID;
TTime currentTime;
TInt err;
currentTime.HomeTime();
err = iWS.FindCity( worldID, aCityData.iCity );
if( err == KErrNone )
{
err = iWS.CalculateSunlight( aSunrise, aSunset, worldID,
currentTime );
}
return err;
}
// ---------------------------------------------------------
// CWSEngine::HomeCity()
// Fill aCityData with information about current home city.
// ---------------------------------------------------------
void CWSEngine::HomeCity( TCityData& aCityData )
{
TWorldId worldID;
if( iWS.Home( worldID ) == KErrNone )
{
iWS.CityData( aCityData, worldID );
}
}
// ---------------------------------------------------------
// CWSEngine::HomeCountry()
// Fills aCountryData with information about current home
// country.
// ---------------------------------------------------------
void CWSEngine::HomeCountry( TCountryData& aCountryData )
{
TWorldId worldID;
if( iWS.DefaultCountry( worldID ) == KErrNone )
{
iWS.CountryData( aCountryData, worldID );
}
}
// ---------------------------------------------------------
// CWSEngine::SetUnit()
// Sets the distance unit for world server.
// ---------------------------------------------------------
void CWSEngine::SetUnit( const TWldDistanceUnits aUnit )
{
iWS.SetDistanceUnits( aUnit );
iCurrentUnit = aUnit;
}
// ---------------------------------------------------------
// CWSEngine::GetUnit()
// Gets the distance unit used for world server.
// ---------------------------------------------------------
TWldDistanceUnits CWSEngine::GetUnit()
{
return iCurrentUnit;
}
// ---------------------------------------------------------
// CWSEngine::CalculateDistance()
// Calculates the distance between the two cities
// ---------------------------------------------------------
TInt CWSEngine::CalculateDistance( const TDesC& aFirstCity,
const TDesC& aSecondCity, TInt& aDistance )
{
TWorldId worldIDFirst;
TWorldId worldIDSecond;
TInt err;
err = iWS.FindCity( worldIDFirst, aFirstCity );
if( err != KErrNone )
return err;
err = iWS.FindCity( worldIDSecond, aSecondCity );
if( err != KErrNone )
return err;
return iWS.CalculateDistance( aDistance, worldIDFirst, worldIDSecond );
}
// ---------------------------------------------------------
// CWSEngine::DataFileImport()
// Import datafile to world server database.
// ---------------------------------------------------------
TInt CWSEngine::DataFileImport( const TDesC& aFileName )
{
return iWS.DataFileOpen( aFileName );
}
// ---------------------------------------------------------
// CWSEngine::DataFileExport()
// Export datafile from world server database.
// ---------------------------------------------------------
TInt CWSEngine::DataFileExport( const TDesC& aFileName )
{
return iWS.DataFileSaveAs( aFileName, ETrue );
}
// ---------------------------------------------------------
// CWSEngine::ResetDatabase()
// Reset the world database to the rom version.
// ---------------------------------------------------------
TInt CWSEngine::ResetDatabase()
{
return iWS.ResetAllData();
}
// ---------------------------------------------------------
// CWSEngine::AddCity()
// add city to database.
// ---------------------------------------------------------
TInt CWSEngine::AddCity( const TCityData& aCityData )
{
return iWS.AddCity( aCityData );
}
// ---------------------------------------------------------
// CWSEngine::RemoveCity()
// remove city from database.
// ---------------------------------------------------------
TInt CWSEngine::RemoveCity( const TDesC& aCityName )
{
TInt err;
TWorldId cityID;
err = iWS.FindCity( cityID, aCityName );
if( err == KErrNone )
{
err = iWS.DeleteCity( cityID );
}
return err;
}
// ---------------------------------------------------------
// CWSEngine::AddCountry()
// add country and capital city for the country to be in
// the world server database.
// ---------------------------------------------------------
TInt CWSEngine::AddCountry( const TCountryData& aCountry,
const TCityData& aCapitalCity )
{
return iWS.AddCountry( aCountry, aCapitalCity );
}
// ---------------------------------------------------------
// CWSEngine::RemoveCountry()
// Remove country from database.
// ---------------------------------------------------------
TInt CWSEngine::RemoveCountry( const TDesC& aCountryName )
{
TInt err;
TWorldId countryID;
err = iWS.FindCountry( countryID, aCountryName );
if( err == KErrNone )
{
err = iWS.DeleteCountry( countryID );
}
return err;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -