1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-04 13:17:34 +02:00

Update deletefiles commands to, well, work. Maybe.

This commit is contained in:
Jeff Mitchell
2011-09-30 14:46:36 -04:00
parent 757f1c6175
commit 4aa8ec6bb8
3 changed files with 90 additions and 65 deletions

View File

@@ -60,82 +60,95 @@ DatabaseCommand_DeleteFiles::exec( DatabaseImpl* dbi )
// qDebug() << Q_FUNC_INFO; // qDebug() << Q_FUNC_INFO;
Q_ASSERT( !source().isNull() ); Q_ASSERT( !source().isNull() );
int deleted = 0;
QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id(); QVariant srcid = source()->isLocal() ? QVariant( QVariant::Int ) : source()->id();
TomahawkSqlQuery delquery = dbi->newquery(); TomahawkSqlQuery delquery = dbi->newquery();
QString lastPath; QString lastPath;
if ( m_dir.path() != QString( "." ) && source()->isLocal() ) if ( source()->isLocal() )
{ {
qDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid; if ( m_dir.path() != QString( "." ) )
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() )
{ {
QFileInfo fi( dirquery.value( 1 ).toString().mid( 7 ) ); // remove file:// qDebug() << "Deleting" << m_dir.path() << "from db for localsource" << srcid;
if ( fi.canonicalPath() != m_dir.canonicalPath() ) 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() ) QFileInfo fi( dirquery.value( 1 ).toString().mid( 7 ) ); // remove file://
qDebug() << "Skipping subdir:" << fi.canonicalPath(); if ( fi.canonicalPath() != m_dir.canonicalPath() )
{
if ( lastPath != fi.canonicalPath() )
qDebug() << "Skipping subdir:" << fi.canonicalPath();
lastPath = fi.canonicalPath(); lastPath = fi.canonicalPath();
continue; 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();
} }
else if ( !m_ids.isEmpty() )
foreach ( const QVariant& id, m_ids )
{ {
delquery.bindValue( 0, id.toUInt() ); TomahawkSqlQuery dirquery = dbi->newquery();
if( !delquery.exec() )
{
qDebug() << "Failed to delete file:"
<< delquery.lastError().databaseText()
<< delquery.lastError().driverText()
<< delquery.boundValues();
continue;
}
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 ) foreach( const QVariant& id, m_ids )
{ m_files << QString( "servent://%1\t%2" ).arg( source()->userName() ).arg( id.toString() );
// 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++;
}
} }
// 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() ); emit done( m_files, source()->collection() );
} }

View File

@@ -32,20 +32,27 @@ class DLLEXPORT DatabaseCommand_DeleteFiles : public DatabaseCommandLoggable
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY( QVariantList ids READ ids WRITE setIds ) Q_PROPERTY( QVariantList ids READ ids WRITE setIds )
Q_PROPERTY( bool deleteAll READ deleteAll WRITE setDeleteAll )
public: public:
explicit DatabaseCommand_DeleteFiles( QObject* parent = 0 ) explicit DatabaseCommand_DeleteFiles( QObject* parent = 0 )
: DatabaseCommandLoggable( parent ) : 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 ) 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 ); setSource( source );
} }
explicit DatabaseCommand_DeleteFiles( const QVariantList& ids, const Tomahawk::source_ptr& source, QObject* parent = 0 ) 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 ); setSource( source );
} }
@@ -54,23 +61,24 @@ public:
virtual void exec( DatabaseImpl* ); virtual void exec( DatabaseImpl* );
virtual bool doesMutates() const { return true; } virtual bool doesMutates() const { return true; }
virtual bool localOnly() const { return m_files.isEmpty(); } virtual bool localOnly() const { return false; }
virtual void postCommitHook(); virtual void postCommitHook();
QStringList files() const { return m_files; }
void setFiles( const QStringList& f ) { m_files = f; }
QVariantList ids() const { return m_ids; } QVariantList ids() const { return m_ids; }
void setIds( const QVariantList& i ) { m_ids = i; } void setIds( const QVariantList& i ) { m_ids = i; }
bool deleteAll() const { return m_deleteAll; }
void setDeleteAll( const bool deleteAll ) { m_deleteAll = deleteAll; }
signals: signals:
void done( const QStringList&, const Tomahawk::collection_ptr& ); void done( const QStringList&, const Tomahawk::collection_ptr& );
void notify( const QStringList& ); void notify( const QStringList& );
private: private:
QDir m_dir;
QStringList m_files; QStringList m_files;
QDir m_dir;
QVariantList m_ids; QVariantList m_ids;
bool m_deleteAll;
}; };
#endif // DATABASECOMMAND_DELETEFILES_H #endif // DATABASECOMMAND_DELETEFILES_H

View File

@@ -30,6 +30,7 @@
#include "database/database.h" #include "database/database.h"
#include "database/databasecommand_dirmtimes.h" #include "database/databasecommand_dirmtimes.h"
#include "database/databasecommand_deletefiles.h"
#include "utils/logger.h" #include "utils/logger.h"
@@ -147,6 +148,9 @@ ScanManager::runDirScan( const QStringList& paths, bool manualFull )
if ( !Database::instance() || ( Database::instance() && !Database::instance()->isReady() ) ) if ( !Database::instance() || ( Database::instance() && !Database::instance()->isReady() ) )
return; return;
if ( paths.isEmpty() )
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( new DatabaseCommand_DeleteFiles( SourceList::instance()->getLocal() ) ) );
if ( !m_musicScannerThreadController && m_scanner.isNull() ) //still running if these are not zero if ( !m_musicScannerThreadController && m_scanner.isNull() ) //still running if these are not zero
{ {
m_scanTimer->stop(); m_scanTimer->stop();