1 | /* |
2 | SPDX-FileCopyrightText: 2008-2010 Sebastian Trueg <trueg at kde.org> |
3 | SPDX-FileCopyrightText: 2012-2014 Vishesh Handa <me@vhanda.in> |
4 | SPDX-FileCopyrightText: 2020 Stefan BrĂ¼ns <bruns@kde.org> |
5 | |
6 | SPDX-License-Identifier: GPL-2.0-or-later |
7 | */ |
8 | |
9 | #include "kio_search.h" |
10 | #include "../common/udstools.h" |
11 | |
12 | #include "query.h" |
13 | #include "resultiterator.h" |
14 | #include <sys/stat.h> |
15 | |
16 | #include <QUrl> |
17 | #include <QUrlQuery> |
18 | #include <KUser> |
19 | #include <QCoreApplication> |
20 | #include <KLocalizedString> |
21 | |
22 | using namespace Baloo; |
23 | |
24 | // Pseudo plugin class to embed meta data |
25 | class KIOPluginForMetaData : public QObject |
26 | { |
27 | Q_OBJECT |
28 | Q_PLUGIN_METADATA(IID "org.kde.kio.worker.baloosearch" FILE "baloosearch.json" ) |
29 | }; |
30 | |
31 | namespace |
32 | { |
33 | |
34 | KIO::UDSEntry statSearchFolder(const QUrl& url) |
35 | { |
36 | KIO::UDSEntry uds; |
37 | uds.reserve(size: 9); |
38 | #ifdef Q_OS_WIN |
39 | uds.fastInsert(KIO::UDSEntry::UDS_ACCESS, _S_IREAD ); |
40 | #else |
41 | uds.fastInsert(field: KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IXUSR); |
42 | #endif |
43 | uds.fastInsert(field: KIO::UDSEntry::UDS_USER, value: KUser().loginName()); |
44 | uds.fastInsert(field: KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); |
45 | uds.fastInsert(field: KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory" )); |
46 | uds.fastInsert(field: KIO::UDSEntry::UDS_ICON_OVERLAY_NAMES, QStringLiteral("baloo" )); |
47 | uds.fastInsert(field: KIO::UDSEntry::UDS_DISPLAY_TYPE, i18n("Search Folder" )); |
48 | uds.fastInsert(field: KIO::UDSEntry::UDS_URL, value: url.url()); |
49 | |
50 | QUrlQuery query(url); |
51 | QString title = query.queryItemValue(QStringLiteral("title" ), encoding: QUrl::FullyDecoded); |
52 | if (!title.isEmpty()) { |
53 | uds.fastInsert(field: KIO::UDSEntry::UDS_NAME, value: title); |
54 | uds.fastInsert(field: KIO::UDSEntry::UDS_DISPLAY_NAME, value: title); |
55 | } |
56 | |
57 | return uds; |
58 | } |
59 | |
60 | } |
61 | |
62 | SearchProtocol::SearchProtocol(const QByteArray& poolSocket, const QByteArray& appSocket) |
63 | : KIO::WorkerBase("baloosearch" , poolSocket, appSocket) |
64 | { |
65 | } |
66 | |
67 | SearchProtocol::~SearchProtocol() |
68 | { |
69 | } |
70 | |
71 | KIO::WorkerResult SearchProtocol::listDir(const QUrl& url) |
72 | { |
73 | Query q = Query::fromSearchUrl(url); |
74 | |
75 | q.setSortingOption(Query::SortNone); |
76 | ResultIterator it = q.exec(); |
77 | |
78 | UdsFactory udsf; |
79 | |
80 | while (it.next()) { |
81 | KIO::UDSEntry uds = udsf.createUdsEntry(filePath: it.filePath()); |
82 | if (uds.count()) { |
83 | listEntry(entry: uds); |
84 | } |
85 | } |
86 | |
87 | KIO::UDSEntry uds; |
88 | uds.reserve(size: 5); |
89 | uds.fastInsert(field: KIO::UDSEntry::UDS_NAME, QStringLiteral("." )); |
90 | uds.fastInsert(field: KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); |
91 | uds.fastInsert(field: KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory" )); |
92 | #ifdef Q_OS_WIN |
93 | uds.fastInsert(KIO::UDSEntry::UDS_ACCESS, _S_IREAD ); |
94 | #else |
95 | uds.fastInsert(field: KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IXUSR); |
96 | #endif |
97 | uds.fastInsert(field: KIO::UDSEntry::UDS_USER, value: KUser().loginName()); |
98 | listEntry(entry: uds); |
99 | |
100 | return KIO::WorkerResult::pass(); |
101 | } |
102 | |
103 | KIO::WorkerResult SearchProtocol::mimetype(const QUrl&) |
104 | { |
105 | mimeType(QStringLiteral("inode/directory" )); |
106 | return KIO::WorkerResult::pass(); |
107 | } |
108 | |
109 | KIO::WorkerResult SearchProtocol::stat(const QUrl& url) |
110 | { |
111 | statEntry(entry: statSearchFolder(url)); |
112 | return KIO::WorkerResult::pass(); |
113 | } |
114 | |
115 | extern "C" |
116 | { |
117 | Q_DECL_EXPORT int kdemain(int argc, char** argv) |
118 | { |
119 | QCoreApplication app(argc, argv); |
120 | app.setApplicationName(QStringLiteral("kio_baloosearch" )); |
121 | Baloo::SearchProtocol worker(argv[2], argv[3]); |
122 | worker.dispatchLoop(); |
123 | return 0; |
124 | } |
125 | } |
126 | |
127 | #include "kio_search.moc" |
128 | |