securitysettings/cpeapuiplugins/cpeapuserpasswordui/src/cpeapuserpasswordui.cpp
branchRCL_3
changeset 46 c74b3d9f6b9e
parent 45 bad0cc58d154
child 55 9c2aa05919d9
equal deleted inserted replaced
45:bad0cc58d154 46:c74b3d9f6b9e
     1 /*
       
     2  * Copyright (c) 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  * Description: 
       
    15  *   Control Panel QT UI for username-password based EAP method configuration
       
    16  *
       
    17  */
       
    18 
       
    19 /*
       
    20  * %version: 27 %
       
    21  */
       
    22 
       
    23 // System includes
       
    24 #include <HbDataForm>
       
    25 #include <HbDataFormModel>
       
    26 #include <HbDataFormViewItem>
       
    27 #include <HbParameterLengthLimiter>
       
    28 #include <HbMessageBox> 
       
    29 #include <HbAction>
       
    30 #include <HbLineEdit>
       
    31 #include <cpsettingformitemdata.h>
       
    32 #include <cpitemdatahelper.h>
       
    33 #include <eapqtvalidator.h>
       
    34 #include <eapqtexpandedeaptype.h>
       
    35 
       
    36 // User includes
       
    37 #include "cpeapuserpasswordui.h"
       
    38 
       
    39 /*!
       
    40  * \class CpEapUserPasswordUi
       
    41  * \brief Implementes username-password based EAP method configuration ui. 
       
    42  */
       
    43 
       
    44 // External function prototypes
       
    45 
       
    46 // Local constants
       
    47 
       
    48 /*! 
       
    49  * If password has already been stored into the database
       
    50  * user is shown dummy password (fixed number of asterisks)
       
    51  */
       
    52 static const QVariant passwordExistString = "xxxx";
       
    53 
       
    54 // ======== LOCAL FUNCTIONS ========
       
    55 
       
    56 // ======== MEMBER FUNCTIONS ========
       
    57 
       
    58 /*!
       
    59  * Constructor.
       
    60  * 
       
    61  * @param bearer        Bearer type of the accessed configuration.
       
    62  * @param iapId         IAP ID of the accessed configuration.
       
    63  * @param plugin        Plugin.
       
    64  * @param outerHandle   Outer handle.
       
    65  */
       
    66 CpEapUserPasswordUi::CpEapUserPasswordUi(
       
    67     const EapQtConfigInterface::EapBearerType bearer,
       
    68     const int iapId,
       
    69     const EapQtPluginInfo &plugin, 
       
    70     const EapQtPluginHandle& outerHandle) :
       
    71         mConfigIf(NULL),
       
    72         mPluginInfo(plugin), 
       
    73         mOuterHandle(outerHandle), 
       
    74         mForm(NULL),
       
    75         mModel(NULL),
       
    76         mItemDataHelper(NULL),
       
    77         mGroupItem(NULL),
       
    78         mUsername(NULL), 
       
    79         mPasswordPrompt(NULL), 
       
    80         mPassword(NULL), 
       
    81         mValidatorUsername(NULL),
       
    82         mValidatorPassword(NULL), 
       
    83         mPasswordStored(false), 
       
    84         mPasswordChanged(false), 
       
    85         mPasswordEdit(NULL)
       
    86 {
       
    87     qDebug("CpEapUserPasswordUi::CpEapUserPasswordUi");
       
    88 
       
    89     // IAP must be valid in construction (check includes
       
    90     // EapQtConfigInterface::IapIdUndefined)
       
    91     if (iapId < 0) {
       
    92         QT_THROW(std::bad_alloc());
       
    93         // scoped pointer gets deleted automaticaly on exception
       
    94     }
       
    95 
       
    96     // Get EAP config interface
       
    97     mConfigIf.reset(new EapQtConfigInterface(bearer, iapId));
       
    98     
       
    99     createUi();
       
   100 }
       
   101 
       
   102 /*!
       
   103  * Destructor.
       
   104  */
       
   105 CpEapUserPasswordUi::~CpEapUserPasswordUi()
       
   106 {
       
   107     qDebug("CpEapUserPasswordUi::~CpEapUserPasswordUi");
       
   108 
       
   109     // mValidatorUsername, mValidatorPassword
       
   110     // mConfigIf: scoped pointer deleted automatically
       
   111 }
       
   112 
       
   113 /*!
       
   114  * Creates username-password UI and initilizes settings read via
       
   115  * EapQtConfigInterface
       
   116  */
       
   117 void CpEapUserPasswordUi::createUi()
       
   118 {
       
   119     qDebug("CpEapUserPasswordUi::createUi");
       
   120 
       
   121     // Read Configurations
       
   122     bool configurationRead = mConfigIf->readConfiguration(mOuterHandle, mPluginInfo.pluginHandle(),
       
   123         mEapConfig);
       
   124     if (!configurationRead) {
       
   125         qDebug("CpEapUserPasswordUi::initializeUserPasswordUi - read configuration failed.");
       
   126     }
       
   127 
       
   128     // Construct username-password UI
       
   129     mForm = new HbDataForm();
       
   130     this->setWidget(mForm);
       
   131     mModel = new HbDataFormModel(mForm);
       
   132 
       
   133     // Create settings group
       
   134     mGroupItem = new HbDataFormModelItem(HbDataFormModelItem::GroupItem,
       
   135         HbParameterLengthLimiter(
       
   136             hbTrId("txt_occ_subhead_eap_module_settings")).arg(
       
   137                 mPluginInfo.localizationId()));
       
   138     mModel->appendDataFormItem(mGroupItem);
       
   139 
       
   140     // The parameter given as 0 is a HbDataForm pointer, not parent
       
   141     mItemDataHelper = new CpItemDataHelper(0);
       
   142     mItemDataHelper->setParent(this);
       
   143 
       
   144     // Create UI components
       
   145     createUsername();
       
   146     createPassword();
       
   147     
       
   148     mItemDataHelper->bindToForm(mForm);
       
   149     mForm->setModel(mModel);
       
   150     
       
   151     // Connect signal to add validators when items get activated (visualization created).
       
   152     bool connected = connect(mForm, SIGNAL( itemShown(const QModelIndex&) ), this,
       
   153         SLOT( setValidator(const QModelIndex) ));
       
   154     Q_ASSERT(connected);
       
   155     
       
   156     // Expand username-password settings group
       
   157     mForm->setExpanded(mModel->indexFromItem(mGroupItem), true);
       
   158 }
       
   159 
       
   160 /*!
       
   161  * Creates Username lineEdit
       
   162  */
       
   163 void CpEapUserPasswordUi::createUsername()
       
   164 {
       
   165     qDebug("CpEapUserPasswordUi::createUsername()");
       
   166     // Username
       
   167     mUsername = new CpSettingFormItemData(HbDataFormModelItem::TextItem, hbTrId(
       
   168         "txt_occ_setlabel_user_name"));
       
   169     // Initialize the value from EapQtConfig
       
   170     mUsername->setContentWidgetData("text", mEapConfig.value(EapQtConfig::Username));
       
   171     mGroupItem->appendChild(mUsername);
       
   172 }
       
   173 
       
   174 /*!
       
   175  * Creates Password group:
       
   176  * Prompt checkBox and password lineEdit
       
   177  */
       
   178 void CpEapUserPasswordUi::createPassword()
       
   179 {
       
   180     qDebug("CpEapUserPasswordUi::createPassword()");
       
   181     // Password prompting
       
   182     mPasswordPrompt = new CpSettingFormItemData(HbDataFormModelItem::CheckBoxItem, hbTrId(
       
   183         "txt_occ_setlabel_password"));
       
   184     mPasswordPrompt->setContentWidgetData("text", hbTrId("txt_occ_setlabel_password_val_prompt"));
       
   185     mPasswordPrompt->setContentWidgetData("checkState", boolToCheckState(mEapConfig.value(
       
   186         EapQtConfig::PasswordPrompt).toBool()));
       
   187     // Connect signal to disable/enable password when passwordPrompt changed 
       
   188     mForm->addConnection(mPasswordPrompt, SIGNAL(stateChanged(int)), this,
       
   189         SLOT(passwordPromptChanged(int)));
       
   190     mGroupItem->appendChild(mPasswordPrompt);
       
   191 
       
   192     // Password
       
   193     mPassword = new CpSettingFormItemData(HbDataFormModelItem::TextItem, hbTrId(
       
   194         "txt_occ_setlabel_password"));
       
   195     mPasswordStored = mEapConfig.value(EapQtConfig::PasswordStored).toBool();
       
   196     // If password has already been stored into the databse
       
   197     // fixed number of asterisks are shown in UI
       
   198     if (mPasswordStored) {
       
   199         mPassword->setContentWidgetData("text", passwordExistString);
       
   200     }
       
   201     // Set password echo mode
       
   202     mPassword->setContentWidgetData("echoMode", HbLineEdit::Password);
       
   203     // Dim password if passwordPrompt is selected
       
   204     passwordPromptChanged(mPasswordPrompt->contentWidgetData("checkState") == Qt::Checked);
       
   205     // Connect signal to get info that user has changed the password
       
   206     mForm->addConnection(mPassword, SIGNAL(editingFinished()), this, SLOT(passwordChanged()));
       
   207     mGroupItem->appendChild(mPassword);
       
   208 }
       
   209 
       
   210 /*!
       
   211  * Adds validators and connects focusIn event to the Password.
       
   212  * 
       
   213  * @param modelIndex Model index
       
   214  */
       
   215 void CpEapUserPasswordUi::setValidator(const QModelIndex modelIndex)
       
   216 {
       
   217     qDebug("CpEapUserPasswordUi::itemActivated");
       
   218 
       
   219     HbDataFormViewItem *viewItem = qobject_cast<HbDataFormViewItem *>
       
   220         (mForm->itemByIndex(modelIndex));
       
   221     HbDataFormModelItem *modelItem = mModel->itemFromIndex(modelIndex);
       
   222     
       
   223     if (modelItem == mUsername) {
       
   224         // When username lineEdit is activated (shown) first time, validator is added
       
   225         mValidatorUsername.reset(mConfigIf->validatorEap(mPluginInfo.pluginHandle().type(),
       
   226             EapQtConfig::Username));
       
   227         HbLineEdit *usernameEdit = qobject_cast<HbLineEdit *> (viewItem->dataItemContentWidget());
       
   228         mValidatorUsername->updateEditor(usernameEdit);
       
   229     }
       
   230     else if (modelItem == mPassword) {
       
   231         // When password lineEdit is activated (shown) first time, validator is added
       
   232         mValidatorPassword.reset(mConfigIf->validatorEap(mPluginInfo.pluginHandle().type(),
       
   233             EapQtConfig::Password));
       
   234         mPasswordEdit = qobject_cast<HbLineEdit *> (viewItem->dataItemContentWidget());
       
   235         mValidatorPassword->updateEditor(mPasswordEdit);
       
   236         // Install event filter to clear dummy password, when password is started to edit.
       
   237         mPasswordEdit->installEventFilter(this);
       
   238     }
       
   239 }
       
   240 
       
   241 /*!
       
   242  * This is called when user is about to exit the view.
       
   243  * Validates configuration and saves settings.
       
   244  * If configuration is not valid prompts question dialog.
       
   245  * If user chooses "OK" leaves without saving.
       
   246  * 
       
   247  */
       
   248 void CpEapUserPasswordUi::close()
       
   249 {
       
   250     qDebug("CpEapUserPasswordUi::close");
       
   251     
       
   252     // Validate configuration
       
   253     if (validate()) {
       
   254         qDebug("CpEapUserPasswordUi::close - Validation OK");
       
   255         
       
   256         // Store settings
       
   257         if (storeSettings()){
       
   258             qDebug("CpEapUserPasswordUi::close - Settings stored, close view");
       
   259             // Close view
       
   260             CpBaseSettingView::close();   
       
   261         }
       
   262         else {
       
   263             qDebug("CpEapUserPasswordUi::close - Store settings failed, prompt warning");
       
   264             // Store failed. Show error note to user
       
   265             QScopedPointer<HbMessageBox> infoBox;
       
   266             infoBox.reset(new HbMessageBox(
       
   267                 HbMessageBox::MessageTypeWarning));
       
   268             infoBox->setAttribute(Qt::WA_DeleteOnClose);
       
   269             infoBox->setText(hbTrId("txt_occ_info_unable_to_save_setting"));
       
   270             infoBox->clearActions();
       
   271             // Connect 'OK'-button to CpBaseSettingView 'aboutToClose'-signal
       
   272             HbAction *okAction = new HbAction(hbTrId("txt_common_button_ok"));
       
   273             infoBox->addAction(okAction);
       
   274             bool connected = connect(
       
   275                 okAction,
       
   276                 SIGNAL(triggered()),
       
   277                 this,
       
   278                 SIGNAL(aboutToClose()));
       
   279             Q_ASSERT(connected);
       
   280             infoBox->open();
       
   281             infoBox.take();
       
   282         }
       
   283     }
       
   284     else {
       
   285         qDebug("CpEapUserPasswordUi::close - validation failed. Prompt question.");
       
   286 
       
   287         // Validate failed. Request user to exit anyway
       
   288         QScopedPointer<HbMessageBox> messageBox;
       
   289         messageBox.reset(new HbMessageBox(
       
   290             HbMessageBox::MessageTypeQuestion));
       
   291         messageBox->setAttribute(Qt::WA_DeleteOnClose);
       
   292         messageBox->setText(hbTrId("txt_occ_info_incomplete_details_return_without_sa"));
       
   293         messageBox->clearActions();
       
   294         // Connect 'YES'-button to CpBaseSettingView 'aboutToClose'-signal
       
   295         HbAction *okAction = new HbAction(hbTrId("txt_common_button_yes"));
       
   296         messageBox->addAction(okAction);
       
   297         bool connected = connect(
       
   298             okAction,
       
   299             SIGNAL(triggered()),
       
   300             this,
       
   301             SIGNAL(aboutToClose()));
       
   302         Q_ASSERT(connected);
       
   303         // Clicking 'NO'-button does nothing
       
   304         HbAction *cancelAction = new HbAction(hbTrId("txt_common_button_no"));
       
   305         messageBox->addAction(cancelAction);
       
   306         messageBox->setTimeout(HbPopup::NoTimeout);
       
   307         messageBox->open();
       
   308         messageBox.take();
       
   309     }
       
   310 }
       
   311 
       
   312 /*!
       
   313  * Converts check box state to boolean.
       
   314  * 
       
   315  * @param state Check box state
       
   316  * 
       
   317  * @return true if Check box is checked, false otherwise.
       
   318  */
       
   319 bool CpEapUserPasswordUi::checkStateToBool(const int state)
       
   320 {
       
   321     return (Qt::Unchecked == state ? false : true);
       
   322 }
       
   323 
       
   324 /*!
       
   325  * Converts boolean to check box state.
       
   326  * 
       
   327  * @param state Tells is check box checked.
       
   328  * 
       
   329  * @return Qt check state
       
   330  */
       
   331 int CpEapUserPasswordUi::boolToCheckState(const bool state)
       
   332 {
       
   333     return (false == state ? Qt::Unchecked : Qt::Checked);
       
   334 }
       
   335 
       
   336 /*!
       
   337  * Dims the password if passwordPrompt has been selected.
       
   338  * 
       
   339  * @param state Tells is password prompt checked.
       
   340  */
       
   341 void CpEapUserPasswordUi::passwordPromptChanged(int state)
       
   342 {
       
   343     qDebug("CpEapUserPasswordUi::passwordPromptChanged - state: %d", state);
       
   344 
       
   345     mPassword->setContentWidgetData("enabled", !checkStateToBool(state));
       
   346 }
       
   347 
       
   348 /*!
       
   349  * Saves information that password has been changed and must be stored.
       
   350  * If the new password is not given, the stored password is kept (if exists).
       
   351  */
       
   352 void CpEapUserPasswordUi::passwordChanged()
       
   353 {
       
   354     if (mPassword->contentWidgetData("text").toString().length() > 0) {
       
   355         // Stored password is considered as changed only if user
       
   356         // gives new unempty password
       
   357         qDebug("CpEapUserPasswordUi::passwordChanged");
       
   358         mPasswordChanged = true;
       
   359     }
       
   360 }
       
   361 
       
   362 /*!
       
   363  * When a focus moves into the password editor, possible dummy password is cleared.
       
   364  */
       
   365 bool CpEapUserPasswordUi::eventFilter(QObject *obj, QEvent *event)
       
   366 {
       
   367     
       
   368     if (obj == mPasswordEdit && event->type() == QEvent::FocusIn 
       
   369         && mPasswordStored && !mPasswordChanged) {
       
   370         qDebug("CpEapUserPasswordUi::eventFilter - mPasswordEdit and FocusIn");
       
   371         mPassword->setContentWidgetData("text", "");
       
   372     }
       
   373     return false;
       
   374 }
       
   375 
       
   376 /*!
       
   377  * Validates settings configuration.
       
   378  * 
       
   379  * @return true if configuration OK, false otherwise.
       
   380  */
       
   381 bool CpEapUserPasswordUi::validate()
       
   382 {
       
   383     qDebug("CpEapUserPasswordUi::validate");
       
   384     bool valid = false;
       
   385 
       
   386     if (validateUsername()
       
   387         && validatePasswordGroup()) {
       
   388         qDebug("CpEapUserPasswordUi::validate - OK");
       
   389         valid = true;
       
   390     }
       
   391 
       
   392     return valid;
       
   393 }
       
   394 
       
   395 /*!
       
   396  * Validates username
       
   397  * 
       
   398  * @return true if OK, false otherwise
       
   399  */
       
   400 bool CpEapUserPasswordUi::validateUsername()
       
   401 {
       
   402     qDebug("CpEapUserPasswordUi::validateUsername");
       
   403     bool status = false;
       
   404     // true if (given username is valid) or 
       
   405     // (password prompt is on and username is empty)
       
   406     if ((EapQtValidator::StatusOk == mValidatorUsername->validate(
       
   407             mUsername->contentWidgetData("text"))) 
       
   408         || (mPasswordPrompt->contentWidgetData("checkState") == Qt::Checked
       
   409             && mUsername->contentWidgetData("text").toString().length() == 0)) {
       
   410         qDebug("CpEapUserPasswordUi::validateUsername - OK");
       
   411         status = true;
       
   412     }
       
   413     return status;       
       
   414 }
       
   415 
       
   416 /*!
       
   417  * Validates password and passwordPrompt.
       
   418  * 
       
   419  * @return true if OK, false otherwise.
       
   420  */
       
   421 bool CpEapUserPasswordUi::validatePasswordGroup()
       
   422 {
       
   423     qDebug("CpEapUserPasswordUi::validatePasswordGroup");
       
   424     bool status = false;
       
   425     // true if password prompt is checked, stored password has not changed
       
   426     // or changed/given passrword is valid
       
   427     if (mPasswordPrompt->contentWidgetData("checkState") == Qt::Checked 
       
   428         || (mPasswordStored && !mPasswordChanged) 
       
   429         || (mPasswordStored && mPasswordChanged && EapQtValidator::StatusOk
       
   430             == mValidatorPassword->validate(mPassword->contentWidgetData("text").toString()))
       
   431         || (!mPasswordStored && EapQtValidator::StatusOk
       
   432             == mValidatorPassword->validate(mPassword->contentWidgetData("text").toString()))) {
       
   433         qDebug("CpEapUserPasswordUi::validatePasswordGroup - OK");
       
   434         status = true;
       
   435     }
       
   436     return status;
       
   437 }
       
   438 
       
   439 /*!
       
   440  * Stores settings given via username-password configuration UI
       
   441  * 
       
   442  * @return false if saving failed, true otherwisev
       
   443  */
       
   444 bool CpEapUserPasswordUi::storeSettings()
       
   445 {
       
   446     qDebug("CpEapUserPasswordUi::storeSettings");
       
   447 
       
   448     EapQtConfig eapConfig;
       
   449 
       
   450     eapConfig.setValue(EapQtConfig::OuterType, qVariantFromValue(mOuterHandle));
       
   451     eapConfig.setValue(EapQtConfig::Username, mUsername->contentWidgetData("text"));
       
   452     eapConfig.setValue(EapQtConfig::PasswordPrompt, checkStateToBool(
       
   453         mPasswordPrompt->contentWidgetData("checkState").toInt()));
       
   454     if (mPasswordPrompt->contentWidgetData("checkState") == Qt::Checked) {
       
   455         if(mPasswordStored) {
       
   456             // Stored password is cleared if prompting has been enabled
       
   457             eapConfig.setValue(EapQtConfig::PasswordClear, true);
       
   458             eapConfig.setValue(EapQtConfig::Password, "");
       
   459         }
       
   460         // else do nothing (password is not saved)
       
   461     }
       
   462     else {
       
   463         // Password prompt is disabled
       
   464         if (!mPasswordChanged && mPasswordStored) {
       
   465             // Stored password has not been changed
       
   466             eapConfig.setValue(EapQtConfig::PasswordStored, true);
       
   467         }
       
   468         else if (mPasswordChanged || !mPasswordStored) {
       
   469             // Store the new password
       
   470             eapConfig.setValue(EapQtConfig::Password, mPassword->contentWidgetData("text").toString());
       
   471         }
       
   472     }
       
   473     
       
   474     // Save configuration
       
   475     if (!mConfigIf->saveConfiguration(mPluginInfo.pluginHandle(), eapConfig)) {
       
   476         qDebug("CpEapUserPasswordUi::storeSettings - configuration saving failed.");
       
   477         return false;
       
   478     }
       
   479     return true;
       
   480 }