From 761737eb4df64b0cff4b71fe261f9376eaf6ff42 Mon Sep 17 00:00:00 2001 From: John Maguire Date: Thu, 5 Jan 2012 15:56:46 +0000 Subject: [PATCH] Add more robust subdirectory detection in LibraryWatcher. --- src/core/macfslistener.mm | 3 +++ src/library/librarywatcher.cpp | 17 +++++++++-------- src/library/librarywatcher.h | 18 ++++++++---------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/core/macfslistener.mm b/src/core/macfslistener.mm index e28c2885f..6d0326710 100644 --- a/src/core/macfslistener.mm +++ b/src/core/macfslistener.mm @@ -46,6 +46,9 @@ void MacFSListener::EventStreamCallback( for (int i = 0; i < num_events; ++i) { QString path = QString::fromUtf8(paths[i]); qLog(Debug) << "Something changed at:" << path; + if (path.endsWith('/')) { + path.chop(1); + } emit me->PathChanged(path); } } diff --git a/src/library/librarywatcher.cpp b/src/library/librarywatcher.cpp index 74457d239..b99366c09 100644 --- a/src/library/librarywatcher.cpp +++ b/src/library/librarywatcher.cpp @@ -115,7 +115,7 @@ LibraryWatcher::ScanTransaction::~ScanTransaction() { if (watcher_->monitor_) { // Watch the new subdirectories foreach (const Subdirectory& subdir, new_subdirs) { - watcher_->AddWatch(subdir.path); + watcher_->AddWatch(watcher_->watched_dirs_[dir_], subdir.path); } } } @@ -207,7 +207,7 @@ void LibraryWatcher::AddDirectory(const Directory& dir, const SubdirectoryList& ScanSubdirectory(subdir.path, subdir, &transaction); if (monitor_) - AddWatch(subdir.path); + AddWatch(data, subdir.path); } } @@ -546,13 +546,14 @@ uint LibraryWatcher::GetMtimeForCue(const QString& cue_path) { : 0; } -void LibraryWatcher::AddWatch(const QString& path) { +void LibraryWatcher::AddWatch(DirData dir, const QString& path) { if (!QFile::exists(path)) return; connect(fs_watcher_, SIGNAL(PathChanged(const QString&)), this, SLOT(DirectoryChanged(const QString&)), Qt::UniqueConnection); fs_watcher_->AddPath(path); + subdir_mapping_[path] = dir; } void LibraryWatcher::RemoveDirectory(const Directory& dir) { @@ -573,11 +574,11 @@ bool LibraryWatcher::FindSongByPath(const SongList& list, const QString& path, S void LibraryWatcher::DirectoryChanged(const QString &subdir) { // Find what dir it was in - Directory dir; - foreach (const DirData& info, watched_dirs_) { - if (subdir.startsWith(info.dir.path)) - dir = info.dir; + QHash::const_iterator it = subdir_mapping_.constFind(subdir); + if (it == subdir_mapping_.constEnd()) { + return; } + Directory dir = it->dir; qLog(Debug) << "Subdir" << subdir << "changed under directory" << dir.path << "id" << dir.id; @@ -700,7 +701,7 @@ void LibraryWatcher::ReloadSettings() { foreach (const DirData& data, watched_dirs_.values()) { SubdirectoryList subdirs = backend_->SubdirsInDirectory(data.dir.id); foreach (const Subdirectory& subdir, subdirs) { - AddWatch(subdir.path); + AddWatch(data, subdir.path); } } } diff --git a/src/library/librarywatcher.h b/src/library/librarywatcher.h index f47c6207f..024acc98f 100644 --- a/src/library/librarywatcher.h +++ b/src/library/librarywatcher.h @@ -21,6 +21,7 @@ #include "directory.h" #include "core/song.h" +#include #include #include #include @@ -140,13 +141,18 @@ class LibraryWatcher : public QObject { ScanTransaction* t, bool force_noincremental = false); private: + // One of these gets stored for each Directory we're watching + struct DirData { + Directory dir; + }; + static bool FindSongByPath(const SongList& list, const QString& path, Song* out); inline static QString NoExtensionPart( const QString &fileName ); inline static QString ExtensionPart( const QString &fileName ); inline static QString DirectoryPart( const QString &fileName ); QString PickBestImage(const QStringList& images); QString ImageForSong(const QString& path, QMap& album_art); - void AddWatch(const QString& path); + void AddWatch(DirData dir, const QString& path); uint GetMtimeForCue(const QString& cue_path); void PerformScan(bool incremental, bool ignore_mtimes); @@ -171,16 +177,12 @@ class LibraryWatcher : public QObject { const QString& matching_cue, QSet* cues_processed); private: - // One of these gets stored for each Directory we're watching - struct DirData { - Directory dir; - }; - LibraryBackend* backend_; TaskManager* task_manager_; QString device_name_; FileSystemWatcherInterface* fs_watcher_; + QHash subdir_mapping_; /* A list of words use to try to identify the (likely) best image * found in an directory to use as cover artwork. @@ -203,10 +205,6 @@ class LibraryWatcher : public QObject { CueParser* cue_parser_; static QStringList sValidImages; - - #ifdef Q_OS_DARWIN - static const int kMaxWatches = 100; - #endif }; inline QString LibraryWatcher::NoExtensionPart( const QString &fileName ) {