locationsystemui/locationsysui/possettings/poslocationservices/src/poslocationservices.cpp
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     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: This is for Location Services Settings plugin 
       
    15  *							for Positioning Settings's Advanced view
       
    16  *
       
    17  */
       
    18 
       
    19 #include "poslocationservices.h"
       
    20 #include "possettingsadvop.h"
       
    21 #include "locationservicescustomviewitem.h"
       
    22 #include "possettingssignalmapper.h"
       
    23 
       
    24 #include <QtCore>
       
    25 #include <QTranslator>
       
    26 #include <QDebug>
       
    27 
       
    28 #include <hbaction.h>
       
    29 #include <hbpushbutton.h>
       
    30 #include <hbmessagebox.h>
       
    31 #include <hbicon.h>
       
    32 #include <hbextendedlocale.h>
       
    33 
       
    34 // constants
       
    35 const int KMinToSecs = 60;
       
    36 const int KHourToSecs = 3600;
       
    37 const int KOneDay = 24;
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // PosLocationServices::PosLocationServices
       
    41 // ---------------------------------------------------------------------------
       
    42 //
       
    43 PosLocationServices::PosLocationServices() :
       
    44     mLocationServiceEngine(0), mGroup(0), mOperation(0), mSignalMapper(0),
       
    45             mStopQuery(0), mServiceId(0)
       
    46     {
       
    47     mLocationServiceEngine = new PosLocationServicesEngine();
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // PosLocationServices::~PosLocationServices
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 PosLocationServices::~PosLocationServices()
       
    55     {
       
    56     qDebug() << "+PosLocationServices::~PosLocationServices()";
       
    57 
       
    58     if (mLocationServiceEngine)
       
    59         {
       
    60         delete mLocationServiceEngine;
       
    61         mLocationServiceEngine = 0;
       
    62         }
       
    63 
       
    64     if (mGroup)
       
    65         {
       
    66         if (mOperation)
       
    67             {
       
    68             mOperation->removeItem(mGroup);
       
    69             }
       
    70         }
       
    71 
       
    72     if (mSignalMapper)
       
    73         {
       
    74         delete mSignalMapper;
       
    75         mSignalMapper = 0;
       
    76         }
       
    77 
       
    78     if (mStopQuery)
       
    79         {
       
    80         delete mStopQuery;
       
    81         mStopQuery = 0;
       
    82         }
       
    83 
       
    84     //Clear the list
       
    85     mServiceList.clear();
       
    86 
       
    87     //clear the signal mapper list
       
    88     int count = mSignalMapperList.count();
       
    89     qDebug() << "mSignalMapperList.count() = " << count;
       
    90 
       
    91     for (int i = 0; i < count; ++i)
       
    92         {
       
    93         delete mSignalMapperList[i];
       
    94         mSignalMapperList[i] = 0;
       
    95         }
       
    96     mSignalMapperList.clear();
       
    97 
       
    98     qDebug() << "-PosLocationServices::~PosLocationServices()";
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // PosLocationServices::initialise
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 int PosLocationServices::initialise(PosSettingsAdvOp* operation)
       
   106     {
       
   107     qDebug() << "+PosLocationServices::initialise()";
       
   108 
       
   109     int result = 0;
       
   110 
       
   111     // Assign to the member variable
       
   112     mOperation = operation;
       
   113 
       
   114     //get the prototype list from the data form and append the custom control to the list
       
   115     QList<HbAbstractViewItem *> protoTypeList;
       
   116     mOperation->itemPrototypes(protoTypeList);
       
   117 
       
   118     LocationServicesCustomViewItem* item =
       
   119             new LocationServicesCustomViewItem();
       
   120 
       
   121     protoTypeList.append(item);
       
   122 
       
   123     mOperation->setItemPrototypes(protoTypeList);
       
   124 
       
   125     // create the signal mapper
       
   126     mSignalMapper = new QSignalMapper();
       
   127 
       
   128     //get the list of location services
       
   129     mServiceList.clear();
       
   130     if (mLocationServiceEngine)
       
   131         {
       
   132         result = mLocationServiceEngine->getLocationServices(mServiceList);
       
   133         }
       
   134 
       
   135     qDebug() << "PosLocationServices::initialise(): result" << result;
       
   136 
       
   137     // if no error while getting the services list then add the group
       
   138     // for Location based services
       
   139     if (result == 0)
       
   140         {
       
   141         mGroup = new HbDataFormModelItem(HbDataFormModelItem::GroupItem,
       
   142                 hbTrId("txt_loe_subtitle_background_positioning"), 0);
       
   143 
       
   144         //Add the group to the data form
       
   145         mOperation->appendDataFormItem(mGroup);
       
   146 
       
   147         //list all the location services inside the group
       
   148         listServices();
       
   149         }
       
   150     QObject::connect(mLocationServiceEngine, SIGNAL(locationServiceUpdate()),
       
   151             this, SLOT(updateList()));
       
   152 
       
   153     qDebug() << "-PosLocationServices::initialise()";
       
   154     return result;
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // PosLocationServices::listServices
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void PosLocationServices::listServices()
       
   162     {
       
   163     qDebug() << "+PosLocationServices::listServices()";
       
   164     qDebug() << "No of Location Services = " << mServiceList.count();
       
   165 
       
   166     QList<PosLocationServiceInfo>::const_iterator serviceIterator;
       
   167 
       
   168     qDebug() << "Appending the services in group ";
       
   169 
       
   170     // Get the location services names and display
       
   171     for (serviceIterator = mServiceList.begin(); serviceIterator
       
   172             != mServiceList.end(); ++serviceIterator)
       
   173         {
       
   174         qDebug() << "serviceIterator->mServiceId = "
       
   175                 << serviceIterator->mServiceId;
       
   176 
       
   177         qDebug() << "serviceIterator->mServiceName = "
       
   178                 << serviceIterator->mServiceName;
       
   179 
       
   180         qDebug() << "serviceIterator->mEndDateTime = "
       
   181                 << serviceIterator->mEndDateTime.date().toString();
       
   182 
       
   183         qDebug() << "serviceIterator->mEndDateTime.isValid() = "
       
   184                 << serviceIterator->mEndDateTime.date().isValid();
       
   185 
       
   186         qDebug() << "serviceIterator->mUpdateInterval = "
       
   187                 << serviceIterator->mUpdateInterval;
       
   188 
       
   189         // if any of the fields are invalid then skip listing
       
   190           if (serviceIterator->mUpdateInterval > 0)
       
   191             {
       
   192             qDebug() << "creating services";
       
   193 
       
   194             //for each services create new custom item 
       
   195             HbDataFormModelItem
       
   196                     * service =
       
   197                             new HbDataFormModelItem(
       
   198                                     static_cast<HbDataFormModelItem::DataItemType> (LocationServicesCustomItem));
       
   199                                     	
       
   200             if(!serviceIterator->mServiceName.isEmpty())
       
   201             //set the widget data to the server name
       
   202             service->setContentWidgetData("primarytext",
       
   203                     serviceIterator->mServiceName);
       
   204             else
       
   205             service->setContentWidgetData("primarytext",
       
   206                     QString(hbTrId("txt_loe_info_unknown_requestor")));	        
       
   207 
       
   208             QString updateIntervalString;
       
   209             //get the update interval String
       
   210             getUpdateIntervalString(serviceIterator->mUpdateInterval,
       
   211                     updateIntervalString);
       
   212             service->setContentWidgetData("sectext", updateIntervalString);
       
   213 
       
   214             //get the validity end date from the engine and convert to string
       
   215             QString validity(serviceIterator->mEndDateTime.date().toString(
       
   216                     Qt::ISODate));
       
   217 
       
   218             service->setContentWidgetData("sec2text", QString(hbTrId(
       
   219                     "txt_loe_list_valid_until_1")).arg(validity));
       
   220 
       
   221             qDebug() << "Adding service into the group";
       
   222             mOperation->appendDataFormItem(service, mGroup);
       
   223 
       
   224             //create an object of dummy signal mapper class
       
   225             //this is done to send the index info to the slots
       
   226             //since checkbox is not of QObject type and dummy class is 
       
   227             // created.
       
   228             PosSettingsSignalMapper* signalMapper =
       
   229                     new PosSettingsSignalMapper();
       
   230 
       
   231             qDebug() << "Adding the signalMapper into mSignalMapperList";
       
   232             mSignalMapperList.append(signalMapper);
       
   233 
       
   234             //add connection for clicked for the custom item, depending on which the
       
   235             //add connections to launch the dialog for the stopping the service  
       
   236             mOperation->addConnection(
       
   237                     service,
       
   238                     SIGNAL(clicked(bool)),
       
   239                     mSignalMapperList[serviceIterator - mServiceList.begin()],
       
   240                     SLOT(signalmap()));
       
   241 
       
   242             QObject::connect(mSignalMapperList[serviceIterator
       
   243                     - mServiceList.begin()], SIGNAL(signalmapped()),
       
   244                     mSignalMapper, SLOT(map()));
       
   245 
       
   246             // set mapping of each check box item to its index
       
   247             if (mSignalMapper)
       
   248                 {
       
   249                 qDebug() << "mSignalMapper->setMapping ";
       
   250                 mSignalMapper->setMapping(mSignalMapperList[serviceIterator
       
   251                         - mServiceList.begin()], (serviceIterator
       
   252                         - mServiceList.begin()));
       
   253                 }
       
   254             }
       
   255         }
       
   256 
       
   257     qDebug() << "mGroup->childCount() = " << mGroup->childCount();
       
   258 
       
   259     if (mGroup->childCount() == 0)
       
   260         {
       
   261         qDebug() << "mOperation->removeItem mGroup";
       
   262         mOperation->removeItem(mGroup);
       
   263         mGroup = NULL;
       
   264         }
       
   265     else
       
   266         {
       
   267         //use signal mapper to connect
       
   268         qDebug() << "use signal mapper to use stop service";
       
   269         QObject::connect(mSignalMapper, SIGNAL(mapped(int)), this,
       
   270                 SLOT(stopService(int)));
       
   271         }
       
   272     qDebug() << "-PosLocationServices::listServices()";
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // PosLocationServices::updateList
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 void PosLocationServices::updateList()
       
   280     {
       
   281     qDebug() << "+PosLocationServices::updateList()";
       
   282     int result = 0;
       
   283     int oldChildCount = 0;
       
   284 
       
   285     //store the current state (expanded) of the group
       
   286     if (mGroup)
       
   287         {
       
   288         qDebug() << "Store the group info";
       
   289 
       
   290         oldChildCount = mGroup->childCount();
       
   291 
       
   292         // remove all connections
       
   293         int count = mSignalMapperList.count();
       
   294         qDebug() << "mSignalMapperList.count() = " << count;
       
   295 
       
   296         if (count > 0)
       
   297             {
       
   298             for (TInt i = 0; i < count; ++i)
       
   299                 {
       
   300                 QObject::disconnect(mSignalMapperList[i],
       
   301                         SIGNAL(signalmapped()), mSignalMapper, SLOT(map()));
       
   302                 }
       
   303             QObject::disconnect(mSignalMapper, SIGNAL(mapped(int)), this,
       
   304                     SLOT(stopService(int)));
       
   305             }
       
   306         mSignalMapperList.clear();
       
   307 
       
   308         qDebug() << " oldChildCount" << oldChildCount;
       
   309         if (oldChildCount > 0)
       
   310             {
       
   311             qDebug() << "Removing the old children";
       
   312             //mGroup->removeChildren(0, oldChildCount);
       
   313             //ToDo: Hack from HbDataForm team         
       
   314             for (int i = 0; i < oldChildCount; ++i)
       
   315                 {
       
   316                 qDebug() << "remove chile index " << i;
       
   317                 mGroup->removeChild(0);
       
   318                 }
       
   319             }
       
   320 
       
   321         }
       
   322     else
       
   323         {
       
   324         // this part of code is called when the services are added and 
       
   325         // Advanced view is in the background. Since there is no group
       
   326         // a new group has to be created to add the services.
       
   327         qDebug() << "Creating new group";
       
   328         mGroup = new HbDataFormModelItem(HbDataFormModelItem::GroupItem,
       
   329                 hbTrId("txt_loe_subtitle_background_positioning"), 0);
       
   330         mOperation->appendDataFormItem(mGroup);
       
   331         }
       
   332 
       
   333     qDebug() << "oldChildCount = " << oldChildCount;
       
   334     qDebug() << "mServiceList.count() before update = "
       
   335             << mServiceList.count();
       
   336 
       
   337     //clear the list and get the list of server present for positioning
       
   338     mServiceList.clear();
       
   339     if (mLocationServiceEngine)
       
   340         {
       
   341         result = mLocationServiceEngine->getLocationServices(mServiceList);
       
   342         }
       
   343 
       
   344     qDebug() << "PosLocationServices::listServices(): result" << result;
       
   345 
       
   346     //update the new list to the group
       
   347     if (result == 0)
       
   348         {
       
   349         listServices();
       
   350         }
       
   351     if (result == -1)
       
   352         {
       
   353         //remove the group
       
   354         mOperation->removeItem(mGroup);
       
   355         mGroup = NULL;
       
   356         }
       
   357 
       
   358     qDebug() << "-PosLocationServices::updateList()";
       
   359     }
       
   360 
       
   361 // ---------------------------------------------------------------------------
       
   362 // PosLocationServices::stopService
       
   363 // ---------------------------------------------------------------------------
       
   364 //
       
   365 void PosLocationServices::stopService(int serviceId)
       
   366     {
       
   367     qDebug() << "+PosLocationServices::stopService() service Id" << serviceId;
       
   368 
       
   369     mServiceId = serviceId;
       
   370 
       
   371     //Construct the display string
       
   372     QString displayString;
       
   373     displayString.append(QString(hbTrId("txt_loe_info_stop_service_1")).arg(
       
   374             mServiceList[serviceId].mServiceName));
       
   375 
       
   376     //create the message mStopQuery dialog
       
   377     //HbMessageBox * mStopQuery = new HbMessageBox();
       
   378 
       
   379     if (mStopQuery == 0)
       
   380         {
       
   381         mStopQuery = new HbMessageBox();
       
   382         mStopQuery->setTimeout(HbDialog::NoTimeout);
       
   383    
       
   384         //the secondry action that will come in the SK2 of the Dialog box
       
   385         mStopQuery->addAction(new HbAction(QString(hbTrId(
       
   386                 "txt_common_button_cancel"))));
       
   387 
       
   388         //To-Do: set the actual stop service icon after released in sdk 
       
   389         QIcon stopServiceIcon(":/resources/qgn_note_warning.svg");
       
   390         mStopQuery->setIcon(HbIcon(stopServiceIcon));
       
   391         }
       
   392 
       
   393     //set the mStopQuery text
       
   394     mStopQuery->setText(displayString);
       
   395     mStopQuery->open(this,SLOT(stopSession(HbAction*)));
       
   396 
       
   397     qDebug() << "-PosLocationServices::stopService()";
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // PosLocationServices::stopSession
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void PosLocationServices::stopSession(HbAction* action)
       
   405     {
       
   406     qDebug() << " + PosLocationServices::stopSession()";
       
   407     
       
   408     //if LSK is pressed
       
   409     if ((action == mStopQuery->actions().at(0)) && mLocationServiceEngine)
       
   410         {
       
   411         //stop the session
       
   412         mLocationServiceEngine->removeLocationService(
       
   413                 mServiceList[mServiceId].mServiceId);
       
   414         }
       
   415     qDebug() << " - PosLocationServices::stopSession()";
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------------------------
       
   419 // PosLocationServices::getUpdateIntervalString
       
   420 // ---------------------------------------------------------------------------
       
   421 //
       
   422 void PosLocationServices::getUpdateIntervalString(int updateInterval,
       
   423         QString& updateIntervalString)
       
   424     {
       
   425     qDebug()
       
   426             << "+PosLocationServices::getUpdateIntervalString(): updateInterval"
       
   427             << updateInterval;
       
   428 
       
   429     //To-Do: clarification needed from chelli
       
   430     if (updateInterval / KMinToSecs < 1)
       
   431         {
       
   432         //seconds
       
   433         QVariant var(updateInterval);
       
   434         updateIntervalString = QString(hbTrId(
       
   435                 "txt_loe_list_updates_every_l1_seconds")).arg(var.toString());
       
   436         }
       
   437     else if (updateInterval / KHourToSecs < 1)
       
   438         {
       
   439         //minutes
       
   440         int minutes = (updateInterval / KMinToSecs);
       
   441         int seconds = (updateInterval % KMinToSecs);
       
   442         QVariant varMinutes(minutes);
       
   443         QVariant varSeconds(seconds);
       
   444         updateIntervalString = QString(hbTrId(
       
   445                 "txt_loe_list_updates_every_l1_minutes_and_l2_sec")).arg(
       
   446                 varMinutes.toString()).arg(varSeconds.toString());
       
   447         }
       
   448     else if (updateInterval / (KHourToSecs * KOneDay) < 1)
       
   449         {
       
   450         //hour
       
   451         int hours = (updateInterval / KHourToSecs);
       
   452         int minutes = (updateInterval % KHourToSecs) / KMinToSecs;
       
   453         QVariant varhour(hours);
       
   454         QVariant varMinutes(minutes);
       
   455 
       
   456         updateIntervalString = QString(hbTrId(
       
   457                 "txt_loe_list_updates_every_l1_hours_and_l2_minut")).arg(
       
   458                 varhour.toString()).arg(varMinutes.toString());
       
   459         }
       
   460     else
       
   461         {
       
   462         //days
       
   463         int days = (updateInterval / (KHourToSecs * KOneDay));
       
   464         int hours = (updateInterval % (KHourToSecs * KOneDay)) / KMinToSecs;
       
   465         QVariant varDays(days);
       
   466         QVariant varHours(hours);
       
   467         updateIntervalString = QString(hbTrId(
       
   468                 "txt_loe_list_updates_every_l1_days_and_l2_hours")).arg(
       
   469                 varDays.toString()).arg(varHours.toString());
       
   470         }
       
   471 
       
   472     qDebug() << "-PosLocationServices::updateInterval()";
       
   473 
       
   474     }
       
   475 
       
   476 Q_EXPORT_PLUGIN2(poslocationservices, PosLocationServices)
       
   477 ;
       
   478