cmmanager/cpdestinationplugin/src/cpdestinationgroup.cpp
changeset 20 9c97ad6591ae
child 23 7ec726f93df1
child 28 860702281757
equal deleted inserted replaced
18:fcbbe021d614 20:9c97ad6591ae
       
     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 *   CpDestinationGroup contains all destination UI items as well as 
       
    16 *   items for uncategorized access points and adding new destinations.
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <QDir>
       
    21 #include <QPluginLoader>
       
    22 #include <QStringList>
       
    23 #include <HbDataform>
       
    24 #include <HbDataFormModel>
       
    25 #include <HbDataFormViewItem>
       
    26 #include <cpbasesettingview.h>
       
    27 #include <cpitemdatahelper.h>
       
    28 #include <cpsettingformentryitemdataimpl.h>
       
    29 #include <cpsettingformitemdata.h>
       
    30 #include <cmmanager_shim.h>
       
    31 #include <cmdestination_shim.h>
       
    32 #include <cmconnectionmethod_shim.h>
       
    33 #include <cmmanagerdefines_shim.h>
       
    34 
       
    35 // User includes
       
    36 #include "cpdestinationgroup.h"
       
    37 #include "cpdestinationentryitem.h"
       
    38 #include "cpadddestinationentryitemdata.h"
       
    39 #include "OstTraceDefinitions.h"
       
    40 #ifdef OST_TRACE_COMPILER_IN_USE
       
    41 #include "cpdestinationgroupTraces.h"
       
    42 #endif
       
    43 
       
    44 /*!
       
    45     \class  CpDestinationGroup
       
    46     \brief  This class contains all data items representing destinations
       
    47     in commsdat. 
       
    48 */
       
    49 
       
    50 // External function prototypes
       
    51 
       
    52 // Local constants
       
    53 
       
    54 // ======== LOCAL FUNCTIONS ========
       
    55 
       
    56 // ======== MEMBER FUNCTIONS ========
       
    57 
       
    58 /*!
       
    59     
       
    60     Constructor.
       
    61     
       
    62     @param[in] itemDataHelper Helper for connecting signals and slots.
       
    63  */
       
    64 CpDestinationGroup::CpDestinationGroup(CpItemDataHelper &itemDataHelper) :
       
    65     CpSettingFormItemData(HbDataFormModelItem::GroupItem, 
       
    66         hbTrId("txt_occ_subhead_destinations_access_points")),
       
    67    mItemDataHelper(&itemDataHelper),
       
    68    mUncategorisedShown(false)
       
    69 {
       
    70     OstTraceFunctionEntry0(CPDESTINATIONGROUP_CPDESTINATIONGROUP_ENTRY);
       
    71     mCmManager = new CmManagerShim();
       
    72     mBearerPlugins = new QList<CpBearerApPluginInterface *>();
       
    73     this->loadBearerPlugins();
       
    74     
       
    75     QList<QSharedPointer<CmDestinationShim> > destinationList;
       
    76     fetchDestinations(destinationList);
       
    77 
       
    78     // Create Destination list
       
    79     for (int i = 0; i < destinationList.count(); i++) {
       
    80         QString iapCount = 
       
    81 			getDestinationAdditionalText(destinationList.at(i)->connectionMethodCount());
       
    82         
       
    83         // Create new destination entry
       
    84         CpDestinationEntryItemData *destDataItem;
       
    85         destDataItem = new CpDestinationEntryItemData(itemDataHelper);
       
    86         destDataItem->setContentWidgetData(QString("text"), destinationList.at(i)->name());
       
    87         destDataItem->setContentWidgetData(QString("additionalText"),iapCount);
       
    88         destDataItem->setDestinationId(destinationList.at(i)->id());
       
    89         destDataItem->setDestinationName(destinationList.at(i)->name());
       
    90         bool connected = connect(destDataItem, 
       
    91                                  SIGNAL(destChanged()), 
       
    92                                  this, 
       
    93                                  SLOT(updateDestinationInformation()));
       
    94         Q_ASSERT(connected);
       
    95         this->appendChild(destDataItem);
       
    96     }
       
    97     
       
    98     // Add "Uncategorised Iaps" button
       
    99     createUncategorisedDestination();
       
   100     
       
   101     // "Add Destination" button
       
   102     CpSettingFormEntryItemData *addDest = new CpAddDestinationEntryItemData(itemDataHelper, this);
       
   103     addDest->setContentWidgetData(QString("text"), hbTrId("txt_occ_button_add_destination"));
       
   104     this->appendChild(addDest);
       
   105     OstTraceFunctionExit0(CPDESTINATIONGROUP_CPDESTINATIONGROUP_EXIT);
       
   106 }
       
   107 
       
   108 /*!
       
   109     Destructor
       
   110  */
       
   111 CpDestinationGroup::~CpDestinationGroup()
       
   112 {
       
   113     OstTraceFunctionEntry0(DUP1_CPDESTINATIONGROUP_CPDESTINATIONGROUP_ENTRY);
       
   114     delete mBearerPlugins;
       
   115     delete mCmManager;
       
   116     OstTraceFunctionExit0(DUP1_CPDESTINATIONGROUP_CPDESTINATIONGROUP_EXIT);
       
   117 }
       
   118 
       
   119 /*!
       
   120     addDestination() creates new destination item to Destinations group item.
       
   121     The new destination is shown in the UI immediately.
       
   122  */
       
   123 void CpDestinationGroup::addDestination(const QString &dest, int destId)
       
   124 {
       
   125     OstTraceFunctionEntry0(CPDESTINATIONGROUP_ADDDESTINATION_ENTRY);
       
   126     QString iapCount = getDestinationAdditionalText(0);
       
   127     
       
   128     // Create UI item for new destination
       
   129     CpDestinationEntryItemData *destDataItem;
       
   130     destDataItem = new CpDestinationEntryItemData(*mItemDataHelper);
       
   131     destDataItem->setContentWidgetData(QString("text"), dest);
       
   132     destDataItem->setContentWidgetData(QString("additionalText"), iapCount);
       
   133     destDataItem->setDestinationId(destId);
       
   134     destDataItem->setDestinationName(dest);
       
   135     connect(destDataItem, SIGNAL(destChanged()), this, SLOT(updateDestinationInformation()));
       
   136     
       
   137     // Use ItemDataHelper to make connections
       
   138     QObject* form = this->model()->parent();
       
   139     mItemDataHelper->bindToForm(static_cast<HbDataForm*>(form));
       
   140     
       
   141     // Insert Child to correct position
       
   142     QList<QSharedPointer<CmDestinationShim> > destinationList;
       
   143     fetchDestinations(destinationList);
       
   144     insertChild(destinationList.count() - 1, destDataItem);
       
   145     OstTraceFunctionExit0(CPDESTINATIONGROUP_ADDDESTINATION_EXIT);
       
   146 }
       
   147 
       
   148 /*!
       
   149     Deletes destination from commsdat and removes it from UI.
       
   150  */
       
   151 void CpDestinationGroup::deleteDestination(int destId)
       
   152 {
       
   153     OstTraceFunctionEntry0(CPDESTINATIONGROUP_DELETEDESTINATION_ENTRY);
       
   154     // "Add Destination" child is removed from count (childCount() -1)
       
   155     for (int i = 0; i < this->childCount() - 1; i++) { 
       
   156         CpDestinationEntryItemData  *destDataItem = 
       
   157             static_cast<CpDestinationEntryItemData*>(this->childAt(i));
       
   158 
       
   159         if (destDataItem->destinationId() == destId) {
       
   160             int index = this->indexOf(destDataItem);
       
   161             this->removeChild(index);
       
   162             
       
   163             if (!mUncategorisedShown) {
       
   164             // Return "Uncategorised" item to UI if necessary
       
   165             createUncategorisedDestination();
       
   166             }
       
   167         }
       
   168     }
       
   169     OstTraceFunctionExit0(CPDESTINATIONGROUP_DELETEDESTINATION_EXIT);
       
   170 }
       
   171 
       
   172 /*!
       
   173     Finds correct access point settings implementation according to
       
   174     access point bearer type.
       
   175     
       
   176     \return Returns pointer to CpBearerApPluginInterface which implements
       
   177             access point settins.
       
   178  */
       
   179 CpBearerApPluginInterface *CpDestinationGroup::findBearerPlugin(int apId)
       
   180 {
       
   181     OstTraceFunctionEntry0(CPDESTINATIONGROUP_FINDBEARERPLUGIN_ENTRY);
       
   182     CpBearerApPluginInterface *retVal = NULL;
       
   183     try {
       
   184         CmConnectionMethodShim *cm = mCmManager->connectionMethod(apId);
       
   185         int apBearerType = cm->getIntAttribute(CMManagerShim::CmBearerType);
       
   186     
       
   187         for (int i = 0; i < mBearerPlugins->count(); i++) {
       
   188             if (mBearerPlugins->at(i)->bearerType() == apBearerType) {
       
   189                 retVal = mBearerPlugins->at(i);
       
   190                 break;
       
   191             }
       
   192         }
       
   193         delete cm;
       
   194     }
       
   195     catch (const std::exception&) {
       
   196         OstTrace0(TRACE_NORMAL, CPDESTINATIONGROUP_FINDBEARERPLUGIN, "CpDestinationGroup::findBearerPlugin: Exception caught");
       
   197         // Let return value be NULL.
       
   198     }
       
   199     OstTraceFunctionExit0(CPDESTINATIONGROUP_FINDBEARERPLUGIN_EXIT);
       
   200     return retVal;
       
   201 }
       
   202 
       
   203 /*!
       
   204     Updates all destination group items' destinations' information. 
       
   205     (Name and access point count)
       
   206  */
       
   207 void CpDestinationGroup::updateDestinationInformation()
       
   208 {
       
   209 	OstTraceFunctionEntry0(CPDESTINATIONGROUP_UPDATEDESTINATIONINFORMATION_ENTRY);
       
   210 	int apCount = 0;
       
   211 	// "Add Destination" child is removed from count (childCount() -1)
       
   212     for (int i = 0; i < childCount() - 1; i++) {
       
   213         CpDestinationEntryItemData  *destDataItem = 
       
   214                 static_cast<CpDestinationEntryItemData*>(this->childAt(i));
       
   215         
       
   216         if (destDataItem->destinationId() == 0) {
       
   217             // Uncategrised Iaps
       
   218 	        QList<uint> apList;
       
   219             mCmManager->connectionMethod(apList);
       
   220             if (apList.count() == 0) {
       
   221                 // delete empty uncategorised IAPs destination
       
   222                 int index = this->indexOf(destDataItem);
       
   223                 this->removeChild(index);
       
   224                 mUncategorisedShown = false;
       
   225                 continue;
       
   226             }
       
   227             apCount = apList.count();  
       
   228         } else {
       
   229             CmDestinationShim *destination;
       
   230             destination = mCmManager->destination(destDataItem->destinationId());
       
   231             apCount = destination->connectionMethodCount();
       
   232             delete destination;
       
   233         }
       
   234 
       
   235         QString iapCount = getDestinationAdditionalText(apCount);
       
   236         destDataItem->setContentWidgetData(QString("additionalText"), iapCount);
       
   237         destDataItem->setContentWidgetData(QString("text"), destDataItem->destinationName());
       
   238     }
       
   239     // Update UI
       
   240     HbDataForm *form = static_cast<HbDataForm*>(this->model()->parent());
       
   241     HbDataFormModel* model = static_cast<HbDataFormModel*>(this->model());
       
   242     QModelIndex index = model->indexFromItem(this);
       
   243     HbDataFormViewItem *viewItem = form->dataFormViewItem(index);
       
   244     viewItem->setExpanded(false);
       
   245     viewItem->setExpanded(true);
       
   246     OstTraceFunctionExit0(CPDESTINATIONGROUP_UPDATEDESTINATIONINFORMATION_EXIT);
       
   247 }
       
   248 
       
   249 /*!
       
   250     Creates item for uncategorized access points to destination group item
       
   251     if there is any.
       
   252  */
       
   253 void CpDestinationGroup::createUncategorisedDestination()
       
   254 {
       
   255     OstTraceFunctionEntry0(CPDESTINATIONGROUP_CREATEUNCATEGORISEDDESTINATION_ENTRY);
       
   256     QList<uint> apList;
       
   257     mCmManager->connectionMethod(apList);
       
   258     if (apList.count() > 0) {
       
   259         // Create new destination entry
       
   260         CpDestinationEntryItemData *destDataItem;
       
   261         destDataItem = new CpDestinationEntryItemData(*mItemDataHelper);
       
   262         destDataItem->setContentWidgetData(QString("text"), hbTrId("txt_occ_dblist_uncategorized"));
       
   263         QString iapCount = getDestinationAdditionalText(apList.count());
       
   264         destDataItem->setContentWidgetData(QString("additionalText"),iapCount);
       
   265         destDataItem->setDestinationId(0);
       
   266         destDataItem->setDestinationName(hbTrId("txt_occ_dblist_uncategorized"));
       
   267         bool connected = connect(
       
   268             destDataItem, 
       
   269             SIGNAL(destChanged()), 
       
   270             this, 
       
   271             SLOT(updateDestinationInformation()));
       
   272         Q_ASSERT(connected);
       
   273         QList<QSharedPointer<CmDestinationShim> > destinationList;
       
   274         fetchDestinations(destinationList);
       
   275         
       
   276         insertChild(destinationList.count(), destDataItem);
       
   277         mUncategorisedShown = true;
       
   278     }
       
   279     OstTraceFunctionExit0(CPDESTINATIONGROUP_CREATEUNCATEGORISEDDESTINATION_EXIT);
       
   280 }
       
   281 
       
   282 /*!
       
   283     Searches all destinations from commsdat. List of representing 
       
   284     CmDestinationShim * items returned on completion.
       
   285  */
       
   286 void CpDestinationGroup::fetchDestinations(
       
   287     QList<QSharedPointer<CmDestinationShim> > &destinationList)
       
   288 {
       
   289     OstTraceFunctionEntry0(CPDESTINATIONGROUP_FETCHDESTINATIONS_ENTRY);
       
   290     try {
       
   291         QList<uint> destArray;
       
   292         mCmManager->allDestinations(destArray);
       
   293     
       
   294         for (int i = 0; i < destArray.count(); i++) {
       
   295             CmDestinationShim *destination;
       
   296             destination = mCmManager->destination(destArray[i]);
       
   297             if (!destination->isHidden()) {
       
   298                 destinationList.append(QSharedPointer<CmDestinationShim>(destination));
       
   299             }
       
   300         }
       
   301     } catch (const std::exception&) {
       
   302         OstTrace0(TRACE_NORMAL, CPDESTINATIONGROUP_FETCHDESTINATIONS, "CpDestinationGroup::fetchDestinations: exception caught, Reading destinations");
       
   303     }
       
   304     OstTraceFunctionExit0(CPDESTINATIONGROUP_FETCHDESTINATIONS_EXIT);
       
   305 }
       
   306 
       
   307 /*!
       
   308     Searches all bearer plugins from fixed location and loads them.
       
   309     
       
   310     \sa findBearerPlugin()
       
   311  */
       
   312 void CpDestinationGroup::loadBearerPlugins()
       
   313 {
       
   314     OstTraceFunctionEntry0(CPDESTINATIONGROUP_LOADBEARERPLUGINS_ENTRY);
       
   315     // Load bearer plugins
       
   316     QDir pluginsDir("\\resource\\qt\\plugins\\controlpanel\\bearerap");
       
   317     foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
       
   318         
       
   319         QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
       
   320         CpBearerApPluginInterface *plugin = 
       
   321                 qobject_cast<CpBearerApPluginInterface *>(loader.instance());
       
   322         if (plugin) {
       
   323             mBearerPlugins->append(plugin);
       
   324         }
       
   325     } 
       
   326     OstTraceFunctionExit0(CPDESTINATIONGROUP_LOADBEARERPLUGINS_EXIT);
       
   327 }
       
   328 
       
   329 /*!
       
   330     Helper function to be used in localisation.
       
   331     
       
   332     \return Returns correct localized QString according to access point count.
       
   333  */
       
   334 QString CpDestinationGroup::getDestinationAdditionalText(int iapCount)
       
   335 {
       
   336     OstTraceFunctionEntry0(CPDESTINATIONGROUP_GETDESTINATIONADDITIONALTEXT_ENTRY);
       
   337     QString result = "";
       
   338     if (iapCount > 0) {
       
   339         result = hbTrId("txt_occ_dblist_internet_val_ln_access_points", iapCount);
       
   340     } else {
       
   341         result = hbTrId("txt_occ_dblist_internet_val_no_access_points");
       
   342     }
       
   343     OstTrace0(TRACE_FLOW, CPDESTINATIONGROUP_GETDESTINATIONADDITIONALTEXT_EXIT, "Exit");
       
   344     return result;
       
   345 }