clfwrapper/ClientSrc/CCLFDbItemProvider.cpp
changeset 0 c53acadfccc6
child 14 646a02f170b9
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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 "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "CCLFDbItemProvider.h"
       
    21 #include    "CCLFItemImpl.h"
       
    22 #include    "MCLFItemProviderObserver.h"
       
    23 #include    "CCLFDbItemContainer.h"
       
    24 #include    "CLFPanics.h"
       
    25 #include    "MGDebugPrint.h"
       
    26 #include    "CCLFQueryAdapter.h"
       
    27 #include    "CLFUtils.h"
       
    28 #include    <mdeobjectquery.h>
       
    29 #include    <mdeconstants.h>
       
    30 
       
    31 const TInt KCLFItemArrayGranularity( 25 );
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CCLFDbItemProvider::CCLFDbItemProvider
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CCLFDbItemProvider::CCLFDbItemProvider( CCLFDbItemContainer& aDbItemContainer,
       
    40                                                CCLFQueryAdapter& aQueryAdapter, CMdESession* aMdESession )
       
    41     : CActive( EPriorityStandard ),
       
    42       iProviderState( EItemsReady ),
       
    43       iObserver( NULL ),
       
    44       iItemArray( KCLFItemArrayGranularity ),
       
    45       iDbItemContainer( aDbItemContainer ),
       
    46       iQueryAdapter( aQueryAdapter ),
       
    47       iObjectQuery( NULL ),
       
    48       iObjDefStr( &KNullDesC ),
       
    49       iMdESession( aMdESession )
       
    50     {
       
    51     CActiveScheduler::Add( this );
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CCLFDbItemProvider::ConstructL
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 void CCLFDbItemProvider::ConstructL()
       
    59     {
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CCLFDbItemProvider::NewL
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CCLFDbItemProvider* CCLFDbItemProvider::NewL( CCLFDbItemContainer& aDbItemContainer,
       
    67                                               CCLFQueryAdapter& aQueryAdapter, CMdESession* aMdESession )
       
    68     {
       
    69     CCLFDbItemProvider* self = new( ELeave ) CCLFDbItemProvider( aDbItemContainer, 
       
    70                                                                                                        aQueryAdapter, 
       
    71                                                                                                        aMdESession );
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CCLFDbItemProvider::~CCLFDbItemProvider
       
    80 // Destructor
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CCLFDbItemProvider::~CCLFDbItemProvider()
       
    84     {
       
    85     Cancel();
       
    86     delete iObjectQuery;
       
    87     iItemArray.ResetAndDestroy();
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CCLFDbItemProvider::DoCancel
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CCLFDbItemProvider::DoCancel()
       
    95     {
       
    96     MG_DEBUG1( dr1, "[CLF]\t CCLFDbItemProvider::DoCancel" ); 
       
    97 
       
    98     if ( iObjectQuery )
       
    99         {
       
   100         iObjectQuery->Cancel();
       
   101         }
       
   102 
       
   103     iObjDefStr = &KNullDesC;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CCLFDbItemProvider::RunL
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CCLFDbItemProvider::RunL()
       
   111     {
       
   112     MG_DEBUG2( r1, "[CLF]\t CCLFDbItemProvider::RunL %d", iStatus.Int() );
       
   113 
       
   114     TInt error( iStatus.Int() );
       
   115     if ( error == KErrNone )
       
   116         {
       
   117         DoRunL();
       
   118         }
       
   119     if ( EItemsReady == iProviderState )
       
   120         {
       
   121         iObserver->OperationCompleteL( KErrNone );
       
   122         }
       
   123     }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CCLFDbItemProvider::DoRunL
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CCLFDbItemProvider::DoRunL()
       
   130     {
       
   131     switch ( iProviderState )
       
   132         {
       
   133         case EPrepareItemIds:
       
   134             {
       
   135             DoFetchMdSIdDataCachedL();
       
   136             break;
       
   137             }
       
   138         case EPrepareItems:
       
   139             {
       
   140             DoFetchMdSDataL();
       
   141             break;
       
   142             }
       
   143         default:
       
   144             {
       
   145             User::Leave( ECLFIncorrectCommand );
       
   146             }
       
   147         }
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CCLFDbItemProvider::RunError
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 TInt CCLFDbItemProvider::RunError( const TInt aError )
       
   155     {
       
   156     MG_DEBUG2( re1, "[CLF]\t CCLFDbItemProvider::RunError: %d", aError ); 
       
   157 
       
   158     if ( (EItemsReady == iProviderState || aError) && iObserver )
       
   159     	{
       
   160         TRAP_IGNORE( iObserver->OperationCompleteL( aError ) );
       
   161         }
       
   162     
       
   163     return KErrNone;
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CCLFDbItemProvider::PrepareItemsL
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CCLFDbItemProvider::PrepareItemsL( const MDesCArray& aMimeTypeArray,
       
   171                                         const TArray< TInt >& aMediaTypeArray,
       
   172                                         MCLFItemProviderObserver& aObserver )
       
   173     {
       
   174     MG_DEBUG1( PI1, "[CLF]\t CCLFDbItemProvider::PrepareItemsL 1" ); 
       
   175 
       
   176     Cancel();
       
   177     iObserver = &aObserver;
       
   178     iItemArray.ResetAndDestroy();
       
   179 
       
   180     TBool image = EFalse;
       
   181     TBool video = EFalse;
       
   182     TBool audio = EFalse;
       
   183     TBool other = EFalse;
       
   184     
       
   185     // find all mediatypes
       
   186     const TInt typeCount( aMediaTypeArray.Count() );
       
   187     for ( TInt i = 0; i < typeCount; i++ )
       
   188         {
       
   189         iObjDefStr = &KNullDesC;
       
   190         iObjDefStr = &CLFUtils::MapClfType( aMediaTypeArray[ i ] );
       
   191         
       
   192         if (iObjDefStr->Compare(MdeConstants::Image::KImageObject) == 0)
       
   193             {
       
   194             image = ETrue;
       
   195             }
       
   196         else if (iObjDefStr->Compare(MdeConstants::Video::KVideoObject) == 0)
       
   197             {
       
   198             video = ETrue;
       
   199             }
       
   200         else if (iObjDefStr->Compare(MdeConstants::Audio::KAudioObject) == 0)
       
   201             {
       
   202             audio = ETrue;
       
   203             }
       
   204         else
       
   205             {
       
   206             other = ETrue;
       
   207             }
       
   208         }
       
   209     
       
   210     // images & videos (gallery)
       
   211     if (image && video && !audio)
       
   212         {
       
   213         iObjDefStr = &MdeConstants::Object::KBaseObject;
       
   214         }
       
   215     // images
       
   216     else if(image && !video && !audio && !other)
       
   217         {
       
   218         iObjDefStr = &MdeConstants::Image::KImageObject;
       
   219         }
       
   220     // videos
       
   221     else if (!image && video && !audio && !other)
       
   222         {
       
   223         iObjDefStr = &MdeConstants::Video::KVideoObject;
       
   224         }
       
   225     // audio
       
   226     else if (!image && !video && audio && !other)
       
   227         {
       
   228         iObjDefStr = &MdeConstants::Audio::KAudioObject;
       
   229         }
       
   230     // other
       
   231     else
       
   232         {
       
   233         iObjDefStr = &MdeConstants::Object::KBaseObject;
       
   234         }
       
   235     
       
   236     iProviderState = EPrepareItemIds;
       
   237 
       
   238     delete iObjectQuery;
       
   239     iObjectQuery = NULL;
       
   240     iQueryAdapter.QueryMdEObjectsL( aMimeTypeArray, aMediaTypeArray, iObjectQuery, iStatus );
       
   241     SetActive();
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CCLFDbItemProvider::PrepareItemsL
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CCLFDbItemProvider::PrepareItemsL( const TArray< TCLFItemId >& aItemIDArray,
       
   249                                         MCLFItemProviderObserver& aObserver )
       
   250     {
       
   251     MG_DEBUG1( PI1, "[CLF]\t CCLFDbItemProvider::PrepareItemsL 2" ); 
       
   252 
       
   253     Cancel();
       
   254     iObserver = &aObserver;
       
   255     iItemArray.ResetAndDestroy();
       
   256     iProviderState = EPrepareItems;
       
   257 
       
   258     const TDesC& objDefStr = CLFUtils::MapClfType( ECLFMediaTypeUnknown );
       
   259     delete iObjectQuery;
       
   260     iObjectQuery = NULL;
       
   261     iQueryAdapter.QueryMdEObjectsL( aItemIDArray, objDefStr, iObjectQuery, iStatus );
       
   262     SetActive();
       
   263     }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CCLFDbItemProvider::GetItems
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 TInt CCLFDbItemProvider::GetItems( RPointerArray< MCLFItem >& aItemArray )
       
   270     {
       
   271     MG_DEBUG1( df1, "[CLF]\t CCLFDbItemProvider::GetItems" ); 
       
   272 
       
   273     const TInt KCLFItemIndex( 0 ); // do not change
       
   274 
       
   275     while ( iItemArray.Count() )
       
   276         {
       
   277         const TInt error( aItemArray.Append(
       
   278                             iItemArray[ KCLFItemIndex ] ) ); // takes ownership
       
   279         if ( error )
       
   280             {
       
   281             return error;
       
   282             }
       
   283         iItemArray.Remove( KCLFItemIndex );
       
   284         }
       
   285     return KErrNone;
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CCLFDbItemProvider::DoFetchMdSIdDataCachedL
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CCLFDbItemProvider::DoFetchMdSIdDataCachedL()
       
   293     {
       
   294     MG_DEBUG1( df1, "[CLF]\t CCLFDbItemProvider::DoFetchMdSIdDataCachedL" ); 
       
   295 
       
   296     RArray< TCLFItemId > itemIdArray;
       
   297     CleanupClosePushL( itemIdArray );
       
   298     DoFetchMdSIdDataL( itemIdArray );
       
   299 
       
   300     // Check if items are in cache
       
   301     TInt idCount( itemIdArray.Count() );
       
   302     for ( TInt i = 0 ; i < idCount ; ++i )
       
   303         {
       
   304         CCLFContainerItem* containerItem = 
       
   305                                 iDbItemContainer.ItemById( itemIdArray[ i ] );
       
   306         if ( containerItem )
       
   307             {
       
   308             // found in cache
       
   309             CreateCLFItemL( *containerItem ); // reserve item for this model
       
   310 
       
   311             // remove handled item ID
       
   312             itemIdArray.Remove( i );
       
   313             --i;
       
   314             --idCount;
       
   315             }
       
   316         }
       
   317     iProviderState = EPrepareItems;
       
   318 
       
   319     delete iObjectQuery;
       
   320     iObjectQuery = NULL;
       
   321     iQueryAdapter.QueryMdEObjectsL( itemIdArray.Array(), *iObjDefStr, iObjectQuery, iStatus );
       
   322     SetActive();
       
   323     CleanupStack::PopAndDestroy( &itemIdArray );
       
   324     }
       
   325 
       
   326 // -----------------------------------------------------------------------------
       
   327 // CCLFDbItemProvider::DoFetchMdSDataL
       
   328 // -----------------------------------------------------------------------------
       
   329 //
       
   330 void CCLFDbItemProvider::DoFetchMdSDataL()
       
   331     {
       
   332 #ifdef _DEBUG
       
   333     MG_DEBUG1( df1, "[CLF]\t CCLFDbItemProvider::DoFetchDataL" ); 
       
   334     
       
   335     _LIT( KCLFWIPPanicText, "DoFetchMdSDataL");
       
   336     __ASSERT_DEBUG( iObjectQuery, User::Panic( KCLFWIPPanicText, KErrNotReady ));
       
   337 #endif
       
   338 
       
   339     const TInt count( iObjectQuery->Count() );
       
   340     for ( TInt index( 0 ); index < count; index++ )
       
   341         {
       
   342         const TInt64 id = (TInt64)iObjectQuery->ResultId( index );
       
   343         CMdEObject* object = iMdESession->GetFullObjectL( id );
       
   344         
       
   345         if( !object )
       
   346             {
       
   347             continue;
       
   348             }
       
   349 
       
   350         // check if other model is already put this item to container
       
   351         CCLFContainerItem* containerItem = 
       
   352                                 iDbItemContainer.ItemById( id );
       
   353         if ( containerItem )
       
   354             {
       
   355             delete object;
       
   356             object = NULL;
       
   357             continue;
       
   358             }
       
   359         else
       
   360             {
       
   361             // create container item
       
   362             containerItem = CCLFContainerItem::NewLC( object );
       
   363 
       
   364             // append container item to container
       
   365             // container takes ownership of container item
       
   366             iDbItemContainer.AddL( containerItem );
       
   367             CleanupStack::Pop( containerItem );
       
   368             }
       
   369         CreateCLFItemL( *containerItem );
       
   370         }
       
   371     iProviderState = EItemsReady;
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CCLFDbItemProvider::DoFetchMdSIdDataL
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 void CCLFDbItemProvider::DoFetchMdSIdDataL( RArray< TCLFItemId >& aItemIDArray )
       
   379     {
       
   380 #ifdef _DEBUG
       
   381     MG_DEBUG1( df1, "[CLF]\t CCLFDbItemProvider::DoFetchDataL" ); 
       
   382 
       
   383     _LIT( KCLWIPPanicString, "DoFetchMdSIdDataL" );
       
   384     __ASSERT_DEBUG( iObjectQuery,
       
   385                      User::Panic( KCLWIPPanicString, KErrAbort ));
       
   386 #endif
       
   387 
       
   388     const TInt count( iObjectQuery->Count() );
       
   389     for ( TInt index( 0 ); index < count; index++ )
       
   390         {
       
   391         aItemIDArray.AppendL( (TInt64)iObjectQuery->ResultId( index ));
       
   392         }
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CCLFDbItemProvider::CreateCLFItemL
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 void CCLFDbItemProvider::CreateCLFItemL( CCLFContainerItem& aCItem )
       
   400     {
       
   401     MG_DEBUG1( cci1, "[CLF]\t CCLFDbItemProvider::CreateCLFItemL" ); 
       
   402 
       
   403     // CLF item releases reference in destructor
       
   404     CCLFItemImpl* item = CCLFItemImpl::NewLC( aCItem, iDbItemContainer );
       
   405     iItemArray.AppendL( item ); // array takes ownership of the item
       
   406     CleanupStack::Pop( item );
       
   407     }
       
   408 
       
   409 //  End of File