omads/omadsextensions/adapters/mediads/src/cmdemanager.cpp
branchRCL_3
changeset 24 8e7494275d3a
equal deleted inserted replaced
23:2bb96f4ecad8 24:8e7494275d3a
       
     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:  CMdEManager implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cmdemanager.h"
       
    20 #include <mdeconstants.h>
       
    21 
       
    22 #include <mmf\common\mmfcontrollerpluginresolver.h>
       
    23 
       
    24 #include "logger.h"
       
    25 
       
    26 // Warning:  #940-D: missing return statement at end of non-void function
       
    27 #pragma  diag_remark 940
       
    28 
       
    29 CMdEManager::CMdEManager( MMdEManagerObserver& aObserver ) :
       
    30     iObserver(aObserver)
       
    31     {
       
    32     }
       
    33 
       
    34 
       
    35 CMdEManager* CMdEManager::NewL( MMdEManagerObserver& aObserver )
       
    36     {
       
    37     CMdEManager* self = new (ELeave) CMdEManager( aObserver );
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43 
       
    44 void CMdEManager::ConstructL()
       
    45     {
       
    46     TRACE_FUNC;
       
    47     iMde = CMdESession::NewL( *this );
       
    48     }
       
    49 
       
    50 
       
    51 CMdEManager::~CMdEManager()
       
    52     {
       
    53     TRACE_FUNC;
       
    54     Cancel(); // Cancel any request, if outstanding
       
    55     delete iAlbumQuery;
       
    56     delete iObjectQuery;
       
    57     delete iContainmentQuery;
       
    58     delete iMde;
       
    59     
       
    60     iAlbumsInProgress.ResetAndDestroy();
       
    61     iAlbums.ResetAndDestroy();
       
    62     }
       
    63 
       
    64 void CMdEManager::GetAlbumsL()
       
    65     {
       
    66     TRACE_FUNC_ENTRY;
       
    67     iAlbumsInProgress.ResetAndDestroy();
       
    68     iAlbums.ResetAndDestroy();
       
    69     if ( !iMde )
       
    70         {
       
    71         LOGGER_WRITE("Session was not ready!");
       
    72         iState = EUninitialized;
       
    73         iMde = CMdESession::NewL( *this );
       
    74         }
       
    75     
       
    76     if ( iState == EUninitialized)
       
    77         {
       
    78         LOGGER_WRITE("Starting processing albums after session is ready");
       
    79         iState = EWaitingToEnumerateAlbums;
       
    80         }
       
    81     else if ( iState == EIdle )
       
    82         {
       
    83         StartProcessingAlbumsL();
       
    84         }
       
    85     else
       
    86         {
       
    87         LOGGER_WRITE_1("Wrong state: %d", iState);
       
    88         User::Leave( KErrGeneral );
       
    89         }
       
    90     
       
    91     TRACE_FUNC_EXIT;
       
    92     }
       
    93 
       
    94 const CPlaylistItem& CMdEManager::AlbumL( TInt aAlbumId ) const
       
    95     {
       
    96     for ( TInt i=0; i<iAlbums.Count(); i++ )
       
    97         {
       
    98         if ( iAlbums[i]->Id() == aAlbumId )
       
    99             {
       
   100             LOGGER_WRITE("Album found");
       
   101             return *iAlbums[i];
       
   102             }
       
   103         }
       
   104     LOGGER_WRITE_1("CMdEManager::AlbumL - aAlbumId %d does not exist - Leaving KErrNotFound", aAlbumId);
       
   105     User::Leave( KErrNotFound );
       
   106     }
       
   107 
       
   108 void CMdEManager::CreateAlbumL( CPlaylistItem& aAlbum )
       
   109     {
       
   110     TRACE_FUNC_ENTRY;
       
   111     if ( !iMde || iState != EIdle )
       
   112         {
       
   113         LOGGER_WRITE("Not ready!");
       
   114         User::Leave( KErrNotReady );
       
   115         }
       
   116     CMdENamespaceDef& defaultNamespaceDef = iMde->GetDefaultNamespaceDefL();
       
   117     CMdEObjectDef& albumObjDef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Album::KAlbumObject );
       
   118     
       
   119     CMdEPropertyDef& titlePropDef = albumObjDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );   
       
   120     
       
   121     // Validate album name
       
   122     TInt albumLength = aAlbum.Title().Length();
       
   123     if ( albumLength < titlePropDef.MinTextLengthL() || 
       
   124          albumLength > titlePropDef.MaxTextLengthL() )
       
   125         {
       
   126         LOGGER_WRITE("Album length is not on valid range!");
       
   127         User::Leave( KErrBadName );
       
   128         }
       
   129     
       
   130     CMdEObject* albumObject = iMde->NewObjectLC( albumObjDef, KNullDesC );
       
   131     
       
   132     CMdEPropertyDef& sizePropDef = albumObjDef.GetPropertyDefL( MdeConstants::Object::KSizeProperty );
       
   133     albumObject->AddUint32PropertyL(sizePropDef, 0);
       
   134     CMdEPropertyDef& creationDatePropDef = albumObjDef.GetPropertyDefL( MdeConstants::Object::KCreationDateProperty );
       
   135     CMdEPropertyDef& lastModifiedDatePropDef = albumObjDef.GetPropertyDefL( MdeConstants::Object::KLastModifiedDateProperty );
       
   136     
       
   137     TTime timeDate;
       
   138     timeDate.UniversalTime();
       
   139     
       
   140     albumObject->AddTimePropertyL( creationDatePropDef, timeDate );
       
   141     albumObject->AddTimePropertyL( lastModifiedDatePropDef, timeDate );
       
   142     
       
   143     CMdEPropertyDef& typePropDef = albumObjDef.GetPropertyDefL( MdeConstants::Object::KItemTypeProperty );
       
   144     albumObject->AddTextPropertyL( typePropDef,MdeConstants::Album::KAlbumItemType );
       
   145     
       
   146     albumObject->AddTextPropertyL( titlePropDef, aAlbum.Title() );
       
   147     TItemId newAlbumId = iMde->AddObjectL( *albumObject );
       
   148     
       
   149     CleanupStack::PopAndDestroy( albumObject );
       
   150     
       
   151     if ( newAlbumId == KNoId )
       
   152         {
       
   153         LOGGER_WRITE("Adding album failed!");
       
   154         User::Leave( KErrGeneral );
       
   155         }
       
   156     LOGGER_WRITE_1("New almbum created, id: %d", newAlbumId);
       
   157     
       
   158     CMdERelationDef& relationDef = defaultNamespaceDef.GetRelationDefL( MdeConstants::Relations::KContains );
       
   159     TMdEObject mediaObject;
       
   160     RPointerArray<CMdEInstanceItem> relations;
       
   161     CleanupResetAndDestroyPushL( relations);
       
   162     for ( TInt i=0; i<aAlbum.ItemCount(); i++ )
       
   163         {
       
   164         TRAPD(err, iMde->CheckObjectL( mediaObject, aAlbum.ItemAt(i) ));
       
   165         LOGGER_WRITE_1("url: %S", &aAlbum.ItemAt(i));
       
   166         LOGGER_WRITE_1("CheckObjectL err: %d", err);
       
   167         if ( !err )
       
   168             {
       
   169             LOGGER_WRITE_1("object def: %S", &mediaObject.DefL().Name());
       
   170             if ( mediaObject.DefL().Name().Compare( MdeConstants::Image::KImageObject ) == 0 
       
   171                     || mediaObject.DefL().Name().Compare( MdeConstants::Video::KVideoObject ) == 0 )
       
   172                 {
       
   173                 CMdERelation* relation = iMde->NewRelationL( relationDef, newAlbumId, mediaObject.Id() );
       
   174                 relations.AppendL( relation );
       
   175                 }
       
   176             else
       
   177                 {
       
   178                 LOGGER_WRITE("type not supported");
       
   179                 }
       
   180             }
       
   181         }
       
   182     
       
   183     if ( relations.Count() > 0 )
       
   184         {
       
   185         TInt err = iMde->AddItemsL( relations );
       
   186         LOGGER_WRITE_1("AddItemsL first err: %d", err);
       
   187         }
       
   188     
       
   189     CleanupStack::PopAndDestroy( &relations );
       
   190     
       
   191     aAlbum.SetId( newAlbumId );
       
   192     
       
   193     TRACE_FUNC_EXIT;
       
   194     }
       
   195 
       
   196 void CMdEManager::ReplaceAlbumL( TInt aAlbumId, CPlaylistItem& aAlbum )
       
   197     {
       
   198     TRACE_FUNC_ENTRY;
       
   199     if ( !iMde || iState != EIdle )
       
   200         {
       
   201         LOGGER_WRITE("Not ready!");
       
   202         User::Leave( KErrNotReady );
       
   203         }
       
   204     // get old album from local cache and check do we need to change album title
       
   205     const CPlaylistItem& oldAlbum = AlbumL( aAlbumId );
       
   206     CMdENamespaceDef& defaultNamespaceDef = iMde->GetDefaultNamespaceDefL();
       
   207     if ( oldAlbum.Title().Compare( aAlbum.Title() ) != 0 )
       
   208         {
       
   209         // Title changed, open item from MdE and update it
       
   210         
       
   211         CMdEObjectDef& albumObjDef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Album::KAlbumObject );
       
   212         
       
   213         CMdEPropertyDef& titlePropDef = albumObjDef.GetPropertyDefL( 
       
   214                 MdeConstants::Object::KTitleProperty );
       
   215         
       
   216         // Validate album name
       
   217         TInt albumLength = aAlbum.Title().Length();
       
   218         if ( albumLength < titlePropDef.MinTextLengthL() || 
       
   219              albumLength > titlePropDef.MaxTextLengthL() )
       
   220             {
       
   221             LOGGER_WRITE("Album length is not on valid range!");
       
   222             User::Leave( KErrBadName );
       
   223             }
       
   224         
       
   225         CMdEObject* albumObject = iMde->OpenObjectL( aAlbumId, albumObjDef );
       
   226         
       
   227         CMdEProperty* titleProp = NULL;
       
   228         TInt index = albumObject->Property( titlePropDef, titleProp );
       
   229         if ( index == KErrNotFound )
       
   230             {
       
   231             LOGGER_WRITE("Cannot find title property");
       
   232             User::Leave( KErrCorrupt );
       
   233             }
       
   234         
       
   235         LOGGER_WRITE("Change title property");
       
   236         titleProp->SetTextValueL( aAlbum.Title() );
       
   237         
       
   238         CMdEPropertyDef& lastModDatePropDef = albumObjDef.GetPropertyDefL(
       
   239                 MdeConstants::Object::KLastModifiedDateProperty );
       
   240         CMdEProperty* lastModDateProp = NULL;
       
   241         index = albumObject->Property( lastModDatePropDef, lastModDateProp );
       
   242         
       
   243         if ( index == KErrNotFound )
       
   244             {
       
   245             LOGGER_WRITE("Cannot find lastModDateProp property");
       
   246             User::Leave( KErrCorrupt );
       
   247             }
       
   248         
       
   249         TTime now;
       
   250         now.UniversalTime();
       
   251         lastModDateProp->SetTimeValueL( now );
       
   252         
       
   253         iMde->CommitObjectL( *albumObject );
       
   254         }
       
   255     
       
   256     // Update album and content relations
       
   257     // Search added relations
       
   258     CMdERelationDef& containsRelationDef = defaultNamespaceDef.GetRelationDefL( MdeConstants::Relations::KContains );
       
   259     TMdEObject mediaObject;
       
   260     RPointerArray<CMdEInstanceItem> addedRelations;
       
   261     CleanupResetAndDestroyPushL( addedRelations);
       
   262     for ( TInt i=0; i< aAlbum.ItemCount(); i++)
       
   263         {
       
   264         TInt index(KErrNotFound);
       
   265         TInt foundRes = oldAlbum.FindItem( aAlbum.ItemAt(i) ,index );
       
   266         if ( foundRes != 0 )
       
   267             {
       
   268             // Item not found for old album -> Added relation
       
   269             // Find object by uri
       
   270             TRAPD( err, iMde->CheckObjectL( mediaObject, aAlbum.ItemAt(i) ));
       
   271             LOGGER_WRITE_1("url: %S", &aAlbum.ItemAt(i));
       
   272             LOGGER_WRITE_1("CheckObjectL err: %d", err);
       
   273             if ( !err )
       
   274                 {
       
   275                 LOGGER_WRITE_1("object def: %S", &mediaObject.DefL().Name());
       
   276                 if ( mediaObject.DefL().Name().Compare( MdeConstants::Image::KImageObject ) == 0 
       
   277                         || mediaObject.DefL().Name().Compare( MdeConstants::Video::KVideoObject ) == 0 )
       
   278                     {
       
   279                     CMdERelation* relation = iMde->NewRelationL( containsRelationDef, aAlbumId, mediaObject.Id() );
       
   280                     addedRelations.AppendL( relation );
       
   281                     }
       
   282                 else
       
   283                     {
       
   284                     LOGGER_WRITE("type not supported");
       
   285                     }
       
   286                 }
       
   287             
       
   288             }
       
   289         }
       
   290     if ( addedRelations.Count() > 0 )
       
   291         {
       
   292         TInt err = iMde->AddItemsL( addedRelations );
       
   293         LOGGER_WRITE_1("AddItemsL first err: %d", err);
       
   294         }
       
   295     
       
   296     CleanupStack::PopAndDestroy( &addedRelations );
       
   297     
       
   298     // search removed relations
       
   299     if ( iContainmentQuery )
       
   300         {
       
   301         delete iContainmentQuery;
       
   302         iContainmentQuery = NULL;
       
   303         }
       
   304     iContainmentQuery = iMde->NewRelationQueryL( defaultNamespaceDef, this );
       
   305     
       
   306     CMdELogicCondition& rootCondition = iContainmentQuery->Conditions();
       
   307     CMdERelationCondition& relationCondition =
       
   308         rootCondition.AddRelationConditionL(containsRelationDef,
       
   309                                             ERelationConditionSideLeft); // "AND"
       
   310     
       
   311     CMdELogicCondition& leftCondition = relationCondition.LeftL();
       
   312     CMdELogicCondition& rightCondition = relationCondition.RightL();
       
   313     
       
   314     leftCondition.AddObjectConditionL( aAlbumId );
       
   315     
       
   316     CMdELogicCondition& objectDefLogicCond = 
       
   317             rightCondition.AddLogicConditionL( ELogicConditionOperatorOr);
       
   318     
       
   319     TBool removingRelationsNeeded( EFalse );
       
   320     for ( TInt i=0; i< oldAlbum.ItemCount(); i++)
       
   321         {
       
   322         TInt index(KErrNotFound);
       
   323         TInt foundRes = aAlbum.FindItem( oldAlbum.ItemAt(i) ,index );
       
   324         if ( foundRes != 0 )
       
   325             {
       
   326             removingRelationsNeeded = ETrue;
       
   327             // Item not found from new album -> Removed relation (add uri to search condition)
       
   328             LOGGER_WRITE_1("relation to be removed, uri: %S", &oldAlbum.ItemAt(i) );
       
   329             objectDefLogicCond.AddObjectConditionL( EObjectConditionCompareUri, oldAlbum.ItemAt(i) );
       
   330             }
       
   331         }
       
   332     
       
   333     if ( removingRelationsNeeded )
       
   334         {
       
   335         // find all removed relation ID:s. HandleRelationQueryCompleted will be called when ready.
       
   336         iContainmentQuery->SetResultMode( EQueryResultModeId );
       
   337         iContainmentQuery->FindL();
       
   338         iState = EReplacingAlbum;
       
   339         }
       
   340     else
       
   341         {
       
   342         // All done
       
   343         iState = EIdle;
       
   344         iObserver.AlbumReplaced( KErrNone );
       
   345         }
       
   346     
       
   347     TRACE_FUNC_EXIT;
       
   348     }
       
   349 
       
   350 void CMdEManager::DeleteAlbumL( TInt aAlbumId )
       
   351     {
       
   352     TRACE_FUNC_ENTRY;
       
   353     if ( !iMde || iState != EIdle )
       
   354         {
       
   355         LOGGER_WRITE("Not ready!");
       
   356         User::Leave( KErrNotReady );
       
   357         }
       
   358     
       
   359     CMdENamespaceDef& defaultNamespaceDef = iMde->GetDefaultNamespaceDefL();
       
   360     CMdEObjectDef& albumObjDef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Album::KAlbumObject );
       
   361     
       
   362     CMdEObject* albumObject = iMde->GetObjectL( aAlbumId, albumObjDef );
       
   363     
       
   364     CMdEPropertyDef& typePropDef = albumObjDef.GetPropertyDefL( 
       
   365            MdeConstants::Album::KTypeProperty );
       
   366     
       
   367     CMdEProperty* typeProp = NULL;
       
   368     TInt index = albumObject->Property( typePropDef, typeProp );
       
   369     if ( index != KErrNotFound )
       
   370         {
       
   371         TUint16 typeVal = typeProp->Uint16ValueL();
       
   372         if ( typeVal == MdeConstants::Album::EAlbumSystemFavourite )
       
   373             {
       
   374             LOGGER_WRITE("Item type is EAlbumSystemFavourite, deletion not allowed!");
       
   375             User::Leave( KErrPermissionDenied );
       
   376             }
       
   377         }
       
   378     
       
   379     TItemId removedId(KNoId);
       
   380     removedId = iMde->RemoveObjectL( aAlbumId );
       
   381     if ( removedId == KNoId )
       
   382         {
       
   383         LOGGER_WRITE("Deletion failed!");
       
   384         User::Leave( KErrNotFound );
       
   385         }
       
   386     
       
   387     TRACE_FUNC_EXIT;
       
   388     }
       
   389 
       
   390 void CMdEManager::StartProcessingAlbumsL()
       
   391     {
       
   392     TRACE_FUNC_ENTRY;
       
   393     CMdENamespaceDef& defaultNamespaceDef = iMde->GetDefaultNamespaceDefL();
       
   394     CMdEObjectDef& albumObjDef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Album::KAlbumObject );
       
   395     if ( iAlbumQuery )
       
   396         {
       
   397         delete iAlbumQuery;
       
   398         iAlbumQuery = NULL;
       
   399         }
       
   400     // query objects with object definition "Album"
       
   401     iAlbumQuery = iMde->NewObjectQueryL( defaultNamespaceDef, albumObjDef, this );
       
   402     
       
   403     // Add order
       
   404     CMdEObjectDef& objdef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Object::KBaseObject );
       
   405     CMdEPropertyDef& propDef = objdef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   406     TMdEOrderRule rule( propDef, ETrue );
       
   407     iAlbumQuery->AppendOrderRuleL( rule );
       
   408     
       
   409     iAlbumQuery->FindL();
       
   410     
       
   411     iState = EEnumeratingAlbums;
       
   412 
       
   413     TRACE_FUNC_EXIT;
       
   414     }
       
   415     
       
   416 
       
   417 void CMdEManager::FindItemsOnAlbumL( TItemId aAlbumObjectId )
       
   418     {
       
   419     TRACE_FUNC_ENTRY;
       
   420     if ( !iMde )
       
   421         {
       
   422         LOGGER_WRITE("Session was not ready!");
       
   423         User::Leave( KErrNotReady );
       
   424         }
       
   425     CMdENamespaceDef& defaultNamespaceDef = iMde->GetDefaultNamespaceDefL();
       
   426     CMdEObjectDef& objDef = defaultNamespaceDef.GetObjectDefL(  MdeConstants::Object::KBaseObject );
       
   427     
       
   428     CMdEPropertyDef& titlePropDef = objDef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   429     
       
   430     if ( iObjectQuery )
       
   431         {
       
   432         delete iObjectQuery;
       
   433         iObjectQuery = NULL;
       
   434         }
       
   435     iObjectQuery = iMde->NewObjectQueryL( defaultNamespaceDef, objDef, this );
       
   436     
       
   437     // get only "Title" property
       
   438     iObjectQuery->AddPropertyFilterL( &titlePropDef );
       
   439     
       
   440     CMdEObjectDef& objdef = defaultNamespaceDef.GetObjectDefL( MdeConstants::Object::KBaseObject );
       
   441     CMdEPropertyDef& propDef = objdef.GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   442 
       
   443     CMdELogicCondition& rootCond = iObjectQuery->Conditions();
       
   444 
       
   445     CMdERelationDef& containsRelDef = defaultNamespaceDef.GetRelationDefL( 
       
   446         MdeConstants::Relations::KContains );
       
   447 
       
   448     // query right side objects from relations
       
   449     CMdERelationCondition& relCond = rootCond.AddRelationConditionL( 
       
   450         containsRelDef, ERelationConditionSideRight );
       
   451 
       
   452     // left side object of relation must be defined album object
       
   453     CMdELogicCondition& leftRelCond = relCond.LeftL();
       
   454     leftRelCond.AddObjectConditionL( aAlbumObjectId );
       
   455     
       
   456     iObjectQuery->FindL();
       
   457 
       
   458     TRACE_FUNC_EXIT;
       
   459     }
       
   460 
       
   461 /**
       
   462  * Called to notify the observer that opening the session has been 
       
   463  * completed and, if the opening succeeded, the session is ready for use.
       
   464  *
       
   465  * @param aSession session
       
   466  * @param aError   <code>KErrNone</code>, if opening the session succeeded;
       
   467  *                 or one of the system-wide error codes, if opening the 
       
   468  *                 session failed
       
   469  */
       
   470 void CMdEManager::HandleSessionOpened(CMdESession& /*aSession*/, TInt aError)
       
   471     {
       
   472     TRACE_FUNC_ENTRY;
       
   473     if ( !aError )
       
   474         {
       
   475         LOGGER_WRITE("Session opened");
       
   476         if ( iState == EWaitingToEnumerateAlbums )
       
   477             {
       
   478             iState = EIdle;
       
   479             TRAPD(err, StartProcessingAlbumsL());
       
   480             if ( err )
       
   481                 {
       
   482                 iObserver.AlbumsReaded( err );
       
   483                 }
       
   484             }
       
   485         else
       
   486             {
       
   487             iState = EIdle;
       
   488             }
       
   489         }
       
   490     else
       
   491         {
       
   492         LOGGER_WRITE_1("Error happened on opening session, aError: %d", aError);
       
   493         if ( iState == EWaitingToEnumerateAlbums )
       
   494             {
       
   495             iObserver.AlbumsReaded( aError );
       
   496             }
       
   497         iState = EUninitialized;
       
   498         delete iMde;
       
   499         iMde = NULL;
       
   500         }
       
   501     TRACE_FUNC_EXIT;
       
   502     }
       
   503 
       
   504 /**
       
   505  * Called to notify the observer about errors, which are not a direct 
       
   506  * consequence of the operations initiated by the client but caused by 
       
   507  * some external source (e.g., other clients). The error cannot be 
       
   508  * recovered and all on-going operations initiated by the client have been 
       
   509  * aborted. Any attempts to continue using the session will cause a panic. 
       
   510  * The client should close the session immediately and try to open a new 
       
   511  * session, if it needs to continue using the metadata engine.
       
   512  *
       
   513  * @param aSession session
       
   514  * @param aError one of the system-wide error codes
       
   515  */
       
   516 void CMdEManager::HandleSessionError(CMdESession& /*aSession*/, TInt aError)
       
   517     {
       
   518     // Something went wrong. Handle the error and delete the old session.
       
   519     LOGGER_WRITE_1("CMdEManager::HandleSessionError - aError: %d", aError)
       
   520     iState = EUninitialized;
       
   521     delete iMde;
       
   522     iMde = NULL;
       
   523     }
       
   524 
       
   525 /**
       
   526  * Called to notify the observer that new results have been received 
       
   527  * in the query.
       
   528  *
       
   529  * @param aQuery              Query instance that received new results.
       
   530  * @param aFirstNewItemIndex  Index of the first new item that was added
       
   531  *                            to the result item array.
       
   532  * @param aNewItemCount       Number of items added to the result item 
       
   533  *                            array.
       
   534  */
       
   535 void CMdEManager::HandleQueryNewResults(CMdEQuery& /*aQuery*/,
       
   536                                    TInt /*aFirstNewItemIndex*/,
       
   537                                    TInt /*aNewItemCount*/)
       
   538     {
       
   539     }
       
   540 
       
   541 void CMdEManager::ProcessNextAlbumL()
       
   542     {
       
   543     TRACE_FUNC_ENTRY;
       
   544     if ( iAlbumsInProgress.Count() == 0 )
       
   545         {
       
   546         LOGGER_WRITE("All ready");
       
   547         // all ready
       
   548         return;
       
   549         }
       
   550     
       
   551     FindItemsOnAlbumL( iAlbumsInProgress[0]->Id() );
       
   552     
       
   553     TRACE_FUNC_EXIT;
       
   554     }
       
   555 
       
   556         
       
   557 /**
       
   558  * Called to notify the observer that the query has been completed,
       
   559  * or that an error has occured.
       
   560  *
       
   561  * @param aQuery  Query instance.
       
   562  * @param aError  <code>KErrNone</code>, if the query was completed
       
   563  *                successfully. Otherwise one of the system-wide error 
       
   564  *                codes.
       
   565  */
       
   566 void CMdEManager::HandleQueryCompleted(CMdEQuery& aQuery, TInt aError)
       
   567     {
       
   568     TRACE_FUNC_ENTRY;
       
   569     TInt err( aError );
       
   570     
       
   571     if ( iState == EEnumeratingAlbums )
       
   572         {
       
   573         CMdEObjectQuery& query = (CMdEObjectQuery&)aQuery;
       
   574         if ( !err && &aQuery == iAlbumQuery )
       
   575             {
       
   576             LOGGER_WRITE("Album query");
       
   577             TRAP( err, HandleAlbumQueryCompletedL( query ));
       
   578             }
       
   579         else if ( !err && &aQuery == iObjectQuery )
       
   580             {
       
   581             LOGGER_WRITE("Object query");
       
   582             TRAP( err, HandleObjectQueryCompletedL( query ));
       
   583             }
       
   584         else
       
   585             {
       
   586             LOGGER_WRITE("unknown query or error happened");
       
   587             }
       
   588         
       
   589         if ( err )
       
   590             {
       
   591             LOGGER_WRITE_1("Error happened: %d", err);
       
   592             iState = EIdle;
       
   593             iAlbumsInProgress.ResetAndDestroy();
       
   594             iAlbums.ResetAndDestroy();
       
   595             iObserver.AlbumsReaded( err );
       
   596             }
       
   597         }
       
   598     else if ( iState == EReplacingAlbum )
       
   599         {
       
   600         if ( !err && &aQuery == iContainmentQuery )
       
   601             {
       
   602             LOGGER_WRITE("relation query");
       
   603             CMdERelationQuery& query = (CMdERelationQuery&)aQuery;
       
   604             TRAP( err, HandleRelationQueryCompletedL( query ));
       
   605             }
       
   606         
       
   607         iState = EIdle;
       
   608         iObserver.AlbumReplaced( err );
       
   609         
       
   610         }
       
   611     TRACE_FUNC_EXIT;
       
   612     }
       
   613 
       
   614 
       
   615 // -----------------------------------------------------------------------------
       
   616 // CMdEManager::HandleAlbumQueryCompletedL
       
   617 // Callback function for find all albums. Save albums to iAlbumsInProgress array 
       
   618 // and start collecting objects on each album.
       
   619 // -----------------------------------------------------------------------------
       
   620 void CMdEManager::HandleAlbumQueryCompletedL( CMdEObjectQuery& aQuery )
       
   621     {
       
   622     TRACE_FUNC_ENTRY;
       
   623     // clear old items
       
   624     iAlbumsInProgress.ResetAndDestroy();
       
   625     iAlbums.ResetAndDestroy();
       
   626     
       
   627     for ( TInt i = 0; i < aQuery.Count(); i++ )
       
   628         {
       
   629         CPlaylistItem* albumItem = CPlaylistItem::NewLC();
       
   630         CMdEObject& object = aQuery.Result(i);
       
   631         LOGGER_WRITE_1("Id: %d", object.Id());
       
   632         albumItem->SetId( object.Id() );
       
   633         
       
   634         CMdEPropertyDef& titlePropDef = object.Def().GetPropertyDefL( MdeConstants::Object::KTitleProperty );
       
   635               
       
   636         CMdEProperty* titleProp = NULL;
       
   637         TInt index = object.Property( titlePropDef, titleProp );
       
   638         if ( index != KErrNotFound )
       
   639             {
       
   640             const TDesC& title = titleProp->TextValueL();
       
   641             LOGGER_WRITE_1("Title: %S", &title);
       
   642             albumItem->SetTitleL( title );
       
   643             }
       
   644         else
       
   645             {
       
   646             // not found, might be default album
       
   647             albumItem->SetTitleL( object.Uri() );
       
   648             }
       
   649         
       
   650         LOGGER_WRITE("");
       
   651         iAlbumsInProgress.AppendL( albumItem );
       
   652         CleanupStack::Pop( albumItem );
       
   653         }
       
   654     
       
   655     if ( aQuery.Count() > 0 )
       
   656         {
       
   657         // Start processing albums
       
   658         ProcessNextAlbumL();
       
   659         }
       
   660     else
       
   661         {
       
   662         // no albums
       
   663         iObserver.AlbumsReaded( KErrNone );
       
   664         }
       
   665     TRACE_FUNC_EXIT;
       
   666     }
       
   667 
       
   668 void CMdEManager::HandleObjectQueryCompletedL( CMdEObjectQuery& aQuery )
       
   669     {
       
   670     if ( !iAlbumsInProgress.Count() )
       
   671         {
       
   672         LOGGER_WRITE("Error! No items on iAlbumsInProgress");
       
   673         User::Leave( KErrGeneral );
       
   674         }
       
   675     for ( TInt i = 0; i < aQuery.Count(); i++ )
       
   676         {
       
   677         CMdEObject& object = aQuery.Result(i);
       
   678         iAlbumsInProgress[0]->AddItemL( object.Uri() );
       
   679         }
       
   680     iAlbums.AppendL( iAlbumsInProgress[0] );
       
   681     iAlbumsInProgress.Remove( 0 );
       
   682     if ( iAlbumsInProgress.Count() > 0)
       
   683         {
       
   684         ProcessNextAlbumL();
       
   685         }
       
   686     else
       
   687         {
       
   688         // all albums processed
       
   689         iState = EIdle;
       
   690         iObserver.AlbumsReaded( KErrNone );
       
   691         }
       
   692     }
       
   693 
       
   694 void CMdEManager::HandleRelationQueryCompletedL( CMdERelationQuery& aQuery )
       
   695     {
       
   696     TRACE_FUNC_ENTRY;
       
   697     const RArray<TItemId>& res = aQuery.ResultIds();
       
   698     LOGGER_WRITE_1("founded relations count: %d", res.Count())
       
   699     for (TInt i=0; i<res.Count(); i++ )
       
   700         {
       
   701         LOGGER_WRITE_1(" %d", res[i]);
       
   702         }
       
   703     LOGGER_WRITE_1("q count: %d", aQuery.Count() );
       
   704     RArray<TItemId> removed;
       
   705     CMdENamespaceDef& defaultNameSpace = iMde->GetDefaultNamespaceDefL();
       
   706     iMde->RemoveRelationsL( aQuery.ResultIds(),removed, &defaultNameSpace);
       
   707     LOGGER_WRITE_1("Removed relations count: %d", removed.Count())
       
   708     for (TInt i=0; i<removed.Count(); i++)
       
   709         {
       
   710         LOGGER_WRITE_1(" %d", removed[i]);
       
   711         }
       
   712     TRACE_FUNC_EXIT;
       
   713     }
       
   714 
       
   715 void CMdEManager::Cancel()
       
   716     {
       
   717     TRACE_FUNC_ENTRY;
       
   718     if ( iState == EWaitingToEnumerateAlbums )
       
   719         {
       
   720         // Cancel enumeration before it even begins
       
   721         iState = EUninitialized;
       
   722         iObserver.AlbumsReaded( KErrCancel );
       
   723         }
       
   724     
       
   725     if ( iAlbumQuery )
       
   726         {
       
   727         LOGGER_WRITE("iAlbumQuery->Cancel()");
       
   728         iAlbumQuery->Cancel();
       
   729         }
       
   730     if ( iObjectQuery )
       
   731         {
       
   732         LOGGER_WRITE("iObjectQuery->Cancel()");
       
   733         iObjectQuery->Cancel();
       
   734         }
       
   735     if ( iContainmentQuery )
       
   736         {
       
   737         LOGGER_WRITE("iContainmentQuery->Cancel()");
       
   738         iContainmentQuery->Cancel();
       
   739         }
       
   740     TRACE_FUNC_EXIT;
       
   741     }