controlpanelui/src/tonefetcher/tonefetcherengine/private/symbian/toneselectionengine_p.cpp
changeset 19 36aa4756ee82
child 21 2883a5458389
equal deleted inserted replaced
12:624337f114fe 19:36aa4756ee82
       
     1 /*
       
     2  * Copyright (c) 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  *     The source file for mde tone fetcher.
       
    16  *     
       
    17  */
       
    18 #include "toneselectionengine_p.h"
       
    19 #include <XQConversions>
       
    20 #include <pathinfo.h>
       
    21 #include <bautils.h>
       
    22 #include "tonepreviewprivate.h"
       
    23 #include "tonefetcherengine.h"
       
    24 #include <centralrepository.h>
       
    25 #include <ProfileEngineDomainCRKeys.h>
       
    26 
       
    27 ToneSelectionEnginePrivate::ToneSelectionEnginePrivate( ToneFetcherEngine *engine ) : mServiceEngine( engine )
       
    28 
       
    29     {
       
    30     iSession = CMdESession::NewL( *this );
       
    31     
       
    32     }
       
    33 
       
    34 ToneSelectionEnginePrivate::~ToneSelectionEnginePrivate()
       
    35     {
       
    36     delete iQuery;
       
    37     delete iSession;
       
    38     }
       
    39 
       
    40 void ToneSelectionEnginePrivate::HandleSessionOpened( CMdESession& /*aSession*/, TInt aError )
       
    41     {
       
    42     if ( aError != KErrNone )
       
    43         {
       
    44         delete iSession;
       
    45         iSession = 0;
       
    46         iSessionOpen = EFalse;
       
    47         emit mdeSessionError( aError );
       
    48         }
       
    49     else
       
    50         {
       
    51         iSessionOpen = ETrue;
       
    52         TRAP_IGNORE( AddObjectObserverL() );
       
    53         emit mdeSessionOpened();
       
    54         }
       
    55     }
       
    56 
       
    57 
       
    58 
       
    59 void ToneSelectionEnginePrivate::HandleSessionError( CMdESession& /*aSession*/, TInt aError )
       
    60     {
       
    61     if ( aError == KErrNone )
       
    62         {
       
    63         return;
       
    64         }
       
    65         
       
    66     delete iSession;
       
    67     iSession = 0;
       
    68     iSessionOpen = EFalse;
       
    69     emit mdeSessionError( aError );            
       
    70     }
       
    71 
       
    72 
       
    73 void ToneSelectionEnginePrivate::HandleQueryNewResults( CMdEQuery& /*aQuery*/, 
       
    74                                                TInt /*aFirstNewItemIndex*/,
       
    75                                                TInt /*aNewItemCount*/ )
       
    76     {
       
    77     }
       
    78 
       
    79 void ToneSelectionEnginePrivate::HandleObjectNotification( CMdESession& /*aSession*/, 
       
    80                                         TObserverNotificationType aType,
       
    81                                         const RArray<TItemId>& /*aObjectIdArray*/ )
       
    82     {    
       
    83     if ( aType == ENotifyAdd || aType == ENotifyModify || aType == ENotifyRemove )
       
    84         {
       
    85         emit notifyObjectChanged();
       
    86         }
       
    87     }
       
    88 
       
    89 void ToneSelectionEnginePrivate::AddObjectObserverL()
       
    90     {
       
    91     if ( iSessionOpen )
       
    92         {
       
    93         TUint32 notificationType = ENotifyAdd | ENotifyModify | ENotifyRemove;
       
    94         CMdENamespaceDef& defNS = iSession->GetDefaultNamespaceDefL();
       
    95         iSession->AddObjectObserverL( *this, 0, notificationType, &defNS );
       
    96         
       
    97         iSession->AddObjectPresentObserverL( *this );
       
    98         }
       
    99     }
       
   100 
       
   101 void ToneSelectionEnginePrivate::HandleObjectPresentNotification( CMdESession& /*aSession*/, 
       
   102                          TBool /*aPresent*/, const RArray<TItemId>& aObjectIdArray )
       
   103     {
       
   104     if( aObjectIdArray.Count() > 0 )
       
   105         {
       
   106         emit notifyObjectChanged();
       
   107         }    
       
   108     }
       
   109 
       
   110 void ToneSelectionEnginePrivate::HandleQueryCompleted( CMdEQuery& aQuery, TInt aError )
       
   111     {
       
   112     iNameList.clear();
       
   113     iUriList.clear();
       
   114     if ( aError == KErrCancel )
       
   115         {      
       
   116         emit queryError( aError );
       
   117         return;
       
   118         }
       
   119     else
       
   120         {
       
   121         CMdEObjectQuery* query = static_cast<CMdEObjectQuery*> (&aQuery);
       
   122         TInt count = query->Count();
       
   123         for (TInt i = 0; i < count; ++i)
       
   124             {
       
   125             CMdEObject* object =
       
   126                     (CMdEObject*) query->TakeOwnershipOfResult(i);
       
   127             CleanupStack::PushL(object);
       
   128             CMdEPropertyDef& propDef = 
       
   129                         ToneSelectionEnginePrivate::PropertyDefL( iSession, ToneSelectionEnginePrivate::EAttrSongName  );
       
   130                 
       
   131             CMdEProperty* property = 0;
       
   132             TInt err = object->Property( propDef, property, 0 );
       
   133             if ( err != KErrNotFound && property )
       
   134                 {
       
   135                 QString songName( XQConversions::s60DescToQString( property->TextValueL() ) );
       
   136                 QString uriValue( XQConversions::s60DescToQString( object->Uri() ) );
       
   137                 iNameList.append( songName );
       
   138                 iUriList.append( uriValue );
       
   139                 }
       
   140             CleanupStack::PopAndDestroy(object);
       
   141             }
       
   142         emit queryComplete(iNameList, iUriList );        
       
   143         }
       
   144     }
       
   145 
       
   146 void ToneSelectionEnginePrivate::QueryTones()
       
   147     {
       
   148     LeaveIfSessionClosedL();
       
   149         
       
   150     CMdENamespaceDef& defNS = iSession->GetDefaultNamespaceDefL();
       
   151     CMdEObjectDef& musicObjectDef =
       
   152     defNS.GetObjectDefL( MdeConstants::Audio::KAudioObject );
       
   153   
       
   154     delete iQuery;
       
   155     iQuery = 0;
       
   156     iQuery = iSession->NewObjectQueryL( defNS, musicObjectDef, this );    
       
   157     
       
   158     
       
   159     // set attributes that are included in query result  
       
   160     CMdEPropertyDef& namePropertyDef = PropertyDefL( EAttrSongName );
       
   161     iQuery->AddPropertyFilterL( &namePropertyDef );
       
   162     
       
   163     iQuery->SetResultMode( EQueryResultModeItem );
       
   164     
       
   165     CMdELogicCondition& conditions = iQuery->Conditions();
       
   166     ExcludeMusicPropertiesL( conditions );
       
   167 
       
   168     iQuery->FindL();
       
   169     }
       
   170 
       
   171 void ToneSelectionEnginePrivate::LeaveIfSessionClosedL()
       
   172     {
       
   173     if ( !iSession || !iSessionOpen )
       
   174         {
       
   175         User::Leave( KErrDisconnected );
       
   176         }
       
   177     }
       
   178 
       
   179 CMdEPropertyDef& ToneSelectionEnginePrivate::PropertyDefL( TInt aAttr )
       
   180     {
       
   181     return PropertyDefL( iSession, aAttr );
       
   182     }
       
   183 
       
   184 CMdEPropertyDef& ToneSelectionEnginePrivate::PropertyDefL( CMdESession* aSession, TInt aAttr )
       
   185     {
       
   186     CMdENamespaceDef& defNS = aSession->GetDefaultNamespaceDefL();
       
   187     
       
   188     CMdEObjectDef& objectDef =
       
   189     defNS.GetObjectDefL( MdeConstants::Audio::KAudioObject );
       
   190    
       
   191     if ( aAttr == EAttrFileSize )
       
   192         {
       
   193         return objectDef.GetPropertyDefL( MdeConstants::Object::KSizeProperty );
       
   194         }
       
   195     else if ( aAttr == EAttrMediaType )
       
   196         {
       
   197         return objectDef.GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   198         }
       
   199     else if ( aAttr == EAttrSongName || aAttr == EAttrFileName )
       
   200         {
       
   201         return objectDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   202         }
       
   203     else if ( aAttr == EAttrArtist )
       
   204         {
       
   205         return objectDef.GetPropertyDefL( MdeConstants::MediaObject::KArtistProperty );
       
   206         }
       
   207     else if ( aAttr == EAttrAlbum )
       
   208         {
       
   209         return objectDef.GetPropertyDefL( MdeConstants::Audio::KAlbumProperty );
       
   210         }
       
   211     else if ( aAttr == EAttrGenre )
       
   212         {
       
   213         return objectDef.GetPropertyDefL( MdeConstants::MediaObject::KGenreProperty );
       
   214         }
       
   215     else if ( aAttr == EAttrComposer )
       
   216         {
       
   217         return objectDef.GetPropertyDefL( MdeConstants::Audio::KComposerProperty );
       
   218         }
       
   219     else
       
   220         {
       
   221         User::Leave( KErrNotSupported );
       
   222         }
       
   223     
       
   224     return objectDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   225     }
       
   226 
       
   227 void ToneSelectionEnginePrivate::ExcludeMusicPropertiesL( CMdELogicCondition& aCondition )
       
   228     {
       
   229     TInt sizeLimitKB = 0;
       
   230     CRepository* cenrep = CRepository::NewL( KCRUidProfileEngine );
       
   231     CleanupStack::PushL( cenrep );
       
   232     User::LeaveIfError( cenrep->Get( KProEngRingingToneMaxSize, sizeLimitKB ) );
       
   233     CleanupStack::PopAndDestroy(); // cenrep
       
   234 
       
   235     SetAttr( ToneFetcherEngine::EAttrFileSize, sizeLimitKB );
       
   236     CMdEPropertyDef& sizeTypeDef = PropertyDefL( EAttrFileSize );
       
   237     CMdEPropertyDef& mimeTypeDef = PropertyDefL( EAttrMediaType );
       
   238     CMdEPropertyDef& artistTypeDef = PropertyDefL( EAttrArtist );
       
   239     CMdEPropertyDef& albumTypeDef = PropertyDefL( EAttrAlbum );
       
   240     CMdEPropertyDef& genreTypeDef = PropertyDefL( EAttrGenre );
       
   241     CMdEPropertyDef& composerTypeDef = PropertyDefL( EAttrComposer );
       
   242     
       
   243     CMdELogicCondition& condition = 
       
   244                         aCondition.AddLogicConditionL( ELogicConditionOperatorAnd );
       
   245     condition.AddPropertyConditionL( sizeTypeDef, TMdEIntRange(0, iMaxFileSize, EMdERangeTypeBetween) );
       
   246     condition.AddPropertyConditionL( mimeTypeDef, 
       
   247             ETextPropertyConditionCompareContains, KMimeMp3 );
       
   248     condition.AddPropertyConditionL( artistTypeDef );
       
   249     condition.AddPropertyConditionL( albumTypeDef );
       
   250     condition.AddPropertyConditionL( genreTypeDef );
       
   251     condition.AddPropertyConditionL( composerTypeDef );
       
   252     
       
   253     condition.SetNegate( ETrue );
       
   254     }
       
   255 
       
   256 void ToneSelectionEnginePrivate::SetAttr( int attr, int value )
       
   257 {
       
   258     switch ( attr )
       
   259         {
       
   260         case ToneFetcherEngine::EAttrFileSize:
       
   261             {
       
   262             iMaxFileSize = value;
       
   263             break;
       
   264             }            
       
   265         default:
       
   266             {
       
   267             break;
       
   268             }
       
   269         }
       
   270 }
       
   271 // End of File
       
   272