mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbhandler.cpp
branchRCL_3
changeset 21 a1247965635c
parent 18 c54d95799c80
child 26 70a8526f03f2
--- a/mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbhandler.cpp	Wed Apr 14 15:54:18 2010 +0300
+++ b/mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbhandler.cpp	Tue Apr 27 16:34:37 2010 +0300
@@ -178,9 +178,9 @@
     iDbAlbum = CMPXDbAlbum::NewL(*iDbManager, EMPXAlbum, *this);
     iDbGenre = CMPXDbGenre::NewL(*iDbManager, EMPXGenre);
     iDbComposer = CMPXDbComposer::NewL(*iDbManager, EMPXComposer);
-#ifdef ABSTRACTAUDIOALBUM_INCLUDED  
-    iDbAbstractAlbum = CMPXDbAbstractAlbum::NewL(*iDbManager, EMPXAbstractAlbum);
-#endif // ABSTRACTAUDIOALBUM_INCLUDED 
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED
+    iDbAbstractAlbum = CMPXDbAbstractAlbum::NewL(*iDbManager, EMPXAbstractAlbum, iFs);
+#endif // ABSTRACTAUDIOALBUM_INCLUDED
     iAutoPlaylist = CMPXDbAutoPlaylist::NewL(*iDbManager, iFs, iResource);
     iDbAuxiliary = CMPXDbAuxiliary::NewL(*iDbManager);
 
@@ -315,24 +315,22 @@
     MPX_FUNC("CMPXDbHandler::AddAbstractAlbumL");
 
     BeginTransactionL();
-   // TUint32 abstractAlbumId(0);
     TBool newRecord(EFalse);
     TInt err(KErrNone);
 
     TDriveUnit drive(aMedia.ValueText(KMPXMediaGeneralUri));
-    TPtrC name(aMedia.ValueText(KMPXMediaGeneralUri).Left(KMCMaxTextLen));
+    TPtrC uri(aMedia.ValueText(KMPXMediaGeneralUri).Left(KMCMaxTextLen));
+    TPtrC name(aMedia.ValueText(KMPXMediaGeneralTitle).Left(KMCMaxTextLen));
     TPtrC albumartist(aMedia.ValueText(KMPXMediaMusicAlbumArtist).Left(KMCMaxTextLen));
-    TPtrC genre(aMedia.ValueText(KMPXMediaMusicGenre).Left(KMCMaxTextLen));
-    
-    //only insert to AbstractAlbum table when it is new item  
-     TUint32 abstractAlbumId(MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, name, (EMPXAbstractAlbum!=EMPXGenre)));      
-   
+
+    //only insert to AbstractAlbum table when it is new item
+    TUint32 abstractAlbumId(MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, uri, EFalse));
+
     newRecord = !iDbAbstractAlbum->CategoryItemExistsL(drive, abstractAlbumId);
-   
-    
-    if (newRecord)   
+
+    if (newRecord)
         {
-        MPX_TRAP(err, abstractAlbumId = iDbAbstractAlbum->AddItemL( name, albumartist, genre, drive, newRecord, (EMPXAbstractAlbum != EMPXGenre)));      
+        MPX_TRAP(err, abstractAlbumId = iDbAbstractAlbum->AddItemL(uri, name, albumartist, drive, newRecord, EFalse));
         if (iOutOfDisk && (err == KErrNotFound))
             {
             err = KErrDiskFull;
@@ -345,57 +343,134 @@
         }
     EndTransactionL(err);
 
-    return abstractAlbumId;  
+    return abstractAlbumId;
     }
 
 // ----------------------------------------------------------------------------
-// Update all songs which associate with AbstractAlbum in the collection
+// Update abstractalbum info to AbstractAlbum table and all songs which associate 
+// with AbstractAlbum in the collection
 // ----------------------------------------------------------------------------
 //
-CMPXDbActiveTask::TChangeVisibility CMPXDbHandler::UpdateAbstractAlbumSongsL(
+CMPXDbActiveTask::TChangeVisibility CMPXDbHandler::UpdateAbstractAlbumL(
     const CMPXMedia& aMedia,
     CMPXMessageArray& aItemChangedMessages)
     {
-    MPX_FUNC("CMPXDbHandler::UpdateAbstractAlbumSongsL");
+    MPX_FUNC("CMPXDbHandler::UpdateAbstractAlbumL");
 
     CMPXDbActiveTask::TChangeVisibility visibleChange(CMPXDbActiveTask::ENotVisibile);
-
+    TUint32 itemId(0);
     if (aMedia.IsSupported(KMPXMediaGeneralUri))
         {
-        const TDesC& art(aMedia.ValueText (KMPXMediaGeneralUri));
-    
-        TBool existRecord(EFalse); 
+        const TDesC& uri(aMedia.ValueText (KMPXMediaGeneralUri));
         TDriveUnit drive(aMedia.ValueText(KMPXMediaGeneralUri));
-        TUint32 rowId(MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, art, (EMPXAbstractAlbum != EMPXGenre)));    
-        //check if abstractAlbum in ABSTRACTALBUM table before update songs
-        existRecord = iDbAbstractAlbum->CategoryItemExistsL(drive, rowId);
-         
-        //update songs associated with abstractalbum only when abstractalbum associated already
-        //in the AbstractAlbum table     
-        if (existRecord)
-            {         
-            if (aMedia.IsSupported(KMPXMediaArrayContents))
+
+        //get Id based on new uri
+        itemId = MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, uri, EFalse);
+        if (!itemId)
+            {
+            User::Leave(KErrNotSupported);
+            }
+
+        //if updating to new item, need do renaming for AbstractAlbum: delete old one and insert a new entry to AbstractAlbum table
+        //if a new item, need update songs associated and renaming albstractalbum table
+        if (aMedia.IsSupported(KMPXMediaGeneralId))
+            {
+            MPX_DEBUG1("CMPXDbHandler::UpdateAbstractAlbumL, rename case");
+            
+            BeginTransactionL();
+            //get old id, for renaming
+            TInt err(KErrNone);
+            TUint32 oldId = (aMedia.ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId)).iId2;
+            if (oldId && (oldId != itemId) )  //pass the old UID for renaming, do not care if new itemId exist or not
                 {
-
-                CMPXMediaArray* mediaArray = aMedia.Value<CMPXMediaArray>(KMPXMediaArrayContents);
-                User::LeaveIfNull(mediaArray);
+                //get old Uri based on old Id
+                HBufC* oldUri = iDbAbstractAlbum->GetUriL(oldId);
+                CleanupStack::PushL(oldUri);
+
+                //add and update new item to AbstractAlbum table
+                //old existing item field values need to be saved and added when adding new item                        
+                MPX_TRAP(err, itemId = iDbAbstractAlbum->AddUpdatedItemL(oldId, uri ));
+
+                if (iOutOfDisk && (err == KErrNotFound))
+                    {
+                    err = KErrDiskFull;
+                    }
+
+                MPXDbCommonUtil::AddItemChangedMessageL(aItemChangedMessages, itemId, EMPXItemInserted,
+                EMPXAbstractAlbum, KDBPluginUid);
+
+                //find and update all songs associated with oldId
+                RArray<TMPXAttribute> songAttributes;
+                CleanupClosePushL(songAttributes);
+                songAttributes.AppendL(KMPXMediaGeneralId);
+
+                CMPXMediaArray* mediaArray = CMPXMediaArray::NewL();
+                CleanupStack::PushL(mediaArray);
+
+                //get all songs associated
+                MPX_TRAP(err, iDbMusic->GetAllSongsForAbstractAlbumL(oldId, songAttributes.Array(), *mediaArray));
+                if (err == KErrNotFound)
+                    {
+                    // Leave with KErrNotFound
+                    MPX_DEBUG1("CMPXDbHandler::UpdateAbstractAlbumL, leave with not found");
+                    User::Leave(KErrNotFound);
+                    }
+
                 TInt count(mediaArray->Count());
+                MPX_DEBUG2("CMPXDbHandler::UpdateAbstractAlbumL, [%d] songs associated", count);
+                //go through all songs for updating
                 for (TInt i = 0; i < count; i++)
                     {
-                    CMPXMedia* element = mediaArray->AtL(i);
-                    element->SetTextValueL(KMPXMediaMusicAlbumArtFileName, art );
-                    visibleChange = UpdateSongL(*element, aItemChangedMessages);
+                    CMPXMedia* song = mediaArray->AtL(i);
+                    song->SetTextValueL(KMPXMediaMusicAlbumArtFileName, uri );
+                    visibleChange = UpdateSongL(*song, aItemChangedMessages);
                     }
-               }
-            else
-                {
-                iDbAbstractAlbum->UpdateItemL(rowId, aMedia, drive, &aItemChangedMessages);
+                CleanupStack::PopAndDestroy(mediaArray);
+                CleanupStack::PopAndDestroy(&songAttributes);
+
+                //rename TN
+                iDbAbstractAlbum->HandleTNL(*oldUri, uri, 0);
+                CleanupStack::PopAndDestroy(oldUri);
                 }
-          }
+            EndTransactionL(err);
+            }//renaming
+
+        //check if abstractAlbum exist in ABSTRACTALBUM table before update it
+        else if (iDbAbstractAlbum->CategoryItemExistsL(drive, itemId))
+            {
+            //support updating Name, AlbumArtist fields for AbstractAlbum table
+            iDbAbstractAlbum->UpdateItemL(itemId, aMedia, drive, &aItemChangedMessages);
+            }
         }
     return visibleChange;
-}
+    }
+
+// ----------------------------------------------------------------------------
+// Update all songs which associate with AbstractAlbum to new AbstractAlbum info 
+// in the collection
+// ----------------------------------------------------------------------------
+//
+CMPXDbActiveTask::TChangeVisibility CMPXDbHandler::UpdateSongsAbstractAlbumInfoL(
+    const CMPXMedia& aMedia,
+    CMPXMessageArray& aItemChangedMessages)
+    {
+    MPX_FUNC("CMPXDbHandler::UpdateSongsAbstractAlbumInfoL");
+    const TDesC& uri(aMedia.ValueText (KMPXMediaGeneralUri));
+    //need to update songs information to music table
+    CMPXMediaArray* mediaArray = aMedia.Value<CMPXMediaArray>(KMPXMediaArrayContents);
+    User::LeaveIfNull(mediaArray);
+    TInt count(mediaArray->Count());
+    CMPXDbActiveTask::TChangeVisibility visibleChange(CMPXDbActiveTask::ENotVisibile);
+    for (TInt i = 0; i < count; i++)
+        {
+        CMPXMedia* mediaSong = mediaArray->AtL(i);
+        mediaSong->SetTextValueL(KMPXMediaMusicAlbumArtFileName, uri );
+        visibleChange = UpdateSongL(*mediaSong, aItemChangedMessages);
+        }
+    return visibleChange;
+    }
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
+
 // ----------------------------------------------------------------------------
 // Update a song in the collection
 // ----------------------------------------------------------------------------
@@ -589,40 +664,10 @@
 //
 void CMPXDbHandler::RemoveAbstractAlbumL(
     TUint32 aAbstractAlbumId,
-    CDesCArray& aUriArray,
-    CMPXMessageArray& aItemChangedMessages)
+    CMPXMessageArray& aItemChangedMessages, TBool aFileDeleted)
     {
     MPX_FUNC("CMPXDbHandler::RemoveAbstractAlbumL");
-
-    BeginTransactionL();
-    MPX_TRAPD(err, DoRemoveAbstractAlbumL(aAbstractAlbumId, aUriArray, aItemChangedMessages));
-    EndTransactionL(err);
-    }
-
-// ----------------------------------------------------------------------------
-// Remove specified abstractalbum
-// ----------------------------------------------------------------------------
-//
-void CMPXDbHandler::DoRemoveAbstractAlbumL(
-    TUint32 aAbstractAlbumId,
-    CDesCArray& aUriArray,
-    CMPXMessageArray& aItemChangedMessages)
-    {
-    MPX_FUNC("CMPXDbHandler::DoRemoveAbstractAlbumL");
-
-
-    HBufC* uri(iDbAbstractAlbum->DeleteAbstractAlbumL(aAbstractAlbumId));
-    if (uri)
-        {
-        CleanupStack::PushL(uri);
-        aUriArray.AppendL(*uri);
-        CleanupStack::PopAndDestroy(uri);
-        }
-
-    MPXDbCommonUtil::AddItemChangedMessageL(aItemChangedMessages, aAbstractAlbumId, EMPXItemDeleted,
-        EMPXAbstractAlbum, KDBPluginUid);
-
-   
+    iDbAbstractAlbum->RemoveAbstractAlbumL(aAbstractAlbumId, aItemChangedMessages, aFileDeleted);
     }
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
 
@@ -889,7 +934,7 @@
     const TDesC& aUri)
     {
     MPX_FUNC("CMPXDbHandler::GetAbstractAlbumIdMatchingUriL");
-    return MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, aUri, ETrue);
+    return MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, aUri, EFalse);
     }
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
 
@@ -1501,7 +1546,6 @@
             iOutOfDisk = ETrue;
             }
     }
-    
 #ifdef __RAMDISK_PERF_ENABLE
 	iDbManager->CopyDBsToRamL();
 #endif //__RAMDISK_PERF_ENABLE
@@ -1527,6 +1571,13 @@
         curTime.HomeTime();
         SetLastRefreshedTimeL(curTime);
         }
+
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED
+    //for AbstractAlbum garbage collection
+    //can not leave
+    TRAP_IGNORE( AbstractAlbumCleanUpL() );
+#endif
+   
 #ifdef __RAMDISK_PERF_ENABLE
     iDbManager->CopyDBsFromRamL();
 #endif //__RAMDISK_PERF_ENABLE
@@ -1567,15 +1618,24 @@
     iMtpInUse = EFalse;
     iOpOnDbCount = 0;
     iDbManager->CommitL();
+    TInt err = KErrNone;
+
 
 #ifdef __RAMDISK_PERF_ENABLE
-    TRAPD(err, iDbManager->CopyDBsFromRamL());
+    TRAP(err, iDbManager->CopyDBsFromRamL());
     if ( err != KErrNone )
         {
         MPX_DEBUG2("CMPXDbHandler::MtpEndL error=%d", err);
         }
 #endif //__RAMDISK_PERF_ENABLE
-    
+
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED
+    TRAP(err, RemoveAbstractAlbumsWithNoSongL());
+    if ( err != KErrNone )
+        {
+        MPX_DEBUG2("CMPXDbHandler::MtpEndL error happened when cleanup albstractalbum with no songs association[%d]", err);
+        }
+#endif
     MPX_DEBUG1("<--CMPXDbHandler::MtpEndL");
     }
 
@@ -2302,6 +2362,84 @@
     MPXDbCommonUtil::AddItemChangedMessageL(aItemChangedMessages, aSongId, EMPXItemDeleted,
         EMPXSong, KDBPluginUid);
     }
+    
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED  
+// ----------------------------------------------------------------------------
+// Remove abstractalbums which have no songs associated.
+// ----------------------------------------------------------------------------
+//
+void CMPXDbHandler::RemoveAbstractAlbumsWithNoSongL()
+    {
+    MPX_FUNC("CMPXDbHandler::RemoveAbstractAlbumsWithNoSongL");
+    
+    BeginTransactionL();
+    RArray<TUint32> iItemsIds;
+    CleanupClosePushL(iItemsIds);
+      //get all abstractalbum with no songs associated.
+    iDbAbstractAlbum->GetAllItemsWithNoSongL(iItemsIds);
+    TInt count = iItemsIds.Count();
+    TInt err = KErrNone;
+    if (count)
+        {
+        MPX_DEBUG2("CMPXDbHandler::RemoveAbstractAlbumsWithNoSongL, abstractalbum count[%d] ", iItemsIds.Count());
+        CMPXMessageArray* itemChangedMessages = CMPXMediaArray::NewL();
+        CleanupStack::PushL(itemChangedMessages);
+
+        //go through each one to delete
+        for (TInt i=0; i< count; i++)
+            {
+            TRAP(err, RemoveAbstractAlbumL(iItemsIds[i], *itemChangedMessages, EFalse));
+            if (err != KErrNone)
+                {
+                MPX_DEBUG2("CMPXDbHandler::RemoveAbstractAlbumsWithNoSongL, error happens when delete abstractalbum, err ", err);
+                }
+            }
+       CleanupStack::PopAndDestroy(itemChangedMessages);
+       }
+    CleanupStack::PopAndDestroy(&iItemsIds);
+    EndTransactionL(err); 
+  }
+  
+// ----------------------------------------------------------------------------
+// Remove .alb entry from AbstractAlnum table, TN table if .alb files deleted 
+// from file manager when refresh library
+// ----------------------------------------------------------------------------
+//
+void CMPXDbHandler::AbstractAlbumCleanUpL()
+    {
+    MPX_FUNC("CMPXDbHandler::AbstractAlbumCleanUpL");
+    RArray<TMPXAttribute> attributes;
+    CleanupClosePushL(attributes);
+    attributes.AppendL(KMPXMediaGeneralUri);
+    CMPXMediaArray* mediaArray = CMPXMediaArray::NewL();
+    CleanupStack::PushL(mediaArray);
+    
+    iDbAbstractAlbum->GetAllCategoryItemsL(attributes.Array(), *mediaArray);
+
+    TInt count(mediaArray->Count());
+    if (count)
+        {
+        CMPXMessageArray* itemChangedMessages = CMPXMediaArray::NewL();
+        CleanupStack::PushL(itemChangedMessages);
+        for (TInt i = 0; i < count; i++)
+            {
+            CMPXMedia* element = mediaArray->AtL(i);
+            const TDesC& uri = element->ValueText(KMPXMediaGeneralUri);                                 
+
+            //check if the file exists in file system
+            if (!(BaflUtils::FileExists(iFs, uri)))
+                {
+                //generate abstractalbum UID with the Uri
+                TUint32 abstractAlbumId(MPXDbCommonUtil::GenerateUniqueIdL(iFs, EMPXAbstractAlbum, uri, EFalse));
+                RemoveAbstractAlbumL(abstractAlbumId, *itemChangedMessages, ETrue);             
+                }
+            }
+            CleanupStack::PopAndDestroy(itemChangedMessages);
+        }
+        CleanupStack::PopAndDestroy(mediaArray);       
+        CleanupStack::PopAndDestroy(&attributes);     
+    }
+#endif //ABSTRACTAUDIOALBUM_INCLUDED
 
 // ----------------------------------------------------------------------------
 // CMPXDbHandler::DoCleanupDeletedRecordsL
@@ -2425,8 +2563,7 @@
         TInt attrCount(aAttrs.Count());
         if ( attrCount > 1 || (attrCount == 1 && !(aAttrs[0] == KMPXMediaGeneralId)) )
             {
-            TInt abstractAlbumDrive(iDbAbstractAlbum->GetDriveIdL(abstractAlbumId));
-            MPX_TRAPD(err, iDbMusic->GetAllSongsForAbstractAlbumL(abstractAlbumDrive, abstractAlbumId, aAttrs, aMediaArray));
+            MPX_TRAPD(err, iDbMusic->GetAllSongsForAbstractAlbumL( abstractAlbumId, aAttrs, aMediaArray));
         if (err == KErrNotFound)
             {
             //
@@ -2987,9 +3124,9 @@
     TInt aDriveId,
     CMPXMessageArray* aItemChangedMessages,
     TBool& aItemExist
-#ifdef ABSTRACTAUDIOALBUM_INCLUDED    
-      ,const TDesC& aAlbumArtist,
-    const TDesC& aGenre
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED
+    ,const TDesC& aUri
+    ,const TDesC& aAlbumArtist
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
     )
     {
@@ -2998,21 +3135,28 @@
     MPX_PERF_START(CMPXDbHandler_AddCategoryItemL);
 
     TBool newRecord(EFalse);
-#ifdef ABSTRACTAUDIOALBUM_INCLUDED    
-    TUint32 id(0);   
+#ifdef ABSTRACTAUDIOALBUM_INCLUDED
+    TUint32 id(0);
     //for AbstractAlbum, SetAbstractAlbum, associate songs with abstractalbum.
     //only called with newRecord as EFalse
     //increment songCount if id exist in AbstractAlbum table, otherwise do nothing.
+
+    //only Genre, AbstractAlbum are not case sensitive
+    TBool caseSensitive = ETrue;
+    if ((aCategory == EMPXGenre) || (aCategory == EMPXAbstractAlbum))
+        caseSensitive = EFalse;
+
     if (aCategory == EMPXAbstractAlbum)
-        {     
-        id = iDbAbstractAlbum->AddItemL( aName, aAlbumArtist, aGenre, aDriveId, newRecord, (aCategory != EMPXGenre));     
-        }    
+        {
+        id =  iDbAbstractAlbum->AddItemL(aUri, aName, aAlbumArtist, aDriveId, newRecord, caseSensitive);
+        }
     else
         {
-        id = DbCategoryL(aCategory)->AddItemL(aName, aDriveId, newRecord, (aCategory != EMPXGenre));
-#else 
+        id = DbCategoryL(aCategory)->AddItemL(aName, aDriveId, newRecord, caseSensitive);
+#else
     TUint32 id(DbCategoryL(aCategory)->AddItemL(aName, aDriveId, newRecord, (aCategory != EMPXGenre)));
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
+
     if (newRecord && aItemChangedMessages)
         {
         MPXDbCommonUtil::AddItemChangedMessageL(*aItemChangedMessages, id, EMPXItemInserted,
@@ -3022,7 +3166,7 @@
     // and it is NOT a new record,
     // we should send the item number changed message
     else if ( ( aCategory == EMPXGenre || aCategory == EMPXComposer ) &&
-    		!newRecord && aItemChangedMessages )
+            !newRecord && aItemChangedMessages )
         {
         MPXDbCommonUtil::AddItemChangedMessageL(*aItemChangedMessages, id, EMPXItemModified,
             aCategory, KDBPluginUid);
@@ -3032,7 +3176,6 @@
        }
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
     MPX_PERF_END(CMPXDbHandler_AddCategoryItemL);
-
     return id;
     }
 
@@ -3220,10 +3363,10 @@
 // ----------------------------------------------------------------------------------------------------------
 // CMPXDbHandler::HandleGetAlbumNameForSongL
 // ----------------------------------------------------------------------------------------------------------
-// 
+//
 HBufC* CMPXDbHandler::HandleGetAlbumNameFromIdL(TUint32 aId)
     {
-    return iDbAbstractAlbum->GetNameL(aId);
+    return iDbAbstractAlbum->GetUriL(aId);
     }
 #endif // ABSTRACTAUDIOALBUM_INCLUDED
 // End of file