From 4b1522b82a234abef22726dde1db4d7afdb2b9cd Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 8 Jan 2023 17:37:28 +0100 Subject: [PATCH] Introduce FileSaverOptions to consolidate common parameters --- cli/commandadd.cpp | 9 ++++++++- cli/commandupdate.cpp | 8 ++++++-- gui/mainwindow.cpp | 7 +++++-- shared/filesaver.cpp | 17 ++++++++--------- shared/filesaver.h | 14 ++++++++++---- shared/filesaveroptions.h | 14 ++++++++++++++ shared/indexer.cpp | 19 +++++++------------ shared/indexer.h | 7 +++---- shared/indexsyncer.cpp | 25 +++++++++++-------------- shared/indexsyncer.h | 9 ++++----- shared/shared.pro | 1 + 11 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 shared/filesaveroptions.h diff --git a/cli/commandadd.cpp b/cli/commandadd.cpp index cb7d5a3..40003be 100644 --- a/cli/commandadd.cpp +++ b/cli/commandadd.cpp @@ -71,9 +71,16 @@ int CommandAdd::handle(QStringList arguments) } } + FileSaverOptions fileSaverOptions; + fileSaverOptions.keepGoing = keepGoing; + fileSaverOptions.fillPathsOnlyWithContent = fillContent; + fileSaverOptions.pathsOnly = pathsOnly; + fileSaverOptions.verbose = false; + indexer = new Indexer(*this->dbService); + indexer->setFileSaverOptions(fileSaverOptions); + indexer->setTargetPaths(files.toVector()); - indexer->setKeepGoing(keepGoing); connect(indexer, &Indexer::pathsCountChanged, this, [](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; }); diff --git a/cli/commandupdate.cpp b/cli/commandupdate.cpp index 22fcd6f..71a1a3f 100644 --- a/cli/commandupdate.cpp +++ b/cli/commandupdate.cpp @@ -40,8 +40,12 @@ int CommandUpdate::handle(QStringList arguments) bool hasErrors = false; IndexSyncer *syncer = new IndexSyncer(*this->dbService); - syncer->setKeepGoing(keepGoing); - syncer->setVerbose(verbose); + + FileSaverOptions fileOptions; + fileOptions.keepGoing = keepGoing; + fileOptions.verbose = verbose; + + syncer->setFileSaverOptions(fileOptions); syncer->setPattern(pattern); syncer->setDryRun(dryRun); syncer->setRemoveDeletedFromIndex(deleteMissing); diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 8cae20f..32a4256 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -264,8 +264,11 @@ void MainWindow::startIndexSync() progressDialog.setValue(0); progressDialog.open(); - indexSyncer->setKeepGoing(true); - indexSyncer->setVerbose(false); + FileSaverOptions options; + options.keepGoing = true; + options.verbose = false; + + indexSyncer->setFileSaverOptions(options); indexSyncer->setDryRun(false); indexSyncer->setRemoveDeletedFromIndex(true); diff --git a/shared/filesaver.cpp b/shared/filesaver.cpp index 6dfb66e..858de10 100644 --- a/shared/filesaver.cpp +++ b/shared/filesaver.cpp @@ -38,18 +38,17 @@ SaveFileResult FileSaver::updateFile(QString path) return saveFile(info); } -int FileSaver::addFiles(const QVector paths, bool keepGoing, bool verbose) +int FileSaver::addFiles(const QVector paths) { - return processFiles(paths, std::bind(&FileSaver::addFile, this, std::placeholders::_1), keepGoing, verbose); + return processFiles(paths, std::bind(&FileSaver::addFile, this, std::placeholders::_1)); } -int FileSaver::updateFiles(const QVector paths, bool keepGoing, bool verbose) +int FileSaver::updateFiles(const QVector paths) { - return processFiles(paths, std::bind(&FileSaver::updateFile, this, std::placeholders::_1), keepGoing, verbose); + return processFiles(paths, std::bind(&FileSaver::updateFile, this, std::placeholders::_1)); } -int FileSaver::processFiles(const QVector paths, std::function saverFunc, - bool keepGoing, bool verbose) +int FileSaver::processFiles(const QVector paths, std::function saverFunc) { std::atomic terminate{false}; std::atomic processedCount{0}; @@ -60,7 +59,7 @@ int FileSaver::processFiles(const QVector paths, std::functionfileSaverOptions.verbose) { Logger::info() << "Processing " << path << Qt::endl; } @@ -68,7 +67,7 @@ int FileSaver::processFiles(const QVector paths, std::functionfileSaverOptions.keepGoing) { terminate = true; } @@ -76,7 +75,7 @@ int FileSaver::processFiles(const QVector paths, std::functionfileSaverOptions.verbose) { if(result == SKIPPED) { diff --git a/shared/filesaver.h b/shared/filesaver.h index 58733ec..1024790 100644 --- a/shared/filesaver.h +++ b/shared/filesaver.h @@ -2,6 +2,7 @@ #define FILESAVER_H #include #include +#include "filesaveroptions.h" #include "pagedata.h" #include "filedata.h" #include "sqlitedbservice.h" @@ -11,16 +12,21 @@ class FileSaver private: SqliteDbService *dbService; QStringList excludedPaths = Common::excludedPaths(); + FileSaverOptions fileSaverOptions; public: FileSaver(SqliteDbService &dbService); SaveFileResult addFile(QString path); SaveFileResult updateFile(QString path); SaveFileResult saveFile(const QFileInfo &fileInfo); - int processFiles(const QVector paths, std::function saverFunc, - bool keepGoing, bool verbose); - int addFiles(const QVector paths, bool keepGoing, bool verbose); - int updateFiles(const QVector paths, bool keepGoing, bool verbose); + int processFiles(const QVector paths, std::function saverFunc); + int addFiles(const QVector paths); + int updateFiles(const QVector paths); + + void setFileSaverOptions(FileSaverOptions options) + { + this->fileSaverOptions = options; + } }; #endif // FILESAVER_H diff --git a/shared/filesaveroptions.h b/shared/filesaveroptions.h new file mode 100644 index 0000000..9088685 --- /dev/null +++ b/shared/filesaveroptions.h @@ -0,0 +1,14 @@ +#ifndef FILESAVEROPTIONS_H +#define FILESAVEROPTIONS_H + +class FileSaverOptions +{ + public: + bool verbose = false; + bool keepGoing = false; + bool pathsOnly = false; + /* Whether those previously explicitly without content should be filled */ + bool fillPathsOnlyWithContent = false; +}; + +#endif // FILESAVEROPTIONS_H diff --git a/shared/indexer.cpp b/shared/indexer.cpp index b1f9b0a..5cc33b2 100644 --- a/shared/indexer.cpp +++ b/shared/indexer.cpp @@ -73,16 +73,6 @@ void Indexer::setTargetPaths(QVector pathsToScan) this->pathsToScan = pathsToScan; } -void Indexer::setVerbose(bool verbose) -{ - this->verbose = verbose; -} - -void Indexer::setKeepGoing(bool keepGoing) -{ - this->keepGoing = keepGoing; -} - void Indexer::requestCancellation() { this->dirScanner->cancel(); @@ -123,7 +113,7 @@ void Indexer::processFileScanResult(FileScanResult result) if(isErrorSaveFileResult(result.second)) { this->currentIndexResult.results.append(result); - if(!keepGoing) + if(!this->fileSaverOptions.keepGoing) { this->requestCancellation(); emit finished(); @@ -132,7 +122,7 @@ void Indexer::processFileScanResult(FileScanResult result) } else { - if(verbose) + if(this->fileSaverOptions.verbose) { this->currentIndexResult.results.append(result); } @@ -175,3 +165,8 @@ void Indexer::processFinishedWorker() emit finished(); } } + +void Indexer::setFileSaverOptions(FileSaverOptions options) +{ + this->fileSaverOptions = options; +} diff --git a/shared/indexer.h b/shared/indexer.h index a14958e..f05d534 100644 --- a/shared/indexer.h +++ b/shared/indexer.h @@ -52,8 +52,7 @@ class Indexer : public QObject { Q_OBJECT protected: - bool verbose = false; - bool keepGoing = true; + FileSaverOptions fileSaverOptions; SqliteDbService *db; int progressReportThreshold = 50; @@ -80,8 +79,8 @@ class Indexer : public QObject void beginIndexing(); void setIgnorePattern(QStringList ignorePattern); void setTargetPaths(QVector pathsToScan); - void setVerbose(bool verbose); - void setKeepGoing(bool keepGoing); + + void setFileSaverOptions(FileSaverOptions options); void requestCancellation(); diff --git a/shared/indexsyncer.cpp b/shared/indexsyncer.cpp index 13b5654..70c5f3b 100644 --- a/shared/indexsyncer.cpp +++ b/shared/indexsyncer.cpp @@ -7,21 +7,16 @@ IndexSyncer::IndexSyncer(SqliteDbService &dbService) this->dbService = &dbService; } +void IndexSyncer::setFileSaverOptions(FileSaverOptions options) +{ + fileSaverOptions = options; +} + void IndexSyncer::setDryRun(bool dryRun) { this->dryRun = dryRun; } -void IndexSyncer::setVerbose(bool verbose) -{ - this->verbose = verbose; -} - -void IndexSyncer::setKeepGoing(bool keepGoing) -{ - this->keepGoing = keepGoing; -} - void IndexSyncer::setRemoveDeletedFromIndex(bool removeDeletedFromIndex) { this->removeDeletedFromIndex = removeDeletedFromIndex; @@ -35,7 +30,7 @@ void IndexSyncer::setPattern(QString pattern) void IndexSyncer::sync() { this->stopToken.store(false, std::memory_order_relaxed); - FileSaver saver(*this->dbService); + QVector files; int offset = 0; int limit = 10000; @@ -87,7 +82,7 @@ void IndexSyncer::sync() if(!this->dbService->deleteFile(fileData.absPath)) { emit error("Error: Failed to delete " + fileData.absPath + " from the index"); - if(!this->keepGoing) + if(!this->fileSaverOptions.keepGoing) { emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount); return; @@ -104,13 +99,15 @@ void IndexSyncer::sync() } } - unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate, keepGoing, verbose); + FileSaver saver(*this->dbService); + saver.setFileSaverOptions(this->fileSaverOptions); + unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate); unsigned int shouldHaveUpdatedCount = static_cast(filePathsToUpdate.size()); if(updatedFilesCount != shouldHaveUpdatedCount) { totalErroredFilesCount += (shouldHaveUpdatedCount - updatedFilesCount); - if(!keepGoing) + if(!this->fileSaverOptions.keepGoing) { QString errorMsg = QString("Failed to update all files selected for updating in this batch. Updated") + updatedFilesCount + "out of" + shouldHaveUpdatedCount + "selected for updating"; diff --git a/shared/indexsyncer.h b/shared/indexsyncer.h index 2656fcf..fc8fd46 100644 --- a/shared/indexsyncer.h +++ b/shared/indexsyncer.h @@ -1,16 +1,15 @@ #ifndef INDEXSYNCER_H #define INDEXSYNCER_H #include "sqlitedbservice.h" - +#include "filesaveroptions.h" class IndexSyncer : public QObject { Q_OBJECT private: SqliteDbService *dbService = nullptr; - bool keepGoing = true; + FileSaverOptions fileSaverOptions; bool removeDeletedFromIndex = true; bool dryRun = false; - bool verbose = false; QString pattern; std::atomic stopToken{false}; @@ -18,12 +17,12 @@ class IndexSyncer : public QObject public: IndexSyncer(SqliteDbService &dbService); + void setFileSaverOptions(FileSaverOptions options); + public slots: void sync(); void cancel(); void setDryRun(bool dryRun); - void setVerbose(bool verbose); - void setKeepGoing(bool keepGoing); void setRemoveDeletedFromIndex(bool removeDeletedFromIndex); void setPattern(QString pattern); diff --git a/shared/shared.pro b/shared/shared.pro index 2139c9f..a355c22 100644 --- a/shared/shared.pro +++ b/shared/shared.pro @@ -74,6 +74,7 @@ HEADERS += sqlitesearch.h \ encodingdetector.h \ filedata.h \ filesaver.h \ + filesaveroptions.h \ filescanworker.h \ indexer.h \ indexsyncer.h \