8#include "experimental/databasesanitizer.h"
11#include <KLocalizedString>
13#include <QCoreApplication>
14#include <QCommandLineOption>
15#include <QCommandLineParser>
16#include <QStandardPaths>
19#include <QElapsedTimer>
32 QStringList{QStringLiteral(
"i"), QStringLiteral(
"device-id")},
33 i18n(
"Filter by device id."
34 "\n0 (default) does not filter and everything is printed."
35 "\nPositive numbers are including filters printing only the mentioned device id."
36 "\nNegative numbers are excluding filters printing everything but the mentioned device id."
37 "\nMay be given multiple times."),
42 QStringList{QStringLiteral(
"m"), QStringLiteral(
"missing-only")},
43 i18n(
"List only inaccessible entries.\nOnly applies to \"%1\"", QStringLiteral(
"list"))
46 QStringList{QStringLiteral(
"u"), QStringLiteral(
"mounted-only")},
47 i18n(
"Act only on item on mounted devices")
50 QStringList{QStringLiteral(
"D"), QStringLiteral(
"dry-run")},
51 i18n(
"Print results of a cleaning operation, but do not change anything."
52 "\nOnly applies to \"%1\" command", QStringLiteral(
"clean"))
56const auto commands = std::vector<Command>{
58 QStringLiteral(
"list"),
59 i18n(
"List database contents. Use a regular expression as argument to filter output"),
61 QStringLiteral(
"pattern")
64 QStringLiteral(
"missing-only"),
65 QStringLiteral(
"device-id")
69 QStringLiteral(
"devices"),
84 QStringLiteral(
"clean"),
85 i18n(
"Remove stale database entries"),
87 QStringLiteral(
"pattern")
90 QStringLiteral(
"dry-run"),
91 QStringLiteral(
"device-id"),
92 QStringLiteral(
"mounted-only")
100 for (
const auto& c : commands) {
107 for (
const auto& c : commands) {
108 if (c.name == name) {
117 for (
const auto& c: commands) {
118 auto options = getOptions(c.name);
119 const QString optionStr = options.isEmpty()
121 : QStringLiteral(
" [--%1]").arg(options.join(
QLatin1String(
"] [--")));
124 if (!c.args.isEmpty() ) {
125 argumentStr = QStringLiteral(
" [%1]").arg(c.args.join(QStringLiteral(
"] [")));
128 const QString commandStr = QStringLiteral(
"%1%2%3")
133 const QString str = QStringLiteral(
"%1 %2")
134 .arg(commandStr, -58)
137 allowedcommands.
append(str);
140 return i18n(
"\n\nCommands:\n %1", allCommandsStr);
143int main(
int argc,
char* argv[])
146 KAboutData aboutData(QStringLiteral(
"baloodb"),
147 i18n(
"Baloo Database Sanitizer"),
149 i18n(
"The Baloo Database Lister & Sanitizer"),
151 i18n(
"(c) 2018, Michael Heidelbach"));
152 aboutData.addAuthor(
i18n(
"Michael Heidelbach"),
i18n(
"Maintainer"), QStringLiteral(
"ottwolt@gmail.com"));
158 i18n(
"The command to execute"),
162 i18nc(
"Command",
"A regular expression applied to the URL of database items"
164 ,
"baloodb list '^/media/videos/series'"
167 const QString warnExperiment = QStringLiteral(
168 "===\nPlease note: This is an experimental tool. Command line switches or their meaning may change.\n===");
176 qDebug() <<
"No command";
181 auto command = args.
at(0);
184 if(!allowedCommands().contains(command)) {
185 qDebug() <<
"Unknown command" << command;
190 const auto allowedOptions = getOptions(command);
193 for (
const auto& dev : parser.
values(QStringLiteral(
"device-id"))) {
194 deviceIds.
append(dev.toInt());
197 parser.
isSet(QStringLiteral(
"missing-only"))
198 ? DatabaseSanitizer::IgnoreAvailable
199 : DatabaseSanitizer::IgnoreNone
201 parser.
isSet(QStringLiteral(
"mounted-only"))
202 ? DatabaseSanitizer::IgnoreUnmounted
203 : DatabaseSanitizer::IgnoreNone
212 auto db = globalDatabaseInstance();
218 if (!db->open(Database::ReadOnlyDatabase)) {
219 err <<
i18n(
"Baloo Index could not be opened") <<
endl;
223 err <<
i18n(
"Listing database contents...") <<
endl;
224 san.printList(deviceIds, accessFilter, urlFilter);
226 if (!db->open(Database::ReadOnlyDatabase)) {
227 err <<
i18n(
"Baloo Index could not be opened") <<
endl;
231 err <<
i18n(
"Listing database contents...") <<
endl;
232 san.printDevices(deviceIds, accessFilter);
235 auto dbMode = Database::ReadWriteDatabase;
236 if (!db->open(dbMode)) {
237 err <<
i18n(
"Baloo Index could not be opened") <<
endl;
241 err <<
i18n(
"Removing stale database contents...") <<
endl;
242 san.removeStaleEntries(deviceIds, accessFilter, parser.
isSet(QStringLiteral(
"dry-run")), urlFilter);
An Abstract class from which all other balooctl commands can inherit from.
Provide methods to show database problems and sanitize them.
static void setApplicationData(const KAboutData &aboutData)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Implements storage for docIds without any associated data Instantiated for:
QCommandLineOption addHelpOption()
bool addOptions(const QList< QCommandLineOption > &options)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
bool isSet(const QCommandLineOption &option) const const
QStringList optionNames() const const
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void showHelp(int exitCode)
QStringList values(const QCommandLineOption &option) const const
qint64 nsecsElapsed() const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool isEmpty() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString join(QChar separator) const const
QTextStream & endl(QTextStream &stream)