phonebookengines/cntmaptileservice/src/cntmaptileservice.cpp
changeset 46 efe85016a067
parent 25 76a2435edfd4
child 47 7cbcb2896f0e
--- a/phonebookengines/cntmaptileservice/src/cntmaptileservice.cpp	Fri Jun 11 13:29:23 2010 +0300
+++ b/phonebookengines/cntmaptileservice/src/cntmaptileservice.cpp	Wed Jun 23 18:02:44 2010 +0300
@@ -18,7 +18,9 @@
 
 
 #include <centralrepository.h> 
-
+#include <locationservicedefines.h>
+#include <qvaluespacepublisher.h>
+#include <qvaluespacesubscriber.h>
 
 #include "cntmaptileservice.h"
 #include "cntmaptiledblookuptable.h"
@@ -29,9 +31,49 @@
 // Central Repository Key IDs
 const TInt KEnableLocationFeature           = 0x1;
 
+const char *CNT_MAPTILE_STATUS_RECEIVER = "/maptilestatuspublisher/name";
+const char *CNT_MAPTILE_STATUS_PUBLISHER = "/cntmaptilepublisher";
+
 
 // -----------------------------------------------------------------------------
-// MapTileService::isLocationFeatureEnabled()
+// CntMapTileService::CntMapTileService()
+// Default constructor
+// -----------------------------------------------------------------------------
+//
+CntMapTileService::CntMapTileService()
+{
+    //publisher
+    mPublisher = NULL;
+    
+    //subscriber
+    mSubscriber = new QValueSpaceSubscriber( CNT_MAPTILE_STATUS_RECEIVER );
+    //Connect for maptile status change key
+    QObject::connect(mSubscriber, SIGNAL(contentsChanged()), this, SLOT(setMaptileStatus()));
+
+}
+
+
+// -----------------------------------------------------------------------------
+// CntMapTileService::~CntMapTileService()
+// Destructor
+// -----------------------------------------------------------------------------
+//
+CntMapTileService::~CntMapTileService()
+{
+    if( mSubscriber )
+    {
+        delete mSubscriber;
+        mSubscriber = NULL;
+    }
+    
+    if( mPublisher )
+    {
+        delete mPublisher;
+        mPublisher = NULL;
+    }
+}
+// -----------------------------------------------------------------------------
+// CntMapTileService::isLocationFeatureEnabled()
 // Checks whether location feature is enabled or disabled
 // -----------------------------------------------------------------------------
 //
@@ -41,8 +83,9 @@
     bool enableLocationFeature = false;
     
     CRepository* centralRepository = NULL;  
-     
+
     TRAPD( err, centralRepository = CRepository::NewL( KUidMapTileInterface ) );
+
     if ( KErrNone == err )
     {
         TInt repValue;
@@ -64,54 +107,187 @@
 
 
 // -----------------------------------------------------------------------------
-// MapTileService::getMapTileImage()
+// CntMapTileService::getMapTileImage()
 // Gets the maptile image path associated with a contact.
 // -----------------------------------------------------------------------------
 //
-QString CntMapTileService::getMapTileImage( int contactId , ContactAddressType sourceType )    
+int CntMapTileService::getMapTileImage( int contactId , ContactAddressType sourceType, QString& imagePath )    
+{
+    
+    TLookupItem lookupItem;
+    
+    int addressCount = 0;
+    int maptileStatus = MapTileFetchingUnknownError;
+    
+    //Read the entry from maptile database
+    int error = readEntryFromMaptileDataBase( contactId, sourceType, lookupItem, addressCount );
+    
+    //if entry available returns the file path otherwise NULL. 
+    if ( KErrNone == error  )
+    {
+        maptileStatus = lookupItem.iFetchingStatus;
+        
+        if( maptileStatus == MapTileFetchingCompleted )
+        {
+            //Get the image path
+            QString imageFile((QChar*)lookupItem.iFilePath.Ptr(),
+                    lookupItem.iFilePath.Length());
+            imagePath = imageFile;
+        }
+        else if( maptileStatus == MapTileFetchingNetworkError ||
+                    maptileStatus == MapTileFetchingInProgress )
+        {
+            //Publish the contact id for maptile processing
+           	publishValue( contactId, sourceType, addressCount );
+        }
+
+    }
+    else if ( KErrNotFound == error )
+    {
+        //If entry is not found , it will be a newly added entry.
+        publishValue( contactId, sourceType, addressCount );
+        maptileStatus = MapTileFetchingInProgress;
+    }
+   
+    //Return the maptile status
+    return maptileStatus;
+}
+
+
+// -----------------------------------------------------------------------------
+// CntMapTileService::setMaptileStatus()
+// Emits the maptile status changed event to contact application
+// -----------------------------------------------------------------------------
+//
+void CntMapTileService::setMaptileStatus()
 {
-    //Image file to return;
-   
+    QString imagePath;
+    ContactAddressType addressType = AddressPreference;
+    
+    QStringList subPath = mSubscriber->subPaths();
+    QVariant value = mSubscriber->value(subPath.at(0));
+    
+    //Subscriber Protocol : [contactid-addresstype-maptilestatus]
+    QStringList text = value.toString().split("-");
+    int id = text.at(0).toInt();
+    int status = text.at(2).toInt();
+    
+    switch( text.at(1).toInt() )
+    {
+        case ESourceContactsPref:
+            addressType = AddressPreference;
+            break;
+        case ESourceContactsWork:
+            addressType = AddressWork;
+            break;        
+        case ESourceContactsHome:
+            addressType = AddressHome;
+            break;
+        default:
+            break;
+    }
+  
+    //Emit the maptile status signal 
+    int type = addressType;
+    if( mLastViewedContactId == id )
+    {
+        emit maptileFetchingStatusUpdate( mLastViewedContactId, type , status );
+    }
+}
+
+
+// -----------------------------------------------------------------------------
+// CntMapTileService::publishValue()
+// Publish the the contact id and address for which maptile to be processed.
+// -----------------------------------------------------------------------------
+//
+void CntMapTileService::publishValue( int id, ContactAddressType sourceType, int addressCount )
+{
+    
+    mLastViewedContactId = id;
+    int addressType = ESourceInvalid;
+    
+    switch( sourceType )
+    {
+        case AddressPreference:
+            addressType = ESourceContactsPref;
+            break;
+        case AddressWork:
+            addressType = ESourceContactsWork;
+            break;
+        case AddressHome:
+            addressType = ESourceContactsHome;
+            break;
+    }
+    
+    if ( !mPublisher )
+    {
+        /* Constructs a QValueSpacePublisher that publishes values under path /mypublisher*/
+        mPublisher = new QValueSpacePublisher( CNT_MAPTILE_STATUS_PUBLISHER, this);
+    }
+       
+    /* Publisher protocol [appid-addresstype-count] */
+    mContactEntryInfo.clear();
+    mContactEntryInfo.append( QVariant(id).toString());
+    mContactEntryInfo.append( QChar('-') );
+    mContactEntryInfo.append( QVariant(addressType).toString());
+    mContactEntryInfo.append( QChar('-') );
+    mContactEntryInfo.append( QVariant(addressCount).toString() );
+    
+    mPublisher->setValue("name", mContactEntryInfo.toAscii() );
+    mPublisher->sync();
+    
+}
+
+// -----------------------------------------------------------------------------
+// CntMapTileService::readEntryFromMaptileDataBase()
+// Read the entry from maptile database
+// -----------------------------------------------------------------------------
+//
+int CntMapTileService::readEntryFromMaptileDataBase( 
+            int id, ContactAddressType sourceType, TLookupItem& aLookupItem, int& aNoOfAddress )
+{
+    
     //Maptile database  instance
     CLookupMapTileDatabase* mapTileDatabase = NULL;
-   
+
     TRAPD( err, mapTileDatabase = CLookupMapTileDatabase::NewL(
             KMapTileLookupDatabaseName ) );
+
     if ( KErrNone == err )
     {
-        TLookupItem lookupItem;
-        lookupItem.icntUid = contactId;
+        int appId = id;
+        TRAP( err, aNoOfAddress = mapTileDatabase->FindNumberOfAddressL(appId) );
+       
+        if( err != KErrNone )
+        {
+            aNoOfAddress = 0;
+        }
+            
+        aLookupItem.iUid = id;
         switch( sourceType )
         {
            case AddressPreference:
-               lookupItem.iSource = ESourceContactsPref;
+               aLookupItem.iSource = ESourceContactsPref;
                break;
            case AddressWork:
-               lookupItem.iSource = ESourceContactsWork;
+               aLookupItem.iSource = ESourceContactsWork;
                break;
            case AddressHome:
-               lookupItem.iSource = ESourceContactsHome;
+               aLookupItem.iSource = ESourceContactsHome;
                break;
            default: 
                break;
         }
-       
-        TRAP( err , mapTileDatabase->FindEntryL( lookupItem ) );
-       
+   
+        TRAP( err , mapTileDatabase->FindEntryL( aLookupItem ) );
+        
         //delet the database instance
         delete mapTileDatabase;
-       
-        //if entry available returns the file path otherwise NULL. 
-        if ( KErrNone == err  )
-        {
-           //Get the image path
-            QString imageFile((QChar*)lookupItem.iFilePath.Ptr(),
-                    lookupItem.iFilePath.Length());
-            return imageFile;
-        }
+           
     }
     
-    return QString();
+    return err;
 }
 
 // End of file