clock/ftudatetimewizard/src/ftudatetimeview.cpp
branchGCC_SURGE
changeset 54 e6894b852bc6
parent 40 4b686cfad39d
parent 53 e08ac1a3ba2b
equal deleted inserted replaced
40:4b686cfad39d 54:e6894b852bc6
     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: FTU wizard plugin view implementaion
       
    15  *
       
    16  */
       
    17 
       
    18 // System includes
       
    19 #include <QObject>
       
    20 #include <QDate>
       
    21 #include <QTime>
       
    22 #include <qabstractitemmodel.h>
       
    23 #include <hbdataform.h>
       
    24 #include <hbview.h>
       
    25 #include <hbdataformmodel.h>
       
    26 #include <hbpushbutton.h>
       
    27 #include <hbdataformviewitem.h>
       
    28 #include <hbdatetimepicker.h>
       
    29 #include <hbaction.h>
       
    30 #include <xqsettingsmanager.h>
       
    31 #include <xqsettingskey.h>
       
    32 
       
    33 // User includes
       
    34 #include "settingsutility.h"
       
    35 #include "timezoneclient.h"
       
    36 #include "ftudatetimeview.h"
       
    37 #include "ftudatetimecustomitem.h"
       
    38 #include "ftudatetimeprivatecrkeys.h"
       
    39 
       
    40 /*!
       
    41     \class FtuDateTimeView
       
    42     This is the view class for the FTU plugin
       
    43  */
       
    44 /*!
       
    45     \enum FtuDateTimeView::dateTimePicker
       
    46     This enum defines different pickers to be launched(date picker/time picker)
       
    47  */
       
    48 /*!
       
    49     \var FtuDateTimeView::datePicker
       
    50     Date picker.
       
    51  */
       
    52 /*!
       
    53     \var FtuDateTimeView::timePicker
       
    54     Time picker.
       
    55  */
       
    56 
       
    57 /*!
       
    58     Constructor.
       
    59  */
       
    60 FtuDateTimeView::FtuDateTimeView() :
       
    61 HbView(),
       
    62 mDatePicker(NULL),
       
    63 mTimePicker(NULL)
       
    64 {
       
    65 	//TODO: Localisation has to be done
       
    66 	// set title for wizard
       
    67 	setTitle("Date and Time");
       
    68 	mDateTimePlaceForm = new HbDataForm();
       
    69 
       
    70 	QList <HbAbstractViewItem*> prototypes = mDateTimePlaceForm->itemPrototypes();
       
    71 	FtuDateTimeCustomItem *customItem =
       
    72 			new FtuDateTimeCustomItem(mDateTimePlaceForm);
       
    73 	prototypes.append(customItem);
       
    74 	mDateTimePlaceForm->setItemPrototypes(prototypes);
       
    75 
       
    76 	// Construct the settings utility.
       
    77 	mSettingsUtility = new SettingsUtility();
       
    78 	mTimeZoneClient = TimezoneClient::getInstance();
       
    79 	mTimeAutoUpdate = mTimeZoneClient->timeUpdateOn();
       
    80 }
       
    81 
       
    82 /*!
       
    83     Destructor.
       
    84  */
       
    85 FtuDateTimeView::~FtuDateTimeView()
       
    86 {
       
    87 	delete mDateTimePlaceForm;
       
    88 	delete mDateTimePlaceModel;
       
    89 	delete mSettingsUtility;
       
    90 
       
    91 	if (!mTimeZoneClient->isNull()) {
       
    92 		mTimeZoneClient->deleteInstance();
       
    93 	}
       
    94 }
       
    95 /*!
       
    96     Creates the main view.
       
    97  */
       
    98 void FtuDateTimeView::constructView()
       
    99 {
       
   100 	createMainLayout();
       
   101 	setWidget(mDateTimePlaceForm);
       
   102 }
       
   103 
       
   104 /*!
       
   105     Implemantation to create the main layout using the dataform.
       
   106  */
       
   107 void FtuDateTimeView::createMainLayout()
       
   108 {
       
   109 	if (mDateTimePlaceForm->model()) {
       
   110 		delete mDateTimePlaceForm->model();
       
   111 		mDateTimePlaceForm->setModel(0);
       
   112 	}
       
   113 
       
   114 	mDateTimePlaceModel = new HbDataFormModel();
       
   115 
       
   116 	// Populates the datetime and place groups
       
   117 	populateDateTimeGroup();
       
   118 	populatePlaceGroup();
       
   119 
       
   120 	setItemDisplayed();
       
   121 	mDateTimePlaceForm->setModel(mDateTimePlaceModel);
       
   122 }
       
   123 
       
   124 /*!
       
   125     Populates the Date and Time Group.
       
   126  */
       
   127 void FtuDateTimeView::populateDateTimeGroup()
       
   128 {
       
   129 	HbDataFormModelItem *dateTimeGroup =
       
   130 			mDateTimePlaceModel->appendDataFormGroup
       
   131 			(QString(tr("Date and Time")),
       
   132 			 mDateTimePlaceModel->invisibleRootItem());
       
   133 
       
   134 	//Custom Date item
       
   135 	mDateItem = mDateTimePlaceModel->appendDataFormItem(
       
   136 								HbDataFormModelItem::CustomItemBase,
       
   137 								QString(tr("Date")), dateTimeGroup);
       
   138 	// Custom Time item
       
   139 	mTimeItem = mDateTimePlaceModel->appendDataFormItem(
       
   140 								HbDataFormModelItem::CustomItemBase,
       
   141 								QString(tr("Time")), dateTimeGroup);
       
   142 	// Auto TimeUpdate item
       
   143 	mAutoTimeUpdateItem = mDateTimePlaceModel->appendDataFormItem(
       
   144 								HbDataFormModelItem::CustomItemBase,
       
   145 								QString(tr("Time Autoupdate")), dateTimeGroup);
       
   146 
       
   147 	// Connect the items to the proper slots
       
   148 	mDateTimePlaceForm->addConnection(mDateItem, SIGNAL(clicked()), this,
       
   149 								SLOT(populateDatePicker()));
       
   150 	mDateTimePlaceForm->addConnection(mTimeItem, SIGNAL(clicked()), this,
       
   151 								SLOT(populateTimePicker()));
       
   152 	mDateTimePlaceForm->addConnection(mAutoTimeUpdateItem, SIGNAL(clicked()),
       
   153 								this, SLOT(setAutoTimeupDate()));
       
   154 }
       
   155 
       
   156 /*!
       
   157     Populates the Place Group.
       
   158  */
       
   159 void FtuDateTimeView::populatePlaceGroup()
       
   160 {
       
   161 	mPlaceGroup = mDateTimePlaceModel->appendDataFormGroup(QString(tr("Place")),
       
   162 									mDateTimePlaceModel->invisibleRootItem());
       
   163 	// Custom country item
       
   164 	mCountryItem = mDateTimePlaceModel->appendDataFormItem(
       
   165 									HbDataFormModelItem::CustomItemBase,
       
   166 									QString(tr("Country")), mPlaceGroup);
       
   167 	// Custom city item
       
   168 	mCityItem = mDateTimePlaceModel->appendDataFormItem(
       
   169 									HbDataFormModelItem::CustomItemBase,
       
   170 									QString(tr("City")), mPlaceGroup);
       
   171 
       
   172 	// Connect the items to the proper slots
       
   173 	mDateTimePlaceForm->addConnection(mCountryItem, SIGNAL(clicked()), this,
       
   174 									SLOT(populateCitySelectionList()));
       
   175 	mDateTimePlaceForm->addConnection(mCityItem, SIGNAL(clicked()), this,
       
   176 									SLOT(populateCitySelectionList()));
       
   177 
       
   178 
       
   179 }
       
   180 
       
   181 /*!
       
   182     Sets the item index.
       
   183  */
       
   184 void FtuDateTimeView::setItemDisplayed()
       
   185 {
       
   186 	// Display the items with proper data
       
   187 	mDateItem->setContentWidgetData("text", mSettingsUtility->date());
       
   188 	mTimeItem->setContentWidgetData("text", mSettingsUtility->time());
       
   189 
       
   190 	if (mTimeAutoUpdate) {
       
   191 		mAutoTimeUpdateItem->setContentWidgetData("text", tr("ON"));
       
   192 	} else {
       
   193 		mAutoTimeUpdateItem->setContentWidgetData("text", tr("OFF"));
       
   194 	}
       
   195 	mCountryItem->setContentWidgetData("text",
       
   196 							mTimeZoneClient->getCurrentZoneInfoL().countryName);
       
   197 	mCityItem->setContentWidgetData("text",
       
   198 							mTimeZoneClient->getCurrentZoneInfoL().cityName);
       
   199 
       
   200 	// Set the date,time,country and city fields disable
       
   201 	// if auto time update is ON
       
   202 	if (mTimeAutoUpdate) {
       
   203 		mDateItem->setEnabled(false);
       
   204 		mTimeItem->setEnabled(false);
       
   205 		mCountryItem->setEnabled(false);
       
   206 		mCityItem->setEnabled(false);
       
   207 	}
       
   208 }
       
   209 
       
   210 /*!
       
   211     Populates the Date Picker.
       
   212  */
       
   213 void FtuDateTimeView::populateDatePicker()
       
   214 {
       
   215 	if (mDatePickerDialog) {
       
   216 		delete mDatePickerDialog;
       
   217 	}
       
   218 	mDatePickerDialog = new HbDialog();
       
   219 
       
   220 	mDatePickerDialog->setDismissPolicy(HbDialog::NoDismiss);
       
   221 	mDatePickerDialog->setTimeout(HbDialog::NoDismiss);
       
   222 
       
   223 	if(mDatePicker) {
       
   224 		mDatePicker = NULL;
       
   225 	}
       
   226 	mDatePicker = new HbDateTimePicker(QDate::currentDate(),
       
   227 	                                   this);
       
   228 	mDatePicker->setMinimumDate(QDate::fromString("01/01/1900", "dd/MM/yyyy"));
       
   229 	mDatePicker->setMaximumDate(QDate::fromString("31/12/2100", "dd/MM/yyyy"));
       
   230 
       
   231 	mDatePickerDialog->setContentWidget(mDatePicker);
       
   232 
       
   233 	// Add ok and cancel actions.
       
   234 	mOkAction = new HbAction(
       
   235 			hbTrId("txt_common_button_ok"), mDatePickerDialog);
       
   236 	mCancelAction =  new HbAction(
       
   237 			hbTrId("txt_common_button_cancel"), mDatePickerDialog);
       
   238 
       
   239 	mDatePickerDialog->addAction(mOkAction);
       
   240 	mDatePickerDialog->addAction(mCancelAction);
       
   241 
       
   242 	mDatePickerDialog->open(this, SLOT(selectedAction(HbAction *)));
       
   243 }
       
   244 
       
   245 /*!
       
   246     Populates the Time Picker.
       
   247  */
       
   248 void FtuDateTimeView::populateTimePicker()
       
   249 {
       
   250 	if (mTimePickerDialog) {
       
   251 		delete mTimePickerDialog;
       
   252 	}
       
   253 	mTimePickerDialog = new HbDialog();
       
   254 	mTimePickerDialog->setDismissPolicy(HbDialog::NoDismiss);
       
   255 	mTimePickerDialog->setTimeout(HbDialog::NoDismiss);
       
   256 
       
   257 	if(mTimePicker) {
       
   258 		mTimePicker = NULL;
       
   259 	}
       
   260 	mTimePicker = new HbDateTimePicker(
       
   261 			QTime().currentTime(), this);
       
   262 	mTimePicker->setDisplayFormat(mSettingsUtility->timeFormatString());
       
   263 
       
   264 	mTimePickerDialog->setContentWidget(mTimePicker);
       
   265 
       
   266 	// Add ok and cancel actions.
       
   267 	mOkAction = new HbAction(
       
   268 			hbTrId("txt_common_button_ok"), mTimePickerDialog);
       
   269 
       
   270 	mCancelAction = new HbAction(
       
   271 			hbTrId("txt_common_button_cancel"), mTimePickerDialog);
       
   272 
       
   273 	mTimePickerDialog->addAction(mOkAction);
       
   274 	mTimePickerDialog->addAction(mCancelAction);
       
   275 
       
   276 	mTimePickerDialog->open(this, SLOT(selectedAction(HbAction*)));
       
   277 }
       
   278 
       
   279 /*!
       
   280     Sets the Auto Time update.
       
   281  */
       
   282 void FtuDateTimeView::setAutoTimeupDate()
       
   283 {
       
   284 	if (mTimeAutoUpdate) {
       
   285 		mAutoTimeUpdateItem->setContentWidgetData("text", "OFF");
       
   286 		// Set the fields enabled if auto time update is OFF
       
   287 		mDateItem->setEnabled(true);
       
   288 		mTimeItem->setEnabled(true);
       
   289 		mCountryItem->setEnabled(true);
       
   290 		mCityItem->setEnabled(true);
       
   291 		// SetAutomaticTimeUpdate OFF, UnLoad the Plugins
       
   292 		setAutomaticTimeUpdateOff(false);
       
   293 	} else {
       
   294 		mAutoTimeUpdateItem->setContentWidgetData("text", "ON");
       
   295 		// Set the fields disabled if auto time update is ON
       
   296 		mDateItem->setEnabled(false);
       
   297 		mTimeItem->setEnabled(false);
       
   298 		mCountryItem->setEnabled(false);
       
   299 		mCityItem->setEnabled(false);
       
   300 		// SetAutomaticTimeUpdate ON, Load the Plugins
       
   301 		setAutomaticTimeUpdateOff(true);
       
   302 	}
       
   303 	wizardEditedDate(QDate::currentDate());
       
   304 }
       
   305 
       
   306 /*!
       
   307     Populates the City Selection List.
       
   308  */
       
   309 void FtuDateTimeView::populateCitySelectionList()
       
   310 {
       
   311 	if(mCitySelectionList) {
       
   312 		mCitySelectionList = NULL;
       
   313 	}
       
   314 	mCitySelectionList = new ClockCitySelectionList(mTimeZoneClient, this);
       
   315 	connect(mCitySelectionList,SIGNAL(citySelected(LocationInfo)),
       
   316 	        SLOT(HandleLocationChange(LocationInfo)));
       
   317 	mCitySelectionList->showCityList();
       
   318 }
       
   319 
       
   320 /*!
       
   321 	Slot to handle the case when a city has been selected from the city.
       
   322 	\param info of type LocationInfo which contains the city selected.
       
   323  */
       
   324 void FtuDateTimeView::HandleLocationChange(LocationInfo location)
       
   325 {
       
   326 	// Check if the location is valid. If its not valid the timezoneId will be -1
       
   327 	if(location.timezoneId != -1) {
       
   328 		// Set the location
       
   329 		mTimeZoneClient->setAsCurrentLocationL(location);
       
   330 		mCountryItem->setContentWidgetData("text", location.countryName);
       
   331 		mCityItem->setContentWidgetData("text", location.cityName);
       
   332 	}
       
   333 	// Cleanup
       
   334 	mCitySelectionList->deleteLater();
       
   335 }
       
   336 
       
   337 /*!
       
   338 	Slot to handle the selected action.
       
   339  */
       
   340 void FtuDateTimeView::selectedAction(HbAction *action)
       
   341 {
       
   342 	// Update time/date based on the picker selected.
       
   343 	if (action == mOkAction) {
       
   344 		if (mTimePickerDialog) {
       
   345 			updateTime();
       
   346 		} else if(mDatePickerDialog) {
       
   347 			updateDate();
       
   348 		}
       
   349 	}else {
       
   350 		if(mTimePickerDialog) {
       
   351 			mTimePickerDialog->deleteLater();
       
   352 		} else if(mDatePickerDialog) {
       
   353 			mDatePickerDialog->deleteLater();
       
   354 		}
       
   355 	}
       
   356 }
       
   357 
       
   358 /*!
       
   359     Sets the device date.
       
   360  */
       
   361 void FtuDateTimeView::updateDate()
       
   362 {
       
   363 	QDate date = mDatePicker->date();
       
   364 	// Set device Date
       
   365 	if (date.isValid()) {
       
   366 		mDateItem->setContentWidgetData("text",
       
   367 						date.toString(mSettingsUtility->dateFormatString()));
       
   368 		mTimeZoneClient->setDateTime(QDateTime(date, QTime::currentTime()));
       
   369 		wizardEditedDate(date);
       
   370 	}
       
   371 }
       
   372 
       
   373 /*!
       
   374     Sets the device time.
       
   375  */
       
   376 void FtuDateTimeView::updateTime()
       
   377 {
       
   378 	QTime time = mTimePicker->time();
       
   379 	// Set device Time
       
   380 	if (time.isValid()) {
       
   381 		mTimeItem->setContentWidgetData("text",
       
   382 						time.toString(mSettingsUtility->timeFormatString()));
       
   383 		mTimeZoneClient->setDateTime(QDateTime(QDate::currentDate(), time));
       
   384 		wizardEditedDate(QDate::currentDate());
       
   385 	}
       
   386 
       
   387 }
       
   388 
       
   389 /*!
       
   390     Gets the wizard completed status.
       
   391  */
       
   392 QDate FtuDateTimeView::getWizardCompletedDate()
       
   393 {
       
   394 	XQSettingsManager *settingsManager = new XQSettingsManager();
       
   395 	XQSettingsKey *ftuPluginDateCenrepKey =
       
   396 			new XQSettingsKey(XQSettingsKey::TargetCentralRepository,
       
   397 			                  KCRUidClockApp, KFtuPluginDate);
       
   398 	// Read the initial values from the cenrep
       
   399 	QString dateString = settingsManager->readItemValue(*ftuPluginDateCenrepKey,
       
   400 	                                                    XQSettingsManager::TypeString).toString();
       
   401 	QDate completedDate = QDate::fromString(dateString,
       
   402 	                                        mSettingsUtility->dateFormatString());
       
   403 
       
   404 	// Cleanup.
       
   405 	delete ftuPluginDateCenrepKey;
       
   406 	delete settingsManager;
       
   407 
       
   408 	return completedDate;
       
   409 }
       
   410 
       
   411 /*!
       
   412     Gets the wizard Edited date.
       
   413  */
       
   414 void FtuDateTimeView::wizardEditedDate(const QDate &date)
       
   415 {
       
   416 	XQSettingsManager *settingsManager = new XQSettingsManager();
       
   417 	XQSettingsKey *ftuPluginDateCenrepKey =
       
   418 			new XQSettingsKey(XQSettingsKey::TargetCentralRepository,
       
   419 			                  KCRUidClockApp, KFtuPluginDate);
       
   420 	QString dateString = date.toString(mSettingsUtility->dateFormatString());
       
   421 	settingsManager->writeItemValue(*ftuPluginDateCenrepKey,dateString);
       
   422 
       
   423 	// Cleanup.
       
   424 	delete ftuPluginDateCenrepKey;
       
   425 	delete settingsManager;
       
   426 }
       
   427 
       
   428 /*!
       
   429     To set AutomaticTimeUpdate
       
   430  */
       
   431 void FtuDateTimeView::setAutomaticTimeUpdateOff(bool value)
       
   432 {
       
   433 	mTimeZoneClient->setTimeUpdateOn(value);
       
   434 	mTimeAutoUpdate = value;
       
   435 }
       
   436 // End of file  --Don't remove this.