| 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 "mkdirjob.h" |
| 10 | #include "job.h" |
| 11 | #include "job_p.h" |
| 12 | #include "kiocoredebug.h" |
| 13 | #include <kurlauthorized.h> |
| 14 | #include <worker_p.h> |
| 15 | |
| 16 | using namespace KIO; |
| 17 | |
| 18 | class KIO::MkdirJobPrivate : public SimpleJobPrivate |
| 19 | { |
| 20 | public: |
| 21 | MkdirJobPrivate(const QUrl &url, int command, const QByteArray &packedArgs) |
| 22 | : SimpleJobPrivate(url, command, packedArgs) |
| 23 | { |
| 24 | } |
| 25 | QUrl m_redirectionURL; |
| 26 | void slotRedirection(const QUrl &url); |
| 27 | |
| 28 | /*! |
| 29 | * \internal |
| 30 | * Called by the scheduler when a @p worker gets to |
| 31 | * work on this job. |
| 32 | * \a worker the worker that starts working on this job |
| 33 | */ |
| 34 | void start(Worker *worker) override; |
| 35 | |
| 36 | Q_DECLARE_PUBLIC(MkdirJob) |
| 37 | |
| 38 | static inline MkdirJob *newJob(const QUrl &url, int command, const QByteArray &packedArgs) |
| 39 | { |
| 40 | MkdirJob *job = new MkdirJob(*new MkdirJobPrivate(url, command, packedArgs)); |
| 41 | job->setUiDelegate(KIO::createDefaultJobUiDelegate()); |
| 42 | return job; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | MkdirJob::MkdirJob(MkdirJobPrivate &dd) |
| 47 | : SimpleJob(dd) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | MkdirJob::~MkdirJob() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void MkdirJobPrivate::start(Worker *worker) |
| 56 | { |
| 57 | Q_Q(MkdirJob); |
| 58 | q->connect(sender: worker, signal: &KIO::WorkerInterface::redirection, context: q, slot: [this](const QUrl &url) { |
| 59 | slotRedirection(url); |
| 60 | }); |
| 61 | |
| 62 | SimpleJobPrivate::start(worker); |
| 63 | } |
| 64 | |
| 65 | // Worker got a redirection request |
| 66 | void MkdirJobPrivate::slotRedirection(const QUrl &url) |
| 67 | { |
| 68 | Q_Q(MkdirJob); |
| 69 | // qDebug() << url; |
| 70 | if (!KUrlAuthorized::authorizeUrlAction(QStringLiteral("redirect" ), baseUrl: m_url, destUrl: url)) { |
| 71 | qCWarning(KIO_CORE) << "Redirection from" << m_url << "to" << url << "REJECTED!" ; |
| 72 | q->setError(ERR_ACCESS_DENIED); |
| 73 | q->setErrorText(url.toDisplayString()); |
| 74 | return; |
| 75 | } |
| 76 | m_redirectionURL = url; // We'll remember that when the job finishes |
| 77 | // Tell the user that we haven't finished yet |
| 78 | Q_EMIT q->redirection(job: q, url: m_redirectionURL); |
| 79 | } |
| 80 | |
| 81 | void MkdirJob::slotFinished() |
| 82 | { |
| 83 | Q_D(MkdirJob); |
| 84 | |
| 85 | if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid()) { |
| 86 | // qDebug() << "MkdirJob: Redirection to " << m_redirectionURL; |
| 87 | if (queryMetaData(QStringLiteral("permanent-redirect" )) == QLatin1String("true" )) { |
| 88 | Q_EMIT permanentRedirection(job: this, fromUrl: d->m_url, toUrl: d->m_redirectionURL); |
| 89 | } |
| 90 | |
| 91 | if (d->m_redirectionHandlingEnabled) { |
| 92 | QUrl dummyUrl; |
| 93 | int permissions; |
| 94 | QDataStream istream(d->m_packedArgs); |
| 95 | istream >> dummyUrl >> permissions; |
| 96 | |
| 97 | d->m_packedArgs.truncate(pos: 0); |
| 98 | QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly); |
| 99 | stream << d->m_redirectionURL << permissions; |
| 100 | |
| 101 | d->restartAfterRedirection(redirectionUrl: &d->m_redirectionURL); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Return worker to the scheduler |
| 107 | SimpleJob::slotFinished(); |
| 108 | } |
| 109 | |
| 110 | KIO::MkdirJob *KIO::mkdir(const QUrl &url, int permissions) |
| 111 | { |
| 112 | // qDebug() << "mkdir " << url; |
| 113 | KIO_ARGS << url << permissions; |
| 114 | return MkdirJobPrivate::newJob(url, command: CMD_MKDIR, packedArgs); |
| 115 | } |
| 116 | |
| 117 | #include "moc_mkdirjob.cpp" |
| 118 | |