locationdataharvester/maptileservice/src/maptileservice.cpp
branchRCL_3
changeset 17 1fc85118c3ae
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
       
     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 *     Maptile service implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <centralrepository.h> 
       
    21 #include <locationservicedefines.h>
       
    22 #include <qvaluespacepublisher.h>
       
    23 #include <qvaluespacesubscriber.h>
       
    24 #include <qfile.h>
       
    25 #include "mylocationlogger.h"
       
    26 
       
    27 #include "maptileservice.h"
       
    28 #include "maptiledblookuptable.h"
       
    29 #include <QVariant>
       
    30 // CONSTANTS
       
    31 // Maptile interface uid
       
    32 const TUid KUidMapTileInterface = { 0x2002E6E8 };
       
    33 
       
    34 // Central Repository Key IDs
       
    35 const TInt KEnableLocationFeature  = 0x1;
       
    36 
       
    37 const char *MAPTILE_STATUS_RECEIVER = "/maptilestatuspublisher/name";
       
    38 const char *MAPTILE_STATUS_PUBLISHER = "/maptilecontactpublisher";
       
    39 const char *MAPTILE_STATUS_CALENDAR_PUBLISHER = "/maptilecalpublisher";
       
    40 
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // MapTileService::MapTileService()
       
    44 // Default constructor
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 MapTileService::MapTileService():mCalPublisher(0)
       
    48 {
       
    49    /* Constructs a QValueSpacePublisher that publishes values under path /mypublisher*/
       
    50     mPublisher = NULL;    
       
    51     mSubscriber = new QValueSpaceSubscriber( MAPTILE_STATUS_RECEIVER );
       
    52     //Connect for maptile status change key
       
    53     QObject::connect(mSubscriber, SIGNAL(contentsChanged()), this, SLOT(setMaptileStatus()));
       
    54     
       
    55 }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // MapTileService::~MapTileService()
       
    59 // Destructor
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 MapTileService::~MapTileService()
       
    63 {
       
    64     if( mSubscriber )
       
    65     {
       
    66         delete mSubscriber;
       
    67         mSubscriber = NULL;
       
    68     }
       
    69     
       
    70     if( mPublisher )
       
    71     {
       
    72         delete mPublisher;
       
    73         mPublisher = NULL;
       
    74     }
       
    75 	if( mCalPublisher )
       
    76 	{
       
    77 	    delete mCalPublisher;
       
    78 		mCalPublisher = NULL;
       
    79 	}
       
    80 }
       
    81 // -----------------------------------------------------------------------------
       
    82 // MapTileService::isLocationFeatureEnabled()
       
    83 // Checks whether location feature is enabled or disabled
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 bool MapTileService::isLocationFeatureEnabled(AppType appType)
       
    87 {    
       
    88     //Create the centrep with uid 0x2002C3A8
       
    89     bool enableLocationFeature = false;
       
    90     
       
    91     CRepository* centralRepository = NULL;  
       
    92      
       
    93     TRAPD( err, centralRepository = CRepository::NewL( KUidMapTileInterface ) );
       
    94     if ( KErrNone == err )
       
    95     {
       
    96         TInt repValue;
       
    97       
       
    98         //Get the Location feature flag
       
    99         int ret=0;
       
   100         if ( appType == AppTypeContacts )
       
   101         {
       
   102             ret = centralRepository->Get( KEnableLocationFeature , repValue );
       
   103         }
       
   104         else if ( appType == AppTypeCalendar )
       
   105         {
       
   106              ret = centralRepository->Get( KEnableLocationFeature , repValue );
       
   107         }
       
   108       
       
   109         if ( ret == KErrNone && repValue == 1 )
       
   110         {
       
   111             enableLocationFeature  = true;
       
   112         }
       
   113            
       
   114          delete centralRepository;
       
   115     }
       
   116            
       
   117     return   enableLocationFeature;
       
   118     
       
   119 }
       
   120 
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // MapTileService::getMapTileImage()
       
   124 // Gets the maptile image path associated with a contact.
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 int MapTileService::getMapTileImage( int id, AddressType sourceType, 
       
   128              QString& imagePath, Qt::Orientations orientation )    
       
   129 {
       
   130     __TRACE_CALLSTACK;
       
   131     
       
   132     TLookupItem lookupItem;
       
   133     
       
   134     int addressCount = 0;  
       
   135     int maptileStatus = MapTileFetchingUnknownError;
       
   136 
       
   137     int error = readEntryFromMaptileDataBase( id, sourceType, lookupItem, addressCount );
       
   138         
       
   139     //if entry available returns the file path otherwise NULL. 
       
   140     if ( KErrNone == error  )
       
   141     {
       
   142      
       
   143         maptileStatus = lookupItem.iFetchingStatus;
       
   144         MYLOCLOGSTRING1("getMapTileImage maptileStatus  - %d ", maptileStatus );
       
   145         
       
   146         if( maptileStatus == MapTileFetchingCompleted )
       
   147         {
       
   148             //Get the image path
       
   149 	          QString imageFile((QChar*)lookupItem.iFilePath.Ptr(),
       
   150 	                    lookupItem.iFilePath.Length());
       
   151 	          imagePath = imageFile;
       
   152 	        
       
   153 	          if( orientation == Qt::Vertical )
       
   154 	          {
       
   155 	              imagePath.append( MAPTILE_IMAGE_PORTRAIT ); 
       
   156 	          }
       
   157 	          else
       
   158 	          {
       
   159 	              if( sourceType == AddressPlain )
       
   160 	              {
       
   161 	                  imagePath.append( MAPTILE_IMAGE_CALENDAR );
       
   162 	              }
       
   163 	              else if ( sourceType == AddressHome || 
       
   164 	                   sourceType == AddressPreference || sourceType == AddressWork )
       
   165 	              {
       
   166 	                  imagePath.append( MAPTILE_IMAGE_CONTACT );
       
   167 	              }
       
   168 	              imagePath.append( MAPTILE_IMAGE_LANDSCAPE );
       
   169 	          }
       
   170 	          
       
   171 	          //Check if file exist
       
   172 	          if( !QFile::exists( imagePath ) )
       
   173 	          {
       
   174 	              imagePath.clear();
       
   175 	           		maptileStatus = MapTileFetchingUnknownError;
       
   176 	          }
       
   177 	      }
       
   178         else if( maptileStatus == MapTileFetchingNetworkError ||
       
   179                     maptileStatus == MapTileFetchingInProgress )
       
   180         {
       
   181             switch(sourceType)
       
   182             {           
       
   183                 case AddressHome:            
       
   184                 case AddressPreference:           
       
   185                 case AddressWork:
       
   186                 {
       
   187                     publishValue( id, sourceType, addressCount );
       
   188                     break;
       
   189                 }
       
   190                 
       
   191                 case AddressPlain:
       
   192                 {
       
   193                     publishCalEntry( id );
       
   194                     break;
       
   195                 }
       
   196                 default:
       
   197                     break;
       
   198             };
       
   199            		  
       
   200         }
       
   201                     
       
   202 
       
   203     }
       
   204     else if ( KErrNotFound == error )
       
   205     {
       
   206         MYLOCLOGSTRING("getMapTileImage entry not found, return MapTileFetchingInProgress" );
       
   207         //If entry is not found , it will be a newly added entry.       
       
   208         switch (sourceType) {
       
   209             case AddressHome:
       
   210             case AddressPreference:
       
   211             case AddressWork:
       
   212             {
       
   213                 publishValue(id, sourceType, addressCount);
       
   214                 break;
       
   215             }
       
   216     
       
   217             case AddressPlain:
       
   218             {
       
   219                 publishCalEntry(id);
       
   220                 break;
       
   221             }
       
   222             default:
       
   223                 break;
       
   224         };
       
   225         maptileStatus = MapTileFetchingInProgress;
       
   226     }
       
   227  
       
   228     //Return the maptile status
       
   229     MYLOCLOGSTRING("getMapTileImage  returns MapTileFetchingUnknownError" );
       
   230     return maptileStatus;
       
   231 
       
   232 }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // MapTileService::setMaptileStatus()
       
   236 // Gets call back from location engine , after maptile fetching done ,
       
   237 // to set status and emit signal . 
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 void MapTileService::setMaptileStatus()
       
   241 {
       
   242     __TRACE_CALLSTACK;
       
   243     QString imagePath;
       
   244     AddressType addressType = AddressUnknown;   
       
   245     QStringList subPath = mSubscriber->subPaths();
       
   246     QVariant value = mSubscriber->value(subPath.at(0));
       
   247       
       
   248     //Subscriber Protocol : [appid-addresstype-maptilestatus]
       
   249     QStringList text = value.toString().split("-");
       
   250     
       
   251     int id = text.at(0).toInt();
       
   252     int status = text.at(2).toInt();
       
   253     
       
   254     switch( text.at(1).toInt() )
       
   255     {
       
   256         case ESourceContactsPref:
       
   257             addressType = AddressPreference;
       
   258             break;
       
   259         case ESourceContactsWork:
       
   260             addressType = AddressWork;
       
   261             break;        
       
   262         case ESourceContactsHome:
       
   263             addressType = AddressHome;
       
   264             break;
       
   265         case ESourceCalendar:           
       
   266             addressType = AddressPlain;
       
   267             break;
       
   268         default:
       
   269             break;
       
   270     }
       
   271             
       
   272     //Emit the maptile status signal 
       
   273     int type = addressType;
       
   274     MYLOCLOGSTRING1("setMaptileStatus() status  - %d ", addressType );
       
   275     if( mLastViewedEntryId == id )
       
   276     {
       
   277         emit maptileFetchingStatusUpdate( mLastViewedEntryId, type , status );
       
   278 	}
       
   279   
       
   280 }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // MapTileService::publishValue()
       
   284 // publish the contact entry 
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 void MapTileService::publishValue( int id, AddressType sourceType, int addressCount )
       
   288 {
       
   289     __TRACE_CALLSTACK;
       
   290     mLastViewedEntryId = id;
       
   291     
       
   292 	int addressType = ESourceInvalid;    
       
   293     switch( sourceType )
       
   294     {
       
   295         case AddressPreference:
       
   296             addressType = ESourceContactsPref;
       
   297             break;
       
   298         case AddressWork:
       
   299             addressType = ESourceContactsWork;
       
   300             break;
       
   301         case AddressHome:
       
   302             addressType = ESourceContactsHome;
       
   303             break;
       
   304     }
       
   305     
       
   306     if ( !mPublisher )
       
   307     {
       
   308         /* Constructs a QValueSpacePublisher that publishes values under path /mypublisher*/
       
   309         mPublisher = new QValueSpacePublisher( MAPTILE_STATUS_PUBLISHER, this);
       
   310     }
       
   311    
       
   312     
       
   313     /* Publisher protocol [appid-addresstype-count] */
       
   314     QString addressId;
       
   315     addressId.append( QVariant(id).toString());
       
   316     addressId.append( QChar('-') );
       
   317     addressId.append( QVariant(addressType).toString());
       
   318     addressId.append( QChar('-') );
       
   319     addressId.append( QVariant(addressCount).toString() );
       
   320     mPublisher->setValue("name", addressId.toAscii() );
       
   321     mPublisher->sync();
       
   322     
       
   323 }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // MapTileService::publishValue()
       
   327 // publish the calendar entry 
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void MapTileService::publishCalEntry( int id)
       
   331 {
       
   332     __TRACE_CALLSTACK;
       
   333     mLastViewedEntryId = id;       
       
   334     if ( NULL ==mCalPublisher )
       
   335     {
       
   336         /* Constructs a QValueSpacePublisher that publishes values under path /mypublisher*/
       
   337         mCalPublisher = new QValueSpacePublisher( MAPTILE_STATUS_CALENDAR_PUBLISHER, this);
       
   338     }    
       
   339     mCalPublisher->setValue("name", id);
       
   340     mCalPublisher->sync();
       
   341 }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // MapTileService::readEntryFromMaptileDataBase()
       
   345 // Read all entries from database
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 int MapTileService::readEntryFromMaptileDataBase( 
       
   349             int id, AddressType sourceType, TLookupItem& aLookupItem, int& aNoOfAddress )
       
   350 {
       
   351     
       
   352     //Maptile database  instance
       
   353     CLookupMapTileDatabase* mapTileDatabase = NULL;
       
   354    
       
   355     TRAPD( err, mapTileDatabase = CLookupMapTileDatabase::NewL(
       
   356             KMapTileLookupDatabaseName ) );
       
   357     
       
   358     if ( KErrNone == err )
       
   359     {     
       
   360         TRAP( err,aNoOfAddress = mapTileDatabase->FindNumberOfAddressL(id) );
       
   361         MYLOCLOGSTRING1("no of address  FindNumberOfAddressL returns - %d ", err );     
       
   362        if ( KErrNone == err )
       
   363        {
       
   364             aLookupItem.iUid = id;
       
   365             switch (sourceType)
       
   366             {
       
   367                 case AddressPlain:
       
   368                     aLookupItem.iSource = ESourceCalendar;
       
   369                     break;
       
   370                 case AddressPreference:
       
   371                     aLookupItem.iSource = ESourceContactsPref;
       
   372                     break;
       
   373                 case AddressWork:
       
   374                     aLookupItem.iSource = ESourceContactsWork;
       
   375                     break;
       
   376                 case AddressHome:
       
   377                     aLookupItem.iSource = ESourceContactsHome;
       
   378                     break;
       
   379                 default:
       
   380                     break;
       
   381             }
       
   382 
       
   383             TRAP( err , mapTileDatabase->FindEntryL( aLookupItem ) );
       
   384 
       
   385             MYLOCLOGSTRING1("getMapTileImage FindEntryL returns - %d ", err );
       
   386         }
       
   387         //delet the database instance
       
   388         delete mapTileDatabase;
       
   389            
       
   390     }
       
   391     
       
   392     return err;
       
   393 }
       
   394 // End of file