KIO

statjob.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 "statjob.h"
10
11#include "job_p.h"
12#include "scheduler.h"
13#include "worker_p.h"
14#include <KProtocolInfo>
15#include <QTimer>
16#include <kurlauthorized.h>
17
18using namespace KIO;
19
20class KIO::StatJobPrivate : public SimpleJobPrivate
21{
22public:
23 inline StatJobPrivate(const QUrl &url, int command, const QByteArray &packedArgs)
24 : SimpleJobPrivate(url, command, packedArgs)
25 , m_bSource(true)
26 , m_details(KIO::StatDefaultDetails)
27 {
28 }
29
30 UDSEntry m_statResult;
31 QUrl m_redirectionURL;
32 bool m_bSource;
33 KIO::StatDetails m_details;
34 void slotStatEntry(const KIO::UDSEntry &entry);
35 void slotRedirection(const QUrl &url);
36
37 /**
38 * @internal
39 * Called by the scheduler when a @p worker gets to
40 * work on this job.
41 * @param worker the worker that starts working on this job
42 */
43 void start(Worker *worker) override;
44
45 Q_DECLARE_PUBLIC(StatJob)
46
47 static inline StatJob *newJob(const QUrl &url, int command, const QByteArray &packedArgs, JobFlags flags)
48 {
49 StatJob *job = new StatJob(*new StatJobPrivate(url, command, packedArgs));
51 if (!(flags & HideProgressInfo)) {
54 emitStating(job, url);
55 }
56 return job;
57 }
58};
59
60StatJob::StatJob(StatJobPrivate &dd)
61 : SimpleJob(dd)
62{
63 setTotalAmount(Items, 1);
64}
65
66StatJob::~StatJob()
67{
68}
69
70void StatJob::setSide(StatSide side)
71{
72 d_func()->m_bSource = side == SourceSide;
73}
74
76{
77 d_func()->m_details = details;
78}
79
81{
82 return d_func()->m_statResult;
83}
84
86{
87 const QUrl _url = url();
88
89 if (_url.isLocalFile()) {
90 return _url;
91 }
92
93 const UDSEntry &udsEntry = d_func()->m_statResult;
95
96 if (path.isEmpty()) { // Return url as-is
97 return _url;
98 }
99
100 const QString protoClass = KProtocolInfo::protocolClass(_url.scheme());
101 if (protoClass != QLatin1String(":local")) { // UDS_LOCAL_PATH was set but wrong Class
102 qCWarning(KIO_CORE) << "The protocol Class of the url that was being stat'ed" << _url << ", is" << protoClass
103 << ", however UDS_LOCAL_PATH was set; if you use UDS_LOCAL_PATH, the protocolClass"
104 " should be :local, see KProtocolInfo API docs for more details.";
105 return _url;
106 }
107
108 return QUrl::fromLocalFile(path);
109}
110
111void StatJobPrivate::start(Worker *worker)
112{
113 Q_Q(StatJob);
114 m_outgoingMetaData.insert(QStringLiteral("statSide"), m_bSource ? QStringLiteral("source") : QStringLiteral("dest"));
115 m_outgoingMetaData.insert(QStringLiteral("details"), QString::number(m_details));
116
117 q->connect(worker, &KIO::WorkerInterface::statEntry, q, [this](const KIO::UDSEntry &entry) {
118 slotStatEntry(entry);
119 });
120 q->connect(worker, &KIO::WorkerInterface::redirection, q, [this](const QUrl &url) {
121 slotRedirection(url);
122 });
123
124 SimpleJobPrivate::start(worker);
125}
126
127void StatJobPrivate::slotStatEntry(const KIO::UDSEntry &entry)
128{
129 // qCDebug(KIO_CORE);
130 m_statResult = entry;
131}
132
133// Worker got a redirection request
134void StatJobPrivate::slotRedirection(const QUrl &url)
135{
136 Q_Q(StatJob);
137 // qCDebug(KIO_CORE) << m_url << "->" << url;
138 if (!KUrlAuthorized::authorizeUrlAction(QStringLiteral("redirect"), m_url, url)) {
139 qCWarning(KIO_CORE) << "Redirection from" << m_url << "to" << url << "REJECTED!";
140 q->setError(ERR_ACCESS_DENIED);
141 q->setErrorText(url.toDisplayString());
142 return;
143 }
144 m_redirectionURL = url; // We'll remember that when the job finishes
145 // Tell the user that we haven't finished yet
146 Q_EMIT q->redirection(q, m_redirectionURL);
147}
148
149void StatJob::slotFinished()
150{
151 Q_D(StatJob);
152
153 if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid()) {
154 // qCDebug(KIO_CORE) << "StatJob: Redirection to " << m_redirectionURL;
155 if (queryMetaData(QStringLiteral("permanent-redirect")) == QLatin1String("true")) {
156 Q_EMIT permanentRedirection(this, d->m_url, d->m_redirectionURL);
157 }
158
159 if (d->m_redirectionHandlingEnabled) {
160 d->m_packedArgs.truncate(0);
161 QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly);
162 stream << d->m_redirectionURL;
163
164 d->restartAfterRedirection(&d->m_redirectionURL);
165 return;
166 }
167 }
168
169 // Return worker to the scheduler
171}
172
173static bool isUrlValid(const QUrl &url)
174{
175 if (!url.isValid()) {
176 qCWarning(KIO_CORE) << "Invalid url:" << url << ", cancelling job.";
177 return false;
178 }
179
180 if (url.isLocalFile()) {
181 qCWarning(KIO_CORE) << "Url" << url << "already represents a local file, cancelling job.";
182 return false;
183 }
184
185 if (KProtocolInfo::protocolClass(url.scheme()) != QLatin1String(":local")) {
186 qCWarning(KIO_CORE) << "Protocol Class of url" << url << ", isn't ':local', cancelling job.";
187 return false;
188 }
189
190 return true;
191}
192
194{
195 StatJob *job = stat(url, StatJob::SourceSide, KIO::StatDefaultDetails, flags);
196 if (!isUrlValid(url)) {
197 QTimer::singleShot(0, job, &StatJob::slotFinished);
198 Scheduler::cancelJob(job); // deletes the worker if not 0
199 }
200 return job;
201}
202
203StatJob *KIO::stat(const QUrl &url, JobFlags flags)
204{
205 // Assume SourceSide. Gets are more common than puts.
206 return stat(url, StatJob::SourceSide, KIO::StatDefaultDetails, flags);
207}
208
209StatJob *KIO::stat(const QUrl &url, KIO::StatJob::StatSide side, KIO::StatDetails details, JobFlags flags)
210{
211 // qCDebug(KIO_CORE) << "stat" << url;
212 KIO_ARGS << url;
213 StatJob *job = StatJobPrivate::newJob(url, CMD_STAT, packedArgs, flags);
214 job->setSide(side);
215 job->setDetails(details);
216 return job;
217}
218
219#include "moc_statjob.cpp"
QString queryMetaData(const QString &key)
Query meta data received from the worker.
Definition job.cpp:210
A simple job (one url and one command).
Definition simplejob.h:27
virtual void slotFinished()
Called when the worker marks the job as finished.
const QUrl & url() const
Returns the SimpleJob's URL.
Definition simplejob.cpp:70
A KIO job that retrieves information about a file or directory.
Definition statjob.h:26
void permanentRedirection(KIO::Job *job, const QUrl &fromUrl, const QUrl &toUrl)
Signals a permanent redirection.
void setDetails(KIO::StatDetails details)
Selects the level of details we want.
Definition statjob.cpp:75
const UDSEntry & statResult() const
Result of the stat operation.
Definition statjob.cpp:80
void setSide(StatSide side)
A stat() can have two meanings.
Definition statjob.cpp:70
QUrl mostLocalUrl() const
most local URL
Definition statjob.cpp:85
Universal Directory Service.
Definition udsentry.h:78
QString stringValue(uint field) const
Definition udsentry.cpp:365
@ UDS_LOCAL_PATH
A local file path if the KIO worker display files sitting on the local filesystem (but in another hie...
Definition udsentry.h:227
virtual void registerJob(KJob *job)
void setUiDelegate(KJobUiDelegate *delegate)
void setFinishedNotificationHidden(bool hide=true)
static QString protocolClass(const QString &protocol)
Returns the protocol class for the specified protocol.
Q_SCRIPTABLE Q_NOREPLY void start()
A namespace for KIO globals.
KIOCORE_EXPORT StatJob * stat(const QUrl &url, JobFlags flags=DefaultFlags)
Find all details for one file or directory.
Definition statjob.cpp:203
KIOCORE_EXPORT StatJob * mostLocalUrl(const QUrl &url, JobFlags flags=DefaultFlags)
Tries to map a local URL for the given URL, using a KIO job.
Definition statjob.cpp:193
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
Convenience method: use default factory, if there's one, to create a delegate and return it.
@ HideProgressInfo
Hide progress information dialog, i.e. don't show a GUI.
Definition job_base.h:251
@ StatDefaultDetails
Default StatDetail flag when creating a StatJob.
Definition global.h:271
KIOCORE_EXPORT KJobTrackerInterface * getJobTracker()
Returns the job tracker to be used by all KIO jobs (in which HideProgressInfo is not set)
bool authorizeUrlAction(const QString &action, const QUrl &baseURL, const QUrl &destURL)
Returns whether a certain URL related action is authorized.
Q_EMITQ_EMIT
bool isEmpty() const const
QString number(double n, char format, int precision)
QUrl fromLocalFile(const QString &localFile)
bool isLocalFile() const const
bool isValid() const const
QString scheme() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:18:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.