controlpanelui/src/tonefetcher/tonefetcherengine/private/CToneSelection.cpp
changeset 22 a5692c68d772
child 29 313976a11e23
equal deleted inserted replaced
21:2883a5458389 22:a5692c68d772
       
     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 "CToneSelection.h"
       
    19 #include <pathinfo.h>
       
    20 #include <bautils.h>
       
    21 #include "tonefetcherengine.h"
       
    22 #include "MToneSelectionWatcher.h"
       
    23 #include <centralrepository.h>
       
    24 #include <ProfileEngineDomainCRKeys.h>
       
    25 #include <tonefetcherlogger.h>
       
    26 #include <QString>
       
    27 
       
    28 //refresh interval, 2 seconds.
       
    29 const TInt KTimerInterval = 2 * 1000 * 1000;
       
    30 const TInt KObserverCallStep = 100;
       
    31 // CONSTANTS
       
    32 _LIT( KMimeMp3, "mp3" );
       
    33 
       
    34 CMFActiveCaller* CMFActiveCaller::NewL( CToneSelection* aObserver )
       
    35     {
       
    36     CMFActiveCaller* self = new (ELeave) CMFActiveCaller( aObserver );
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );
       
    40 
       
    41     return self;
       
    42     }
       
    43 
       
    44 CMFActiveCaller::~CMFActiveCaller()
       
    45     {
       
    46     Cancel();
       
    47     iTimer.Close();
       
    48     }
       
    49 
       
    50 CMFActiveCaller::CMFActiveCaller(CToneSelection* aObserver) : CActive(CActive::EPriorityStandard)
       
    51     {
       
    52     iObserver = aObserver;
       
    53     }
       
    54 
       
    55 void CMFActiveCaller::ConstructL()
       
    56     {
       
    57     User::LeaveIfError( iTimer.CreateLocal() );
       
    58     CActiveScheduler::Add( this );
       
    59     }
       
    60 
       
    61 void CMFActiveCaller::DoCancel()
       
    62     {
       
    63     iTimer.Cancel();
       
    64     }
       
    65 
       
    66 void CMFActiveCaller::RunL()
       
    67     {
       
    68     iObserver->ChangeObject();
       
    69     }
       
    70 
       
    71 void CMFActiveCaller::Request()
       
    72     {
       
    73     Cancel();
       
    74     SetActive();
       
    75     TRequestStatus* status = &iStatus;
       
    76     User::RequestComplete( status, KErrNone );
       
    77     }
       
    78 
       
    79 void CMFActiveCaller::Start( TInt aMilliseconds )
       
    80     {    
       
    81     Cancel();
       
    82 
       
    83     if ( aMilliseconds <= 0 )
       
    84         {
       
    85         Request();  // no delay - complete asap
       
    86         }
       
    87     else
       
    88         {
       
    89         iTimer.After( iStatus, aMilliseconds );
       
    90         SetActive();
       
    91         }
       
    92     }
       
    93 
       
    94 void CMFActiveCaller::Stop()
       
    95     {
       
    96     Cancel();
       
    97     }
       
    98 
       
    99 CToneSelection* CToneSelection::NewL( MToneSelectionWatcher *aWatcher )
       
   100     {
       
   101     CToneSelection* self = CToneSelection::NewLC(aWatcher);
       
   102     CleanupStack::Pop( self );
       
   103     return self;
       
   104     }
       
   105 
       
   106 CToneSelection* CToneSelection::NewLC( MToneSelectionWatcher *aWatcher )
       
   107     {
       
   108     CToneSelection* self = new ( ELeave ) CToneSelection( aWatcher );
       
   109     CleanupStack::PushL( self );
       
   110     self->ConstructL();
       
   111     return self;
       
   112     }
       
   113 
       
   114 void CToneSelection::ConstructL()
       
   115     {
       
   116     iSession = CMdESession::NewL( *this );    
       
   117     iObjectNotificationCaller = CMFActiveCaller::NewL( this );    
       
   118     }
       
   119 
       
   120 CToneSelection::CToneSelection( MToneSelectionWatcher *aWatcher ) : iToneSelectionWatcher( aWatcher )
       
   121     {
       
   122     iMediaFileCounter = 0;
       
   123     iIsQuerying = EFalse;
       
   124     }
       
   125 
       
   126 CToneSelection::~CToneSelection()
       
   127     {
       
   128     iResultArray.ResetAndDestroy();
       
   129     delete iQuery;
       
   130     delete iSession;
       
   131     delete iObjectNotificationCaller;
       
   132     }
       
   133 
       
   134 void CToneSelection::HandleSessionOpened( CMdESession& /*aSession*/, TInt aError )
       
   135     {
       
   136     if ( aError != KErrNone )
       
   137         {
       
   138         iDefNS = 0;
       
   139         delete iSession;
       
   140         iSession = 0;
       
   141         iSessionOpen = EFalse;
       
   142         iToneSelectionWatcher->HandleMdeSessionError( aError );
       
   143         }
       
   144     else
       
   145         {
       
   146         iDefNS = &iSession->GetDefaultNamespaceDefL();
       
   147         iSessionOpen = ETrue;
       
   148         TRAP_IGNORE( AddObjectObserverL() );
       
   149         iToneSelectionWatcher->HandleMdeSessionOpened();
       
   150         }
       
   151     }
       
   152 
       
   153 
       
   154 
       
   155 void CToneSelection::HandleSessionError( CMdESession& /*aSession*/, TInt aError )
       
   156     {
       
   157     if ( aError == KErrNone )
       
   158         {
       
   159         return;
       
   160         }
       
   161         
       
   162     delete iSession;
       
   163     iSession = 0;
       
   164     iSessionOpen = EFalse;
       
   165     iToneSelectionWatcher->HandleMdeSessionError( aError );
       
   166     }
       
   167 
       
   168 void CToneSelection::HandleQueryNewResults( CMdEQuery& /*aQuery*/, 
       
   169                                                TInt /*aFirstNewItemIndex*/,
       
   170                                                TInt /*aNewItemCount*/ )
       
   171     {
       
   172     }
       
   173 
       
   174 void CToneSelection::HandleObjectNotification( CMdESession& /*aSession*/, 
       
   175                                         TObserverNotificationType aType,
       
   176                                         const RArray<TItemId>& aObjectIdArray )
       
   177     {   
       
   178     /*if ( aObjectIdArray.Count() > 0 && ( aType == ENotifyAdd || aType == ENotifyModify || aType == ENotifyRemove ) )
       
   179         {
       
   180         QString str("CToneSelection::HandleObjectNotification " + QString::number(aObjectIdArray.Count()) + " " + QString::number(aType));
       
   181         TF_LOG(str);        
       
   182         iMediaFileCounter = iMediaFileCounter + aObjectIdArray.Count();
       
   183         if ( iMediaFileCounter >= KObserverCallStep )
       
   184             {
       
   185             iMediaFileCounter = 0;
       
   186             iToneSelectionWatcher->HandleObjectChanged();
       
   187             }
       
   188         else 
       
   189             {
       
   190             iObjectNotificationCaller->Start(KTimerInterval);
       
   191             }        
       
   192         }*/
       
   193     }
       
   194 
       
   195 void CToneSelection::AddObjectObserverL()
       
   196     {
       
   197     if ( iSessionOpen )
       
   198         {
       
   199         TUint32 notificationType = ENotifyAdd | ENotifyModify | ENotifyRemove;        
       
   200         iSession->AddObjectObserverL( *this, 0, notificationType, iDefNS );
       
   201         
       
   202         iSession->AddObjectPresentObserverL( *this );
       
   203         }
       
   204     }
       
   205 
       
   206 void CToneSelection::HandleObjectPresentNotification( CMdESession& /*aSession*/, 
       
   207                          TBool /*aPresent*/, const RArray<TItemId>& aObjectIdArray )
       
   208     {
       
   209     
       
   210     if( aObjectIdArray.Count() > 0 )
       
   211         {
       
   212         //if query is executing, we do not allow the fresh of contents
       
   213         if ( iIsQuerying )
       
   214             {
       
   215             iMediaFileCounter = 0;
       
   216             return;
       
   217             }
       
   218         QString str("CToneSelection::HandleObjectPresentNotification " + QString::number(aObjectIdArray.Count()));
       
   219         TF_LOG(str);
       
   220         iMediaFileCounter = iMediaFileCounter + aObjectIdArray.Count();
       
   221         if ( iMediaFileCounter > KObserverCallStep )
       
   222             {
       
   223             iMediaFileCounter = 0;
       
   224             iToneSelectionWatcher->HandleObjectChanged();
       
   225             }
       
   226         else 
       
   227             {
       
   228             iObjectNotificationCaller->Start(KTimerInterval);
       
   229             }    
       
   230         }    
       
   231     }
       
   232 
       
   233 void CToneSelection::HandleQueryCompleted( CMdEQuery& aQuery, TInt aError )
       
   234     {
       
   235     iIsQuerying = EFalse;
       
   236     iResultArray.ResetAndDestroy();
       
   237     if ( aError == KErrCancel )
       
   238         {      
       
   239         iToneSelectionWatcher->HandleQueryError( aError );
       
   240         return;
       
   241         }
       
   242     else
       
   243         {
       
   244         CMdEObjectQuery* query = static_cast<CMdEObjectQuery*> (&aQuery);
       
   245         TInt count = query->Count();
       
   246         for (TInt i = 0; i < count; ++i)
       
   247             {
       
   248             CMdEObject* object =
       
   249                     (CMdEObject*) query->TakeOwnershipOfResult(i);
       
   250             CleanupStack::PushL(object);
       
   251             CMdEPropertyDef& propDef = 
       
   252                         CToneSelection::PropertyDefL( iSession, CToneSelection::EAttrSongName  );
       
   253                 
       
   254             CMdEProperty* property = 0;
       
   255             TInt err = object->Property( propDef, property, 0 );
       
   256             if ( err != KErrNotFound && property )
       
   257                 {            
       
   258                 HBufC* songUri = HBufC::NewL( object->Uri().Length() );
       
   259                 TPtr ptr = songUri->Des();
       
   260                 ptr.Copy( object->Uri() );
       
   261                 iResultArray.AppendL( songUri );
       
   262                 }
       
   263             CleanupStack::PopAndDestroy( object );
       
   264             }
       
   265         iToneSelectionWatcher->HandleQueryComplete( iResultArray );     
       
   266         }
       
   267     }
       
   268 
       
   269 void CToneSelection::QueryTonesL()
       
   270     {
       
   271     LeaveIfSessionClosedL();
       
   272     delete iQuery;
       
   273     iQuery = 0;
       
   274     CMdEObjectDef& musicObjectDef =
       
   275             iDefNS->GetObjectDefL( MdeConstants::Audio::KAudioObject );    
       
   276     iQuery = iSession->NewObjectQueryL( *iDefNS, musicObjectDef, this );    
       
   277     
       
   278     // set attributes that are included in query result  
       
   279     CMdEPropertyDef& namePropertyDef = PropertyDefL( EAttrSongName );
       
   280     iQuery->AddPropertyFilterL( &namePropertyDef );
       
   281     
       
   282     iQuery->SetResultMode( EQueryResultModeItem );
       
   283     
       
   284     CMdELogicCondition& conditions = iQuery->Conditions();
       
   285     ExcludeMusicPropertiesL( conditions );
       
   286     iIsQuerying = ETrue;
       
   287     iQuery->FindL();
       
   288     }
       
   289 
       
   290 void CToneSelection::LeaveIfSessionClosedL()
       
   291     {
       
   292     if ( !iSession || !iSessionOpen )
       
   293         {
       
   294         User::Leave( KErrDisconnected );
       
   295         }
       
   296     }
       
   297 
       
   298 CMdEPropertyDef& CToneSelection::PropertyDefL( TInt aAttr )
       
   299     {
       
   300     return PropertyDefL( iSession, aAttr );
       
   301     }
       
   302 
       
   303 CMdEPropertyDef& CToneSelection::PropertyDefL( CMdESession* aSession, TInt aAttr )
       
   304     {
       
   305     CMdEObjectDef& objectDef = 
       
   306             iDefNS->GetObjectDefL( MdeConstants::Audio::KAudioObject );
       
   307    
       
   308     if ( aAttr == EAttrFileSize )
       
   309         {
       
   310         return objectDef.GetPropertyDefL( MdeConstants::Object::KSizeProperty );
       
   311         }
       
   312     else if ( aAttr == EAttrMediaType )
       
   313         {
       
   314         return objectDef.GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   315         }
       
   316     else if ( aAttr == EAttrSongName || aAttr == EAttrFileName )
       
   317         {
       
   318         return objectDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   319         }
       
   320     else if ( aAttr == EAttrArtist )
       
   321         {
       
   322         return objectDef.GetPropertyDefL( MdeConstants::MediaObject::KArtistProperty );
       
   323         }
       
   324     else if ( aAttr == EAttrAlbum )
       
   325         {
       
   326         return objectDef.GetPropertyDefL( MdeConstants::Audio::KAlbumProperty );
       
   327         }
       
   328     else if ( aAttr == EAttrGenre )
       
   329         {
       
   330         return objectDef.GetPropertyDefL( MdeConstants::MediaObject::KGenreProperty );
       
   331         }
       
   332     else if ( aAttr == EAttrComposer )
       
   333         {
       
   334         return objectDef.GetPropertyDefL( MdeConstants::Audio::KComposerProperty );
       
   335         }
       
   336     else
       
   337         {
       
   338         User::Leave( KErrNotSupported );
       
   339         }
       
   340     }
       
   341 
       
   342 void CToneSelection::ExcludeMusicPropertiesL( CMdELogicCondition& aCondition )
       
   343     {
       
   344     TInt sizeLimitKB = 0;
       
   345     CRepository* cenrep = CRepository::NewL( KCRUidProfileEngine );
       
   346     CleanupStack::PushL( cenrep );
       
   347     User::LeaveIfError( cenrep->Get( KProEngRingingToneMaxSize, sizeLimitKB ) );
       
   348     CleanupStack::PopAndDestroy(); // cenrep
       
   349 
       
   350     SetAttr( CToneSelection::EAttrFileSize, sizeLimitKB );
       
   351     CMdEPropertyDef& sizeTypeDef = PropertyDefL( EAttrFileSize );
       
   352     CMdEPropertyDef& mimeTypeDef = PropertyDefL( EAttrMediaType );
       
   353     CMdEPropertyDef& artistTypeDef = PropertyDefL( EAttrArtist );
       
   354     CMdEPropertyDef& albumTypeDef = PropertyDefL( EAttrAlbum );
       
   355     CMdEPropertyDef& genreTypeDef = PropertyDefL( EAttrGenre );
       
   356     CMdEPropertyDef& composerTypeDef = PropertyDefL( EAttrComposer );
       
   357     
       
   358     CMdELogicCondition& condition = 
       
   359                         aCondition.AddLogicConditionL( ELogicConditionOperatorAnd );
       
   360     condition.AddPropertyConditionL( sizeTypeDef, TMdEIntRange(0, iMaxFileSize, EMdERangeTypeBetween) );
       
   361     condition.AddPropertyConditionL( mimeTypeDef, 
       
   362             ETextPropertyConditionCompareContains, KMimeMp3 );
       
   363     condition.AddPropertyConditionL( artistTypeDef );
       
   364     condition.AddPropertyConditionL( albumTypeDef );
       
   365     condition.AddPropertyConditionL( genreTypeDef );
       
   366     condition.AddPropertyConditionL( composerTypeDef );
       
   367     
       
   368     condition.SetNegate( ETrue );
       
   369     }
       
   370 
       
   371 void CToneSelection::SetAttr( int attr, int value )
       
   372 {
       
   373     switch ( attr )
       
   374         {
       
   375         case CToneSelection::EAttrFileSize:
       
   376             {
       
   377             iMaxFileSize = value;
       
   378             break;
       
   379             }            
       
   380         default:
       
   381             {
       
   382             break;
       
   383             }
       
   384         }
       
   385 }
       
   386 
       
   387 void CToneSelection::ChangeObject()
       
   388     {    
       
   389     if ( QueryReady() )
       
   390         {
       
   391         iToneSelectionWatcher->HandleObjectChanged();
       
   392         }
       
   393     }
       
   394 
       
   395 TBool CToneSelection::QueryReady() 
       
   396     {
       
   397     if ( iQuery )
       
   398         {
       
   399         return iQuery->IsComplete();    
       
   400         }
       
   401      
       
   402     return ETrue;
       
   403     }
       
   404 // End of File
       
   405