clfwrapper/ClientSrc/CCLFQueryAdapter.cpp
changeset 0 c53acadfccc6
child 19 82c0024438c8
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 2006-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:  QueryAdapter implementation for CLF
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // Includes
       
    20 #include "CCLFQueryAdapter.h"
       
    21 #include "CLFConsts.h"
       
    22 #include "CLFPanics.h"
       
    23 #include "CLF2MdEConsts.h"
       
    24 #include "CLFUtils.h"
       
    25 #include <e32base.h>
       
    26 #include <e32std.h>
       
    27 #include <e32debug.h>
       
    28 #include "MGDebugPrint.h"
       
    29 #include "MGTracePrint.h"
       
    30 #include <mdecondition.h>
       
    31 #include <mdeconstants.h>
       
    32 #include <mdetextpropertycondition.h>
       
    33 #include <mdeobject.h>
       
    34 #include <mdeproperty.h>
       
    35 #include <mdequery.h>
       
    36 #include <mdenamespacedef.h>
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CCLFQueryAdapter::CCLFQueryAdapter
       
    43 // Default constructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CCLFQueryAdapter::CCLFQueryAdapter( CMdESession& aMdESession )
       
    47     : iMdESession( aMdESession ), iDefaultNamespaceDef( NULL )
       
    48     {
       
    49     // pass
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CCLFQueryAdapter::ConstructL
       
    54 // Two-phase constructor
       
    55 // ---------------------------------------------------------------------------
       
    56 //
       
    57 void CCLFQueryAdapter::ConstructL()
       
    58     {
       
    59     iDefaultNamespaceDef = &iMdESession.GetDefaultNamespaceDefL();
       
    60     }
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CCLFQueryAdapter::NewL
       
    64 // Two-phase API constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CCLFQueryAdapter* CCLFQueryAdapter::NewL( CMdESession& aMdESession )
       
    68     {
       
    69     CCLFQueryAdapter* self = new( ELeave ) CCLFQueryAdapter( aMdESession );
       
    70     CleanupStack::PushL( self );
       
    71     self->ConstructL();
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CCLFQueryAdapter::~CCLFQueryAdapter
       
    78 // Destructor
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CCLFQueryAdapter::~CCLFQueryAdapter()
       
    82     {
       
    83     iQueryArray.Reset();
       
    84     iStatusArray.Reset();
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CCLFQueryAdapter::QueryMdEObjectsL
       
    89 // Queries the objects based on CLF media and mime types
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void CCLFQueryAdapter::QueryMdEObjectsL( const MDesCArray& aMimeTypeArray,
       
    93                                          const TArray<TInt>& aMediaTypeArray,
       
    94                                          CMdEObjectQuery*& aQuery,
       
    95                                          TRequestStatus& aStatus )
       
    96     {
       
    97     // hard-coded mime types for music
       
    98     const TDesC* clfwrappermusictypes[] =
       
    99             {
       
   100             &KCLFWrapperMusicTypeMpeg,
       
   101             &KCLFWrapperMusicTypeAac,
       
   102             &KCLFWrapperMusicTypeMp3,
       
   103             &KCLFWrapperMusicTypeX_Mp3,
       
   104             &KCLFWrapperMusicTypeMp4,
       
   105             &KCLFWrapperMusicType3gpp,
       
   106             &KCLFWrapperMusicTypeM4a,
       
   107             &KCLFWrapperMusicType3gpp2,
       
   108             &KCLFWrapperMusicTypeMpeg4,
       
   109             &KCLFWrapperMusicTypeX_Pn_RealAudio,
       
   110             &KCLFWrapperMusicTypeX_Ms_Wma,
       
   111             };
       
   112     
       
   113     CMdEObjectDef& def = iDefaultNamespaceDef->GetObjectDefL( MdeConstants::Object::KBaseObject );
       
   114     __ASSERT_DEBUG( !aQuery, User::Panic( KCLFPanicText, KErrNotReady ));
       
   115     aQuery = iMdESession.NewObjectQueryL( *iDefaultNamespaceDef, def, this );
       
   116     aQuery->SetResultMode( EQueryResultModeId );
       
   117 
       
   118     CMdELogicCondition& rootCondition = aQuery->Conditions();
       
   119 
       
   120     if ( aMimeTypeArray.MdcaCount() > 0 )
       
   121         {
       
   122         rootCondition.SetOperator( ELogicConditionOperatorOr );
       
   123         CMdEPropertyDef& propDef = def.GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   124         
       
   125         // Add object conditions based on mime types.
       
   126         for ( TInt index( 0 ); index < aMimeTypeArray.MdcaCount(); index++ )
       
   127             {
       
   128             CMdELogicCondition& logicCond = rootCondition.AddLogicConditionL( ELogicConditionOperatorOr );
       
   129             AddMimeTypeConditionL(logicCond, propDef, aMimeTypeArray.MdcaPoint( index ));
       
   130             }
       
   131         }
       
   132     else
       
   133         {
       
   134         TBool music = EFalse;
       
   135         TBool sounds = EFalse;
       
   136 
       
   137         // Add mime conditions for audio types
       
   138         CMdEObjectDef* objDef = NULL;
       
   139         CMdEPropertyDef* propDef = NULL;
       
   140 
       
   141         const TInt typeCount( aMediaTypeArray.Count() );
       
   142         
       
   143         // Add object conditions based on CLF media types.
       
   144         if ( typeCount > 0 )
       
   145             {
       
   146             rootCondition.SetOperator( ELogicConditionOperatorOr );
       
   147 
       
   148             for ( TInt index( 0 ); index < typeCount; index++ )
       
   149                {
       
   150                const TDesC& type = CLFUtils::MapClfType( aMediaTypeArray[ index ] );
       
   151                if ( type != KNullDesC )
       
   152                    {
       
   153                    CMdEObjectDef& objDef = iDefaultNamespaceDef->GetObjectDefL( type );
       
   154                    if( aMediaTypeArray[ index ] == ECLFMediaTypeSound )
       
   155                        {
       
   156                        sounds = ETrue;
       
   157                        }
       
   158                    else if( aMediaTypeArray[ index ] == ECLFMediaTypeMusic )
       
   159                        {
       
   160                        music = ETrue;
       
   161                        }
       
   162                    else
       
   163                        {
       
   164                        rootCondition.AddObjectConditionL( objDef );     
       
   165                        }
       
   166                    }
       
   167                }
       
   168             if( sounds && music )
       
   169                 {
       
   170                 CMdEObjectDef& objDef = iDefaultNamespaceDef->GetObjectDefL( CLFUtils::MapClfType( ECLFMediaTypeMusic ) );
       
   171                 rootCondition.AddObjectConditionL( objDef );    
       
   172                 }
       
   173             else if( sounds )
       
   174                 {
       
   175                 objDef = &iDefaultNamespaceDef->GetObjectDefL( CLFUtils::MapClfType( ECLFMediaTypeSound ) );
       
   176                 propDef = &objDef->GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   177                 CMdELogicCondition& soundCond = rootCondition.AddLogicConditionL( ELogicConditionOperatorAnd );
       
   178 
       
   179                 // add all necessary mime types to query conditions
       
   180                 const TInt num = sizeof ( clfwrappermusictypes ) / sizeof( TDesC* );
       
   181                 for ( TInt i = 0; i < num; ++i )
       
   182                     {
       
   183                     CMdELogicCondition& mimeCond = soundCond.AddLogicConditionL( ELogicConditionOperatorOr );
       
   184                     mimeCond.AddPropertyConditionL( *propDef, ETextPropertyConditionCompareEquals, *clfwrappermusictypes[i] );
       
   185                     mimeCond.SetNegate( ETrue );
       
   186                     }  
       
   187                 soundCond.AddObjectConditionL( *objDef );
       
   188                 }
       
   189             else if( music )
       
   190                 {
       
   191                 objDef = &iDefaultNamespaceDef->GetObjectDefL( CLFUtils::MapClfType( ECLFMediaTypeMusic ) );
       
   192                 propDef = &objDef->GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   193                 CMdELogicCondition& musicCond = rootCondition.AddLogicConditionL( ELogicConditionOperatorAnd );
       
   194 
       
   195                 // add all necessary mime types to query conditions
       
   196                 const TInt num = sizeof ( clfwrappermusictypes ) / sizeof( TDesC* );
       
   197                 CMdELogicCondition& mimeCond = musicCond.AddLogicConditionL( ELogicConditionOperatorOr );
       
   198                 for ( TInt i = 0; i < num; ++i )
       
   199                     {
       
   200                     mimeCond.AddPropertyConditionL( *propDef, ETextPropertyConditionCompareEquals, *clfwrappermusictypes[i] );
       
   201                     }  
       
   202                 musicCond.AddObjectConditionL( *objDef );
       
   203                 }
       
   204             }
       
   205         }
       
   206 
       
   207     aStatus = KRequestPending;
       
   208     iStatusArray.Append( &aStatus );
       
   209     iQueryArray.Append( aQuery );
       
   210 
       
   211     // Check that we can actually execute the query. We can if we have at least one query condition.
       
   212     if ( rootCondition.Count() > 0 )
       
   213         {
       
   214         MG_DEBUG1( QMO5, "[CLF]\t CCLFQueryAdapter::QueryMdEObjectsL execute query");
       
   215         aQuery->FindL();
       
   216         }
       
   217     else
       
   218         {
       
   219         MG_DEBUG1( QMO6, "[CLF]\t CCLFQueryAdapter::QueryMdEObjectsL unknown conditions");
       
   220         CompleteRequest( *aQuery, KErrNone );
       
   221         }
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CCLFQueryAdapter::QueryMdEObjectsL
       
   226 // Queries the objects based on CLF ids
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 void CCLFQueryAdapter::QueryMdEObjectsL( const TArray< TCLFItemId >& aItemIDArray,
       
   230                                          const TDesC& aObjDefStr,
       
   231                                          CMdEObjectQuery*& aQuery,
       
   232                                          TRequestStatus& aStatus )
       
   233     {
       
   234     CMdEObjectDef& objDef = iDefaultNamespaceDef->GetObjectDefL( aObjDefStr );
       
   235     __ASSERT_DEBUG( !aQuery, User::Panic( KCLFPanicText, KErrNotReady ));
       
   236     aQuery = iMdESession.NewObjectQueryL( *iDefaultNamespaceDef, objDef, this );
       
   237     aQuery->SetResultMode( EQueryResultModeId );
       
   238 
       
   239     CMdELogicCondition& rootCondition = aQuery->Conditions();
       
   240     rootCondition.SetOperator( ELogicConditionOperatorOr );
       
   241 
       
   242     const TInt idCount( aItemIDArray.Count()  );
       
   243     if ( idCount > 0 )
       
   244         {
       
   245         RArray< TItemId > objectIds;
       
   246 
       
   247         // Add object conditions based on CLF ids.
       
   248         for ( TInt index( 0 ); index < idCount; index++ )
       
   249             {
       
   250             objectIds.Append( aItemIDArray[ index ] );
       
   251             }
       
   252         CleanupClosePushL( objectIds );
       
   253         rootCondition.AddObjectConditionL( objectIds );
       
   254         CleanupStack::PopAndDestroy( &objectIds );
       
   255         }
       
   256 
       
   257     aStatus = KRequestPending;
       
   258     iStatusArray.Append( &aStatus );
       
   259     iQueryArray.Append( aQuery );
       
   260 
       
   261     // Check that we can actually execute the query. We can if we have at least one query condition.
       
   262     if ( rootCondition.Count() > 0 )
       
   263         {
       
   264         MG_DEBUG1( QMO4, "[CLF]\t CCLFQueryAdapter::QueryMdEObjectsL execute query" );
       
   265         aQuery->FindL();
       
   266         }
       
   267     else
       
   268         {
       
   269         MG_DEBUG1( QMO5, "[CLF]\t CCLFQueryAdapter::QueryMdEObjectsL unknown conditions" );
       
   270         CompleteRequest( *aQuery, KErrNone );
       
   271         }
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // CCLFQueryAdapter::HandleQueryNewResults
       
   276 // Called to notify the observer that new results have been received 
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 #ifdef MG_ENABLE_TRACE_PRINT
       
   280 void CCLFQueryAdapter::HandleQueryNewResults( CMdEQuery& /* aQuery */,
       
   281                                               const TInt aFirstNewItemIndex,
       
   282                                               const TInt aNewItemCount )
       
   283 #else
       
   284 void CCLFQueryAdapter::HandleQueryNewResults( CMdEQuery& /* aQuery */,
       
   285                                               const TInt /* aFirstNewItemIndex */,
       
   286                                               const TInt /* aNewItemCount */ )
       
   287 #endif
       
   288     {
       
   289     MG_DEBUG3( HQN1, "[CLF]\t HandleQueryNewResults, aFirstNewItemIndex == %d, aNewItemCount == %d",
       
   290                aFirstNewItemIndex, aNewItemCount );
       
   291 
       
   292     /* This method could be used to receive metadata query results in batches.
       
   293      * This feature is currently unimplemented. */
       
   294     }
       
   295 
       
   296 // ---------------------------------------------------------------------------
       
   297 // CCLFQueryAdapter::HandleQueryCompleted
       
   298 // Called to notify the observer that the query has been completed
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CCLFQueryAdapter::HandleQueryCompleted( CMdEQuery& aQuery, const TInt aError )
       
   302     {
       
   303     MG_DEBUG2( HQC1, "[CLF]\t CCLFQueryAdapter::HandleQueryCompleted %d", aError );
       
   304     MG_DEBUG2( HQC2, "[CLF]\t aQuery.Count() returns: %d", aQuery.Count() );
       
   305     MG_DEBUG2( HQC3, "[CLF]\t Class instance %d", this );
       
   306 
       
   307     CompleteRequest( aQuery, aError );
       
   308     }
       
   309 
       
   310 // ---------------------------------------------------------------------------
       
   311 // CCLFQueryAdapter::CompleteRequest
       
   312 // ---------------------------------------------------------------------------
       
   313 //
       
   314 void CCLFQueryAdapter::CompleteRequest( CMdEQuery& aQuery, const TInt aError )
       
   315     {
       
   316     MG_DEBUG1( HQC4, "[CLF]\t CCLFQueryAdapter::CompleteRequest" );
       
   317     const TInt count = iQueryArray.Count();
       
   318     TInt i( 0 );
       
   319 
       
   320     for ( i = 0; i < count; ++i )
       
   321         {
       
   322         if ( iQueryArray[ i ] == &aQuery )
       
   323             {
       
   324             User::RequestComplete( iStatusArray[ i ], aError );
       
   325             iStatusArray.Remove( i );
       
   326             iQueryArray.Remove( i );
       
   327             break;
       
   328             }
       
   329         }
       
   330 #ifdef _DEBUG
       
   331     _LIT( KCLFWQAPanicText, "HandleQueryCompleted");
       
   332     __ASSERT_DEBUG( i < count, User::Panic( KCLFWQAPanicText, KErrAbort ));
       
   333 #endif
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // CCLFQueryAdapter::AddMimeTypeConditionL
       
   338 // ---------------------------------------------------------------------------
       
   339 //
       
   340 void CCLFQueryAdapter::AddMimeTypeConditionL( CMdELogicCondition& aLogicCondition,
       
   341                                               const CMdEPropertyDef& aPropDef, 
       
   342                                               const TPtrC& aMimeType ) 
       
   343     {
       
   344     HBufC16* result = HBufC16::NewLC( aMimeType.Length() );
       
   345     TPtr16 modResult = result->Des();
       
   346     modResult.Copy( aMimeType );
       
   347 
       
   348     // strip possible left asterisk
       
   349     if ( modResult.Locate( '*' ) == 0 )
       
   350         {
       
   351         modResult.Copy( modResult.Right( modResult.Length() - 1 ));
       
   352         }
       
   353 
       
   354     if ( modResult.Length() > 0 )
       
   355         {
       
   356         // strip possible right asterisk
       
   357         if ( modResult.LocateReverse( '*' ) == modResult.Length() - 1 )
       
   358             {
       
   359             modResult.Copy( modResult.Left( modResult.Length() - 1 ));
       
   360             }
       
   361 
       
   362         // add type condition
       
   363         aLogicCondition.AddPropertyConditionL( aPropDef, ETextPropertyConditionCompareContains, modResult );
       
   364         }
       
   365     CleanupStack::PopAndDestroy( result );
       
   366     }
       
   367 
       
   368 //  End of File