cmmanager/cpdestinationplugin/src/cpdestinationentryitem.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     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 *   This items represents one Destination in UI.
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbDataForm>
       
    20 #include <HbDataFormModel>
       
    21 #include <HbToolBar>
       
    22 #include <HbAction>
       
    23 #include <HbDataFormViewItem>
       
    24 #include <HbMenu>
       
    25 #include <HbInputDialog>
       
    26 #include <HbMessageBox>
       
    27 #include <HbPopup>
       
    28 #include <HbListWidget>
       
    29 #include <HbListWidgetItem>
       
    30 #include <HbMainWindow>
       
    31 #include <HbInstance>
       
    32 #include <HbParameterLengthLimiter>
       
    33 #include <cppluginutility.h>
       
    34 #include <cpitemdatahelper.h>
       
    35 #include <cpbasesettingview.h>
       
    36 #include <cmdestination_shim.h>
       
    37 #include <cmconnectionmethod_shim.h>
       
    38 #include <cmmanager_shim.h>
       
    39 
       
    40 // User includes
       
    41 #include "cpiapitem.h"
       
    42 #include "cpdestinationentryitem.h"
       
    43 #include "cpdestinationgroup.h"
       
    44 #include "OstTraceDefinitions.h"
       
    45 #ifdef OST_TRACE_COMPILER_IN_USE
       
    46 #include "cpdestinationentryitemTraces.h"
       
    47 #endif
       
    48 /*!
       
    49     \class  CpDestinationEntryItemData
       
    50     \brief  This class represents destination in data model. 
       
    51             It takes care of all destination related operations.
       
    52 */
       
    53 
       
    54 // External function prototypes
       
    55 
       
    56 // Local constants
       
    57 
       
    58 // ======== LOCAL FUNCTIONS ========
       
    59 
       
    60 // ======== MEMBER FUNCTIONS ========
       
    61 
       
    62 /*!         
       
    63     Constructor.
       
    64     
       
    65     @param[in] itemDataHelper Helper for connecting signals and slots.
       
    66  */
       
    67 CpDestinationEntryItemData::CpDestinationEntryItemData(CpItemDataHelper &itemDataHelper)
       
    68     : CpSettingFormEntryItemData(CpSettingFormEntryItemData::ButtonEntryItem, itemDataHelper),
       
    69     mList(0),
       
    70     mDialog(0),
       
    71     mOkAction(0)
       
    72 {
       
    73     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CPDESTINATIONENTRYITEMDATA_ENTRY);
       
    74     mAps = new QList<CpIapItem*>();
       
    75     try {
       
    76         mCmm = new CmManagerShim();
       
    77     } catch (const std::exception&) {
       
    78         OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_CPDESTINATIONENTRYITEMDATA, "CpDestinationEntryItemData::CpDestinationEntryItemData: Exception caught");
       
    79         mCmm = NULL;
       
    80     }
       
    81         
       
    82     // Fix connections
       
    83     itemDataHelper.removeConnection(this,SIGNAL(pressed()),this,SLOT(onLaunchView()));
       
    84     itemDataHelper.addConnection(this,SIGNAL(clicked()),this,SLOT(onLaunchView()));
       
    85     itemDataHelper.addConnection(this,SIGNAL(longPress(QPointF)),this,SLOT(showItemMenu(QPointF)));
       
    86     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CPDESTINATIONENTRYITEMDATA_EXIT);
       
    87 }
       
    88 
       
    89 /*!
       
    90     Destructor
       
    91  */
       
    92 CpDestinationEntryItemData::~CpDestinationEntryItemData()
       
    93 {
       
    94     OstTraceFunctionEntry0(DUP1_CPDESTINATIONENTRYITEMDATA_CPDESTINATIONENTRYITEMDATA_ENTRY);
       
    95     delete mCmm;
       
    96     delete mAps;
       
    97     OstTraceFunctionExit0(DUP1_CPDESTINATIONENTRYITEMDATA_CPDESTINATIONENTRYITEMDATA_EXIT);
       
    98 }
       
    99 
       
   100 /*!
       
   101     \return unique destination ID of this item.
       
   102  */
       
   103 int CpDestinationEntryItemData::destinationId()
       
   104 {
       
   105     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_DESTINATIONID_ENTRY);
       
   106     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_DESTINATIONID_EXIT);
       
   107     return mDestinationId;
       
   108 }
       
   109 
       
   110 /*!
       
   111     Sets destination ID to be given integer.
       
   112  */
       
   113 void CpDestinationEntryItemData::setDestinationId(int destId)
       
   114 {
       
   115     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SETDESTINATIONID_ENTRY);
       
   116     mDestinationId = destId;
       
   117     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SETDESTINATIONID_EXIT);
       
   118 }
       
   119 
       
   120 /*!
       
   121     \return Returns destination name as QString
       
   122  */
       
   123 QString CpDestinationEntryItemData::destinationName()
       
   124 {
       
   125     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_DESTINATIONNAME_ENTRY);
       
   126     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_DESTINATIONNAME_EXIT);
       
   127     return mDestinationName;
       
   128 }
       
   129 
       
   130 /*!
       
   131     Sets given QString to be destination name.
       
   132  */
       
   133 void CpDestinationEntryItemData::setDestinationName(const QString destinationName)
       
   134 {
       
   135     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SETDESTINATIONNAME_ENTRY);
       
   136     mDestinationName = destinationName;
       
   137     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SETDESTINATIONNAME_EXIT);
       
   138 }
       
   139 
       
   140 /*!
       
   141     Inherited member from CpSettingFormEntryItemData. Returns view that contains
       
   142     all access points included in this destination item.
       
   143  */
       
   144 CpBaseSettingView *CpDestinationEntryItemData::createSettingView() const
       
   145 {
       
   146     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CREATESETTINGVIEW_ENTRY);
       
   147     CpBaseSettingView* view = new CpBaseSettingView();
       
   148     constructSettingView(view);
       
   149     
       
   150     // Toolbar. Allow arrange if more than one AP is shown
       
   151     if (mDestinationId != 0 && mAps->count() > 1) {
       
   152         HbToolBar *tb = view->toolBar();
       
   153         HbIcon arrangeIcon("qtg_mono_sort");
       
   154         HbAction *arrangeAction 
       
   155             = tb->addAction(arrangeIcon, "", this, SLOT(activateArrangeMode()));
       
   156     }
       
   157     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CREATESETTINGVIEW_EXIT);
       
   158     return view;        
       
   159 }
       
   160 
       
   161 /*!
       
   162     Worker function for createSettingView(). Adds Access points to given view.
       
   163     
       
   164      @param[out] view Target view where settings are constructed.
       
   165  */
       
   166 void CpDestinationEntryItemData::constructSettingView(CpBaseSettingView *view) const
       
   167 {
       
   168     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CONSTRUCTSETTINGVIEW_ENTRY);
       
   169     mAps->clear();
       
   170     CpItemDataHelper *itemDataHelper = new CpItemDataHelper();
       
   171     HbDataForm *form = new HbDataForm();
       
   172     HbDataFormModel *model = new HbDataFormModel;
       
   173     form->setModel(model);
       
   174     model->setParent(form);
       
   175     view->setWidget(form);
       
   176     CpPluginUtility::addCpItemPrototype(form);
       
   177     QList<QSharedPointer<CmConnectionMethodShim> > apList;
       
   178     // Following won't throw exception
       
   179     fetchReferencedAps(apList, mCmm);
       
   180     form->setHeading(text());
       
   181 
       
   182     for (int i = 0; i < apList.count(); i++) {
       
   183         int apId = apList[i]->getIntAttribute(CMManagerShim::CmId);
       
   184         CpBearerApPluginInterface *bearerPlugin = 
       
   185                 static_cast<CpDestinationGroup*>(HbDataFormModelItem::parent())->findBearerPlugin(apId);
       
   186         bool apProtected = false;
       
   187         if (mDestinationId != 0) {
       
   188             CmDestinationShim *destination = mCmm->destination(mDestinationId);
       
   189             CMManagerShim::CmmProtectionLevel level = destination->protectionLevel();
       
   190             if (level == CMManagerShim::ProtLevel3) {
       
   191                 apProtected = apList[i]->getBoolAttribute(CMManagerShim::CmProtected);
       
   192             } else if (level == CMManagerShim::ProtLevel1) {
       
   193                 apProtected = true;
       
   194             }
       
   195             delete destination;
       
   196         }
       
   197         
       
   198         CpIapItem *iapDataItem;
       
   199         iapDataItem = new CpIapItem(
       
   200             *itemDataHelper, 
       
   201             apId, 
       
   202             apList[i]->getStringAttribute(CMManagerShim::CmName), 
       
   203             mDestinationId,
       
   204             apProtected,
       
   205             bearerPlugin);
       
   206        
       
   207         // Add name to UI item
       
   208         iapDataItem->setContentWidgetData(
       
   209             QString("text"), 
       
   210             apList[i]->getStringAttribute(CMManagerShim::CmName));
       
   211         
       
   212         // Add Icon if found
       
   213         HbIcon iapIcon(resolveApIcon(apList[i]));
       
   214         iapDataItem->setEntryItemIcon(iapIcon);
       
   215        
       
   216         // Add priority to item if not Uncategorised "Destination"
       
   217         if (mDestinationId  != 0) {
       
   218             // Access Points are listed in priority order.
       
   219             QString priority = hbTrId("txt_occ_dblist_val_priority_l1").arg(i + 1);
       
   220             iapDataItem->setContentWidgetData(QString("additionalText"), priority);
       
   221         } else {
       
   222             iapDataItem->setContentWidgetData(QString("stretched"), true);
       
   223         }
       
   224         
       
   225         // Set item disabled if it is protected
       
   226         if (apProtected) {
       
   227             iapDataItem->setContentWidgetData(QString("enabled"), false);
       
   228         }
       
   229         
       
   230         // Add to model
       
   231         model->appendDataFormItem(iapDataItem);
       
   232         
       
   233         // Save and make connections
       
   234         mAps->append(iapDataItem);
       
   235         bool connected 
       
   236             = connect(iapDataItem, SIGNAL(iapChanged()), this, SLOT(updateDestinationView()));
       
   237         Q_ASSERT(connected);
       
   238     }
       
   239     
       
   240     itemDataHelper->bindToForm(form);
       
   241     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CONSTRUCTSETTINGVIEW_EXIT);
       
   242 }
       
   243 
       
   244 /*!
       
   245     Rebuilds view with access points after access points have changed.
       
   246  */
       
   247 void CpDestinationEntryItemData::updateDestinationView()
       
   248 {
       
   249     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_UPDATEDESTINATIONVIEW_ENTRY);
       
   250     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();            
       
   251     HbMainWindow *mainWnd = mainWindows.front();
       
   252        
       
   253     if (mainWnd) {
       
   254         HbView *view = mainWnd->currentView();
       
   255         CpBaseSettingView  *cpView = static_cast<CpBaseSettingView  *>(view);
       
   256         constructSettingView(cpView);
       
   257         
       
   258         // Remove Toolbar if necessary
       
   259         if (mDestinationId != 0 && mAps->count() <= 1) {
       
   260             HbToolBar *tb = view->toolBar();
       
   261             tb->clearActions();
       
   262         }
       
   263     }
       
   264     OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_UPDATEDESTINATIONVIEW, "CpDestinationEntryItemData::updateDestinationView: Emit destination changed");
       
   265     emit destChanged();
       
   266     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_UPDATEDESTINATIONVIEW_EXIT);
       
   267 }
       
   268 
       
   269 /*!
       
   270     Prompts user for action after long tap. Choices for actions are "Rename" and "Delete".
       
   271     
       
   272     @param[in] position The item specific menu is shown in given position.
       
   273  */
       
   274 void CpDestinationEntryItemData::showItemMenu(QPointF position)
       
   275 {
       
   276     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SHOWITEMMENU_ENTRY);
       
   277     if (mDestinationId != 0) {
       
   278         CmDestinationShim *destination = NULL;
       
   279         CMManagerShim::CmmProtectionLevel protLvl = CMManagerShim::ProtLevel1;
       
   280         
       
   281         try {
       
   282             destination = mCmm->destination(mDestinationId);
       
   283             protLvl = destination->protectionLevel();
       
   284             delete destination;
       
   285         } catch (const std::exception&) {
       
   286             OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_SHOWITEMMENU, "CpDestinationEntryItemData::showItemMenu: Exception caught");
       
   287             if (destination != NULL) {
       
   288                 delete destination;
       
   289             }
       
   290         }
       
   291         
       
   292         HbMenu *menu = createItemMenu(protLvl, position);
       
   293         menu->show();
       
   294     }
       
   295     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SHOWITEMMENU_EXIT);
       
   296 }
       
   297 
       
   298 /*!
       
   299    Opens selected destination.
       
   300  */
       
   301 void CpDestinationEntryItemData::openDestination()
       
   302 {    
       
   303     onLaunchView();
       
   304 }
       
   305 
       
   306 /*!
       
   307     Prompts user for new destination name and makes 
       
   308     the change in commsdat if new name is valid.
       
   309     
       
   310     \sa isDestinationNameValid()
       
   311  */
       
   312 void CpDestinationEntryItemData::renameDestination()
       
   313 {    
       
   314     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_RENAMEDESTINATION_ENTRY);
       
   315     mDialog = new HbInputDialog();
       
   316     mDialog->setAttribute(Qt::WA_DeleteOnClose);
       
   317     mDialog->lineEdit()->setMaxLength(CMManagerShim::CmNameLength);
       
   318     mDialog->clearActions();
       
   319     mDialog->setPromptText(hbTrId("txt_occ_dialog_destination_name"));
       
   320     mDialog->setInputMode(HbInputDialog::TextInput);
       
   321     mDialog->setValue(mDestinationName);
       
   322     mOkAction = new HbAction(
       
   323         hbTrId("txt_common_button_ok"),
       
   324         mDialog);
       
   325     bool connected = connect(mOkAction, 
       
   326                              SIGNAL(triggered()), 
       
   327                              this, 
       
   328                              SLOT(saveNewDestinationName()));
       
   329     Q_ASSERT(connected);
       
   330     HbAction *cancelAction = new HbAction(
       
   331         hbTrId("txt_common_button_cancel"),
       
   332         mDialog);
       
   333     mDialog->addAction(mOkAction);
       
   334     mDialog->addAction(cancelAction);
       
   335     mDialog->show();
       
   336     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_RENAMEDESTINATION_EXIT);
       
   337 }
       
   338 
       
   339 /*!
       
   340     Shows confirmation query for user before deleteting destination.
       
   341     \sa deleteDestination()
       
   342  */
       
   343 void CpDestinationEntryItemData::confirmDestinationDelete()
       
   344 {
       
   345     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CONFIRMDESTINATIONDELETE_ENTRY);
       
   346     HbMessageBox *note = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
       
   347     note->setAttribute(Qt::WA_DeleteOnClose);
       
   348     note->setText(HbParameterLengthLimiter(
       
   349         "txt_occ_info_delete_snap").arg(mDestinationName));
       
   350     note->clearActions();
       
   351     HbAction *okAction = new HbAction(
       
   352         hbTrId("txt_common_button_yes"),
       
   353         note);
       
   354     note->addAction(okAction);
       
   355     bool connected = connect(
       
   356         okAction, 
       
   357         SIGNAL(triggered()), 
       
   358         this, 
       
   359         SLOT(deleteDestination()));
       
   360     Q_ASSERT(connected);
       
   361     HbAction *cancelAction = new HbAction(
       
   362         hbTrId("txt_common_button_no"),
       
   363         note);
       
   364     note->addAction(cancelAction);
       
   365     note->setTimeout(HbPopup::NoTimeout);
       
   366     note->show();
       
   367     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CONFIRMDESTINATIONDELETE_EXIT);
       
   368 }
       
   369 
       
   370 /*!
       
   371     Destination is deleted after user chooses "OK" from destination deletion
       
   372     confirmation query.
       
   373     \sa confirmDestinationDelete()
       
   374  */
       
   375 void CpDestinationEntryItemData::deleteDestination()
       
   376 {
       
   377     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_DELETEDESTINATION_ENTRY);
       
   378     CmDestinationShim *destination = NULL;
       
   379     CmConnectionMethodShim *cm = NULL;
       
   380     try {
       
   381         destination = mCmm->destination(mDestinationId);
       
   382         int cmCount = destination->connectionMethodCount();
       
   383         while (cmCount > 0) {
       
   384             cm = destination->connectionMethod(0);
       
   385             destination->removeConnectionMethod(cm);
       
   386             cmCount--;
       
   387             delete cm;
       
   388             cm = NULL;
       
   389         }
       
   390         destination->update();
       
   391         destination->deleteDestination();
       
   392         CpDestinationGroup *parent 
       
   393             = static_cast<CpDestinationGroup *>(HbDataFormModelItem::parent());
       
   394         parent->deleteDestination(mDestinationId);
       
   395         delete destination;
       
   396     } catch (const std::exception&) {
       
   397         OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_DELETEDESTINATION, "CpDestinationEntryItemData::deleteDestination: Exception caught");
       
   398         if (destination != NULL) {
       
   399             delete destination;
       
   400         }
       
   401         if (cm != NULL) {
       
   402             delete cm;
       
   403         }
       
   404         showErrorNote(hbTrId("txt_occ_info_unable_to_save_setting"));
       
   405     }
       
   406     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_DELETEDESTINATION_EXIT);
       
   407 }
       
   408 
       
   409 /*!
       
   410     Creates new view where access point priorities can 
       
   411     be changed by dragging and dropping and shows it to user.
       
   412  */
       
   413 void CpDestinationEntryItemData::activateArrangeMode()
       
   414 {
       
   415     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_ACTIVATEARRANGEMODE_ENTRY);
       
   416     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();            
       
   417     HbMainWindow *mainWnd = mainWindows.front();
       
   418     HbView *view = new HbView();
       
   419     createArrangeModeView(view);
       
   420     mList->installEventFilter(this);
       
   421     
       
   422     if (mainWnd && view) {
       
   423         mPreView = mainWnd->currentView();
       
   424         mainWnd->addView(view);
       
   425         mainWnd->setCurrentView(view, false);
       
   426         HbAction *arrangeViewBackAction = new HbAction(Hb::BackNaviAction, view);
       
   427         bool connected = connect(
       
   428             arrangeViewBackAction, 
       
   429             SIGNAL(triggered()), 
       
   430             this, 
       
   431             SLOT(viewCancel()));
       
   432         Q_ASSERT(connected);
       
   433         view->setNavigationAction(arrangeViewBackAction);
       
   434     }
       
   435     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_ACTIVATEARRANGEMODE_EXIT);
       
   436 }
       
   437 
       
   438 /*!
       
   439     This function is called when user returns from arrange mode with
       
   440     toolbar command "Done" thus accapting the changes. New priorities
       
   441     are saved to commsdat according to order of the items in the previous view.
       
   442  */
       
   443 void CpDestinationEntryItemData::viewDone()
       
   444 {
       
   445     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_VIEWDONE_ENTRY);
       
   446     try {
       
   447         // Arrange items
       
   448         CmDestinationShim *destination = mCmm->destination(mDestinationId);
       
   449         for (int i = 0; i < mList->count(); i++) {
       
   450             int apId = mList->item(i)->data(Hb::IndexFeedbackRole).toInt();
       
   451             CmConnectionMethodShim *cm = mCmm->connectionMethod(apId);
       
   452             destination->modifyPriority(cm,i);
       
   453             delete cm;
       
   454         }
       
   455         destination->update();
       
   456         delete destination;
       
   457     } catch (const std::exception&) {
       
   458         OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_VIEWDONE, "CpDestinationEntryItemData::viewDone: Exception caught");
       
   459         viewCancel();
       
   460         showErrorNote(QString("txt_occ_info_unable_to_save_setting"));
       
   461         OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_VIEWDONE_EXIT);
       
   462         return;
       
   463     }
       
   464     
       
   465     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();            
       
   466     HbMainWindow *mainWnd = mainWindows.front();
       
   467     HbView* view = mainWnd->currentView();
       
   468     
       
   469     if (mainWnd && view)  {
       
   470         //restore previous status
       
   471         mList->removeEventFilter(this);
       
   472         mainWnd->removeView(view);
       
   473         mainWnd->setCurrentView(mPreView);
       
   474         mPreView = NULL;
       
   475     }
       
   476     updateDestinationView();
       
   477     OstTraceFunctionExit0(DUP1_CPDESTINATIONENTRYITEMDATA_VIEWDONE_EXIT);
       
   478 }
       
   479 
       
   480 /*!
       
   481     User returns from view without accepting the changes to priorities.
       
   482  */
       
   483 void CpDestinationEntryItemData::viewCancel()
       
   484 {
       
   485     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_VIEWCANCEL_ENTRY);
       
   486     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();            
       
   487     HbMainWindow *mainWnd = mainWindows.front();
       
   488     HbView* view = mainWnd->currentView();
       
   489     
       
   490     if (mainWnd && view) {
       
   491         //restore previous status
       
   492         mList->removeEventFilter(this);
       
   493         mainWnd->removeView(view);
       
   494         view->deleteLater();
       
   495         mainWnd->setCurrentView(mPreView);
       
   496         mPreView = NULL;
       
   497     }
       
   498     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_VIEWCANCEL_EXIT);
       
   499 }
       
   500 
       
   501 /*!
       
   502     This function updates access points priorities shown in UI
       
   503     when user is in arrange mode.
       
   504     
       
   505     \sa activateArrangeMode()
       
   506  */
       
   507 void CpDestinationEntryItemData::updateIndex()
       
   508 {
       
   509     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_UPDATEINDEX_ENTRY);
       
   510     for (int i = 0; i < mList->count(); i++) {
       
   511         HbListWidgetItem *item = mList->item(i);
       
   512         QString priority = hbTrId("txt_occ_dblist_val_priority_l1").arg(i + 1);
       
   513         item->setSecondaryText(priority);
       
   514     }
       
   515     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_UPDATEINDEX_EXIT);
       
   516 }
       
   517 
       
   518 /*!
       
   519     This function searches all connection methods from commsdat that are
       
   520     connected to this destination item. Connection methods are returned in
       
   521     apList reference parameter.
       
   522     
       
   523     @param[out] apList Constains list of this destination's accesspoints.
       
   524     @param[in] cmm Pointer to CmManagerShim instance to avoid multiple session openings.
       
   525  */
       
   526 void CpDestinationEntryItemData::fetchReferencedAps(
       
   527     QList<QSharedPointer<CmConnectionMethodShim> > &apList, 
       
   528     const CmManagerShim *cmm) const
       
   529 {   
       
   530     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_FETCHREFERENCEDAPS_ENTRY);
       
   531     CmDestinationShim *destination = NULL;
       
   532     try {
       
   533         if (mDestinationId == 0) {
       
   534             QList<uint> apIds;
       
   535             cmm->connectionMethod(apIds);
       
   536             for (int i = 0; i < apIds.count(); i++) {
       
   537                 CmConnectionMethodShim *cm;
       
   538                 cm = cmm->connectionMethod(apIds.at(i));
       
   539                 if (!cm->getBoolAttribute(CMManagerShim::CmHidden)) {
       
   540                     apList.append(QSharedPointer<CmConnectionMethodShim>(cm));
       
   541                 } else {
       
   542                     delete cm;
       
   543                 }
       
   544             }
       
   545         } else {
       
   546             destination = cmm->destination(mDestinationId);
       
   547             int apCount = destination->connectionMethodCount();
       
   548             for (int i = 0; i < apCount; i++) {
       
   549                 CmConnectionMethodShim *cm = NULL;
       
   550                 cm = destination->connectionMethod(i);
       
   551                 if (!cm->getBoolAttribute(CMManagerShim::CmDestination)
       
   552                      && !cm->getBoolAttribute(CMManagerShim::CmHidden)) {
       
   553                     apList.append(QSharedPointer<CmConnectionMethodShim>(cm));
       
   554                 } else {
       
   555                     delete cm;
       
   556                 }
       
   557             }
       
   558             delete destination;
       
   559         }
       
   560     } catch (const std::exception&) {
       
   561         OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_FETCHREFERENCEDAPS, "CpDestinationEntryItemData::fetchReferencedAps: Exception caught");
       
   562         if (destination != NULL) {
       
   563             delete destination;
       
   564         }
       
   565     }
       
   566     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_FETCHREFERENCEDAPS_EXIT);
       
   567 }
       
   568 
       
   569 /*!
       
   570     Function for checking if the given destination name is valid. Duplicate and
       
   571     and empty names are rejected.
       
   572     
       
   573     @param[out] destination Constains the destination name to be validated. If name 
       
   574                 is valid it remains unchanged. If the given name is invalid, appropriate
       
   575                 error text is set the variable.
       
   576     @param[in] cmm Pointer to CmManagerShim instance to avoid multiple session openings.
       
   577     
       
   578     \return true if name is valid.
       
   579  */
       
   580 bool CpDestinationEntryItemData::isDestinationNameValid(
       
   581     QString &destination, 
       
   582     const CmManagerShim *cmm)
       
   583 {
       
   584     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_ISDESTINATIONNAMEVALID_ENTRY);
       
   585     bool retVal = true;
       
   586     
       
   587     if (destination.length() > 0) {
       
   588         QList<uint> destinationList;
       
   589         cmm->allDestinations(destinationList);
       
   590     
       
   591         for (int i = 0; i < destinationList.count(); i ++) {
       
   592             CmDestinationShim *dest = cmm->destination(destinationList[i]);
       
   593             if (0 == destination.compare(dest->name())) {
       
   594                 destination = hbTrId("txt_occ_info_name_already_in_use");
       
   595                 retVal = false;
       
   596                 delete dest;
       
   597                 break;
       
   598             }
       
   599             delete dest;
       
   600         }
       
   601     } else {
       
   602         destination = hbTrId("txt_occ_info_invalid_name");
       
   603         retVal = false;
       
   604     }
       
   605     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_ISDESTINATIONNAMEVALID_EXIT);
       
   606     return retVal;
       
   607 }
       
   608 
       
   609 /*!
       
   610     Worker function for activateArrangeMode(). Adds list of access points
       
   611     to given view.
       
   612     
       
   613     @param[out] view Target view where the arrange mode is constructed.
       
   614 
       
   615     \sa activateArrangeMode()
       
   616  */
       
   617 void CpDestinationEntryItemData::createArrangeModeView(HbView *view)
       
   618 {
       
   619     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CREATEARRANGEMODEVIEW_ENTRY);
       
   620     QList<QSharedPointer<CmConnectionMethodShim> >  apList;
       
   621     fetchReferencedAps(apList, mCmm);   
       
   622     CmDestinationShim *destination = NULL;
       
   623     mList = new HbListWidget();
       
   624     view->setWidget(mList);
       
   625     
       
   626     try {
       
   627         destination = mCmm->destination(mDestinationId);
       
   628         for (int i = 0; i < apList.count(); i++) {
       
   629             HbListWidgetItem *item = new HbListWidgetItem();
       
   630             item->setText(apList[i]->getStringAttribute(CMManagerShim::CmName));
       
   631             item->setData(apList[i]->getIntAttribute(CMManagerShim::CmId), Hb::IndexFeedbackRole);
       
   632 
       
   633             uint pri = destination->priority(apList[i].data());
       
   634             QString priority = hbTrId("txt_occ_dblist_val_priority_l1").arg(pri);
       
   635             item->setSecondaryText(priority);
       
   636             
       
   637             HbIcon iapIcon(resolveApIcon(apList[i]));
       
   638             item->setIcon(iapIcon);
       
   639         
       
   640             mList->addItem(item);
       
   641         }
       
   642         mList->setArrangeMode(true);
       
   643         delete destination;
       
   644     } catch (const std::exception&) {
       
   645         OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_CREATEARRANGEMODEVIEW, "CpDestinationEntryItemData::createArrangeModeView: Exception caught");
       
   646         // return empty view
       
   647         if (destination != NULL) {
       
   648             delete destination;
       
   649         }
       
   650         while (mList->count() > 0) {
       
   651             HbListWidgetItem *item = mList->item(0);
       
   652             delete item;
       
   653         }
       
   654         mList->clear();
       
   655     }
       
   656     // Toolbar
       
   657     HbToolBar *tb = view->toolBar();
       
   658     HbIcon okIcon("qtg_mono_tick");
       
   659     HbAction *doneAction 
       
   660         = tb->addAction(okIcon, "", this, SLOT(viewDone()));
       
   661     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CREATEARRANGEMODEVIEW_EXIT);
       
   662 }
       
   663 
       
   664 /*!
       
   665     Helper function for creating item specific menu.
       
   666     
       
   667     @param[in] protLvl Effective protection level for this
       
   668                        access point.
       
   669  */
       
   670 HbMenu *CpDestinationEntryItemData::createItemMenu(
       
   671     CMManagerShim::CmmProtectionLevel protLvl,
       
   672     const QPointF position)
       
   673 {
       
   674     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_CREATEITEMMENU_ENTRY);
       
   675     HbMenu *menu = new HbMenu();
       
   676     menu->setAttribute(Qt::WA_DeleteOnClose);
       
   677     HbAction *openDestAction = menu->addAction(hbTrId("txt_common_menu_open"));
       
   678     bool connected = 
       
   679         connect(openDestAction, SIGNAL(triggered()), this, SLOT(openDestination()));
       
   680     Q_ASSERT(connected);
       
   681     HbAction *renameDestAction = menu->addAction(hbTrId("txt_common_menu_rename_item"));
       
   682     connected = 
       
   683         connect(renameDestAction, SIGNAL(triggered()), this, SLOT(renameDestination()));
       
   684     Q_ASSERT(connected);
       
   685     HbAction *deleteDestAction = menu->addAction(hbTrId("txt_common_menu_delete"));
       
   686     connected = 
       
   687         connect(deleteDestAction, SIGNAL(triggered()), this, SLOT(confirmDestinationDelete()));
       
   688     Q_ASSERT(connected);
       
   689                      
       
   690     if (protLvl == CMManagerShim::ProtLevel1
       
   691          || protLvl == CMManagerShim::ProtLevel2 ) {
       
   692     
       
   693         // Disable operations for protected destinations
       
   694         renameDestAction->setDisabled(true);
       
   695         deleteDestAction->setDisabled(true);
       
   696     }
       
   697         
       
   698     menu->setModal(true);
       
   699     menu->setPreferredPos(position);
       
   700     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_CREATEITEMMENU_EXIT);
       
   701     return menu;
       
   702 }
       
   703 
       
   704 /*!
       
   705     This function is called when user selects OK from destination
       
   706     name query popup. The given name is validated and if the name is
       
   707     valid, new destination name is saved to commsdat. If validation fails
       
   708     user is prompted again for destination name.
       
   709  */
       
   710 void CpDestinationEntryItemData::saveNewDestinationName()
       
   711 {
       
   712     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SAVENEWDESTINATIONNAME_ENTRY);
       
   713     QString destinationName = mDialog->value().toString();
       
   714     if (destinationName != mDestinationName) {
       
   715         // Destination name was changed
       
   716         bool destinationNameInvalid = true;
       
   717         CmManagerShim *cmm = NULL;
       
   718         CmDestinationShim *destination = NULL;
       
   719         
       
   720         try {
       
   721             cmm = new CmManagerShim();
       
   722             if (isDestinationNameValid(destinationName, cmm)) {
       
   723                 destination = cmm->destination(mDestinationId);
       
   724                 destination->setName(destinationName);
       
   725                 destination->update();
       
   726                 mDestinationName = destinationName;
       
   727                 destinationNameInvalid = false;
       
   728             }
       
   729         } catch (const std::exception&) {
       
   730             OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SAVENEWDESTINATIONNAME_EXIT);
       
   731             return;
       
   732         }
       
   733         delete destination;
       
   734         delete cmm;
       
   735         
       
   736         if (destinationNameInvalid) {   
       
   737             // Validation function has modified destination name 
       
   738             // to be error string
       
   739             showRenameError(destinationName);
       
   740         } else {
       
   741             OstTrace0(TRACE_NORMAL, CPDESTINATIONENTRYITEMDATA_SAVENEWDESTINATIONNAME, "CpDestinationEntryItemData::saveNewDestinationName: emit destination changed");
       
   742             emit destChanged();
       
   743         }
       
   744     }
       
   745     OstTraceFunctionExit0(DUP1_CPDESTINATIONENTRYITEMDATA_SAVENEWDESTINATIONNAME_EXIT);
       
   746 }
       
   747 
       
   748 /*!
       
   749  * Helper function for showing error note when user inputs
       
   750  * invalid destination name.
       
   751  */
       
   752 void CpDestinationEntryItemData::showRenameError(const QString &info)
       
   753 {
       
   754     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SHOWRENAMEERROR_ENTRY);
       
   755     // Destination name NOK. Inform user and ask again.
       
   756     HbMessageBox *note = new HbMessageBox(HbMessageBox::MessageTypeInformation);
       
   757     note->clearActions();
       
   758     note->setAttribute(Qt::WA_DeleteOnClose);
       
   759     note->setText(info);
       
   760     note->setTimeout(HbPopup::NoTimeout);
       
   761     HbAction *errorOk = new HbAction(
       
   762         hbTrId("txt_common_button_ok"),
       
   763         note);
       
   764     bool connected = connect(
       
   765         errorOk,
       
   766         SIGNAL(triggered()),
       
   767         this,
       
   768         SLOT(renameDestination()));
       
   769     Q_ASSERT(connected);                        
       
   770     note->addAction(errorOk);
       
   771     note->show();
       
   772     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SHOWRENAMEERROR_EXIT);
       
   773 }
       
   774 
       
   775 /*!
       
   776     Helper function for showing error notes.
       
   777     
       
   778     @param[in] info Info string to be shown in note.
       
   779  */
       
   780 void CpDestinationEntryItemData::showErrorNote(const QString &info)
       
   781 {
       
   782     OstTraceFunctionEntry0(CPDESTINATIONENTRYITEMDATA_SHOWERRORNOTE_ENTRY);
       
   783     HbMessageBox *note = new HbMessageBox(HbMessageBox::MessageTypeWarning);
       
   784     note->clearActions();
       
   785     note->setAttribute(Qt::WA_DeleteOnClose);
       
   786     note->setText(info);
       
   787     note->setTimeout(HbPopup::NoTimeout);
       
   788     HbAction *errorOk = new HbAction(
       
   789         hbTrId("txt_common_button_ok"),
       
   790         note);
       
   791     note->addAction(errorOk);
       
   792     note->show();    
       
   793     OstTraceFunctionExit0(CPDESTINATIONENTRYITEMDATA_SHOWERRORNOTE_EXIT);
       
   794 }
       
   795 
       
   796 /*!
       
   797     Helper function for showing icons.
       
   798     
       
   799     \return Returns string representing given destination's icon
       
   800  */
       
   801 QString CpDestinationEntryItemData::resolveApIcon(QSharedPointer<CmConnectionMethodShim> cm) const
       
   802 {
       
   803     QString result(cm->getIcon());
       
   804     
       
   805     if (result.isEmpty()) {
       
   806         uint bearerType = cm->getIntAttribute(CMManagerShim::CmBearerType);
       
   807         switch (bearerType) {
       
   808             case CMManagerShim::BearerTypeWlan:
       
   809                 result = "qtg_small_wlan";
       
   810                 break;
       
   811             case CMManagerShim::BearerTypePacketData:
       
   812                 result = "qtg_small_gprs";
       
   813                 break;
       
   814             default:
       
   815                 // Unknown bearer type
       
   816                 break;
       
   817         }
       
   818     }
       
   819     return result;
       
   820 }
       
   821 
       
   822 /*!
       
   823     Event filter for updating priorities when arrange mode is active.
       
   824     
       
   825     \return Returns false so that event gets forwarded
       
   826  */
       
   827 bool CpDestinationEntryItemData::eventFilter(QObject *object, QEvent *event)
       
   828 {
       
   829     Q_UNUSED(object);
       
   830     if (event->type() == QEvent::GraphicsSceneMouseRelease) {
       
   831         updateIndex();
       
   832     }
       
   833     return false;
       
   834 }
       
   835