1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-05 08:32:42 +02:00

More work on dir watchers -- it's mostly there except for the actual DB modifications

This commit is contained in:
Jeff Mitchell 2011-04-01 17:05:40 -04:00
parent de92b0b726
commit 06f0dd8768
6 changed files with 99 additions and 11 deletions

View File

@ -38,21 +38,33 @@ DatabaseCommand_DirMtimes::execSelect( DatabaseImpl* dbi )
{
QMap<QString,unsigned int> 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<QString, unsigned int> &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 );
}

View File

@ -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<QString, unsigned int> 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<QString, unsigned int> &mtimes );
void execSelect( DatabaseImpl* dbi );
void execUpdate( DatabaseImpl* dbi );
QString m_prefix;
QStringList m_prefixes;
bool m_update;
QMap<QString, unsigned int> m_tosave;
};

View File

@ -185,9 +185,12 @@ MusicScanner::listerFinished( const QMap<QString, unsigned int>& newmtimes )
{
qDebug() << "Removing stale dir:" << path;
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( 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() ) );

View File

@ -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 );

View File

@ -22,11 +22,15 @@
#include <QThread>
#include <QCoreApplication>
#include <QFileSystemWatcher>
#include <QTimer>
#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<QString, unsigned int> ) ),
SLOT( setInitialPaths( QMap<QString, unsigned int> ) ) );
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>(cmd) );
}
void
ScanManager::setInitialPaths( QMap<QString, unsigned int> 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;

View File

@ -21,6 +21,7 @@
#include <QObject>
#include <QStringList>
#include <QMap>
#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<QString, unsigned int> pathMap );
private slots:
void scannerQuit();
void scannerFinished();
void scannerDestroyed( QObject* scanner );
void startupWatchPaths();
void onSettingsChanged();