mpserviceplugins/mpxsqlitedbhgplugin/src/mpxdbalbum.cpp
changeset 51 560ce2306a17
parent 38 b93f525c9244
child 55 f3930dda3342
equal deleted inserted replaced
47:4cc1412daed0 51:560ce2306a17
    35 // CONSTANTS
    35 // CONSTANTS
    36 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
    36 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
    37 _LIT( KAbstractAlbumExt, ".alb" );
    37 _LIT( KAbstractAlbumExt, ".alb" );
    38 #endif // ABSTRACTAUDIOALBUM_INCLUDED
    38 #endif // ABSTRACTAUDIOALBUM_INCLUDED
    39 
    39 
       
    40 _LIT( KAlbumAlbumArtistSeperator, "\t1");
       
    41 _LIT( KAlbumArtistSeperator, "\t2");
       
    42 
       
    43 const TInt KUnknownAlbumsGranularity = 250;
       
    44 
    40 // ============================ MEMBER FUNCTIONS ==============================
    45 // ============================ MEMBER FUNCTIONS ==============================
    41 
    46 
    42 // ----------------------------------------------------------------------------
    47 // ----------------------------------------------------------------------------
    43 // Two-phased constructor.
    48 // Two-phased constructor.
    44 // ----------------------------------------------------------------------------
    49 // ----------------------------------------------------------------------------
    77 // ----------------------------------------------------------------------------
    82 // ----------------------------------------------------------------------------
    78 //
    83 //
    79 CMPXDbAlbum::~CMPXDbAlbum()
    84 CMPXDbAlbum::~CMPXDbAlbum()
    80     {
    85     {
    81     MPX_FUNC("CMPXDbAlbum::~CMPXDbAlbum");
    86     MPX_FUNC("CMPXDbAlbum::~CMPXDbAlbum");
       
    87     
       
    88     iUnknownArtists.ResetAndDestroy();
       
    89     iUnknownAlbums.ResetAndDestroy();
    82     }
    90     }
    83 
    91 
    84 // ----------------------------------------------------------------------------
    92 // ----------------------------------------------------------------------------
    85 // Constructor
    93 // Constructor
    86 // ----------------------------------------------------------------------------
    94 // ----------------------------------------------------------------------------
    88 CMPXDbAlbum::CMPXDbAlbum(
    96 CMPXDbAlbum::CMPXDbAlbum(
    89     CMPXDbManager& aDbManager,
    97     CMPXDbManager& aDbManager,
    90     TMPXGeneralCategory aCategory,
    98     TMPXGeneralCategory aCategory,
    91     MMPXDbAlbumObserver& aObserver) :
    99     MMPXDbAlbumObserver& aObserver) :
    92     CMPXDbCategory(aDbManager, aCategory),
   100     CMPXDbCategory(aDbManager, aCategory),
    93     iObserver(aObserver)
   101     iObserver(aObserver),
       
   102     iUnknownArtists(KUnknownAlbumsGranularity),
       
   103     iUnknownAlbums(KUnknownAlbumsGranularity)
    94     {
   104     {
    95     MPX_FUNC("CMPXDbAlbum::CMPXDbAlbum");
   105     MPX_FUNC("CMPXDbAlbum::CMPXDbAlbum");
    96     }
   106     }
    97 
   107 
    98 // ----------------------------------------------------------------------------
   108 // ----------------------------------------------------------------------------
   109 // ----------------------------------------------------------------------------
   119 // ----------------------------------------------------------------------------
   110 // CMPXDbAlbum::AddItemL
   120 // CMPXDbAlbum::AddItemL
   111 // ----------------------------------------------------------------------------
   121 // ----------------------------------------------------------------------------
   112 //
   122 //
   113 TUint32 CMPXDbAlbum::AddItemL(
   123 TUint32 CMPXDbAlbum::AddItemL(
   114     const TDesC& aName,
   124     TMPXGeneralCategory /*aCategory*/,        
   115     const TDesC& aArtistName,
   125     const CMPXMedia& aMedia,
   116     const TDesC& aArt,
       
   117     TInt aDriveId,
   126     TInt aDriveId,
   118     TBool& aNewRecord,
   127     TBool& aNewRecord,
   119     TBool aCaseSensitive)
   128     TBool aCaseSensitive)
   120     {
   129     {
   121     MPX_FUNC("CMPXDbAlbum::AddItemL");
   130     MPX_FUNC("CMPXDbAlbum::AddItemL");
   122 
   131 
       
   132     TPtrC album(KNullDesC);
       
   133     TPtrC albumArtist(KNullDesC);
       
   134     TPtrC artist(KNullDesC);
       
   135     TPtrC albumArt(KNullDesC);
       
   136     
   123     // try to find the item first
   137     // try to find the item first
   124     TUint32 rowId(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), iCategory,
   138     TUint32 rowId(GenerateUniqueIdL(aMedia));
   125         aName, aCaseSensitive));
   139     
   126     aNewRecord = !CategoryItemExistsL(aDriveId, rowId);
   140     // retrieve the existing record
   127 
   141     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
   128     if (aNewRecord)
   142     RSqlStatement recordset(
   129         {
   143         iDbManager.ExecuteSelectQueryL(aDriveId, *query, rowId));
       
   144     CleanupStack::PopAndDestroy(query);
       
   145     CleanupClosePushL(recordset);
       
   146     
       
   147     TInt result = recordset.Next();   
       
   148     if (result == KSqlAtEnd)
       
   149         {
       
   150         aNewRecord = result;
       
   151         
       
   152         // check Album attribute
       
   153         if (aMedia.IsSupported(KMPXMediaMusicAlbum))     
       
   154             {
       
   155             album.Set(aMedia.ValueText(KMPXMediaMusicAlbum).Left(KMCMaxTextLen));
       
   156             }
       
   157         
       
   158         // check AlbumArt attirbute
       
   159         if (aMedia.IsSupported(KMPXMediaMusicAlbumArtFileName))
       
   160             {
       
   161             albumArt.Set(aMedia.ValueText(KMPXMediaMusicAlbumArtFileName).Left(KMCMaxTextLen));
       
   162             }
       
   163 
   130         // insert new
   164         // insert new
   131         HBufC* query = PreProcessStringLC(KQueryAlbumInsert);
   165         HBufC* name = MPXDbCommonUtil::ProcessSingleQuotesLC(album);
   132         HBufC* name = MPXDbCommonUtil::ProcessSingleQuotesLC(aName);
   166         TUint32 artistId(0);
   133         HBufC* artistname = MPXDbCommonUtil::ProcessSingleQuotesLC(aArtistName);
   167         HBufC* artistName = NULL;        
   134 		HBufC* art = MPXDbCommonUtil::ProcessSingleQuotesLC(aArt);
   168         if (RetrieveAlbumArtistL(aMedia, albumArtist))
   135 
   169             {
   136         iDbManager.ExecuteQueryL(aDriveId, *query, rowId, artistname, name, 1, art); 
   170             artistId = iObserver.AddAlbumArtistL(albumArtist, albumArt, aDriveId);
   137 
   171             artistName = MPXDbCommonUtil::ProcessSingleQuotesLC(albumArtist);
   138 		CleanupStack::PopAndDestroy(art);
   172             }
   139         CleanupStack::PopAndDestroy(artistname);
   173         else
       
   174             {
       
   175             RetrieveArtist(aMedia, artist);
       
   176             artistId = iObserver.AddAlbumArtistL(artist, albumArt, aDriveId);
       
   177             artistName = MPXDbCommonUtil::ProcessSingleQuotesLC(artist);
       
   178             }
       
   179         
       
   180         HBufC* art = MPXDbCommonUtil::ProcessSingleQuotesLC(albumArt);
       
   181                 
       
   182         iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumInsert, rowId, name, 1, artistId, artistName,art); 
       
   183 
       
   184         CleanupStack::PopAndDestroy(art);
       
   185         CleanupStack::PopAndDestroy(artistName);
   140         CleanupStack::PopAndDestroy(name);
   186         CleanupStack::PopAndDestroy(name);
   141         CleanupStack::PopAndDestroy(query);
   187         }
   142         }
   188     else if (result == KSqlAtRow)
   143     else
   189         {       
   144         {
   190         // retrieve Art from Album table
   145         // retrieve the existing record
       
   146         HBufC* query = NULL;
       
   147         query = PreProcessStringLC(KQueryCategoryItem);
       
   148         RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, rowId));
       
   149         CleanupStack::PopAndDestroy(query);
       
   150 
       
   151         CleanupClosePushL(recordset);
       
   152 
       
   153         if (recordset.Next() != KSqlAtRow)
       
   154             {
       
   155             User::Leave(KErrNotFound);
       
   156 	    }
       
   157 
       
   158         // Artist
       
   159         TPtrC artistname(KNullDesC);
       
   160         artistname.Set(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArtistName));
       
   161 
       
   162         // the current one is Unknown and the new one is Not Unknown.
       
   163          if ( artistname == KNullDesC  && aArtistName!= KNullDesC )
       
   164             {
       
   165             HBufC* artistnameReplaceSingleQuote = 
       
   166                             MPXDbCommonUtil::ProcessSingleQuotesLC( aArtistName );
       
   167             TPtrC criteria(KCriterionArtistName);
       
   168             HBufC* setStr = HBufC::NewLC(criteria.Length() + artistnameReplaceSingleQuote->Length()); 
       
   169             setStr->Des().Format( KCriterionArtistName, artistnameReplaceSingleQuote );
       
   170            
       
   171             iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, rowId);
       
   172             CleanupStack::PopAndDestroy(setStr);
       
   173             CleanupStack::PopAndDestroy(artistnameReplaceSingleQuote);
       
   174             }
       
   175 
       
   176         // Album Art
       
   177         TPtrC art(KNullDesC);
   191         TPtrC art(KNullDesC);
   178         art.Set(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArt));
   192         art.Set(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArt));
   179 
   193 
   180         // the current one is Unknown and the new one is Not Unknown
   194         // the current one is Unknown and the new one is Not Unknown
   181         if ( art == KNullDesC && aArt != KNullDesC )
   195         if ( art == KNullDesC && albumArt != KNullDesC )
   182             {
   196             {
   183             HBufC* artReplaceSingleQuote = 
   197             HBufC* artReplaceSingleQuote = 
   184                             MPXDbCommonUtil::ProcessSingleQuotesLC( aArt );
   198                 MPXDbCommonUtil::ProcessSingleQuotesLC( albumArt );
   185 
   199 
   186             TPtrC criteria(KCriterionArt);
   200             TPtrC criteria(KCriterionArt);
   187             HBufC* setStr = HBufC::NewLC(criteria.Length() + artReplaceSingleQuote->Length()); 
   201             HBufC* setStr = HBufC::NewLC(criteria.Length() + artReplaceSingleQuote->Length()); 
   188             setStr->Des().Format( KCriterionArt, artReplaceSingleQuote );
   202             setStr->Des().Format( KCriterionArt, artReplaceSingleQuote );
   189 
   203 
   190             iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, rowId);
   204             iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, rowId);
   191             CleanupStack::PopAndDestroy(setStr);
   205             CleanupStack::PopAndDestroy(setStr);
   192             CleanupStack::PopAndDestroy(artReplaceSingleQuote);
   206             CleanupStack::PopAndDestroy(artReplaceSingleQuote);
   193             }
   207             }
   194 
       
   195         CleanupStack::PopAndDestroy(&recordset);
       
   196 
   208 
   197         // increment the number of songs for the category
   209         // increment the number of songs for the category
   198         query = PreProcessStringLC(KQueryCategoryIncrementSongCount);
   210         query = PreProcessStringLC(KQueryCategoryIncrementSongCount);
   199         iDbManager.ExecuteQueryL(aDriveId, *query, rowId);
   211         iDbManager.ExecuteQueryL(aDriveId, *query, rowId);
   200         CleanupStack::PopAndDestroy(query);
   212         CleanupStack::PopAndDestroy(query);
   201         }
   213         } // existing record
   202 
   214     else
       
   215         {
       
   216         MPX_DEBUG2("SQL error %d", result);
       
   217         User::Leave(result);
       
   218         }
       
   219 
       
   220     CleanupStack::PopAndDestroy(&recordset);
       
   221     
   203     return rowId;
   222     return rowId;
   204     }
   223     }
   205 
   224 
   206 // ----------------------------------------------------------------------------
   225 // ----------------------------------------------------------------------------
   207 // CMPXDbAlbum::DecrementSongsForCategoryL
   226 // CMPXDbAlbum::DecrementSongsForAlbumL
   208 // ----------------------------------------------------------------------------
   227 // ----------------------------------------------------------------------------
   209 //
   228 //
   210 void CMPXDbAlbum::DecrementSongsForCategoryL(
   229 void CMPXDbAlbum::DecrementSongsForAlbumL(
   211     const TUint32 aId,
   230     const TUint32 aId,
   212     TInt aDriveId,
   231     TInt aDriveId,
   213     CMPXMessageArray* aItemChangedMessages,
   232     CMPXMessageArray* aItemChangedMessages,
   214     TBool& aItemExist,
   233     TBool& aItemExist,
   215     const TUint32 aArtist,
   234     const TDesC& aArt)       
   216     const TDesC& aArt)
   235     {
   217     {
   236     MPX_FUNC("CMPXDbAlbum::DecrementSongsForAlbumL");
   218     MPX_FUNC("CMPXDbAlbum::DecrementSongsForCategoryL");
   237 
       
   238     // retrieve the existing record
       
   239     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   240     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, aId));
       
   241     CleanupStack::PopAndDestroy(query);
       
   242 
       
   243     CleanupClosePushL(recordset);
       
   244 
       
   245     if (recordset.Next() != KSqlAtRow)
       
   246         {
       
   247         User::Leave(KErrNotFound);
       
   248         }
   219 
   249 
   220     // if just one song uses this category. Use <= just in case
   250     // if just one song uses this category. Use <= just in case
   221     if (GetSongsCountL(aDriveId, aId) <= 1)
   251     if (recordset.ColumnInt64(EAlbumSongCount) <= 1)
   222         {
   252         {
   223         aItemExist = EFalse;
   253         aItemExist = EFalse;
       
   254 
       
   255         // Decrement albums count on artist       
       
   256         TUint32 albumArtistId = recordset.ColumnInt64(EAlbumArtist);
       
   257         iObserver.DeleteAlbumForArtistL(albumArtistId, aDriveId, aItemChangedMessages);
       
   258         CleanupStack::PopAndDestroy(&recordset);
       
   259                 
   224         // delete the category
   260         // delete the category
   225         DeleteCategoryL(aId, aDriveId);
   261         DeleteCategoryL(aId, aDriveId);
   226 
   262 
   227         if (aItemChangedMessages)
   263         if (aItemChangedMessages)
   228             {
   264             {
   233         }
   269         }
   234     else
   270     else
   235         {
   271         {
   236         aItemExist = ETrue;
   272         aItemExist = ETrue;
   237 
   273 
   238         // retrieve the existing record
       
   239         HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   240         RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, aId));
       
   241         CleanupStack::PopAndDestroy(query);
       
   242 
       
   243         CleanupClosePushL(recordset);
       
   244 
       
   245         if (recordset.Next() != KSqlAtRow)
       
   246             {
       
   247             User::Leave(KErrNotFound);
       
   248         }
       
   249 
       
   250         TBool itemModified = EFalse;
   274         TBool itemModified = EFalse;
   251         TPtrC artistname(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArtistName));
   275         		
   252         TUint32 artistId = MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), EMPXArtist,
   276         TPtrC art(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArt));        
   253 	            artistname, ETrue);
   277         if (NeedToUpdateArt(aArt, art))
   254 
       
   255         // the current artist is equal to deleted song's artist
       
   256         if ( artistId == aArtist )
       
   257             {
       
   258             HBufC* newArtistname = ArtistForAlbumL(aId);
       
   259             
       
   260             CleanupStack::PushL(newArtistname);
       
   261             if (newArtistname && newArtistname->CompareF(artistname)!=0)
       
   262                 { 
       
   263                 HBufC* artistnameReplaceSingleQuote = MPXDbCommonUtil::ProcessSingleQuotesLC( *newArtistname );
       
   264                 TPtrC criteria(KCriterionArtistName);
       
   265                 HBufC* setStr = HBufC::NewLC(criteria.Length() + artistnameReplaceSingleQuote->Length()); 
       
   266                 setStr->Des().Format( KCriterionArtistName, artistnameReplaceSingleQuote );                    	
       
   267 
       
   268                 iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, aId);
       
   269                 CleanupStack::PopAndDestroy(setStr);
       
   270                 CleanupStack::PopAndDestroy(artistnameReplaceSingleQuote);
       
   271                 itemModified = ETrue;
       
   272                 }
       
   273             CleanupStack::PopAndDestroy(newArtistname);             
       
   274             }         
       
   275         
       
   276 #ifdef ABSTRACTAUDIOALBUM_INCLUDED      
       
   277     TBool nonEmbeddedArt = EFalse;
       
   278     if (aArt.Length()>0)
       
   279         {
       
   280         TParsePtrC parse(aArt);
       
   281         TPtrC ext(parse.Ext());
       
   282         //set flag to false, so .alb will not overwrite art field in album, artist table 
       
   283         // when song with embedded art
       
   284         if (ext.CompareF(KAbstractAlbumExt)== 0) 
       
   285             {     
       
   286             nonEmbeddedArt = ETrue;        
       
   287             }
       
   288         }
       
   289   if (!nonEmbeddedArt)       
       
   290        {
       
   291 #endif // ABSTRACTAUDIOALBUM_INCLUDED
       
   292        TPtrC art(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArt));
       
   293         // the current art is equal to deleted song's art      
       
   294         if(aArt.Length()>0 && art.Length()>0 && aArt.CompareF(art) ==0 )
       
   295             {
   278             {
   296             MPX_DEBUG2("AlbumArt of the Song to be deleted is [%S]", &aArt);
   279             MPX_DEBUG2("AlbumArt of the Song to be deleted is [%S]", &aArt);
   297             
   280             
   298             //need to get alternative art in the same album to display
   281             //need to get alternative art in the same album to display
   299             HBufC* newArt = AlbumartForAlbumL(aId, aArt);
   282             HBufC* newArt = AlbumartForAlbumL(aId, aArt);
   300             CleanupStack::PushL(newArt);
   283             CleanupStack::PushL(newArt);
   301 
   284 
   302             //update Album table only if alternative albumart found
   285             //update Album table only if alternative albumart found
   303             if (newArt)
   286             if (newArt)
   304                 {
   287                 {
   305                 MPX_DEBUG1("CMPXDbAlbum::DecrementSongsForCategoryL, get newArt");
   288                 MPX_DEBUG1("CMPXDbAlbum::DecrementSongsForAlbumL, get newArt");
   306                 HBufC* artReplaceSingleQuote = MPXDbCommonUtil::ProcessSingleQuotesLC( *newArt );
   289                 HBufC* artReplaceSingleQuote = MPXDbCommonUtil::ProcessSingleQuotesLC( *newArt );
   307                 TPtrC criteria(KCriterionArt);   
   290                 TPtrC criteria(KCriterionArt);   
   308                 HBufC* setStr = HBufC::NewLC(criteria.Length() + artReplaceSingleQuote->Length()); 
   291                 HBufC* setStr = HBufC::NewLC(criteria.Length() + artReplaceSingleQuote->Length()); 
   309                 setStr->Des().Format( KCriterionArt, artReplaceSingleQuote );
   292                 setStr->Des().Format( KCriterionArt, artReplaceSingleQuote );
   310 
   293 
   311                 iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, aId);
   294                 iDbManager.ExecuteQueryL(aDriveId, KQueryAlbumUpdate, setStr, aId);
   312                 CleanupStack::PopAndDestroy(setStr);
   295                 CleanupStack::PopAndDestroy(setStr);
   313                 CleanupStack::PopAndDestroy(artReplaceSingleQuote);
   296                 CleanupStack::PopAndDestroy(artReplaceSingleQuote);
   314                 itemModified = ETrue;                     
   297                 itemModified = ETrue;                     
   315                 }
   298                 }
   316            CleanupStack::PopAndDestroy(newArt);             
   299             CleanupStack::PopAndDestroy(newArt);             
   317            }
   300             }
   318 #ifdef ABSTRACTAUDIOALBUM_INCLUDED            
   301 
   319          }
       
   320 #endif // ABSTRACTAUDIOALBUM_INCLUDED
       
   321         if (aItemChangedMessages && itemModified)
   302         if (aItemChangedMessages && itemModified)
   322            {
   303             {
   323            // add the item changed message
   304             // add the item changed message
   324            MPXDbCommonUtil::AddItemAlbumChangedMessageL(*aItemChangedMessages, aId, EMPXItemModified,
   305             MPXDbCommonUtil::AddItemAlbumChangedMessageL(*aItemChangedMessages, aId, EMPXItemModified,
   325                       EMPXAlbum, KDBPluginUid, ETrue, 0 );
   306                 EMPXAlbum, KDBPluginUid, ETrue, 0 );
   326            }
   307             }
   327         CleanupStack::PopAndDestroy(&recordset);
   308         CleanupStack::PopAndDestroy(&recordset);
   328         
   309         
   329         // decrement the number of songs for the category
   310         // decrement the number of songs for the category
   330         query = PreProcessStringLC(KQueryCategoryDecrementSongCount);
   311         query = PreProcessStringLC(KQueryCategoryDecrementSongCount);
   331         iDbManager.ExecuteQueryL(aDriveId, *query, aId);
   312         iDbManager.ExecuteQueryL(aDriveId, *query, aId);
   341     const TArray<TMPXAttribute>& aAttrs,
   322     const TArray<TMPXAttribute>& aAttrs,
   342     CMPXMediaArray& aMediaArray)
   323     CMPXMediaArray& aMediaArray)
   343     {
   324     {
   344     MPX_FUNC("CMPXDbAlbum::GetAllCategoryItemsL");
   325     MPX_FUNC("CMPXDbAlbum::GetAllCategoryItemsL");
   345 
   326 
   346     HBufC* query = PreProcessStringLC(KQueryAlbumAll);
   327     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryAlbumAll));
   347     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   348     CleanupStack::PopAndDestroy(query);
       
   349 
   328 
   350     CleanupClosePushL(recordset);
   329     CleanupClosePushL(recordset);
   351     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
   330     TRAPD(err, ProcessAlbumRecordSetL(aAttrs, recordset, aMediaArray));
       
   331     if (err != KErrNone)
       
   332         {
       
   333         iUnknownArtists.ResetAndDestroy();
       
   334         iUnknownAlbums.ResetAndDestroy();
       
   335 
       
   336         User::LeaveIfError(err);
       
   337         }
   352     CleanupStack::PopAndDestroy(&recordset);
   338     CleanupStack::PopAndDestroy(&recordset);
   353     }
   339     }
   354 
   340 
   355 // ----------------------------------------------------------------------------
   341 // ----------------------------------------------------------------------------
   356 // CMPXDbAlbum::GetAllCategoryItemsMediaWallL
   342 // CMPXDbAlbum::GetAllCategoryItemsMediaWallL
   360     const TArray<TMPXAttribute>& aAttrs,
   346     const TArray<TMPXAttribute>& aAttrs,
   361     CMPXMediaArray& aMediaArray)
   347     CMPXMediaArray& aMediaArray)
   362     {
   348     {
   363     MPX_FUNC("CMPXDbAlbum::GetAllCategoryItemsL");
   349     MPX_FUNC("CMPXDbAlbum::GetAllCategoryItemsL");
   364 
   350 
   365     HBufC* query = PreProcessStringLC(KQueryAlbumAllMediaWall);
   351     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryAlbumAllMediaWall));
   366     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   367     CleanupStack::PopAndDestroy(query);
       
   368 
   352 
   369     CleanupClosePushL(recordset);
   353     CleanupClosePushL(recordset);
   370     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
   354     TRAPD(err, ProcessMediaWallAlbumRecordSetL(aAttrs, recordset, aMediaArray));
       
   355     if (err != KErrNone)
       
   356         {
       
   357         iUnknownArtists.ResetAndDestroy();
       
   358         iUnknownAlbums.ResetAndDestroy();
       
   359 
       
   360         User::LeaveIfError(err);
       
   361         }
   371     CleanupStack::PopAndDestroy(&recordset);
   362     CleanupStack::PopAndDestroy(&recordset);
   372     }
   363     }
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // CMPXDbCategory::GetSubCategoryItemsL
       
   367 // ----------------------------------------------------------------------------
       
   368 //
       
   369 void CMPXDbAlbum::GetSubCategoryItemsL(
       
   370     TMPXGeneralCategory aParentCategory,
       
   371     TUint32 aParentId,
       
   372     const TArray<TMPXAttribute>& aAttrs,
       
   373     CMPXMediaArray& aMediaArray)
       
   374     {
       
   375     MPX_FUNC("CMPXDbCategory::GetSubCategoryItemsL");
       
   376 
       
   377     // this is only valid for albums belonging to an artist
       
   378     ASSERT((iCategory == EMPXAlbum) && (aParentCategory == EMPXArtist));
       
   379 
       
   380     // to handle the UREL warning
       
   381     (void)aParentCategory;
       
   382 
       
   383     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryMusicGetAlbumsforArtist, aParentId));
       
   384     CleanupClosePushL(recordset);
       
   385     ProcessAlbumRecordSetL(aAttrs, recordset, aMediaArray);
       
   386     CleanupStack::PopAndDestroy(&recordset);
       
   387     }
       
   388 
   373 // ----------------------------------------------------------------------------
   389 // ----------------------------------------------------------------------------
   374 // CMPXDbAlbum::UpdateItemL
   390 // CMPXDbAlbum::UpdateItemL
   375 // ----------------------------------------------------------------------------
   391 // ----------------------------------------------------------------------------
   376 //
   392 //
   377 void CMPXDbAlbum::UpdateItemL(
   393 void CMPXDbAlbum::UpdateItemL(
   402         }
   418         }
   403 
   419 
   404 	CleanupStack::PopAndDestroy(setStr);
   420 	CleanupStack::PopAndDestroy(setStr);
   405 	CleanupStack::PopAndDestroy(values);
   421 	CleanupStack::PopAndDestroy(values);
   406 	CleanupStack::PopAndDestroy(fields);
   422 	CleanupStack::PopAndDestroy(fields);
   407 	}
       
   408 
       
   409 // ----------------------------------------------------------------------------
       
   410 // CMPXDbAlbum::GetAlbumsCountForArtistL
       
   411 // ----------------------------------------------------------------------------
       
   412 //
       
   413 TInt CMPXDbAlbum::GetAlbumsCountForArtistL(TUint32 aArtistId)
       
   414 	{
       
   415     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(KQueryCategorySubcategoryItems, aArtistId));
       
   416 
       
   417     TInt prevId(0);
       
   418     TInt count(0);
       
   419 	TInt err(KErrNone);
       
   420 
       
   421     while ((err = recordset.Next()) == KSqlAtRow)
       
   422 		{
       
   423 		TUint32 rowId(recordset.ColumnInt64(EAlbumUniqueId));
       
   424         if (prevId == rowId)
       
   425             {
       
   426             continue;
       
   427             }
       
   428 
       
   429 		count++;
       
   430 		prevId = rowId;
       
   431 		}
       
   432 
       
   433     if (err != KSqlAtEnd)
       
   434         {
       
   435         User::Leave(err);
       
   436         }
       
   437 
       
   438 	return count;
       
   439 
       
   440 	}
   423 	}
   441 
   424 
   442 // ----------------------------------------------------------------------------
   425 // ----------------------------------------------------------------------------
   443 // CMPXDbAlbum::GetSongsCountInAlbumMatchingArtistL
   426 // CMPXDbAlbum::GetSongsCountInAlbumMatchingArtistL
   444 // ----------------------------------------------------------------------------
   427 // ----------------------------------------------------------------------------
   499 			{
   482 			{
   500 			if (attributeId & EMPXMediaMusicArtist)
   483 			if (attributeId & EMPXMediaMusicArtist)
   501 				{
   484 				{
   502 				MPX_DEBUG1("	EMPXMediaMusicArtist");
   485 				MPX_DEBUG1("	EMPXMediaMusicArtist");
   503 
   486 
   504 				TPtrC artistName(KNullDesC);
   487 				TPtrC artistName(MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumArtistName));
   505 
       
   506 				// if album is unknown, ignore arist name
       
   507 				if (MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumName) != KNullDesC)
       
   508 					{
       
   509 					artistName.Set(MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumArtistName));
       
   510 					}
       
   511 
       
   512 				aMedia.SetTextValueL(KMPXMediaMusicArtist, artistName);
   488 				aMedia.SetTextValueL(KMPXMediaMusicArtist, artistName);
   513 				MPX_DEBUG2("	Artist[%S]", &artistName);
   489 				MPX_DEBUG2("	Artist[%S]", &artistName);
   514 				}
   490 				}
   515 
   491 
   516 			if (attributeId & EMPXMediaMusicAlbum)
   492 			if (attributeId & EMPXMediaMusicAlbum)
   532 				}
   508 				}
   533 			if (attributeId & EMPXMediaMusicAlbumArtFileName)
   509 			if (attributeId & EMPXMediaMusicAlbumArtFileName)
   534 				{
   510 				{
   535 				MPX_DEBUG1("	EMPXMediaMusicAlbumArtFileName");
   511 				MPX_DEBUG1("	EMPXMediaMusicAlbumArtFileName");
   536 
   512 
   537 				TPtrC art(KNullDesC);
   513 				TPtrC art(MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumArt));
   538 
       
   539 				// if album is unknown, ignore album art name
       
   540 				if (MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumName) != KNullDesC)
       
   541 					{
       
   542 					art.Set(MPXDbCommonUtil::GetColumnTextL(aRecord, EAlbumArt));
       
   543 					}
       
   544 
       
   545 				aMedia.SetTextValueL(KMPXMediaMusicAlbumArtFileName, art);
   514 				aMedia.SetTextValueL(KMPXMediaMusicAlbumArtFileName, art);
   546 				MPX_DEBUG2("	Art[%S]", &art);
   515 				MPX_DEBUG2("	Art[%S]", &art);
   547 				}
   516 				}
   548 			}
   517 			}
   549 		} // end for
   518 		} // end for
   560 	{
   529 	{
   561 	if (aMedia.IsSupported(KMPXMediaMusicAlbumArtFileName))
   530 	if (aMedia.IsSupported(KMPXMediaMusicAlbumArtFileName))
   562 		{
   531 		{
   563 		const TDesC& albumArtFilename = aMedia.ValueText(KMPXMediaMusicAlbumArtFileName).Left(KMCMaxTextLen);
   532 		const TDesC& albumArtFilename = aMedia.ValueText(KMPXMediaMusicAlbumArtFileName).Left(KMCMaxTextLen);
   564 		MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCMusicArt, albumArtFilename);
   533 		MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCMusicArt, albumArtFilename);
   565 		}
   534 		}	
   566 
       
   567 	if (aMedia.IsSupported(KMPXMediaMusicArtist))
       
   568 	    {
       
   569 	    const TDesC& artistName = aMedia.ValueText(KMPXMediaMusicArtist).Left(KMCMaxTextLen);
       
   570 	    MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCAlbumArtistName, artistName);
       
   571 	    }
       
   572 	}
   535 	}
   573 
   536 
   574 // ----------------------------------------------------------------------------
   537 // ----------------------------------------------------------------------------
   575 // CMPXDbAlbum::IsUnknownArtistL
       
   576 // ----------------------------------------------------------------------------
       
   577 //
       
   578 TBool CMPXDbAlbum::IsUnknownArtistL(TUint32 aId)
       
   579 	{
       
   580 	return iObserver.HandleIsUnknownArtistL(aId);
       
   581 	}
       
   582 
       
   583 // ----------------------------------------------------------------------------
       
   584 // CMPXDbAlbum::ArtistForAlbumL
       
   585 // ----------------------------------------------------------------------------
       
   586 //
       
   587 HBufC* CMPXDbAlbum::ArtistForAlbumL(const TUint32 aId)
       
   588     {
       
   589     return iObserver.HandleArtistForAlbumL(aId);
       
   590     }
       
   591 
       
   592 // ----------------------------------------------------------------------------
       
   593 // CMPXDbAlbum::AlbumartForAlbumL
   538 // CMPXDbAlbum::AlbumartForAlbumL
   594 // ----------------------------------------------------------------------------
   539 // ----------------------------------------------------------------------------
   595 //
   540 //
   596 HBufC* CMPXDbAlbum::AlbumartForAlbumL(const TUint32 aId, TPtrC aArt)
   541 HBufC* CMPXDbAlbum::AlbumartForAlbumL(const TUint32 aId, TPtrC aArt)
   597     {
   542     {
   598     return iObserver.HandleAlbumartForAlbumL(aId, aArt);
   543     return iObserver.HandleAlbumartForAlbumL(aId, aArt);
       
   544     }
       
   545 
       
   546 // ----------------------------------------------------------------------------
       
   547 // CMPXDbAlbum::ProcessAlbumRecordSetL
       
   548 // Unknown album or Unknown artist are stored in the database as NULL (Name field and ArtistName field). 
       
   549 // The available unknown artist is saved for later and will be appended to the array 
       
   550 // as the last item among items which have the same artist.
       
   551 // The available unknown albums are saved for later and will be appended to the array
       
   552 // as the last items after all known albums are appended.
       
   553 // The unknown album and unknown artist is saved for later and will be appended to the array
       
   554 // as the last item.
       
   555 //
       
   556 // NOTE: putting unknown album to the end of the array only takes place when title
       
   557 //       field is requested. normal sorting algorithm occurs if title isn't
       
   558 //       requested.
       
   559 //
       
   560 // Example:
       
   561 //     Album X of artist A
       
   562 //     Album X of artist B
       
   563 //     Album X of unknown artist
       
   564 //     Album Y of artist A
       
   565 //     Album Y of unknown artist
       
   566 //     Unknown album of artist A
       
   567 //     Unknown album of artist B
       
   568 //     Unknown album of unknown artist
       
   569 // ----------------------------------------------------------------------------
       
   570 //
       
   571 void CMPXDbAlbum::ProcessAlbumRecordSetL(
       
   572     const TArray<TMPXAttribute>& aAttrs,
       
   573     RSqlStatement& aRecordset,
       
   574     CMPXMediaArray& aMediaArray)
       
   575     {
       
   576     // populate the array
       
   577     TBool firstRecord(ETrue);
       
   578     CMPXMedia* unknownAlbumUnknownArtistMedia(NULL);
       
   579     CMPXMedia* unknownMedia(NULL);
       
   580     TUint32 prevId(0);
       
   581     TUint32 albumId(0);
       
   582     TUint32 prevAlbumId(0);
       
   583     TInt count(0);
       
   584     TInt err(KErrNone);
       
   585        
       
   586     // Reset array for unknown artists
       
   587     if (iUnknownArtists.Count() > 0)
       
   588         {
       
   589         iUnknownArtists.ResetAndDestroy();
       
   590         }
       
   591     
       
   592     // Reset array for unknown albums
       
   593     if (iUnknownAlbums.Count() > 0)
       
   594         {
       
   595         iUnknownAlbums.ResetAndDestroy();
       
   596         }
       
   597         
       
   598     TInt pPath(0);
       
   599     if (aMediaArray.Count())
       
   600         {
       
   601         CMPXMedia* pMedia = aMediaArray[0];
       
   602         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   603             { // Query excuted by OpenL
       
   604             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   605             MPX_ASSERT(pPath);
       
   606             }
       
   607         }
       
   608     
       
   609     RArray<TMPXItemId> ids;
       
   610     CleanupClosePushL(ids);
       
   611     
       
   612     while ((err = aRecordset.Next()) == KSqlAtRow)
       
   613         {
       
   614         TUint32 rowId(aRecordset.ColumnInt64(EAlbumUniqueId));
       
   615         if (prevId == rowId)
       
   616             {
       
   617             continue;
       
   618             }
       
   619 
       
   620         prevId = rowId;
       
   621         CMPXMedia* media = CMPXMedia::NewL();
       
   622         CleanupStack::PushL(media);
       
   623 
       
   624         UpdateMediaL(aRecordset, aAttrs, *media);
       
   625         
       
   626         albumId = MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), EMPXAlbum,
       
   627                 MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumName), ETrue);
       
   628         
       
   629         // Append Known album and Unknown artists
       
   630         count = iUnknownArtists.Count();
       
   631         if ( prevAlbumId != albumId && count > 0)
       
   632             {
       
   633             unknownMedia = NULL;
       
   634                 
       
   635             for (TInt i = 0; i < count; i++)
       
   636                 {
       
   637                 unknownMedia = iUnknownArtists[i];
       
   638                 if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   639                     {
       
   640                     ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   641                     }
       
   642                 aMediaArray.AppendL(*unknownMedia);                 
       
   643                 }
       
   644                 
       
   645             iUnknownArtists.ResetAndDestroy();
       
   646             }
       
   647         
       
   648         // for Unknown album and Unknown artist
       
   649         if (firstRecord && (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumArtistName).Length() == 0) &&
       
   650             (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumName).Length() == 0))
       
   651             {
       
   652             unknownAlbumUnknownArtistMedia = media;
       
   653             }
       
   654         // for Unknown albums and Known artists
       
   655         else if (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumName).Length() == 0)
       
   656             {
       
   657             iUnknownAlbums.Append(media);
       
   658             CleanupStack::Pop(media);
       
   659             }
       
   660         // for Known album and Unknown artist
       
   661         else if (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumArtistName).Length() == 0)
       
   662             {
       
   663             iUnknownArtists.Append(media);
       
   664             CleanupStack::Pop(media);
       
   665             
       
   666             prevAlbumId = albumId;
       
   667             }
       
   668         else
       
   669             {
       
   670             if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   671                 {
       
   672                 ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   673                 }
       
   674             aMediaArray.AppendL(*media);
       
   675             CleanupStack::PopAndDestroy(media);       
       
   676             }
       
   677         
       
   678         firstRecord = EFalse;               
       
   679         } // end while
       
   680 
       
   681     if (err != KSqlAtEnd)
       
   682         {
       
   683         User::LeaveIfError(err);
       
   684         }
       
   685 
       
   686     // Append Known artist and Unknown albums
       
   687     count = iUnknownArtists.Count();
       
   688     if ( count > 0)
       
   689         {
       
   690         unknownMedia = NULL;
       
   691         
       
   692         for (TInt i = 0; i < count; i++)
       
   693             {
       
   694             unknownMedia = iUnknownArtists[i];
       
   695             if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   696                 {
       
   697                 ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   698                 }
       
   699             aMediaArray.AppendL(*unknownMedia);                 
       
   700             }
       
   701         
       
   702         iUnknownArtists.ResetAndDestroy();
       
   703         }
       
   704 
       
   705     // Append Unknown artists to MediaArray
       
   706     count = iUnknownAlbums.Count();
       
   707     if ( count > 0)
       
   708         {
       
   709         CMPXMedia* unknownMedia = NULL;
       
   710         
       
   711         for (TInt i = 0; i < count; i++)
       
   712             {
       
   713             unknownMedia = iUnknownAlbums[i];
       
   714             if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   715                 {
       
   716                 ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   717                 }
       
   718             aMediaArray.AppendL(*unknownMedia);                 
       
   719             }
       
   720         
       
   721         iUnknownAlbums.ResetAndDestroy();
       
   722         }
       
   723     
       
   724     // the last item in the list
       
   725     if (unknownAlbumUnknownArtistMedia)
       
   726         {
       
   727         if (unknownAlbumUnknownArtistMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   728             {
       
   729             ids.AppendL(unknownAlbumUnknownArtistMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   730             }
       
   731         aMediaArray.AppendL(*unknownAlbumUnknownArtistMedia);
       
   732         CleanupStack::PopAndDestroy(unknownAlbumUnknownArtistMedia);
       
   733         }
       
   734     
       
   735     // Append ids to the returned path
       
   736     if (pPath)
       
   737         {
       
   738         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   739         }
       
   740     CleanupStack::PopAndDestroy(&ids);
       
   741     }
       
   742 
       
   743 // ----------------------------------------------------------------------------
       
   744 // CMPXDbAlbum::ProcessMediaWallAlbumRecordSetL
       
   745 // Unknown album or Unknown artist are stored in the database as NULL (Name field and ArtistName field). 
       
   746 // The available unknown album is saved for later and will be appended to the array 
       
   747 // as the last item among items which have the same artist.
       
   748 // The available unknown artists are saved for later and will be appended to the array
       
   749 // as the last items after all known artists are appended.
       
   750 // The unknown album and unknown artist is saved for later and will be appended to the array
       
   751 // as the last item.
       
   752 //
       
   753 // NOTE: putting unknown album to the end of the array only takes place when title
       
   754 //       field is requested. normal sorting algorithm occurs if title isn't
       
   755 //       requested.
       
   756 //
       
   757 // Example:
       
   758 //     Album X of artist A
       
   759 //     Album Y of artist A
       
   760 //     Unknown album of artist A
       
   761 //     Album X of artist B
       
   762 //     Album Z of artist B
       
   763 //     Unknown album of artist B
       
   764 //     Album X of unknown artist
       
   765 //     Album Y of unknown artist
       
   766 //     Unknown album of unknown artist
       
   767 // ----------------------------------------------------------------------------
       
   768 //
       
   769 //
       
   770 void CMPXDbAlbum::ProcessMediaWallAlbumRecordSetL(
       
   771     const TArray<TMPXAttribute>& aAttrs,
       
   772     RSqlStatement& aRecordset,
       
   773     CMPXMediaArray& aMediaArray)
       
   774     {
       
   775     // populate the array
       
   776     TBool firstRecord(ETrue);
       
   777     CMPXMedia* unknownAlbumUnknownArtistMedia(NULL);
       
   778     CMPXMedia* unknownMedia(NULL);
       
   779     TUint32 prevId(0);
       
   780     TUint32 artistId(0);
       
   781     TUint32 prevArtistId(0);
       
   782     TInt count(0);
       
   783     TInt err(KErrNone);
       
   784        
       
   785     // Reset array for unknown artists
       
   786     if (iUnknownArtists.Count() > 0)
       
   787         {
       
   788         iUnknownArtists.ResetAndDestroy();
       
   789         }
       
   790     
       
   791     // Reset array for unknown albums
       
   792     if (iUnknownAlbums.Count() > 0)
       
   793         {
       
   794         iUnknownAlbums.ResetAndDestroy();
       
   795         }
       
   796         
       
   797     TInt pPath(0);
       
   798     if (aMediaArray.Count())
       
   799         {
       
   800         CMPXMedia* pMedia = aMediaArray[0];
       
   801         if (pMedia->IsSupported(KMPXMediaGeneralValue))
       
   802             { // Query excuted by OpenL
       
   803             pPath = pMedia->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   804             MPX_ASSERT(pPath);
       
   805             }
       
   806         }
       
   807     
       
   808     RArray<TMPXItemId> ids;
       
   809     CleanupClosePushL(ids);
       
   810 
       
   811     while ((err = aRecordset.Next()) == KSqlAtRow)
       
   812         {
       
   813         TUint32 rowId(aRecordset.ColumnInt64(EAlbumUniqueId));
       
   814         if (prevId == rowId)
       
   815             {
       
   816             continue;
       
   817             }
       
   818 
       
   819         prevId = rowId;
       
   820         CMPXMedia* media = CMPXMedia::NewL();
       
   821         CleanupStack::PushL(media);
       
   822 
       
   823         UpdateMediaL(aRecordset, aAttrs, *media);
       
   824     
       
   825         artistId = aRecordset.ColumnInt64(EAlbumArtist);
       
   826 
       
   827         // Append Known artist and Unknown albums
       
   828         count = iUnknownAlbums.Count();
       
   829         if (prevArtistId != artistId && count > 0)
       
   830             {
       
   831             unknownMedia = NULL;
       
   832                 
       
   833             for (TInt i = 0; i < count; i++)
       
   834                 {
       
   835                 unknownMedia = iUnknownAlbums[i];
       
   836                 if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   837                     {
       
   838                     ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   839                     }
       
   840                 aMediaArray.AppendL(*unknownMedia);                 
       
   841                 }
       
   842                 
       
   843             iUnknownAlbums.ResetAndDestroy();
       
   844             }
       
   845         
       
   846         // for Unknown artist and Unknown album
       
   847         if (firstRecord && (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumName).Length() == 0) &&
       
   848             (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumArtistName).Length() == 0))
       
   849             {
       
   850             unknownAlbumUnknownArtistMedia = media;
       
   851             }
       
   852         // for Unknown artists and Known albums
       
   853         else if (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumArtistName).Length() == 0)
       
   854             {
       
   855             iUnknownArtists.Append(media);
       
   856             CleanupStack::Pop(media);
       
   857             }
       
   858         // for Known artist and Unknown album
       
   859         else if (MPXDbCommonUtil::GetColumnTextL(aRecordset, EAlbumName).Length() == 0)
       
   860             {
       
   861             iUnknownAlbums.Append(media);
       
   862             CleanupStack::Pop(media);
       
   863             prevArtistId = artistId;
       
   864             }
       
   865         else
       
   866             {
       
   867             if (media->IsSupported(KMPXMediaGeneralId) && pPath)
       
   868                 {
       
   869                 ids.AppendL(media->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   870                 }
       
   871             aMediaArray.AppendL(*media);
       
   872             CleanupStack::PopAndDestroy(media);       
       
   873             }
       
   874         
       
   875         firstRecord = EFalse;               
       
   876         } // end while
       
   877 
       
   878     if (err != KSqlAtEnd)
       
   879         {
       
   880         User::LeaveIfError(err);
       
   881         }
       
   882 
       
   883     // Append Known artist and Unknown albums
       
   884     count = iUnknownAlbums.Count();
       
   885     if ( count > 0)
       
   886         {
       
   887         unknownMedia = NULL;
       
   888         
       
   889         for (TInt i = 0; i < count; i++)
       
   890             {
       
   891             unknownMedia = iUnknownAlbums[i];
       
   892             if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   893                 {
       
   894                 ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   895                 }
       
   896             aMediaArray.AppendL(*unknownMedia);                 
       
   897             }
       
   898         
       
   899         iUnknownAlbums.ResetAndDestroy();
       
   900         }
       
   901 
       
   902     // Append Unknown artists to MediaArray
       
   903     count = iUnknownArtists.Count();
       
   904     if ( count > 0)
       
   905         {
       
   906         CMPXMedia* unknownMedia = NULL;
       
   907         
       
   908         for (TInt i = 0; i < count; i++)
       
   909             {
       
   910             unknownMedia = iUnknownArtists[i];
       
   911             if (unknownMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   912                 {
       
   913                 ids.Append(unknownMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   914                 }
       
   915             aMediaArray.AppendL(*unknownMedia);                 
       
   916             }
       
   917         
       
   918         iUnknownArtists.ResetAndDestroy();
       
   919         }
       
   920     
       
   921     // the last item in the list
       
   922     if (unknownAlbumUnknownArtistMedia)
       
   923         {
       
   924         if (unknownAlbumUnknownArtistMedia->IsSupported(KMPXMediaGeneralId) && pPath)
       
   925             {
       
   926             ids.AppendL(unknownAlbumUnknownArtistMedia->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId));
       
   927             }
       
   928         aMediaArray.AppendL(*unknownAlbumUnknownArtistMedia);
       
   929         CleanupStack::PopAndDestroy(unknownAlbumUnknownArtistMedia);
       
   930         }
       
   931     
       
   932     // Append ids to the returned path
       
   933     if (pPath)
       
   934         {
       
   935         ((CMPXCollectionPath*)pPath)->AppendL(ids.Array());
       
   936         }
       
   937     CleanupStack::PopAndDestroy(&ids);
       
   938     }
       
   939 
       
   940 // ----------------------------------------------------------------------------
       
   941 // CMPXDbAlbum::GenerateUniqueIdL
       
   942 // ----------------------------------------------------------------------------
       
   943 //
       
   944 TUint32 CMPXDbAlbum::GenerateUniqueIdL(const CMPXMedia& aMedia)
       
   945     {
       
   946     TPtrC album(KNullDesC);
       
   947     TPtrC albumArtist(KNullDesC);
       
   948     TPtrC artist(KNullDesC);
       
   949     
       
   950     // check Album attirbute
       
   951     if (aMedia.IsSupported(KMPXMediaMusicAlbum))     
       
   952         {
       
   953         album.Set(aMedia.ValueText(KMPXMediaMusicAlbum).Left(KMCMaxTextLen));
       
   954         }
       
   955     
       
   956     HBufC* buf(NULL);    
       
   957     if (RetrieveAlbumArtistL(aMedia, albumArtist))
       
   958         {
       
   959         buf = HBufC::NewLC(album.Length() + albumArtist.Length() + KAlbumAlbumArtistSeperator().Length());
       
   960     
       
   961         buf->Des().Append(album);
       
   962         buf->Des().Append(KAlbumAlbumArtistSeperator);
       
   963         buf->Des().Append(albumArtist);
       
   964         }
       
   965     else
       
   966         {
       
   967         RetrieveArtist(aMedia, artist);
       
   968         
       
   969         buf = HBufC::NewLC(album.Length() + artist.Length() + KAlbumArtistSeperator().Length());
       
   970         
       
   971         buf->Des().Append(album);
       
   972         buf->Des().Append(KAlbumArtistSeperator);
       
   973         buf->Des().Append(artist);
       
   974         }
       
   975     
       
   976     // try to find the item first
       
   977     TUint32 id(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), EMPXAlbum,
       
   978         *buf, ETrue));
       
   979     CleanupStack::PopAndDestroy(buf);
       
   980     
       
   981     return id;
       
   982     }
       
   983 
       
   984 // ----------------------------------------------------------------------------
       
   985 // CMPXDbAlbum::IsUnknownAlbumL
       
   986 // ----------------------------------------------------------------------------
       
   987 //
       
   988 TBool CMPXDbAlbum::IsUnknownAlbumL(const TUint32 aId)
       
   989     {
       
   990     HBufC* query = PreProcessStringLC(KQueryCategoryItem);
       
   991     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, aId));
       
   992     CleanupStack::PopAndDestroy(query);
       
   993     CleanupClosePushL(recordset);
       
   994 
       
   995     if (recordset.Next() != KSqlAtRow)
       
   996         {
       
   997         User::Leave(KErrNotFound);
       
   998         }
       
   999     
       
  1000     TPtrC album(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumName)); 
       
  1001     TPtrC artistName(MPXDbCommonUtil::GetColumnTextL(recordset, EAlbumArtistName)); 
       
  1002 
       
  1003     TBool ret = EFalse;
       
  1004     if (album.Length() == 0 || artistName.Length() == 0)
       
  1005         {
       
  1006         ret = ETrue;   
       
  1007         }
       
  1008     else
       
  1009         {
       
  1010         ret = EFalse;
       
  1011         }
       
  1012     
       
  1013     CleanupStack::PopAndDestroy(&recordset);
       
  1014     
       
  1015     return ret;
       
  1016     }
       
  1017 
       
  1018 // ----------------------------------------------------------------------------
       
  1019 // CMPXDbAlbum::RetrieveAlbumArtistL
       
  1020 // ----------------------------------------------------------------------------
       
  1021 //
       
  1022 TBool CMPXDbAlbum::RetrieveAlbumArtistL(const CMPXMedia& aMedia, TPtrC& aName)
       
  1023     {
       
  1024     // check AlbumArtist attribute
       
  1025     if (aMedia.IsSupported(KMPXMediaMusicAlbumArtist))
       
  1026         {
       
  1027         aName.Set(aMedia.ValueText(KMPXMediaMusicAlbumArtist).Left(KMCMaxTextLen));
       
  1028         }
       
  1029     else
       
  1030         {
       
  1031         aName.Set(KNullDesC);
       
  1032         }
       
  1033     
       
  1034     if (aName.Length() > 0 )
       
  1035         {
       
  1036         return ETrue;
       
  1037         }
       
  1038     else
       
  1039         {
       
  1040         return EFalse;
       
  1041         }
       
  1042     }
       
  1043 
       
  1044 // ----------------------------------------------------------------------------
       
  1045 // CMPXDbAlbum::RetrieveArtist
       
  1046 // ----------------------------------------------------------------------------
       
  1047 //
       
  1048 void CMPXDbAlbum::RetrieveArtist(const CMPXMedia& aMedia, TPtrC& aName)
       
  1049     {
       
  1050     // check Artist attribute
       
  1051     if (aMedia.IsSupported(KMPXMediaMusicArtist))
       
  1052         {
       
  1053         aName.Set(aMedia.ValueText(KMPXMediaMusicArtist).Left(KMCMaxTextLen));
       
  1054         }
       
  1055     else
       
  1056         {
       
  1057         aName.Set(KNullDesC);            
       
  1058         }
       
  1059     }
       
  1060 
       
  1061 // ----------------------------------------------------------------------------
       
  1062 // CMPXDbAlbum::NeedToUpdateArt
       
  1063 // ----------------------------------------------------------------------------
       
  1064 //
       
  1065 TBool CMPXDbAlbum::NeedToUpdateArt(const TDesC& aDeletedSongArt, const TDesC& aCurrentAlbumArt)
       
  1066     {    
       
  1067     if (aDeletedSongArt.Length() == 0)
       
  1068         {
       
  1069         // Deleted song's art has default album art
       
  1070         return EFalse;
       
  1071         }
       
  1072 #ifdef ABSTRACTAUDIOALBUM_INCLUDED
       
  1073     else
       
  1074     if (aDeletedSongArt.Length() > 0)
       
  1075         {
       
  1076         TParsePtrC parse(aDeletedSongArt);
       
  1077         TPtrC ext(parse.Ext());
       
  1078         //set flag to false, so .alb will not overwrite art field in album, artist table 
       
  1079         // when song with embedded art
       
  1080         if (ext.CompareF(KAbstractAlbumExt) == 0) 
       
  1081             {     
       
  1082             // Deleted song's art is Non-embedded album art
       
  1083             return EFalse;
       
  1084             }
       
  1085         }
       
  1086     else
       
  1087 #endif   
       
  1088     if (aDeletedSongArt.Length() > 0 && aCurrentAlbumArt.Length() > 0 && aDeletedSongArt.CompareF(aCurrentAlbumArt) == 0)
       
  1089         {
       
  1090         // Deleted song's art is Embedded album art and it is the same as Album's current art
       
  1091         return ETrue;
       
  1092         }
       
  1093     else
       
  1094         {
       
  1095         return EFalse;
       
  1096         }
   599     }
  1097     }
   600 
  1098 
   601 // ----------------------------------------------------------------------------
  1099 // ----------------------------------------------------------------------------
   602 // CMPXDbAlbum::CreateTableL
  1100 // CMPXDbAlbum::CreateTableL
   603 // ----------------------------------------------------------------------------
  1101 // ----------------------------------------------------------------------------
   607     TBool /* aCorruptTable */)
  1105     TBool /* aCorruptTable */)
   608     {
  1106     {
   609     MPX_FUNC("CMPXDbCategory::CreateTableL");
  1107     MPX_FUNC("CMPXDbCategory::CreateTableL");
   610 
  1108 
   611     // create the table
  1109     // create the table
   612     HBufC* query = PreProcessStringLC(KAlbumCreateTable);
  1110     User::LeaveIfError(aDatabase.Exec(KAlbumCreateTable));
   613     User::LeaveIfError(aDatabase.Exec(*query));
       
   614     CleanupStack::PopAndDestroy(query);
       
   615 
  1111 
   616     // do not create an index on the Name field
  1112     // do not create an index on the Name field
   617     // as it only slows down the insert/update queries overall
  1113     // as it only slows down the insert/update queries overall
   618     }
  1114     }
   619 
  1115 
   624 TBool CMPXDbAlbum::CheckTableL(
  1120 TBool CMPXDbAlbum::CheckTableL(
   625     RSqlDatabase& aDatabase)
  1121     RSqlDatabase& aDatabase)
   626     {
  1122     {
   627     MPX_FUNC("CMPXDbCategory::CheckTableL");
  1123     MPX_FUNC("CMPXDbCategory::CheckTableL");
   628 
  1124 
   629     HBufC* query = PreProcessStringLC(KAlbumCheckTable);
  1125     TBool check(DoCheckTable(aDatabase, KAlbumCheckTable));
   630     TBool check(DoCheckTable(aDatabase, *query));
       
   631     CleanupStack::PopAndDestroy(query);
       
   632 
  1126 
   633     return check;
  1127     return check;
   634     }
  1128     }
   635 
  1129 
   636 // End of File
  1130 // End of File