Bug 190881

Summary: Delete empty directories after "organize files"
Product: [Applications] amarok Reporter: Iñaki Baz Castillo <ibc>
Component: Collections/LocalAssignee: Amarok Developers <amarok-bugs-dist>
Status: RESOLVED FIXED    
Severity: major CC: bugs, eqisow, kuba.serafinowski, matej, ralf-engels, unnamedrambler
Priority: NOR    
Version: 2.3.1-GIT   
Target Milestone: 2.4.0   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In: 2.4.1
Sentry Crash Report:

Description Iñaki Baz Castillo 2009-04-27 23:40:35 UTC
Version:           2.0.2 (using 4.2.2 (KDE 4.2.2), Kubuntu packages)
Compiler:          cc
OS:                Linux (x86_64) release 2.6.28-11-generic

Most of the times I use "organize files" to move/rename files, I get empty directories (those in which originally the files were). If so, I must explore these directories and delete them.

It would be great if Amarok automatically deletes empty directories after moving all the files to other location.
Comment 1 Justin Newman 2009-09-15 16:41:26 UTC
This is also true for the "Delete" function and is still present in 2.1.80.
Comment 2 DrSlony 2009-10-29 10:54:48 UTC
It should also be configurable in Amarok's settings whether a dir with no music files should be treated as empty, and therefore get deleted once we move that music to our collection. There could be leftover garbage like playlists, images, text files.
Comment 3 Myriam Schweingruber 2009-10-29 11:15:58 UTC
Changing version, 2.0.2 was a very long time ago...
Comment 4 Casey Link 2010-04-04 06:16:55 UTC
*** Bug 233197 has been marked as a duplicate of this bug. ***
Comment 5 Casey Link 2010-04-04 18:18:06 UTC
commit 02686e6c2d9fb9697f48ce0137e8cd0aa6857873
Author: Casey Link <unnamedrambler@gmail.com>
Date:   Sun Apr 4 12:11:11 2010 -0400

    Fix deletion, and remove empty directories in the process. Organizing
    files will now remove empty directories as well.
    BUG: 190881

diff --git a/src/core-impl/collections/sqlcollection/SqlCollectionLocation.cpp b/src/core-impl/collections/sqlcollection/SqlCollectionLocation.cpp
index 66a5da2..f119956 100644
--- a/src/core-impl/collections/sqlcollection/SqlCollectionLocation.cpp
+++ b/src/core-impl/collections/sqlcollection/SqlCollectionLocation.cpp
@@ -124,10 +124,12 @@ SqlCollectionLocation::remove( const Meta::TrackPtr &track )
     {
         bool removed;
         KUrl src = track->playableUrl();
-        if( destination() == source() ) // is organize operation?
+        if( isGoingToRemoveSources() ) // is organize operation?
         {
             SqlCollectionLocation* destinationloc = dynamic_cast<SqlCollectionLocation*>( destination() );
             src = destinationloc->m_originalUrls[track];
+            if( src == track->playableUrl() )
+                return false;
         }
         // we are going to delete it from the database only if is no longer on disk
         removed = !QFile::exists( src.path() );
@@ -152,6 +154,7 @@ SqlCollectionLocation::remove( const Meta::TrackPtr &track )
 
             QFileInfo file( src.path() );
             QDir dir = file.dir();
+            dir.setFilter( QDir::NoDotAndDotDot );
             const QStringList collectionFolders = m_collection->mountPointManager()->collectionFolders();
             while( !collectionFolders.contains( dir.absolutePath() ) && !dir.isRoot() && dir.count() == 0 )
             {
@@ -285,9 +288,11 @@ void
 SqlCollectionLocation::slotRemoveJobFinished( KJob *job )
 {
     DEBUG_BLOCK
-    if( job->error() )
+    int error = job->error();
+    if( error != 0 && error != KIO::ERR_DOES_NOT_EXIST )
     {
         //TODO: proper error handling
+        debug() << "KIO::ERR_DOES_NOT_EXIST" << KIO::ERR_DOES_NOT_EXIST;
         warning() << "An error occurred when removing a file: " << job->errorString();
         transferError(m_removejobs.value( job ), KIO::buildErrorString( job->error(), job->errorString() ) );
     }
@@ -575,15 +580,17 @@ bool SqlCollectionLocation::startNextRemoveJob()
         Meta::TrackPtr track = m_removetracks.takeFirst();
         KUrl src = track->playableUrl();
 
-        if( destination() == source() ) // is organize operation?
+        debug() << "isGoingToRemoveSources() " << isGoingToRemoveSources();
+        if( isGoingToRemoveSources() ) // is organize operation?
         {
             SqlCollectionLocation* destinationloc = dynamic_cast<SqlCollectionLocation*>( destination() );
             src = destinationloc->m_originalUrls[track];
+            if( src == track->playableUrl() ) {
+                debug() << "src == dst";
+                break;
+            }
         }
 
-        if( src == track->playableUrl() ) // src == dst
-            break;
-
         KIO::DeleteJob *job = 0;
 
         src.cleanPath();
diff --git a/src/core/collections/CollectionLocation.cpp b/src/core/collections/CollectionLocation.cpp
index 80ab2b7..27e9efd 100644
--- a/src/core/collections/CollectionLocation.cpp
+++ b/src/core/collections/CollectionLocation.cpp
@@ -48,6 +48,7 @@ CollectionLocation::CollectionLocation( const Collections::Collection* parentCol
     , m_parentCollection( parentCollection )
     , m_removeSources( false )
     , m_isRemoveAction( false )
+    , m_noRemoveConfirmation( false )
 {
     //nothing to do
 }
Comment 6 Kuba Serafinowski 2010-04-06 18:12:03 UTC
Still doesn't remove empty dirs, when some of files to be moved have src == dest, and some not.

debug output: http://pastebin.com/VYTRqsSc
Comment 7 Myriam Schweingruber 2010-08-12 09:34:38 UTC
Wasn't this fixed in 2.3.1?
Comment 8 Kuba Serafinowski 2010-08-12 11:04:22 UTC
at most partially, nothing changed since my last comment
Comment 9 Sergey Ivanov 2011-02-04 18:13:20 UTC
Git commit 0c7c4872e9bd923b1b10fe2ecd84f2630d8e6024 by Sergey Ivanov.
Committed on 04/02/11 at 18:08.
Pushed by ivanov into branch 'master'.

Fixed empty folders removeing after collection organization.
BUG: 190881

M  +1    -0    ChangeLog     
M  +2    -23   src/core-impl/collections/support/FileCollectionLocation.cpp     
M  +0    -1    src/core-impl/collections/support/FileCollectionLocation.h     
M  +31   -1    src/core/collections/CollectionLocation.cpp     

http://commits.kde.org/amarok/0c7c4872e9bd923b1b10fe2ecd84f2630d8e6024