1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-25 02:09:48 +01:00

Finish (for now) the file scanning code. (Also adds an update/rescan difference to the options, where one fully rescans and the other runs a normal update). N.B.: This code is currently hard-coded to watch for changes at 120 seconds and to use filemtimes, not dirmtimes.

This commit is contained in:
Jeff Mitchell 2011-05-20 19:41:03 -04:00
parent c2884e696f
commit 32c84b84bd
7 changed files with 72 additions and 41 deletions

View File

@ -37,6 +37,7 @@ DirLister::go()
{
qDebug() << Q_FUNC_INFO;
qDebug() << "Recursive? : " << (m_recursive ? "true" : "false");
qDebug() << "Manual full? : " << (m_manualFull ? "true" : "false");
if( !m_recursive )
{
foreach( QString dir, m_dirs )
@ -68,7 +69,7 @@ DirLister::go()
void
DirLister::scanDir( QDir dir, int depth, DirLister::Mode mode )
{
qDebug() << "DirLister::scanDir scanning: " << dir.canonicalPath() << " with mode " << mode;
//qDebug() << "DirLister::scanDir scanning: " << dir.canonicalPath() << " with mode " << mode;
if( !dir.exists() )
{
@ -80,15 +81,16 @@ DirLister::scanDir( QDir dir, int depth, DirLister::Mode mode )
const uint mtime = QFileInfo( dir.canonicalPath() ).lastModified().toUTC().toTime_t();
m_newdirmtimes.insert( dir.canonicalPath(), mtime );
if ( m_mode == TomahawkSettings::Dirs && m_dirmtimes.contains( dir.canonicalPath() ) && mtime == m_dirmtimes.value( dir.canonicalPath() ) )
if ( !m_manualFull && m_mode == TomahawkSettings::Dirs && m_dirmtimes.contains( dir.canonicalPath() ) && mtime == m_dirmtimes.value( dir.canonicalPath() ) )
{
// dont scan this dir, unchanged since last time.
}
else
{
if( m_mode == TomahawkSettings::Dirs
&& ( m_dirmtimes.contains( dir.canonicalPath() ) || !m_recursive )
&& mtime == m_dirmtimes.value( dir.canonicalPath() ) )
if( m_manualFull ||
( m_mode == TomahawkSettings::Dirs
&& ( m_dirmtimes.contains( dir.canonicalPath() ) || !m_recursive )
&& mtime == m_dirmtimes.value( dir.canonicalPath() ) ) )
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( new DatabaseCommand_DeleteFiles( dir, SourceList::instance()->getLocal() ) ) );
dir.setFilter( QDir::Files | QDir::Readable | QDir::NoDotAndDotDot );
@ -105,9 +107,9 @@ DirLister::scanDir( QDir dir, int depth, DirLister::Mode mode )
foreach( const QFileInfo& di, dirs )
{
const QString canonical = di.canonicalFilePath();
qDebug() << "Considering dir " << canonical;
//qDebug() << "Considering dir " << canonical;
const bool haveDi = m_dirmtimes.contains( canonical );
qDebug() << "m_dirmtimes contains it?" << haveDi;
//qDebug() << "m_dirmtimes contains it?" << haveDi;
if( !m_newdirmtimes.contains( canonical ) && ( mode == DirLister::Recursive || !haveDi ) ) {
scanDir( di.canonicalFilePath(), depth + 1, DirLister::Recursive );
}
@ -115,10 +117,11 @@ DirLister::scanDir( QDir dir, int depth, DirLister::Mode mode )
}
MusicScanner::MusicScanner( const QStringList& dirs, TomahawkSettings::ScannerMode mode, bool recursive, quint32 bs )
MusicScanner::MusicScanner( const QStringList& dirs, TomahawkSettings::ScannerMode mode, bool manualFull, bool recursive, quint32 bs )
: QObject()
, m_dirs( dirs )
, m_mode( mode )
, m_mode( manualFull ? TomahawkSettings::Dirs : mode )
, m_manualFull( manualFull )
, m_recursive( recursive )
, m_batchsize( bs )
, m_dirListerThreadController( 0 )
@ -218,12 +221,12 @@ MusicScanner::scan()
if ( m_mode == TomahawkSettings::Files )
qDebug() << "Num saved file mtimes from last scan:" << m_filemtimes.size();
connect( this, SIGNAL( batchReady( QVariantList ) ),
SLOT( commitBatch( QVariantList ) ), Qt::DirectConnection );
connect( this, SIGNAL( batchReady( QVariantList, QVariantList ) ),
SLOT( commitBatch( QVariantList, QVariantList ) ), Qt::DirectConnection );
m_dirListerThreadController = new QThread( this );
m_dirLister = QWeakPointer< DirLister >( new DirLister( m_dirs, m_dirmtimes, m_mode, m_recursive ) );
m_dirLister = QWeakPointer< DirLister >( new DirLister( m_dirs, m_dirmtimes, m_mode, m_manualFull, m_recursive ) );
m_dirLister.data()->moveToThread( m_dirListerThreadController );
connect( m_dirLister.data(), SIGNAL( fileToScan( QFileInfo ) ),
@ -247,7 +250,7 @@ MusicScanner::listerFinished( const QMap<QString, unsigned int>& newmtimes )
if( m_scannedfiles.length() )
{
SourceList::instance()->getLocal()->scanningFinished( m_scanned );
commitBatch( m_scannedfiles );
commitBatch( m_scannedfiles, m_filesToDelete );
}
// remove obsolete / stale files
@ -308,11 +311,19 @@ MusicScanner::deleteLister()
void
MusicScanner::commitBatch( const QVariantList& tracks )
MusicScanner::commitBatch( const QVariantList& tracks, const QVariantList& deletethese )
{
if ( deletethese.length() )
{
qDebug() << Q_FUNC_INFO << " deleting " << deletethese.length() << " tracks ";
source_ptr localsrc = SourceList::instance()->getLocal();
Database::instance()->enqueue(
QSharedPointer<DatabaseCommand>( new DatabaseCommand_DeleteFiles( deletethese, SourceList::instance()->getLocal() ) )
);
}
if ( tracks.length() )
{
qDebug() << Q_FUNC_INFO << tracks.length();
qDebug() << Q_FUNC_INFO << " adding " << tracks.length() << " tracks ";
source_ptr localsrc = SourceList::instance()->getLocal();
Database::instance()->enqueue(
QSharedPointer<DatabaseCommand>( new DatabaseCommand_AddFiles( tracks, localsrc ) )
@ -331,15 +342,9 @@ MusicScanner::scanFile( const QFileInfo& fi )
//qDebug() << "All keys: " << m_filemtimes.keys();
//qDebug() << "Checking " << fi.canonicalFilePath() << " with last modified time " << fi.lastModified().toUTC().toTime_t() << " << against value in m_filemtimes " << m_filemtimes.value( "file://" + fi.canonicalFilePath() ).values().first();
if ( fi.lastModified().toUTC().toTime_t() == m_filemtimes.value( "file://" + fi.canonicalFilePath() ).values().first() )
{
qDebug() << "Same mtime";
return;
}
else
{
qDebug() << "Different mtime";
return;
}
m_filesToDelete << m_filemtimes.value( "file://" + fi.canonicalFilePath() ).keys().first();
}
QVariant m = readFile( fi );
@ -350,8 +355,9 @@ MusicScanner::scanFile( const QFileInfo& fi )
if ( m_batchsize != 0 && (quint32)m_scannedfiles.length() >= m_batchsize )
{
qDebug() << "batchReady, size:" << m_scannedfiles.length();
emit batchReady( m_scannedfiles );
emit batchReady( m_scannedfiles, m_filesToDelete );
m_scannedfiles.clear();
m_filesToDelete.clear();
}
}

View File

@ -49,8 +49,8 @@ public:
MTimeOnly
};
DirLister( const QStringList& dirs, const QMap<QString, unsigned int>& dirmtimes, TomahawkSettings::ScannerMode mode, bool recursive )
: QObject(), m_dirs( dirs ), m_dirmtimes( dirmtimes ), m_mode( mode ), m_recursive( recursive )
DirLister( const QStringList& dirs, const QMap<QString, unsigned int>& dirmtimes, TomahawkSettings::ScannerMode mode, bool manualFull, bool recursive )
: QObject(), m_dirs( dirs ), m_dirmtimes( dirmtimes ), m_mode( mode ), m_manualFull( manualFull ), m_recursive( recursive )
{
qDebug() << Q_FUNC_INFO;
}
@ -72,6 +72,7 @@ private:
QStringList m_dirs;
QMap<QString, unsigned int> m_dirmtimes;
TomahawkSettings::ScannerMode m_mode;
bool m_manualFull;
bool m_recursive;
QMap<QString, unsigned int> m_newdirmtimes;
@ -83,13 +84,13 @@ class MusicScanner : public QObject
Q_OBJECT
public:
MusicScanner( const QStringList& dirs, TomahawkSettings::ScannerMode mode, bool recursive = true, quint32 bs = 0 );
MusicScanner( const QStringList& dirs, TomahawkSettings::ScannerMode mode, bool manualFull, bool recursive = true, quint32 bs = 0 );
~MusicScanner();
signals:
//void fileScanned( QVariantMap );
void finished();
void batchReady( const QVariantList& );
void batchReady( const QVariantList&, const QVariantList& );
private:
QVariant readFile( const QFileInfo& fi );
@ -102,7 +103,7 @@ private slots:
void scan();
void setDirMtimes( const QMap< QString, unsigned int >& m );
void setFileMtimes( const QMap< QString, QMap< unsigned int, unsigned int > >& m );
void commitBatch( const QVariantList& );
void commitBatch( const QVariantList& tracks, const QVariantList& deletethese );
private:
QStringList m_dirs;
@ -117,7 +118,9 @@ private:
QMap<QString, QMap< unsigned int, unsigned int > > m_filemtimes;
QMap<QString, unsigned int> m_newdirmtimes;
QList<QVariant> m_scannedfiles;
QVariantList m_scannedfiles;
QVariantList m_filesToDelete;
bool m_manualFull;
bool m_recursive;
quint32 m_batchsize;

View File

@ -50,7 +50,7 @@ ScanManager::ScanManager( QObject* parent )
m_scanTimer = new QTimer( this );
m_scanTimer->setSingleShot( false );
m_scanTimer->setInterval( 10000 );
m_scanTimer->setInterval( 120000 );
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
connect( m_scanTimer, SIGNAL( timeout() ), SLOT( scanTimerTimeout() ) );
@ -141,18 +141,18 @@ ScanManager::scanTimerTimeout()
void
ScanManager::runScan()
ScanManager::runScan( bool manualFull )
{
qDebug() << Q_FUNC_INFO;
if ( !Database::instance() || ( Database::instance() && !Database::instance()->isReady() ) )
return;
runDirScan( TomahawkSettings::instance()->scannerPaths() );
runDirScan( TomahawkSettings::instance()->scannerPaths(), manualFull );
}
void
ScanManager::runDirScan( const QStringList& paths )
ScanManager::runDirScan( const QStringList& paths, bool manualFull )
{
qDebug() << Q_FUNC_INFO;
@ -162,7 +162,7 @@ ScanManager::runDirScan( const QStringList& paths )
if ( !m_musicScannerThreadController && m_scanner.isNull() ) //still running if these are not zero
{
m_musicScannerThreadController = new QThread( this );
m_scanner = QWeakPointer< MusicScanner>( new MusicScanner( paths, TomahawkSettings::instance()->scannerMode() ) );
m_scanner = QWeakPointer< MusicScanner>( new MusicScanner( paths, TomahawkSettings::instance()->scannerMode(), manualFull ) );
m_scanner.data()->moveToThread( m_musicScannerThreadController );
connect( m_scanner.data(), SIGNAL( finished() ), SLOT( scannerFinished() ) );
m_musicScannerThreadController->start( QThread::IdlePriority );

View File

@ -47,8 +47,8 @@ signals:
void finished();
public slots:
void runScan();
void runDirScan( const QStringList& paths );
void runScan( bool manualFull = false );
void runDirScan( const QStringList& paths, bool manualFull );
private slots:
void scannerFinished();

View File

@ -271,7 +271,8 @@ TomahawkWindow::setupSignals()
connect( ui->actionDiagnostics, SIGNAL( triggered() ), SLOT( showDiagnosticsDialog() ) );
connect( ui->actionToggleConnect, SIGNAL( triggered() ), SipHandler::instance(), SLOT( toggleConnect() ) );
// connect( ui->actionAddPeerManually, SIGNAL( triggered() ), SLOT( addPeerManually() ) );
connect( ui->actionRescanCollection, SIGNAL( triggered() ), SLOT( updateCollectionManually() ) );
connect( ui->actionUpdateCollection, SIGNAL( triggered() ), SLOT( updateCollectionManually() ) );
connect( ui->actionRescanCollection, SIGNAL( triggered() ), SLOT( rescanCollectionManually() ) );
connect( ui->actionLoadXSPF, SIGNAL( triggered() ), SLOT( loadSpiff() ));
connect( ui->actionCreatePlaylist, SIGNAL( triggered() ), SLOT( createPlaylist() ));
connect( ui->actionCreateAutomaticPlaylist, SIGNAL( triggered() ), SLOT( createAutomaticPlaylist() ));
@ -378,6 +379,14 @@ TomahawkWindow::updateCollectionManually()
}
void
TomahawkWindow::rescanCollectionManually()
{
if ( TomahawkSettings::instance()->hasScannerPaths() )
ScanManager::instance()->runScan( true );
}
void
TomahawkWindow::addPeerManually()
{

View File

@ -68,6 +68,7 @@ public slots:
void showSettingsDialog();
void showDiagnosticsDialog();
void updateCollectionManually();
void rescanCollectionManually();
void pluginMenuAdded(QMenu*);
void pluginMenuRemoved(QMenu*);

View File

@ -35,7 +35,7 @@
<x>0</x>
<y>0</y>
<width>1000</width>
<height>22</height>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="menuSettings">
@ -48,6 +48,7 @@
<property name="title">
<string>&amp;Music Player</string>
</property>
<addaction name="actionUpdateCollection"/>
<addaction name="actionRescanCollection"/>
<addaction name="separator"/>
<addaction name="actionShowOfflineSources"/>
@ -115,9 +116,12 @@
<string>Add &amp;Friend...</string>
</property>
</action>
<action name="actionRescanCollection">
<action name="actionUpdateCollection">
<property name="text">
<string>Re&amp;scan Collection...</string>
<string>U&amp;pdate Collection</string>
</property>
<property name="toolTip">
<string>Update Collection</string>
</property>
</action>
<action name="actionPreferences">
@ -190,6 +194,14 @@
<enum>QAction::ApplicationSpecificRole</enum>
</property>
</action>
<action name="actionRescanCollection">
<property name="text">
<string>Fully &amp;Rescan Collection</string>
</property>
<property name="toolTip">
<string>Fully Rescan Collection</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>