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