| 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 "mimetypejob.h" |
| 10 | #include "job_p.h" |
| 11 | |
| 12 | using namespace KIO; |
| 13 | |
| 14 | class KIO::MimetypeJobPrivate : public KIO::TransferJobPrivate |
| 15 | { |
| 16 | public: |
| 17 | MimetypeJobPrivate(const QUrl &url, int command, const QByteArray &packedArgs) |
| 18 | : TransferJobPrivate(url, command, packedArgs, QByteArray()) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | Q_DECLARE_PUBLIC(MimetypeJob) |
| 23 | |
| 24 | static inline MimetypeJob *newJob(const QUrl &url, int command, const QByteArray &packedArgs, JobFlags flags) |
| 25 | { |
| 26 | MimetypeJob *job = new MimetypeJob(*new MimetypeJobPrivate(url, command, packedArgs)); |
| 27 | job->setUiDelegate(KIO::createDefaultJobUiDelegate()); |
| 28 | if (!(flags & HideProgressInfo)) { |
| 29 | job->setFinishedNotificationHidden(); |
| 30 | KIO::getJobTracker()->registerJob(job); |
| 31 | emitStating(job, url); |
| 32 | } |
| 33 | return job; |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | MimetypeJob::MimetypeJob(MimetypeJobPrivate &dd) |
| 38 | : TransferJob(dd) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | MimetypeJob::~MimetypeJob() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void MimetypeJob::slotFinished() |
| 47 | { |
| 48 | Q_D(MimetypeJob); |
| 49 | // qDebug(); |
| 50 | if (error() == KIO::ERR_IS_DIRECTORY) { |
| 51 | // It is in fact a directory. This happens when HTTP redirects to FTP. |
| 52 | // Due to the "protocol doesn't support listing" code in KRun, we |
| 53 | // assumed it was a file. |
| 54 | // qDebug() << "It is in fact a directory!"; |
| 55 | d->m_mimetype = QStringLiteral("inode/directory" ); |
| 56 | Q_EMIT TransferJob::mimeTypeFound(job: this, mimeType: d->m_mimetype); |
| 57 | setError(0); |
| 58 | } |
| 59 | |
| 60 | if (!d->m_redirectionURL.isEmpty() && d->m_redirectionURL.isValid() && !error()) { |
| 61 | // qDebug() << "Redirection to " << m_redirectionURL; |
| 62 | if (queryMetaData(QStringLiteral("permanent-redirect" )) == QLatin1String("true" )) { |
| 63 | Q_EMIT permanentRedirection(job: this, fromUrl: d->m_url, toUrl: d->m_redirectionURL); |
| 64 | } |
| 65 | |
| 66 | if (d->m_redirectionHandlingEnabled) { |
| 67 | d->staticData.truncate(pos: 0); |
| 68 | d->m_internalSuspended = false; |
| 69 | d->m_packedArgs.truncate(pos: 0); |
| 70 | QDataStream stream(&d->m_packedArgs, QIODevice::WriteOnly); |
| 71 | stream << d->m_redirectionURL; |
| 72 | |
| 73 | d->restartAfterRedirection(redirectionUrl: &d->m_redirectionURL); |
| 74 | return; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Return worker to the scheduler |
| 79 | TransferJob::slotFinished(); |
| 80 | } |
| 81 | |
| 82 | MimetypeJob *KIO::mimetype(const QUrl &url, JobFlags flags) |
| 83 | { |
| 84 | KIO_ARGS << url; |
| 85 | return MimetypeJobPrivate::newJob(url, command: CMD_MIMETYPE, packedArgs, flags); |
| 86 | } |
| 87 | |
| 88 | #include "moc_mimetypejob.cpp" |
| 89 | |