KIO

listjob.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
4 SPDX-FileCopyrightText: 2000-2009 David Faure <faure@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "listjob.h"
10#include "../utils_p.h"
11#include "job_p.h"
12#include "worker_p.h"
13#include <QTimer>
14#include <kurlauthorized.h>
15
16#include <QDebug>
17
18using namespace KIO;
19
20class KIO::ListJobPrivate : public KIO::SimpleJobPrivate
21{
22public:
23 ListJobPrivate(const QUrl &url, bool _recursive, const QString &prefix, const QString &displayPrefix, ListJob::ListFlags listFlags)
24 : SimpleJobPrivate(url, CMD_LISTDIR, QByteArray())
25 , recursive(_recursive)
26 , listFlags(listFlags)
27 , m_prefix(prefix)
28 , m_displayPrefix(displayPrefix)
29 , m_processedEntries(0)
30 {
31 }
32 bool recursive;
33 ListJob::ListFlags listFlags;
34 QString m_prefix;
35 QString m_displayPrefix;
36 unsigned long m_processedEntries;
37 QUrl m_redirectionURL;
38
39 /**
40 * @internal
41 * Called by the scheduler when a @p worker gets to
42 * work on this job.
43 * @param worker the worker that starts working on this job
44 */
45 void start(Worker *worker) override;
46
47 void slotListEntries(const KIO::UDSEntryList &list);
48 void slotRedirection(const QUrl &url);
49 void gotEntries(KIO::Job *subjob, const KIO::UDSEntryList &list);
50 void slotSubError(ListJob *job, ListJob *subJob);
51
52 Q_DECLARE_PUBLIC(ListJob)
53
54 static inline ListJob *
55 newJob(const QUrl &u, bool _recursive, const QString &prefix, const QString &displayPrefix, ListJob::ListFlags listFlags, JobFlags flags = HideProgressInfo)
56 {
57 ListJob *job = new ListJob(*new ListJobPrivate(u, _recursive, prefix, displayPrefix, listFlags));
59 if (!(flags & HideProgressInfo)) {
61 }
62 return job;
63 }
64 static inline ListJob *newJobNoUi(const QUrl &u, bool _recursive, const QString &prefix, const QString &displayPrefix, ListJob::ListFlags listFlags)
65 {
66 return new ListJob(*new ListJobPrivate(u, _recursive, prefix, displayPrefix, listFlags));
67 }
68};
69
70ListJob::ListJob(ListJobPrivate &dd)
71 : SimpleJob(dd)
72{
73 Q_D(ListJob);
74 // We couldn't set the args when calling the parent constructor,
75 // so do it now.
76 QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly);
77 stream << d->m_url;
78}
79
80ListJob::~ListJob()
81{
82}
83
84void ListJobPrivate::slotListEntries(const KIO::UDSEntryList &list)
85{
86 Q_Q(ListJob);
87
88 const bool includeHidden = listFlags.testFlag(ListJob::ListFlag::IncludeHidden);
89
90 // Emit progress info (takes care of emit processedSize and percent)
91 m_processedEntries += list.count();
92 slotProcessedSize(m_processedEntries);
93
94 if (recursive) {
97
98 for (; it != end; ++it) {
99 const UDSEntry &entry = *it;
100
101 QUrl itemURL;
102 const QString udsUrl = entry.stringValue(KIO::UDSEntry::UDS_URL);
103 QString filename;
104 if (!udsUrl.isEmpty()) {
105 itemURL = QUrl(udsUrl);
106 filename = itemURL.fileName();
107 } else { // no URL, use the name
108 itemURL = q->url();
109 filename = entry.stringValue(KIO::UDSEntry::UDS_NAME);
110 Q_ASSERT(!filename.isEmpty()); // we'll recurse forever otherwise :)
111 itemURL.setPath(Utils::concatPaths(itemURL.path(), filename));
112 }
113
114 if (entry.isDir() && !entry.isLink()) {
115 Q_ASSERT(!filename.isEmpty());
117 if (displayName.isEmpty()) {
118 displayName = filename;
119 }
120 // skip hidden dirs when listing if requested
121 if (filename != QLatin1String("..") && filename != QLatin1String(".") && (includeHidden || filename[0] != QLatin1Char('.'))) {
122 ListJob *job = ListJobPrivate::newJobNoUi(itemURL,
123 true /*recursive*/,
124 m_prefix + filename + QLatin1Char('/'),
125 m_displayPrefix + displayName + QLatin1Char('/'),
126 listFlags);
127 QObject::connect(job, &ListJob::entries, q, [this](KIO::Job *job, const KIO::UDSEntryList &list) {
128 gotEntries(job, list);
129 });
130 QObject::connect(job, &ListJob::subError, q, [this](KIO::ListJob *job, KIO::ListJob *ljob) {
131 slotSubError(job, ljob);
132 });
133
134 q->addSubjob(job);
135 }
136 }
137 }
138 }
139
140 // Not recursive, or top-level of recursive listing : return now (send . and .. as well)
141 // exclusion of hidden files also requires the full sweep, but the case for full-listing
142 // a single dir is probably common enough to justify the shortcut
143 if (m_prefix.isNull() && includeHidden) {
144 Q_EMIT q->entries(q, list);
145 } else {
146 UDSEntryList newlist = list;
147
148 auto removeFunc = [this, includeHidden](const UDSEntry &entry) {
149 const QString filename = entry.stringValue(KIO::UDSEntry::UDS_NAME);
150 // Avoid returning entries like subdir/. and subdir/.., but include . and .. for
151 // the toplevel dir, and skip hidden files/dirs if that was requested
152 const bool shouldEmit = (m_prefix.isNull() || (filename != QLatin1String("..") && filename != QLatin1String(".")))
153 && (includeHidden || (filename[0] != QLatin1Char('.')));
154 return !shouldEmit;
155 };
156 newlist.erase(std::remove_if(newlist.begin(), newlist.end(), removeFunc), newlist.end());
157
158 for (UDSEntry &newone : newlist) {
159 // Modify the name in the UDSEntry
160 const QString filename = newone.stringValue(KIO::UDSEntry::UDS_NAME);
162 if (displayName.isEmpty()) {
163 displayName = filename;
164 }
165
166 // ## Didn't find a way to use the iterator instead of re-doing a key lookup
167 newone.replace(KIO::UDSEntry::UDS_NAME, m_prefix + filename);
168 newone.replace(KIO::UDSEntry::UDS_DISPLAY_NAME, m_displayPrefix + displayName);
169 }
170
171 Q_EMIT q->entries(q, newlist);
172 }
173}
174
175void ListJobPrivate::gotEntries(KIO::Job *, const KIO::UDSEntryList &list)
176{
177 // Forward entries received by subjob - faking we received them ourselves
178 Q_Q(ListJob);
179 Q_EMIT q->entries(q, list);
180}
181
182void ListJobPrivate::slotSubError(KIO::ListJob * /*job*/, KIO::ListJob *subJob)
183{
184 Q_Q(ListJob);
185 Q_EMIT q->subError(q, subJob); // Let the signal of subError go up
186}
187
188void ListJob::slotResult(KJob *job)
189{
190 Q_D(ListJob);
191 if (job->error()) {
192 // If we can't list a subdir, the result is still ok
193 // This is why we override KCompositeJob::slotResult - to not set
194 // an error on parent job.
195 // Let's emit a signal about this though
196 Q_EMIT subError(this, static_cast<KIO::ListJob *>(job));
197 }
198 removeSubjob(job);
199 if (!hasSubjobs() && !d->m_worker) { // if the main directory listing is still running, it will emit result in SimpleJob::slotFinished()
200 emitResult();
201 }
202}
203
204void ListJobPrivate::slotRedirection(const QUrl &url)
205{
206 Q_Q(ListJob);
207 if (!KUrlAuthorized::authorizeUrlAction(QStringLiteral("redirect"), m_url, url)) {
208 qCWarning(KIO_CORE) << "Redirection from" << m_url << "to" << url << "REJECTED!";
209 return;
210 }
211 m_redirectionURL = url; // We'll remember that when the job finishes
212 Q_EMIT q->redirection(q, m_redirectionURL);
213}
214
215void ListJob::slotFinished()
216{
217 Q_D(ListJob);
218
219 if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid() && !error()) {
220 // qDebug() << "Redirection to " << d->m_redirectionURL;
221 if (queryMetaData(QStringLiteral("permanent-redirect")) == QLatin1String("true")) {
222 Q_EMIT permanentRedirection(this, d->m_url, d->m_redirectionURL);
223 }
224
225 if (d->m_redirectionHandlingEnabled) {
226 d->m_packedArgs.truncate(0);
227 QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly);
228 stream << d->m_redirectionURL;
229
230 d->restartAfterRedirection(&d->m_redirectionURL);
231 return;
232 }
233 }
234
235 // Return worker to the scheduler
237}
238
240{
241 return ListJobPrivate::newJob(url, false, QString(), QString(), listFlags, flags);
242}
243
245{
246 return ListJobPrivate::newJob(url, true, QString(), QString(), listFlags, flags);
247}
248
249void ListJob::setUnrestricted(bool unrestricted)
250{
251 Q_D(ListJob);
252 if (unrestricted) {
253 d->m_extraFlags |= JobPrivate::EF_ListJobUnrestricted;
254 } else {
255 d->m_extraFlags &= ~JobPrivate::EF_ListJobUnrestricted;
256 }
257}
258
259void ListJobPrivate::start(Worker *worker)
260{
261 Q_Q(ListJob);
262 if (!KUrlAuthorized::authorizeUrlAction(QStringLiteral("list"), m_url, m_url) && !(m_extraFlags & EF_ListJobUnrestricted)) {
263 q->setError(ERR_ACCESS_DENIED);
264 q->setErrorText(m_url.toDisplayString());
265 QTimer::singleShot(0, q, &ListJob::slotFinished);
266 return;
267 }
268 QObject::connect(worker, &Worker::listEntries, q, [this](const KIO::UDSEntryList &list) {
269 slotListEntries(list);
270 });
271
272 QObject::connect(worker, &Worker::totalSize, q, [this](KIO::filesize_t size) {
273 slotTotalSize(size);
274 });
275
276 QObject::connect(worker, &Worker::redirection, q, [this](const QUrl &url) {
277 slotRedirection(url);
278 });
279
280 SimpleJobPrivate::start(worker);
281}
282
284{
285 return d_func()->m_redirectionURL;
286}
287
288#include "moc_listjob.cpp"
bool hasSubjobs() const
The base class for all jobs.
Definition job_base.h:45
QString queryMetaData(const QString &key)
Query meta data received from the worker.
Definition job.cpp:210
bool removeSubjob(KJob *job) override
Mark a sub job as being done.
Definition job.cpp:80
A ListJob is allows you to get the get the content of a directory.
Definition listjob.h:28
const QUrl & redirectionUrl() const
Returns the ListJob's redirection URL.
Definition listjob.cpp:283
@ IncludeHidden
Include hidden files in the listing.
void entries(KIO::Job *job, const KIO::UDSEntryList &list)
This signal emits the entry found by the job while listing.
void permanentRedirection(KIO::Job *job, const QUrl &fromUrl, const QUrl &toUrl)
Signals a permanent redirection.
void subError(KIO::ListJob *job, KIO::ListJob *subJob)
This signal is emitted when a sub-directory could not be listed.
void setUnrestricted(bool unrestricted)
Do not apply any KIOSK restrictions to this job.
Definition listjob.cpp:249
A simple job (one url and one command).
Definition simplejob.h:27
virtual void slotFinished()
Called when the worker marks the job as finished.
Universal Directory Service.
Definition udsentry.h:78
QString stringValue(uint field) const
Definition udsentry.cpp:365
bool isLink() const
Definition udsentry.cpp:380
@ UDS_URL
An alternative URL (If different from the caption).
Definition udsentry.h:251
@ UDS_DISPLAY_NAME
If set, contains the label to display instead of the 'real name' in UDS_NAME.
Definition udsentry.h:272
@ UDS_NAME
Filename - as displayed in directory listings etc.
Definition udsentry.h:224
bool isDir() const
Definition udsentry.cpp:375
virtual void registerJob(KJob *job)
void emitResult()
int error() const
void setUiDelegate(KJobUiDelegate *delegate)
Q_SCRIPTABLE Q_NOREPLY void start()
AKONADI_CALENDAR_EXPORT QString displayName(Akonadi::ETMCalendar *calendar, const Akonadi::Collection &collection)
A namespace for KIO globals.
KIOCORE_EXPORT ListJob * listDir(const QUrl &url, JobFlags flags=DefaultFlags, ListJob::ListFlags listFlags=ListJob::ListFlag::IncludeHidden)
List the contents of url, which is assumed to be a directory.
Definition listjob.cpp:239
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there's one, to create a delegate and return it.
KIOCORE_EXPORT ListJob * listRecursive(const QUrl &url, JobFlags flags=DefaultFlags, ListJob::ListFlags listFlags=ListJob::ListFlag::IncludeHidden)
The same as the previous method, but recurses subdirectories.
Definition listjob.cpp:244
@ HideProgressInfo
Hide progress information dialog, i.e. don't show a GUI.
Definition job_base.h:251
qulonglong filesize_t
64-bit file size
Definition global.h:35
KIOCORE_EXPORT KJobTrackerInterface * getJobTracker()
Returns the job tracker to be used by all KIO jobs (in which HideProgressInfo is not set)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
const QList< QKeySequence > & end()
bool authorizeUrlAction(const QString &action, const QUrl &baseURL, const QUrl &destURL)
Returns whether a certain URL related action is authorized.
bool testFlag(Enum flag) const const
iterator begin()
qsizetype count() const const
iterator end()
iterator erase(const_iterator begin, const_iterator end)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
bool isNull() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString fileName(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
void setPath(const QString &path, ParsingMode mode)
QString url(FormattingOptions options) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:13:15 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.