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