Baloo

tools/baloosearch/main.cpp
1/*
2 This file is part of the KDE Baloo Project
3 SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <vhanda@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include <QCoreApplication>
9#include <QCommandLineParser>
10#include <QCommandLineOption>
11#include <QFileInfo>
12#include <QTextStream>
13#include <QElapsedTimer>
14
15#include <KAboutData>
16#include <KLocalizedString>
17
18#include "query.h"
19
20#include <iostream>
21
22int main(int argc, char* argv[])
23{
24 QCoreApplication app(argc, argv);
25
26 KAboutData aboutData(QStringLiteral("Baloo"),
27 i18n("Baloo Search"),
28 QStringLiteral(PROJECT_VERSION),
29 i18n("A tool to search through the files indexed by Baloo"),
31 aboutData.addAuthor(i18n("Vishesh Handa"), QString(), QStringLiteral("vhanda@kde.org"));
32
34
35 QCommandLineParser parser;
36 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("l") << QStringLiteral("limit"),
37 i18n("The maximum number of results"),
38 i18n("limit")));
39 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("o") << QStringLiteral("offset"),
40 i18n("Offset from which to start the search"),
41 i18n("offset")));
42 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("t") << QStringLiteral("type"),
43 i18n("Type of data to be searched"),
44 i18n("typeStr")));
45 parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("d") << QStringLiteral("directory"),
46 i18n("Limit search to specified directory"),
47 i18n("directory")));
48 parser.addOption(QCommandLineOption({QStringLiteral("i"), QStringLiteral("id")},
49 i18n("Show document IDs")));
50 parser.addOption(QCommandLineOption({QStringLiteral("s"), QStringLiteral("sort")},
51 i18n("Sorting criteria"), QStringLiteral("auto|time|none"), QStringLiteral("auto")));
52 parser.addPositionalArgument(i18n("query"), i18n("List of words to query for"));
53 parser.addHelpOption();
54 parser.addVersionOption();
55 parser.process(app);
56
57 int queryLimit = -1;
58 int offset = 0;
59 QString typeStr;
60 bool showDocumentId = parser.isSet(QStringLiteral("id"));
61
62 QStringList args = parser.positionalArguments();
63 if (args.isEmpty()) {
64 parser.showHelp(1);
65 }
66
67 if (parser.isSet(QStringLiteral("type"))) {
68 typeStr = parser.value(QStringLiteral("type"));
69 }
70 if (parser.isSet(QStringLiteral("limit"))) {
71 queryLimit = parser.value(QStringLiteral("limit")).toInt();
72 }
73 if (parser.isSet(QStringLiteral("offset"))) {
74 offset = parser.value(QStringLiteral("offset")).toInt();
75 }
76 const Baloo::Query::SortingOption orderBy = [&parser]() {
77 auto val = parser.value(QStringLiteral("sort"));
78 if (val == QStringLiteral("auto")) {
80 } else if (val == QStringLiteral("time")) {
82 } else if (val == QStringLiteral("none")) {
84 } else {
85 parser.showHelp(1);
86 }
87 }();
88
89 QString queryStr = args.join(QLatin1Char(' '));
90
92 query.addType(typeStr);
93 query.setSearchString(queryStr);
94 query.setLimit(queryLimit);
95 query.setOffset(offset);
96 query.setSortingOption(orderBy);
97
98 if (parser.isSet(QStringLiteral("directory"))) {
99 QString folderName = parser.value(QStringLiteral("directory"));
100 const QFileInfo fi(folderName);
101 if (!fi.isDir()) {
102 std::cerr << qPrintable(i18n("%1 is not a valid directory", folderName)) << std::endl;
103 return 1;
104 }
105 while (folderName.endsWith(QLatin1Char('/')) && (folderName.size() > 1)) {
106 folderName.chop(1);
107 }
108 auto canonicalPath = fi.canonicalFilePath();
109 if (canonicalPath != folderName) {
110 std::cerr << qPrintable(i18n("Using canonical path '%1' for '%2'", canonicalPath, folderName)) << std::endl;
111 }
112 query.setIncludeFolder(canonicalPath);
113 }
114
115 QElapsedTimer timer;
116 timer.start();
117
118 Baloo::ResultIterator iter = query.exec();
119 while (iter.next()) {
120 const QString filePath = iter.filePath();
121 if (showDocumentId) {
122 std::cout << iter.documentId().constData() << " ";
123 }
124 std::cout << qPrintable(filePath) << std::endl;
125 }
126 std::cerr << qPrintable(i18n("Elapsed: %1 msecs", timer.nsecsElapsed() / 1000000.0)) << std::endl;
127
128 return 0;
129}
The Query class is the central class to query to search for files from the Index.
Definition query.h:54
@ SortNone
The results are returned in the most efficient order.
Definition query.h:109
@ SortAuto
The results are returned in the order Baloo decides should be ideal.
Definition query.h:118
static void setApplicationData(const KAboutData &aboutData)
QString i18n(const char *text, const TYPE &arg...)
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
const char * constData() const const
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
bool isSet(const QCommandLineOption &option) const const
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void showHelp(int exitCode)
QString value(const QCommandLineOption &option) const const
qint64 nsecsElapsed() const const
bool isEmpty() const const
void chop(qsizetype n)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
qsizetype size() const const
int toInt(bool *ok, int base) const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:07 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.