clock/clockmw/clocktimezone/src/timezoneclient.cpp
changeset 45 b6db4fd4947b
parent 18 c198609911f9
child 55 2c54b51f39c4
equal deleted inserted replaced
23:fd30d51f876b 45:b6db4fd4947b
    17 */
    17 */
    18 
    18 
    19 // System includes
    19 // System includes
    20 #include <QList>
    20 #include <QList>
    21 #include <QStandardItemModel>
    21 #include <QStandardItemModel>
    22 #include <QDebug>
       
    23 #include <QtAlgorithms>
    22 #include <QtAlgorithms>
    24 #include <tzlocalizationdatatypes.h>
    23 #include <tzlocalizationdatatypes.h>
    25 #include <tzlocalizer.h>
    24 #include <tzlocalizer.h>
    26 #include <tz.h>
    25 #include <tz.h>
    27 #include <vtzrules.h>
    26 #include <vtzrules.h>
       
    27 #include <xqsettingsmanager.h>
       
    28 #include <xqsettingskey.h>
    28 
    29 
    29 // User includes
    30 // User includes
    30 #include "timezoneclient.h"
    31 #include "timezoneclient.h"
    31 #include "clockcommon.h"
    32 #include "clockcommon.h"
    32 #include "debug.h"
       
    33 #include "clockserverclt.h"
    33 #include "clockserverclt.h"
       
    34 #include "clockprivatecrkeys.h"
    34 
    35 
    35 const int KDaysInWeek(7);
    36 const int KDaysInWeek(7);
    36 const int KZerothRule(0);
    37 const int KZerothRule(0);
    37 const int KOneHour(1);
    38 const int KOneHour(1);
    38 const int KNoDifference(0);
    39 const int KNoDifference(0);
    42 
    43 
    43 	This class is the QObject based client interface for services offered by
    44 	This class is the QObject based client interface for services offered by
    44 	tzserver and timezonelocalization.
    45 	tzserver and timezonelocalization.
    45  */
    46  */
    46 
    47 
       
    48 TimezoneClient* TimezoneClient::mTimezoneClient = 0;
       
    49 bool TimezoneClient::mReferenceCount = false;
    47 /*!
    50 /*!
    48 	Default constructor.
    51 	Call this funtion to instantiate the TimezoneClient class.
    49 
       
    50 	\param parent The parent.
       
    51  */
    52  */
    52 TimezoneClient::TimezoneClient(QObject* parent)
    53 TimezoneClient* TimezoneClient::getInstance()
    53 :QObject(parent),
    54 {
    54  mTimeUpdateOn(false),
    55 	if (!mTimezoneClient) {
    55  mFetchCount(0)
    56 		mTimezoneClient = new TimezoneClient();
    56 {
    57 		mReferenceCount = true;
    57 	qDebug("clock: TimezoneClient::TimezoneClient() -->");
    58 	}
    58 
    59 
       
    60 	return mTimezoneClient;
       
    61 }
       
    62 
       
    63 /*!
       
    64 	Call this function to clean up the instance of TimezoneClient class.
       
    65  */
       
    66 void TimezoneClient::deleteInstance()
       
    67 {
       
    68 	if (mReferenceCount) {
       
    69 		delete mTimezoneClient;
       
    70 		mTimezoneClient = 0;
       
    71 	}
       
    72 }
       
    73 
       
    74 /*!
       
    75 	Call this function to check if the object is NULL.
       
    76  */
       
    77 bool TimezoneClient::isNull()
       
    78 {
       
    79 	return !mReferenceCount;
       
    80 }
       
    81 
       
    82 /*!
       
    83 	The constructor.
       
    84  */
       
    85 TimezoneClient::TimezoneClient()
       
    86 {
    59 	TCallBack callback(environmentCallback, this);
    87 	TCallBack callback(environmentCallback, this);
    60 
    88 
    61 	mNotifier = CEnvironmentChangeNotifier::NewL(
    89 	mNotifier = CEnvironmentChangeNotifier::NewL(
    62 			CActive::EPriorityStandard, callback);
    90 			CActive::EPriorityStandard, callback);
    63 	mNotifier->Start();
    91 	mNotifier->Start();
    64 
    92 
    65 	RClkSrvInterface clkSrvInterface1;
       
    66 	User::LeaveIfError(clkSrvInterface1.Connect());
       
    67 	TBool aUpdate;
       
    68 	clkSrvInterface1.IsAutoTimeUpdateOn(aUpdate);
       
    69 	if(aUpdate){
       
    70 	mTimeUpdateOn = true;
       
    71 	}
       
    72 	else {
       
    73 	mTimeUpdateOn = false;
       
    74 	}
       
    75 	clkSrvInterface1.Close();
       
    76 
       
    77 	mTzLocalizer = CTzLocalizer::NewL();
    93 	mTzLocalizer = CTzLocalizer::NewL();
    78 
    94 
    79 	qDebug("clock: TimezoneClient::TimezoneClient() <--");
    95 	// Create the settings manager.
       
    96 	mSettingsManager = new XQSettingsManager(this);
       
    97 
       
    98 	// Create the key for auto time update.
       
    99 	mAutoTimeUpdateKey = new XQSettingsKey(
       
   100 			XQSettingsKey::TargetCentralRepository,
       
   101 			KCRUidNitz,
       
   102 			KActiveProtocol);
       
   103 
       
   104 	// Start the monitoring for the auto time update key.
       
   105 	mSettingsManager->startMonitoring(*mAutoTimeUpdateKey);
       
   106 
       
   107 	// Get the value of auto time update from cenrep.
       
   108 	mAutoTimeUpdateValue = timeUpdateOn();
       
   109 
       
   110 	// Listen to the key value changes.
       
   111 	connect(
       
   112 			mSettingsManager, SIGNAL(valueChanged(XQSettingsKey, QVariant)),
       
   113 			this, SLOT(eventMonitor(XQSettingsKey, QVariant)));
    80 }
   114 }
    81 
   115 
    82 /*!
   116 /*!
    83 	Destructor.
   117 	Destructor.
    84  */
   118  */
    97 		mTzLocalizer = NULL;
   131 		mTzLocalizer = NULL;
    98 	}
   132 	}
    99 	if (mTimeZoneIds.count()) {
   133 	if (mTimeZoneIds.count()) {
   100 		mTimeZoneIds.clear();
   134 		mTimeZoneIds.clear();
   101 	}
   135 	}
       
   136 	// Clear the locations if exist.
       
   137 	if (mAllLocations.count()) {
       
   138 		mAllLocations.clear();
       
   139 	}
       
   140 
   102 }
   141 }
   103 
   142 
   104 /*!
   143 /*!
   105 	This functions returns a QList of location info which represent all the
   144 	This functions returns a QList of location info which represent all the
   106 	cities preset in the device returned by the tz server.
   145 	cities preset in the device returned by the tz server.
   107 
   146 
   108 	\return QList<LocationInfo> A list of location info objects which contain a
   147 	\return QList<LocationInfo> A list of location info objects which contain a
   109 			valid cityName, countryName, tz id and city group id. None of the other
   148 			valid cityName, countryName, tz id and city group id. None of the other
   110 			data is filled.
   149 			data is filled.
   111  */
   150  */
   112 QList<LocationInfo> TimezoneClient::getLocations()
   151 QList<LocationInfo>& TimezoneClient::getLocations()
   113 {
   152 {
   114 	qDebug("clock: TimezoneClient::getLocations() -->");
   153 	if (mAllLocations.count()) {
   115 
   154 		mAllLocations.clear();
   116 	// This list will contain the info of the cities fetched from tz server.
   155 	}
   117 	QList<LocationInfo> infoList;
       
   118 
       
   119 	// Construct the timezone localizer.
       
   120 	CTzLocalizer* localizer = CTzLocalizer::NewL();
       
   121 	CleanupStack::PushL(localizer);
       
   122 
   156 
   123 	// Get the cities, in alphabetical-ascending sorted order.
   157 	// Get the cities, in alphabetical-ascending sorted order.
   124 	CTzLocalizedCityArray* cityArray =
   158 	CTzLocalizedCityArray* cityArray =
   125 			localizer->GetCitiesL(CTzLocalizer::ETzAlphaNameAscending);
   159 			mTzLocalizer->GetCitiesL(CTzLocalizer::ETzAlphaNameAscending);
   126 	ASSERT(cityArray);
   160 	ASSERT(cityArray);
   127 	CleanupStack::PushL(cityArray);
   161 	CleanupStack::PushL(cityArray);
   128 	int cityCount = cityArray->Count();
   162 	int cityCount = cityArray->Count();
   129 
   163 
   130 	CTzLocalizedCityGroup* country = 0;
   164 	CTzLocalizedCityGroup* country = 0;
   148 			prevGroupId = cityGroupId;
   182 			prevGroupId = cityGroupId;
   149 
   183 
   150 			if (country) {
   184 			if (country) {
   151 				delete country;
   185 				delete country;
   152 			}
   186 			}
   153 			country = localizer->GetCityGroupL(cityGroupId);
   187 			country = mTzLocalizer->GetCityGroupL(cityGroupId);
   154 			countryName.Set(country->Name());
   188 			countryName.Set(country->Name());
   155 		}
   189 		}
   156 
   190 
   157 		// Now insert that data into the info list.
   191 		// Now insert that data into the info list.
   158 		LocationInfo cityInfo;
   192 		LocationInfo cityInfo;
   160 		cityInfo.timezoneId = tzId;
   194 		cityInfo.timezoneId = tzId;
   161 		cityInfo.cityName = QString::fromUtf16(
   195 		cityInfo.cityName = QString::fromUtf16(
   162 				cityName.Ptr(), cityName.Length());
   196 				cityName.Ptr(), cityName.Length());
   163 		cityInfo.countryName = QString::fromUtf16(
   197 		cityInfo.countryName = QString::fromUtf16(
   164 				countryName.Ptr(), countryName.Length());
   198 				countryName.Ptr(), countryName.Length());
   165 		infoList.append(cityInfo);
   199 		mAllLocations.append(cityInfo);
   166 	}
   200 	}
   167 
   201 
   168 	// Cleanup.
   202 	// Cleanup.
   169 	CleanupStack::PopAndDestroy(cityArray);
   203 	CleanupStack::PopAndDestroy(cityArray);
   170 	CleanupStack::PopAndDestroy(localizer);
   204 	return mAllLocations;
   171 
       
   172 	qDebug("clock: TimezoneClient::getLocations() <--");
       
   173 
       
   174 	return infoList;
       
   175 }
   205 }
   176 
   206 
   177 bool TimezoneClient::getUtcDstOffsetL(int& dstOffset, const CTzId& timezoneId)
   207 bool TimezoneClient::getUtcDstOffsetL(int& dstOffset, const CTzId& timezoneId)
   178 {
   208 {
   179 	RTz tzHandle;
   209 	RTz tzHandle;
   244 	return false;
   274 	return false;
   245 }
   275 }
   246 
   276 
   247 LocationInfo TimezoneClient::getCurrentZoneInfoL()
   277 LocationInfo TimezoneClient::getCurrentZoneInfoL()
   248 {
   278 {
   249 	qDebug() << "clock: TimezoneClient::getCurrentZoneInfoL -->";
       
   250 
       
   251 	// Current zone info.
   279 	// Current zone info.
   252 	LocationInfo currentLocation;
   280 	LocationInfo currentLocation;
   253 	int timezoneId(0);
   281 	int timezoneId(0);
   254 	int utcOffset(0);
   282 	int utcOffset(0);
   255 
   283 
   279 
   307 
   280 	// Close the resource handles
   308 	// Close the resource handles
   281 	timeZones.Close();
   309 	timeZones.Close();
   282 	zoneOffsets.Close();
   310 	zoneOffsets.Close();
   283 
   311 
   284 	// Construct CTzLocalizer object to get the timezone from the ID.
       
   285 	CTzLocalizer* tzLocalizer = CTzLocalizer::NewL();
       
   286 
       
   287 	// Get all the localized timezones for the current timezone ID.
   312 	// Get all the localized timezones for the current timezone ID.
   288 	CTzLocalizedTimeZone* localizedTimeZone(NULL);
   313 	CTzLocalizedTimeZone* localizedTimeZone(NULL);
   289 
   314 
   290 	if (tzLocalizer) {
   315 	localizedTimeZone =
   291 		// Get the currently set localized timezone.
   316 			mTzLocalizer->GetLocalizedTimeZoneL(tzId->TimeZoneNumericID());
   292 		CleanupStack::PushL(tzLocalizer);
   317 
   293 		localizedTimeZone =
   318 	if (localizedTimeZone) {
   294 			tzLocalizer->GetLocalizedTimeZoneL(tzId->TimeZoneNumericID());
   319 		CleanupStack::PushL(localizedTimeZone);
   295 
   320 
   296 		if (localizedTimeZone) {
   321 		// Get the frequently used localized city.
   297 			CleanupStack::PushL(localizedTimeZone);
   322 		CTzLocalizedCity* localizedCity(0);
   298 
   323 		localizedCity = mTzLocalizer->GetFrequentlyUsedZoneCityL(
   299 			// Get the frequently used localized city.
   324 				CTzLocalizedTimeZone::ECurrentZone);
   300 			CTzLocalizedCity* localizedCity(0);
   325 		CleanupStack::PushL(localizedCity);
   301 			localizedCity = tzLocalizer->GetFrequentlyUsedZoneCityL(
   326 
   302 					CTzLocalizedTimeZone::ECurrentZone);
   327 		// Get all the city groups.
   303 			CleanupStack::PushL(localizedCity);
   328 		CTzLocalizedCityGroupArray* cityGroupArray =
   304 
   329 				mTzLocalizer->GetAllCityGroupsL(
   305 			// Get all the city groups.
   330 						CTzLocalizer::ETzAlphaNameAscending);
   306 			CTzLocalizedCityGroupArray* cityGroupArray =
   331 		CleanupStack::PushL(cityGroupArray);
   307 					tzLocalizer->GetAllCityGroupsL(
   332 
   308 							CTzLocalizer::ETzAlphaNameAscending);
   333 		// Get the index of the country corresponding to the city group ID.
   309 			CleanupStack::PushL(cityGroupArray);
   334 		int countryIndex(1);
   310 
   335 
   311 			// Get the index of the country corresponding to the city group ID.
   336 		for (int index = 0; index < cityGroupArray->Count(); index++) {
   312 			int countryIndex(1);
   337 			if (localizedCity->GroupId() ==
   313 
   338 					cityGroupArray->At(index).Id()) {
   314 			for (int index = 0; index < cityGroupArray->Count(); index++) {
   339 				countryIndex = index;
   315 				if (localizedCity->GroupId() ==
       
   316 						cityGroupArray->At(index).Id()) {
       
   317 					countryIndex = index;
       
   318 				}
       
   319 			}
   340 			}
   320 
   341 		}
   321 			// Get all the cities within the currently set country.
   342 
   322 			CTzLocalizedCityArray* cityList = tzLocalizer->GetCitiesInGroupL(
   343 		// Get all the cities within the currently set country.
   323 					(cityGroupArray->At(countryIndex )).Id(),
   344 		CTzLocalizedCityArray* cityList = mTzLocalizer->GetCitiesInGroupL(
   324 					CTzLocalizer::ETzAlphaNameAscending);
   345 				(cityGroupArray->At(countryIndex )).Id(),
   325 			CleanupStack::PushL(cityList);
   346 				CTzLocalizer::ETzAlphaNameAscending);
   326 
   347 		CleanupStack::PushL(cityList);
   327 			// Check if automatic time update is enabled.
   348 
   328 			bool timeUpdateOn( false );
   349 		// Check if automatic time update is enabled.
   329 
   350 		bool timeUpdateOn( false );
   330 			// Connect to the clock server.
   351 
   331 			// TODO: connect to the clock server and get auto update status in
   352 		// Connect to the clock server.
   332 			// var:  timeUpdateOn
   353 		// TODO: connect to the clock server and get auto update status in
   333 			// Check if the country contains only one city or if automatic
   354 		// var:  timeUpdateOn
   334 			// time update is on.
   355 		// Check if the country contains only one city or if automatic
   335 			if (1 == cityList->Count() || timeUpdateOn) {
   356 		// time update is on.
   336 				// If yes, then display only the country name.
   357 		if (1 == cityList->Count() || timeUpdateOn) {
   337 				// TODO
   358 			// If yes, then display only the country name.
       
   359 			// TODO
       
   360 		} else {
       
   361 			// Display city name.
       
   362 			// TODO:
       
   363 		}
       
   364 
       
   365 		// Get the country name.
       
   366 		TPtrC countryName(cityGroupArray->At(countryIndex).Name());
       
   367 		currentLocation.countryName = QString::fromUtf16(
       
   368 				countryName.Ptr(), countryName.Length());
       
   369 
       
   370 		// Get the city name.
       
   371 		TPtrC cityName(localizedCity->Name());
       
   372 		currentLocation.cityName = QString::fromUtf16(
       
   373 				cityName.Ptr(), cityName.Length());
       
   374 
       
   375 		// Get the UTC offset.
       
   376 		timezoneId = localizedCity->TimeZoneId();
       
   377 
       
   378 		if (timezoneId) {
       
   379 			// Check if the DST is on for the current timezone.
       
   380 			if (isDSTOnL(timezoneId)) {
       
   381 				// Get the offset with DST enabled.
       
   382 				getUtcDstOffsetL(utcOffset, *tzId);
       
   383 
       
   384 				currentLocation.dstOn = true;
       
   385 				currentLocation.timezoneId = timezoneId;
       
   386 				currentLocation.zoneOffset = utcOffset;
   338 			} else {
   387 			} else {
   339 				// Display city name.
   388 				// Use the standard offset.
   340 				// TODO:
   389 				currentLocation.dstOn = false;
       
   390 				currentLocation.timezoneId = timezoneId;
       
   391 				currentLocation.zoneOffset = initialTimeZoneOffset;
   341 			}
   392 			}
   342 
       
   343 			// Get the country name.
       
   344 			TPtrC countryName(cityGroupArray->At(countryIndex).Name());
       
   345 			currentLocation.countryName = QString::fromUtf16(
       
   346 					countryName.Ptr(), countryName.Length());
       
   347 
       
   348 			// Get the city name.
       
   349 			TPtrC cityName(localizedCity->Name());
       
   350 			currentLocation.cityName = QString::fromUtf16(
       
   351 					cityName.Ptr(), cityName.Length());
       
   352 
       
   353 			// Get the UTC offset.
       
   354 			timezoneId = localizedCity->TimeZoneId();
       
   355 
       
   356 			if (timezoneId) {
       
   357 				// Check if the DST is on for the current timezone.
       
   358 				if (isDSTOnL(timezoneId)) {
       
   359 					// Get the offset with DST enabled.
       
   360 					getUtcDstOffsetL(utcOffset, *tzId);
       
   361 
       
   362 					currentLocation.dstOn = true;
       
   363 					currentLocation.timezoneId = timezoneId;
       
   364 					currentLocation.zoneOffset = utcOffset;
       
   365 				} else {
       
   366 					// Use the standard offset.
       
   367 					currentLocation.dstOn = false;
       
   368 					currentLocation.timezoneId = timezoneId;
       
   369 					currentLocation.zoneOffset = initialTimeZoneOffset;
       
   370 				}
       
   371 			}
       
   372 
       
   373 			// Cleanup.
       
   374 			CleanupStack::PopAndDestroy( cityList );
       
   375 			CleanupStack::PopAndDestroy( cityGroupArray );
       
   376 			CleanupStack::PopAndDestroy( localizedCity );
       
   377 			CleanupStack::PopAndDestroy( localizedTimeZone );
       
   378 		}
   393 		}
   379 
   394 
   380 		// Cleanup.
   395 		// Cleanup.
   381 		CleanupStack::PopAndDestroy( tzLocalizer );
   396 		CleanupStack::PopAndDestroy( cityList );
       
   397 		CleanupStack::PopAndDestroy( cityGroupArray );
       
   398 		CleanupStack::PopAndDestroy( localizedCity );
       
   399 		CleanupStack::PopAndDestroy( localizedTimeZone );
   382 	}
   400 	}
   383 
   401 
   384 	// Cleanup.
   402 	// Cleanup.
   385 	CleanupStack::PopAndDestroy( tzId );
   403 	CleanupStack::PopAndDestroy( tzId );
   386 	CleanupStack::PopAndDestroy( &tzHandle );
   404 	CleanupStack::PopAndDestroy( &tzHandle );
   387 
       
   388 	qDebug() << "clock: TimezoneClient::getCurrentZoneInfoL <--";
       
   389 
       
   390 	return currentLocation;
   405 	return currentLocation;
   391 }
   406 }
   392 
   407 
   393 void TimezoneClient::setAsCurrentLocationL(LocationInfo &location)
   408 void TimezoneClient::setAsCurrentLocationL(LocationInfo &location)
   394 {
   409 {
   395 	Debug::writeDebugMsg(
   410 /*	Debug::writeDebugMsg(
   396 			"In time zone client setAsCurrentLocationL " + location.cityName +
   411 			"In time zone client setAsCurrentLocationL " + location.cityName +
   397 			" " +
   412 			" " +
   398 			location.countryName +
   413 			location.countryName +
   399 			" " +
   414 			" " +
   400 			QString::number(location.zoneOffset));
   415 			QString::number(location.zoneOffset));*/
   401 
   416 
   402 	// Construct CTzLocalizer object.
   417 	LocationInfo prevLocationInfo ;
   403 	CTzLocalizer* tzLocalizer = CTzLocalizer::NewL();
   418 	prevLocationInfo = getCurrentZoneInfoL();
   404 	CleanupStack::PushL( tzLocalizer );
   419 
   405 	tzLocalizer->SetTimeZoneL( location.timezoneId );
   420 	mTzLocalizer->SetTimeZoneL( location.timezoneId );
   406 
   421 
   407 	TPtrC ptrCityName(
   422 	TPtrC ptrCityName(
   408 			reinterpret_cast<const TText*>(location.cityName.constData()));
   423 			reinterpret_cast<const TText*>(location.cityName.constData()));
   409 	CTzLocalizedCity* localizedCity =
   424 	CTzLocalizedCity* localizedCity =
   410 			tzLocalizer->FindCityByNameL(ptrCityName, location.timezoneId);
   425 			mTzLocalizer->FindCityByNameL(ptrCityName, location.timezoneId);
   411 	CleanupStack::PushL( localizedCity );
   426 	CleanupStack::PushL( localizedCity );
   412 	tzLocalizer->SetFrequentlyUsedZoneL(
   427 	mTzLocalizer->SetFrequentlyUsedZoneL(
   413 			*localizedCity, CTzLocalizedTimeZone::ECurrentZone);
   428 			*localizedCity, CTzLocalizedTimeZone::ECurrentZone);
   414 
   429 
   415 	// Cleanup.
   430 	// Cleanup.
   416 	CleanupStack::PopAndDestroy( localizedCity );
   431 	CleanupStack::PopAndDestroy( localizedCity );
   417 	CleanupStack::PopAndDestroy( tzLocalizer );
   432 
       
   433 	if(prevLocationInfo.timezoneId == location.timezoneId) {
       
   434 		emit cityUpdated();	
       
   435 	}
   418 }
   436 }
   419 
   437 
   420 bool TimezoneClient::isDSTOnL(int timezoneId)
   438 bool TimezoneClient::isDSTOnL(int timezoneId)
   421 {
   439 {
   422 	bool returnVal( false );
   440 	bool returnVal( false );
   586 	emit listUpdated();
   604 	emit listUpdated();
   587 }
   605 }
   588 
   606 
   589 void TimezoneClient::getCountries(QMap<QString, int>& countries)
   607 void TimezoneClient::getCountries(QMap<QString, int>& countries)
   590 {
   608 {
   591 	// Construct the timezone localizer.
       
   592 	CTzLocalizer* localizer = CTzLocalizer::NewL();
       
   593 	CleanupStack::PushL(localizer);
       
   594 
       
   595 	// Get all the city groups(countries).
   609 	// Get all the city groups(countries).
   596 	QTime t;
   610 	QTime t;
   597 	t.start();
   611 	t.start();
   598 	CTzLocalizedCityGroupArray* cityGroupArray =
   612 	CTzLocalizedCityGroupArray* cityGroupArray =
   599 		localizer->GetAllCityGroupsL(CTzLocalizer::ETzAlphaNameAscending);
   613 			mTzLocalizer->GetAllCityGroupsL(CTzLocalizer::ETzAlphaNameAscending);
   600 	qDebug() << "Fetching city groups from tzloc: " << t.elapsed();
       
   601 	CleanupStack::PushL(cityGroupArray);
   614 	CleanupStack::PushL(cityGroupArray);
   602 
   615 
   603 	t.restart();
   616 	t.restart();
   604 	// Iterate through each of the city groups.
   617 	// Iterate through each of the city groups.
   605 	for (int i = 0; i < cityGroupArray->Count(); i++) {
   618 	for (int i = 0; i < cityGroupArray->Count(); i++) {
   609 		// Get the QString of country name
   622 		// Get the QString of country name
   610 		QString qCountryName = QString::fromUtf16(
   623 		QString qCountryName = QString::fromUtf16(
   611 				countryName.Ptr(),countryName.Length());
   624 				countryName.Ptr(),countryName.Length());
   612 	    countries[qCountryName] = cityGroup.Id();
   625 	    countries[qCountryName] = cityGroup.Id();
   613 	}
   626 	}
   614 	qDebug() << "Converting " <<
       
   615 			cityGroupArray->Count() <<
       
   616 			"countries to qstring: " <<
       
   617 			t.elapsed();
       
   618 
   627 
   619 	// Cleanup.
   628 	// Cleanup.
   620 	CleanupStack::PopAndDestroy(cityGroupArray);
   629 	CleanupStack::PopAndDestroy(cityGroupArray);
   621 	CleanupStack::PopAndDestroy(localizer);
       
   622 }
   630 }
   623 
   631 
   624 void TimezoneClient::getCitiesForCountry(int id, QMap<QString, int>& cities)
   632 void TimezoneClient::getCitiesForCountry(int id, QMap<QString, int>& cities)
   625 {
   633 {
   626 	// Construct the timezone localizer.
       
   627 	CTzLocalizer* localizer = CTzLocalizer::NewL();
       
   628 	CleanupStack::PushL(localizer);
       
   629 
       
   630 	// Get the city group for the given id.
   634 	// Get the city group for the given id.
   631 	CTzLocalizedCityArray* cityArray = localizer->GetCitiesInGroupL(id,
   635 	CTzLocalizedCityArray* cityArray = mTzLocalizer->GetCitiesInGroupL(id,
   632 			CTzLocalizer::ETzAlphaNameAscending);
   636 			CTzLocalizer::ETzAlphaNameAscending);
   633 	CleanupStack::PushL(cityArray);
   637 	CleanupStack::PushL(cityArray);
   634 	CTzLocalizedCityArray* unsortedArray = localizer->GetCitiesInGroupL(id,
   638 	CTzLocalizedCityArray* unsortedArray = mTzLocalizer->GetCitiesInGroupL(id,
   635 			CTzLocalizer::ETzUnsorted);
   639 			CTzLocalizer::ETzUnsorted);
   636 	CleanupStack::PushL(unsortedArray);
   640 	CleanupStack::PushL(unsortedArray);
   637 
   641 
   638 	for (int i = 0; i < cityArray->Count(); i++) {
   642 	for (int i = 0; i < cityArray->Count(); i++) {
   639 		CTzLocalizedCity *city = &(cityArray->At(i));
   643 		CTzLocalizedCity *city = &(cityArray->At(i));
   652 	}
   656 	}
   653 
   657 
   654 	// Cleanup.
   658 	// Cleanup.
   655 	CleanupStack::PopAndDestroy(unsortedArray);
   659 	CleanupStack::PopAndDestroy(unsortedArray);
   656 	CleanupStack::PopAndDestroy(cityArray);
   660 	CleanupStack::PopAndDestroy(cityArray);
   657 	CleanupStack::PopAndDestroy(localizer);
       
   658 }
   661 }
   659 
   662 
   660 void TimezoneClient::getLocationInfo(
   663 void TimezoneClient::getLocationInfo(
   661 		int groupId, int cityIndex, LocationInfo& cityInfo)
   664 		int groupId, int cityIndex, LocationInfo& cityInfo)
   662 {
   665 {
   663 	TRAPD(
   666 	TRAPD(
   664 			error,
   667 			error,
   665 
   668 
   666 			// Construct the localizer.
       
   667 			CTzLocalizer* localizer = CTzLocalizer::NewLC();
       
   668 
       
   669 			// Get the localized city group.
   669 			// Get the localized city group.
   670 			CTzLocalizedCityGroup* cityGroup = localizer->GetCityGroupL(groupId);
   670 			CTzLocalizedCityGroup* cityGroup = mTzLocalizer->GetCityGroupL(groupId);
   671 			CleanupStack::PushL(cityGroup);
   671 			CleanupStack::PushL(cityGroup);
   672 
   672 
   673 			// Get the localized city array for the given city group.
   673 			// Get the localized city array for the given city group.
   674 			CTzLocalizedCityArray* cityArray = localizer->GetCitiesInGroupL(
   674 			CTzLocalizedCityArray* cityArray = mTzLocalizer->GetCitiesInGroupL(
   675 					groupId, CTzLocalizer::ETzUnsorted);
   675 					groupId, CTzLocalizer::ETzUnsorted);
   676 			CleanupStack::PushL(cityArray);
   676 			CleanupStack::PushL(cityArray);
   677 
   677 
   678 			// Get the required city.
   678 			// Get the required city.
   679 			CTzLocalizedCity* neededCity = &(cityArray->At(cityIndex));
   679 			CTzLocalizedCity* neededCity = &(cityArray->At(cityIndex));
   688 			cityInfo.zoneOffset = getDstZoneOffset(cityInfo.timezoneId);
   688 			cityInfo.zoneOffset = getDstZoneOffset(cityInfo.timezoneId);
   689 
   689 
   690 			// Cleanup.
   690 			// Cleanup.
   691 			CleanupStack::PopAndDestroy(cityArray);
   691 			CleanupStack::PopAndDestroy(cityArray);
   692 			CleanupStack::PopAndDestroy(cityGroup);
   692 			CleanupStack::PopAndDestroy(cityGroup);
   693 			CleanupStack::PopAndDestroy(localizer);
       
   694 	)
   693 	)
   695 	Q_UNUSED(error)
   694 	Q_UNUSED(error)
   696 }
   695 }
   697 
   696 
   698 QDataStream &operator<<(
   697 QDataStream &operator<<(
   785 int TimezoneClient::getCityGroupIdByName(const QString& name)
   784 int TimezoneClient::getCityGroupIdByName(const QString& name)
   786 {
   785 {
   787 	TPtrC namePtr;
   786 	TPtrC namePtr;
   788 	namePtr.Set(name.utf16(), name.length());
   787 	namePtr.Set(name.utf16(), name.length());
   789 
   788 
   790 	// Construct the timezone localizer.
       
   791 	CTzLocalizer *localizer = CTzLocalizer::NewLC();
       
   792 
       
   793 	// Get the citygroup matching the name.
   789 	// Get the citygroup matching the name.
   794 	CTzLocalizedCityGroup *cityGroup = localizer->FindCityGroupByNameL(namePtr);
   790 	CTzLocalizedCityGroup *cityGroup = mTzLocalizer->FindCityGroupByNameL(namePtr);
   795 	CleanupStack::PushL(cityGroup);
   791 	CleanupStack::PushL(cityGroup);
   796 
   792 
   797 	// Get the id.
   793 	// Get the id.
   798 	int id = cityGroup->Id();
   794 	int id = cityGroup->Id();
   799 
   795 
   800 	// Cleanup.
   796 	// Cleanup.
   801 	CleanupStack::PopAndDestroy(cityGroup);
   797 	CleanupStack::PopAndDestroy(cityGroup);
   802 	CleanupStack::PopAndDestroy(localizer);
       
   803 
   798 
   804 	return id;
   799 	return id;
   805 }
   800 }
   806 
   801 
   807 int TimezoneClient::getCityOffsetByNameAndId(const QString& name, int tzId)
   802 int TimezoneClient::getCityOffsetByNameAndId(const QString& name, int tzId)
   808 {
   803 {
   809 	TPtrC namePtr;
   804 	TPtrC namePtr;
   810 	namePtr.Set(name.utf16(), name.length());
   805 	namePtr.Set(name.utf16(), name.length());
   811 
   806 
   812 	// Construct the timezone localizer.
       
   813 	CTzLocalizer *localizer = CTzLocalizer::NewLC();
       
   814 
       
   815 	// Get the citygroup matching the name.
   807 	// Get the citygroup matching the name.
   816 	CTzLocalizedCityArray *cityArray = localizer->GetCitiesL(tzId);
   808 	CTzLocalizedCityArray *cityArray = mTzLocalizer->GetCitiesL(tzId);
   817 	CleanupStack::PushL(cityArray);
   809 	CleanupStack::PushL(cityArray);
   818 
   810 
   819 	int id;
   811 	int id;
   820 	for (id = 0; id < cityArray->Count(); id++) {
   812 	for (id = 0; id < cityArray->Count(); id++) {
   821 		if (KErrNone == cityArray->At(id).Name().Compare(namePtr)) {
   813 		if (KErrNone == cityArray->At(id).Name().Compare(namePtr)) {
   826 		id = -1;
   818 		id = -1;
   827 	}
   819 	}
   828 
   820 
   829 	// Cleanup.
   821 	// Cleanup.
   830 	CleanupStack::PopAndDestroy(cityArray);
   822 	CleanupStack::PopAndDestroy(cityArray);
   831 	CleanupStack::PopAndDestroy(localizer);
       
   832 
   823 
   833 	return id;
   824 	return id;
   834 
       
   835 }
   825 }
   836 
   826 
   837 void TimezoneClient::setDateTime(QDateTime dateTime)
   827 void TimezoneClient::setDateTime(QDateTime dateTime)
   838 {
   828 {
   839 	TMonth month = intToMonth(dateTime.date().month());
   829 	TMonth month = intToMonth(dateTime.date().month());
   848 	CleanupStack::PopAndDestroy(&tz);
   838 	CleanupStack::PopAndDestroy(&tz);
   849 }
   839 }
   850 
   840 
   851 void TimezoneClient::setTimeUpdateOn(bool timeUpdate)
   841 void TimezoneClient::setTimeUpdateOn(bool timeUpdate)
   852 {
   842 {
   853     RClkSrvInterface clkSrvInterface;
   843 	RClkSrvInterface clkSrvInterface;
   854     User::LeaveIfError(clkSrvInterface.Connect());
   844 	User::LeaveIfError(clkSrvInterface.Connect());
   855     if (timeUpdate) {
   845 	if (timeUpdate) {
   856     clkSrvInterface.ActivateAllProtocols();
   846 		clkSrvInterface.ActivateAllProtocols();
   857     }
   847 	}
   858     else {
   848 	else {
   859     clkSrvInterface.DeActivateAllProtocols();
   849 		clkSrvInterface.DeActivateAllProtocols();
   860     }
   850 	}
   861 	mTimeUpdateOn = timeUpdate;
       
   862 	clkSrvInterface.Close();
   851 	clkSrvInterface.Close();
   863 }
   852 }
   864 
   853 
   865 bool TimezoneClient::timeUpdateOn()
   854 bool TimezoneClient::timeUpdateOn()
   866 {
   855 {
   867 	return mTimeUpdateOn;
   856 	TBool autoTimeUpdateOn;
       
   857 	RClkSrvInterface clkSrvInterface;
       
   858 	User::LeaveIfError(clkSrvInterface.Connect());
       
   859 	clkSrvInterface.IsAutoTimeUpdateOn(autoTimeUpdateOn);
       
   860 	clkSrvInterface.Close();
       
   861 	return autoTimeUpdateOn;
   868 }
   862 }
   869 
   863 
   870 QStandardItemModel *TimezoneClient::locationSelectorModel()
   864 QStandardItemModel *TimezoneClient::locationSelectorModel()
   871 {
   865 {
   872 	if (!mWorldClockModel) {
   866 	if (!mWorldClockModel) {
  1243 
  1237 
  1244 	CleanupStack::PopAndDestroy(newCity);
  1238 	CleanupStack::PopAndDestroy(newCity);
  1245 
  1239 
  1246 	return info;
  1240 	return info;
  1247 }
  1241 }
       
  1242 
       
  1243 /*!
       
  1244 	Slot which is called when the value changes in cevrep.
       
  1245 
       
  1246 	\param key The key which got changed in cenrep.
       
  1247 	\param value The new value of that key.
       
  1248  */
       
  1249 void TimezoneClient::eventMonitor(const XQSettingsKey& key, const QVariant& value)
       
  1250 {
       
  1251 	if (key.uid() == KCRUidNitz && key.key() == KActiveProtocol) {
       
  1252 		if (mSettingsManager->error() == XQSettingsManager::NoError) {
       
  1253 
       
  1254 			// New value of auto time update.
       
  1255 			int autoTimeUpdate = value.toInt();
       
  1256 
       
  1257 			// Check if the auto time update value has actually changed.
       
  1258 			bool keyValueChanged = (autoTimeUpdate != mAutoTimeUpdateValue);
       
  1259 
       
  1260 			if(keyValueChanged) {
       
  1261 				mAutoTimeUpdateValue = autoTimeUpdate;
       
  1262 				emit autoTimeUpdateChanged(mAutoTimeUpdateValue);
       
  1263 			}
       
  1264 		}
       
  1265 	}
       
  1266 }
  1248 // End of file	--Don't remove this.
  1267 // End of file	--Don't remove this.