mtpdataproviders/mtpimagedp/mediasyncserver/src/cmediasyncobserver.cpp
changeset 51 64200268cac2
parent 50 965bb42340b2
child 52 866b4af7ffbe
equal deleted inserted replaced
50:965bb42340b2 51:64200268cac2
     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 //
       
    22 #include <mdequery.h>
       
    23 #include <mdeconstants.h>
       
    24 
       
    25 #include "cmediasyncdatabase.h"
       
    26 #include "cmediasyncobserver.h"
       
    27 
       
    28 __FLOG_STMT(_LIT8(KComponent,"MediaSyncObserver");)
       
    29 
       
    30 CMediaSyncObserver*  CMediaSyncObserver::NewL(CMediaSyncDatabase* aDb)
       
    31     {       
       
    32     CMediaSyncObserver* self = new (ELeave) CMediaSyncObserver(aDb);
       
    33     CleanupStack::PushL(self);
       
    34     self->ConstructL();
       
    35     CleanupStack::Pop(self);
       
    36     return self;
       
    37     }
       
    38 
       
    39 void CMediaSyncObserver::ConstructL()
       
    40     {
       
    41     __FLOG_OPEN(KMSSSubsystem, KComponent);
       
    42     __FLOG(_L8("CMediaSyncObserver::ConstructL - Entry"));
       
    43     
       
    44     iSessionWait = new (ELeave) CActiveSchedulerWait();
       
    45     iSession = CMdESession::NewL(*this);
       
    46     iSessionWait->Start();
       
    47     
       
    48     User::LeaveIfError(iMdeSessionError);    
       
    49     
       
    50 //    SubscribeForChangeNotificationL();
       
    51     
       
    52     __FLOG(_L8("CMediaSyncObserver::ConstructL - Exit"));
       
    53     }
       
    54 
       
    55 CMediaSyncObserver::CMediaSyncObserver(CMediaSyncDatabase* aDb)
       
    56     :iDb(aDb),
       
    57     iSubscribed(EFalse)
       
    58     {    
       
    59     }
       
    60 
       
    61 CMediaSyncObserver::~CMediaSyncObserver()
       
    62     {
       
    63     __FLOG(_L8("CMediaSyncObserver::~CMediaSyncObserver - Entry"));  
       
    64     
       
    65 //    TRAP_IGNORE(UnsubscribeForChangeNotificationL());
       
    66     
       
    67     delete iSession;
       
    68     delete iSessionWait;
       
    69 	
       
    70     __FLOG(_L8("CMediaSyncObserver::~CMediaSyncObserver - Exit"));
       
    71     __FLOG_CLOSE;
       
    72     }
       
    73 
       
    74 void CMediaSyncObserver::SubscribeForChangeNotificationL()
       
    75     {
       
    76     __FLOG(_L8("CMediaSyncObserver::SubscribeForChangeNotificationL - Entry"));
       
    77     
       
    78     if (!iSubscribed)
       
    79         {        
       
    80         CMdENamespaceDef& def = iSession->GetDefaultNamespaceDefL();
       
    81         CMdEObjectDef& imageObjDef = def.GetObjectDefL(MdeConstants::Image::KImageObject);
       
    82         
       
    83         // add observer        
       
    84         CMdELogicCondition* addCondition = CMdELogicCondition::NewLC(ELogicConditionOperatorAnd);          
       
    85         CMdEPropertyDef& itemTypePropDef = imageObjDef.GetPropertyDefL(MdeConstants::Object::KItemTypeProperty);       
       
    86         addCondition->AddPropertyConditionL(itemTypePropDef, ETextPropertyConditionCompareEndsWith, _L("jpeg"));                       
       
    87         iSession->AddObjectObserverL(*this, addCondition, ENotifyAdd);
       
    88         CleanupStack::Pop(addCondition);
       
    89         
       
    90         // modify observer
       
    91         CMdELogicCondition* modifyCondition = CMdELogicCondition::NewLC(ELogicConditionOperatorAnd);          
       
    92         CMdEPropertyDef& titlePropDef = imageObjDef.GetPropertyDefL(MdeConstants::Object::KTitleProperty);
       
    93         modifyCondition->AddPropertyConditionL(titlePropDef);
       
    94         iSession->AddObjectObserverL(*this, modifyCondition, ENotifyModify);
       
    95         CleanupStack::Pop(modifyCondition);
       
    96         
       
    97         // remove observer
       
    98         iSession->AddObjectObserverL(*this, NULL, ENotifyRemove);
       
    99         
       
   100         // present observer
       
   101         iSession->AddObjectPresentObserverL(*this);
       
   102                         
       
   103         iSubscribed = ETrue;               
       
   104         }
       
   105 
       
   106     __FLOG(_L8("CMediaSyncObserver::SubscribeForChangeNotificationL - Exit"));
       
   107     }
       
   108 
       
   109 void CMediaSyncObserver::UnsubscribeForChangeNotificationL()
       
   110     {
       
   111     __FLOG(_L8("CMediaSyncObserver::UnsubscribeForChangeNotificationL - Entry"));
       
   112     
       
   113     if (iSubscribed)
       
   114         {
       
   115         iSession->RemoveObjectObserverL(*this);//add observer
       
   116         iSession->RemoveObjectObserverL(*this);//modify observer
       
   117         iSession->RemoveObjectObserverL(*this);//remove observer
       
   118         iSession->RemoveObjectPresentObserverL(*this);
       
   119         iSubscribed = EFalse;
       
   120         }
       
   121     
       
   122     __FLOG(_L8("CMediaSyncObserver::UnsubscribeForChangeNotificationL - Exit"));
       
   123     }
       
   124 
       
   125 // From MMdESessionObserver
       
   126 void CMediaSyncObserver::HandleSessionOpened(CMdESession& /*aSession*/, TInt aError)
       
   127     {
       
   128     __FLOG(_L8("CMediaSyncObserver::HandleSessionOpened - Entry"));
       
   129     
       
   130     HandleSessionCallback(aError);
       
   131     
       
   132     __FLOG(_L8("CMediaSyncObserver::HandleSessionOpened - Exit"));
       
   133     }
       
   134 
       
   135 void CMediaSyncObserver::HandleSessionError(CMdESession& /*aSession*/, TInt aError)
       
   136     {
       
   137     __FLOG(_L8("CMediaSyncObserver::HandleSessionError - Entry"));
       
   138     
       
   139     HandleSessionCallback(aError);
       
   140     
       
   141     __FLOG(_L8("CMediaSyncObserver::HandleSessionError - Exit"));
       
   142     }
       
   143 
       
   144 void CMediaSyncObserver::HandleSessionCallback(TInt aError)
       
   145     {
       
   146     __ASSERT_DEBUG(iSessionWait, User::Invariant());
       
   147     iMdeSessionError = aError;    
       
   148     if (iSessionWait->IsStarted())
       
   149         {
       
   150         iSessionWait->AsyncStop();
       
   151         }
       
   152     }
       
   153 
       
   154 /*
       
   155  * After receiving object change notification, check if there is any dp subscribed right now.
       
   156  * if none, store change into database
       
   157  * if yes, check the type of file with subscribed providers, if there is any match, just forward
       
   158  * the change to that dp, if none, store change into database.
       
   159  */
       
   160 void CMediaSyncObserver::HandleObjectNotification(CMdESession& aSession,
       
   161                                             TObserverNotificationType aType,
       
   162                                             const RArray<TItemId>& aObjectIdArray)
       
   163     {
       
   164     TRAPD(err, HandleObjectNotificationL(aSession, aType, aObjectIdArray));
       
   165     
       
   166     if (err != KErrNone)
       
   167         {
       
   168         __FLOG(_L8("CMediaSyncObserver::HandleObjectNotification - Rollback database"));        
       
   169         iDb->Rollback();
       
   170         }
       
   171     }
       
   172 
       
   173 /*
       
   174  * L Function
       
   175  */
       
   176 void CMediaSyncObserver::HandleObjectNotificationL(CMdESession& /*aSession*/,
       
   177                                                    TObserverNotificationType aType,
       
   178                                                    const RArray<TItemId>& aObjectIdArray)
       
   179     {
       
   180     __FLOG(_L8("CMediaSyncObserver::HandleObjectNotificationL - Entry"));
       
   181     
       
   182     iDb->SaveNotificationsL(aObjectIdArray, aType, *iSession);
       
   183     
       
   184     __FLOG(_L8("CMediaSyncObserver::HandleObjectNotificationL - Exit"));
       
   185     }
       
   186 
       
   187 /*
       
   188  * Called to notify the observer that objects has been set
       
   189  * to present or not present state in the metadata engine database.
       
   190  */
       
   191 void CMediaSyncObserver::HandleObjectPresentNotification(CMdESession& aSession,
       
   192                                                         TBool aPresent, 
       
   193                                                         const RArray<TItemId>& aObjectIdArray)
       
   194     {
       
   195     TRAPD(err, HandleObjectPresentNotificationL(aSession, aPresent, aObjectIdArray));
       
   196     
       
   197     if (err != KErrNone)
       
   198         {
       
   199         __FLOG(_L8("CMediaSyncObserver::HandleObjectPresentNotification - Rollback database"));
       
   200         iDb->Rollback();
       
   201         }    
       
   202     }
       
   203 
       
   204 /*
       
   205  * L Function
       
   206  */
       
   207 void CMediaSyncObserver::HandleObjectPresentNotificationL(CMdESession& /*aSession*/,
       
   208                                                         TBool aPresent, 
       
   209                                                         const RArray<TItemId>& aObjectIdArray)
       
   210     {
       
   211     __FLOG(_L8("CMediaSyncObserver::HandleObjectPresentNotificationL - Entry"));
       
   212      
       
   213     iDb->SaveNotificationsL(aObjectIdArray, aPresent, *iSession);
       
   214        
       
   215     __FLOG(_L8("CMediaSyncObserver::HandleObjectPresentNotificationL - Exit"));
       
   216     }