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