clock/clockui/clockviews/src/clockworldview.cpp
changeset 18 c198609911f9
child 26 a949c2543c15
child 45 b6db4fd4947b
equal deleted inserted replaced
0:f979ecb2b13e 18:c198609911f9
       
     1 /*
       
     2 * Copyright (c) 2009 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 * Definition file for class ClockWorldView.
       
    16 *
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <QGraphicsItem>
       
    21 #include <QStandardItem>
       
    22 #include <QStandardItemModel>
       
    23 #include <QDebug>
       
    24 #include <HbInstance>
       
    25 #include <HbLabel>
       
    26 #include <HbPushButton>
       
    27 #include <HbAbstractViewItem>
       
    28 #include <HbMenu>
       
    29 #include <HbAction>
       
    30 #include <HbStyleLoader>
       
    31 #include <HbListView>
       
    32 
       
    33 // User includes
       
    34 #include "clockworldview.h"
       
    35 #include "clockdocloader.h"
       
    36 #include "clockappcontrollerif.h"
       
    37 #include "clockcommon.h"
       
    38 #include "settingsutility.h"
       
    39 #include "timezoneclient.h"
       
    40 #include "clockcityselectionlist.h"
       
    41 #include "listitemprototype.h"
       
    42 
       
    43 /*!
       
    44 	\class ClockWorldView
       
    45 
       
    46 	The world clock view of the clock application.
       
    47  */
       
    48 
       
    49 /*!
       
    50 	Constructor.
       
    51 
       
    52 	\param parent The parent of type QGraphicsWidget.
       
    53  */
       
    54 ClockWorldView::ClockWorldView(QGraphicsItem *parent)
       
    55 :HbView(parent),
       
    56  mSelectedItem(-1)
       
    57 {
       
    58 	qDebug("clock: ClockWorldView::ClockWorldView() -->");
       
    59 
       
    60 	// Timer for updating list data upon time change/update.
       
    61 	mRefreshTimer = new QTimer();
       
    62 	connect(
       
    63 			mRefreshTimer, SIGNAL(timeout()),
       
    64 			this, SLOT(refreshCityList()));
       
    65 
       
    66 	// Create the model.
       
    67 	mCityListModel = new QStandardItemModel();
       
    68 
       
    69 	qDebug("clock: ClockWorldView::ClockWorldView() <--");
       
    70 }
       
    71 
       
    72 /*!
       
    73 	Destructor.
       
    74  */
       
    75 ClockWorldView::~ClockWorldView()
       
    76 {
       
    77 	qDebug("clock: ClockWorldView::~ClockWorldView() -->");
       
    78 
       
    79 	if (mDocLoader) {
       
    80 		delete mDocLoader;
       
    81 	}
       
    82 
       
    83 	qDebug("clock: ClockWorldView::~ClockWorldView() <--");
       
    84 }
       
    85 
       
    86 /*!
       
    87 	Called by the ClockViewManager after loading the view from the docml.
       
    88 	The initializaion/setup of the view is done here.
       
    89 
       
    90 	\param controller The ClockAppController object.
       
    91 	\param docLoader Pointer to ClockDocLoader object.
       
    92  */
       
    93 void ClockWorldView::setupView(
       
    94 		ClockAppControllerIf &controllerIf,
       
    95 		ClockDocLoader *docLoader)
       
    96 {
       
    97 	qDebug("clock: ClockWorldView::setupView() <--");
       
    98 
       
    99 	mDocLoader = docLoader;
       
   100 	mAppControllerIf = &controllerIf;
       
   101 
       
   102 	mTimezoneClient = controllerIf.timezoneClient();
       
   103 	mSettingsUtility = controllerIf.settingsUtility();
       
   104 
       
   105 	// Establish required connections.
       
   106 	connect(
       
   107 			mTimezoneClient, SIGNAL(timechanged()),
       
   108 			this, SLOT(updateClockDisplay()));
       
   109 	connect(
       
   110 			mTimezoneClient, SIGNAL(timechanged()),
       
   111 			this, SLOT(updateDayDateInfo()));
       
   112 	connect(
       
   113 			mTimezoneClient, SIGNAL(timechanged()),
       
   114 			this, SLOT(updateCurrentLocationInfo()));
       
   115 	connect(
       
   116 			mTimezoneClient, SIGNAL(timechanged()),
       
   117 			this, SLOT(updateCurrentZoneInfo()));
       
   118 
       
   119 	mDisplayWorldClockView = static_cast<HbAction *> (
       
   120 	            mDocLoader->findObject("worldClockAction"));
       
   121 
       
   122 	mDisplayWorldClockView->setCheckable(true);
       
   123 	mDisplayWorldClockView->setChecked(true);
       
   124 
       
   125 	connect(
       
   126 	        mDisplayWorldClockView, SIGNAL(changed()),
       
   127             this, SLOT(refreshWorldView()));
       
   128 
       
   129 	// Get the currently added locations to the list.
       
   130 	mCityInfoList = mTimezoneClient->getSavedLocations();
       
   131 
       
   132 	// Create the custom prototype.
       
   133 	ListItemPrototype *customPrototype = new ListItemPrototype();
       
   134 	mCityListView = qobject_cast<HbListView *> (
       
   135 			mDocLoader->findWidget("worldClockCityListView"));
       
   136 	HbStyleLoader::registerFilePath(":/style/");
       
   137 	mCityListView->setModel(mCityListModel);
       
   138 	mCityListView->setItemPrototype(customPrototype);
       
   139 	mCityListView->setLayoutName("custom");
       
   140 
       
   141 	mPlaceLabel =
       
   142 			qobject_cast<HbLabel *> (mDocLoader->findWidget("placeLabel"));
       
   143 //	mPlaceLabel->setTextWrapping(Hb::TextWordWrap);
       
   144 
       
   145 	mDayDateLabel =
       
   146 			qobject_cast<HbLabel *> (mDocLoader->findWidget("dateLabel"));
       
   147 
       
   148 	// Get the toolbar/menu actions.
       
   149 	mAddCityAction = static_cast<HbAction *> (
       
   150 			mDocLoader->findObject("addCityAction"));
       
   151 	mAddCityAction->setIcon(HbIcon(":/clock/add_new_city.svg"));
       
   152 	connect(
       
   153 			mAddCityAction, SIGNAL(triggered()),
       
   154 			this, SLOT(handleAddLocation()));
       
   155 
       
   156 	mShowAlarmsViewAction = static_cast<HbAction *> (
       
   157 			mDocLoader->findObject("alarmsAction"));
       
   158 	connect(
       
   159 			mShowAlarmsViewAction, SIGNAL(triggered()),
       
   160 			this, SLOT(showAlarmsView()));
       
   161 
       
   162 	HbMainWindow *window;
       
   163 	window = hbInstance->allMainWindows().first();
       
   164 	updateToolbarTexts(window->orientation());
       
   165 
       
   166 	connect(
       
   167 			window, SIGNAL(orientationChanged(Qt::Orientation)),
       
   168 			this, SLOT(updateToolbarTexts(Qt::Orientation)));
       
   169 
       
   170 	if (mCityInfoList.count() > 0) {
       
   171 		// There is atleast one city. Refresh needed.
       
   172 		// Calculate after how much time the refresh has to happen.
       
   173 		QTime currentTime = QDateTime::currentDateTime().time();
       
   174 		int after = 60 - currentTime.second();
       
   175 		mRefreshTimer->start(after * 1000);
       
   176 	}
       
   177 
       
   178 	// Update the date and day info.
       
   179 	updateDayDateInfo();
       
   180 
       
   181 	connect(
       
   182 			mCityListView,
       
   183 			SIGNAL(longPressed(HbAbstractViewItem *, const QPointF &)),
       
   184 			this,
       
   185 			SLOT(handleItemLongPressed(HbAbstractViewItem *, const QPointF &)));
       
   186 
       
   187 	// Populate the list.
       
   188 	int index = 0;
       
   189 	// Update current location info.
       
   190 	updateCurrentLocationInfo();
       
   191 
       
   192 	for (; index < mCityInfoList.count(); index++) {
       
   193 		addCityToList(mCityInfoList.at(index));
       
   194 	}
       
   195 
       
   196 	qDebug("clock: ClockWorldView::setupView() <--");
       
   197 }
       
   198 
       
   199 /*!
       
   200 	Refreshes the data in the city list.
       
   201  */
       
   202 void ClockWorldView::refreshCityList()
       
   203 {
       
   204 	int cityInfoCount = mCityInfoList.count();
       
   205 
       
   206 	if (cityInfoCount) {
       
   207 		QStandardItem *item = 0;
       
   208 		for (int infoIndex = 0; infoIndex < cityInfoCount; infoIndex++) {
       
   209 			item = mCityListModel->item(infoIndex);
       
   210 
       
   211 			QDateTime dateTime = QDateTime::currentDateTime();
       
   212 			dateTime = dateTime.toUTC();
       
   213 			dateTime = dateTime.addSecs(
       
   214 					mCityInfoList.at(infoIndex).zoneOffset * 60);
       
   215 
       
   216 			// Display day/night indicators.
       
   217 			QString dayNightIconPath = "";
       
   218 			if (isDay(dateTime)) {
       
   219 				dayNightIconPath = ":/clock/day";
       
   220 			} else {
       
   221 				dayNightIconPath = ":/clock/night";
       
   222 			}
       
   223 			item->setData(dayNightIconPath, Qt::UserRole + 1000);
       
   224 
       
   225 			// Update the date info.
       
   226 			QString dateInfo = dateTime.toString(
       
   227 					mSettingsUtility->dateFormatString());
       
   228 			if (dateTime.date() == QDate::currentDate()) {
       
   229 				dateInfo = tr("Today");
       
   230 			}
       
   231 			item->setData(dateInfo, Qt::UserRole + 1001);
       
   232 
       
   233 			// Set the DST icon.
       
   234 			QString dstIconPath = "";
       
   235 			if (mCityInfoList.at(infoIndex).dstOn) {
       
   236 				dstIconPath = ":/clock/dst_icon";
       
   237 			}
       
   238 			item->setData(dstIconPath, Qt::UserRole + 1004);
       
   239 
       
   240 			// Update the time info.
       
   241 			QString timeInfo = dateTime.toString(
       
   242 					mSettingsUtility->timeFormatString());
       
   243 			item->setData(timeInfo, Qt::UserRole + 1005);
       
   244 		}
       
   245 
       
   246 		// Start the timer again for one minute.
       
   247 		QTimer::singleShot(60 * 1000, this, SLOT(refreshCityList()));
       
   248 	}
       
   249 }
       
   250 
       
   251 /*!
       
   252 	Updates the clock display.
       
   253  */
       
   254 void ClockWorldView::updateClockDisplay()
       
   255 {
       
   256 //	mSkinnableClock->updateDisplay(true);
       
   257 }
       
   258 
       
   259 /*!
       
   260 	Updates the day-date info.
       
   261  */
       
   262 void ClockWorldView::updateDayDateInfo()
       
   263 {
       
   264 	// Get the current datetime.
       
   265 	QDateTime dateTime = QDateTime::currentDateTime();
       
   266 	QString dayName = dateTime.toString("dddd");
       
   267 
       
   268 	QString currentDate = mSettingsUtility->date();
       
   269 	// Construct the day + date string.
       
   270 	QString dayDateString;
       
   271 	dayDateString+= dayName;
       
   272 	dayDateString += " ";
       
   273 	dayDateString += currentDate;
       
   274 
       
   275 	mDayDateLabel->clear();
       
   276 	mDayDateLabel->setPlainText(dayDateString);
       
   277 }
       
   278 
       
   279 /*!
       
   280 	Updates the current location info.
       
   281  */
       
   282 void ClockWorldView::updateCurrentLocationInfo()
       
   283 {
       
   284 	// Get the updated home city.
       
   285 	LocationInfo homeCity = mTimezoneClient->getCurrentZoneInfoL();
       
   286 
       
   287 	// Construct the GMT +/- X string.
       
   288 	QString gmtOffset;
       
   289 
       
   290 	int utcOffset = homeCity.zoneOffset;
       
   291 	int offsetInHours (utcOffset/60);
       
   292 	int offsetInMinutes (utcOffset%60);
       
   293 
       
   294 	// Check wether the offset is +ve or -ve.
       
   295 	if (0 < utcOffset) {
       
   296 		// We have a positive offset. Append the '+' character.
       
   297 		gmtOffset += tr(" +");
       
   298 	} else if (0 > utcOffset) {
       
   299 		// We have a negative offset. Append the '-' character.
       
   300 		gmtOffset += tr(" -");
       
   301 		offsetInHours = -offsetInHours;
       
   302 	} else {
       
   303 		// We dont have an offset. We are at GMT zone.
       
   304 	}
       
   305 
       
   306 	// Append the hour component.
       
   307 	gmtOffset += QString::number(offsetInHours);
       
   308 
       
   309 	// Get the time separator from settings and append it.
       
   310 	QStringList timeSeparatorList;
       
   311 	int index = mSettingsUtility->timeSeparator(timeSeparatorList);
       
   312 	gmtOffset += timeSeparatorList.at(index);
       
   313 
       
   314 	// Append the minute component.
       
   315 	// If minute component is less less than 10, append a '00'
       
   316 	if (0 <= offsetInMinutes && offsetInMinutes < 10) {
       
   317 		gmtOffset += tr("00");
       
   318 	} else {
       
   319 		gmtOffset += QString::number(offsetInMinutes);
       
   320 	}
       
   321 
       
   322 	// Append space.
       
   323 	gmtOffset += tr(" ");
       
   324 
       
   325 	// Append GMT sting.
       
   326 	gmtOffset += hbTrId("txt_common_common_gmt");
       
   327 
       
   328 	// Append space.
       
   329 	gmtOffset += tr(" ");
       
   330 
       
   331 	// Append DST info.
       
   332 	if (homeCity.dstOn) {
       
   333 		gmtOffset += hbTrId("txt_common_setlabel_dst");
       
   334 	}
       
   335 
       
   336 	// Set the city and country name .
       
   337 	mPlaceLabel->clear();
       
   338 	if (mTimezoneClient->timeUpdateOn()) {
       
   339 		mPlaceLabel->setPlainText(homeCity.countryName + tr(" ") + gmtOffset);
       
   340 	} else {
       
   341 		mPlaceLabel->setPlainText(
       
   342 				homeCity.cityName + tr(", ") + homeCity.countryName
       
   343 				+ tr(" ") + gmtOffset);
       
   344 	}
       
   345 }
       
   346 
       
   347 void ClockWorldView::handleItemLongPressed(
       
   348 		HbAbstractViewItem *item, const QPointF &coords)
       
   349 {
       
   350 	// Get the ndex of the selected item.
       
   351 	mSelectedItem = item->modelIndex().row();
       
   352 
       
   353 	// On long press we display item specific context menu.
       
   354 	HbMenu *itemContextMenu = new HbMenu();
       
   355 
       
   356 	// Add the actions to the context menu.
       
   357 	mSetCurrentLocationAction = itemContextMenu->addAction(
       
   358 	    hbTrId("txt_clk_menu_set_as_current_location"));
       
   359 	mRemoveCityAction = itemContextMenu->addAction(
       
   360 	    hbTrId("txt_clk_menu_delete"));
       
   361 
       
   362 	connect(
       
   363 			mSetCurrentLocationAction, SIGNAL(triggered()),
       
   364 			this, SLOT(handleSetAsCurrentLocationAction()));
       
   365 	connect(
       
   366 			mRemoveCityAction, SIGNAL(triggered()),
       
   367 			this, SLOT(handleDeleteAction()));
       
   368 
       
   369 	// Show the menu.
       
   370 	itemContextMenu->exec(coords);
       
   371 }
       
   372 
       
   373 /*!
       
   374 	This slot is called when Add location action is triggered. It is responsible
       
   375 	for launching the city selection list.
       
   376  */
       
   377 void ClockWorldView::handleAddLocation()
       
   378 {
       
   379 	// Construct the city selection list and show the same.
       
   380 	mCitySelectionList = new ClockCitySelectionList(mTimezoneClient, this);
       
   381 	connect(
       
   382 			mCitySelectionList, SIGNAL(citySelected(LocationInfo)),
       
   383 			this, SLOT(handleCitySelected(LocationInfo)));
       
   384 
       
   385 	// Show the city list.
       
   386 	mCitySelectionList->showCityList();
       
   387 }
       
   388 
       
   389 /*!
       
   390 	This slot handles delete action on the city list. It removes the item from
       
   391 	the view.
       
   392  */
       
   393 void ClockWorldView::handleDeleteAction()
       
   394 {
       
   395 	if (-1 != mSelectedItem) {
       
   396 		int temp = mCityListModel->rowCount();
       
   397 
       
   398 		QStandardItem *item = mCityListModel->takeItem(mSelectedItem);
       
   399 		mCityListModel->removeRow(mSelectedItem);
       
   400 		delete item;
       
   401 
       
   402 		temp = mCityListModel->rowCount();
       
   403 
       
   404 		mCityInfoList.removeAt(mSelectedItem);
       
   405 		mSelectedItem = -1;
       
   406 
       
   407 		// Update the data file.
       
   408 		mTimezoneClient->saveLocations(mCityInfoList);
       
   409 
       
   410 		if (mCityInfoList.count() <= 0) {
       
   411 			// No need to refresh the list anymore.
       
   412 			mRefreshTimer->stop();
       
   413 		}
       
   414 	}
       
   415 }
       
   416 
       
   417 /*!
       
   418 	This slot sets the selected city as the current location.
       
   419  */
       
   420 void ClockWorldView::handleSetAsCurrentLocationAction()
       
   421 {
       
   422 	// Check if time update is set to ON.
       
   423 	// If yes, reset it to OFF and change the home location.
       
   424 	if (mTimezoneClient->timeUpdateOn()) {
       
   425 		mTimezoneClient->setTimeUpdateOn(false);
       
   426 	} else {
       
   427 		// The current location and home city should be swapped.
       
   428 		// Store the info of current home city.
       
   429 		LocationInfo oldHomeCity = mTimezoneClient->getCurrentZoneInfoL();
       
   430 		// Get the info of the selected item.
       
   431 		LocationInfo newHomeCity = mCityInfoList[mSelectedItem];
       
   432 
       
   433 		QStandardItem *cityItem = mCityListModel->item(mSelectedItem);
       
   434 		QDateTime dateTime = QDateTime::currentDateTime();
       
   435 
       
   436 		// Display day/night indicators.
       
   437 		QString dayNightIconPath = "";
       
   438 		if (isDay(dateTime)) {
       
   439 			dayNightIconPath = ":/clock/day";
       
   440 		} else {
       
   441 			dayNightIconPath = ":/clock/night";
       
   442 		}
       
   443 		cityItem->setData(dayNightIconPath, Qt::UserRole + 1000);
       
   444 
       
   445 		// Date.
       
   446 		QString dateString = dateTime.toString(
       
   447 				mSettingsUtility->dateFormatString());
       
   448 		if (dateTime.date() == QDate::currentDate()) {
       
   449 			dateString = tr("Today");
       
   450 			}
       
   451 		cityItem->setData(dateString, Qt::UserRole + 1001);
       
   452 
       
   453 		// The city/country name.
       
   454 		QString placeInfo =
       
   455 				oldHomeCity.cityName + tr(", ") + oldHomeCity.countryName;
       
   456 		cityItem->setData(placeInfo, Qt::UserRole + 1002);
       
   457 
       
   458 		// Dst icon.
       
   459 		QString dstIconPath = "";
       
   460 		if (oldHomeCity.dstOn) {
       
   461 			dstIconPath = ":/clock/dst_icon";
       
   462 		}
       
   463 		cityItem->setData(dstIconPath, Qt::UserRole + 1004);
       
   464 
       
   465 		// Time.
       
   466 		QString timeString = dateTime.toString(
       
   467 				mSettingsUtility->timeFormatString());
       
   468 		cityItem->setData(timeString, Qt::UserRole + 1005);
       
   469 
       
   470 		// Update the info list.
       
   471 		mCityInfoList.removeAt(mSelectedItem);
       
   472 		mCityInfoList.insert(mSelectedItem, oldHomeCity);
       
   473 		// Update the home city with the timezone server.
       
   474 		mTimezoneClient->setAsCurrentLocationL(newHomeCity);
       
   475 
       
   476 		// Update the current location info.
       
   477 		updateCurrentLocationInfo();
       
   478 
       
   479 		// Update the offset difference in each list item.
       
   480 		updateOffsetDifferences();
       
   481 	}
       
   482 
       
   483 	// Update the data file.
       
   484 	mTimezoneClient->saveLocations(mCityInfoList);
       
   485 	mSelectedItem = -1;
       
   486 }
       
   487 
       
   488 /*!
       
   489 	Slot to handle the case when a city has been selected in from the city
       
   490 	selection list.
       
   491 
       
   492 	\param info of type LocationInfo which contains the city selected.
       
   493  */
       
   494 void ClockWorldView::handleCitySelected(LocationInfo info)
       
   495 {
       
   496 	// Info is invalid if the timezoneId is set to -1. We don't do anything in
       
   497 	// that case.
       
   498 	if (-1 != info.timezoneId) {
       
   499 		// Now we check if the city is already added in the list.
       
   500 		bool proceed = true;
       
   501 		for (int i = 0; i < mCityInfoList.count(); i++) {
       
   502 			if (info.timezoneId == mCityInfoList.at(i).timezoneId) {
       
   503 				proceed = false;
       
   504 				break;
       
   505 			}
       
   506 		}
       
   507 
       
   508 		// Check if the selected city is not the home city.
       
   509 		if (info.timezoneId
       
   510 				== mTimezoneClient->getCurrentZoneInfoL().timezoneId) {
       
   511 			proceed = false;
       
   512 		}
       
   513 
       
   514 		if (proceed) {
       
   515 			// This is new city totally. So add it.
       
   516 			// Add the city information to the list
       
   517 			mCityInfoList.append(info);
       
   518 			QModelIndex index = addCityToList(info);
       
   519 			mCityListView->scrollTo(index);
       
   520 			mTimezoneClient->saveLocations(mCityInfoList);
       
   521 
       
   522 			// Start the refresh timer if it was stopped previously.
       
   523 			if (!mRefreshTimer->isActive()) {
       
   524 				// Calculate after how much time the refresh has to happen.
       
   525 				QTime currentTime = QDateTime::currentDateTime().time();
       
   526 				int after = 60 - currentTime.second();
       
   527 				mRefreshTimer->start(after * 1000);
       
   528 			}
       
   529 		}
       
   530 	}
       
   531 
       
   532 	// Cleanup.
       
   533 	mCitySelectionList->deleteLater();
       
   534 }
       
   535 
       
   536 /*!
       
   537 	Navigates to the clock alarms view.
       
   538  */
       
   539 
       
   540 void ClockWorldView::showAlarmsView()
       
   541 {
       
   542 	qDebug() << "clock: ClockWorldView::showAlarmsView -->";
       
   543 
       
   544 	mAppControllerIf->switchToView(MainView);
       
   545 
       
   546 	qDebug() << "clock: ClockWorldView::showAlarmsView <--";
       
   547 }
       
   548 
       
   549 /*!
       
   550 	Updates the offset difference shown in each item with respect to the home
       
   551 	city.
       
   552  */
       
   553 void ClockWorldView::updateOffsetDifferences()
       
   554 {
       
   555 	// Get the home city information.
       
   556 	LocationInfo homeCityInfo = mTimezoneClient->getCurrentZoneInfoL();
       
   557 
       
   558 	for (int iter = 0; iter < mCityListModel->rowCount(); iter++) {
       
   559 		QModelIndex index = mCityListModel->index(iter, 0);
       
   560 		LocationInfo cityInfo = mCityInfoList[iter];
       
   561 
       
   562 		// Find out if the city being added has an offset greater than or less
       
   563 		// than the homecity offset.
       
   564 		QString offsetDifference;
       
   565 		if (cityInfo.zoneOffset < homeCityInfo.zoneOffset) {
       
   566 			offsetDifference += "-";
       
   567 		} else if (cityInfo.zoneOffset > homeCityInfo.zoneOffset) {
       
   568 			offsetDifference += "+";
       
   569 		}
       
   570 		// Now get the hours and minutes.
       
   571 		int difference =
       
   572 				qAbs(homeCityInfo.zoneOffset - cityInfo.zoneOffset);
       
   573 		int hours = difference / 60;
       
   574 		int minutes = difference % 60;
       
   575 		offsetDifference += QString::number(hours);
       
   576 		offsetDifference += "hrs";
       
   577 		if (minutes) {
       
   578 			offsetDifference += ", ";
       
   579 			offsetDifference += QString::number(minutes);
       
   580 			offsetDifference += "mins";
       
   581 		}
       
   582 		// TODO : Need to enable these code once we recieve the localisation.
       
   583 		/*QString displayFormat = tr("%1hrs, %2mins");
       
   584 		QString offsetString = displayFormat.arg(hours, minutes);
       
   585 		offsetDifference += offsetString;*/
       
   586 		mCityListModel->setData(index, offsetDifference, Qt::UserRole + 1003);
       
   587 	}
       
   588 }
       
   589 
       
   590 /*!
       
   591     Slot which gets called when `World Clock' action is triggered from the view
       
   592     toolbar. This is responsible for reloading the content of the world clock view.
       
   593  */
       
   594 void ClockWorldView::refreshWorldView()
       
   595 {
       
   596     qDebug() << "clock: ClockWorldView::refreshWorldView -->";
       
   597 
       
   598 	mDisplayWorldClockView->setChecked(true);
       
   599 
       
   600     qDebug() << "clock: ClockWorldView::refreshWorldView <--";
       
   601 }
       
   602 
       
   603 /*!
       
   604 	Slot to handle orientation changes
       
   605  */
       
   606 void ClockWorldView::updateToolbarTexts(Qt::Orientation currentOrientation)
       
   607 {
       
   608 	if (Qt::Horizontal == currentOrientation) {
       
   609 		// Display toolbar item's texts
       
   610 		// TODO to use text ids from ts file.
       
   611 		mShowAlarmsViewAction->setText(tr("Alarms"));
       
   612 		mDisplayWorldClockView->setText(tr("World clock"));
       
   613 		mAddCityAction->setText(tr("Add city"));
       
   614 	} else if(Qt::Vertical == currentOrientation){
       
   615 		// Remove toolbar item's texts as only icons are shown.
       
   616 		// TODO to use text ids from ts file.
       
   617 		mShowAlarmsViewAction->setText(tr(""));
       
   618 		mDisplayWorldClockView->setText(tr(""));
       
   619 		mAddCityAction->setText("");
       
   620 	}
       
   621 }
       
   622 
       
   623 /*!
       
   624 	Adds the location info to the city list.
       
   625 
       
   626 	\param locationInfo Details of the city to be added to the list.
       
   627  */
       
   628 QModelIndex ClockWorldView::addCityToList(const LocationInfo& locationInfo)
       
   629 {
       
   630 	// Here we construct a model item and add it to the list model.
       
   631 	QStandardItem *modelItem = new QStandardItem();
       
   632 
       
   633 	QDateTime dateTime = QDateTime::currentDateTime();
       
   634 	dateTime = dateTime.toUTC();
       
   635 	dateTime = dateTime.addSecs(locationInfo.zoneOffset * 60);
       
   636 
       
   637 	// Display day/night indicators.
       
   638 	QString dayNightIconPath = "";
       
   639 	if (isDay(dateTime)) {
       
   640 		dayNightIconPath = ":/clock/day";
       
   641 	} else {
       
   642 		dayNightIconPath = ":/clock/night";
       
   643 	}
       
   644 	modelItem->setData(dayNightIconPath, Qt::UserRole + 1000);
       
   645 
       
   646 	// Show the date. If date is current date then show 'today'.
       
   647 	QString dateInfo = dateTime.toString(mSettingsUtility->dateFormatString());
       
   648 	if (dateTime.date() == QDate::currentDate()) {
       
   649 		dateInfo = tr("Today");
       
   650 	}
       
   651 	modelItem->setData(dateInfo, Qt::UserRole + 1001);
       
   652 
       
   653 	// Show the city and country name.
       
   654 	QString placeInfo =
       
   655 			locationInfo.cityName + tr(", ") + locationInfo.countryName;
       
   656 	modelItem->setData(placeInfo, Qt::UserRole + 1002);
       
   657 
       
   658 	// Get the homecity information.
       
   659 	LocationInfo homeCityInfo = mTimezoneClient->getCurrentZoneInfoL();
       
   660 	// Find out if the city being added has an offset greater than or less than
       
   661 	// the homecity offset.
       
   662 	QString offsetDifference;
       
   663 	if (locationInfo.zoneOffset < homeCityInfo.zoneOffset) {
       
   664 		offsetDifference += "-";
       
   665 	} else if (locationInfo.zoneOffset > homeCityInfo.zoneOffset) {
       
   666 		offsetDifference += "+";
       
   667 	}
       
   668 	// Now get the hours and minutes.
       
   669 	int difference = qAbs(homeCityInfo.zoneOffset - locationInfo.zoneOffset);
       
   670 	int hours = difference / 60;
       
   671 	int minutes = difference % 60;
       
   672 
       
   673 	if ( hours && minutes ) {
       
   674 		if (hours == 1) {
       
   675 			QString displayFormat =
       
   676 					hbTrId("txt_clock_dblist_daily_val_1_hr_2_mins");
       
   677 			QString offsetString = displayFormat.arg(hours).arg(minutes);
       
   678 			offsetDifference += offsetString;
       
   679 		}
       
   680 		else {
       
   681 			QString displayFormat =
       
   682 					hbTrId("txt_clock_dblist_daily_val_1_hrs_2_mins");
       
   683 			QString offsetString = displayFormat.arg(hours).arg(minutes);
       
   684 			offsetDifference += offsetString;
       
   685 		}
       
   686 	}
       
   687 	else if ( hours ){
       
   688 		if(hours == 1 ) {
       
   689 			QString displayFormat = hbTrId("txt_clock_dblist_val_1_hr");
       
   690 			QString offsetString = displayFormat.arg(hours);
       
   691 			offsetDifference += offsetString;
       
   692 		}
       
   693 		else {
       
   694 			QString displayFormat = hbTrId("txt_clock_dblist_val_1_hrs");
       
   695 			QString offsetString = displayFormat.arg(hours);
       
   696 			offsetDifference += offsetString;
       
   697 		}
       
   698 	}
       
   699 	else if (minutes){
       
   700 		QString displayFormat = hbTrId("txt_clock_dblist_val_1_mins");
       
   701 		QString offsetString = displayFormat.arg(minutes);
       
   702 		offsetDifference += offsetString;
       
   703 	}
       
   704 
       
   705 	modelItem->setData(offsetDifference, Qt::UserRole + 1003);
       
   706 
       
   707 	// Show dst icon when needed.
       
   708 	QString dstIconPath = "";
       
   709 	if (locationInfo.dstOn) {
       
   710 		dstIconPath = ":/clock/dst_icon";
       
   711 	}
       
   712 	modelItem->setData(dstIconPath, Qt::UserRole + 1004);
       
   713 
       
   714 	// Show the time at that location.
       
   715 	QString timeInfo = dateTime.toString(mSettingsUtility->timeFormatString());
       
   716 	modelItem->setData(timeInfo, Qt::UserRole + 1005);
       
   717 
       
   718 	// Add the item to the model.
       
   719 	mCityListModel->appendRow(modelItem);
       
   720 
       
   721 	return(mCityListModel->indexFromItem(modelItem));
       
   722 }
       
   723 
       
   724 /*!
       
   725 	Checks if the given time is day or night.
       
   726 	6:00 AM and 6:00 PM is considered as day. Otherwise night.
       
   727 
       
   728 	\param dateTime Time for which check has to be performed.
       
   729 	\return bool True if it is day otherwise false.
       
   730  */
       
   731 bool ClockWorldView::isDay(QDateTime dateTime)
       
   732 {
       
   733 	// It is day between 6:00 AM and 6:00 PM. Otherwise night.
       
   734 	if (17 < dateTime.time().hour() || 6 > dateTime.time().hour()) {
       
   735 		return false;
       
   736 	}
       
   737 	return true;
       
   738 }
       
   739 
       
   740 // End of file-- Don't delete.