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