videocollection/mpxmyvideoscollection/src/vcxmyvideosalbums.cpp
branchRCL_3
changeset 26 67eb01668b0e
child 45 baf439b22ddd
equal deleted inserted replaced
23:8f0df5c82986 26:67eb01668b0e
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Albums related collection functionality (except db operations).
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <mpxlog.h>
       
    22 #include <mpxmediacontainerdefs.h>
       
    23 #include <mpxcollectionpluginobserver.h>
       
    24 #include <mpxmediageneraldefs.h>
       
    25 #include "vcxmyvideosalbums.h"
       
    26 #include "vcxmyvideoscollectionutil.h"
       
    27 #include "vcxmyvideoscollectionplugin.h"
       
    28 #include "vcxmyvideosmdsdb.h"
       
    29 #include "vcxmyvideosmdsalbums.h"
       
    30 #include "vcxmyvideoscollectionutil.h"
       
    31 #include "vcxmyvideosmessagelist.h"
       
    32 #include "vcxmyvideosopenhandler.h"
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS =============================
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // Constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CVcxMyVideosAlbums::CVcxMyVideosAlbums( CVcxMyVideosCollectionPlugin& aCollection )
       
    41 : iCollection( aCollection )
       
    42     {
       
    43     }
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // 2nd-phase constructor
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 void CVcxMyVideosAlbums::ConstructL()
       
    50     {
       
    51     iAlbums.Reset();
       
    52     iChangedAlbums.Reset();
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // Two-Phase Constructor
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CVcxMyVideosAlbums* CVcxMyVideosAlbums::NewL( CVcxMyVideosCollectionPlugin& aCollection )
       
    60     {
       
    61     CVcxMyVideosAlbums* self = new(ELeave) CVcxMyVideosAlbums( aCollection );
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     CleanupStack::Pop( self );
       
    65     return self;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // Destructor
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CVcxMyVideosAlbums::~CVcxMyVideosAlbums()
       
    73     {
       
    74     delete iAlbumList;
       
    75     TInt count = iAlbums.Count();
       
    76     for ( TInt i = 0; i < count; i++ )
       
    77         {
       
    78         delete iAlbums[i];
       
    79         }
       
    80     iAlbums.Close();
       
    81     iMdsOpTargetIds.Close();
       
    82     iMdsOpResults.Close();
       
    83     iMdsOpResultsUint32.Close();
       
    84     iRemoveFromAlbumVideos.Close();
       
    85     iChangedAlbums.Close();
       
    86     }
       
    87     
       
    88 // ----------------------------------------------------------------------------
       
    89 // CVcxMyVideosAlbums::CreateAlbumListL
       
    90 // ----------------------------------------------------------------------------
       
    91 //
       
    92 void CVcxMyVideosAlbums::CreateAlbumListL()
       
    93     {
       
    94     if ( iAlbumList )
       
    95         {
       
    96         return;
       
    97         }
       
    98     iAlbumList = TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
    99     iCollection.iMyVideosMdsDb->iAlbums->GetAlbumsL( iAlbumList, *this );
       
   100     }
       
   101 
       
   102 // ----------------------------------------------------------------------------
       
   103 // CVcxMyVideosAlbums::CreateAlbumsL
       
   104 // Called when iAlbumList is ready, fills iAlbums.
       
   105 // Pointers to CVcxMyVideosVideoCache::iVideoList are not filled in yet.
       
   106 // ----------------------------------------------------------------------------
       
   107 //
       
   108 void CVcxMyVideosAlbums::CreateAlbumsL()
       
   109     {
       
   110     CMPXMediaArray* albumArray = iAlbumList->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   111 
       
   112     TInt count = albumArray->Count();
       
   113     CVcxMyVideosAlbum* album;
       
   114     CMPXMedia* media;
       
   115     for ( TInt i = 0; i < count; i++ )
       
   116         {
       
   117         media = albumArray->AtL( i );
       
   118         album = CVcxMyVideosAlbum::NewLC( iCollection );
       
   119         album->iMdsId = TVcxMyVideosCollectionUtil::IdL( *media ).iId1;
       
   120         album->iMedia = media;
       
   121         iAlbums.AppendL( album );
       
   122         CleanupStack::Pop( album );
       
   123         }
       
   124     }
       
   125 
       
   126 // ----------------------------------------------------------------------------
       
   127 // CVcxMyVideosAlbums::GetAlbumContentIdsL
       
   128 // Fetches MDS IDs for all albums.
       
   129 // ----------------------------------------------------------------------------
       
   130 //
       
   131 void CVcxMyVideosAlbums::GetAlbumContentIdsL()
       
   132     {
       
   133     TInt count = iAlbums.Count();
       
   134     for ( TInt i = 0; i < count; i++ )
       
   135         {
       
   136         // These calls go to queue and responses arrive asynchronously
       
   137         // to HandleGetAlbumContentIdsRespL.
       
   138         iCollection.iMyVideosMdsDb->iAlbums->GetAlbumContentIdsL( iAlbums[i]->iMdsId,
       
   139                 iAlbums[i]->iVideoList, *this );
       
   140         }
       
   141     }
       
   142 
       
   143 // ----------------------------------------------------------------------------
       
   144 // CVcxMyVideosAlbums::Album
       
   145 // ----------------------------------------------------------------------------
       
   146 //
       
   147 CVcxMyVideosAlbum* CVcxMyVideosAlbums::Album( TUint32 aMdsId, TInt* aPos )
       
   148     {
       
   149     TInt count = iAlbums.Count();
       
   150     for ( TInt i = 0; i < count; i++ )
       
   151         {
       
   152         if ( iAlbums[i]->iMdsId == aMdsId )
       
   153             {
       
   154             if ( aPos )
       
   155                 {
       
   156                 *aPos = i;
       
   157                 }
       
   158             return iAlbums[i];
       
   159             }
       
   160         }
       
   161     return NULL;
       
   162     }
       
   163 
       
   164 // ----------------------------------------------------------------------------
       
   165 // CVcxMyVideosAlbums::AddVideosToAlbumL
       
   166 // Command from MPX client.
       
   167 // ----------------------------------------------------------------------------
       
   168 //
       
   169 void CVcxMyVideosAlbums::AddVideosToAlbumL( CMPXMedia* aCmd )
       
   170     {    
       
   171     TInt albumId = TVcxMyVideosCollectionUtil::Uint32ValueL( *aCmd );
       
   172     CVcxMyVideosAlbum* album = Album( albumId );
       
   173     if ( album )
       
   174         {
       
   175         CMPXMediaArray* mediaArray = TVcxMyVideosCollectionUtil::MediaArrayL( *aCmd );
       
   176         TInt count = mediaArray->Count();
       
   177         CMPXMedia* video;
       
   178         TInt mdsId;
       
   179         RArray<TUint32> uniqueVideoIds;
       
   180         uniqueVideoIds.Reset();
       
   181         CleanupClosePushL( uniqueVideoIds );
       
   182         
       
   183         for ( TInt i = 0; i < count; i++ )
       
   184             {
       
   185             video = mediaArray->AtL( i );
       
   186             mdsId = TVcxMyVideosCollectionUtil::IdL( *video ).iId1;
       
   187 
       
   188             // Mark duplicates as failed to aCmd (KErrAlreadyExists)
       
   189             if ( uniqueVideoIds.Find( mdsId ) == KErrNotFound )
       
   190                 {
       
   191                 uniqueVideoIds.AppendL( mdsId );
       
   192 
       
   193                 // Mark videos which are already in album as failed to aCmd (KErrAlreadyExists)
       
   194                 if ( album->BelongsToAlbum( mdsId ) )
       
   195                     {
       
   196                     MPX_DEBUG3("CVcxMyVideosAlbums:: %d already belongs to %d album",
       
   197                             mdsId, albumId );
       
   198                     video->SetTObjectValueL<TInt32>( KVcxMediaMyVideosInt32Value, KErrAlreadyExists );
       
   199                     }
       
   200                 else
       
   201                     {
       
   202                     video->SetTObjectValueL<TInt32>( KVcxMediaMyVideosInt32Value, KErrNone );
       
   203                     }
       
   204                 }
       
   205             else
       
   206                 {
       
   207                 MPX_DEBUG2("CVcxMyVideosAlbums:: %d already present in the aCmd, marking as KErrAlreadyExists", mdsId);
       
   208                 video->SetTObjectValueL<TInt32>( KVcxMediaMyVideosInt32Value, KErrAlreadyExists );
       
   209                 }
       
   210 
       
   211             }
       
   212         
       
   213         CleanupStack::PopAndDestroy( &uniqueVideoIds );
       
   214         }
       
   215     
       
   216     iCollection.iMyVideosMdsDb->iAlbums->AddVideosToAlbumL( aCmd, *this );
       
   217     }
       
   218 
       
   219 // ----------------------------------------------------------------------------
       
   220 // CVcxMyVideosAlbums::RemoveVideosFromAlbumL
       
   221 // Converts video mpx ids to relation ids and calls RemoveRelationsL.
       
   222 // ----------------------------------------------------------------------------
       
   223 //
       
   224 void CVcxMyVideosAlbums::RemoveVideosFromAlbumL( CMPXMedia* aCmd )
       
   225     {
       
   226     TInt albumId = TVcxMyVideosCollectionUtil::Uint32ValueL( *aCmd );
       
   227     CMPXMediaArray* array = TVcxMyVideosCollectionUtil::MediaArrayL( *aCmd );
       
   228     TVcxMyVideosAlbumVideo video;
       
   229     TInt index;
       
   230     TInt error;
       
   231     CMPXMedia* media;
       
   232     TInt count = array->Count();
       
   233     iMdsOpTargetIds.Reset();
       
   234     iMdsOpTargetIds.Reserve( count );
       
   235     iRemoveFromAlbumVideos.Reset();
       
   236     iRemoveFromAlbumVideos.Reserve( count );
       
   237     TVcxMyVideosAlbumVideo paddingVideo;
       
   238     paddingVideo.iMdsId         = 0;
       
   239     paddingVideo.iRelationMdsId = 0;
       
   240     
       
   241     for ( TInt i = 0; i < count; i++ )
       
   242         {
       
   243         media = array->AtL( i );
       
   244         error = Album( albumId )->Video(
       
   245                 TVcxMyVideosCollectionUtil::IdL( *media ).iId1, video, index );
       
   246         if ( error == KErrNone )
       
   247             {
       
   248             iRemoveFromAlbumVideos.AppendL( video ); //store album video data now to avoid second search when response arrives
       
   249             iMdsOpTargetIds.AppendL( video.iRelationMdsId );
       
   250             }
       
   251         else
       
   252             {
       
   253             iRemoveFromAlbumVideos.AppendL( paddingVideo ); //add dummy video to keep media array and iRemoveFromAlbumVideos in sync
       
   254             }
       
   255         }
       
   256     iMdsOpResultsUint32.Reset();
       
   257     iCollection.iMyVideosMdsDb->iAlbums->RemoveRelationsL(
       
   258             iMdsOpTargetIds, iMdsOpResultsUint32, *this );
       
   259     }
       
   260 
       
   261 // ----------------------------------------------------------------------------
       
   262 // CVcxMyVideosAlbums::AddAlbumL
       
   263 // ----------------------------------------------------------------------------
       
   264 //
       
   265 void CVcxMyVideosAlbums::AddAlbumL( CMPXMedia& aCmd )
       
   266     {
       
   267     if ( TVcxMyVideosCollectionUtil::Title( aCmd ).Length() > 255 )
       
   268         {
       
   269         User::Leave( KErrArgument );
       
   270         }
       
   271     
       
   272     iCollection.iMyVideosMdsDb->iAlbums->AddAlbumL( aCmd );
       
   273     
       
   274     TUint32 mdsId = TVcxMyVideosCollectionUtil::IdL( aCmd ).iId1;
       
   275     if ( !Album( mdsId ) )
       
   276         {
       
   277         CMPXMediaArray* albumArray = iAlbumList->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   278         albumArray->AppendL( aCmd );
       
   279         
       
   280         CVcxMyVideosAlbum* album = CVcxMyVideosAlbum::NewLC( iCollection );
       
   281         album->iMdsId = mdsId;
       
   282         album->iMedia = albumArray->AtL( albumArray->Count() - 1 );
       
   283         iAlbums.AppendL( album );
       
   284         CleanupStack::Pop( album );
       
   285         
       
   286         iCollection.iMessageList->AddEventL( TMPXItemId( mdsId, KVcxMvcMediaTypeAlbum ),
       
   287                 EMPXItemInserted, EVcxMyVideosListNoInfo, album->iMedia );
       
   288         iCollection.iMessageList->SendL();
       
   289         }
       
   290     }
       
   291 
       
   292 // ----------------------------------------------------------------------------
       
   293 // CVcxMyVideosAlbums::RemoveAlbumsFromMdsOnlyL
       
   294 // ----------------------------------------------------------------------------
       
   295 //
       
   296 void CVcxMyVideosAlbums::RemoveAlbumsFromMdsOnlyL( CMPXMedia* aCmd )
       
   297     {
       
   298     iCollection.iMyVideosMdsDb->iAlbums->RemoveAlbumsL( aCmd, *this );
       
   299     }
       
   300 
       
   301 // ----------------------------------------------------------------------------
       
   302 // CVcxMyVideosAlbums::RemoveAlbumsL
       
   303 // From MDS delete event.
       
   304 // ----------------------------------------------------------------------------
       
   305 //
       
   306 void CVcxMyVideosAlbums::RemoveAlbumsL( RArray<TUint32>& aAlbumIds )
       
   307     {
       
   308     TInt count = aAlbumIds.Count();
       
   309     for ( TInt i = count -1; i >= 0; i-- )
       
   310         {
       
   311         TBool removed = RemoveAlbumL( aAlbumIds[i], EFalse /* dont compress */);
       
   312         if ( removed )
       
   313             {
       
   314             aAlbumIds.Remove( i );
       
   315             }
       
   316         }
       
   317     iAlbums.Compress();
       
   318     
       
   319     iCollection.iMessageList->SendL();        
       
   320     }
       
   321 
       
   322 // ----------------------------------------------------------------------------
       
   323 // CVcxMyVideosAlbums::RemoveAlbumL
       
   324 // ----------------------------------------------------------------------------
       
   325 //
       
   326 TBool CVcxMyVideosAlbums::RemoveAlbumL( TUint32 aMdsId, TBool aCompress )
       
   327     {
       
   328     TInt pos;
       
   329     CVcxMyVideosAlbum* album = Album( aMdsId, &pos );
       
   330     if ( album )
       
   331         {
       
   332         CMPXMediaArray* albumArray = TVcxMyVideosCollectionUtil::MediaArrayL( *iAlbumList );
       
   333         
       
   334         iAlbums.Remove( pos );
       
   335         albumArray->Remove( pos );
       
   336         
       
   337         MPX_DEBUG2("CVcxMyVideosAlbums:: removing album from pos %d", pos);
       
   338         
       
   339         if ( aCompress )
       
   340             {
       
   341             iAlbums.Compress();
       
   342             }
       
   343         iCollection.iMessageList->AddEventL( TMPXItemId( aMdsId, KVcxMvcMediaTypeAlbum ),
       
   344                 EMPXItemDeleted, EVcxMyVideosListNoInfo );
       
   345         return ETrue;
       
   346         }
       
   347     return EFalse;
       
   348     }
       
   349 
       
   350 // ----------------------------------------------------------------------------
       
   351 // CVcxMyVideosAlbums::CalculateAttributesL
       
   352 // ----------------------------------------------------------------------------
       
   353 //
       
   354 void CVcxMyVideosAlbums::CalculateAttributesL()
       
   355     {
       
   356     TInt count = iAlbums.Count();
       
   357     for ( TInt i = 0; i < count; i++ )
       
   358         {
       
   359         iAlbums[i]->CalculateAttributesL();
       
   360         }
       
   361     }
       
   362 
       
   363 // ----------------------------------------------------------------------------
       
   364 // CVcxMyVideosAlbums::AddAlbumsFromMdsL
       
   365 // From MDS insert event, no need to fetch content since the album was just
       
   366 // created.
       
   367 // ----------------------------------------------------------------------------
       
   368 //
       
   369 void CVcxMyVideosAlbums::AddAlbumsFromMdsL( RArray<TUint32>& /*aAlbumIds*/ )
       
   370     {
       
   371     //TODO: implement if we want to support albums which are added by someone else
       
   372     //      than My Videos Collection.
       
   373     }
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // CVcxMyVideosAlbums::HandleGetAlbumsRespL
       
   377 // ----------------------------------------------------------------------------
       
   378 //
       
   379 void CVcxMyVideosAlbums::HandleGetAlbumsRespL( CMPXMedia* aAlbumList )
       
   380     {
       
   381     // aAlbumList = iAlbumList
       
   382     CMPXMediaArray* array = aAlbumList->Value<CMPXMediaArray>( KMPXMediaArrayContents );
       
   383     TInt count = array->Count();
       
   384     TMPXItemId mpxId;
       
   385     CMPXMedia* album;
       
   386     for ( TInt i = 0; i < count; i++ )
       
   387         {
       
   388         album = array->AtL( i );
       
   389         mpxId = TVcxMyVideosCollectionUtil::IdL( *album );
       
   390         iCollection.iMessageList->AddEventL( mpxId, EMPXItemInserted, 0, album );
       
   391         }
       
   392     CreateAlbumsL(); //fills in iAlbums from iAlbumList
       
   393     GetAlbumContentIdsL(); //fills iAlbums with MDS IDs of the videos asynchronously
       
   394     iCollection.iMessageList->SendL();
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------------------------------
       
   398 // CVcxMyVideosAlbums::HandleGetAlbumContentIdsRespL
       
   399 // ----------------------------------------------------------------------------
       
   400 //
       
   401 void CVcxMyVideosAlbums::HandleGetAlbumContentIdsRespL( TUint32 aAlbumId,
       
   402         RArray<TVcxMyVideosAlbumVideo>& /*aAlbumContentIds*/ )
       
   403     {    
       
   404     //&aAlbumContent = iAlbums[x]->iVideoList
       
   405     
       
   406     CVcxMyVideosAlbum* album( Album( aAlbumId ) ); 
       
   407     if ( album )
       
   408         {
       
   409         album->Sort(); //sorts iAlbum[x]->iVideoList by MDS ID.
       
   410         }
       
   411 
       
   412     //if last album -> album list is complete
       
   413     if ( iAlbums[iAlbums.Count() -1]->iMdsId == aAlbumId )
       
   414         {
       
   415         MPX_DEBUG1("CVcxMyVideosAlbums:: content ids for last album arrived -> album list is complete" );
       
   416         iAlbumListIsComplete = ETrue;
       
   417 
       
   418         if ( iCollection.iOpenHandler->iPendingAlbumOpenId )
       
   419             {
       
   420             MPX_DEBUG1("CVcxMyVideosAlbums:: there was album open pending, calling HandleAlbumOpenL()" );
       
   421 
       
   422             TRAPD( err, iCollection.iOpenHandler->HandleAlbumOpenL() );
       
   423             if ( err != KErrNone )
       
   424                 {
       
   425                 MPX_DEBUG3("CVcxMyVideosAlbums:: HandleAlbumOpenL() left: %d, calling HandleOpen(NULL,%d)",
       
   426                         err, err);
       
   427                 iCollection.iObs->HandleOpen( static_cast<CMPXMedia*>(NULL), err );
       
   428                 }
       
   429             }
       
   430         }
       
   431     
       
   432     
       
   433 #ifdef _DEBUG
       
   434     if ( album )
       
   435         {
       
   436         TInt count = album->iVideoList.Count();
       
   437 
       
   438         MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: content ids for album %d arrived", aAlbumId);
       
   439 
       
   440         for ( TInt i = 0; i < album->iVideoList.Count(); i++ )
       
   441             {
       
   442             MPX_DEBUG2("CVcxMyVideosCollectionPlugin:: mds id = %d", album->iVideoList[i].iMdsId );
       
   443             }
       
   444         }
       
   445 #endif
       
   446     }
       
   447 
       
   448 // ----------------------------------------------------------------------------
       
   449 // CVcxMyVideosAlbums::HandleGetAlbumsResp
       
   450 // ----------------------------------------------------------------------------
       
   451 //
       
   452 void CVcxMyVideosAlbums::HandleGetAlbumsResp( CMPXMedia* aAlbumList )
       
   453     {
       
   454     TRAP_IGNORE( HandleGetAlbumsRespL( aAlbumList ) );
       
   455     }
       
   456 
       
   457 // ----------------------------------------------------------------------------
       
   458 // CVcxMyVideosAlbums::HandleGetAlbumContentIdsResp
       
   459 // ----------------------------------------------------------------------------
       
   460 //
       
   461 void CVcxMyVideosAlbums::HandleGetAlbumContentIdsResp( TUint32 aAlbumId,
       
   462         RArray<TVcxMyVideosAlbumVideo>& aAlbumContent )
       
   463     {
       
   464     TRAP_IGNORE( HandleGetAlbumContentIdsRespL( aAlbumId, aAlbumContent ) );    
       
   465     }
       
   466 
       
   467 // ----------------------------------------------------------------------------
       
   468 // CVcxMyVideosAlbums::HandleAddVideosToAlbumResp
       
   469 // ----------------------------------------------------------------------------
       
   470 //
       
   471 void CVcxMyVideosAlbums::HandleAddVideosToAlbumResp( CMPXMedia* aCmd,
       
   472         RPointerArray<CMdEInstanceItem>& aItemArray )
       
   473     {    
       
   474     TRAPD( err, DoHandleAddVideosToAlbumRespL( aCmd, aItemArray ) );
       
   475     
       
   476     if ( err != KErrNone )
       
   477         {
       
   478         CMPXMedia* cmd = iCollection.iActiveTask->Command(); 
       
   479         if ( cmd )
       
   480             {
       
   481             cmd->SetTObjectValueL<TInt>( KVcxMediaMyVideosInt32Value, err );
       
   482             iCollection.iActiveTask->Done();
       
   483             }
       
   484         }
       
   485     }
       
   486 
       
   487 // ----------------------------------------------------------------------------
       
   488 // CVcxMyVideosAlbums::DoHandleAddVideosToAlbumResp
       
   489 // ----------------------------------------------------------------------------
       
   490 //
       
   491 void CVcxMyVideosAlbums::DoHandleAddVideosToAlbumRespL( CMPXMedia* aCmd,
       
   492         RPointerArray<CMdEInstanceItem>& aItemArray )
       
   493     {
       
   494     MPX_DEBUG1("CVcxMyVideosAlbums::DoHandleAddVideosToAlbumResp() start");
       
   495 
       
   496     TUint32 albumId = TVcxMyVideosCollectionUtil::Uint32ValueL( *aCmd );
       
   497     CVcxMyVideosAlbum* album = Album( albumId );
       
   498 
       
   499     CMPXMediaArray* mediaArray = TVcxMyVideosCollectionUtil::MediaArrayL( *aCmd );
       
   500     
       
   501     TVcxMyVideosAlbumVideo video;
       
   502     RArray<CVcxMyVideosAlbum*> modifiedAlbums;
       
   503     modifiedAlbums.Reset();
       
   504     CleanupClosePushL( modifiedAlbums );
       
   505 
       
   506     TInt mediaArrayCount  = mediaArray->Count();
       
   507     TInt resultArrayCount = aItemArray.Count();
       
   508     CMPXMedia* media;
       
   509     TInt j = 0;
       
   510     for ( TInt i = 0; i < mediaArrayCount; i++ )
       
   511         {
       
   512         if ( j > resultArrayCount - 1 )
       
   513             {
       
   514             MPX_DEBUG1("CVcxMyVideosAlbums:: result array already at end, skipping the rest");
       
   515             break; //break from for loop
       
   516             }
       
   517 
       
   518         media = mediaArray->AtL( i );
       
   519 
       
   520         // Skip items which were already failed (KErrAlreadyExists)
       
   521         if ( TVcxMyVideosCollectionUtil::Int32ValueL( *media ) != KErrAlreadyExists )
       
   522             {
       
   523             video.iRelationMdsId = aItemArray[j]->Id(); 
       
   524             MPX_DEBUG3( "CVcxMyVideosAlbums:: item result[%d] = %d (id)", j, video.iRelationMdsId );
       
   525             if ( video.iRelationMdsId == KNoId )
       
   526                 {
       
   527                 media->SetTObjectValueL<TInt>( KVcxMediaMyVideosInt32Value,
       
   528                         KErrGeneral );
       
   529                 }
       
   530             else
       
   531                 {
       
   532                 media->SetTObjectValueL<TInt>( KVcxMediaMyVideosInt32Value, KErrNone );
       
   533                 video.iMdsId = TVcxMyVideosCollectionUtil::IdL( *media ).iId1;
       
   534                 if ( album )
       
   535                     {
       
   536                     album->AddL( video );
       
   537                     if ( modifiedAlbums.Find( album ) == KErrNotFound )
       
   538                         {
       
   539                         modifiedAlbums.AppendL( album );
       
   540                         }
       
   541                     iCollection.iMessageList->AddEventL( TMPXItemId( albumId, KVcxMvcMediaTypeAlbum ),
       
   542                             EMPXItemModified, EVcxMyVideosVideoListOrderChanged );
       
   543                     }
       
   544                 }
       
   545             j++;
       
   546             }
       
   547         else
       
   548             {
       
   549             MPX_DEBUG2("CVcxMyVideosAlbums:: skipping already failed %d(KErrAlreadyExists) media array item",
       
   550                     TVcxMyVideosCollectionUtil::IdL( *media ).iId1 );
       
   551             }
       
   552         }
       
   553 
       
   554     TInt count = modifiedAlbums.Count();
       
   555     for ( TInt i = 0; i < count; i++ )
       
   556         {
       
   557         modifiedAlbums[i]->CalculateAttributesL();
       
   558         }
       
   559     
       
   560     CleanupStack::PopAndDestroy( &modifiedAlbums );
       
   561     
       
   562     iCollection.iActiveTask->Done();
       
   563     iCollection.iMessageList->SendL();
       
   564     
       
   565     MPX_DEBUG1("CVcxMyVideosAlbums::DoHandleAddVideosToAlbumResp() exit");
       
   566     }
       
   567 
       
   568 // ----------------------------------------------------------------------------
       
   569 // CVcxMyVideosAlbums::HandleRemoveRelationsResp
       
   570 // ----------------------------------------------------------------------------
       
   571 //
       
   572 void CVcxMyVideosAlbums::HandleRemoveRelationsResp( RArray<TUint32>& /*aRelationIds*/,
       
   573         RArray<TUint32>& aResults )
       
   574     {
       
   575     MPX_DEBUG1("CVcxMyVideosAlbums::HandleRemoveRelationsResp() start");
       
   576     
       
   577     // iRemoveFromAlbumVideos and mediaArray are in sync
       
   578     
       
   579     TRAP_IGNORE(
       
   580     
       
   581     CMPXMedia* cmd             = iCollection.iActiveTask->Command();
       
   582     CMPXMediaArray* mediaArray = TVcxMyVideosCollectionUtil::MediaArrayL( *cmd );
       
   583     TInt count                 = iRemoveFromAlbumVideos.Count();
       
   584     CMPXMedia* video;
       
   585     TInt relationId;
       
   586     for ( TInt i = 0; i < count; i++ )
       
   587         {
       
   588         video = mediaArray->AtL( i );
       
   589         relationId = iRemoveFromAlbumVideos[i].iRelationMdsId;
       
   590         if ( relationId != 0 && 
       
   591                 aResults.Find( relationId ) != KErrNotFound )
       
   592             {
       
   593             video->SetTObjectValueL( KVcxMediaMyVideosInt32Value, KErrNone );
       
   594             }
       
   595         else
       
   596             {
       
   597             video->SetTObjectValueL( KVcxMediaMyVideosInt32Value, KErrGeneral );
       
   598             }
       
   599         }
       
   600     
       
   601     );
       
   602     
       
   603     iRemoveFromAlbumVideos.Reset();
       
   604     
       
   605     iCollection.iActiveTask->Done();
       
   606 
       
   607     MPX_DEBUG1("CVcxMyVideosAlbums::HandleRemoveRelationsResp() exit");
       
   608     }
       
   609 
       
   610 // ----------------------------------------------------------------------------
       
   611 // CVcxMyVideosAlbums::HandleRemoveAlbumsResp
       
   612 // ----------------------------------------------------------------------------
       
   613 //
       
   614 void CVcxMyVideosAlbums::HandleRemoveAlbumsResp( CMPXMedia* aCmd,
       
   615         RArray<TUint32>& aResultIds )
       
   616     {    
       
   617     TRAP_IGNORE(
       
   618     
       
   619     CMPXMediaArray* mediaArray = TVcxMyVideosCollectionUtil::MediaArrayL( *aCmd );
       
   620     TInt count = mediaArray->Count();
       
   621     TUint32 mdsId;
       
   622     CMPXMedia* album;
       
   623     for ( TInt i = 0; i < count; i++ )
       
   624         {
       
   625         album = mediaArray->AtL( i );
       
   626         mdsId = TVcxMyVideosCollectionUtil::IdL( *album ).iId1;
       
   627         if ( aResultIds.Find( mdsId ) != KErrNotFound )
       
   628             {
       
   629             album->SetTObjectValueL( KVcxMediaMyVideosInt32Value, KErrNone );
       
   630             }
       
   631         else
       
   632             {
       
   633             album->SetTObjectValueL( KVcxMediaMyVideosInt32Value, KErrGeneral );
       
   634             }
       
   635         }
       
   636     
       
   637     );
       
   638     
       
   639     iCollection.iActiveTask->Done();
       
   640     }
       
   641 
       
   642 // ----------------------------------------------------------------------------
       
   643 // CVcxMyVideosAlbums::HandleRelationEvent
       
   644 // Only relation removed events are received here.
       
   645 // ----------------------------------------------------------------------------
       
   646 //
       
   647 void CVcxMyVideosAlbums::HandleRelationEvent( TObserverNotificationType /*aType*/,
       
   648         const RArray<TMdERelation>& aRelationArray )
       
   649     {    
       
   650     TRAP_IGNORE(
       
   651 
       
   652     RArray<CVcxMyVideosAlbum*> modifiedAlbums;
       
   653     modifiedAlbums.Reset();
       
   654     CleanupClosePushL( modifiedAlbums );
       
   655 
       
   656     TUint albumId;
       
   657     CVcxMyVideosAlbum* album;
       
   658     TInt count = aRelationArray.Count();
       
   659     
       
   660     
       
   661     for ( TInt i = 0; i < count; i++ )
       
   662         {
       
   663         albumId = aRelationArray[i].LeftObjectId();
       
   664         
       
   665         MPX_DEBUG3("CVcxMyVideosAlbums:: relation (%d,%d) deleted from MDS",
       
   666                 aRelationArray[i].LeftObjectId(), aRelationArray[i].RightObjectId() );
       
   667         
       
   668         if ( iAlbumListIsComplete )
       
   669             {
       
   670             album = Album( albumId );
       
   671             if ( album )
       
   672                 {
       
   673                 if ( modifiedAlbums.Find( album ) == KErrNotFound )
       
   674                     {
       
   675                     modifiedAlbums.AppendL( album );
       
   676                     }
       
   677                 album->Remove( aRelationArray[i].RightObjectId(), ETrue /* compress */ );
       
   678                 iCollection.iMessageList->AddEventL( TMPXItemId( albumId, KVcxMvcMediaTypeAlbum ),
       
   679                         EMPXItemModified, EVcxMyVideosVideoListOrderChanged );
       
   680                 }
       
   681             }
       
   682         else
       
   683             {
       
   684             //We are fetching albums already, we assume that it will be up to date.
       
   685             //If that is not the case, then we should set some flag here and restart
       
   686             //album fetching from scratch.
       
   687             }
       
   688         }
       
   689     
       
   690     count = modifiedAlbums.Count();
       
   691     for ( TInt i = 0; i < count; i++ )
       
   692         {
       
   693         modifiedAlbums[i]->CalculateAttributesL();
       
   694         }
       
   695     
       
   696     CleanupStack::PopAndDestroy( &modifiedAlbums );
       
   697     
       
   698     iCollection.iMessageList->SendL();
       
   699     
       
   700     );
       
   701     }
       
   702 
       
   703 //HandleRelationIdEvent
       
   704 
       
   705 // ----------------------------------------------------------------------------
       
   706 // CVcxMyVideosAlbums::UpdateAlbumL
       
   707 // Updates album attributes from aAlbum, if album is not found from memory,
       
   708 // nothing is done (no fetching from MDS).
       
   709 // This func is also called (by MDS modified event) when items are added or
       
   710 // removed from album.
       
   711 // ----------------------------------------------------------------------------
       
   712 //
       
   713 TBool CVcxMyVideosAlbums::UpdateAlbumL( const CMPXMedia& aAlbum )
       
   714     {
       
   715     TBool changed = EFalse;
       
   716     
       
   717     TMPXItemId mpxId = TVcxMyVideosCollectionUtil::IdL( aAlbum );
       
   718     CVcxMyVideosAlbum* album = Album( mpxId.iId1 );
       
   719 
       
   720     if ( album && album->iMedia )
       
   721         {
       
   722         CMPXMedia* media = album->iMedia;
       
   723 
       
   724         if ( media->IsSupported( KMPXMediaGeneralTitle ) )
       
   725             {
       
   726             TPtrC newTitle( TVcxMyVideosCollectionUtil::Title( aAlbum ) );
       
   727             TPtrC oldTitle( TVcxMyVideosCollectionUtil::Title( *media ) );
       
   728 
       
   729             if ( newTitle != oldTitle )
       
   730                 {
       
   731                 media->SetTextValueL( KMPXMediaGeneralTitle, newTitle );
       
   732                 iCollection.iMessageList->AddEventL( mpxId, EMPXItemModified );
       
   733                 changed = ETrue;
       
   734                 }
       
   735             }
       
   736         // calculate attributes, in case that videos were removed or added to this album
       
   737         album->CalculateAttributesL();
       
   738         }
       
   739         
       
   740     return changed;
       
   741     }
       
   742 
       
   743 // ----------------------------------------------------------------------------
       
   744 // CVcxMyVideosAlbums::NewVideoFlagChangedL
       
   745 // ----------------------------------------------------------------------------
       
   746 //
       
   747 void CVcxMyVideosAlbums::NewVideoFlagChangedL( TUint32 aMdsId )
       
   748     {    
       
   749     TInt count = iAlbums.Count();
       
   750     for ( TInt i = 0; i < count; i++ )
       
   751         {
       
   752         if ( iAlbums[i]->BelongsToAlbum( aMdsId ) )
       
   753             {
       
   754             iAlbums[i]->CalculateAttributesL();
       
   755             iCollection.iMessageList->AddEventL(
       
   756                     TMPXItemId( iAlbums[i]->iMdsId, KVcxMvcMediaTypeAlbum ),
       
   757                     EMPXItemModified, EVcxMyVideosListNoInfo );
       
   758             }
       
   759         }
       
   760     }
       
   761 
       
   762 // ----------------------------------------------------------------------------
       
   763 // CVcxMyVideosAlbums::VideoTitleChangedL
       
   764 // ----------------------------------------------------------------------------
       
   765 //
       
   766 void CVcxMyVideosAlbums::VideoTitleChangedL( TUint32 aMdsId )
       
   767     {    
       
   768     NewVideoFlagChangedL( aMdsId ); // same calculation works for this
       
   769     }
       
   770 
       
   771 // ----------------------------------------------------------------------------
       
   772 // CVcxMyVideosAlbums::VideoAddedOrRemovedFromCacheL
       
   773 // ----------------------------------------------------------------------------
       
   774 //
       
   775 void CVcxMyVideosAlbums::VideoAddedOrRemovedFromCacheL( CMPXMedia& aVideo )
       
   776     {
       
   777     TInt count = iAlbums.Count();
       
   778     TUint32 mdsId = TVcxMyVideosCollectionUtil::IdL( aVideo ).iId1;
       
   779     for ( TInt i = 0; i < count; i++ )
       
   780         {
       
   781         if ( iAlbums[i]->BelongsToAlbum( mdsId ) )
       
   782             {
       
   783             if ( iChangedAlbums.Find( i ) == KErrNotFound )
       
   784                 {
       
   785                 iChangedAlbums.AppendL( i );
       
   786                 }
       
   787             }
       
   788         }    
       
   789     }
       
   790 // ----------------------------------------------------------------------------
       
   791 // CVcxMyVideosAlbums::UpdateChangedAlbumsL
       
   792 // ----------------------------------------------------------------------------
       
   793 //
       
   794 void CVcxMyVideosAlbums::UpdateChangedAlbumsL()
       
   795     {
       
   796     TInt count = iChangedAlbums.Count();
       
   797     for ( TInt i = 0; i < count; i++ )
       
   798         {
       
   799         iCollection.iMessageList->AddEventL(
       
   800                 TMPXItemId( iAlbums[iChangedAlbums[i]]->iMdsId, KVcxMvcMediaTypeAlbum ),
       
   801                 EMPXItemModified, EVcxMyVideosVideoListOrderChanged );
       
   802         iAlbums[iChangedAlbums[i]]->CalculateAttributesL();
       
   803         }
       
   804     iChangedAlbums.Reset();
       
   805     }
       
   806 // END OF FILE