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; |
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 ); |
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)); |
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)) { |