videocollection/mpxmyvideoscollection/src/vcxmyvideoscollectionplugin.cpp
changeset 34 bbb98528c666
parent 0 96612d01cf9f
child 35 3738fe97f027
equal deleted inserted replaced
33:48e74db5d516 34:bbb98528c666
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:    Implementation of My Videos collection Plugin interface*
    14 * Description:   Implementation of My Videos collection Plugin interface*
    15 */
    15 */
    16 
       
    17 
    16 
    18 
    17 
    19 
    18 
    20 // INCLUDE FILES
    19 // INCLUDE FILES
    21 #include <e32cmn.h>
    20 #include <e32cmn.h>
    33 #include <drmutility.h>
    32 #include <drmutility.h>
    34 #include <bautils.h>
    33 #include <bautils.h>
    35 #include "vcxmyvideoscollectionplugin.h"
    34 #include "vcxmyvideoscollectionplugin.h"
    36 #include "vcxmyvideoscollection.hrh"
    35 #include "vcxmyvideoscollection.hrh"
    37 #include "vcxmyvideoscollectionutil.h"
    36 #include "vcxmyvideoscollectionutil.h"
    38 #include "vcxmyvideosdownloadutil.h"
       
    39 #include "vcxmyvideosvideocache.h"
    37 #include "vcxmyvideosvideocache.h"
    40 #include "vcxmyvideoscategories.h"
    38 #include "vcxmyvideoscategories.h"
    41 #include "vcxmyvideosmessagelist.h"
    39 #include "vcxmyvideosmessagelist.h"
    42 #include "vcxmyvideosasyncfileoperations.h"
    40 #include "vcxmyvideosasyncfileoperations.h"
    43 #include "vcxmyvideosopenhandler.h"
    41 #include "vcxmyvideosopenhandler.h"
    44 
    42 #include "vcxmyvideosmdsalbums.h"
    45 const TInt KMaxFileDeleteAttempts = 4;
    43 #include "vcxmyvideosalbums.h"
    46 const TInt KFileDeleteLoopDelay = 100000;
       
    47 
       
    48 //       Add 2000 new videos to memory card. Reboot phone, mds starts harvesting,
       
    49 //       open my videos -> mds server crashes and lots of events is sent to client.
       
    50 //       If one waits until all are harvested before opening my videos, it works.
       
    51 
    44 
    52 
    45 
    53 // ============================ MEMBER FUNCTIONS ==============================
    46 // ============================ MEMBER FUNCTIONS ==============================
    54 
    47 
    55 // ----------------------------------------------------------------------------
    48 // ----------------------------------------------------------------------------
    76     {
    69     {
    77     MPX_FUNC("CVcxMyVideosCollectionPlugin::~CVcxMyVideosCollectionPlugin");
    70     MPX_FUNC("CVcxMyVideosCollectionPlugin::~CVcxMyVideosCollectionPlugin");
    78     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: this = %x", this);
    71     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: this = %x", this);
    79         
    72         
    80     delete iMyVideosMdsDb;
    73     delete iMyVideosMdsDb;
    81     delete iDownloadUtil;
       
    82     delete iCache;
    74     delete iCache;
    83     delete iMessageList;
    75     delete iMessageList;
    84     delete iCategories;
    76     delete iCategories;
    85     delete iAsyncFileOperations;
    77     delete iAsyncFileOperations;
    86     delete iActiveTask;
    78     delete iActiveTask;
    87     delete iOpenHandler;
    79     delete iOpenHandler;
       
    80     delete iAlbums;
    88     iFs.Close();
    81     iFs.Close();
    89     }
    82     }
    90 
    83 
    91 // ----------------------------------------------------------------------------
    84 // ----------------------------------------------------------------------------
    92 // Constructor.
    85 // Constructor.
   106     {
    99     {
   107     MPX_FUNC("CVcxMyVideosCollectionPlugin::ConstructL");
   100     MPX_FUNC("CVcxMyVideosCollectionPlugin::ConstructL");
   108     
   101     
   109     User::LeaveIfError( iFs.Connect() );
   102     User::LeaveIfError( iFs.Connect() );
   110         
   103         
   111     iMyVideosMdsDb = CVcxMyVideosMdsDb::NewL( this, iFs );    
   104     iMyVideosMdsDb = CVcxMyVideosMdsDb::NewL( this, &AlbumsL(), iFs );    
   112     iActiveTask    = CVcxMyVideosActiveTask::NewL( *this );
   105     iActiveTask    = CVcxMyVideosActiveTask::NewL( *this );
   113     iCache         = CVcxMyVideosVideoCache::NewL( *this );
   106     iCache         = CVcxMyVideosVideoCache::NewL( *this );
   114     iMessageList   = CVcxMyVideosMessageList::NewL( *this );
   107     iMessageList   = CVcxMyVideosMessageList::NewL( *this );
   115     iOpenHandler   = CVcxMyVideosOpenHandler::NewL( *this, *iCache, *iMyVideosMdsDb );
   108     iOpenHandler   = CVcxMyVideosOpenHandler::NewL( *this, *iCache, *iMyVideosMdsDb );
   116     }
   109     }
   135     const CMPXCollectionPath& aPath,
   128     const CMPXCollectionPath& aPath,
   136     const TArray<TMPXAttribute>& aAttrs,
   129     const TArray<TMPXAttribute>& aAttrs,
   137     const TArray<TCapability>& /*aCaps*/,
   130     const TArray<TCapability>& /*aCaps*/,
   138     CMPXAttributeSpecs* /*aSpecs*/)
   131     CMPXAttributeSpecs* /*aSpecs*/)
   139     {
   132     {
   140     MPX_FUNC("CMPXMyVideosDbPlugin::MediaL");
   133     MPX_FUNC("CVcxMyVideosCollectionPlugin::MediaL");
   141     MPX_DEBUG_PATH(aPath);
   134     MPX_DEBUG_PATH(aPath);
   142     
   135     
   143     RArray<TInt> supportedIds;
   136     RArray<TInt> supportedIds;
   144     CleanupClosePushL( supportedIds ); // 1->
   137     CleanupClosePushL( supportedIds ); // 1->
   145 
   138 
   168         CleanupStack::PopAndDestroy( path );
   161         CleanupStack::PopAndDestroy( path );
   169         }
   162         }
   170 
   163 
   171     if ( ids.Count() == 0 )
   164     if ( ids.Count() == 0 )
   172         {
   165         {
   173         MPX_DEBUG1("CMPXMyVideosDbPlugin:: request didn't contain any items ids, aborting");
   166         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: request didn't contain any items ids, aborting");
   174         
   167         
   175         iObs->HandleMedia( NULL, KErrArgument );
   168         iObs->HandleMedia( NULL, KErrArgument );
   176         CleanupStack::PopAndDestroy( &ids );          // <-2
   169         CleanupStack::PopAndDestroy( &ids );          // <-2
   177         CleanupStack::PopAndDestroy( &supportedIds ); // <-1
   170         CleanupStack::PopAndDestroy( &supportedIds ); // <-1
   178         return;
   171         return;
   184     CMPXMedia* videoInCache = iCache->FindVideoByMdsIdL( ids[0].iId1, pos );
   177     CMPXMedia* videoInCache = iCache->FindVideoByMdsIdL( ids[0].iId1, pos );
   185     
   178     
   186     if ( videoInCache )
   179     if ( videoInCache )
   187         {
   180         {
   188         // 0 attributes means "get all" -> can't use cache
   181         // 0 attributes means "get all" -> can't use cache
   189         MPX_DEBUG2("CMPXMyVideosDbPlugin:: client is requesting %d attributes", aAttrs.Count());
   182         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: client is requesting %d attributes", aAttrs.Count());
   190         if ( aAttrs.Count() > 0 )
   183         if ( aAttrs.Count() > 0 )
   191             {
   184             {
   192             TBool nonSupportedAttrCanBeFoundFromMds;
   185             TBool nonSupportedAttrCanBeFoundFromMds;
   193             if ( TVcxMyVideosCollectionUtil::AreSupported( *videoInCache, aAttrs,
   186             if ( TVcxMyVideosCollectionUtil::AreSupported( *videoInCache, aAttrs,
   194                     nonSupportedAttrCanBeFoundFromMds ) )
   187                     nonSupportedAttrCanBeFoundFromMds ) )
   195                 {
   188                 {
   196                 MPX_DEBUG1("CMPXMyVideosDbPlugin:: all attributes found from cache");
   189                 MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: all attributes found from cache");
   197                 useCachedVideo = ETrue;
   190                 useCachedVideo = ETrue;
   198                 }
   191                 }
   199             else
   192             else
   200                 {
   193                 {
   201                 MPX_DEBUG1("CMPXMyVideosDbPlugin:: all attributes NOT found from cache");
   194                 MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: all attributes NOT found from cache");
   202                 if ( !nonSupportedAttrCanBeFoundFromMds )
   195                 if ( !nonSupportedAttrCanBeFoundFromMds )
   203                     {
   196                     {
   204                     MPX_DEBUG1("CMPXMyVideosDbPlugin:: none of the non cached attrs can be found from MDS -> use cached version");
   197                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: none of the non cached attrs can be found from MDS -> use cached version");
   205                     useCachedVideo = ETrue;
   198                     useCachedVideo = ETrue;
   206                     }
   199                     }
   207                 else
   200                 else
   208                     {
   201                     {
   209                     MPX_DEBUG1("CMPXMyVideosDbPlugin:: at least one of the non cached attributes can be found from MDS");
   202                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: at least one of the non cached attributes can be found from MDS");
   210                     }
   203                     }
   211                 }
   204                 }
   212             }
   205             }
   213         }
   206         }
   214 
   207 
   215     CMPXMedia* video;
   208     CMPXMedia* video;
   216 
   209 
   217     if ( useCachedVideo )
   210     if ( useCachedVideo )
   218         {
   211         {
   219         MPX_DEBUG1("CMPXMyVideosDbPlugin:: using cached video");
   212         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: using cached video");
   220         video = CMPXMedia::CopyL( *videoInCache );
   213         video = CMPXMedia::CopyL( *videoInCache );
   221         }
   214         }
   222     else
   215     else
   223         {
   216         {
   224         MPX_DEBUG1("CMPXMyVideosDbPlugin:: fetching from MDS");
   217         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: fetching from MDS");
   225         video = iMyVideosMdsDb->CreateVideoL( ids[0].iId1, ETrue /* full details */ );    
   218         video = iMyVideosMdsDb->CreateVideoL( ids[0].iId1, ETrue /* full details */ );    
   226         }
   219         }
   227         
   220         
   228     iObs->HandleMedia( video, KErrNone );
   221     iObs->HandleMedia( video, KErrNone );
   229     
   222     
   316                         {
   309                         {
   317                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: sync KVcxCommandMyVideosCancelDelete arrived");
   310                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: sync KVcxCommandMyVideosCancelDelete arrived");
   318                         iActiveTask->Cancel();
   311                         iActiveTask->Cancel();
   319                         }
   312                         }
   320                         break;
   313                         break;
       
   314                     
       
   315                     case KVcxCommandMyVideosAddAlbum:
       
   316                         {
       
   317                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: sync KVcxCommandMyVideosAddAlbum arrived");
       
   318                         AlbumsL().AddAlbumL( aCmd );
       
   319                         }
       
   320                         break;
   321                     }
   321                     }
   322                 }
   322                 }
   323                 break;
   323                 break;
   324                    
   324                    
   325             default:
   325             default:
   425         TMPXChangeEventType aEvent,
   425         TMPXChangeEventType aEvent,
   426         RArray<TUint32>& aId )
   426         RArray<TUint32>& aId )
   427     {
   427     {
   428     MPX_FUNC("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL");
   428     MPX_FUNC("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL");
   429     
   429     
       
   430     RArray<TUint32> nonVideoIds;
       
   431     nonVideoIds.Reset();
       
   432     CleanupClosePushL( nonVideoIds );
       
   433     
   430     switch ( aEvent )
   434     switch ( aEvent )
   431         {
   435         {
   432         case EMPXItemDeleted:
   436         case EMPXItemDeleted:
   433             {
   437             {
   434             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------------.");
   438             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------------.");
   435             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() Items from MDS deleted, deleting from cache |" );
   439             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() Items from MDS deleted, deleting from cache |" );
   436             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------------'");
   440             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------------'");
   437                         
   441                         
   438             iCache->RemoveL( aId );            
   442             iCache->RemoveL( aId );
       
   443             AlbumsL().RemoveAlbumsL( aId );
   439             }
   444             }
   440             break;
   445             break;
   441         
   446         
   442         case EMPXItemInserted:
   447         case EMPXItemInserted:
   443             {
   448             {
   446             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() ------------------------------------'");
   451             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() ------------------------------------'");
   447             
   452             
   448             if ( iMyVideosMdsDb->iVideoListFetchingIsOngoing )
   453             if ( iMyVideosMdsDb->iVideoListFetchingIsOngoing )
   449                 {
   454                 {
   450                 MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: video list fetching is ongoing, ignoring add event");
   455                 MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: video list fetching is ongoing, ignoring add event");
       
   456                 CleanupStack::PopAndDestroy( &nonVideoIds );
   451                 return;
   457                 return;
   452                 }
   458                 }
   453                 
   459                 
   454             TBool videoListFetchingWasCancelled = EFalse;
   460             TBool videoListFetchingWasCancelled = EFalse;
   455             
   461 
   456             // After the call, aId will contain only items which were actually inserted to cache.
   462             // After the call, aId will contain only items which were actually inserted to cache.
   457             // We receive add events for all object types. When fetching the item from MDS we use
   463             // We receive add events for all object types. When fetching the item from MDS we use
   458             // video condition and only video objects are added to cache.
   464             // video condition and only video objects are added to cache. Items which were detected
   459             iCache->AddVideosFromMdsL( aId, videoListFetchingWasCancelled );
   465             // to not be videos are added to nonVideoIds.
       
   466             iCache->AddVideosFromMdsL( aId, videoListFetchingWasCancelled, &nonVideoIds );
       
   467 
       
   468 #if 0 //TODO: do this if we want to support albums which are being added by someone else than My Videos Collection
       
   469             
       
   470             //After the call nonVideoIds will contain only items which were actually added
       
   471             //to albums.
       
   472             AlbumsL().AddAlbumsFromMdsL( nonVideoIds );
       
   473 #endif
       
   474             
   460             if ( videoListFetchingWasCancelled )
   475             if ( videoListFetchingWasCancelled )
   461                 {
   476                 {
   462                 RestartVideoListFetchingL();
   477                 RestartVideoListFetchingL();
   463                 }
   478                 }
   464              
   479             }
   465             SyncWithDownloadsL( aId );
   480             
   466             }
       
   467             break;
   481             break;
   468         
   482         
   469         case EMPXItemModified:
   483         case EMPXItemModified:
   470             {
   484             {
   471             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------.");
   485             MPX_DEBUG1("CVcxMyVideosCollectionPlugin::DoHandleMyVideosDbEventL() --------------------------------------.");
   486                     {
   500                     {
   487                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: couldn't find the modified item from MDS");
   501                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: couldn't find the modified item from MDS");
   488                     }
   502                     }
   489                 }
   503                 }
   490             }
   504             }
   491             SyncWithDownloadsL( aId );
   505             
       
   506             //TODO: handle album modify events
   492             break;
   507             break;
   493         }
   508         }
   494         
   509 
   495     TInt pos;
   510         TInt pos;
   496     for ( TInt i = 0; i < aId.Count(); i++ )
   511         TInt count = aId.Count();
   497         {
   512         for ( TInt i = 0; i < count; i++ )
       
   513             {
       
   514             if ( aEvent == EMPXItemInserted )
       
   515                 {
       
   516                 // add item from cache to the message if we have it.
       
   517                 CMPXMedia* video = iCache->FindVideoByMdsIdL( aId[i], pos );
       
   518                 TRAP_IGNORE( iMessageList->AddEventL( TMPXItemId( aId[i], KVcxMvcMediaTypeVideo),
       
   519                         aEvent, 0, video ) );
       
   520                 }
       
   521             else
       
   522                 {
       
   523                 TRAP_IGNORE( iMessageList->AddEventL( TMPXItemId( aId[i], KVcxMvcMediaTypeVideo),
       
   524                         aEvent ) );
       
   525                 }
       
   526             }
       
   527 
   498         if ( aEvent == EMPXItemInserted )
   528         if ( aEvent == EMPXItemInserted )
   499             {
   529             {
   500             // add item from cache to the message if we have it.
   530             //nonVideoIds are albums
   501             CMPXMedia* video = iCache->FindVideoByMdsIdL( aId[i], pos );
   531             TInt count = nonVideoIds.Count();
   502             TRAP_IGNORE( iMessageList->AddEventL( TMPXItemId( aId[i], 0), aEvent, 0, video ) );
   532             for ( TInt i = 0; i < count; i++ )
   503             }
   533                 {
   504         else
   534                 TRAP_IGNORE( iMessageList->AddEventL(
   505             {
   535                         TMPXItemId( nonVideoIds[i], KVcxMvcMediaTypeAlbum ), aEvent ) );
   506             TRAP_IGNORE( iMessageList->AddEventL( TMPXItemId( aId[i], 0), aEvent ) );
   536                 }
   507             }
   537             }
   508         }
   538 
       
   539     CleanupStack::PopAndDestroy( &nonVideoIds );
   509     
   540     
   510     iMessageList->SendL();
   541     iMessageList->SendL();
   511     }
   542     }
   512     
   543     
   513 // ----------------------------------------------------------------------------
   544 // ----------------------------------------------------------------------------
   514 // CVcxMyVideosCollectionPlugin::HandleStepL
   545 // CVcxMyVideosCollectionPlugin::HandleStepL
   515 // ----------------------------------------------------------------------------
   546 // ----------------------------------------------------------------------------
   516 //
   547 //
   517 TBool CVcxMyVideosCollectionPlugin::HandleStepL()
   548 MVcxMyVideosActiveTaskObserver::TStepResult CVcxMyVideosCollectionPlugin::HandleStepL()
   518     {
   549     {
   519     MPX_FUNC("CVcxMyVideosCollectionPlugin::HandleStepL");
   550     MPX_FUNC("CVcxMyVideosCollectionPlugin::HandleStepL");
   520 
   551 
   521     TBool done(ETrue);
   552     MVcxMyVideosActiveTaskObserver::TStepResult stepResult(MVcxMyVideosActiveTaskObserver::EDone);
   522 
   553 
   523     switch ( iActiveTask->GetTask() )
   554     switch ( iActiveTask->GetTask() )
   524         {
   555         {
   525         case KMPXCommandIdCollectionSet:
   556         case KMPXCommandIdCollectionSet:
   526             {
   557             {
   535 
   566 
   536             CleanupStack::PopAndDestroy( video );
   567             CleanupStack::PopAndDestroy( video );
   537             
   568             
   538             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: async KMPXCommandIdCollectionSet out");
   569             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: async KMPXCommandIdCollectionSet out");
   539             
   570             
   540             done = ETrue;
   571             stepResult = MVcxMyVideosActiveTaskObserver::EDone;            
   541             
       
   542             break;
   572             break;
   543             }
   573             }
   544         case KVcxCommandIdMyVideos:
   574         case KVcxCommandIdMyVideos:
   545             {
   575             {
   546             CMPXCommand& cmd = iActiveTask->GetCommand();
   576             CMPXCommand& cmd = iActiveTask->GetCommand();
   551                 }
   581                 }
   552 
   582 
   553             TInt myVideosCmd( cmd.ValueTObjectL<TUint>( KVcxMediaMyVideosCommandId ) );
   583             TInt myVideosCmd( cmd.ValueTObjectL<TUint>( KVcxMediaMyVideosCommandId ) );
   554 
   584 
   555             switch ( myVideosCmd )
   585             switch ( myVideosCmd )
   556                 {
   586                 {                
   557                 case KVcxCommandMyVideosStartDownload:
       
   558                     {
       
   559                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: Handling KVcxCommandMyVideosStartDownload command.");
       
   560                     
       
   561                     CMPXMedia* video = CMPXMedia::NewL( *(iActiveTask->GetCommand().Value<CMPXMedia>(
       
   562                             KMPXCommandColAddMedia)) );
       
   563                     CleanupStack::PushL( video ); // 1->
       
   564 
       
   565                     if ( !iCache->iVideoList )
       
   566                         {
       
   567                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: iVideoListCache = NULL -> creating new empty iVideoListCache");
       
   568                         iCache->iVideoListIsPartial = ETrue;
       
   569                         iCache->iVideoList          = TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
   570                         }
       
   571 
       
   572                     TBool resume = EFalse;
       
   573                     
       
   574                     if ( video->IsSupported( KVcxMediaMyVideosDownloadId ) )
       
   575                         {
       
   576                         TUint32 downloadId = video->ValueTObjectL<TUint32>( KVcxMediaMyVideosDownloadId );
       
   577                         if ( downloadId != 0 )
       
   578                             {
       
   579                             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: download id %d given by the client -> this is download resume",
       
   580                                     downloadId);
       
   581                             resume = ETrue;
       
   582                             
       
   583                             // load the existing item to cache if its not there already
       
   584                             }
       
   585                         }
       
   586                     
       
   587                     if ( !resume )
       
   588                         {    
       
   589                         if ( !video->IsSupported( KVcxMediaMyVideosRemoteUrl ) )
       
   590                             {
       
   591                             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxMediaMyVideosRemoteUrl not supported -> leaving with KErrArgument");
       
   592                             User::Leave( KErrArgument );
       
   593                             }
       
   594 
       
   595                         if ( video->ValueText( KVcxMediaMyVideosRemoteUrl ).Length() >
       
   596                                 KVcxMvcMaxUrlLength )
       
   597                             {
       
   598                             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: url longer than 1024 -> leaving with KErrArgument");
       
   599                             User::Leave( KErrArgument );
       
   600                             }
       
   601                         
       
   602                         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: starting download for url: %S", 
       
   603                             &video->ValueText( KVcxMediaMyVideosRemoteUrl ) );
       
   604 
       
   605                         video->SetTObjectValueL<TUint8>( KVcxMediaMyVideosOrigin, EVcxMyVideosOriginDownloaded );    
       
   606                         video->SetTObjectValueL<TUint8>( KVcxMediaMyVideosDownloadState,
       
   607                                 static_cast<TUint8>(EVcxMyVideosDlStateDownloading) );
       
   608                         video->SetTObjectValueL<TUint32>( KMPXMediaGeneralFlags, EVcxMyVideosVideoNew );
       
   609                             
       
   610                         HBufC* fileName = DownloadUtilL().CreateFilePathL( *video );
       
   611                         CleanupStack::PushL( fileName ); // 2->
       
   612                         video->SetTextValueL( KMPXMediaGeneralUri, *fileName );
       
   613                         CleanupStack::PopAndDestroy( fileName ); // <-2
       
   614                         }
       
   615 
       
   616                     TRAPD( err, DownloadUtilL().StartDownloadL( *video ) ); //download id is written to video object
       
   617                     
       
   618                     if ( err != KErrNone )
       
   619                         {
       
   620                         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: StartDownloadL left: %d", err);
       
   621                         User::Leave( err );
       
   622                         }
       
   623                     
       
   624                     if ( !resume )
       
   625                         {    
       
   626                         TUint32 newDownloadId = video->ValueTObjectL<TUint32>( KVcxMediaMyVideosDownloadId ); 
       
   627                         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: new download ID: %d",
       
   628                             newDownloadId);
       
   629 
       
   630                         AddVideoToMdsAndCacheL( *video );
       
   631                         }
       
   632                     else
       
   633                         {
       
   634                         // clear old error codes from the dl item
       
   635                         TInt pos;
       
   636                         
       
   637                         CMPXMedia* videoInCache = iCache->FindVideoByMdsIdL(
       
   638                                 TVcxMyVideosCollectionUtil::IdL( *video ), pos );
       
   639                         if ( videoInCache )
       
   640                             {
       
   641                             videoInCache->SetTObjectValueL<TInt>( KVcxMediaMyVideosDownloadError, 0 );
       
   642                             videoInCache->SetTObjectValueL<TInt>( KVcxMediaMyVideosDownloadGlobalError, 0 );
       
   643                             }
       
   644                         }
       
   645                         
       
   646                     CleanupStack::PopAndDestroy( video ); // <-1
       
   647                     done = ETrue;
       
   648                     }
       
   649                     break;
       
   650                     
       
   651                 case KVcxCommandMyVideosCancelDownload:
       
   652                     {
       
   653                     // Error code is returned to client if dl item was left to system.
       
   654                     // If file delete fails, then mds item is also left to system.
       
   655                     
       
   656                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: Handling KVcxCommandMyVideosCancelDownload command.");
       
   657                     
       
   658                     CMPXMedia* video = CMPXMedia::NewL( *(iActiveTask->GetCommand().Value<CMPXMedia>(
       
   659                             KMPXCommandColAddMedia)) );
       
   660                     CleanupStack::PushL( video ); // 1->
       
   661 
       
   662                     if ( !video->IsSupported( KVcxMediaMyVideosDownloadId ) ||
       
   663                             !video->IsSupported( KMPXMediaGeneralId ) ||
       
   664                             !video->IsSupported( KMPXMediaGeneralUri ) )
       
   665                         {
       
   666                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: parameter missing, can't cancel dl, leaving with KErrArgument");
       
   667                         User::Leave( KErrArgument );
       
   668                         }
       
   669 
       
   670                     TUint32 downloadId = video->ValueTObjectL<TUint32>(
       
   671                             KVcxMediaMyVideosDownloadId );
       
   672                     DownloadUtilL().CancelDownload( downloadId, ETrue /* remove file */ );
       
   673  
       
   674                     if ( BaflUtils::FileExists( iFs, video->ValueText( KMPXMediaGeneralUri ) ) )
       
   675                         {
       
   676                         TMPXItemId mpxItemId = video->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
       
   677                         TInt err( KErrNone );
       
   678                         
       
   679                         for ( TInt i = 0; i < KMaxFileDeleteAttempts; i++ )
       
   680                             {
       
   681                             TRAP( err, AsyncFileOperationsL().DeleteVideoL( mpxItemId.iId1, ETrue ) );
       
   682                                         
       
   683                             if ( err == KErrInUse )
       
   684                                 {
       
   685                                 MPX_DEBUG1( "CVcxMyVideosCollectionPlugin:: file is already in use, waiting a moment and try again");
       
   686                                 User::After( KFileDeleteLoopDelay );
       
   687                                 }
       
   688                             else
       
   689                                 {
       
   690                                 break;
       
   691                                 }
       
   692                             }
       
   693                         
       
   694                         if ( err != KErrNone && err != KErrNotFound )
       
   695                             {
       
   696 #ifdef _DEBUG                        
       
   697                             if ( err == KErrInUse )
       
   698                                 {
       
   699                                 TVcxMyVideosCollectionUtil::PrintOpenFileHandlesL(
       
   700                                         video->ValueText( KMPXMediaGeneralUri ), iFs );
       
   701                                 }
       
   702 #endif
       
   703 							
       
   704                             // Some error occured when cancelling download operation, dl item is however gone and file is left
       
   705                             // -> change dl id to 0 and leave mpx collection item there. Report operation to client as a success.
       
   706                             MPX_DEBUG1( "CVcxMyVideosCollectionPlugin:: dl item is gone from dl manager, file and mpx item are left, setting dl id to 0");
       
   707                             TRAP_IGNORE( SetDownloadIdToZeroL( downloadId ) );
       
   708                             }
       
   709                         }
       
   710                     
       
   711                     CleanupStack::PopAndDestroy( video ); // <-1
       
   712                     done = ETrue;
       
   713                     }
       
   714                     break;
       
   715                                         
       
   716                 case KVcxCommandMyVideosPauseDownload:
       
   717                     {
       
   718                     CMPXMedia& cmd = iActiveTask->GetCommand();
       
   719                     if ( !cmd.IsSupported( KVcxMediaMyVideosDownloadId ) )
       
   720                         {
       
   721                         User::Leave( KErrArgument );
       
   722                         }
       
   723                     else
       
   724                         {
       
   725                         TInt err = DownloadUtilL().PauseDownload(
       
   726                                 cmd.ValueTObjectL<TUint32>( KVcxMediaMyVideosDownloadId ) );
       
   727                         User::LeaveIfError( err );
       
   728                         }
       
   729                     done = ETrue;
       
   730                     }
       
   731                     break;
       
   732                 
       
   733                 case KVcxCommandMyVideosGetMediaFullDetailsByMpxId:
   587                 case KVcxCommandMyVideosGetMediaFullDetailsByMpxId:
   734                     {
   588                     {
   735                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosGetMediaFullDetailsByMpxId received");
   589                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosGetMediaFullDetailsByMpxId received");
   736                     CMPXMedia& cmd = iActiveTask->GetCommand();
   590                     CMPXMedia& cmd = iActiveTask->GetCommand();
   737                     TMPXItemId mpxId( TVcxMyVideosCollectionUtil::IdL( cmd ) );
   591                     TMPXItemId mpxId( TVcxMyVideosCollectionUtil::IdL( cmd ) );
   741                         }
   595                         }
   742                     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosGetMediaFullDetailsByMpxId cmd: MDS ID %d requested", mpxId.iId1 );
   596                     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosGetMediaFullDetailsByMpxId cmd: MDS ID %d requested", mpxId.iId1 );
   743 
   597 
   744                     CMPXMedia* video = iMyVideosMdsDb->CreateVideoL(
   598                     CMPXMedia* video = iMyVideosMdsDb->CreateVideoL(
   745                             mpxId.iId1, ETrue /* full details */ );
   599                             mpxId.iId1, ETrue /* full details */ );
   746 
       
   747                     if ( !video )
       
   748                         {
       
   749                         User::Leave( KErrGeneral );
       
   750                         }
       
   751                     
   600                     
   752                     CleanupStack::PushL( video ); // 1->
   601                     CleanupStack::PushL( video ); // 1->
   753                     
   602                                         
   754                     TBool eventsAdded;
       
   755                     SyncVideoWithDownloadsL( *video, eventsAdded,
       
   756                             EFalse /* dont add event to iMessageList */ );
       
   757                     
       
   758                     cmd.SetCObjectValueL<CMPXMedia>( KMPXCommandColAddMedia, video );
   603                     cmd.SetCObjectValueL<CMPXMedia>( KMPXCommandColAddMedia, video );
   759                     CleanupStack::PopAndDestroy( video ); // <-1
   604                     CleanupStack::PopAndDestroy( video ); // <-1
   760 
   605 
   761                     cmd.SetTObjectValueL<TUid>(KMPXMessageCollectionId, TUid::Uid(
   606                     cmd.SetTObjectValueL<TUid>(KMPXMessageCollectionId, TUid::Uid(
   762                             KVcxUidMyVideosMpxCollection));
   607                             KVcxUidMyVideosMpxCollection));
   763                     
   608                     
   764                     done = ETrue;
   609                     stepResult = MVcxMyVideosActiveTaskObserver::EDone;            
   765                     }
   610                     }
   766                     break;
   611                     break;
   767                     
   612                     
   768                 case KVcxCommandMyVideosGetMediasByMpxId:
   613                 case KVcxCommandMyVideosGetMediasByMpxId:
   769                     {
   614                     {
   815                         
   660                         
   816                     if ( iCache->iVideoListIsPartial )
   661                     if ( iCache->iVideoListIsPartial )
   817                         {                            
   662                         {                            
   818                         // Load items to cache
   663                         // Load items to cache
   819                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: loading requested items to iCache->iVideoList");
   664                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: loading requested items to iCache->iVideoList");
   820 
   665                         
   821                         iCache->AddVideosFromMdsL( mdsIds, videoListFetchingWasCancelled );
   666                         iCache->AddVideosFromMdsL( mdsIds, videoListFetchingWasCancelled );
   822                         if ( mdsIds.Count() > 0 )
       
   823                             {
       
   824                             SyncWithDownloadsL( mdsIds );
       
   825                             }
       
   826                         }
   667                         }
   827                     else
   668                     else
   828                         {
   669                         {
   829                         // iCache->iVideoList contains all 
   670                         // iCache->iVideoList contains all 
   830                         }
   671                         }
   853 
   694 
   854                     CleanupStack::Pop( message );            //  <-3
   695                     CleanupStack::Pop( message );            //  <-3
   855                     CleanupStack::PopAndDestroy( &mdsIds2 ); //  <-2
   696                     CleanupStack::PopAndDestroy( &mdsIds2 ); //  <-2
   856                     CleanupStack::PopAndDestroy( &mdsIds );  //  <-1
   697                     CleanupStack::PopAndDestroy( &mdsIds );  //  <-1
   857                         
   698                         
   858                     done = ETrue;
   699                     stepResult = MVcxMyVideosActiveTaskObserver::EDone;            
   859                     }
   700                     }
   860                     break;
   701                     break;
   861                 
   702                 
   862                 case KVcxCommandMyVideosCopy:
   703                 case KVcxCommandMyVideosCopy:
   863                 case KVcxCommandMyVideosMove:
   704                 case KVcxCommandMyVideosMove:
   864                     {
   705                     {
   865                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosMove or Copy step");
   706                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosMove or Copy step");
   866                     done = AsyncFileOperationsL().HandleMoveOrCopyStepL();
   707                     stepResult = AsyncFileOperationsL().HandleMoveOrCopyStepL();
   867                     }
   708                     }
   868                     break;
   709                     break;
   869                     
   710                     
   870                 case KVcxCommandMyVideosDelete:
   711                 case KVcxCommandMyVideosDelete:
   871                     {
   712                     {
   872                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosDelete step");
   713                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: KVcxCommandMyVideosDelete step");
   873                     done = AsyncFileOperationsL().HandleDeleteStepL();
   714                     stepResult = AsyncFileOperationsL().HandleDeleteStepL();
   874                     }
   715                     }
   875                     break;
   716                     break;
       
   717                     
       
   718                 case KVcxCommandMyVideosAddToAlbum:
       
   719                     iAlbums->AddVideosToAlbumL( &iActiveTask->GetCommand() );
       
   720                     stepResult = MVcxMyVideosActiveTaskObserver::EStopStepping;
       
   721                     break;
       
   722 
       
   723                 case KVcxCommandMyVideosRemoveFromAlbum:
       
   724                     iAlbums->RemoveVideosFromAlbumL( &iActiveTask->GetCommand() );
       
   725                     stepResult = MVcxMyVideosActiveTaskObserver::EStopStepping;
       
   726                     break;
       
   727                     
       
   728                 case KVcxCommandMyVideosAddAlbum:
       
   729                     {
       
   730                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: async KVcxCommandMyVideosAddAlbum arrived");
       
   731                     AlbumsL().AddAlbumL( iActiveTask->GetCommand() );
       
   732                     stepResult = MVcxMyVideosActiveTaskObserver::EDone;
       
   733                     break;
       
   734                     }
       
   735 
       
   736                 case KVcxCommandMyVideosRemoveAlbums:
       
   737                     {
       
   738                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: async KVcxCommandMyVideosRemoveAlbums arrived");
       
   739                     AlbumsL().RemoveAlbumsFromMdsOnlyL( iActiveTask->Command() );
       
   740                     stepResult = MVcxMyVideosActiveTaskObserver::EStopStepping;
       
   741                     break;
       
   742                     }
       
   743 
       
   744                     
   876                 }
   745                 }
   877             }
   746             }
   878             break;
   747             break;
   879 
   748 
   880             
   749             
   881         case KMPXCommandIdCollectionRemoveMedia:
   750         case KMPXCommandIdCollectionRemoveMedia:
   882             {
   751             {
   883             TMPXItemId mpxId = iActiveTask->GetCommand().ValueTObjectL<TMPXItemId>(
   752             TMPXItemId mpxId = iActiveTask->GetCommand().ValueTObjectL<TMPXItemId>(
   884                     KMPXMediaGeneralId );
   753                     KMPXMediaGeneralId );
   885             AsyncFileOperationsL().DeleteVideoL( mpxId.iId1 );
   754             AsyncFileOperationsL().DeleteVideoL( mpxId.iId1 );
   886             done = ETrue;
   755             stepResult = MVcxMyVideosActiveTaskObserver::EDone;            
   887             break;
   756             break;
   888             }
   757             }
   889             
   758             
   890         default:
   759         default:
   891             {
   760             {
   892             // Should never happen!
   761             // Should never happen!
   893             ASSERT(0);
   762             ASSERT(0);
   894             break;
   763             break;
   895             }
   764             }
   896         }
   765         }
   897     return done;
   766     return stepResult;
   898     }
   767     }
   899 
   768 
   900 
   769 
   901 // ----------------------------------------------------------------------------
   770 // ----------------------------------------------------------------------------
   902 // Handler for async operations completed
   771 // Handler for async operations completed
   908     MPX_FUNC("CVcxMyVideosCollectionPlugin::HandleOperationCompleted");
   777     MPX_FUNC("CVcxMyVideosCollectionPlugin::HandleOperationCompleted");
   909     
   778     
   910     if ( aErr != KErrNone )
   779     if ( aErr != KErrNone )
   911         {
   780         {
   912         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: Leave or cancel happened during the operation: %d", aErr);
   781         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: Leave or cancel happened during the operation: %d", aErr);
   913         TRAPD( err, AsyncFileOperationsL().CancelOperationL( aErr ) ); // generates resp message for move,copy or delete operations
   782         
   914         if ( err != KErrNone )
   783         if ( iAsyncFileOperations )
   915             {
   784             {
   916             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: failed to generate resp msg: %d", err);
   785             TRAPD( err, AsyncFileOperationsL().CancelOperationL( aErr ) ); // generates resp message for move,copy or delete operations
       
   786             if ( err != KErrNone )
       
   787                 {
       
   788                 MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: failed to generate resp msg: %d", err);
       
   789                 }
   917             }
   790             }
   918         }
   791         }
   919 
   792 
   920     
   793     
   921 
   794 
   924 
   797 
   925     iObs->HandleCommandComplete( &cmd, KErrNone );
   798     iObs->HandleCommandComplete( &cmd, KErrNone );
   926     }
   799     }
   927 
   800 
   928 // ----------------------------------------------------------------------------
   801 // ----------------------------------------------------------------------------
   929 // CVcxMyVideosCollectionPlugin::HandleDlEvent
       
   930 // From CVcxMyVideosDownloadUtilObserver
       
   931 // ----------------------------------------------------------------------------
       
   932 //
       
   933 void CVcxMyVideosCollectionPlugin::HandleDlEvent( TVcxMyVideosDownloadState aState,
       
   934                 TUint32 aDownloadId,
       
   935                 TInt aProgress,
       
   936                 TInt64 aDownloaded,
       
   937                 TInt32 aError,
       
   938                 TInt32 aGlobalError )
       
   939     {
       
   940     TRAPD( err, DoHandleDlEventL( aState, aDownloadId, aProgress,
       
   941             aDownloaded, aError, aGlobalError ) );
       
   942     if ( err != KErrNone )
       
   943         {
       
   944         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: DoHandleDlEventL left with error code: %d", err);
       
   945         }
       
   946     }
       
   947     
       
   948 // ----------------------------------------------------------------------------
       
   949 // CVcxMyVideosCollectionPlugin::DoHandleDlEventL
       
   950 // ----------------------------------------------------------------------------
       
   951 //
       
   952 void CVcxMyVideosCollectionPlugin::DoHandleDlEventL( TVcxMyVideosDownloadState aState,
       
   953                 TUint32 aDownloadId,
       
   954                 TInt aProgress,
       
   955                 TInt64 aDownloaded,
       
   956                 TInt32 aError,
       
   957                 TInt32 aGlobalError )
       
   958     {
       
   959     MPX_FUNC("CVcxMyVideosCollectionPlugin::DoHandleDlEventL");
       
   960     
       
   961     CMPXMedia* video = iCache->FindVideoByDownloadIdL( aDownloadId );
       
   962 
       
   963     MPX_DEBUG3("CVcxMyVideosCollectionPlugin:: dl event for download ID %d, pointer = %x) arrived.", aDownloadId, video);
       
   964     
       
   965     TBool sendEvent = EFalse;
       
   966     if ( video )
       
   967         {
       
   968         TMPXItemId mpxId( TVcxMyVideosCollectionUtil::IdL( *video ) );
       
   969         MPX_DEBUG4("CVcxMyVideosCollectionPlugin:: MPX item (MDS ID %d) (DL ID %d) %S",
       
   970                 mpxId.iId1, aDownloadId, &TVcxMyVideosCollectionUtil::Title( *video ) );
       
   971                 
       
   972         TUint8 currentState = TVcxMyVideosCollectionUtil::DownloadStateL( *video );
       
   973         
       
   974         if ( currentState == EVcxMyVideosDlStateDownloaded )
       
   975             {
       
   976             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: already in Downloaded state, discarding event");
       
   977             return;
       
   978             }
       
   979             
       
   980         if ( currentState != aState )
       
   981             {
       
   982             MPX_DEBUG5("CVcxMyVideosCollectionPlugin:: updating (mds id: %d) (dl id: %d) state: %S -> %S",
       
   983                         mpxId.iId1, aDownloadId, &DownloadState( currentState ), &DownloadState( aState ) );
       
   984             video->SetTObjectValueL<TUint8>( KVcxMediaMyVideosDownloadState, static_cast<TUint8>(aState) );
       
   985             sendEvent = ETrue;
       
   986 
       
   987             if ( aState == EVcxMyVideosDlStateDownloaded )
       
   988                 {
       
   989                 MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: downloaded state received -> setting download id to 0");
       
   990                 
       
   991                 //1. set download id to 0
       
   992                 video->SetTObjectValueL<TUint32>( KVcxMediaMyVideosDownloadId, 0 );
       
   993                 
       
   994                 //2. update drm flag
       
   995 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   996                 RFile64 dlFile;
       
   997 #else
       
   998                 RFile dlFile;
       
   999 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1000                 TInt err = dlFile.Open( iFs, video->ValueText( KMPXMediaGeneralUri ), EFileRead );
       
  1001                 if ( err == KErrNone )
       
  1002                     {
       
  1003                     MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: file opened ok for drm reading");
       
  1004                     CleanupClosePushL( dlFile ); // 1->
       
  1005                     DRM::CDrmUtility* drmUtil = DRM::CDrmUtility::NewLC(); // 2->
       
  1006                     if ( drmUtil->IsProtectedL( dlFile ) )
       
  1007                         {
       
  1008                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: file is DRM protected, setting the property flag");
       
  1009                         TUint32 flags = video->ValueTObjectL<TUint32>( KMPXMediaGeneralFlags );
       
  1010                         flags |= EVcxMyVideosVideoDrmProtected;
       
  1011                         video->SetTObjectValueL<TUint32>( KMPXMediaGeneralFlags, flags );
       
  1012                         }
       
  1013                     else
       
  1014                         {
       
  1015                         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: file is not DRM protected");
       
  1016                         }
       
  1017                     CleanupStack::PopAndDestroy( drmUtil ); // <-2
       
  1018                     CleanupStack::PopAndDestroy( &dlFile ); // <-1
       
  1019                     }
       
  1020                 else
       
  1021                     {
       
  1022                     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: file didnt open for drm reading, %d", err);
       
  1023                     }
       
  1024                 NotifyDownloadCompletedL( *video );
       
  1025                     
       
  1026                 //3. Update file size using iCache->UpdateVideoL function since it changes item position and
       
  1027                 //   sends category modified events if necessarry.
       
  1028                 CMPXMedia* updateObject = CMPXMedia::NewL();
       
  1029                 CleanupStack::PushL( updateObject ); // 1->
       
  1030                 updateObject->SetTObjectValueL<TMPXItemId>( KMPXMediaGeneralId, mpxId );
       
  1031 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1032                 updateObject->SetTObjectValueL<TInt64>( KMPXMediaGeneralExtSizeInt64,
       
  1033                         static_cast<TInt64>( aDownloaded ) );
       
  1034                 // set current value to 0 to force event sending and video list position updating    
       
  1035                 video->SetTObjectValueL<TInt64>( KMPXMediaGeneralExtSizeInt64, 0 );                
       
  1036 #else
       
  1037                 updateObject->SetTObjectValueL<TInt>( KMPXMediaGeneralSize,
       
  1038                         static_cast<TInt>( aDownloaded ) );
       
  1039                 // set current value to 0 to force event sending and video list position updating    
       
  1040                 video->SetTObjectValueL<TInt>( KMPXMediaGeneralSize, 0 );                
       
  1041 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1042                 iCache->UpdateVideoL( *updateObject );
       
  1043                 CleanupStack::PopAndDestroy( updateObject ); // <-1                
       
  1044                 // find video again since it might have been deleted in iCache->UpdateVideoL
       
  1045                 TInt pos;
       
  1046                 video = iCache->FindVideoByMdsIdL( mpxId.iId1, pos );
       
  1047 
       
  1048                 //file size and download id are saved to database
       
  1049                 iMyVideosMdsDb->UpdateVideoL( *video );
       
  1050                 sendEvent = EFalse; // MDS will send the event, this avoids duplicate
       
  1051                 }
       
  1052                 
       
  1053             if ( aState == EVcxMyVideosDlStateFailed )
       
  1054                 {
       
  1055                 video->SetTObjectValueL<TInt32>( KVcxMediaMyVideosDownloadError, aError );
       
  1056                 video->SetTObjectValueL<TInt32>( KVcxMediaMyVideosDownloadGlobalError,
       
  1057                         aGlobalError );
       
  1058                 }            
       
  1059             }
       
  1060         else
       
  1061             {
       
  1062             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: state was already same(%S), skipping state update.", &DownloadState( currentState ));
       
  1063             }
       
  1064  
       
  1065         TInt8 currentProgress = video->ValueTObjectL<TInt8>( KVcxMediaMyVideosDownloadProgress );
       
  1066         if ( currentProgress != aProgress )
       
  1067             {
       
  1068             MPX_DEBUG4("CVcxMyVideosCollectionPlugin:: (dl id: %d) progress: %d -> %d",
       
  1069                         aDownloadId, currentProgress, aProgress );
       
  1070 
       
  1071             video->SetTObjectValueL<TInt8>( KVcxMediaMyVideosDownloadProgress,
       
  1072                     static_cast<TInt8>( aProgress ) );
       
  1073             // Don't send the update event for progress.
       
  1074             //sendEvent = ETrue;
       
  1075             }
       
  1076         else
       
  1077             {
       
  1078             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: progress was already same(%d), skipping progress update.", currentProgress);
       
  1079             }
       
  1080             
       
  1081         TInt64 currentFileSize = 0;
       
  1082 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1083         if ( video->IsSupported( KMPXMediaGeneralExtSizeInt64 ) )
       
  1084             {
       
  1085             currentFileSize = video->ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 );
       
  1086             }
       
  1087 #else
       
  1088         if ( video->IsSupported( KMPXMediaGeneralSize ) )
       
  1089             {
       
  1090             currentFileSize = video->ValueTObjectL<TInt>( KMPXMediaGeneralSize );
       
  1091             }
       
  1092 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1093         if ( currentFileSize != aDownloaded )
       
  1094             {
       
  1095             MPX_DEBUG4("CVcxMyVideosCollectionPlugin:: updating (dl id: %d) size: %ld -> %ld",
       
  1096                         aDownloadId, currentFileSize, aDownloaded );
       
  1097                         
       
  1098 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1099             video->SetTObjectValueL<TInt64>( KMPXMediaGeneralExtSizeInt64, aDownloaded );
       
  1100 #else
       
  1101             TInt newFileSize( aDownloaded );
       
  1102             video->SetTObjectValueL<TInt>( KMPXMediaGeneralSize, newFileSize );
       
  1103 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1104             //sendEvent = ETrue;
       
  1105             }
       
  1106         
       
  1107         if ( sendEvent )
       
  1108             {
       
  1109             iMessageList->AddEventL( mpxId, EMPXItemModified );
       
  1110             iMessageList->SendL();
       
  1111             }
       
  1112         }
       
  1113     else
       
  1114         {
       
  1115         if ( (aState != EVcxMyVideosDlStateDownloaded) && (aProgress < 100) &&
       
  1116                 !iCache->iVideoListIsPartial )
       
  1117             {
       
  1118             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -----------------------------------------------------------------------.");
       
  1119             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: Event for progressing download arrived, but the MPX/MDS item not found!|");
       
  1120             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -> deleting download.                                                  |");
       
  1121             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -----------------------------------------------------------------------'");
       
  1122             
       
  1123             RHttpDownload* download = DownloadUtilL().Download( aDownloadId );
       
  1124             if ( download )
       
  1125                 {
       
  1126                 MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: Download ID (%d) not found from MPX/MDS, deleting download!",
       
  1127                         aDownloadId );                
       
  1128                 DownloadUtilL().DeleteDownloadAsync( aDownloadId, ETrue );
       
  1129                 }
       
  1130             }
       
  1131         }
       
  1132     }
       
  1133 
       
  1134 // ----------------------------------------------------------------------------
       
  1135 // CVcxMyVideosCollectionPlugin::SyncWithDownloadsL
       
  1136 // ----------------------------------------------------------------------------
       
  1137 //
       
  1138 void CVcxMyVideosCollectionPlugin::SyncWithDownloadsL(
       
  1139         RArray<TUint32>& aItemsInCache )
       
  1140     {
       
  1141     MPX_FUNC("CVcxMyVideosCollectionPlugin::SyncWithDownloadsL()");
       
  1142     
       
  1143     TBool eventsAdded = EFalse;
       
  1144     for ( TInt i = 0; i < aItemsInCache.Count(); i++ )
       
  1145         {
       
  1146         TInt pos;
       
  1147         CMPXMedia* video = iCache->FindVideoByMdsIdL( aItemsInCache[i], pos );
       
  1148         if ( video )
       
  1149             {
       
  1150             SyncVideoWithDownloadsL( *video, eventsAdded );
       
  1151             }
       
  1152         }
       
  1153     if ( eventsAdded )
       
  1154         {
       
  1155         iMessageList->SendL();
       
  1156         }
       
  1157     }
       
  1158 
       
  1159 // ----------------------------------------------------------------------------
       
  1160 // CVcxMyVideosCollectionPlugin::SyncVideoWithDownloadsL
       
  1161 // ----------------------------------------------------------------------------
       
  1162 //
       
  1163 void CVcxMyVideosCollectionPlugin::SyncVideoWithDownloadsL( CMPXMedia& aVideo,
       
  1164         TBool& aEventAdded, TBool aAddEvent )
       
  1165     {    
       
  1166     TInt downloadId( TVcxMyVideosCollectionUtil::DownloadIdL( aVideo ) );
       
  1167     
       
  1168     if ( downloadId )
       
  1169         {
       
  1170         RHttpDownload* download( DownloadUtilL().Download( downloadId ) );
       
  1171 
       
  1172         if ( download )
       
  1173             {
       
  1174             MPX_DEBUG2("CVcxMyVideosCollectionPlugin::SyncVideoWithDownloadsL() item (DL ID: %d) found from dl manager", downloadId);
       
  1175     
       
  1176             TBool modified = EFalse;
       
  1177             SyncVideoAndDownloadL( aVideo, *download, modified );
       
  1178             if ( modified && aAddEvent )
       
  1179                 {
       
  1180                 iMessageList->AddEventL( TVcxMyVideosCollectionUtil::IdL( aVideo ),
       
  1181                         EMPXItemModified );
       
  1182                 aEventAdded = ETrue;
       
  1183                 }
       
  1184             }
       
  1185         else
       
  1186             {
       
  1187             //download id != 0 and it is not found from download manager -> we set download id to 0
       
  1188             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: download id %d != 0 and no corresponding download found from Download Manager",
       
  1189                     downloadId);
       
  1190             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -> setting download id to 0");
       
  1191             aVideo.SetTObjectValueL<TUint32>( KVcxMediaMyVideosDownloadId, 0 );
       
  1192             iMyVideosMdsDb->UpdateVideoL( aVideo ); // if video list fetching is ongoing, this will leave with KErrNotReady
       
  1193             }
       
  1194         }
       
  1195     }
       
  1196  
       
  1197 // ----------------------------------------------------------------------------
       
  1198 // CVcxMyVideosCollectionPlugin::SyncVideoAndDownloadL
       
  1199 // ----------------------------------------------------------------------------
       
  1200 //
       
  1201 void  CVcxMyVideosCollectionPlugin::SyncVideoAndDownloadL(
       
  1202         CMPXMedia& aVideo,
       
  1203         RHttpDownload& aDownload,
       
  1204         TBool& aModified )
       
  1205     {
       
  1206     MPX_FUNC("CVcxMyVideosCollectionPlugin::SyncVideoAndDownloadL()");
       
  1207         
       
  1208     aModified = EFalse;
       
  1209          
       
  1210     TBuf<KMaxUrlLength> downloadUrl;
       
  1211     aDownload.GetStringAttribute( EDlAttrReqUrl, downloadUrl );
       
  1212     if ( aVideo.ValueText( KVcxMediaMyVideosRemoteUrl ) 
       
  1213             != downloadUrl )
       
  1214         {
       
  1215         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: urls in MPX and DL Manager differ!");
       
  1216         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: remote url in MPX: %S",
       
  1217                 &(aVideo.ValueText( KVcxMediaMyVideosRemoteUrl )));
       
  1218         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: remote url in DL Manager: %S",
       
  1219                 &downloadUrl);
       
  1220         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -> updating MPX cache");
       
  1221         
       
  1222         aVideo.SetTextValueL( KVcxMediaMyVideosRemoteUrl, downloadUrl );
       
  1223         aModified = ETrue;
       
  1224         }
       
  1225     else
       
  1226         {
       
  1227         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: remote urls already same in dl manager and mpx");
       
  1228         }
       
  1229 
       
  1230     // KVcxMediaMyVideosDownloadState
       
  1231     TVcxMyVideosDownloadState dlStateInDlManager;
       
  1232     DownloadUtilL().GetDownloadState( aDownload, dlStateInDlManager );
       
  1233     
       
  1234     TUint8 dlStateInMpxCache; 
       
  1235     if ( aVideo.IsSupported( KVcxMediaMyVideosDownloadState ))
       
  1236         {
       
  1237         dlStateInMpxCache = aVideo.ValueTObjectL<TUint8>( KVcxMediaMyVideosDownloadState );
       
  1238         }
       
  1239     else
       
  1240         {
       
  1241         dlStateInMpxCache = static_cast<TUint8>( EVcxMyVideosDlStateNone );
       
  1242         }
       
  1243 
       
  1244     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: dl state in dl manager: %S", &DownloadState( dlStateInDlManager ));
       
  1245     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: dl state in mpx cache: %S", &DownloadState( dlStateInMpxCache ));
       
  1246     
       
  1247     if ( static_cast<TUint8>( dlStateInDlManager ) != dlStateInMpxCache )
       
  1248         {        
       
  1249         if ( dlStateInDlManager == EVcxMyVideosDlStateDownloaded )
       
  1250             {
       
  1251             // Download finished event has arrived when we weren't around, call event handler to get things right.
       
  1252             // Collection is updated and download is deleted from Download Manager.
       
  1253             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: Download is in Finished state and collection has download id != 0");
       
  1254             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: -> we have missed download finished event, lets generate it by ourselves.");
       
  1255             
       
  1256             TUint64 downloaded( 0 );
       
  1257             TUint8 progress( DownloadUtilL().DownloadProgress( aDownload, downloaded, EFalse ) );            
       
  1258             TUint32 downloadId( aVideo.ValueTObjectL<TUint32>( KVcxMediaMyVideosDownloadId ) );
       
  1259             HandleDlEvent( dlStateInDlManager, downloadId,
       
  1260                     progress, downloaded, KErrNone, KErrNone );
       
  1261             DownloadUtilL().DeleteDownloadAsync( downloadId, EFalse /* don't delete content */ );
       
  1262             }
       
  1263         else
       
  1264             {
       
  1265             MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: dl state in dl manager differs of mpx cache-> updating mpx cache");
       
  1266             aVideo.SetTObjectValueL<TUint8>( KVcxMediaMyVideosDownloadState,
       
  1267                     static_cast<TUint8>( dlStateInDlManager ) );
       
  1268             aModified = ETrue;
       
  1269             }
       
  1270         }
       
  1271     else
       
  1272         {
       
  1273         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: download state already same in dl manager and mds");
       
  1274         }
       
  1275     
       
  1276     // KVcxMediaMyVideosDownloadProgress
       
  1277     TUint64 downloaded = 0;
       
  1278     TInt8 dlProgressInDlManager = DownloadUtilL().DownloadProgress(
       
  1279             aDownload, downloaded, EFalse );
       
  1280     
       
  1281     TInt8 dlProgressInMpxCache;
       
  1282     if ( aVideo.IsSupported( KVcxMediaMyVideosDownloadProgress ) )
       
  1283         {
       
  1284         dlProgressInMpxCache = aVideo.ValueTObjectL<TInt8>( KVcxMediaMyVideosDownloadProgress );
       
  1285         }
       
  1286     else
       
  1287         {
       
  1288         aVideo.SetTObjectValueL<TInt8>( KVcxMediaMyVideosDownloadProgress, 0 );
       
  1289         dlProgressInMpxCache = 0;
       
  1290         }
       
  1291 
       
  1292     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: dl progress in dl manager: %d", dlProgressInDlManager);
       
  1293     MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: dl progress in mpx cache: %d", dlProgressInMpxCache);
       
  1294 
       
  1295     if ( dlProgressInDlManager != dlProgressInMpxCache )
       
  1296         {
       
  1297         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: dl progress in dl manager differs of mpx cache-> updating mpx cache");
       
  1298         aVideo.SetTObjectValueL<TInt8>( KVcxMediaMyVideosDownloadProgress,
       
  1299                 static_cast<TInt8>( dlProgressInDlManager ) );
       
  1300         aModified = ETrue;
       
  1301         }
       
  1302     else
       
  1303         {
       
  1304         MPX_DEBUG1("CVcxMyVideosCollectionPlugin:: download progress already same in dl manager and mds");
       
  1305         }        
       
  1306     }
       
  1307 
       
  1308 // ----------------------------------------------------------------------------
       
  1309 // CVcxMyVideosCollectionPlugin::SyncVideoListWithDownloadsL
       
  1310 // ----------------------------------------------------------------------------
       
  1311 //
       
  1312 void CVcxMyVideosCollectionPlugin::SyncVideoListWithDownloadsL( CMPXMedia& aVideoList,
       
  1313         TBool aSendEvents, TInt aStartPos )
       
  1314     {
       
  1315     MPX_FUNC("CVcxMyVideosCollectionPlugin::SyncVideoListWithDownloadsL");
       
  1316 
       
  1317     CMPXMediaArray* videoArray = aVideoList.Value<CMPXMediaArray>(
       
  1318                                 KMPXMediaArrayContents);    
       
  1319 
       
  1320     CMPXMedia* video;
       
  1321     
       
  1322     TBool eventsAdded = EFalse;
       
  1323     for ( TInt i = aStartPos; i < videoArray->Count(); i++ )
       
  1324         {
       
  1325         video = (*videoArray)[i];        
       
  1326         SyncVideoWithDownloadsL( *video, eventsAdded, aSendEvents );        
       
  1327         }
       
  1328     if ( eventsAdded )
       
  1329         {
       
  1330         iMessageList->SendL();
       
  1331         }
       
  1332     }
       
  1333 
       
  1334 // ----------------------------------------------------------------------------
       
  1335 // CVcxMyVideosCollectionPlugin::DownloadUtil
       
  1336 // ----------------------------------------------------------------------------
       
  1337 //
       
  1338 CVcxMyVideosDownloadUtil& CVcxMyVideosCollectionPlugin::DownloadUtilL()
       
  1339     {
       
  1340     if ( !iDownloadUtil )
       
  1341         {
       
  1342         iDownloadUtil = CVcxMyVideosDownloadUtil::NewL( *this, iFs );        
       
  1343         }
       
  1344     
       
  1345     if ( !iOrphanDownloadsCleared )
       
  1346         {
       
  1347         if ( !iCache->iVideoListIsPartial )
       
  1348             {
       
  1349             iOrphanDownloadsCleared = ETrue;
       
  1350             iDownloadUtil->ClearOrphanDownloadsL( *iCache->iVideoList );
       
  1351             }
       
  1352         }
       
  1353     
       
  1354     return *iDownloadUtil;
       
  1355     }
       
  1356 
       
  1357 // ----------------------------------------------------------------------------
       
  1358 // CVcxMyVideosCollectionPlugin::CategoriesL
   802 // CVcxMyVideosCollectionPlugin::CategoriesL
  1359 // ----------------------------------------------------------------------------
   803 // ----------------------------------------------------------------------------
  1360 //
   804 //
  1361 CVcxMyVideosCategories& CVcxMyVideosCollectionPlugin::CategoriesL()
   805 CVcxMyVideosCategories& CVcxMyVideosCollectionPlugin::CategoriesL()
  1362     {
   806     {
  1363     if ( !iCategories )
   807     if ( !iCategories )
  1364         {
   808         {
  1365         iCategories = CVcxMyVideosCategories::NewL( *this );
   809         iCategories = CVcxMyVideosCategories::NewL( *this );
  1366         }
   810         }
  1367     return *iCategories;
   811     return *iCategories;
       
   812     }
       
   813 
       
   814 // ----------------------------------------------------------------------------
       
   815 // CVcxMyVideosCollectionPlugin::AlbumsL
       
   816 // TODO: Unecessarry func since we always load this
       
   817 // ----------------------------------------------------------------------------
       
   818 //
       
   819 CVcxMyVideosAlbums& CVcxMyVideosCollectionPlugin::AlbumsL()
       
   820     {
       
   821     if ( !iAlbums )
       
   822         {
       
   823         iAlbums = CVcxMyVideosAlbums::NewL( *this );
       
   824         }
       
   825     return *iAlbums;
  1368     }
   826     }
  1369 
   827 
  1370 // ----------------------------------------------------------------------------
   828 // ----------------------------------------------------------------------------
  1371 // CVcxMyVideosCollectionPlugin::HandleCreateVideoListResp
   829 // CVcxMyVideosCollectionPlugin::HandleCreateVideoListResp
  1372 // ----------------------------------------------------------------------------
   830 // ----------------------------------------------------------------------------
  1436 	    {
   894 	    {
  1437 		User::Leave( leave );
   895 		User::Leave( leave );
  1438 		}
   896 		}
  1439     }
   897     }
  1440 
   898 
       
   899 #if 0
  1441 // ----------------------------------------------------------------------------
   900 // ----------------------------------------------------------------------------
  1442 // CVcxMyVideosCollectionPlugin::NotifyDownloadCompletedL
   901 // CVcxMyVideosCollectionPlugin::NotifyDownloadCompletedL
  1443 // ----------------------------------------------------------------------------
   902 // ----------------------------------------------------------------------------
  1444 //
   903 //
  1445 void CVcxMyVideosCollectionPlugin::NotifyDownloadCompletedL( CMPXMedia& aVideo )
   904 void CVcxMyVideosCollectionPlugin::NotifyDownloadCompletedL( CMPXMedia& aVideo )
  1462     
   921     
  1463     DownloadUtilL().NotifyDownloadCompleted( *buffer );
   922     DownloadUtilL().NotifyDownloadCompleted( *buffer );
  1464     
   923     
  1465     CleanupStack::PopAndDestroy( buffer );
   924     CleanupStack::PopAndDestroy( buffer );
  1466     }
   925     }
       
   926 #endif
  1467 
   927 
  1468 // ----------------------------------------------------------------------------
   928 // ----------------------------------------------------------------------------
  1469 // CVcxMyVideosCollectionPlugin::NotifyNewVideosCountDecreasedL
   929 // CVcxMyVideosCollectionPlugin::NotifyNewVideosCountDecreasedL
  1470 // ----------------------------------------------------------------------------
   930 // ----------------------------------------------------------------------------
  1471 //
   931 //
  1484     aVideo.ExternalizeL( stream );
   944     aVideo.ExternalizeL( stream );
  1485     
   945     
  1486     stream.CommitL();
   946     stream.CommitL();
  1487     
   947     
  1488     CleanupStack::PopAndDestroy( &stream );    
   948     CleanupStack::PopAndDestroy( &stream );    
  1489     
   949         
  1490     DownloadUtilL().NotifyNewVideosCountDecreased( *buffer );
       
  1491     
       
  1492     CleanupStack::PopAndDestroy( buffer );
   950     CleanupStack::PopAndDestroy( buffer );
  1493     }
   951     }
  1494 
   952 
  1495 // ----------------------------------------------------------------------------
   953 // ----------------------------------------------------------------------------
  1496 // CVcxMyVideosCollectionPlugin::AddVideoToMdsAndCacheL
   954 // CVcxMyVideosCollectionPlugin::AddVideoToMdsAndCacheL
  1624             EVcxMyVideosVideoListOrderChanged );
  1082             EVcxMyVideosVideoListOrderChanged );
  1625 
  1083 
  1626     iMessageList->SendL();    
  1084     iMessageList->SendL();    
  1627     }
  1085     }
  1628 
  1086 
       
  1087 #if 0
  1629 // ----------------------------------------------------------------------------
  1088 // ----------------------------------------------------------------------------
  1630 // CVcxMyVideosCollectionPlugin::SetDownloadIdToZeroL
  1089 // CVcxMyVideosCollectionPlugin::SetDownloadIdToZeroL
  1631 // ----------------------------------------------------------------------------
  1090 // ----------------------------------------------------------------------------
  1632 //
  1091 //
  1633 void CVcxMyVideosCollectionPlugin::SetDownloadIdToZeroL( TUint aDownloadId )
  1092 void CVcxMyVideosCollectionPlugin::SetDownloadIdToZeroL( TUint aDownloadId )
  1674             return KNullDesC;
  1133             return KNullDesC;
  1675         }
  1134         }
  1676     }
  1135     }
  1677 #endif
  1136 #endif
  1678 
  1137 
       
  1138 #endif