diff --git a/src/libtomahawk/database/databasecommand_dirmtimes.cpp b/src/libtomahawk/database/databasecommand_dirmtimes.cpp index 829a609c8..1bedc8402 100644 --- a/src/libtomahawk/database/databasecommand_dirmtimes.cpp +++ b/src/libtomahawk/database/databasecommand_dirmtimes.cpp @@ -38,21 +38,33 @@ DatabaseCommand_DirMtimes::execSelect( DatabaseImpl* dbi ) { QMap mtimes; TomahawkSqlQuery query = dbi->newquery(); - if( m_prefix.isEmpty() ) + if( m_prefix.isEmpty() && m_prefixes.isEmpty() ) query.exec( "SELECT name, mtime FROM dirs_scanned" ); + else if( m_prefixes.isEmpty() ) + execSelectPath( dbi, m_prefix, mtimes ); else { - query.prepare( QString( "SELECT name, mtime " - "FROM dirs_scanned " - "WHERE name LIKE '%1%'" ).arg( m_prefix.replace( '\'',"''" ) ) ); - query.exec(); + if( !m_prefix.isEmpty() ) + execSelectPath( dbi, m_prefix, mtimes ); + foreach( QString path, m_prefixes ) + execSelectPath( dbi, path, mtimes ); } + emit done( mtimes ); +} + +void +DatabaseCommand_DirMtimes::execSelectPath( DatabaseImpl *dbi, QString &path, QMap &mtimes ) +{ + TomahawkSqlQuery query = dbi->newquery(); + query.prepare( QString( "SELECT name, mtime " + "FROM dirs_scanned " + "WHERE name LIKE '%1%'" ).arg( path.replace( '\'',"''" ) ) ); + query.exec(); + while( query.next() ) { mtimes.insert( query.value( 0 ).toString(), query.value( 1 ).toUInt() ); } - - emit done( mtimes ); } diff --git a/src/libtomahawk/database/databasecommand_dirmtimes.h b/src/libtomahawk/database/databasecommand_dirmtimes.h index f35a78437..3ff81607b 100644 --- a/src/libtomahawk/database/databasecommand_dirmtimes.h +++ b/src/libtomahawk/database/databasecommand_dirmtimes.h @@ -37,6 +37,10 @@ public: explicit DatabaseCommand_DirMtimes( const QString& prefix = "", QObject* parent = 0 ) : DatabaseCommand( parent ), m_prefix( prefix ), m_update( false ) {} + + explicit DatabaseCommand_DirMtimes( const QStringList& prefixes = QStringList(), QObject* parent = 0 ) + : DatabaseCommand( parent ), m_prefixes( prefixes ), m_update( false ) + {} explicit DatabaseCommand_DirMtimes( QMap tosave, QObject* parent = 0 ) : DatabaseCommand( parent ), m_update( true ), m_tosave( tosave ) @@ -52,9 +56,12 @@ signals: public slots: private: + void execSelectPath( DatabaseImpl *dbi, QString &path, QMap &mtimes ); + void execSelect( DatabaseImpl* dbi ); void execUpdate( DatabaseImpl* dbi ); QString m_prefix; + QStringList m_prefixes; bool m_update; QMap m_tosave; }; diff --git a/src/musicscanner.cpp b/src/musicscanner.cpp index a97171416..fe8eb29f6 100644 --- a/src/musicscanner.cpp +++ b/src/musicscanner.cpp @@ -185,9 +185,12 @@ MusicScanner::listerFinished( const QMap& newmtimes ) { qDebug() << "Removing stale dir:" << path; Database::instance()->enqueue( QSharedPointer( new DatabaseCommand_DeleteFiles( path, SourceList::instance()->getLocal() ) ) ); + emit removeWatchedDir( path ); } } + emit addWatchedDirs( newmtimes.keys() ); + // save mtimes, then quit thread DatabaseCommand_DirMtimes* cmd = new DatabaseCommand_DirMtimes( newmtimes ); connect( cmd, SIGNAL( finished() ), SLOT( deleteLister() ) ); diff --git a/src/musicscanner.h b/src/musicscanner.h index c4fc5bb5b..dd2725334 100644 --- a/src/musicscanner.h +++ b/src/musicscanner.h @@ -76,6 +76,8 @@ signals: //void fileScanned( QVariantMap ); void finished(); void batchReady( const QVariantList& ); + void addWatchedDirs( const QStringList & ); + void removeWatchedDir( const QString & ); private: QVariant readFile( const QFileInfo& fi ); diff --git a/src/scanmanager.cpp b/src/scanmanager.cpp index 81d11969e..e1d5f028c 100644 --- a/src/scanmanager.cpp +++ b/src/scanmanager.cpp @@ -22,11 +22,15 @@ #include #include #include +#include #include "musicscanner.h" #include "tomahawksettings.h" #include "tomahawkutils.h" +#include "database/database.h" +#include "database/databasecommand_dirmtimes.h" + ScanManager* ScanManager::s_instance = 0; @@ -53,8 +57,8 @@ ScanManager::ScanManager( QObject* parent ) if ( TomahawkSettings::instance()->hasScannerPath() ) m_currScannerPath = TomahawkSettings::instance()->scannerPath(); - m_dirWatcher->addPaths( m_currScannerPath ); - qDebug() << "filewatcher dirs = " << m_dirWatcher->directories(); + qDebug() << "loading initial directories to watch"; + QTimer::singleShot( 1000, this, SLOT( startupWatchPaths() ) ); } @@ -98,6 +102,36 @@ ScanManager::onSettingsChanged() } +void +ScanManager::startupWatchPaths() +{ + qDebug() << Q_FUNC_INFO; + + if( !Database::instance() ) + { + QTimer::singleShot( 1000, this, SLOT( startupWatchPaths() ) ); + return; + } + + DatabaseCommand_DirMtimes* cmd = new DatabaseCommand_DirMtimes( m_currScannerPath ); + connect( cmd, SIGNAL( done( QMap ) ), + SLOT( setInitialPaths( QMap ) ) ); + Database::instance()->enqueue( QSharedPointer(cmd) ); +} + + +void +ScanManager::setInitialPaths( QMap pathMap ) +{ + qDebug() << Q_FUNC_INFO; + foreach( QString path, pathMap.keys() ) + { + qDebug() << "Adding " << path << " to watcher"; + m_dirWatcher->addPath( path ); + } +} + + void ScanManager::runManualScan( const QStringList& path ) { @@ -109,6 +143,8 @@ ScanManager::runManualScan( const QStringList& path ) m_scanner = new MusicScanner( path ); m_scanner->moveToThread( m_musicScannerThreadController ); connect( m_scanner, SIGNAL( finished() ), SLOT( scannerFinished() ) ); + connect( m_scanner, SIGNAL( addWatchedDirs( const QStringList & ) ), SLOT( addWatchedDirs( const QStringList & ) ) ); + connect( m_scanner, SIGNAL( removeWatchedDir( const QString & ) ), SLOT( removeWatchedDir( const QString & ) ) ); m_musicScannerThreadController->start( QThread::IdlePriority ); QMetaObject::invokeMethod( m_scanner, "startScan" ); } @@ -116,9 +152,31 @@ ScanManager::runManualScan( const QStringList& path ) qDebug() << "Could not run manual scan, old scan still running"; } +void +ScanManager::addWatchedDirs( const QStringList& paths ) +{ + qDebug() << Q_FUNC_INFO; + QStringList currentWatchedPaths = m_dirWatcher->directories(); + foreach( QString path, paths ) + { + if( !currentWatchedPaths.contains( path ) ) + { + qDebug() << "adding " << path << " to watched dirs"; + m_dirWatcher->addPath( path ); + } + } +} void -ScanManager::handleChangedDir( const QString &path ) +ScanManager::removeWatchedDir( const QString& path ) +{ + qDebug() << Q_FUNC_INFO; + qDebug() << "removing " << path << " from watched dirs"; + m_dirWatcher->removePath( path ); +} + +void +ScanManager::handleChangedDir( const QString& path ) { qDebug() << Q_FUNC_INFO; qDebug() << "Dir changed: " << path; diff --git a/src/scanmanager.h b/src/scanmanager.h index 999b629b5..232627058 100644 --- a/src/scanmanager.h +++ b/src/scanmanager.h @@ -21,6 +21,7 @@ #include #include +#include #include "dllmacro.h" @@ -44,12 +45,17 @@ signals: void finished(); public slots: - void handleChangedDir( const QString &path ); + void handleChangedDir( const QString& path ); + void addWatchedDirs( const QStringList& paths ); + void removeWatchedDir( const QString& path ); + void setInitialPaths( QMap pathMap ); private slots: void scannerQuit(); void scannerFinished(); void scannerDestroyed( QObject* scanner ); + + void startupWatchPaths(); void onSettingsChanged();