Changeset View
Changeset View
Standalone View
Standalone View
src/ioslave/recentdocumentsactivities.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Méven Car <meven.car@kdemail.net> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. | ||||
19 | * If not, see <http://www.gnu.org/licenses/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "kactivity-ioslave-logsettings.h" | ||||
23 | #include "recentdocumentsactivities.h" | ||||
24 | | ||||
25 | #include <QCoreApplication> | ||||
26 | #include <QDBusInterface> | ||||
27 | #include <QFileInfo> | ||||
28 | #include <QDebug> | ||||
29 | #include <QUrl> | ||||
30 | #include <QUrlQuery> | ||||
31 | #include <QString> | ||||
32 | | ||||
33 | #include <KRecentDocument> | ||||
34 | #include <KDirWatch> | ||||
35 | #include <KDesktopFile> | ||||
36 | #include <KIO/Job> | ||||
37 | #include <KLocalizedString> | ||||
38 | #include <KProtocolManager> | ||||
39 | | ||||
40 | | ||||
41 | #include <stdio.h> | ||||
42 | | ||||
43 | #include <KActivitiesStats/Cleaning> | ||||
44 | #include <KActivitiesStats/ResultModel> | ||||
45 | #include <KActivitiesStats/Terms> | ||||
46 | | ||||
47 | namespace KAStats = KActivities::Stats; | ||||
48 | | ||||
49 | using namespace KAStats; | ||||
50 | using namespace KAStats::Terms; | ||||
51 | | ||||
52 | extern "C" int Q_DECL_EXPORT kdemain(int argc, char **argv) | ||||
53 | { | ||||
54 | // necessary to use other kio slaves | ||||
55 | QCoreApplication app(argc, argv); | ||||
56 | app.setApplicationName(QStringLiteral("kio_recentdocumentsactivities")); | ||||
57 | if (argc != 4) { | ||||
58 | fprintf(stderr, "Usage: kio_recentdocumentsactivities protocol domain-socket1 domain-socket2\n"); | ||||
59 | exit(-1); | ||||
60 | } | ||||
61 | // start the slave | ||||
62 | RecentDocumentActivities slave(argv[2], argv[3]); | ||||
63 | slave.dispatchLoop(); | ||||
64 | return 0; | ||||
65 | } | ||||
66 | | ||||
67 | bool isRootUrl(const QUrl &url) | ||||
68 | { | ||||
69 | const QString path = url.adjusted(QUrl::StripTrailingSlash).path(); | ||||
70 | return (path.isEmpty() || path == QLatin1String("/")); | ||||
71 | } | ||||
72 | | ||||
73 | | ||||
74 | RecentDocumentActivities::RecentDocumentActivities(const QByteArray& pool, const QByteArray& app): | ||||
75 | ForwardingSlaveBase("recentdocumentsactivities", pool, app) | ||||
76 | {} | ||||
77 | | ||||
78 | RecentDocumentActivities::~RecentDocumentActivities() | ||||
79 | {} | ||||
80 | | ||||
81 | | ||||
82 | bool RecentDocumentActivities::rewriteUrl(const QUrl& url, QUrl& newUrl) | ||||
83 | { | ||||
84 | Q_UNUSED(url) | ||||
85 | Q_UNUSED(newUrl); | ||||
86 | return false; | ||||
87 | } | ||||
88 | | ||||
89 | ResultModel* runQuery(const QUrl& url) { | ||||
90 | auto query = UsedResources | ||||
91 | | RecentlyUsedFirst | ||||
92 | | Agent::any() | ||||
93 | | Limit(30); | ||||
94 | | ||||
95 | // Parse url query parameter | ||||
96 | auto urlQuery = QUrlQuery(url); | ||||
97 | | ||||
98 | // handles type aka mimetype | ||||
99 | if (urlQuery.hasQueryItem(QStringLiteral("type"))) { | ||||
100 | auto typeValue = urlQuery.queryItemValue(QStringLiteral("type")); | ||||
101 | if (typeValue.contains(QLatin1Char(','))) { | ||||
102 | // multiple mimetypes were passed | ||||
103 | query = query | Type(typeValue.split(QLatin1Char(','))); | ||||
104 | } else { | ||||
105 | query = query | Type(typeValue); | ||||
106 | } | ||||
107 | } | ||||
108 | | ||||
109 | // limit parameter | ||||
110 | if (urlQuery.hasQueryItem(QStringLiteral("limit"))) { | ||||
111 | auto limitValue = urlQuery.queryItemValue(QStringLiteral("limit")); | ||||
112 | bool parseOk; | ||||
113 | auto limitInt = limitValue.toInt(&parseOk); | ||||
114 | if (parseOk) { | ||||
115 | query = query | Limit(limitInt); | ||||
116 | } | ||||
117 | } | ||||
118 | | ||||
119 | // url parameter for exact path match or folders, supports wildcard pattern matching | ||||
120 | if (urlQuery.hasQueryItem(QStringLiteral("url"))) { | ||||
121 | query = query | Url(urlQuery.queryItemValue(QStringLiteral("url"))); | ||||
122 | } else { | ||||
123 | query = query | Url::file(); | ||||
124 | } | ||||
125 | | ||||
126 | qDebug(KACTIVITY_STAT_IOSLAVE_LOG) << "running query" << query; | ||||
127 | | ||||
128 | return new ResultModel(query); | ||||
129 | } | ||||
130 | | ||||
131 | KIO::UDSEntry RecentDocumentActivities::udsEntryFromResource(const QString& resource){ | ||||
132 | | ||||
133 | // the query only returns files and folders | ||||
134 | QUrl resourceUrl = QUrl::fromLocalFile(resource); | ||||
135 | | ||||
136 | KIO::UDSEntry uds; | ||||
137 | KIO::StatJob* job = KIO::stat(resourceUrl, KIO::HideProgressInfo); | ||||
138 | | ||||
139 | // we do not want to wait for the event loop to delete the job | ||||
140 | QScopedPointer<KIO::StatJob> sp(job); | ||||
141 | job->setAutoDelete(false); | ||||
142 | if (job->exec()) { | ||||
143 | uds = job->statResult(); | ||||
144 | } | ||||
145 | uds.fastInsert(KIO::UDSEntry::UDS_URL, resourceUrl.toString()); | ||||
146 | return uds; | ||||
147 | } | ||||
148 | | ||||
149 | void RecentDocumentActivities::listDir(const QUrl& url) | ||||
150 | { | ||||
151 | if (!isRootUrl(url)) { | ||||
152 | error(KIO::ERR_DOES_NOT_EXIST, url.toDisplayString()); | ||||
153 | return; | ||||
154 | } | ||||
155 | | ||||
156 | auto model = runQuery(url); | ||||
157 | | ||||
158 | KIO::UDSEntryList udslist; | ||||
159 | udslist.reserve(model->rowCount()); | ||||
160 | | ||||
161 | for(int r = 0; r < model->rowCount(); ++r) { | ||||
162 | QModelIndex index = model->index(r, 0); | ||||
163 | QString resource = model->data(index, ResultModel::ResourceRole).toString(); | ||||
164 | | ||||
165 | udslist << udsEntryFromResource(resource); | ||||
166 | } | ||||
167 | | ||||
168 | listEntries(udslist); | ||||
169 | finished(); | ||||
170 | } | ||||
171 | | ||||
172 | void RecentDocumentActivities::stat(const QUrl& url) | ||||
173 | { | ||||
174 | qDebug(KACTIVITY_STAT_IOSLAVE_LOG) << "stating" << " " << url; | ||||
175 | | ||||
176 | if (isRootUrl(url)) { | ||||
177 | // | ||||
178 | // stat the root path | ||||
179 | // | ||||
180 | | ||||
181 | QString dirName = i18n("Recent Documents"); | ||||
182 | KIO::UDSEntry uds; | ||||
183 | uds.reserve(5); | ||||
184 | uds.fastInsert(KIO::UDSEntry::UDS_NAME, dirName); | ||||
185 | uds.fastInsert(KIO::UDSEntry::UDS_DISPLAY_NAME, dirName); | ||||
186 | uds.fastInsert(KIO::UDSEntry::UDS_DISPLAY_TYPE, dirName); | ||||
187 | uds.fastInsert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("document-open-recent")); | ||||
188 | uds.fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); | ||||
189 | uds.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory")); | ||||
190 | | ||||
191 | statEntry(uds); | ||||
192 | finished(); | ||||
193 | } else { | ||||
194 | auto model = runQuery(url); | ||||
195 | if (model->rowCount() != 1) { | ||||
196 | error(KIO::ERR_DOES_NOT_EXIST, url.toDisplayString()); | ||||
197 | return ; | ||||
198 | } | ||||
199 | | ||||
200 | QModelIndex index = model->index(0, 0); | ||||
201 | QString resource = model->data(index, ResultModel::ResourceRole).toString(); | ||||
202 | statEntry(udsEntryFromResource(resource)); | ||||
203 | | ||||
204 | finished(); | ||||
205 | } | ||||
206 | } | ||||
207 | | ||||
208 | void RecentDocumentActivities::mimetype(const QUrl& url) | ||||
209 | { | ||||
210 | // the root url is always a folder | ||||
211 | if (isRootUrl(url)) { | ||||
212 | mimeType(QString::fromLatin1("inode/directory")); | ||||
213 | finished(); | ||||
214 | } | ||||
215 | // results are forwarded | ||||
216 | else { | ||||
217 | ForwardingSlaveBase::mimetype(url); | ||||
218 | } | ||||
219 | } |