videocollection/mpxmyvideoscollection/src/vcxmyvideosmdsdb.cpp
changeset 37 4eb2df7f7cbe
parent 36 8aed59de29f9
child 38 ff53afa8ad05
--- a/videocollection/mpxmyvideoscollection/src/vcxmyvideosmdsdb.cpp	Fri Apr 16 18:13:14 2010 +0300
+++ b/videocollection/mpxmyvideoscollection/src/vcxmyvideosmdsdb.cpp	Fri Apr 30 09:52:11 2010 +0300
@@ -65,6 +65,100 @@
 
 
 // ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::NewL()
+// ---------------------------------------------------------------------------
+//
+CVcxMdsShutdownMonitor* CVcxMdsShutdownMonitor::NewL( MVcxMdsShutdownMonitorObserver& aObserver,
+                                                const TUid& aKeyCategory,
+                                                const TInt aPropertyKey,
+                                                TBool aDefineKey)
+    {
+    CVcxMdsShutdownMonitor* self = new( ELeave )CVcxMdsShutdownMonitor( aObserver, 
+                                                                  aKeyCategory,
+                                                                  aPropertyKey,
+                                                                  aDefineKey);
+    CleanupStack::PushL( self );
+    self->ConstructL();
+    CleanupStack::Pop( self );
+    return self;
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::CVcxMdsShutdownMonitor()
+// ---------------------------------------------------------------------------
+//
+CVcxMdsShutdownMonitor::CVcxMdsShutdownMonitor( MVcxMdsShutdownMonitorObserver& aObserver,
+                                          const TUid& aKeyCategory,
+                                          const TInt aPropertyKey,
+                                          TBool aDefineKey)
+    : CActive( CActive::EPriorityStandard ), iObserver( aObserver ),
+      iKeyCategory( aKeyCategory ), iPropertyKey(aPropertyKey), iDefineKey( aDefineKey )
+    {   
+    CActiveScheduler::Add( this );
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::ConstructL()
+// ---------------------------------------------------------------------------
+//
+void CVcxMdsShutdownMonitor::ConstructL()
+    { 
+    // define P&S property types
+    if ( iDefineKey )
+        {
+        RProperty::Define( iKeyCategory, iPropertyKey,
+                          RProperty::EInt, KAllowAllPolicy, KPowerMgmtPolicy );
+        }
+    
+    // attach to the property
+    TInt err = iProperty.Attach( iKeyCategory, iPropertyKey,EOwnerThread );
+    User::LeaveIfError( err );
+    
+    // wait for the previously attached property to be updated
+    iProperty.Subscribe( iStatus );
+    SetActive();
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::~CVcxMdsShutdownMonitor()
+// ---------------------------------------------------------------------------
+//
+CVcxMdsShutdownMonitor::~CVcxMdsShutdownMonitor()
+    {
+    Cancel();
+    iProperty.Close();
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::RunL()
+// ---------------------------------------------------------------------------
+//
+void CVcxMdsShutdownMonitor::RunL()
+    {
+    // resubscribe before processing new value to prevent missing updates
+    iProperty.Subscribe( iStatus );
+    SetActive();
+    
+    // retrieve the value
+    TInt value = 0;
+    TInt err = iProperty.Get( value );
+    MPX_DEBUG2("CVcxMyVideosMdsDb::RunL(): iProperty.Get(value); returns %d", err);
+    
+    User::LeaveIfError( err );
+
+    iObserver.ShutdownNotification( value );    
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMdsShutdownMonitor::DoCancel()
+// ---------------------------------------------------------------------------
+//
+void CVcxMdsShutdownMonitor::DoCancel()
+    {
+    iProperty.Cancel();
+    }
+
+// ---------------------------------------------------------------------------
 // ---------------------------------------------------------------------------
 //
 CVcxMyVideosMdsDb::CVcxMyVideosMdsDb( MVcxMyVideosMdsDbObserver* aObserver,
@@ -86,8 +180,6 @@
     
     OpenMdsSessionL();
 
-    //  Get the schema definitions
-    GetSchemaDefinitionsL();
         
     iAlbums = CVcxMyVideosMdsAlbums::NewL( *this, iAlbumsObserver );
     
@@ -95,6 +187,9 @@
     iAsyncHandleQueryCompleteCaller = new (ELeave) CAsyncCallBack( callBack,
             CActive::EPriorityStandard );
 
+    iMdsShutdownMonitor = CVcxMdsShutdownMonitor::NewL(
+            *this, KHarvesterPSShutdown, KMdSShutdown, EFalse );
+
     MPX_DEBUG1( "CVcxMyVideosMdsDb::ConstructL exit" );
     }
 
@@ -106,35 +201,47 @@
     {
     MPX_DEBUG1( "CVcxMyVideosMdsDb::OpenMdsSessionL() start" );
 
-    iMdsError = KErrNone;
+    iMdsSessionError = KErrNone;
 
+    delete iMdsSession;
+    iMdsSession = NULL;
     iMdsSession = CMdESession::NewL( *this );
     if ( !iMdsSession )
         {
-        //  Failed to create session, leave
-        User::Leave( iMdsError );
+        User::Leave( KErrGeneral );
         }
     
-    if ( iMdsError != KErrNone )
-        {
-        MPX_DEBUG2("Failed to create session to MDS: %d", iMdsError);
-        User::LeaveIfError( iMdsError );
-        }
-
     //  Wait until session opened
     iActiveSchedulerWait->Start();    
-    MPX_DEBUG1( "CVcxMyVideosMdsDb::ConstructL iActiveSchedulerWait->Start done" );
+    MPX_DEBUG1( "CVcxMyVideosMdsDb:: iActiveSchedulerWait->Start done" );
 
-    MPX_DEBUG1( "CVcxMyVideosMdsDb::OpenMdsSessionL Adding observers" );
+    if ( iMdsSessionError == KErrNone )
+        {
+        MPX_DEBUG1( "CVcxMyVideosMdsDb:: session opened ok, adding observers" );
 
-    // We order all object notifications. If we set video condition, then we wont
-    // receive remove notifications at all (mds feature). Extra notifications
-    // do not bother us much since we try to fetch the item from the db
-    // after the add notification anyways, and then we use video condition.
-    // Eventually extra events are ignored.    
-    iMdsSession->AddObjectObserverL( *this, NULL );
+        // We order all object notifications. If we set video condition, then we wont
+        // receive remove notifications at all (mds feature). Extra notifications
+        // do not bother us much since we try to fetch the item from the db
+        // after the add notification anyways, and then we use video condition.
+        // Eventually extra events are ignored.    
+        iMdsSession->AddObjectObserverL( *this, NULL );
     
-    iMdsSession->AddObjectPresentObserverL( *this );
+        iMdsSession->AddObjectPresentObserverL( *this );
+        
+        GetSchemaDefinitionsL();
+        
+        if ( iAlbums )
+            {
+            iAlbums->GetSchemaDefinitionsL();
+            }
+        }
+    else
+        {
+        MPX_DEBUG2( "CVcxMyVideosMdsDb:: session opening failed: %d", iMdsSessionError );
+        iMdsSessionError = KErrGeneral; // this ensures that next time when mds is tried to use, it tries to open session again
+        delete iMdsSession;
+        iMdsSession = NULL;        
+        }
 
     MPX_DEBUG1( "CVcxMyVideosMdsDb::OpenMdsSessionL() exit" );
     }
@@ -193,22 +300,18 @@
 //
 CVcxMyVideosMdsDb::~CVcxMyVideosMdsDb()
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::~CVcxMyVideosMdsDb()" );
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::~CVcxMyVideosMdsDb() start" );
 
     Cancel( EGetVideoList );
 
     delete iAlbums;
-    delete iCmdQueue;    
-
-    if ( iMdsSession )
-        {
-        TRAP_IGNORE( iMdsSession->RemoveObjectObserverL( *this ) );        
-        }
-
+    delete iCmdQueue;
     delete iVideoQuery;
     delete iMdsSession;
     delete iActiveSchedulerWait;
     delete iAsyncHandleQueryCompleteCaller;
+    delete iMdsShutdownMonitor;
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::~CVcxMyVideosMdsDb() exit" );
     }
 
 // ---------------------------------------------------------------------------
@@ -246,15 +349,9 @@
         CMPXMedia& aVideo, 
         TUint32& aMdsId )
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::AddVideoL" );
-
-    if ( !iMdsSession )
-        {
-        MPX_DEBUG2("CVcxMyVideosMdsDb:: no mds session(%d), leaving", iMdsError);
-        User::Leave( iMdsError );
-        }
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::AddVideoL() start" );
          
-    CMdEObject* object = iMdsSession->NewObjectLC(
+    CMdEObject* object = MdsSessionL().NewObjectLC(
             *iVideoObjectDef, aVideo.ValueText( KMPXMediaGeneralUri ) ); // 1->
 
     // Value from aVideo is taken in use in Media2ObjectL if aVideo contains creation date
@@ -282,11 +379,11 @@
         }
 
     Media2ObjectL( aVideo, *object );
-    TRAPD( err, aMdsId = iMdsSession->AddObjectL( *object ) );
+    TRAPD( err, aMdsId = MdsSessionL().AddObjectL( *object ) );
 
     if ( err != KErrNone )
         {
-        MPX_DEBUG2( "CVcxMyVideosMdsDb:: iMdsSession->AddObjectL leaved with error: %d", err );
+        MPX_DEBUG2( "CVcxMyVideosMdsDb:: MdsSessionL().AddObjectL leaved with error: %d", err );
         User::Leave( err );
         }
         
@@ -302,6 +399,7 @@
 
     CleanupStack::PopAndDestroy( object ); // <-1
 
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::AddVideoL() exit" );
     }
 
 // ---------------------------------------------------------------------------
@@ -310,20 +408,14 @@
 //
 TInt CVcxMyVideosMdsDb::RemoveVideo( TUint32 aMdsId )
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::RemoveVideoL" );
-
-    if ( !iMdsSession )
-        {
-        MPX_DEBUG2("CVcxMyVideosMdsDb:: no mds session, returning %d", iMdsError);
-        return iMdsError;
-        }
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::RemoveVideoL() start" );
 
     TInt retValue( KErrNone );
     TItemId id( 0 ); //init to avoid warning
 
     MPX_DEBUG2( "CVcxMyVideosMdsDb:: removing object %d", aMdsId );
     
-    TRAPD( err,  id = iMdsSession->RemoveObjectL( aMdsId ) );
+    TRAPD( err,  id = MdsSessionL().RemoveObjectL( aMdsId ) );
 
     if ( err == KErrNone )
         {
@@ -339,10 +431,11 @@
         }
     else
         {
-        MPX_DEBUG2( "CVcxMyVideosMdsDb:: iMdsSession->RemoveObjectL left: %d", err );
+        MPX_DEBUG2( "CVcxMyVideosMdsDb:: MdsSessionL().RemoveObjectL left: %d", err );
         retValue = err;        
         }
         
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::RemoveVideoL() exit" );        
     return retValue;
     }
 
@@ -352,20 +445,14 @@
 //
 void CVcxMyVideosMdsDb::UpdateVideoL( CMPXMedia& aVideo )
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::UpdateVideoL" );
-
-    if ( !iMdsSession )
-        {
-        MPX_DEBUG2("CVcxMyVideosMdsDb:: no mds session(%d), leaving", iMdsError);
-        User::Leave( iMdsError );
-        }
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::UpdateVideoL() start" );
 
     TMPXItemId mpxId = aVideo.ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
 
     MPX_DEBUG2("CVcxMyVideosMdsDb::UpdateVideoL updating object %d ", mpxId.iId1);
     
     CMdEObject* object =
-            iMdsSession->OpenObjectL( mpxId.iId1, *iVideoObjectDef );
+            MdsSessionL().OpenObjectL( mpxId.iId1, *iVideoObjectDef );
     if ( !object )
         {
         // No object with this ID was found!
@@ -382,7 +469,7 @@
             
             Media2ObjectL( aVideo, *object );
             
-            iMdsSession->CommitObjectL(*object);
+            MdsSessionL().CommitObjectL(*object);
 
             CleanupStack::PopAndDestroy(object);
             }
@@ -394,6 +481,7 @@
             User::Leave( KErrInUse );
             }
         }
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::UpdateVideoL() exit" );
     }
 
 // ---------------------------------------------------------------------------
@@ -403,7 +491,7 @@
 void CVcxMyVideosMdsDb::CreateVideoListL( TVcxMyVideosSortingOrder aSortingOrder,
         TBool aAscending, TBool aFullDetails, CMPXMedia*& aVideoList )
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::CreateVideoListL" );
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::CreateVideoListL() start" );
     
     CVcxMyVideosMdsCmdGetVideoList* cmd = new (ELeave) CVcxMyVideosMdsCmdGetVideoList;
     CleanupStack::PushL( cmd ); // 1->
@@ -425,12 +513,6 @@
         TBool aAscending, TBool aFullDetails, CMPXMedia*& aVideoList )
     {
     MPX_FUNC( "CVcxMyVideosMdsDb::DoCreateVideoListL" );
-
-    if ( !iMdsSession )
-        {
-        MPX_DEBUG2("CVcxMyVideosMdsDb:: no mds session(%d), leaving", iMdsError);
-        User::Leave( iMdsError );
-        }
         
     if ( iVideoListFetchingIsOngoing )
         {
@@ -448,7 +530,7 @@
     delete iVideoQuery;
     iVideoQuery = NULL;
     
-    iVideoQuery = iMdsSession->NewObjectQueryL(
+    iVideoQuery = MdsSessionL().NewObjectQueryL(
             *iNamespaceDef,
             *iVideoObjectDef,
             this);
@@ -553,7 +635,7 @@
         TInt /*aNewItemCount*/ )
 #endif
     {
-    MPX_FUNC("CVcxMyVideosMdsDb::HandleQueryNewResults()");
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleQueryNewResults() start");
     
     if ( !iVideoList )
         {
@@ -585,6 +667,7 @@
     
     iMdsDbObserver->HandleCreateVideoListResp( iVideoList, aFirstNewItemIndex,
             EFalse /* not complete yet */);
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleQueryNewResults() exit");
     }
 
 
@@ -644,7 +727,7 @@
 //
 CMPXMedia* CVcxMyVideosMdsDb::CreateVideoL( TUint32 aId, TBool aFullDetails )
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::CreateVideoL" );
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::CreateVideoL() start" );
 
     CMdEObject* object = ObjectL( aId );
 
@@ -663,6 +746,7 @@
 
     CleanupStack::Pop( video );            // <-2
     CleanupStack::PopAndDestroy( object ); // <-1
+    MPX_DEBUG1( "CVcxMyVideosMdsDb::CreateVideoL() exit" );
 
     return video;
     }
@@ -675,7 +759,7 @@
         CMdESession& /*aSession*/,
         TInt aError)
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::HandleSessionOpened" );
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleSessionOpened() start" );
 
     iActiveSchedulerWait->AsyncStop();
 
@@ -683,10 +767,9 @@
         {
         MPX_DEBUG2( "CVcxMyVideosMdsDb::HandleSessionOpened: %d", aError );
 
-        iMdsError = aError;
-        delete iMdsSession;
-        iMdsSession = NULL;
+        iMdsSessionError = aError;
         }
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleSessionOpened() exit" );
     }
 
 // ---------------------------------------------------------------------------
@@ -697,7 +780,7 @@
         CMdESession& /*aSession*/,
         TInt aError)
     {
-    MPX_FUNC( "CVcxMyVideosMdsDb::HandleSessionError" );
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleSessionError() start" );
 
     if (iActiveSchedulerWait->IsStarted())
         {
@@ -715,9 +798,9 @@
         
     MPX_DEBUG2( "CVcxMyVideosMdsDb::HandleSessionError: %d", aError );
 
-    iMdsError = aError;
-    delete iMdsSession;
-    iMdsSession = NULL;
+    iMdsSessionError = aError;
+
+    MPX_DEBUG1("CVcxMyVideosMdsDb::HandleSessionError() exit" );
     }
 
 // ---------------------------------------------------------------------------
@@ -729,7 +812,7 @@
         TObserverNotificationType aType,
         const RArray<TItemId>& aObjectIdArray)
     {
-    TRAP( iMdsError, DoHandleObjectNotificationL( aType, aObjectIdArray ));
+    TRAP_IGNORE( DoHandleObjectNotificationL( aType, aObjectIdArray ));
     }
     
 // ---------------------------------------------------------------------------
@@ -738,14 +821,9 @@
 //
 CMdEObject* CVcxMyVideosMdsDb::ObjectL( const TItemId aId, TBool aIsVideo )
     {
-    if ( !iMdsSession )
-        {
-        MPX_DEBUG2("CVcxMyVideosMdsDb:: no mds session(%d), leaving", iMdsError);
-        User::Leave( iMdsError );
-        }
-
+    MPX_DEBUG1("CVcxMyVideosMdsDb::ObjectL start");
     //  If the id is not valid, just return NULL, because
-    //  iMdsSession->GetObjectL leaves in that case
+    //  MdsSessionL().GetObjectL leaves in that case
     if ( aId == KNoId )
         {
         return NULL;
@@ -754,11 +832,11 @@
     CMdEObject* object;
     if ( aIsVideo )
         {
-        object = iMdsSession->GetObjectL( aId, *iVideoObjectDef );
+        object = MdsSessionL().GetObjectL( aId, *iVideoObjectDef );
         }
     else
         {
-        object = iMdsSession->GetObjectL( aId, *iAlbums->iAlbumObjectDef );
+        object = MdsSessionL().GetObjectL( aId, *iAlbums->iAlbumObjectDef );
         }
     
     if ( object )
@@ -770,6 +848,7 @@
         MPX_DEBUG2( "CVcxMyVideosMdsDb::ObjectL not found, id: %d", aId );
         }
 
+    MPX_DEBUG1("CVcxMyVideosMdsDb::ObjectL exit");
     return object;
     }
 
@@ -1436,7 +1515,7 @@
     MPX_FUNC( "CVcxMyVideosMdsDb::GetSchemaDefinitionsL" );
 
     //  Namespace
-    iNamespaceDef = &(iMdsSession->GetDefaultNamespaceDefL());
+    iNamespaceDef = &(MdsSessionL().GetDefaultNamespaceDefL());
     
     //  Default object definitions
     iVideoObjectDef = &(iNamespaceDef->GetObjectDefL( KVcxVideoObjectName ));
@@ -1555,3 +1634,52 @@
     aObject.AddInt16PropertyL( *iTimeOffsetPropertyDef, timeOffset.Int() / secondsInMinute );
     aObject.AddTimePropertyL( *iLastModifiedDatePropertyDef, localTime );
     }
+
+// ---------------------------------------------------------------------------
+// CVcxMyVideosMdsDb::MdsSessionL
+// ---------------------------------------------------------------------------
+//
+CMdESession& CVcxMyVideosMdsDb::MdsSessionL()
+    {
+    if ( iMdsSessionError == KErrServerTerminated )
+        {
+        // This ensures that the iMdsSessionError KErrServerTerminated
+        // value does not change, since the user can't access iMdsSession.
+        // Recovery happens in ShutDownNotification func.
+        User::Leave( KErrServerTerminated );
+        }
+
+    if ( !iMdsSession )
+        {
+        OpenMdsSessionL();
+        
+        if ( !iMdsSession )
+            {
+            User::Leave( KErrGeneral );
+            }
+        }
+
+    return *iMdsSession;
+    }
+
+// ---------------------------------------------------------------------------
+// CVcxMyVideosMdsDb::ShutdownNotification
+// From MVcxMdsShutDownMonitorObserver
+// ---------------------------------------------------------------------------
+//    
+void CVcxMyVideosMdsDb::ShutdownNotification( TInt aShutdownState )
+    {
+    if ( aShutdownState == 0 )
+        {
+        // Server has restarted, recreate session.
+        MPX_DEBUG1("CVcxMyVideosMdsDb:: MDS Server restarted!");
+        OpenMdsSessionL();
+        return;
+        }
+        
+    if ( aShutdownState == 1 )
+        {
+        MPX_DEBUG1("CVcxMyVideosMdsDb:: MDS Server went down!");
+        iMdsSessionError = KErrServerTerminated;
+        }
+    }