videocollection/mpxmyvideoscollection/src/vcxmyvideosvideocache.cpp
changeset 0 96612d01cf9f
child 1 6711b85517b7
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2008 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:    Video list cache. Contains cached data from MDS.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <mpxlog.h>
       
    22 #include <mpxmedia.h>
       
    23 #include <mpxmediaarray.h>
       
    24 #include <mpxmediageneraldefs.h>
       
    25 #include <mpxmediageneralextdefs.h>
       
    26 #include <mpxmediacontainerdefs.h>
       
    27 #include <vcxmyvideosdefs.h>
       
    28 #include <centralrepository.h>
       
    29 #include <collate.h>
       
    30 #include <mpxmediavideodefs.h>
       
    31 #include "vcxmyvideosvideocache.h"
       
    32 #include "vcxmyvideoscollectionplugin.h"
       
    33 #include "vcxmyvideosdownloadutil.h"
       
    34 #include "vcxmyvideoscollectionutil.h"
       
    35 #include "vcxmyvideoscategories.h"
       
    36 #include "vcxmyvideosdownloadcache.h"
       
    37 #include "vcxmyvideosmessagelist.h"
       
    38 #include "vcxmyvideosopenhandler.h"
       
    39 
       
    40 //       If video list fetching is going on and at the same time add/delete events
       
    41 //       arrive from mds, what to do?
       
    42 
       
    43 // ============================ MEMBER FUNCTIONS ==============================
       
    44 
       
    45 // ----------------------------------------------------------------------------
       
    46 // Two-phased constructor.
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 CVcxMyVideosVideoCache* CVcxMyVideosVideoCache::NewL(
       
    50     CVcxMyVideosCollectionPlugin& aMyVideosCollectionPlugin )
       
    51     {
       
    52     MPX_FUNC("CVcxMyVideosCollectionCache::NewL");
       
    53 
       
    54     CVcxMyVideosVideoCache* self = new (ELeave) CVcxMyVideosVideoCache( aMyVideosCollectionPlugin );
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop(self);
       
    58     return self;
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // Destructor.
       
    63 // ----------------------------------------------------------------------------
       
    64 //
       
    65 CVcxMyVideosVideoCache::~CVcxMyVideosVideoCache()
       
    66     {
       
    67     MPX_FUNC("CVcxMyVideosVideoCache::~CVcxMyVideosVideoCache");
       
    68     
       
    69     delete iVideoList;
       
    70     delete iDownloadCache;
       
    71     DeletePartialList();
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // Constructor.
       
    76 // ----------------------------------------------------------------------------
       
    77 //
       
    78 CVcxMyVideosVideoCache::CVcxMyVideosVideoCache(
       
    79         CVcxMyVideosCollectionPlugin& aMyVideosCollectionPlugin )
       
    80 : iCollection( aMyVideosCollectionPlugin )
       
    81     {
       
    82     MPX_FUNC("CVcxMyVideosVideoCache::CVcxMyVideosVideoCache");
       
    83     }
       
    84 
       
    85 // ----------------------------------------------------------------------------
       
    86 // Symbian 2nd phase constructor can leave.
       
    87 // ----------------------------------------------------------------------------
       
    88 //
       
    89 void CVcxMyVideosVideoCache::ConstructL ()
       
    90     {
       
    91     MPX_FUNC("CVcxMyVideosVideoCache::ConstructL");
       
    92 
       
    93     iVideoList          = TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
    94     iVideoListIsPartial = ETrue;
       
    95 
       
    96     iDownloadCache = CVcxMyVideosDownloadCache::NewL();
       
    97     iPartialVideoList.Reset();
       
    98     }
       
    99 
       
   100 // ----------------------------------------------------------------------------------------------------------
       
   101 // CVcxMyVideosVideoCache::CreateVideoListByOriginL
       
   102 // ----------------------------------------------------------------------------------------------------------
       
   103 //
       
   104 CMPXMedia* CVcxMyVideosVideoCache::CreateVideoListByOriginL( TUint8 aOrigin )
       
   105     {
       
   106     MPX_FUNC("CVcxMyVideosVideoCache::CreateVideoListByOriginL()");
       
   107     
       
   108     CMPXMediaArray* allVideosArray = NULL;
       
   109     TInt allVideosArrayCount       = 0;
       
   110 
       
   111     if ( aOrigin == EVcxMyVideosOriginSideLoaded )
       
   112         {
       
   113         aOrigin = EVcxMyVideosOriginOther;
       
   114         }
       
   115             
       
   116     allVideosArray = iVideoList->Value<CMPXMediaArray>(
       
   117             KMPXMediaArrayContents);
       
   118 
       
   119     allVideosArrayCount = allVideosArray->Count();
       
   120 
       
   121     MPX_DEBUG2("CVcxMyVideosVideoCache:: iVideoList count = %d", allVideosArrayCount );
       
   122     
       
   123     CMPXMedia* filteredVideos =
       
   124             TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
   125     CleanupStack::PushL( filteredVideos ); // 1->
       
   126                         
       
   127     CMPXMediaArray* filteredVideosArray = filteredVideos->Value<CMPXMediaArray>(
       
   128             KMPXMediaArrayContents );
       
   129 
       
   130     CMPXMedia* media = NULL;
       
   131     
       
   132     for( TInt i = 0; i < allVideosArrayCount; i++ )
       
   133         {
       
   134         media = (*allVideosArray)[i];
       
   135 
       
   136         TUint8 mediaOrigin = EVcxMyVideosOriginOther;
       
   137 
       
   138         if ( media->IsSupported( KVcxMediaMyVideosOrigin ) )
       
   139             {
       
   140             mediaOrigin = *(media->Value<TUint8>( KVcxMediaMyVideosOrigin ));
       
   141             
       
   142             if ( mediaOrigin == EVcxMyVideosOriginSideLoaded )
       
   143                 {
       
   144                 mediaOrigin = EVcxMyVideosOriginOther;
       
   145                 }
       
   146             }
       
   147             
       
   148         if ( mediaOrigin == aOrigin )
       
   149             {
       
   150             filteredVideosArray->AppendL( *media );
       
   151             }
       
   152         }
       
   153     
       
   154     filteredVideos->SetTObjectValueL( KMPXMediaArrayCount, filteredVideosArray->Count() );
       
   155     
       
   156     CleanupStack::Pop( filteredVideos );  // <-1
       
   157     return filteredVideos;
       
   158     }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CVcxMyVideosVideoCache::AppendToListL
       
   162 // ----------------------------------------------------------------------------
       
   163 //
       
   164 void CVcxMyVideosVideoCache::AppendToListL( CMPXMedia& aToList, CMPXMedia& aFromList,
       
   165         TUint8 aOrigin, TInt aNewItemsStartIndex )
       
   166     {
       
   167     MPX_FUNC("CVcxMyVideosVideoCache::AppendToListL()");
       
   168     
       
   169     if ( aNewItemsStartIndex < 0 )
       
   170         {
       
   171         MPX_DEBUG1("CVcxMyVideosVideoCache:: aNewItemsStartIndex < 0 -> skipping");
       
   172         return;
       
   173         }
       
   174         
       
   175     CMPXMediaArray* toArray = aToList.Value<CMPXMediaArray>(
       
   176             KMPXMediaArrayContents );
       
   177 
       
   178     CMPXMediaArray* fromArray = aFromList.Value<CMPXMediaArray>(
       
   179             KMPXMediaArrayContents );
       
   180 
       
   181     TInt fromCount = fromArray->Count();
       
   182     TInt origin;
       
   183     CMPXMedia* media;
       
   184     for ( TInt i = aNewItemsStartIndex; i < fromCount; i++ )
       
   185         {
       
   186         media  = fromArray->AtL( i );
       
   187         origin = TVcxMyVideosCollectionUtil::OriginL( *media );
       
   188         if ( origin == aOrigin )
       
   189             {
       
   190             toArray->AppendL( *media );
       
   191             } 
       
   192         }
       
   193     }
       
   194     
       
   195 // ----------------------------------------------------------------------------
       
   196 // CVcxMyVideosVideoCache::FindVideoByUriL
       
   197 // ----------------------------------------------------------------------------
       
   198 //
       
   199 CMPXMedia* CVcxMyVideosVideoCache::FindVideoByUriL( const TDesC& aUri )
       
   200     {    
       
   201     CMPXMediaArray* cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
   202                                 KMPXMediaArrayContents);
       
   203 
       
   204     for ( TInt i = 0; i < cachedVideoArray->Count(); i++ )
       
   205         {       
       
   206         if ( (*cachedVideoArray)[i]->ValueText( KVcxMediaMyVideosRemoteUrl ) == aUri )
       
   207             {
       
   208             return (*cachedVideoArray)[i];
       
   209             }
       
   210         }
       
   211     return NULL;
       
   212     }
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 // CVcxMyVideosVideoCache::FindVideoByMdsIdL
       
   216 // ----------------------------------------------------------------------------
       
   217 //
       
   218 CMPXMedia* CVcxMyVideosVideoCache::FindVideoByMdsIdL( TUint32 aMdsId, TInt& aPos )
       
   219     {
       
   220     //MPX_FUNC("CVcxMyVideosVideoCache::FindVideoByMdsIdL()");
       
   221     
       
   222     //MPX_DEBUG2("CVcxMyVideosVideoCache:: looking for %d MDS ID from cache", aMdsId);
       
   223     
       
   224     aPos = KErrNotFound;
       
   225     
       
   226     if ( !iVideoList )
       
   227         {
       
   228         return NULL;
       
   229         }
       
   230         
       
   231     CMPXMediaArray* cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
   232                                 KMPXMediaArrayContents);
       
   233    
       
   234     TMPXItemId mpxItemId;
       
   235     CMPXMedia* media;
       
   236     TInt count = cachedVideoArray->Count();
       
   237     for ( TInt i = 0; i < count; i++ )
       
   238         {
       
   239         media = cachedVideoArray->AtL( i );
       
   240         if ( media->IsSupported( KMPXMediaGeneralId ))
       
   241             {
       
   242             mpxItemId = media->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId );
       
   243             if ( mpxItemId.iId1 == aMdsId && mpxItemId.iId2 == 0 )
       
   244                 {
       
   245                 MPX_DEBUG2("CVcxMyVideosVideoCache::FindVideoByMdsIdL() found %d from iVideoList", aMdsId );
       
   246                 aPos = i;
       
   247                 return media;
       
   248                 }
       
   249             }
       
   250         }
       
   251 
       
   252     count = iPartialVideoList.Count();
       
   253     TMPXItemId mpxId;  
       
   254     for ( TInt i = 0; i < count; i++ )
       
   255         {
       
   256         mpxId = TVcxMyVideosCollectionUtil::IdL( *(iPartialVideoList[i]) );
       
   257         if ( aMdsId ==  mpxId.iId1 )
       
   258             {
       
   259             MPX_DEBUG2("CVcxMyVideosVideoCache::FindVideoByMdsIdL() found %d from iPartialVideoList", aMdsId );
       
   260             MPX_DEBUG1("CVcxMyVideosVideoCache::FindVideoByMdsIdL() returning the pointer and setting aPos to KErrNotFound");
       
   261             aPos = KErrNotFound; // this indicates that not found from iVideoList
       
   262             return iPartialVideoList[i];
       
   263             }       
       
   264         }
       
   265 
       
   266     MPX_DEBUG2("CVcxMyVideosCollectionPlugin::FindVideoByMdsIdL() MDSID %d NOT FOUND", aMdsId);
       
   267     return NULL;
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // CVcxMyVideosVideoCache::FindVideoByDownloadIdL
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 CMPXMedia* CVcxMyVideosVideoCache::FindVideoByDownloadIdL( TUint aDownloadId )
       
   275     {
       
   276     MPX_FUNC("CVcxMyVideosVideoCache::FindVideoByDownloadIdL()");
       
   277     
       
   278     MPX_DEBUG2("CVcxMyVideosVideoCache:: looking for MPX item  with download ID %d from cache", aDownloadId);
       
   279         
       
   280     if ( aDownloadId == 0 )
       
   281         {
       
   282         MPX_DEBUG1("CVcxMyVideosVideoCache:: dl id == 0 -> NOT FOUND");
       
   283         return NULL;
       
   284         }
       
   285     
       
   286     if ( !iVideoList )
       
   287         {
       
   288         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList is NULL -> NOT FOUND");
       
   289         return NULL;
       
   290         }
       
   291 
       
   292     CMPXMedia* video = iDownloadCache->Get( aDownloadId );
       
   293     if ( video )
       
   294         {
       
   295         MPX_DEBUG1("CVcxMyVideosVideoCache:: found from Download Cache");
       
   296         return video;
       
   297         }
       
   298 
       
   299     MPX_DEBUG1("CVcxMyVideosVideoCache:: not found from Download Cache");
       
   300 
       
   301     CMPXMediaArray* cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
   302                                 KMPXMediaArrayContents);
       
   303               
       
   304     TUint32 downloadId;
       
   305     TInt count = cachedVideoArray->Count();
       
   306     for ( TInt i = 0; i < count; i++ )
       
   307         {
       
   308         downloadId = TVcxMyVideosCollectionUtil::DownloadIdL( *cachedVideoArray->AtL( i ) );
       
   309         if ( downloadId == aDownloadId )
       
   310             {
       
   311             MPX_DEBUG1("CVcxMyVideosVideoCache:: found from iVideoList");
       
   312             MPX_DEBUG1("CVcxMyVideosVideoCache:: adding to iDownloadCache");
       
   313             iDownloadCache->Add( downloadId, (*cachedVideoArray)[i] );
       
   314             return (*cachedVideoArray)[i];
       
   315             }
       
   316         }
       
   317     
       
   318     count = iPartialVideoList.Count();  
       
   319     for ( TInt i = 0; i < count; i++ )
       
   320         {
       
   321         downloadId = TVcxMyVideosCollectionUtil::DownloadIdL( *(iPartialVideoList[i]) );
       
   322         if ( downloadId ==  aDownloadId )
       
   323             {
       
   324             MPX_DEBUG1("CVcxMyVideosVideoCache:: found from iPartialVideoList");
       
   325             MPX_DEBUG1("CVcxMyVideosVideoCache:: adding to iDownloadCache");
       
   326             iDownloadCache->Add( downloadId, iPartialVideoList[i] );
       
   327             return iPartialVideoList[i];
       
   328             }       
       
   329         }
       
   330         
       
   331     MPX_DEBUG1("CVcxMyVideosVideoCache:: NOT FOUND");
       
   332     return NULL;
       
   333     }
       
   334 
       
   335 
       
   336 // ----------------------------------------------------------------------------
       
   337 // CVcxMyVideosVideoCache::GetVideosL
       
   338 // ----------------------------------------------------------------------------
       
   339 //
       
   340 CMPXMedia* CVcxMyVideosVideoCache::GetVideosL( RArray<TUint32> aMdsIds )
       
   341     {
       
   342     CMPXMessage* videoList = TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
   343     CleanupStack::PushL( videoList ); // 1->
       
   344 
       
   345     // copy from iVideoList
       
   346     TVcxMyVideosCollectionUtil::CopyFromListToListL(
       
   347             *iVideoList, *videoList, aMdsIds );
       
   348 
       
   349     // copy from iPartialVideoList
       
   350     TVcxMyVideosCollectionUtil::CopyFromListToListL(
       
   351             iPartialVideoList, *videoList, aMdsIds );
       
   352     
       
   353     CleanupStack::Pop( videoList ); // <-1
       
   354     return videoList;
       
   355     }
       
   356     
       
   357 // ----------------------------------------------------------------------------
       
   358 // CVcxMyVideosVideoCache::AddVideosFromMdsL
       
   359 // Called when item inserted events arrive from mds or from
       
   360 // KVcxCommandMyVideosGetMediasByMpxId cmd handler.
       
   361 // Synchronizing with downloads is not done here, caller must take care of it.
       
   362 // If mpx item is already in cache, or MDS does not contain the item (or was not video item),
       
   363 // then it is removed from aMdsIds. Ie after this function call aMdsIds contains
       
   364 // only IDs which are actually added to cache.
       
   365 // ----------------------------------------------------------------------------
       
   366 //    
       
   367 void CVcxMyVideosVideoCache::AddVideosFromMdsL( RArray<TUint32>& aMdsIds,
       
   368         TBool& aListFetchingWasCanceled )
       
   369     {
       
   370     MPX_FUNC("CVcxMyVideosVideoCache::AddVideosFromMdsL");
       
   371 
       
   372     TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
   373   
       
   374     aListFetchingWasCanceled = EFalse;
       
   375 
       
   376     MPX_DEBUG2("CVcxMyVideosVideoCache:: adding %d items", aMdsIds.Count());
       
   377 
       
   378     for ( TInt j = aMdsIds.Count()-1; j >= 0 ; j-- )
       
   379         {
       
   380         CMPXMedia* videoToCache;
       
   381         TInt pos;
       
   382         if ( !FindVideoByMdsIdL( aMdsIds[j], pos ) )
       
   383             {
       
   384             MPX_DEBUG2("CVcxMyVideosVideoCache:: MDSID(%d) not found from cache, ok", aMdsIds[j]);
       
   385             
       
   386             if ( iCollection.iMyVideosMdsDb->iVideoListFetchingIsOngoing )
       
   387                 {
       
   388                 // If list fetching is not canceled, CreateVideoObjectL will leave with KErrNotReady.
       
   389                 MPX_DEBUG1("CVcxMyVideosVideoCache:: video list fetching is ongoing, canceling it to be able to fetch single items!");
       
   390                 iCollection.iMyVideosMdsDb->Cancel();
       
   391                 aListFetchingWasCanceled = ETrue;
       
   392                 }
       
   393 
       
   394             videoToCache = iCollection.iMyVideosMdsDb->CreateVideoL( 
       
   395                     aMdsIds[j], EFalse /* brief details */);
       
   396                                 
       
   397             if ( videoToCache )
       
   398                 {
       
   399                 CleanupStack::PushL( videoToCache ); // 1->
       
   400 
       
   401                 TInt err = AddL( videoToCache, sortingOrder );
       
   402                 
       
   403                 if ( err == KErrNone )
       
   404                     {
       
   405                     CleanupStack::Pop( videoToCache ); // <-1
       
   406                     }
       
   407                 else
       
   408                     {
       
   409                     CleanupStack::PopAndDestroy( videoToCache ); // <-1
       
   410                     }
       
   411                     
       
   412                 MPX_DEBUG3("CVcxMyVideosVideoCache:: %d (pointer = %x) added to cache", aMdsIds[j], videoToCache);
       
   413                 }
       
   414             else
       
   415                 {
       
   416                 MPX_DEBUG2("CVcxMyVideosVideoCache:: video with %d ID not found from MDS -> skipping add to cache and deleting from id array", aMdsIds[j] );
       
   417                 aMdsIds.Remove( j );
       
   418                 }
       
   419             }
       
   420         else
       
   421             {
       
   422             MPX_DEBUG2("CVcxMyVideosVideoCache:: %d was already in cache -> skipping add, and deleting from id array", aMdsIds[j]);
       
   423             aMdsIds.Remove( j );
       
   424             }
       
   425         }
       
   426 
       
   427     }
       
   428 
       
   429 // ----------------------------------------------------------------------------
       
   430 // CVcxMyVideosVideoCache::AddL()
       
   431 // This is for single adds, video list fetching does not call this.
       
   432 // ----------------------------------------------------------------------------
       
   433 //
       
   434 TInt CVcxMyVideosVideoCache::AddL( CMPXMedia* aVideo, TVcxMyVideosSortingOrder aSortingOrder )
       
   435     {
       
   436     MPX_FUNC("CVcxMyVideosVideoCache::AddL");
       
   437     
       
   438     if ( iVideoListIsPartial )
       
   439         {
       
   440         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList is partial, adding to iPartialVideoList");
       
   441         return AddToPartialListL( aVideo );
       
   442         }
       
   443     else
       
   444         {
       
   445         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList is complete, adding to iVideoList");
       
   446         return AddToCorrectPlaceL( *aVideo, aSortingOrder );
       
   447         }
       
   448     }
       
   449 
       
   450 // ----------------------------------------------------------------------------
       
   451 // CVcxMyVideosVideoCache::CheckForPartialVideoListItemsL()
       
   452 // This is called by iCollection during video list fetching.
       
   453 // ----------------------------------------------------------------------------
       
   454 //
       
   455 void CVcxMyVideosVideoCache::CheckForPartialVideoListItemsL( TInt aNewItemsStartIndex )
       
   456     {
       
   457     // Replace items in iVideoList with iPartialVideoList items.
       
   458     if ( !iVideoList || !iVideoList->IsSupported( KMPXMediaArrayContents ) )
       
   459         {
       
   460         return;
       
   461         }
       
   462         
       
   463     CMPXMediaArray* videoArray = iVideoList->Value<CMPXMediaArray>(
       
   464                                 KMPXMediaArrayContents);
       
   465     
       
   466     TInt videoArrayCount = videoArray->Count();
       
   467 
       
   468     MPX_DEBUG2("  videoArrayCount           = %d", videoArrayCount);
       
   469     MPX_DEBUG2("  iPartialVideoList.Count() = %d", iPartialVideoList.Count());
       
   470     
       
   471     for ( TInt i = iPartialVideoList.Count()-1; i >= 0 ; i-- )
       
   472         {
       
   473         for ( TInt j = aNewItemsStartIndex; j < videoArrayCount; j++ )
       
   474             {
       
   475             if ( TVcxMyVideosCollectionUtil::IdL( *(iPartialVideoList[i]) )
       
   476                     == TVcxMyVideosCollectionUtil::IdL( *(videoArray->AtL( j )) ) )
       
   477                 {
       
   478                 // Replace item in iVideoList with item from iPartialVideoList
       
   479                 MPX_DEBUG2( "CVcxMyVideosVideoCache:: replacing %d, in iVideoList with item from iPartialVideoList",
       
   480                         TVcxMyVideosCollectionUtil::IdL( *(iPartialVideoList[i]) ).iId1 );
       
   481 
       
   482                 if ( j == videoArray->Count() -1 )
       
   483                     {
       
   484                     videoArray->Remove( j );
       
   485                     videoArray->AppendL( iPartialVideoList[i] );
       
   486                     }
       
   487                 else
       
   488                     {
       
   489                     videoArray->Remove( j );
       
   490                     videoArray->Insert( iPartialVideoList[i], j );
       
   491                     }
       
   492                 iPartialVideoList.Remove( i );
       
   493                 iPartialVideoList.Compress();                    
       
   494                 break; // found -> break from for loop
       
   495                 }
       
   496             }
       
   497         }    
       
   498     }
       
   499 
       
   500 // ----------------------------------------------------------------------------
       
   501 // CVcxMyVideosVideoCache::PosOnPartialVideoListL()
       
   502 // ----------------------------------------------------------------------------
       
   503 //
       
   504 TInt CVcxMyVideosVideoCache::PosOnPartialVideoListL( CMPXMedia& aVideo )
       
   505     {
       
   506     TInt count    = iPartialVideoList.Count();
       
   507     TUint32 mdsId = TVcxMyVideosCollectionUtil::IdL( aVideo );
       
   508     
       
   509     for ( TInt i = 0; i < count; i++ )
       
   510         {
       
   511         if ( TVcxMyVideosCollectionUtil::IdL( *(iPartialVideoList[i]) ).iId1 == mdsId )
       
   512             {
       
   513             return i;
       
   514             }        
       
   515         }
       
   516     return KErrNotFound;
       
   517     }
       
   518 
       
   519 // ----------------------------------------------------------------------------
       
   520 // CVcxMyVideosVideoCache::PosOnVideoListL()
       
   521 // ----------------------------------------------------------------------------
       
   522 //
       
   523 TInt CVcxMyVideosVideoCache::PosOnVideoListL( CMPXMedia& aVideo )
       
   524     {
       
   525     CMPXMediaArray* videoArray = iVideoList->Value<CMPXMediaArray>(
       
   526             KMPXMediaArrayContents);
       
   527 
       
   528     TInt count    = videoArray->Count();
       
   529     TUint32 mdsId = TVcxMyVideosCollectionUtil::IdL( aVideo );
       
   530     
       
   531     for ( TInt i = 0; i < count; i++ )
       
   532         {
       
   533         if ( TVcxMyVideosCollectionUtil::IdL( *(videoArray->AtL( i )) ).iId1 == mdsId )
       
   534             {
       
   535             return i;
       
   536             }        
       
   537         }
       
   538     return KErrNotFound;
       
   539     }
       
   540     
       
   541 // ----------------------------------------------------------------------------
       
   542 // CVcxMyVideosVideoCache::AddToPartialListL()
       
   543 // ----------------------------------------------------------------------------
       
   544 //
       
   545 TInt CVcxMyVideosVideoCache::AddToPartialListL( CMPXMedia* aVideo )
       
   546     {
       
   547     TInt pos = PosOnPartialVideoListL( *aVideo );
       
   548      
       
   549     TInt result;
       
   550     if ( pos == KErrNotFound )
       
   551         {
       
   552         iPartialVideoList.Append( aVideo );
       
   553         result = KErrNone;
       
   554         }
       
   555     else
       
   556         {
       
   557         result = KErrAlreadyExists;
       
   558         }
       
   559     return result;
       
   560     }
       
   561 
       
   562 // ----------------------------------------------------------------------------
       
   563 // CVcxMyVideosVideoCache::DeletePartialList()
       
   564 // ----------------------------------------------------------------------------
       
   565 //
       
   566 void CVcxMyVideosVideoCache::DeletePartialList()
       
   567     {
       
   568     TInt count( iPartialVideoList.Count() );
       
   569     for ( TInt i = count -1; i >= 0; i-- )
       
   570         {
       
   571         delete iPartialVideoList[i];
       
   572         iPartialVideoList[i] = NULL;
       
   573         }
       
   574     iPartialVideoList.Close();
       
   575     }
       
   576 
       
   577 // ----------------------------------------------------------------------------
       
   578 // CVcxMyVideosVideoCache::CreateVideoListL
       
   579 // ----------------------------------------------------------------------------
       
   580 //
       
   581 void CVcxMyVideosVideoCache::CreateVideoListL( TBool aForce )
       
   582     {
       
   583     MPX_FUNC("CVcxMyVideosVideoCache::CreateVideoListL");
       
   584 
       
   585     MPX_DEBUG2("CVcxMyVideosVideoCache:: aForce = %d", aForce );
       
   586     
       
   587     TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
   588         
       
   589     if ( iCollection.iMyVideosMdsDb->iVideoListFetchingIsOngoing
       
   590             && sortingOrder == iLastSortingOrder && !aForce )
       
   591         {
       
   592         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList creation is already ongoing, skipping");
       
   593         return;
       
   594         }
       
   595                    
       
   596     if ( iVideoListIsPartial || (sortingOrder != iLastSortingOrder) || aForce )
       
   597         {
       
   598         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList was partial or in wrong order or aForce was ETrue, recreating");
       
   599 
       
   600         if ( iCollection.iMyVideosMdsDb->iVideoListFetchingIsOngoing )
       
   601             {
       
   602             MPX_DEBUG1("CVcxMyVideosVideoCache:: video list fetching is ongoing, canceling it");
       
   603             iCollection.iMyVideosMdsDb->Cancel();
       
   604             }
       
   605         
       
   606         ResetVideoListL();
       
   607                         
       
   608         TBool ascending = ETrue;
       
   609         
       
   610         switch ( sortingOrder )
       
   611             {
       
   612             case EVcxMyVideosSortingId:
       
   613             case EVcxMyVideosSortingName:
       
   614             case EVcxMyVideosSortingSize:
       
   615                 ascending = ETrue;
       
   616                 break;
       
   617             case EVcxMyVideosSortingCreationDate:
       
   618             case EVcxMyVideosSortingModified:
       
   619                 ascending = EFalse;
       
   620                 break;
       
   621             }
       
   622             
       
   623         iCollection.iMyVideosMdsDb->CreateVideoListL( 
       
   624                 sortingOrder,
       
   625                 ascending,
       
   626                 EFalse /* brief list */,
       
   627                 iVideoList /* use existing */ );
       
   628 
       
   629         iLastSortingOrder   = sortingOrder;
       
   630         iVideoListIsPartial = ETrue;
       
   631         }
       
   632     else
       
   633         {
       
   634         MPX_DEBUG1("CVcxMyVideosVideoCache:: iVideoList is complete and in right sorting order, doing nothing");
       
   635         }              
       
   636     }
       
   637 
       
   638 // ----------------------------------------------------------------------------
       
   639 // CVcxMyVideosVideoCache::UpdateVideoL
       
   640 // ----------------------------------------------------------------------------
       
   641 //
       
   642 TBool CVcxMyVideosVideoCache::UpdateVideoL( CMPXMedia& aVideo )
       
   643     {
       
   644     MPX_FUNC("CVcxMyVideosVideoCache::UpdateVideoL");
       
   645     
       
   646     TInt pos;
       
   647     CMPXMedia* videoInCache = FindVideoByMdsIdL( aVideo.ValueTObjectL<TMPXItemId>(
       
   648             KMPXMediaGeneralId ).iId1, pos );
       
   649     if ( !videoInCache )
       
   650         {
       
   651         MPX_DEBUG2("CVcxMyVideosVideoCache:: MDS ID %d not found from cache, leaving",
       
   652             aVideo.ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ).iId1 );
       
   653         User::Leave( KErrNotFound );
       
   654         }
       
   655 
       
   656     TBool changed = EFalse;
       
   657         
       
   658     // 1 MPX ID cant be changed
       
   659     // 2 
       
   660     if ( aVideo.IsSupported( KMPXMediaGeneralTitle ) )
       
   661         {
       
   662         if ( videoInCache->ValueText( KMPXMediaGeneralTitle ) !=
       
   663                 aVideo.ValueText( KMPXMediaGeneralTitle ) )
       
   664             {
       
   665             videoInCache->SetTextValueL( KMPXMediaGeneralTitle,
       
   666                     aVideo.ValueText( KMPXMediaGeneralTitle ) );
       
   667 
       
   668             //updates sorting order and category attributes if necessarry
       
   669             HandleVideoTitleModifiedL( videoInCache );
       
   670 
       
   671             changed = ETrue;
       
   672             }
       
   673         }
       
   674     // 3
       
   675     if ( aVideo.IsSupported( KMPXMediaGeneralComment ) )
       
   676         {
       
   677         if ( videoInCache->ValueText( KMPXMediaGeneralComment ) != 
       
   678                 aVideo.ValueText( KMPXMediaGeneralComment ) )
       
   679             {
       
   680             videoInCache->SetTextValueL( KMPXMediaGeneralComment,
       
   681                     aVideo.ValueText( KMPXMediaGeneralComment ) );
       
   682             changed = ETrue;
       
   683             }
       
   684         }
       
   685     // 4
       
   686     if ( aVideo.IsSupported( KMPXMediaGeneralUri ) )
       
   687         {
       
   688         MPX_DEBUG2("CVcxMyVideosVideoCache:: old file path: %S", &videoInCache->ValueText( KMPXMediaGeneralUri ));
       
   689         MPX_DEBUG2("CVcxMyVideosVideoCache:: new file path: %S", &aVideo.ValueText( KMPXMediaGeneralUri ));
       
   690 
       
   691         if ( videoInCache->ValueText( KMPXMediaGeneralUri ) !=
       
   692                aVideo.ValueText( KMPXMediaGeneralUri ) ) 
       
   693             {
       
   694 #if 0 // this should be in mds, or mds should keep title empty if it is not set by client
       
   695             TParse parse;
       
   696             HBufC* oldTitle = HBufC::NewL( videoInCache->ValueText( KMPXMediaGeneralTitle ).Length() );
       
   697             CleanupStack::PushL( oldTitle ); // 1->
       
   698             oldTitle->Des() = videoInCache->ValueText( KMPXMediaGeneralTitle );
       
   699             oldTitle->Des().LowerCase();
       
   700             
       
   701             parse.Set( videoInCache->ValueText( KMPXMediaGeneralUri ), NULL, NULL );
       
   702             MPX_DEBUG2("CVcxMyVideosVideoCache:: title generated from old file name: %S", &parse.Name());
       
   703             MPX_DEBUG2("CVcxMyVideosVideoCache:: old title (lower cased): %S", oldTitle);
       
   704             if ( parse.Name() == *oldTitle )
       
   705                 {
       
   706                 // filename has been used as a title -> update title also to new filename
       
   707                 MPX_DEBUG1("CVcxMyVideosVideoCache:: filename has been used as a title -> changing title to new filename");
       
   708                 parse.Set( aVideo.ValueText( KMPXMediaGeneralUri ), NULL, NULL );
       
   709                 videoInCache->SetTextValueL( KMPXMediaGeneralTitle, parse.Name() );
       
   710                 
       
   711                 //updates sorting order and category attributes if necessarry
       
   712                 HandleVideoTitleModifiedL( videoInCache );
       
   713                 }
       
   714             CleanupStack::PopAndDestroy( oldTitle ); // <-1
       
   715 #endif
       
   716             
       
   717             videoInCache->SetTextValueL( KMPXMediaGeneralUri,
       
   718                     aVideo.ValueText( KMPXMediaGeneralUri ) );
       
   719     
       
   720             changed = ETrue;
       
   721             }
       
   722         }
       
   723     // 5
       
   724 	// With 64bit file support this is obsolete.
       
   725 	// Can be removed if this attribute is removed from MPX framework code
       
   726     if ( aVideo.IsSupported( KMPXMediaGeneralSize ) )
       
   727         {
       
   728         if ( aVideo.ValueTObjectL<TInt>( KMPXMediaGeneralSize ) !=
       
   729                 videoInCache->ValueTObjectL<TInt>( KMPXMediaGeneralSize ) )
       
   730             {
       
   731             videoInCache->SetTObjectValueL<TInt>( KMPXMediaGeneralSize,
       
   732                     aVideo.ValueTObjectL<TInt>( KMPXMediaGeneralSize ) );
       
   733 
       
   734 #ifndef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   735             TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
   736             if ( sortingOrder == EVcxMyVideosSortingSize )
       
   737                 {
       
   738                 MPX_DEBUG1("CVcxMyVideosVideoCache:: size updated && sorting order is by size, moving video to correct place on cache");
       
   739                 MoveToCorrectPlaceL( *videoInCache, sortingOrder );
       
   740                 videoInCache = FindVideoByMdsIdL( aVideo.ValueTObjectL<TMPXItemId>(
       
   741                         KMPXMediaGeneralId ).iId1, pos );
       
   742 
       
   743                 iCollection.CategoriesL().GenerateCategoryModifiedEventL(
       
   744                         *videoInCache, ETrue /* flush */, EVcxMyVideosVideoListOrderChanged );
       
   745                 }
       
   746 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   747             changed = ETrue;
       
   748             }
       
   749         }
       
   750 
       
   751     // 6 Creation date changes values when item is moved from one memory to another.
       
   752     //   The first the object is created to MDS with current date, and when file is added
       
   753     //   to file system the value is changed to files creation date.
       
   754     if ( aVideo.IsSupported( KMPXMediaGeneralDate ) )
       
   755         {
       
   756         TInt64 newCreationDate;
       
   757         TInt64 oldCreationDate;
       
   758         newCreationDate = aVideo.ValueTObjectL<TInt64>( KMPXMediaGeneralDate );
       
   759         oldCreationDate = videoInCache->ValueTObjectL<TInt64>( KMPXMediaGeneralDate );
       
   760         if ( newCreationDate != oldCreationDate )
       
   761             {
       
   762             videoInCache->SetTObjectValueL<TInt64>( KMPXMediaGeneralDate,
       
   763                 newCreationDate );
       
   764 
       
   765             TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
   766             if ( sortingOrder == EVcxMyVideosSortingCreationDate )
       
   767                 {
       
   768                 MPX_DEBUG1("CVcxMyVideosVideoCache:: creation date updated && sorting order is by creation date, moving video to correct place on cache");
       
   769                 MoveToCorrectPlaceL( *videoInCache, sortingOrder );
       
   770                 videoInCache = FindVideoByMdsIdL( aVideo.ValueTObjectL<TMPXItemId>(
       
   771                         KMPXMediaGeneralId ).iId1, pos );
       
   772 
       
   773                 iCollection.CategoriesL().GenerateCategoryModifiedEventL(
       
   774                         *videoInCache, ETrue /* flush */, EVcxMyVideosVideoListOrderChanged );
       
   775                 }
       
   776 
       
   777             changed = ETrue;
       
   778             }
       
   779         }
       
   780 
       
   781     // 7
       
   782     if ( aVideo.IsSupported( KMPXMediaGeneralFlags ) )
       
   783         {
       
   784         TUint32 oldFlags = videoInCache->ValueTObjectL<TUint32>( KMPXMediaGeneralFlags );
       
   785         TUint32 newFlags = aVideo.ValueTObjectL<TUint32>( KMPXMediaGeneralFlags );
       
   786 
       
   787         MPX_DEBUG3("CVcxMyVideosVideoCache:: %x -> %x", oldFlags, newFlags);
       
   788 
       
   789         // Play pos is written only after playback. Reset new video flag
       
   790         // now, so there's no need for changing the flag explitically
       
   791         if ( aVideo.IsSupported( KMPXMediaGeneralLastPlaybackPosition ) )
       
   792             {
       
   793             newFlags &= ~EVcxMyVideosVideoNew;
       
   794             MPX_DEBUG3("CVcxMyVideosVideoCache:: %x -> %x", oldFlags, newFlags);
       
   795             }
       
   796 
       
   797         if ( oldFlags != newFlags )
       
   798             {        
       
   799             videoInCache->SetTObjectValueL<TUint32>( KMPXMediaGeneralFlags, newFlags );
       
   800 
       
   801             if ( (newFlags & EVcxMyVideosVideoNew) != (oldFlags & EVcxMyVideosVideoNew) )
       
   802                 {
       
   803                 MPX_DEBUG1("CVcxMyVideosVideoCache:: new video flag modified-> updating categories' new video names and counters");
       
   804     
       
   805                 TBool modified = EFalse;
       
   806                 iCollection.CategoriesL().UpdateCategoryNewVideoNameAndDateL(
       
   807                         *videoInCache, modified );
       
   808     
       
   809                 iCollection.CategoriesL().NewVideoFlagChangedL(
       
   810                         oldFlags, newFlags,
       
   811                         videoInCache->ValueTObjectL<TUint8>( KVcxMediaMyVideosOrigin ),
       
   812                         modified );
       
   813                 
       
   814                 if ( !(newFlags & EVcxMyVideosVideoNew) && 
       
   815                         TVcxMyVideosCollectionUtil::OriginL( *videoInCache ) == EVcxMyVideosOriginDownloaded )
       
   816                     {
       
   817                     MPX_DEBUG1("CVcxMyVideosVideoCache::UpdateVideoL - Count of new videos in Downloaded origin has decreased, sending mediator event to notification launcher");                   
       
   818                     iCollection.NotifyNewVideosCountDecreasedL( *videoInCache );
       
   819                     }
       
   820                 }
       
   821             changed = ETrue;
       
   822             }
       
   823         }
       
   824     // 8
       
   825     if ( aVideo.IsSupported( KMPXMediaGeneralCopyright ) )
       
   826         {
       
   827         if ( videoInCache->ValueText( KMPXMediaGeneralCopyright ) !=
       
   828                 aVideo.ValueText( KMPXMediaGeneralCopyright ) )
       
   829             {
       
   830             videoInCache->SetTextValueL( KMPXMediaGeneralCopyright,
       
   831                     aVideo.ValueText( KMPXMediaGeneralCopyright ) );
       
   832             changed = ETrue;
       
   833             }
       
   834         }
       
   835 #if 0 // type cant be changed
       
   836     // 9
       
   837     if ( aVideo.IsSupported( KMPXMediaGeneralMimeType ) )
       
   838         {
       
   839         videoInCache->SetTextValueL( aVideo.ValueText( KMPXMediaGeneralMimeType ) );
       
   840         changed = ETrue;
       
   841         }
       
   842 #endif
       
   843 
       
   844     // 11
       
   845 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   846     if ( aVideo.IsSupported( KMPXMediaGeneralExtSizeInt64 ) )
       
   847         {
       
   848         if ( aVideo.ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 ) !=
       
   849                 videoInCache->ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 ) )
       
   850             {
       
   851             videoInCache->SetTObjectValueL<TInt64>( KMPXMediaGeneralExtSizeInt64,
       
   852                     aVideo.ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 ) );
       
   853 
       
   854             TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
   855             if ( sortingOrder == EVcxMyVideosSortingSize )
       
   856                 {
       
   857                 MPX_DEBUG1("CVcxMyVideosVideoCache:: size updated && sorting order is by size, moving video to correct place on cache");
       
   858                 MoveToCorrectPlaceL( *videoInCache, sortingOrder );
       
   859                 videoInCache = FindVideoByMdsIdL( aVideo.ValueTObjectL<TMPXItemId>(
       
   860                         KMPXMediaGeneralId ).iId1, pos );
       
   861 
       
   862                 iCollection.CategoriesL().GenerateCategoryModifiedEventL(
       
   863                         *videoInCache, ETrue /* flush */, EVcxMyVideosVideoListOrderChanged );
       
   864                 }
       
   865             changed = ETrue;
       
   866             }
       
   867         }
       
   868 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
   869 
       
   870     // 12
       
   871     if ( aVideo.IsSupported( KVcxMediaMyVideosModifiedDate ) )
       
   872         {
       
   873         if ( videoInCache->ValueTObjectL<TInt64>( KVcxMediaMyVideosModifiedDate ) !=
       
   874                 aVideo.ValueTObjectL<TInt64>( KVcxMediaMyVideosModifiedDate ) )
       
   875             {
       
   876             videoInCache->SetTObjectValueL<TInt64>( KVcxMediaMyVideosModifiedDate,
       
   877                     aVideo.ValueTObjectL<TInt64>( KVcxMediaMyVideosModifiedDate ) );
       
   878             changed = ETrue;
       
   879             }
       
   880         }
       
   881     // 13
       
   882     if ( aVideo.IsSupported( KVcxMediaMyVideosAgeProfile ) )
       
   883         {
       
   884         if ( videoInCache->ValueTObjectL<TInt32>( KVcxMediaMyVideosAgeProfile ) != 
       
   885                 aVideo.ValueTObjectL<TInt32>( KVcxMediaMyVideosAgeProfile ) )
       
   886             {
       
   887             videoInCache->SetTObjectValueL<TInt32>( KVcxMediaMyVideosAgeProfile,
       
   888                     aVideo.ValueTObjectL<TInt32>( KVcxMediaMyVideosAgeProfile ) );
       
   889             changed = ETrue;
       
   890             }
       
   891         }
       
   892     // 14
       
   893     if ( aVideo.IsSupported( KVcxMediaMyVideosAudioLanguage ) )
       
   894         {
       
   895         if ( videoInCache->ValueText( KVcxMediaMyVideosAudioLanguage ) != 
       
   896                 aVideo.ValueText( KVcxMediaMyVideosAudioLanguage ) )
       
   897             {
       
   898             videoInCache->SetTextValueL( KVcxMediaMyVideosAudioLanguage,
       
   899                     aVideo.ValueText( KVcxMediaMyVideosAudioLanguage ) );
       
   900             changed = ETrue;
       
   901             }
       
   902         }
       
   903     // 15
       
   904     if ( aVideo.IsSupported( KVcxMediaMyVideosAuthor ) )
       
   905         {
       
   906         if ( videoInCache->ValueText( KVcxMediaMyVideosAuthor ) !=
       
   907                 aVideo.ValueText( KVcxMediaMyVideosAuthor ))
       
   908             {
       
   909             videoInCache->SetTextValueL( KVcxMediaMyVideosAuthor,
       
   910                     aVideo.ValueText( KVcxMediaMyVideosAuthor ) );
       
   911             changed = ETrue;
       
   912             }
       
   913         }
       
   914 
       
   915     // 16
       
   916     if ( aVideo.IsSupported( KVcxMediaMyVideosOrigin ) )
       
   917         {
       
   918         TUint8 prevOrigin( TVcxMyVideosCollectionUtil::OriginL( *videoInCache ) );
       
   919         TUint8 newOrigin( TVcxMyVideosCollectionUtil::OriginL( aVideo ) );
       
   920                     
       
   921         if ( newOrigin != prevOrigin )
       
   922             {
       
   923             videoInCache->SetTObjectValueL<TUint8>( KVcxMediaMyVideosOrigin, newOrigin );
       
   924             iCollection.CategoriesL().OriginChangedL( *videoInCache, prevOrigin, newOrigin );            
       
   925             changed = ETrue;
       
   926             }
       
   927         }
       
   928 
       
   929     // 17
       
   930     if ( aVideo.IsSupported( KVcxMediaMyVideosDuration ) )
       
   931         {
       
   932         TReal32 durationInCache = TVcxMyVideosCollectionUtil::DurationL( *videoInCache );
       
   933         TReal32 newDuration     = TVcxMyVideosCollectionUtil::DurationL( aVideo );
       
   934         
       
   935         MPX_DEBUG2("SETTING DURATION TO: %f", newDuration );
       
   936         
       
   937         if ( durationInCache != newDuration )
       
   938             {
       
   939             videoInCache->SetTObjectValueL<TReal32>( KVcxMediaMyVideosDuration, newDuration );
       
   940             changed = ETrue;
       
   941             }
       
   942         }
       
   943     // 10
       
   944     if ( aVideo.IsSupported( KMPXMediaGeneralLastPlaybackPosition ) )
       
   945         {
       
   946         MPX_DEBUG1("CVcxMyVideosVideoCache::UpdateVideo Last play pos is supported");
       
   947         
       
   948         if ( videoInCache->ValueTObjectL<TInt>( KMPXMediaGeneralLastPlaybackPosition ) !=
       
   949                 aVideo.ValueTObjectL<TInt>( KMPXMediaGeneralLastPlaybackPosition ))
       
   950             {
       
   951             videoInCache->SetTObjectValueL<TInt>( KMPXMediaGeneralLastPlaybackPosition,
       
   952                     aVideo.ValueTObjectL<TInt>( KMPXMediaGeneralLastPlaybackPosition ) );
       
   953             changed = ETrue;
       
   954             }
       
   955         }
       
   956     // 18
       
   957     if ( aVideo.IsSupported( KVcxMediaMyVideosDownloadId ) )
       
   958         {
       
   959         TUint32 downloadIdInCache( TVcxMyVideosCollectionUtil::DownloadIdL( *videoInCache ) );
       
   960         TUint32 downloadIdInNew( TVcxMyVideosCollectionUtil::DownloadIdL( aVideo ) );
       
   961         if (  downloadIdInNew != downloadIdInCache )
       
   962             {
       
   963             videoInCache->SetTObjectValueL<TUint32>( KVcxMediaMyVideosDownloadId,
       
   964                     downloadIdInNew );
       
   965 
       
   966             if ( downloadIdInNew == 0 )
       
   967                 {
       
   968                 iDownloadCache->Delete( videoInCache );
       
   969                 }
       
   970             changed = ETrue;
       
   971             }
       
   972         }
       
   973     // 19
       
   974     if ( aVideo.IsSupported( KVcxMediaMyVideosRating ) )
       
   975         {
       
   976         TUint8 ratingInCache( TVcxMyVideosCollectionUtil::RatingL( *videoInCache ) );
       
   977         TUint8 ratingInNew( TVcxMyVideosCollectionUtil::RatingL( aVideo ) );
       
   978         if (  ratingInNew != ratingInCache )
       
   979             {
       
   980             videoInCache->SetTObjectValueL<TUint32>( KVcxMediaMyVideosRating,
       
   981                     ratingInNew );
       
   982 
       
   983             changed = ETrue;
       
   984             }
       
   985         }
       
   986 
       
   987     // 20
       
   988     if ( aVideo.IsSupported( KMPXMediaVideoBitRate ) )
       
   989         {
       
   990         TUint16 bitRate = aVideo.ValueTObjectL<TUint16>( KMPXMediaVideoBitRate ); 
       
   991         if ( bitRate != videoInCache->ValueTObjectL<TUint16>( KMPXMediaVideoBitRate ) )
       
   992             {
       
   993             videoInCache->SetTObjectValueL<TUint16>( KMPXMediaVideoBitRate,
       
   994                                                      bitRate );
       
   995             
       
   996             changed = ETrue;
       
   997             }
       
   998         }
       
   999 
       
  1000     // 21
       
  1001     if ( aVideo.IsSupported( KVcxMediaMyVideosAudioFourCc ) )
       
  1002         {
       
  1003         TUint32 AudioFourCcInCache( TVcxMyVideosCollectionUtil::DownloadIdL( *videoInCache ) );
       
  1004         TUint32 AudioFourCcInNew( TVcxMyVideosCollectionUtil::AudioFourCcL( aVideo ) );
       
  1005         if (  AudioFourCcInNew != AudioFourCcInCache )
       
  1006             {
       
  1007             videoInCache->SetTObjectValueL<TUint32>( KVcxMediaMyVideosAudioFourCc,
       
  1008                     AudioFourCcInNew );
       
  1009 
       
  1010             if ( AudioFourCcInNew == 0 )
       
  1011                 {
       
  1012                 iDownloadCache->Delete( videoInCache );
       
  1013                 }
       
  1014             changed = ETrue;
       
  1015             }
       
  1016         }
       
  1017 
       
  1018     // 22
       
  1019     if ( aVideo.IsSupported( KMPXMediaVideoWidth ) )
       
  1020         {
       
  1021         TUint16 width = aVideo.ValueTObjectL<TUint16>( KMPXMediaVideoWidth ); 
       
  1022         if ( width != videoInCache->ValueTObjectL<TUint16>( KMPXMediaVideoWidth ) )
       
  1023             {
       
  1024             videoInCache->SetTObjectValueL<TUint16>( KMPXMediaVideoWidth,
       
  1025                     width );
       
  1026             
       
  1027             changed = ETrue;
       
  1028             }
       
  1029         }
       
  1030     
       
  1031     // 23
       
  1032     if ( aVideo.IsSupported( KMPXMediaVideoHeight ) )
       
  1033         {
       
  1034         TUint16 height = aVideo.ValueTObjectL<TUint16>( KMPXMediaVideoHeight ); 
       
  1035         if ( height != videoInCache->ValueTObjectL<TUint16>( KMPXMediaVideoHeight ) )
       
  1036             {
       
  1037             videoInCache->SetTObjectValueL<TUint16>( KMPXMediaVideoHeight,
       
  1038                     height );
       
  1039             
       
  1040             changed = ETrue;
       
  1041             }
       
  1042         }
       
  1043                 
       
  1044     // 24 
       
  1045     if ( aVideo.IsSupported( KMPXMediaVideoArtist ) )
       
  1046         {
       
  1047         if ( videoInCache->ValueText( KMPXMediaVideoArtist ) !=
       
  1048                 aVideo.ValueText( KMPXMediaVideoArtist ) )
       
  1049             {
       
  1050             videoInCache->SetTextValueL( KMPXMediaVideoArtist,
       
  1051                     aVideo.ValueText( KMPXMediaVideoArtist ) );
       
  1052 
       
  1053             changed = ETrue;
       
  1054             }
       
  1055         }
       
  1056         
       
  1057     return changed;    
       
  1058     }
       
  1059 
       
  1060 // ----------------------------------------------------------------------------
       
  1061 // CVcxMyVideosVideoCache::HandleVideoTitleModifiedL
       
  1062 // Updates sorting order and category attributes if necessarry
       
  1063 // NOTICE that aVideoInCache is not allowed to own the media object, since this
       
  1064 // function may change the pointer value to point to a new object.
       
  1065 // ----------------------------------------------------------------------------
       
  1066 //
       
  1067 void CVcxMyVideosVideoCache::HandleVideoTitleModifiedL( CMPXMedia*& aVideoInCache )
       
  1068     {
       
  1069     MPX_DEBUG1("CVcxMyVideosVideoCache:: video title in cache modified");
       
  1070     MPX_DEBUG1("                         -> updating categories new video names");
       
  1071 
       
  1072     TBool modified = EFalse;
       
  1073     iCollection.CategoriesL().UpdateCategoryNewVideoNameAndDateL(
       
  1074             *aVideoInCache, modified );
       
  1075 
       
  1076     TVcxMyVideosSortingOrder sortingOrder = SortingOrderL();
       
  1077     if ( sortingOrder == EVcxMyVideosSortingName )
       
  1078         {
       
  1079         MPX_DEBUG1(" Title modified and sorting by Name -> moving item to correct place");
       
  1080         
       
  1081         TMPXItemId mpxId( aVideoInCache->ValueTObjectL<TMPXItemId>( KMPXMediaGeneralId ) );
       
  1082         
       
  1083         // Deletes the old media object and creates a new one to a new position
       
  1084         MoveToCorrectPlaceL( *aVideoInCache, SortingOrderL() );
       
  1085         // Find the new object
       
  1086         TInt pos;
       
  1087         aVideoInCache = FindVideoByMdsIdL( mpxId.iId1, pos );
       
  1088         
       
  1089         iCollection.CategoriesL().GenerateCategoryModifiedEventL(
       
  1090                 *aVideoInCache, ETrue /* flush */, EVcxMyVideosVideoListOrderChanged );
       
  1091         }
       
  1092     }
       
  1093 
       
  1094 // ----------------------------------------------------------------------------
       
  1095 // CVcxMyVideosVideoCache::FindCorrectPositionL
       
  1096 // Finds the correct position using bisection method.
       
  1097 // ----------------------------------------------------------------------------
       
  1098 //
       
  1099 TInt CVcxMyVideosVideoCache::FindCorrectPositionL( CMPXMedia& aMedia,
       
  1100         TVcxMyVideosSortingOrder aSortingOrder )
       
  1101     {
       
  1102     MPX_FUNC("CVcxMyVideosVideoCache::FindCorrectPositionL()");
       
  1103             
       
  1104     CMPXMediaArray* cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
  1105                                 KMPXMediaArrayContents);
       
  1106 
       
  1107     TInt videoCount = cachedVideoArray->Count();
       
  1108 
       
  1109     MPX_DEBUG2("CVcxMyVideosVideoCache:: video count = %d", videoCount);
       
  1110 
       
  1111     if ( videoCount == 0 )
       
  1112         {
       
  1113         MPX_DEBUG1("CVcxMyVideosVideoCache:: video count was 0, returning pos 0");
       
  1114         return 0;
       
  1115         }
       
  1116         
       
  1117     TInt stepSize     = 0;
       
  1118     TInt upLimit      = videoCount - 1; //highest possible place
       
  1119     TInt downLimit    = 0;              //lowest possible place
       
  1120     TInt pos          = videoCount / 2; // start from the middle
       
  1121     
       
  1122     if ( videoCount > 5 )
       
  1123         {
       
  1124         stepSize = videoCount / 4; // we start at middle, the first step size is half of the middle = 1/4
       
  1125         MPX_DEBUG2("CVcxMyVideosVideoCache:: stepsize: %d", stepSize);
       
  1126         }
       
  1127     else
       
  1128         {
       
  1129         stepSize = 1;
       
  1130         MPX_DEBUG1("CVcxMyVideosVideoCache:: Moving with single steps.");
       
  1131         MPX_DEBUG2("CVcxMyVideosVideoCache:: stepsize: %d", stepSize);
       
  1132 
       
  1133         if ( videoCount == 1 )
       
  1134             {
       
  1135             MPX_DEBUG1("CVcxMyVideosVideoCache:: only one video -> stepSize = 0");
       
  1136             pos      = 0;
       
  1137             stepSize = 0;
       
  1138             }
       
  1139         }
       
  1140     
       
  1141     MPX_DEBUG2("CVcxMyVideosVideoCache:: start pos = %d", pos);
       
  1142 
       
  1143     TBool found = EFalse;
       
  1144     TInt compare;
       
  1145     while ( !found )
       
  1146         {
       
  1147         compare = CompareL( aMedia, *cachedVideoArray->AtL( pos ), aSortingOrder ); 
       
  1148         if ( compare == 0 )
       
  1149             {
       
  1150             MPX_DEBUG1("CVcxMyVideosVideoCache:: equal item found");
       
  1151             found = ETrue;
       
  1152             break;
       
  1153             }            
       
  1154         else if ( compare == 1 )
       
  1155             {
       
  1156             MPX_DEBUG1("CVcxMyVideosVideoCache:: greater item found");
       
  1157             upLimit = pos - 1;
       
  1158             if ( upLimit < 0 )
       
  1159                 {
       
  1160                 upLimit = 0;
       
  1161                 }
       
  1162             if ( upLimit < downLimit )
       
  1163                 {
       
  1164                 upLimit = downLimit;
       
  1165                 }
       
  1166             pos -= stepSize;
       
  1167             if ( pos < downLimit )
       
  1168                 {
       
  1169                 pos += stepSize;
       
  1170                 MPX_DEBUG1("CVcxMyVideosVideoCache:: hit the limiter");
       
  1171                 }
       
  1172             }
       
  1173         else if ( compare == -1 )
       
  1174             {
       
  1175             MPX_DEBUG1("CVcxMyVideosVideoCache:: smaller item found");
       
  1176             downLimit = pos + 1;
       
  1177             if ( downLimit > videoCount - 1 )
       
  1178                 {
       
  1179                 downLimit = videoCount - 1;
       
  1180                 }
       
  1181             if ( downLimit > upLimit )
       
  1182                 {
       
  1183                 downLimit = upLimit;
       
  1184                 }
       
  1185             pos += stepSize;
       
  1186             if ( pos > upLimit )
       
  1187                 {
       
  1188                 pos -= stepSize;
       
  1189                 MPX_DEBUG1("CVcxMyVideosVideoCache:: hit the limiter");
       
  1190                 }
       
  1191             }
       
  1192         MPX_DEBUG3("CVcxMyVideosVideoCache:: item is between: %d - %d", downLimit, upLimit );            
       
  1193         MPX_DEBUG2("CVcxMyVideosVideoCache:: new pos = %d", pos);
       
  1194         
       
  1195         if ( downLimit == upLimit )
       
  1196             {
       
  1197             MPX_DEBUG1("CVcxMyVideosVideoCache:: downLimit and upLimit are the same, this is last item to check");
       
  1198             compare = CompareL( aMedia, *cachedVideoArray->AtL( pos ), aSortingOrder ); 
       
  1199 
       
  1200             if ( compare == 1 )
       
  1201                 {
       
  1202                 MPX_DEBUG1("CVcxMyVideosVideoCache:: the last possible item had greater value -> this is the correct place");
       
  1203                 }
       
  1204             else
       
  1205                 {
       
  1206                 MPX_DEBUG1("CVcxMyVideosVideoCache:: last possible item had smaller value (or equal) -> adding one to pos");
       
  1207                 pos++;
       
  1208                 }
       
  1209             found = ETrue;
       
  1210             break;
       
  1211             }
       
  1212 
       
  1213         stepSize = (upLimit - downLimit) / 4;
       
  1214         MPX_DEBUG2("CVcxMyVideosVideoCache:: stepsize: %d", stepSize);
       
  1215             
       
  1216         if ( stepSize < 1 )
       
  1217             {
       
  1218             MPX_DEBUG1("CVcxMyVideosVideoCache:: Moving with single steps.");
       
  1219             stepSize = 1;
       
  1220             MPX_DEBUG2("CVcxMyVideosVideoCache:: stepsize: %d", stepSize);
       
  1221             }
       
  1222 
       
  1223         MPX_DEBUG1("CVcxMyVideosVideoCache:: ---- new round ----");
       
  1224         }        
       
  1225     MPX_DEBUG2("CVcxMyVideosVideoCache:: returning pos %d", pos);
       
  1226     return pos;
       
  1227     }
       
  1228 
       
  1229 // ----------------------------------------------------------------------------
       
  1230 // CVcxMyVideosVideoCache::CompareL
       
  1231 // ----------------------------------------------------------------------------
       
  1232 //
       
  1233 TInt CVcxMyVideosVideoCache::CompareL( CMPXMedia& aNewVideo, CMPXMedia& aVideoInList,
       
  1234         TVcxMyVideosSortingOrder aSortingOrder )
       
  1235     {
       
  1236     TInt result = 0;
       
  1237     
       
  1238     switch ( aSortingOrder )
       
  1239         {
       
  1240         case EVcxMyVideosSortingCreationDate:
       
  1241             {
       
  1242             TInt64 newItemsDate  = aNewVideo.ValueTObjectL<TInt64>( KMPXMediaGeneralDate );
       
  1243             TInt64 listItemsDate = aVideoInList.ValueTObjectL<TInt64>( KMPXMediaGeneralDate );
       
  1244             MPX_DEBUG3("CVcxMyVideosVideoCache:: new date = %d, list item date = %d", newItemsDate, listItemsDate);
       
  1245             if ( listItemsDate > newItemsDate /*static_cast<TInt64>(listItemsDate) > static_cast<TInt64>(newItemsDate)*/ )
       
  1246                 {
       
  1247                 result = -1;
       
  1248                 }
       
  1249             else
       
  1250                 {
       
  1251                 if ( listItemsDate == newItemsDate/*static_cast<TInt64>(listItemsDate) == static_cast<TInt64>(newItemsDate)*/ )
       
  1252                     {
       
  1253                     result = 0;
       
  1254                     }
       
  1255                 else
       
  1256                     {
       
  1257                     result = 1;
       
  1258                     }
       
  1259                 }
       
  1260             }              
       
  1261             break;
       
  1262             
       
  1263         case EVcxMyVideosSortingName:
       
  1264             {
       
  1265             TPtrC newVideoName( aNewVideo.ValueText( KMPXMediaGeneralTitle ) );
       
  1266             TPtrC videoInListName( aVideoInList.ValueText( KMPXMediaGeneralTitle ));
       
  1267             MPX_DEBUG3("CVcxMyVideosVideoCache:: newVideoName = %S, videoInListName = %S", &newVideoName, &videoInListName);
       
  1268             TCollationMethod collationMethod = *Mem::CollationMethodByIndex(0); // get the standard method
       
  1269             collationMethod.iFlags |= TCollationMethod::EFoldCase;
       
  1270             result = newVideoName.CompareC( videoInListName, 3, &collationMethod) * -1;
       
  1271             if ( result < 0 )
       
  1272                 {
       
  1273                 result = -1;
       
  1274                 }
       
  1275             if ( result > 0 )
       
  1276                 {
       
  1277                 result = 1;
       
  1278                 }
       
  1279             }
       
  1280             break;
       
  1281             
       
  1282         case EVcxMyVideosSortingSize:
       
  1283             {
       
  1284 #ifdef SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1285             TInt64 newItemsSize  = aNewVideo.ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 );
       
  1286             TInt64 listItemsSize = aVideoInList.ValueTObjectL<TInt64>( KMPXMediaGeneralExtSizeInt64 );
       
  1287 #else
       
  1288             TInt newItemsSize  = aNewVideo.ValueTObjectL<TInt>( KMPXMediaGeneralSize );
       
  1289             TInt listItemsSize = aVideoInList.ValueTObjectL<TInt>( KMPXMediaGeneralSize );
       
  1290 #endif // SYMBIAN_ENABLE_64_BIT_FILE_SERVER_API
       
  1291             MPX_DEBUG3("CVcxMyVideosVideoCache:: newItemsSize = %ld, listItemsSize = %ld", newItemsSize, listItemsSize);
       
  1292             if ( listItemsSize > newItemsSize )
       
  1293                 {
       
  1294                 result = 1;
       
  1295                 }
       
  1296             else
       
  1297                 {
       
  1298                 if ( listItemsSize == newItemsSize )
       
  1299                     {
       
  1300                     result = 0;
       
  1301                     }
       
  1302                 else
       
  1303                     {
       
  1304                     result = -1;
       
  1305                     }
       
  1306                 }
       
  1307             }              
       
  1308             break;
       
  1309         }
       
  1310         
       
  1311     return result;
       
  1312     }
       
  1313 
       
  1314 // ----------------------------------------------------------------------------
       
  1315 // CVcxMyVideosVideoCache::SortingOrderL
       
  1316 // ----------------------------------------------------------------------------
       
  1317 //    
       
  1318 TVcxMyVideosSortingOrder CVcxMyVideosVideoCache::SortingOrderL()
       
  1319     {
       
  1320     TUid uid;
       
  1321     uid.iUid = KVcxMyVideosCollectionCenrepUid;
       
  1322     CRepository* cenRep = CRepository::NewL( uid );
       
  1323     CleanupStack::PushL( cenRep ); // 1->
       
  1324     
       
  1325     TInt sortingOrderInCenrep;
       
  1326 
       
  1327     TInt cenRepError = cenRep->Get( KVcxMyVideosCollectionCenrepKeySortingOrder,
       
  1328             sortingOrderInCenrep );
       
  1329 
       
  1330     CleanupStack::PopAndDestroy( cenRep ); // <-1
       
  1331     
       
  1332     TVcxMyVideosSortingOrder sortingOrder;
       
  1333     
       
  1334     if ( cenRepError != KErrNone )
       
  1335         {
       
  1336         MPX_DEBUG2("CVcxMyVideosVideoCache:: error %d while reading sorting order from Cenrep", cenRepError );
       
  1337         sortingOrder = EVcxMyVideosSortingCreationDate;
       
  1338         }
       
  1339     else
       
  1340         {
       
  1341         sortingOrder = static_cast<TVcxMyVideosSortingOrder>( sortingOrderInCenrep );
       
  1342         }
       
  1343     return sortingOrder;
       
  1344     }
       
  1345 
       
  1346 // ----------------------------------------------------------------------------
       
  1347 // CVcxMyVideosVideoCache::AddToCorrectPlaceL
       
  1348 // ----------------------------------------------------------------------------
       
  1349 //
       
  1350 TInt CVcxMyVideosVideoCache::AddToCorrectPlaceL( CMPXMedia& aVideo,
       
  1351         TVcxMyVideosSortingOrder aSortingOrder, TBool aUpdateCategories )
       
  1352     {    
       
  1353     if ( PosOnVideoListL( aVideo ) != KErrNotFound )
       
  1354         {
       
  1355         MPX_DEBUG2( "CVcxMyVideosVideoCache::AddToCorrectPlaceL() %d is already on iVideoList, skipping add",
       
  1356                 TVcxMyVideosCollectionUtil::IdL( aVideo ).iId1 );
       
  1357         return KErrAlreadyExists;
       
  1358         }
       
  1359         
       
  1360     TInt pos = FindCorrectPositionL( aVideo, aSortingOrder );
       
  1361 
       
  1362     CMPXMediaArray* cachedVideoArray = NULL;            
       
  1363     cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
  1364             KMPXMediaArrayContents );
       
  1365 
       
  1366     if ( pos > (cachedVideoArray->Count() -1) )
       
  1367         {
       
  1368         cachedVideoArray->AppendL( aVideo );                    
       
  1369         }
       
  1370     else
       
  1371         {
       
  1372         cachedVideoArray->InsertL( aVideo, pos );
       
  1373         }
       
  1374         
       
  1375     if ( aUpdateCategories )
       
  1376         {
       
  1377         iCollection.CategoriesL().VideoAddedL( aVideo );
       
  1378         }
       
  1379 
       
  1380     return KErrNone;
       
  1381     }
       
  1382 
       
  1383 // ----------------------------------------------------------------------------
       
  1384 // CVcxMyVideosVideoCache::MoveToCorrectPlaceL
       
  1385 // ----------------------------------------------------------------------------
       
  1386 //
       
  1387 void CVcxMyVideosVideoCache::MoveToCorrectPlaceL( CMPXMedia& aVideo,
       
  1388         TVcxMyVideosSortingOrder aSortingOrder )
       
  1389     {
       
  1390     if ( PosOnPartialVideoListL( aVideo ) != KErrNotFound )
       
  1391         {
       
  1392         MPX_DEBUG2("CVcxMyVideosVideoCache::MoveToCorrectPlaceL() item %d was on iPartialVideoList, not moving ",
       
  1393                 TVcxMyVideosCollectionUtil::IdL( aVideo ).iId1);
       
  1394         return;
       
  1395         }
       
  1396         
       
  1397     CMPXMedia* video = CMPXMedia::NewL( aVideo );
       
  1398     CleanupStack::PushL( video ); // 1->
       
  1399     RemoveL( *video, EFalse /* dont update categories */ );
       
  1400     AddToCorrectPlaceL( *video, aSortingOrder, EFalse /* dont update categories */ );
       
  1401     CleanupStack::PopAndDestroy( video ); // <-1
       
  1402     }
       
  1403 
       
  1404 // ----------------------------------------------------------------------------
       
  1405 // CVcxMyVideosVideoCache::RemoveL
       
  1406 // ----------------------------------------------------------------------------
       
  1407 //
       
  1408 TInt CVcxMyVideosVideoCache::RemoveL( CMPXMedia& aVideo, TBool aUpdateCategories )
       
  1409     {
       
  1410     return RemoveL( TVcxMyVideosCollectionUtil::IdL( aVideo ).iId1, aUpdateCategories );
       
  1411     }
       
  1412 
       
  1413 // ----------------------------------------------------------------------------
       
  1414 // CVcxMyVideosVideoCache::RemoveL
       
  1415 // ----------------------------------------------------------------------------
       
  1416 //
       
  1417 TInt CVcxMyVideosVideoCache::RemoveL( TUint32 aMdsId, TBool aUpdateCategories )
       
  1418     {
       
  1419     MPX_FUNC("CVcxMyVideosVideoCache::RemoveL");
       
  1420     
       
  1421     TInt pos;
       
  1422     CMPXMedia* video = FindVideoByMdsIdL( aMdsId, pos );
       
  1423 
       
  1424     if ( !video )
       
  1425         {
       
  1426         MPX_DEBUG1("CVcxMyVideosVideoCache:: RemoveL failed since the item wasn't on cache");
       
  1427         return KErrNotFound;
       
  1428         }
       
  1429 
       
  1430     iDownloadCache->Delete( video );
       
  1431 
       
  1432     TUint32 flags( 0 );
       
  1433 	if ( video->IsSupported( KMPXMediaGeneralFlags ) )
       
  1434 	    {
       
  1435 		flags = video->ValueTObjectL<TUint32>( KMPXMediaGeneralFlags );
       
  1436 		}
       
  1437     
       
  1438     if ( ( flags & EVcxMyVideosVideoNew ) && 
       
  1439             TVcxMyVideosCollectionUtil::OriginL( *video ) == EVcxMyVideosOriginDownloaded )
       
  1440         {       
       
  1441         MPX_DEBUG1("CVcxMyVideosVideoCache::RemoveL - New video has been removed from Downloaded origin -> sending mediator event to notification launcher");
       
  1442         iCollection.NotifyNewVideosCountDecreasedL( *video );
       
  1443         }
       
  1444     
       
  1445     if ( aUpdateCategories &&
       
  1446             pos != KErrNotFound /* no need to update if item is on iPartialVideoList*/ )
       
  1447         {
       
  1448         iCollection.CategoriesL().VideoRemovedL( *video );
       
  1449         }
       
  1450         
       
  1451     if ( pos != KErrNotFound )
       
  1452         {
       
  1453         CMPXMediaArray* cachedVideoArray = iVideoList->Value<CMPXMediaArray>(
       
  1454                 KMPXMediaArrayContents );
       
  1455         
       
  1456         MPX_DEBUG2("CVcxMyVideosVideoCache:: %d removed from iVideoList", aMdsId);
       
  1457         cachedVideoArray->Remove( pos );
       
  1458         }
       
  1459     else
       
  1460         {
       
  1461         MPX_DEBUG2("CVcxMyVideosVideoCache:: %d removed from iPartialVideoList", aMdsId);
       
  1462         iPartialVideoList.Remove( PosOnPartialVideoListL( *video ) );
       
  1463         iPartialVideoList.Compress();        
       
  1464         }
       
  1465         
       
  1466     return KErrNone;
       
  1467     }
       
  1468 
       
  1469 // ----------------------------------------------------------------------------
       
  1470 // CVcxMyVideosVideoCache::RemoveL
       
  1471 // ----------------------------------------------------------------------------
       
  1472 //
       
  1473 void CVcxMyVideosVideoCache::RemoveL( RArray<TUint32>& aMdsIds, TBool aUpdateCategories )
       
  1474     {        
       
  1475     for ( TInt i = 0; i < aMdsIds.Count(); i++ )
       
  1476         {
       
  1477         RemoveL( aMdsIds[i], aUpdateCategories );
       
  1478         }
       
  1479     }
       
  1480 
       
  1481 // ----------------------------------------------------------------------------
       
  1482 // CVcxMyVideosVideoCache::ResetVideoListL
       
  1483 // iVideoList stays the same, media array is deleted and recreated to 0 length,
       
  1484 // media items are deleted.
       
  1485 // ----------------------------------------------------------------------------
       
  1486 //
       
  1487 void CVcxMyVideosVideoCache::ResetVideoListL()
       
  1488     {
       
  1489     MPX_FUNC("CVcxMyVideosVideoCache::ResetVideoListL");
       
  1490 
       
  1491     iCollection.iMyVideosMdsDb->Cancel();
       
  1492 
       
  1493     CMPXMediaArray* mediaArray =
       
  1494             iVideoList->ValueCObjectL<CMPXMediaArray>( KMPXMediaArrayContents );
       
  1495     CleanupStack::PushL( mediaArray ); // 1->
       
  1496     
       
  1497     for ( TInt i = mediaArray->Count() - 1; i >= 0; i-- )
       
  1498         {
       
  1499         mediaArray->Remove( i );
       
  1500         }        
       
  1501 
       
  1502     iVideoList->Delete( KMPXMediaArrayContents );
       
  1503     iVideoList->SetCObjectValueL<CMPXMediaArray>( KMPXMediaArrayContents, mediaArray );
       
  1504 
       
  1505     CleanupStack::PopAndDestroy( mediaArray ); // <-1
       
  1506     
       
  1507     TInt count = iCollection.iOpenHandler->iVideoListsBeingOpened.Count();
       
  1508     
       
  1509     for ( TInt i = 0; i < count; i++ )
       
  1510         {
       
  1511         CMPXMedia* videoList = iCollection.iOpenHandler->iVideoListsBeingOpened[i];
       
  1512         if ( videoList )
       
  1513             {
       
  1514             MPX_DEBUG3("CVcxMyVideosVideoCache:: reset iVideoListsBeingOpened[%d] category: %d",
       
  1515                     i, iCollection.iOpenHandler->iCategoryIdsBeingOpened[i] );
       
  1516             mediaArray = videoList->ValueCObjectL<CMPXMediaArray>( KMPXMediaArrayContents );
       
  1517             CleanupStack::PushL( mediaArray ); // 1->
       
  1518             for ( TInt i = mediaArray->Count() - 1; i >= 0; i-- )
       
  1519                 {
       
  1520                 mediaArray->Remove( i );
       
  1521                 }
       
  1522             videoList->Delete( KMPXMediaArrayContents );
       
  1523             videoList->SetCObjectValueL<CMPXMediaArray>( KMPXMediaArrayContents,
       
  1524                     mediaArray );
       
  1525             CleanupStack::PopAndDestroy( mediaArray ); // <-1
       
  1526             }
       
  1527         }
       
  1528         
       
  1529     iVideoListIsPartial = ETrue;
       
  1530 
       
  1531     delete iDownloadCache;
       
  1532     iDownloadCache = NULL;
       
  1533     iDownloadCache = CVcxMyVideosDownloadCache::NewL();
       
  1534                 
       
  1535     iCollection.CategoriesL().ResetVideoCountersL(); //this does not send events
       
  1536     }
       
  1537 
       
  1538 // ----------------------------------------------------------------------------
       
  1539 // CVcxMyVideosVideoCache::ReCreateVideoListL
       
  1540 // iVideoList and its media array are deleted and recreated. New instances point to
       
  1541 // different global heap position. Media objects stay the same and point to
       
  1542 // same global heap.
       
  1543 // ----------------------------------------------------------------------------
       
  1544 //
       
  1545 void CVcxMyVideosVideoCache::ReCreateVideoListL()
       
  1546     {
       
  1547     MPX_FUNC("CVcxMyVideosVideoCache::ReCreateVideoListL");
       
  1548     
       
  1549     CMPXMedia* newList = TVcxMyVideosCollectionUtil::CreateEmptyMediaListL();
       
  1550     CleanupStack::PushL( newList ); // 1->
       
  1551 
       
  1552     CMPXMediaArray* oldArray = iVideoList->Value<CMPXMediaArray>(
       
  1553             KMPXMediaArrayContents );
       
  1554     TInt count = oldArray->Count();
       
  1555 
       
  1556     CMPXMediaArray* newArray = newList->Value<CMPXMediaArray>(
       
  1557             KMPXMediaArrayContents );
       
  1558     
       
  1559     CMPXMedia* video;
       
  1560     for ( TInt i = 0; i < count; i++ )
       
  1561         {
       
  1562         video = CMPXMedia::NewL( *oldArray->AtL( i ) ); // points to same heap object
       
  1563         CleanupStack::PushL( video ); // 2->
       
  1564         newArray->AppendL( video ); // ownership moves
       
  1565         CleanupStack::Pop( video ); // <-2
       
  1566         }
       
  1567     
       
  1568     delete iVideoList;
       
  1569     iVideoList = NULL;
       
  1570     iVideoList = newList;
       
  1571     CleanupStack::Pop( newList ); // <-1
       
  1572 
       
  1573     delete iDownloadCache;
       
  1574     iDownloadCache = NULL;
       
  1575     iDownloadCache = CVcxMyVideosDownloadCache::NewL();
       
  1576     }
       
  1577 // End of file
       
  1578