Enter MonitoredZimFileInfo::ZimFileStatus

At this point the new enum serves mainly for separating logging
from the logic. But it enables to enhance the logic with more
elaborate handling.
This commit is contained in:
Veloman Yunkan 2024-08-25 17:46:08 +04:00 committed by Kelson
parent dee1365327
commit a43c53bf3d
2 changed files with 46 additions and 7 deletions

View File

@ -906,27 +906,49 @@ size_t ContentManager::handleNewZimFiles(const QString& dirPath, const QStringSe
{
size_t countOfSuccessfullyAddedZims = 0;
for (const auto& file : fileNames) {
const bool addedToLib = handleZimFileInMonitoredDir(dirPath, file);
const bool addedToLib = handleZimFileInMonitoredDirLogged(dirPath, file);
countOfSuccessfullyAddedZims += addedToLib;
}
return countOfSuccessfullyAddedZims;
}
namespace
{
#ifndef QT_NO_DEBUG
// indexed by MonitoredZimFileInfo::ZimFileStatus enum
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"
};
#endif
} // unnamed namespace
bool ContentManager::handleZimFileInMonitoredDirLogged(QString dir, QString fileName)
{
DBGOUT("ContentManager::handleZimFileInMonitoredDir(" << dir << ", " << fileName << ")");
const int status = handleZimFileInMonitoredDir(dir, fileName);
DBGOUT("\t" << monitoredDirZimFileHandlingMsgs[status]);
return status == MonitoredZimFileInfo::ADDED_TO_THE_LIBRARY;
}
int ContentManager::handleZimFileInMonitoredDir(QString dir, QString fileName)
{
const auto bookPath = QDir::toNativeSeparators(dir + "/" + fileName);
kiwix::Manager manager(mp_library->getKiwixLibrary());
DBGOUT("directory monitoring: file appeared: " << bookPath);
if ( mp_library->isBeingDownloadedByUs(bookPath) ) {
DBGOUT(" it is being downloaded by us, ignoring...");
return MonitoredZimFileInfo::BEING_DOWNLOADED_BY_US;
} else if ( manager.addBookFromPath(bookPath.toStdString()) ) {
DBGOUT(" and was added to the library");
m_knownZimsInDir[dir].insert(fileName);
return 1;
return MonitoredZimFileInfo::ADDED_TO_THE_LIBRARY;
} else {
DBGOUT(" but could not be added to the library");
return MonitoredZimFileInfo::COULD_NOT_BE_ADDED_TO_THE_LIBRARY;
}
return 0;
}
void ContentManager::updateLibraryFromDir(QString dirPath)

View File

@ -108,6 +108,22 @@ public slots:
void downloadWasCancelled(const QString& id);
void handleError(QString errSummary, QString errDetails);
private: // types
struct MonitoredZimFileInfo
{
enum ZimFileStatus
{
// the file is known to be downloaded by our own download manager
BEING_DOWNLOADED_BY_US,
// the file was added the library successfully
ADDED_TO_THE_LIBRARY,
// the attempt to add the file to the library failed
COULD_NOT_BE_ADDED_TO_THE_LIBRARY
};
};
private: // functions
QStringList getBookIds();
// reallyEraseBook() doesn't ask for confirmation (unlike eraseBook())
@ -120,6 +136,7 @@ private: // functions
void updateLibraryFromDir(QString dir);
void handleDisappearedZimFiles(const QString& dirPath, const QStringSet& fileNames);
size_t handleNewZimFiles(const QString& dirPath, const QStringSet& fileNames);
bool handleZimFileInMonitoredDirLogged(QString dirPath, QString fileName);
int handleZimFileInMonitoredDir(QString dirPath, QString fileName);
bool handleDisappearedBook(QString bookId);