From fdac248492e48e9a28438eb9e6d499f8b9a030b4 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Sun, 25 Aug 2024 15:53:49 +0400 Subject: [PATCH] Directory monitoring ignores unchanged bad ZIMs When a change to a monitored directory is detected, any bad ZIM files in it (that could not be added to the library during the previous update) are ignored unless their modification timestamp has changed. --- src/contentmanager.cpp | 33 ++++++++++++++++++++++++++------- src/contentmanager.h | 9 ++++++++- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 53b43ce..744ac29 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -927,7 +927,8 @@ const char* monitoredDirZimFileHandlingMsgs[] = { "", "it is being downloaded by us, ignoring...", "the file was added to the library", - "the file could not be added to the library" + "the file could not be added to the library", + "it is an unchanged known bad zim file" }; #endif @@ -942,14 +943,32 @@ bool ContentManager::handleZimFileInMonitoredDirLogged(QString dir, QString file return status == MonitoredZimFileInfo::ADDED_TO_THE_LIBRARY; } +void ContentManager::MonitoredZimFileInfo::updateStatus(const MonitoredZimFileInfo& prevInfo) +{ + Q_ASSERT(prevInfo.status != ADDED_TO_THE_LIBRARY); + + if ( this->lastModified == prevInfo.lastModified ) { + this->status = UNCHANGED_KNOWN_BAD_ZIM_FILE; + } else { + this->status = PROCESS_NOW; + } +} + ContentManager::MonitoredZimFileInfo ContentManager::getMonitoredZimFileInfo(QString dir, QString fileName) const { - Q_UNUSED(dir); - Q_UNUSED(fileName); - // TODO: implement properly - MonitoredZimFileInfo zfi; - zfi.status = MonitoredZimFileInfo::PROCESS_NOW; - return zfi; + const auto bookPath = QDir::toNativeSeparators(dir + "/" + fileName); + + MonitoredZimFileInfo zimFileInfo; + + zimFileInfo.lastModified = QFileInfo(bookPath).lastModified(); + + const auto& zimsInDir = m_knownZimsInDir[dir]; + const auto fileInfoEntry = zimsInDir.constFind(fileName); + if ( fileInfoEntry != zimsInDir.constEnd() ) { + zimFileInfo.updateStatus(fileInfoEntry.value()); + } + + return zimFileInfo; } int ContentManager::handleZimFileInMonitoredDir(QString dir, QString fileName) diff --git a/src/contentmanager.h b/src/contentmanager.h index 10e659d..6c20991 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -123,10 +123,17 @@ private: // types ADDED_TO_THE_LIBRARY, // the attempt to add the file to the library failed - COULD_NOT_BE_ADDED_TO_THE_LIBRARY + COULD_NOT_BE_ADDED_TO_THE_LIBRARY, + + // the file couldn't be added to the library earlier and hasn't + // changed since then + UNCHANGED_KNOWN_BAD_ZIM_FILE }; + void updateStatus(const MonitoredZimFileInfo& prevInfo); + ZimFileStatus status = PROCESS_NOW; + QDateTime lastModified; }; typedef QMap ZimFileName2InfoMap;