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,152 @@ 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) { + QTextStream out(stdout); + QTextStream err(stderr); + QString separator; + const QString comma = QStringLiteral(","); + const QString objectIn = QStringLiteral("{"); + const QString objectOut = QStringLiteral("}"); + + auto printItem = [&](const QString& name, const QString& value) { + const QString q = QStringLiteral("\""); + out << q << name << q << QStringLiteral(":") << q << value << q; + }; + + out << QStringLiteral("[") << endl; + 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 << separator << objectIn << endl; + printItem(QStringLiteral("file"), filePath.replace(QLatin1String("\""), QLatin1String("\\\""))); + out << comma << endl; + + if (!tr.hasDocument(id)) { + printItem("indexing", "basic"); + out << comma << endl; + if (cfg.shouldBeIndexed(filePath)) { + printItem("status", "scheduled"); + } else { + printItem("status", "disabled"); + } + } else if (QFileInfo(arg).isDir()) { + printItem("indexing", "basic"); + out << comma << endl; + printItem("status", "done"); + } else { + printItem("indexing", "content"); + out << comma << endl; + if (tr.inPhaseOne(id)) { + printItem("status", "scheduled"); + } else if (tr.hasFailed(id)) { + printItem("status", "failed"); + } else { + printItem("status", "done"); + } + } + out << endl << objectOut; + separator = comma; + } + out << endl << "]" << endl; +} + 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 +242,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;