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