diff --git a/src/tools/balooctl/main.cpp b/src/tools/balooctl/main.cpp --- a/src/tools/balooctl/main.cpp +++ b/src/tools/balooctl/main.cpp @@ -89,6 +89,16 @@ parser.addPositionalArgument(QStringLiteral("config"), i18n("Modify the Baloo configuration")); parser.addPositionalArgument(QStringLiteral("monitor"), i18n("Monitor the file indexer")); parser.addPositionalArgument(QStringLiteral("indexSize"), i18n("Display the disk space used by index")); + parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("f") << QStringLiteral("format"), + i18n("Output format <%1|%2|%3>.\nOnly applies to \"%4\"", + QStringLiteral("json"), + QStringLiteral("simple"), + QStringLiteral("multiline (default)"), + QStringLiteral("balooctl status ") + ) + , i18n("format") + + , QStringLiteral("multiline"))); parser.addVersionOption(); parser.addHelpOption(); diff --git a/src/tools/balooctl/statuscommand.cpp b/src/tools/balooctl/statuscommand.cpp --- a/src/tools/balooctl/statuscommand.cpp +++ b/src/tools/balooctl/statuscommand.cpp @@ -45,11 +45,143 @@ return i18n("Print the status of the Indexer"); } +void printMultiLine(Transaction& tr, IndexerConfig& cfg, const QStringList& args) { + QTextStream out(stdout); + QTextStream err(stderr); + for (const QString& arg : args) { + QString filePath = QFileInfo(arg).absoluteFilePath(); + quint64 id = filePathToId(QFile::encodeName(filePath)); + if (id == 0) { + err << i18n("Ignoring non-existent file %1", filePath) << endl; + continue; + } + + + out << i18n("File: %1", filePath) << endl; + if (tr.hasDocument(id)) { + out << i18n("Basic Indexing: Done") << endl; + } else if (cfg.shouldBeIndexed(filePath)) { + out << i18n("Basic Indexing: Scheduled") << endl; + continue; + } else { + // FIXME: Add why it is not being indexed! + out << i18n("Basic Indexing: Disabled") << endl; + continue; + } + + if (QFileInfo(arg).isDir()) { + continue; + } + + if (tr.inPhaseOne(id)) { + out << i18n("Content Indexing: Scheduled") << endl; + } else if (tr.hasFailed(id)) { + out << i18n("Content Indexing: Failed") << endl; + } else { + out << i18n("Content Indexing: Done") << endl; + } + } +} + +void printSimpleFormat(Transaction& tr, IndexerConfig& cfg, const QStringList& args) { + QTextStream out(stdout); + QTextStream err(stderr); + for (const QString& arg : args) { + QString filePath = QFileInfo(arg).absoluteFilePath(); + quint64 id = filePathToId(QFile::encodeName(filePath)); + if (id == 0) { + err << i18n("Ignoring non-existent file %1", filePath) << endl; + continue; + } + + if (QFileInfo(arg).isDir()) { + if (tr.hasDocument(id)) { + out << QLatin1String("Basic indexing done"); + } else if (cfg.shouldBeIndexed(filePath)) { + out << QLatin1String("Basic indexing scheduled"); + + } else { + out << QLatin1String("Indexing disabled"); + } + + } else if (tr.hasDocument(id)) { + if (tr.inPhaseOne(id)) { + out << QLatin1String("Content indexing scheduled"); + } else if (tr.hasFailed(id)) { + out << QLatin1String("Content indexing failed"); + } else { + out << QLatin1String("Content indexing done"); + } + + } else if (cfg.shouldBeIndexed(filePath)) { + out << QLatin1String("Basic indexing scheduled"); + } else { + out << QLatin1String("Indexing disabled"); + } + + out << ":" << filePath << endl; + } +} + +void printJSON(Transaction& tr, IndexerConfig& cfg, const QStringList& args) { + + QJsonArray filesInfo; + for (const QString& arg : args) { + QString filePath = QFileInfo(arg).absoluteFilePath(); + quint64 id = filePathToId(QFile::encodeName(filePath)); + + if (id == 0) { + QTextStream err(stderr); + err << i18n("Ignoring non-existent file %1", filePath) << endl; + continue; + } + QJsonObject fileInfo; + fileInfo["file"] = filePath; + if (!tr.hasDocument(id)) { + fileInfo["indexing"] = "basic"; + if (cfg.shouldBeIndexed(filePath)) { + fileInfo["status"] = "scheduled"; + } else { + fileInfo["status"] = "disabled"; + } + + } else if (QFileInfo(arg).isDir()) { + fileInfo["indexing"] = "basic"; + fileInfo["status"] = "done"; + + } else { + fileInfo["indexing"] = "content"; + if (tr.inPhaseOne(id)) { + fileInfo["status"] = "scheduled"; + } else if (tr.hasFailed(id)) { + fileInfo["status"] = "failed"; + } else { + fileInfo["status"] = "done"; + } + } + + filesInfo.append(fileInfo); + } + + QJsonDocument json; + json.setArray(filesInfo); + QTextStream out(stdout); + out << json.toJson(QJsonDocument::Indented); +} + int StatusCommand::exec(const QCommandLineParser& parser) { QTextStream out(stdout); QTextStream err(stderr); + const QStringList allowedFormats({"simple", "json", "multiline"}); + const QString format = parser.value(QStringLiteral("format")); + + if (!allowedFormats.contains(format)) { + err << i18n("Output format \"%1\" is invalid", format) << endl; + return 1; + } + IndexerConfig cfg; if (!cfg.fileIndexingEnabled()) { err << i18n("Baloo is currently disabled. To enable, please run %1", QStringLiteral("balooctl enable")) << endl; @@ -101,35 +233,12 @@ } else { out << i18n("Index does not exist yet") << endl; } + } else if (format == allowedFormats[0]){ + printSimpleFormat(tr, cfg, args); + } else if (format == allowedFormats[1]){ + printJSON(tr, cfg, args); } else { - for (const QString& arg : args) { - QString filePath = QFileInfo(arg).absoluteFilePath(); - quint64 id = filePathToId(QFile::encodeName(filePath)); - - out << i18n("File: %1", filePath) << endl; - if (tr.hasDocument(id)) { - out << i18n("Basic Indexing: Done") << endl; - } else if (cfg.shouldBeIndexed(filePath)) { - out << i18n("Basic Indexing: Scheduled") << endl; - continue; - } else { - // FIXME: Add why it is not being indexed! - out << i18n("Basic Indexing: Disabled") << endl; - continue; - } - - if (QFileInfo(arg).isDir()) { - continue; - } - - if (tr.inPhaseOne(id)) { - out << i18n("Content Indexing: Scheduled") << endl; - } else if (tr.hasFailed(id)) { - out << i18n("Content Indexing: Failed") << endl; - } else { - out << i18n("Content Indexing: Done") << endl; - } - } + printMultiLine(tr, cfg, args); } return 0;