clock/clockmw/clocksettingsutility/src/settingsutility.cpp
changeset 18 c198609911f9
child 26 a949c2543c15
child 45 b6db4fd4947b
equal deleted inserted replaced
0:f979ecb2b13e 18:c198609911f9
       
     1 /*
       
     2  * Copyright (c) 2009-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  *
       
    15  * Description:   Definition file for the class SettingsUtility
       
    16  *
       
    17  */
       
    18 
       
    19 // System includes
       
    20 #include <e32std.h>
       
    21 #include <QTime>
       
    22 #include <QDateTime>
       
    23 #include <HbGlobal>
       
    24 
       
    25 // User includes
       
    26 #include "settingsutility.h"
       
    27 #include "timezoneclient.h"
       
    28 #include "clockdatatypes.h"
       
    29 
       
    30 /*!
       
    31 	\class SettingsUtility.
       
    32  */
       
    33 
       
    34 /*!
       
    35 	Default constructor.
       
    36  */
       
    37 SettingsUtility::SettingsUtility(QObject *parent)
       
    38 :QObject(parent)
       
    39 {
       
    40 	qDebug("clock: SettingsUtility::SettingsUtility -->");
       
    41 
       
    42 	mTimeSeparatorList << tr(".") << tr(":");
       
    43 	mClockTypeList << tr("Analog") << tr("Digital");
       
    44 	mTimeFormatList << hbTrId("txt_clk_setlabel_val_24_hour") << hbTrId("txt_clk_setlabel_val_12_hour");
       
    45 	mDateFormatList << hbTrId("txt_clk_setlabel_val_dd_mm_yyyy") << hbTrId("txt_clk_setlabel_val_mm_dd_yyyy") << hbTrId("txt_clk_setlabel_val_yyyy_mm_dd");
       
    46 	mDateSeparatorList << tr(".") << tr(":") << tr("/") << tr("-");
       
    47 	mAutoUpdateValueList << tr("ON") << tr("OFF");
       
    48 	mSnoozeValueList << tr("5 minutes") << tr("15 minutes") << tr(" 30 minutes") << tr("1 hour");
       
    49 
       
    50 	mTimeZoneClient = new TimezoneClient(this);
       
    51 
       
    52 	qDebug("clock: SettingsUtility::SettingsUtility <--");
       
    53 
       
    54 //	mSettingsMamager = new XQSettingsManager(this);
       
    55 }
       
    56 
       
    57 /*!
       
    58 	Destructor.
       
    59  */
       
    60 SettingsUtility::~SettingsUtility()
       
    61 {
       
    62 	// Nothing yet.
       
    63 }
       
    64 
       
    65 /*!
       
    66 
       
    67  */
       
    68 void SettingsUtility::setTime(const QString &time)
       
    69 {
       
    70 	QTime newTime = QTime::fromString(time, timeFormatString());
       
    71 
       
    72 	if (newTime.isValid()) {
       
    73 		mTimeZoneClient->setDateTime(QDateTime(QDate::currentDate(), newTime));
       
    74 	}
       
    75 }
       
    76 
       
    77 /*!
       
    78 
       
    79  */
       
    80 QString SettingsUtility::time()
       
    81 {
       
    82 	return QTime::currentTime().toString(timeFormatString());
       
    83 }
       
    84 
       
    85 /*!
       
    86 
       
    87  */
       
    88 void SettingsUtility::setTimeZone(const QString &timezone)
       
    89 {
       
    90 	Q_UNUSED(timezone)
       
    91 }
       
    92 
       
    93 /*!
       
    94 
       
    95  */
       
    96 QString SettingsUtility::timeZone()
       
    97 {
       
    98 	QStringList dummyList;
       
    99 
       
   100 	// Get the current zone info.
       
   101 	LocationInfo currentZoneInfo = mTimeZoneClient->getCurrentZoneInfoL();
       
   102 
       
   103 	// Construct the GMT +/- X string.
       
   104 	QString gmtOffset(hbTrId("txt_common_common_gmt"));
       
   105 
       
   106 	int utcOffset = currentZoneInfo.zoneOffset;
       
   107 	int offsetInHours (utcOffset/60);
       
   108 	int offsetInMinutes (utcOffset%60);
       
   109 
       
   110 	// Check wether the offset is +ve or -ve.
       
   111 	if (0 < utcOffset) {
       
   112 		// We have a positive offset. Append the '+' character.
       
   113 		gmtOffset += tr("+ ");
       
   114 	} else if (0 > utcOffset) {
       
   115 		// We have a negative offset. Append the '-' character.
       
   116 		gmtOffset += tr("- ");
       
   117 		offsetInHours = -offsetInHours;
       
   118 	} else {
       
   119 		// We dont have an offset. We are at GMT zone.
       
   120 	}
       
   121 
       
   122 	// Append the hour component.
       
   123 	gmtOffset += QString::number(offsetInHours);
       
   124 
       
   125 	// Append the time separator.
       
   126 	gmtOffset += mTimeSeparatorList.at(timeSeparator(dummyList));
       
   127 
       
   128 	// Append the minute component.
       
   129 	// If minute component is less less than 10, append a '00'
       
   130 	if (0 <= offsetInMinutes && offsetInMinutes < 10) {
       
   131 		gmtOffset += tr("00");
       
   132 	} else {
       
   133 		gmtOffset += QString::number(offsetInMinutes);
       
   134 	}
       
   135 
       
   136 	gmtOffset += tr(" ");
       
   137 	// TODO: append city name when more than one cities else country name.
       
   138 	gmtOffset += currentZoneInfo.cityName;
       
   139 
       
   140 	return gmtOffset;
       
   141 }
       
   142 
       
   143 /*!
       
   144 
       
   145  */
       
   146 void SettingsUtility::setTimeFormat(const QString& format)
       
   147 {
       
   148 	TLocale locale;
       
   149 
       
   150 	if (format == mTimeFormatList.at(0)) {
       
   151 		locale.SetTimeFormat(ETime24);
       
   152 	} else if (format == mTimeFormatList.at(1)) {
       
   153 		locale.SetTimeFormat(ETime12);
       
   154 	} else {
       
   155 		// Nothing to do.
       
   156 	}
       
   157 	locale.Set();
       
   158 }
       
   159 
       
   160 /*!
       
   161 
       
   162  */
       
   163 int SettingsUtility::timeFormat(QStringList &format)
       
   164 {
       
   165 	TLocale locale;
       
   166 	int value = -1;
       
   167 
       
   168 	if (ETime24 == locale.TimeFormat()) {
       
   169 		value = 0;
       
   170 	} else if (ETime12 == locale.TimeFormat()) {
       
   171 		value = 1;
       
   172 	}
       
   173 
       
   174 	format = mTimeFormatList;
       
   175 
       
   176 	return value;
       
   177 }
       
   178 
       
   179 /*!
       
   180 
       
   181  */
       
   182 void SettingsUtility::setClockType(const QString &type)
       
   183 {
       
   184 	TLocale locale;
       
   185 
       
   186 	if (type == mClockTypeList.at(0)) {
       
   187 	    locale.SetClockFormat(EClockAnalog);
       
   188     } else if (type == mClockTypeList.at(1)) {
       
   189 	    locale.SetClockFormat(EClockDigital);
       
   190     } else {
       
   191     	// Nothing to do.
       
   192     }
       
   193 	locale.Set();
       
   194 }
       
   195 
       
   196 /*!
       
   197 
       
   198  */
       
   199 int SettingsUtility::clockType(QStringList &list)
       
   200 {
       
   201 	TLocale locale;
       
   202 	int value = -1;
       
   203 
       
   204 	if (EClockAnalog == locale.ClockFormat()) {
       
   205 	    value = 0;
       
   206     } else if (EClockDigital == locale.ClockFormat()){
       
   207     	value = 1;
       
   208     } else {
       
   209     	// Nothing to do.
       
   210     }
       
   211 
       
   212 	list = mClockTypeList;
       
   213 
       
   214 	return value;
       
   215 }
       
   216 
       
   217 /*!
       
   218 
       
   219  */
       
   220 void SettingsUtility::setTimeSeparator(const QString &separator)
       
   221 {
       
   222 	if (mTimeSeparatorList.contains(separator)) {
       
   223 	    TLocale locale;
       
   224 
       
   225 		locale.SetTimeSeparator(TChar(separator.unicode()->unicode()), 1);
       
   226 		locale.SetTimeSeparator(TChar(separator.unicode()->unicode()), 2);
       
   227 
       
   228 		locale.Set();
       
   229     }
       
   230 }
       
   231 
       
   232 /*!
       
   233 
       
   234  */
       
   235 int SettingsUtility::timeSeparator(QStringList &list)
       
   236 {
       
   237 	qDebug() << "clock: SettingsUtility::timeSeparator -->";
       
   238 
       
   239 	TLocale locale;
       
   240 	TChar separatorChar = locale.TimeSeparator(1);
       
   241 	int value = -1;
       
   242 
       
   243 	if (separatorChar == (mTimeSeparatorList.at(0).unicode())->unicode()) {
       
   244 		value = 0;
       
   245 	} else if (separatorChar == (mTimeSeparatorList.at(1).unicode())->unicode()) {
       
   246 		value = 1;
       
   247 	} else {
       
   248 		// Nothing to do.
       
   249 	}
       
   250 
       
   251 	list = mTimeSeparatorList;
       
   252 
       
   253 	qDebug() << "clock: SettingsUtility::timeSeparator <--";
       
   254 
       
   255 	return value;
       
   256 }
       
   257 
       
   258 /*!
       
   259 
       
   260  */
       
   261 void SettingsUtility::setAutoUpdate(const QString &value)
       
   262 {
       
   263 	// TODO: implement the changes after server is in place.
       
   264 	// 1. Inform the server about the setting.
       
   265 	// 2. Get the status of the change from server
       
   266 	// 3. Emit the signal to inform the clients of the change in the settings item,
       
   267 	// pass the enum of settings item changed and the updated value.
       
   268 
       
   269 	if (0 == value.compare(tr("ON"), Qt::CaseInsensitive)) {
       
   270 		mTimeZoneClient->setTimeUpdateOn(true);
       
   271     } else {
       
   272     	mTimeZoneClient->setTimeUpdateOn(false);
       
   273     }
       
   274 
       
   275 	emit settingsChanged(AutoTimeUpdate, value);
       
   276 }
       
   277 
       
   278 /*!
       
   279 
       
   280  */
       
   281 int SettingsUtility::autoUpdate(QStringList &list)
       
   282 {
       
   283 	// TODO: implement properly. this is jst a dummy implementation.
       
   284 	int value = 1;
       
   285 
       
   286 	bool autoUpdate = mTimeZoneClient->timeUpdateOn();
       
   287 
       
   288 	if (autoUpdate) {
       
   289 	    value = 0;
       
   290     }
       
   291 	list = mAutoUpdateValueList;
       
   292 	return value;
       
   293 }
       
   294 
       
   295 /*!
       
   296 
       
   297  */
       
   298 void SettingsUtility::setDate(const QString &date)
       
   299 {
       
   300 	QDate newDate = QDate::fromString(date, dateFormatString());
       
   301 
       
   302 	if (newDate.isValid()) {
       
   303 		mTimeZoneClient->setDateTime(QDateTime(newDate, QTime::currentTime()));
       
   304     }
       
   305 }
       
   306 
       
   307 /*!
       
   308 
       
   309  */
       
   310 QString SettingsUtility::date()
       
   311 {
       
   312 	qDebug() << "clock: SettingsUtility::date -->";
       
   313 
       
   314 	qDebug() << "clock: SettingsUtility::date <--";
       
   315 
       
   316 	return QDate::currentDate().toString(dateFormatString());
       
   317 }
       
   318 
       
   319 /*!
       
   320 
       
   321  */
       
   322 void SettingsUtility::setDateFormat(const QString &format)
       
   323 {
       
   324 	TLocale locale;
       
   325 	int index;
       
   326 
       
   327 	for (index = 0; index < mDateFormatList.count(); ++index) {
       
   328 		if (format == mDateFormatList.at(index)) {
       
   329 			break;
       
   330 		}
       
   331 	}
       
   332 
       
   333 	switch (index) {
       
   334 		case 0:
       
   335 			locale.SetDateFormat(EDateEuropean);
       
   336 			break;
       
   337 		case 1:
       
   338 			locale.SetDateFormat(EDateAmerican);
       
   339 			break;
       
   340 		case 2:
       
   341 			locale.SetDateFormat(EDateJapanese);
       
   342 			break;
       
   343 		default:
       
   344 			//Nothing to do.
       
   345 			break;
       
   346 	}
       
   347 
       
   348 	locale.Set();
       
   349 }
       
   350 
       
   351 /*!
       
   352 
       
   353  */
       
   354 int SettingsUtility::dateFormat(QStringList &format)
       
   355 {
       
   356 	TLocale locale;
       
   357 	int index = -1;
       
   358 
       
   359 	switch (locale.DateFormat()) {
       
   360 		case EDateEuropean:
       
   361 			index = 0;
       
   362 			break;
       
   363 		case EDateAmerican:
       
   364 			index = 1;
       
   365 			break;
       
   366 		case EDateJapanese:
       
   367 			index = 2;
       
   368 			break;
       
   369 		default:
       
   370 			// Nothing yet.
       
   371 			break;
       
   372 	}
       
   373 
       
   374 	format = mDateFormatList;
       
   375 
       
   376 	return index;
       
   377 }
       
   378 
       
   379 /*!
       
   380 
       
   381  */
       
   382 void SettingsUtility::setDateSeparator(const QString &separator)
       
   383 {
       
   384 	TLocale locale;
       
   385 	locale.SetDateSeparator(
       
   386 			TChar(separator.unicode()->unicode()), 1);
       
   387 	locale.SetDateSeparator(
       
   388 			TChar(separator.unicode()->unicode()), 2);
       
   389 	locale.Set();
       
   390 }
       
   391 
       
   392 /*!
       
   393 
       
   394  */
       
   395 int SettingsUtility::dateSeparator(QStringList &separator)
       
   396 {
       
   397 	TLocale locale;
       
   398 	TChar separatorChar = locale.DateSeparator(1);
       
   399 
       
   400 	const int at0 = (mDateSeparatorList.at(0).unicode())->unicode();
       
   401 	int at1 = (mDateSeparatorList.at(1).unicode())->unicode();
       
   402 	int at2 = (mDateSeparatorList.at(2).unicode())->unicode();
       
   403 	int at3 = (mDateSeparatorList.at(3).unicode())->unicode();
       
   404 	int value = 0;
       
   405 
       
   406 	if (separatorChar == (mDateSeparatorList.at(0).unicode())->unicode()) {
       
   407 		value = 0;
       
   408 	} else if (separatorChar ==
       
   409 			(mDateSeparatorList.at(1).unicode())->unicode()) {
       
   410 		value = 1;
       
   411 	} else if (separatorChar ==
       
   412 			(mDateSeparatorList.at(2).unicode())->unicode()) {
       
   413 		value = 2;
       
   414 	} else if (separatorChar ==
       
   415 			(mDateSeparatorList.at(3).unicode())->unicode()) {
       
   416 		value = 3;
       
   417 	}
       
   418 
       
   419 	separator = mDateSeparatorList;
       
   420 
       
   421 	return value;
       
   422 }
       
   423 
       
   424 /*!
       
   425 	Sets a new list of workdays.
       
   426 
       
   427 	\param workdays The new list of workdays selected.
       
   428  */
       
   429 void SettingsUtility::setWorkdays(const QString &workdays)
       
   430 {
       
   431 	Q_UNUSED(workdays)
       
   432 }
       
   433 
       
   434 /*!
       
   435 	Returns the workdays list selected by the user.
       
   436 
       
   437 	\param workdaysList A string list of workdays selected in the locale.
       
   438 	\return int Index of the item selected.
       
   439  */
       
   440 int SettingsUtility::workdays(QStringList &workdaysList)
       
   441 {
       
   442 	Q_UNUSED(workdaysList)
       
   443 
       
   444 	return 0;
       
   445 }
       
   446 
       
   447 /*!
       
   448 	Returns a string based on the locale set, to format time.
       
   449 
       
   450 	\return QString String to be used to format time.
       
   451  */
       
   452 QString SettingsUtility::timeFormatString()
       
   453 {
       
   454 	QString format;
       
   455 	TLocale locale;
       
   456 	QStringList dummyList;
       
   457 
       
   458 	if (ETime24 == locale.TimeFormat()) {
       
   459 		format = QString("h:mm");
       
   460 	} else if (ETime12 == locale.TimeFormat()) {
       
   461 		format = QString("h:mm ap");
       
   462 	}
       
   463 
       
   464 	QString separator = mTimeSeparatorList.at(timeSeparator(dummyList));
       
   465 
       
   466 	format.replace(QString(":"), separator);
       
   467 
       
   468 	return format;
       
   469 }
       
   470 
       
   471 /*!
       
   472 	Returns a string based on the locale set, to format date.
       
   473 
       
   474 	\return QString String to be used to format date.
       
   475  */
       
   476 QString SettingsUtility::dateFormatString()
       
   477 {
       
   478 	QStringList dummyList;
       
   479 	QString format(mDateFormatList.at(dateFormat(dummyList)));
       
   480 	QString separator = mDateSeparatorList.at(dateSeparator(dummyList));
       
   481 
       
   482 	format.replace(QString(" "), separator);
       
   483 	format.replace(QString("mm"), QString("MM"));
       
   484 
       
   485 	return format;
       
   486 }
       
   487 
       
   488 /*!
       
   489 	Sets the snooze time value selected by the user.
       
   490 
       
   491 	\param index The index of the preset snooze time values.
       
   492  */
       
   493 void SettingsUtility::setSnoozeTime(int index)
       
   494 {
       
   495 	Q_UNUSED(index)
       
   496 }
       
   497 
       
   498 /*!
       
   499 	Returns a list of default snooze time values.
       
   500 
       
   501 	\param snoozeValueList A string list of preset values.
       
   502 	\return int The index of the item in the list to be focused.
       
   503  */
       
   504 int SettingsUtility::snoozeTime(QStringList &snoozeValueList)
       
   505 {
       
   506 	int index = -1;
       
   507 
       
   508 	snoozeValueList = mSnoozeValueList;
       
   509 	return index;
       
   510 }
       
   511 
       
   512 /*!
       
   513 	Sets the start of week value selected by the user.
       
   514 
       
   515 	\param index The index of the selected value.
       
   516  */
       
   517 void SettingsUtility::setStartOfWeek(int index)
       
   518 {
       
   519 	TLocale locale;
       
   520 	TDay day = (TDay)index;
       
   521 	locale.SetStartOfWeek(day);
       
   522 	locale.Set();
       
   523 }
       
   524 // End of file	--Don't remove this.