Directory monitoring works via file names instead of paths

- ContentManager::m_knownZimFiles[dirPath] now stores filenames (rather
  than full paths) of known ZIM files in directory at dirPath.

- Library::getLibraryZimsFromDir() returns a set of file names (rather
  than paths)

Note that the change of the semantics of ContentManager::m_knownZimFiles
has been carried out via the change in the value of the second argument
of ContentManager::setMonitorDirZims():

1. In KiwixApp::setupDirectoryMonitoring() the latter is fed with the
   output of (the now changed) Library::getLibraryZimsFromDir()

2. In ContentManager::updateLibraryFromDir() all variables representing
   a set of files now contain filenames only (note that
   ContentManager::handleNewZimFiles() returns just a subset of its second
   parameter) and therefore produce a set of filenames.
This commit is contained in:
Veloman Yunkan 2024-07-30 16:55:54 +04:00 committed by Kelson
parent 743400fa1a
commit 354a4d1f8a
3 changed files with 15 additions and 13 deletions

View File

@ -864,10 +864,11 @@ void ContentManager::asyncUpdateLibraryFromDir(QString dir)
}); });
} }
void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths) void ContentManager::handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames)
{ {
const auto kiwixLib = mp_library->getKiwixLibrary(); const auto kiwixLib = mp_library->getKiwixLibrary();
for (auto bookPath : zimPaths) { for (const auto& file : fileNames) {
const auto bookPath = QDir::toNativeSeparators(dirPath + "/" + file);
try { try {
DBGOUT("directory monitoring: file disappeared: " << bookPath); DBGOUT("directory monitoring: file disappeared: " << bookPath);
const auto book = kiwixLib->getBookByPath(bookPath.toStdString()); const auto book = kiwixLib->getBookByPath(bookPath.toStdString());
@ -879,18 +880,19 @@ void ContentManager::handleDisappearedZimFiles(const QStringSet& zimPaths)
} }
} }
ContentManager::QStringSet ContentManager::handleNewZimFiles(const QStringSet& zimPaths) ContentManager::QStringSet ContentManager::handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames)
{ {
QStringSet successfullyAddedZims; QStringSet successfullyAddedZims;
const auto kiwixLib = mp_library->getKiwixLibrary(); const auto kiwixLib = mp_library->getKiwixLibrary();
kiwix::Manager manager(kiwixLib); kiwix::Manager manager(kiwixLib);
for (auto bookPath : zimPaths) { for (const auto& file : fileNames) {
const auto bookPath = QDir::toNativeSeparators(dirPath + "/" + file);
DBGOUT("directory monitoring: file appeared: " << bookPath); DBGOUT("directory monitoring: file appeared: " << bookPath);
if ( mp_library->isBeingDownloadedByUs(bookPath) ) { if ( mp_library->isBeingDownloadedByUs(bookPath) ) {
DBGOUT(" it is being downloaded by us, ignoring..."); DBGOUT(" it is being downloaded by us, ignoring...");
} else if ( manager.addBookFromPath(bookPath.toStdString()) ) { } else if ( manager.addBookFromPath(bookPath.toStdString()) ) {
DBGOUT(" and was added to the library"); DBGOUT(" and was added to the library");
successfullyAddedZims.insert(bookPath); successfullyAddedZims.insert(file);
} else { } else {
DBGOUT(" but could not be added to the library"); DBGOUT(" but could not be added to the library");
} }
@ -906,13 +908,13 @@ void ContentManager::updateLibraryFromDir(QString dirPath)
QStringSet zimsInDir; QStringSet zimsInDir;
for (const auto &file : dir.entryList({"*.zim"})) { for (const auto &file : dir.entryList({"*.zim"})) {
zimsInDir.insert(QDir::toNativeSeparators(dirPath + "/" + file)); zimsInDir.insert(file);
} }
const QStringSet zimsNotInLib = zimsInDir - zimsPresentInLib; const QStringSet zimsNotInLib = zimsInDir - zimsPresentInLib;
const QStringSet removedZims = zimsPresentInLib - zimsInDir; const QStringSet removedZims = zimsPresentInLib - zimsInDir;
handleDisappearedZimFiles(removedZims); handleDisappearedZimFiles(dirPath, removedZims);
const auto successfullyAddedZims = handleNewZimFiles(zimsNotInLib); const auto successfullyAddedZims = handleNewZimFiles(dirPath, zimsNotInLib);
if (!removedZims.empty() || !successfullyAddedZims.empty()) { if (!removedZims.empty() || !successfullyAddedZims.empty()) {
mp_library->save(); mp_library->save();
emit(booksChanged()); emit(booksChanged());

View File

@ -118,8 +118,8 @@ private: // functions
void setCategories(); void setCategories();
void setLanguages(); void setLanguages();
void updateLibraryFromDir(QString dir); void updateLibraryFromDir(QString dir);
void handleDisappearedZimFiles(const QStringSet& zimPaths); void handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames);
QStringSet handleNewZimFiles(const QStringSet& zimPaths); QStringSet handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames);
void handleDisappearedZimFile(QString bookId); void handleDisappearedZimFile(QString bookId);
// Get the book with the specified id from // Get the book with the specified id from

View File

@ -201,9 +201,9 @@ Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
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(); const QFileInfo fileInfo(filePath);
if (absoluteDir == dir) { if (fileInfo.absoluteDir() == dir) {
zimsInDir.insert(filePath); zimsInDir.insert(fileInfo.fileName());
} }
} }
return zimsInDir; return zimsInDir;