Better return type of Library::getLibraryZimsFromDir()

... which results in slightly simpler Library::updateFromDir().
This commit is contained in:
Veloman Yunkan 2024-07-22 17:18:11 +04:00 committed by Kelson
parent 7581b34f7b
commit 8b2e6d23dd
2 changed files with 14 additions and 14 deletions

View File

@ -194,21 +194,21 @@ void Library::save()
mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml")); mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
} }
void Library::setMonitorDirZims(QString monitorDir, QStringList zimList) void Library::setMonitorDirZims(QString monitorDir, QStringSet zimList)
{ {
m_knownZimsInDir[monitorDir] = zimList; m_knownZimsInDir[monitorDir] = zimList;
} }
QStringList Library::getLibraryZimsFromDir(QString dir) const Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
{ {
QStringList zimsInDir; QStringSet zimsInDir;
for (auto str : getBookIds()) { for (auto str : getBookIds()) {
auto filePath = QString::fromStdString(getBookById(str).getPath()); auto filePath = QString::fromStdString(getBookById(str).getPath());
if ( filePath.endsWith(BEINGDOWNLOADEDSUFFIX) ) if ( filePath.endsWith(BEINGDOWNLOADEDSUFFIX) )
continue; continue;
QDir absoluteDir = QFileInfo(filePath).absoluteDir(); QDir absoluteDir = QFileInfo(filePath).absoluteDir();
if (absoluteDir == dir) { if (absoluteDir == dir) {
zimsInDir.push_back(filePath); zimsInDir.insert(filePath);
} }
} }
return zimsInDir; return zimsInDir;
@ -219,20 +219,18 @@ void Library::updateFromDir(QString monitorDir)
QMutexLocker locker(&m_updateFromDirMutex); QMutexLocker locker(&m_updateFromDirMutex);
const QDir dir(monitorDir); const QDir dir(monitorDir);
QStringList newDirEntries = dir.entryList({"*.zim"}); QStringList newDirEntries = dir.entryList({"*.zim"});
QStringList oldDirEntries = m_knownZimsInDir[monitorDir]; const QStringSet oldDirEntries = m_knownZimsInDir[monitorDir];
for (auto &str : newDirEntries) { for (auto &str : newDirEntries) {
str = QDir::toNativeSeparators(monitorDir + "/" + str); str = QDir::toNativeSeparators(monitorDir + "/" + str);
} }
QSet<QString> newDir, oldDir; QSet<QString> newDir;
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
newDir = QSet<QString>::fromList(newDirEntries); newDir = QSet<QString>::fromList(newDirEntries);
oldDir = QSet<QString>::fromList(oldDirEntries);
#else #else
newDir = QSet<QString>(newDirEntries.begin(), newDirEntries.end()); newDir = QSet<QString>(newDirEntries.begin(), newDirEntries.end());
oldDir = QSet<QString>(oldDirEntries.begin(), oldDirEntries.end());
#endif #endif
QStringList addedZims = (newDir - oldDir).values(); QStringList addedZims = (newDir - oldDirEntries).values();
QStringList removedZims = (oldDir - newDir).values(); QStringList removedZims = (oldDirEntries - newDir).values();
auto manager = kiwix::Manager(LibraryManipulator(this)); auto manager = kiwix::Manager(LibraryManipulator(this));
bool needsRefresh = !removedZims.empty(); bool needsRefresh = !removedZims.empty();
for (auto bookPath : addedZims) { for (auto bookPath : addedZims) {
@ -251,7 +249,7 @@ void Library::updateFromDir(QString monitorDir)
} }
if (needsRefresh) { if (needsRefresh) {
emit(booksChanged()); emit(booksChanged());
setMonitorDirZims(monitorDir, newDir.values()); setMonitorDirZims(monitorDir, newDir);
} }
} }

View File

@ -26,6 +26,8 @@ class Library : public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QStringList bookIds READ getBookIds NOTIFY booksChanged) Q_PROPERTY(QStringList bookIds READ getBookIds NOTIFY booksChanged)
public: public:
typedef QSet<QString> QStringSet;
Library(const QString& libraryDirectory); Library(const QString& libraryDirectory);
virtual ~Library(); virtual ~Library();
QString openBookFromPath(const QString& zimPath); QString openBookFromPath(const QString& zimPath);
@ -34,8 +36,8 @@ public:
QStringList getBookIds() const; QStringList getBookIds() const;
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const; QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const;
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); } const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); }
QStringList getLibraryZimsFromDir(QString dir) const; QStringSet getLibraryZimsFromDir(QString dir) const;
void setMonitorDirZims(QString monitorDir, QStringList zimList); void setMonitorDirZims(QString monitorDir, QStringSet zimList);
void addBookToLibrary(kiwix::Book& book); void addBookToLibrary(kiwix::Book& book);
void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir); void addBookBeingDownloaded(const kiwix::Book& book, QString downloadDir);
bool isBeingDownloadedByUs(QString path) const; bool isBeingDownloadedByUs(QString path) const;
@ -59,7 +61,7 @@ private:
QMutex m_updateFromDirMutex; QMutex m_updateFromDirMutex;
kiwix::LibraryPtr mp_library; kiwix::LibraryPtr mp_library;
QString m_libraryDirectory; QString m_libraryDirectory;
QMap<QString, QStringList> m_knownZimsInDir; QMap<QString, QStringSet> m_knownZimsInDir;
friend class LibraryManipulator; friend class LibraryManipulator;
}; };