securitysettings/cpeapuiplugins/cpeapsimakaui/src/cpeapsimakaui.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 EAP-SIM and EAP-AKA method configuration
       
    16  *
       
    17  */
       
    18 
       
    19 /*
       
    20  * %version:  15 %
       
    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 "cpeapsimakaui.h"
       
    37 
       
    38 /*!
       
    39  * \class CpEapSimAkaUi
       
    40  * \brief Implementes EAP-SIM and EAP-AKA method configuration ui. 
       
    41  */
       
    42 
       
    43 // External function prototypes
       
    44 
       
    45 // Local constants
       
    46 
       
    47 // ======== LOCAL FUNCTIONS ========
       
    48 
       
    49 // ======== MEMBER FUNCTIONS ========
       
    50 
       
    51 /*!
       
    52  * Constructor.
       
    53  * 
       
    54  * @param bearer        Bearer type of the accessed configuration.
       
    55  * @param iapId         IAP ID of the accessed configuration.
       
    56  * @param plugin        Plugin.
       
    57  * @param outerHandle   Outer handle.
       
    58  */
       
    59 CpEapSimAkaUi::CpEapSimAkaUi(
       
    60     const EapQtConfigInterface::EapBearerType bearer,
       
    61     const int iapId,
       
    62     const EapQtPluginInfo &plugin,
       
    63     const EapQtPluginHandle& outerHandle) :
       
    64         mConfigIf(new EapQtConfigInterface(bearer, iapId)),
       
    65         mPluginInfo(plugin),
       
    66         mOuterHandle(outerHandle),
       
    67         mForm(NULL),
       
    68         mModel(NULL),
       
    69         mUsernameAutomatic(NULL),
       
    70         mUsername(NULL),
       
    71         mRealmAutomatic(NULL),
       
    72         mRealm(NULL),
       
    73         mValidatorRealm(NULL),
       
    74         mValidatorUsername(NULL)
       
    75 {
       
    76     qDebug("CpEapSimAkaUi::CpEapSimAkaUi");
       
    77 
       
    78     // IAP must be valid in construction (check includes
       
    79     // EapQtConfigInterface::IapIdUndefined)
       
    80     if (iapId < 0) {
       
    81         QT_THROW(std::bad_alloc());
       
    82         // scoped pointer gets deleted automaticaly on exception
       
    83     }
       
    84 
       
    85     initializeSimAkaUi();
       
    86 }
       
    87 
       
    88 /*!
       
    89  * Destructor.
       
    90  */
       
    91 CpEapSimAkaUi::~CpEapSimAkaUi()
       
    92 {
       
    93     qDebug("CpEapSimAkaUi::~CpEapSimAkaUi");
       
    94 
       
    95     delete mValidatorRealm;
       
    96     delete mValidatorUsername;
       
    97 
       
    98     // mConfigIf: scoped pointer deleted automatically
       
    99 }
       
   100 
       
   101 /*!
       
   102  * Initializes the sim-aka ui and initializes objects based on configuration
       
   103  * read from the database.
       
   104  */
       
   105 void CpEapSimAkaUi::initializeSimAkaUi()
       
   106 {
       
   107     qDebug("CpEapSimAkaUi::initializeSimAkaUi");
       
   108 
       
   109     CpItemDataHelper itemDataHelper;
       
   110 
       
   111     // Read Configurations
       
   112     EapQtConfig eapConfig;
       
   113     bool configurationRead = mConfigIf->readConfiguration(mOuterHandle, mPluginInfo.pluginHandle(),
       
   114         eapConfig);
       
   115     if (!configurationRead) {
       
   116         qDebug("CpEapSimAkaUi::initializeSimAkaUi - read configuration failed.");
       
   117     }
       
   118     
       
   119     // Create UI objects and group 
       
   120     mForm = new HbDataForm();
       
   121     this->setWidget(mForm);
       
   122     
       
   123     mModel = new HbDataFormModel(mForm);
       
   124     HbDataFormModelItem *groupItem = new HbDataFormModelItem(HbDataFormModelItem::GroupItem,
       
   125         hbTrId("txt_occ_subhead_eap_module_settings").arg(mPluginInfo.localizationId()));
       
   126     mModel->appendDataFormItem(groupItem);
       
   127 
       
   128     // Connect signal to add validators when items get activated (visualization created).
       
   129     bool connected = connect(mForm, SIGNAL( itemShown(const QModelIndex&) ), this,
       
   130         SLOT( setValidator(const QModelIndex) ));
       
   131     Q_ASSERT(connected);
       
   132 
       
   133     // UsernameAutomatic
       
   134     mUsernameAutomatic = new CpSettingFormItemData(HbDataFormModelItem::CheckBoxItem, hbTrId(
       
   135         "txt_occ_setlabel_user_name"));
       
   136     mUsernameAutomatic->setContentWidgetData("text", hbTrId(
       
   137         "txt_occ_setlabel_user_name_val_generate_automatica"));
       
   138     // Initialize the value from EapQtConfig
       
   139     // Generate username automatically is selected by default
       
   140     mUsernameAutomatic->setContentWidgetData("checkState", boolToCheckState(eapConfig.value(
       
   141         EapQtConfig::UsernameAutomatic).toBool()));
       
   142     // Connect signal to disable/enable username when usernameAutomatic changed   
       
   143     mForm->addConnection(mUsernameAutomatic, SIGNAL(stateChanged(int)), this,
       
   144         SLOT(usernameAutomaticChanged(int)));
       
   145     groupItem->appendChild(mUsernameAutomatic);
       
   146 
       
   147     //Username
       
   148     mUsername = new CpSettingFormItemData(HbDataFormModelItem::TextItem, hbTrId(
       
   149         "txt_occ_setlabel_user_name"));
       
   150     mUsername->setContentWidgetData("text", eapConfig.value(EapQtConfig::Username));
       
   151     // Dimmed username if usernameAutomatic selected
       
   152     usernameAutomaticChanged(mUsernameAutomatic->contentWidgetData("checkState") == Qt::Checked);
       
   153     groupItem->appendChild(mUsername);
       
   154 
       
   155     // RealmAutomatic
       
   156     mRealmAutomatic = new CpSettingFormItemData(HbDataFormModelItem::CheckBoxItem, hbTrId(
       
   157         "txt_occ_setlabel_realm"));
       
   158     mRealmAutomatic->setContentWidgetData("text", hbTrId(
       
   159         "txt_occ_setlabel_realm_val_generate_automatically"));
       
   160     // Initialize the value from EapQtConfig
       
   161     // Generate realm automatically is selected by default
       
   162     mRealmAutomatic->setContentWidgetData("checkState", boolToCheckState(eapConfig.value(
       
   163         EapQtConfig::RealmAutomatic).toBool()));
       
   164     // connect signal to disable/enable realm when realmAutomatic changed 
       
   165     mForm->addConnection(mRealmAutomatic, SIGNAL(stateChanged(int)), this,
       
   166         SLOT(realmAutomaticChanged(int)));
       
   167     groupItem->appendChild(mRealmAutomatic);
       
   168 
       
   169     //Realm
       
   170     mRealm = new CpSettingFormItemData(HbDataFormModelItem::TextItem, hbTrId(
       
   171         "txt_occ_setlabel_realm"));
       
   172     mRealm->setContentWidgetData("text", eapConfig.value(EapQtConfig::Realm));
       
   173     // Dimmed realm if realmAutomatic selected
       
   174     realmAutomaticChanged(mRealmAutomatic->contentWidgetData("checkState") == Qt::Checked);
       
   175     groupItem->appendChild(mRealm);
       
   176 
       
   177     // Set the model
       
   178     itemDataHelper.bindToForm(mForm);
       
   179     mForm->setModel(mModel);
       
   180 
       
   181     // Expand simakaui settings group
       
   182     mForm->setExpanded(mModel->indexFromItem(groupItem), TRUE);
       
   183 }
       
   184 
       
   185 /*!
       
   186  * Adds validators.
       
   187  * 
       
   188  * @param modelIndex Model index
       
   189  */
       
   190 void CpEapSimAkaUi::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 *edit = qobject_cast<HbLineEdit *> (viewItem->dataItemContentWidget());
       
   205         mValidatorUsername->updateEditor(edit);
       
   206     }
       
   207     else if (modelItem == mRealm) {
       
   208         // When realm lineEdit is activated (shown) first time, validator is added
       
   209         if (NULL == mValidatorRealm) {
       
   210             mValidatorRealm = mConfigIf->validatorEap(mPluginInfo.pluginHandle().type(),
       
   211                 EapQtConfig::Realm);           
       
   212         }
       
   213         HbLineEdit *edit = qobject_cast<HbLineEdit *> (viewItem->dataItemContentWidget());
       
   214         mValidatorRealm->updateEditor(edit);
       
   215     }
       
   216 }
       
   217 
       
   218 /*!
       
   219  * This is called when user is about to exit the view.
       
   220  * Validates configuration and saves settings.
       
   221  * If configuration is not valid prompts question dialog.
       
   222  * If user chooses "OK" leaves without saving.
       
   223  * 
       
   224  */
       
   225 void CpEapSimAkaUi::close()
       
   226 {
       
   227     qDebug("CpEapSimAkaUi::close");
       
   228     if (validate()) {
       
   229         qDebug("CpEapSimAkaUi::close - Store settings and exit");
       
   230         storeSettings();
       
   231         CpBaseSettingView::close();
       
   232     }
       
   233     else {
       
   234         qDebug("CpEapSimAkaUi::close - validation failed. Prompt question.");
       
   235         HbMessageBox *note = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
       
   236         note->setAttribute(Qt::WA_DeleteOnClose);
       
   237         note->setText(hbTrId("txt_occ_info_incomplete_details_return_without_sa"));
       
   238         note->clearActions();
       
   239         // Connect 'YES'-button to CpBaseSettingView 'aboutToClose'-signal
       
   240         HbAction *okAction = new HbAction(hbTrId("txt_common_button_yes"));
       
   241         note->addAction(okAction);
       
   242         bool connected = connect(
       
   243             okAction,
       
   244             SIGNAL(triggered()),
       
   245             this,
       
   246             SIGNAL(aboutToClose()));
       
   247         Q_ASSERT(connected);
       
   248         // Clicking 'NO'-button does nothing
       
   249         HbAction *cancelAction = new HbAction(hbTrId("txt_common_button_no"));
       
   250         note->addAction(cancelAction);
       
   251         note->setTimeout(HbPopup::NoTimeout);
       
   252         note->open();
       
   253     }
       
   254 }
       
   255 
       
   256 /*!
       
   257  * Dims the realm if generate realm automatically has been selected.
       
   258  * 
       
   259  * @param state Tells is generate automatically checked.
       
   260  */
       
   261 void CpEapSimAkaUi::realmAutomaticChanged(int state)
       
   262 {
       
   263     qDebug("CpEapSimAkaUi::realmAutomaticChanged");
       
   264 
       
   265     mRealm->setContentWidgetData("enabled", !checkStateToBool(state));
       
   266 }
       
   267 
       
   268 /*!
       
   269  * Dims the username if generate username automatically has been selected.
       
   270  * 
       
   271  * @param state Tells is generate automatically checked.
       
   272  */
       
   273 void CpEapSimAkaUi::usernameAutomaticChanged(int state)
       
   274 {
       
   275     qDebug("CpEapSimAkaUi::usernameAutomaticChanged");
       
   276 
       
   277     mUsername->setContentWidgetData("enabled", !checkStateToBool(state));
       
   278 }
       
   279 
       
   280 /*!
       
   281  * Converts check box state to boolean.
       
   282  * 
       
   283  * @param state Check box state
       
   284  * 
       
   285  * @return true if Check box is checked, false otherwise.
       
   286  */
       
   287 bool CpEapSimAkaUi::checkStateToBool(const int state)
       
   288 {
       
   289     return (Qt::Unchecked == state ? false : true);
       
   290 }
       
   291 
       
   292 /*!
       
   293  * Converts boolean to check box state.
       
   294  * 
       
   295  * @param state Tells is check box checked.
       
   296  * 
       
   297  * @return Qt check state
       
   298  */
       
   299 int CpEapSimAkaUi::boolToCheckState(const bool state)
       
   300 {
       
   301     return (false == state ? Qt::Unchecked : Qt::Checked);
       
   302 }
       
   303 
       
   304 /*!
       
   305  * Validates settings configuration.
       
   306  * 
       
   307  * @return true if configuration OK, false otherwise.
       
   308  */
       
   309 bool CpEapSimAkaUi::validate()
       
   310 {
       
   311     bool valid = false;
       
   312 
       
   313     if (validateGroup(mUsername, mUsernameAutomatic, mValidatorUsername) && validateGroup(mRealm,
       
   314         mRealmAutomatic, mValidatorRealm)) {
       
   315         valid = true;
       
   316     }
       
   317 
       
   318     return valid;
       
   319 }
       
   320 
       
   321 /*!
       
   322  * Validates checkBox and lineEdit group.
       
   323  * 
       
   324  * @return true if OK, false otherwise.
       
   325  */
       
   326 bool CpEapSimAkaUi::validateGroup(CpSettingFormItemData *edit, CpSettingFormItemData *checkBox,
       
   327     EapQtValidator *validator)
       
   328 {
       
   329     bool status = false;
       
   330     // true if generate automatically is checked or given value is valid
       
   331     if (checkBox->contentWidgetData("checkState") == Qt::Checked
       
   332         || EapQtValidator::StatusOk == validator->validate(
       
   333             edit->contentWidgetData("text"))) {
       
   334         status = true;
       
   335     }
       
   336     return status;
       
   337 }
       
   338 
       
   339 /*!
       
   340  * Stores settings given via SIM-AKA configuration UI
       
   341  */
       
   342 void CpEapSimAkaUi::storeSettings()
       
   343 {
       
   344     qDebug("CpEapSimAkaUi::storeSettings");
       
   345 
       
   346     EapQtConfig eapConfig;
       
   347 
       
   348     eapConfig.setValue(EapQtConfig::OuterType, qVariantFromValue(mOuterHandle));
       
   349     eapConfig.setValue(EapQtConfig::UsernameAutomatic, checkStateToBool(
       
   350         mUsernameAutomatic->contentWidgetData("checkState").toInt()));
       
   351     eapConfig.setValue(EapQtConfig::Username, mUsername->contentWidgetData("text"));
       
   352     eapConfig.setValue(EapQtConfig::RealmAutomatic, checkStateToBool(
       
   353         mRealmAutomatic->contentWidgetData("checkState").toInt()));
       
   354     eapConfig.setValue(EapQtConfig::Realm, mRealm->contentWidgetData("text"));
       
   355 
       
   356     if (!mConfigIf->saveConfiguration(mPluginInfo.pluginHandle(), eapConfig)) {
       
   357         qDebug("CpEapSimAkaUi::storeSettings - configuration saving failed.");
       
   358     }
       
   359 }