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 | |
22 | int 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" ), |
30 | KAboutLicense::GPL); |
31 | aboutData.addAuthor(i18n("Vishesh Handa" ), task: QString(), QStringLiteral("vhanda@kde.org" )); |
32 | |
33 | KAboutData::setApplicationData(aboutData); |
34 | |
35 | QCommandLineParser parser; |
36 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << QStringLiteral("l" ) << QStringLiteral("limit" ), |
37 | i18n("The maximum number of results" ), |
38 | i18n("limit" ))); |
39 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << QStringLiteral("o" ) << QStringLiteral("offset" ), |
40 | i18n("Offset from which to start the search" ), |
41 | i18n("offset" ))); |
42 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << QStringLiteral("t" ) << QStringLiteral("type" ), |
43 | i18n("Type of data to be searched" ), |
44 | i18n("typeStr" ))); |
45 | parser.addOption(commandLineOption: QCommandLineOption(QStringList() << QStringLiteral("d" ) << QStringLiteral("directory" ), |
46 | i18n("Limit search to specified directory" ), |
47 | i18n("directory" ))); |
48 | parser.addOption(commandLineOption: QCommandLineOption({QStringLiteral("i" ), QStringLiteral("id" )}, |
49 | i18n("Show document IDs" ))); |
50 | parser.addOption(commandLineOption: 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(exitCode: 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" )) { |
79 | return Baloo::Query::SortAuto; |
80 | } else if (val == QStringLiteral("time" )) { |
81 | return Baloo::Query::SortAuto; |
82 | } else if (val == QStringLiteral("none" )) { |
83 | return Baloo::Query::SortNone; |
84 | } else { |
85 | parser.showHelp(exitCode: 1); |
86 | } |
87 | }(); |
88 | |
89 | QString queryStr = args.join(sep: QLatin1Char(' ')); |
90 | |
91 | Baloo::Query query; |
92 | query.addType(type: 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(c: QLatin1Char('/')) && (folderName.size() > 1)) { |
106 | folderName.chop(n: 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 | } |
130 | |