mtpdataproviders/mtpimagedp/src/cmtpimagedpmdeobserver.cpp
changeset 0 d0791faffa3f
child 2 4843bb5893b6
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <mdesession.h> 
       
    22 #include <mdeconstants.h>
       
    23 #include <mdequery.h>
       
    24 
       
    25 #include <centralrepository.h>
       
    26 
       
    27 #include <mtp/mmtpdataproviderframework.h>
       
    28 #include <mtp/mmtpobjectmgr.h>
       
    29 #include <mtp/cmtpobjectmetadata.h>
       
    30 #include <mtp/tmtptypeevent.h>
       
    31 
       
    32 #include "mtpdebug.h"
       
    33 #include "mtpimagedpconst.h"
       
    34 #include "cmtpimagedpmdeobserver.h"
       
    35 #include "cmtpimagedp.h"
       
    36 #include "cmtpimagedpobjectpropertymgr.h"
       
    37 #include "mtpimagedputilits.h"
       
    38 
       
    39 __FLOG_STMT(_LIT8(KComponent, "CMTPImageDpMdeObserver");)
       
    40 
       
    41 CMTPImageDpMdeObserver* CMTPImageDpMdeObserver::NewL(MMTPDataProviderFramework& aFramework, CMTPImageDataProvider& aDataProvider)
       
    42     {
       
    43     CMTPImageDpMdeObserver* self = new (ELeave) CMTPImageDpMdeObserver(aFramework, aDataProvider);
       
    44     CleanupStack::PushL(self);
       
    45     self->ConstructL();
       
    46     CleanupStack::Pop(self);
       
    47     return self;
       
    48     }
       
    49 // --------------------------------------------------------------------------
       
    50 // Constructor
       
    51 // --------------------------------------------------------------------------
       
    52 //
       
    53 CMTPImageDpMdeObserver::CMTPImageDpMdeObserver(MMTPDataProviderFramework& aFramework, CMTPImageDataProvider& aDataProvider) :
       
    54     iFramework(aFramework),
       
    55     iDataProvider(aDataProvider),
       
    56     iSubscribed(EFalse)
       
    57     {
       
    58     }
       
    59 
       
    60 void CMTPImageDpMdeObserver::ConstructL()
       
    61     {
       
    62     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
    63     __FLOG(_L8("CMTPImageDpMdeObserver::ConstructL - Entry"));
       
    64     __FLOG(_L8("CMTPImageDpMdeObserver::ConstructL - Exit"));
       
    65     }
       
    66 
       
    67 CMTPImageDpMdeObserver::~CMTPImageDpMdeObserver()
       
    68     {
       
    69     __FLOG(_L8("CMTPImageDpMdeObserver::~CMTPImageDpMdeObserver - Entry"));   
       
    70     __FLOG(_L8("CMTPImageDpMdeObserver::~CMTPImageDpMdeObserver - Exit"));   
       
    71     __FLOG_CLOSE;
       
    72     }
       
    73 
       
    74 void CMTPImageDpMdeObserver::CreateMetadataL(const CMdEObject& aMdeObject, CMTPObjectMetaData& aMetaData)
       
    75     {
       
    76     const TDesC& uri = aMdeObject.Uri();
       
    77     TParsePtrC parse(uri);  
       
    78     TMTPFormatCode format = iDataProvider.FindFormatL(parse.Ext().Mid(1));
       
    79     TInt32 storageId = MTPImageDpUtilits::FindStorage(iFramework, uri);
       
    80     TUint32 parentHandle = MTPImageDpUtilits::FindParentHandleL(iFramework, iDataProvider, uri);
       
    81     
       
    82     aMetaData.SetUint(CMTPObjectMetaData::EDataProviderId, iFramework.DataProviderId());
       
    83     aMetaData.SetUint(CMTPObjectMetaData::EFormatCode, format);
       
    84     aMetaData.SetUint(CMTPObjectMetaData::EStorageId, storageId);
       
    85     aMetaData.SetDesCL(CMTPObjectMetaData::ESuid, uri);    
       
    86     aMetaData.SetUint(CMTPObjectMetaData::EParentHandle, parentHandle);
       
    87     aMetaData.SetDesCL(CMTPObjectMetaData::EName, parse.Name());
       
    88     
       
    89     //Increate the new pictures variable
       
    90     ++iNewPics;
       
    91     }
       
    92 
       
    93 void CMTPImageDpMdeObserver::ProcessMdeNotificationL(const RArray<TItemId>& aObjectIdArray, TObserverNotificationType /*aType*/)
       
    94     {    
       
    95     __FLOG_VA((_L8("CMTPImageDpMdeObserver::ProcessMdeNotificationL Start")));       
       
    96     
       
    97     //Because image dp only subscribe add notification from MdS, only the add notifictions can reach here
       
    98     //we ignore checking ENotifyAdd notification type to reduce complexity.    
       
    99     CMdESession& mdeSession = iDataProvider.PropertyMgr().MdeSession();
       
   100     CMdENamespaceDef& defaultNamespaceDef = mdeSession.GetDefaultNamespaceDefL();
       
   101     CMdEObjectDef& imageObjDef = defaultNamespaceDef.GetObjectDefL(MdeConstants::Image::KImageObject);    
       
   102                    
       
   103     TInt objectCount = aObjectIdArray.Count();
       
   104     for (TInt i(0); i < objectCount; ++i)
       
   105         {
       
   106         CMdEObject* mdeObject = mdeSession.GetObjectL(aObjectIdArray[i], imageObjDef);
       
   107         if (mdeObject == NULL)
       
   108             {
       
   109             continue;
       
   110             }
       
   111         CleanupStack::PushL(mdeObject);
       
   112         
       
   113         TUint32 handle = iFramework.ObjectMgr().HandleL(mdeObject->Uri());
       
   114         if (handle == KMTPHandleNone)
       
   115             {
       
   116             CMTPObjectMetaData* object = CMTPObjectMetaData::NewLC();
       
   117             CreateMetadataL(*mdeObject, *object);
       
   118             iFramework.ObjectMgr().InsertObjectL(*object);                
       
   119             __FLOG_VA((_L16("Addition - Handle:%u, ObjectId:%u, URI:%S"), object->Uint(CMTPObjectMetaData::EHandle), aObjectIdArray[i], &mdeObject->Uri()));
       
   120             
       
   121             TMTPTypeEvent event;
       
   122             event.SetUint16(TMTPTypeEvent::EEventCode, EMTPEventCodeObjectAdded);
       
   123             event.SetUint32(TMTPTypeEvent::EEventSessionID, KMTPSessionAll);
       
   124             event.SetUint32(TMTPTypeEvent::EEventTransactionID, KMTPTransactionIdNone);
       
   125           
       
   126             TUint32 handle = object->Uint(CMTPObjectMetaData::EHandle);            
       
   127             event.SetUint32(TMTPTypeEvent::EEventParameter1, handle);                    
       
   128             iFramework.SendEventL(event);
       
   129             __FLOG_VA((_L8("ProcessMdeNotificationL - EMTPEventCodeObjectAdd, object handle:%u"), handle));
       
   130             
       
   131             CleanupStack::PopAndDestroy(object);//object 
       
   132             }
       
   133         
       
   134         CleanupStack::PopAndDestroy(mdeObject);//mdeObject             
       
   135         }    
       
   136     
       
   137     __FLOG_VA((_L8("CMTPImageDpMdeObserver::ProcessMdeNotificationL - Exit")));
       
   138     }
       
   139 
       
   140 /*
       
   141  * After receiving object change notification, check if there is any dp subscribed right now.
       
   142  * if none, store change into database
       
   143  * if yes, check the type of file with subscribed providers, if there is any match, just forward
       
   144  * the change to that dp, if none, store change into database.
       
   145  */
       
   146 void CMTPImageDpMdeObserver::HandleObjectNotification(CMdESession& aSession,
       
   147                                             TObserverNotificationType aType,
       
   148                                             const RArray<TItemId>& aObjectIdArray)
       
   149     {
       
   150     TRAP_IGNORE(HandleObjectNotificationL(aSession, aType, aObjectIdArray));
       
   151     }
       
   152 
       
   153 /*
       
   154  * L Function
       
   155  */
       
   156 void CMTPImageDpMdeObserver::HandleObjectNotificationL(CMdESession& /*aSession*/,
       
   157                                                     TObserverNotificationType aType,
       
   158                                                     const RArray<TItemId>& aObjectIdArray)
       
   159     {   
       
   160     __FLOG_VA((_L8("CMTPImageDpMdeObserver::HandleObjectNotificationL - changeType = %d"), aType));   
       
   161    
       
   162     ProcessMdeNotificationL(aObjectIdArray, aType);
       
   163 
       
   164     //Update RProperty to notify the subscribers.
       
   165     if (iNewPics > 0)
       
   166         {
       
   167         __FLOG_1(_L16("CMTPImageDpMdeObserver::CMTPImageDpMdeObserver - New Pics: %u"), iNewPics);
       
   168         MTPImageDpUtilits::UpdateNewPicturesValue(iDataProvider, iNewPics, ETrue);
       
   169         iNewPics = 0;
       
   170         }
       
   171     
       
   172     __FLOG(_L8("CMTPImageDpMdeObserver::CMTPImageDpMdeObserver - Exit"));
       
   173     }
       
   174 
       
   175 /*
       
   176  * Subscribe MdS notifications
       
   177  */
       
   178 void CMTPImageDpMdeObserver::SubscribeForChangeNotificationL()
       
   179     {    
       
   180     __FLOG_VA((_L8("CMTPImageDpMdeObserver::SubscribeForChangeNotificationL"))); 
       
   181 
       
   182     if (!iSubscribed)
       
   183         {
       
   184         CMdESession& mdeSession = iDataProvider.PropertyMgr().MdeSession();    
       
   185         
       
   186         CMdENamespaceDef& def = mdeSession.GetDefaultNamespaceDefL();
       
   187         CMdEObjectDef& imageObjDef = def.GetObjectDefL(MdeConstants::Image::KImageObject);
       
   188         
       
   189         //Only subscribe add observer        
       
   190         CMdELogicCondition* addCondition = CMdELogicCondition::NewLC(ELogicConditionOperatorAnd);          
       
   191         CMdEPropertyDef& itemTypePropDef = imageObjDef.GetPropertyDefL(MdeConstants::Object::KItemTypeProperty);       
       
   192         addCondition->AddPropertyConditionL(itemTypePropDef, ETextPropertyConditionCompareEndsWith, _L("jpeg"));                       
       
   193         mdeSession.AddObjectObserverL(*this, addCondition, ENotifyAdd);
       
   194         CleanupStack::Pop(addCondition);
       
   195         
       
   196         iSubscribed = ETrue;
       
   197         }
       
   198     
       
   199     __FLOG(_L8("CMTPImageDpMdeObserver::SubscribeForChangeNotificationL - Exit")); 
       
   200     }
       
   201 
       
   202 /*
       
   203  * Unsubscribe MdS notifications
       
   204  */
       
   205 void CMTPImageDpMdeObserver::UnsubscribeForChangeNotificationL()
       
   206     {
       
   207     __FLOG_VA((_L8("CMTPImageDpMdeObserver::UnsubscribeForChangeNotificationL"))); 
       
   208     
       
   209     if (iSubscribed)
       
   210         {
       
   211         CMdESession& mdeSession = iDataProvider.PropertyMgr().MdeSession();  
       
   212         
       
   213         mdeSession.RemoveObjectObserverL(*this);//add observer
       
   214         iSubscribed = EFalse;
       
   215         }
       
   216     
       
   217     __FLOG(_L8("CMTPImageDpMdeObserver::UnsubscribeForChangeNotificationL - Exit")); 
       
   218     }