diff --git a/cli/cli.pro b/cli/cli.pro index 83bd8e1..ba42658 100644 --- a/cli/cli.pro +++ b/cli/cli.pro @@ -20,7 +20,8 @@ SOURCES += \ commanddelete.cpp \ commandupdate.cpp \ commandsearch.cpp \ - commandlist.cpp + commandlist.cpp \ + command.cpp HEADERS += \ command.h \ diff --git a/cli/command.cpp b/cli/command.cpp index 34cbc54..699c885 100644 --- a/cli/command.cpp +++ b/cli/command.cpp @@ -3,3 +3,12 @@ #include #include "command.h" #include "looqsgeneralexception.h" + +void Command::execute() +{ + int result = handle(arguments); + if(autoFinish) + { + emit finishedCmd(result); + } +} diff --git a/cli/command.h b/cli/command.h index 340fbd7..070cf7b 100644 --- a/cli/command.h +++ b/cli/command.h @@ -3,22 +3,37 @@ #include #include #include +#include +#include #include "utils.h" #include "sqlitedbservice.h" -class Command +class Command : public QObject { + Q_OBJECT + signals: + void finishedCmd(int retval); + protected: SqliteDbService *dbService; QString dbConnectionString; + QStringList arguments; + + bool autoFinish = true; public: Command(SqliteDbService &dbService) { this->dbService = &dbService; } - + void setArguments(QStringList arguments) + { + this->arguments = arguments; + } virtual int handle(QStringList arguments) = 0; virtual ~Command(){}; + + public slots: + void execute(); }; #endif // COMMAND_H diff --git a/cli/commandadd.cpp b/cli/commandadd.cpp index 619010a..12ae7af 100644 --- a/cli/commandadd.cpp +++ b/cli/commandadd.cpp @@ -11,6 +11,28 @@ #include "commandadd.h" #include "logger.h" +void CommandAdd::indexerFinished() +{ + IndexResult result = indexer->getResult(); + + Logger::info() << "Total: " << result.total() << Qt::endl; + Logger::info() << "Added: " << result.addedPaths << Qt::endl; + Logger::info() << "Skipped: " << result.skippedPaths << Qt::endl; + auto failedPathsCount = result.erroredPaths; + Logger::info() << "Failed: " << failedPathsCount << Qt::endl; + if(failedPathsCount > 0) + { + Logger::info() << "Failed paths: " << Qt::endl; + for(QString paths : result.failedPaths()) + { + Logger::info() << paths << Qt::endl; + } + } + + /* TODO maybe not 0 if keepGoing not given */ + emit finishedCmd(0); +} + int CommandAdd::handle(QStringList arguments) { QCommandLineParser parser; @@ -51,15 +73,21 @@ int CommandAdd::handle(QStringList arguments) } } - FileSaver saver(*this->dbService); - int numFilesCount = files.size(); - int processedFilesCount = saver.addFiles(files.toVector(), keepGoing, verbose); - if(processedFilesCount != numFilesCount) - { - Logger::error() << "Errors occured while trying to add files to the database. Processed " << processedFilesCount - << "out of" << numFilesCount << "files" << Qt::endl; - return 1; - } + indexer = new Indexer(*this->dbService); + indexer->setTargetPaths(files.toVector()); + + connect(indexer, &Indexer::pathsCountChanged, this, + [](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; }); + + connect(indexer, &Indexer::indexProgress, this, + [](int pathsCount, unsigned int added, unsigned int skipped, unsigned int failed, unsigned int totalCount) + { Logger::info() << "Processed files: " << totalCount << Qt::endl; }); + connect(indexer, &Indexer::finished, this, &CommandAdd::indexerFinished); + + /* TODO: keepGoing, verbose */ + + this->autoFinish = false; + indexer->beginIndexing(); return 0; } diff --git a/cli/commandadd.h b/cli/commandadd.h index 8a56379..125c1bc 100644 --- a/cli/commandadd.h +++ b/cli/commandadd.h @@ -3,15 +3,21 @@ #include #include "command.h" #include "filesaver.h" +#include "indexer.h" + class CommandAdd : public Command { private: SaveFileResult addFile(QString path); + Indexer *indexer; + protected: public: using Command::Command; int handle(QStringList arguments) override; + private slots: + void indexerFinished(); }; #endif // COMMANDADD_H diff --git a/cli/main.cpp b/cli/main.cpp index 1bc17a4..fffa3c0 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -9,6 +9,8 @@ #include #include #include +#include + #include #include "encodingdetector.h" #include "pdfprocessor.h" @@ -23,6 +25,7 @@ #include "logger.h" #include "sandboxedprocessor.h" #include "../shared/common.h" +#include "../shared/filescanworker.h" void printUsage(QString argv0) { @@ -74,6 +77,7 @@ int main(int argc, char *argv[]) return 1; } qRegisterMetaType(); + qRegisterMetaType("FileScanResult"); QString connectionString = Common::databasePath(); DatabaseFactory dbFactory(connectionString); @@ -96,7 +100,9 @@ int main(int argc, char *argv[]) { try { - return cmd->handle(args); + QObject::connect(cmd, &Command::finishedCmd, [](int retval) { QCoreApplication::exit(retval); }); + cmd->setArguments(args); + QTimer::singleShot(0, cmd, &Command::execute); } catch(const LooqsGeneralException &e) { @@ -107,5 +113,6 @@ int main(int argc, char *argv[]) { Logger::error() << "Unknown command " << commandName << Qt::endl; } - return 1; + + return app.exec(); }