diff --git a/src/libtomahawk/database/databasecommand_deletefiles.cpp b/src/libtomahawk/database/databasecommand_deletefiles.cpp index e881c07c9..101b3aadf 100644 --- a/src/libtomahawk/database/databasecommand_deletefiles.cpp +++ b/src/libtomahawk/database/databasecommand_deletefiles.cpp @@ -60,82 +60,95 @@ DatabaseCommand_DeleteFiles::exec( DatabaseImpl* dbi ) // qDebug() << Q_FUNC_INFO; Q_ASSERT( !source().isNull() ); - int deleted = 0; QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id(); TomahawkSqlQuery delquery = dbi->newquery(); QString lastPath; - if ( m_dir.path() != QString( "." ) && source()->isLocal() ) + if ( source()->isLocal() ) { - qDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid; - TomahawkSqlQuery dirquery = dbi->newquery(); - - dirquery.prepare( QString( "SELECT id, url FROM file WHERE source %1 AND url LIKE ?" ) - .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) ); - delquery.prepare( QString( "DELETE FROM file WHERE source %1 AND id = ?" ) - .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) ); - - dirquery.bindValue( 0, "file://" + m_dir.canonicalPath() + "/%" ); - dirquery.exec(); - - while ( dirquery.next() ) + if ( m_dir.path() != QString( "." ) ) { - QFileInfo fi( dirquery.value( 1 ).toString().mid( 7 ) ); // remove file:// - if ( fi.canonicalPath() != m_dir.canonicalPath() ) + qDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid; + TomahawkSqlQuery dirquery = dbi->newquery(); + + dirquery.prepare( QString( "SELECT id, url FROM file WHERE source IS NULL AND url LIKE ?" ) ); + dirquery.bindValue( 0, "file://" + m_dir.canonicalPath() + "/%" ); + dirquery.exec(); + + while ( dirquery.next() ) { - if ( lastPath != fi.canonicalPath() ) - qDebug() << "Skipping subdir:" << fi.canonicalPath(); + QFileInfo fi( dirquery.value( 1 ).toString().mid( 7 ) ); // remove file:// + if ( fi.canonicalPath() != m_dir.canonicalPath() ) + { + if ( lastPath != fi.canonicalPath() ) + qDebug() << "Skipping subdir:" << fi.canonicalPath(); - lastPath = fi.canonicalPath(); - continue; + lastPath = fi.canonicalPath(); + continue; + } + + m_files << dirquery.value( 1 ).toString(); + m_ids << dirquery.value( 1 ).toUInt(); } - - m_ids << dirquery.value( 0 ).toUInt(); - m_files << dirquery.value( 1 ).toString(); } - - foreach ( const QVariant& id, m_ids ) + else if ( !m_ids.isEmpty() ) { - delquery.bindValue( 0, id.toUInt() ); - if( !delquery.exec() ) - { - qDebug() << "Failed to delete file:" - << delquery.lastError().databaseText() - << delquery.lastError().driverText() - << delquery.boundValues(); - continue; - } + TomahawkSqlQuery dirquery = dbi->newquery(); - deleted++; + dirquery.prepare( QString( "SELECT url FROM file WHERE source IS NULL AND id IN ( ? )" ) ); + + QString idstring; + foreach( const QVariant& id, m_ids ) + idstring.append( id.toString() + ", " ); + idstring.chop( 2 ); //remove the trailing ", " + + dirquery.bindValue( 0, idstring ); + dirquery.exec(); + while ( dirquery.next() ) + m_files << dirquery.value( 0 ).toString(); } } - else if ( !m_ids.isEmpty() && source()->isLocal() ) + else { - delquery.prepare( QString( "DELETE FROM file WHERE source %1 AND url = ?" ) - .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) ); - foreach( const QVariant& id, m_ids ) - { -// qDebug() << "Deleting" << id.toUInt() << "from db for source" << srcid; - - const QString url = QString( "servent://%1\t%2" ).arg( source()->userName() ).arg( id.toString() ); - m_files << url; - - delquery.bindValue( 0, id.toUInt() ); - if( !delquery.exec() ) - { - qDebug() << "Failed to delete file:" - << delquery.lastError().databaseText() - << delquery.lastError().driverText() - << delquery.boundValues(); - continue; - } - - deleted++; - } + m_files << QString( "servent://%1\t%2" ).arg( source()->userName() ).arg( id.toString() ); } -// qDebug() << "Deleted" << deleted << m_ids << m_files; + if ( m_deleteAll ) + { + delquery.prepare( QString( "DELETE FROM file WHERE source %1" ) + .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) ); + + if( !delquery.exec() ) + { + qDebug() << "Failed to delete file:" + << delquery.lastError().databaseText() + << delquery.lastError().driverText() + << delquery.boundValues(); + } + + emit done( m_files, source()->collection() ); + return; + } + else if ( !m_ids.isEmpty() ) + { + delquery.prepare( QString( "DELETE FROM file WHERE source %1 AND url IN ( ? )" ) + .arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) ) ); + + QString idstring; + foreach( const QVariant& id, m_ids ) + idstring.append( id.toString() + ", " ); + idstring.chop( 2 ); //remove the trailing ", " + + delquery.bindValue( 0, idstring ); + if( !delquery.exec() ) + { + qDebug() << "Failed to delete file:" + << delquery.lastError().databaseText() + << delquery.lastError().driverText() + << delquery.boundValues(); + } + } emit done( m_files, source()->collection() ); } diff --git a/src/libtomahawk/database/databasecommand_deletefiles.h b/src/libtomahawk/database/databasecommand_deletefiles.h index 16a25c1e2..2cc43871d 100644 --- a/src/libtomahawk/database/databasecommand_deletefiles.h +++ b/src/libtomahawk/database/databasecommand_deletefiles.h @@ -32,20 +32,27 @@ class DLLEXPORT DatabaseCommand_DeleteFiles : public DatabaseCommandLoggable { Q_OBJECT Q_PROPERTY( QVariantList ids READ ids WRITE setIds ) +Q_PROPERTY( bool deleteAll READ deleteAll WRITE setDeleteAll ) public: explicit DatabaseCommand_DeleteFiles( QObject* parent = 0 ) : DatabaseCommandLoggable( parent ) {} + explicit DatabaseCommand_DeleteFiles( const Tomahawk::source_ptr& source, QObject* parent = 0 ) + : DatabaseCommandLoggable( parent ), m_deleteAll( true ) + { + setSource( source ); + } + explicit DatabaseCommand_DeleteFiles( const QDir& dir, const Tomahawk::source_ptr& source, QObject* parent = 0 ) - : DatabaseCommandLoggable( parent ), m_dir( dir ) + : DatabaseCommandLoggable( parent ), m_dir( dir ), m_deleteAll( false ) { setSource( source ); } explicit DatabaseCommand_DeleteFiles( const QVariantList& ids, const Tomahawk::source_ptr& source, QObject* parent = 0 ) - : DatabaseCommandLoggable( parent ), m_ids( ids ) + : DatabaseCommandLoggable( parent ), m_ids( ids ), m_deleteAll( false ) { setSource( source ); } @@ -54,23 +61,24 @@ public: virtual void exec( DatabaseImpl* ); virtual bool doesMutates() const { return true; } - virtual bool localOnly() const { return m_files.isEmpty(); } + virtual bool localOnly() const { return false; } virtual void postCommitHook(); - QStringList files() const { return m_files; } - void setFiles( const QStringList& f ) { m_files = f; } - QVariantList ids() const { return m_ids; } void setIds( const QVariantList& i ) { m_ids = i; } + bool deleteAll() const { return m_deleteAll; } + void setDeleteAll( const bool deleteAll ) { m_deleteAll = deleteAll; } + signals: void done( const QStringList&, const Tomahawk::collection_ptr& ); void notify( const QStringList& ); private: - QDir m_dir; QStringList m_files; + QDir m_dir; QVariantList m_ids; + bool m_deleteAll; }; #endif // DATABASECOMMAND_DELETEFILES_H diff --git a/src/scanmanager.cpp b/src/scanmanager.cpp index 534d0ed0b..981276398 100644 --- a/src/scanmanager.cpp +++ b/src/scanmanager.cpp @@ -30,6 +30,7 @@ #include "database/database.h" #include "database/databasecommand_dirmtimes.h" +#include "database/databasecommand_deletefiles.h" #include "utils/logger.h" @@ -147,6 +148,9 @@ ScanManager::runDirScan( const QStringList& paths, bool manualFull ) if ( !Database::instance() || ( Database::instance() && !Database::instance()->isReady() ) ) return; + if ( paths.isEmpty() ) + Database::instance()->enqueue( QSharedPointer( new DatabaseCommand_DeleteFiles( SourceList::instance()->getLocal() ) ) ); + if ( !m_musicScannerThreadController && m_scanner.isNull() ) //still running if these are not zero { m_scanTimer->stop();