File indexing completed on 2024-05-12 05:22:20

0001 /*
0002  * This file is part of LibKGAPI library
0003  *
0004  * SPDX-FileCopyrightText: 2013 Daniel Vrátil <dvratil@redhat.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #include "filefetchcontentjob.h"
0010 #include "file.h"
0011 
0012 #include <QNetworkReply>
0013 #include <QNetworkRequest>
0014 
0015 using namespace KGAPI2;
0016 using namespace KGAPI2::Drive;
0017 
0018 class Q_DECL_HIDDEN FileFetchContentJob::Private
0019 {
0020 public:
0021     Private(FileFetchContentJob *parent);
0022 
0023     void _k_downloadProgress(qint64 downloaded, qint64 total);
0024 
0025     QUrl url;
0026     QByteArray fileData;
0027 
0028 private:
0029     FileFetchContentJob *const q;
0030 };
0031 
0032 FileFetchContentJob::Private::Private(FileFetchContentJob *parent)
0033     : q(parent)
0034 {
0035 }
0036 
0037 void FileFetchContentJob::Private::_k_downloadProgress(qint64 downloaded, qint64 total)
0038 {
0039     q->emitProgress(downloaded, total);
0040 }
0041 
0042 FileFetchContentJob::FileFetchContentJob(const FilePtr &file, const AccountPtr &account, QObject *parent)
0043     : FetchJob(account, parent)
0044     , d(new Private(this))
0045 {
0046     d->url = file->downloadUrl();
0047 }
0048 
0049 FileFetchContentJob::FileFetchContentJob(const QUrl &url, const AccountPtr &account, QObject *parent)
0050     : FetchJob(account, parent)
0051     , d(new Private(this))
0052 {
0053     d->url = url;
0054 }
0055 
0056 FileFetchContentJob::~FileFetchContentJob()
0057 {
0058     delete d;
0059 }
0060 
0061 QByteArray FileFetchContentJob::data() const
0062 {
0063     return d->fileData;
0064 }
0065 
0066 void FileFetchContentJob::start()
0067 {
0068     QNetworkRequest request(d->url);
0069     enqueueRequest(request);
0070 }
0071 
0072 void FileFetchContentJob::dispatchRequest(QNetworkAccessManager *accessManager,
0073                                           const QNetworkRequest &request,
0074                                           const QByteArray &data,
0075                                           const QString &contentType)
0076 {
0077     Q_UNUSED(data)
0078     Q_UNUSED(contentType)
0079 
0080     QNetworkReply *reply = accessManager->get(request);
0081     connect(reply, &QNetworkReply::downloadProgress, this, [this](qint64 downloaded, qint64 total) {
0082         d->_k_downloadProgress(downloaded, total);
0083     });
0084 }
0085 
0086 void FileFetchContentJob::handleReply(const QNetworkReply *reply, const QByteArray &rawData)
0087 {
0088     Q_UNUSED(reply)
0089 
0090     d->fileData = rawData;
0091 }
0092 
0093 ObjectsList FileFetchContentJob::handleReplyWithItems(const QNetworkReply *reply, const QByteArray &rawData)
0094 {
0095     Q_UNUSED(reply)
0096     Q_UNUSED(rawData)
0097 
0098     return ObjectsList();
0099 }
0100 
0101 #include "moc_filefetchcontentjob.cpp"