MauiKit File Browsing

downloader.cpp
1#include "downloader.h"
2#include <QNetworkAccessManager>
3#include <QNetworkReply>
4#include <QFile>
5
6// #if defined Q_OS_LINUX && !defined Q_OS_ANDROID
7// #include <KIO/CopyJob>
8// #include <KJob>
9// #endif
10
11// QNetworkAccessManager *FMH::Downloader::manager = new QNetworkAccessManager();
12
13FMH::Downloader::Downloader(QObject *parent)
14: QObject(parent)
15, manager( new QNetworkAccessManager(this))
16, reply(nullptr)
17, file(nullptr)
18, array(new QByteArray)
19{
20}
21
22void FMH::Downloader::setConnections()
23{
24 if(!reply)
25 {
26 return;
27 }
28
29 reply->disconnect();
30 connect(reply, &QNetworkReply::downloadProgress, this, &Downloader::onDownloadProgress);
31 connect(reply, &QIODevice::readyRead, this, &Downloader::onReadyRead);
32 connect(reply, &QNetworkReply::finished, this, &Downloader::onReplyFinished);
34 {
35 Q_EMIT this->warning(reply->errorString());
36 });
37
38 // connect(reply, &QNetworkReply::sslErrors, [this](const QList<QSslError> &errors)
39 // {
40 // Q_EMIT this->warning(reply->sslErrors().);
41 // });
42}
43
44FMH::Downloader::~Downloader()
45{
46 qDebug() << "DELETEING DOWNLOADER";
47 this->array->clear();
48}
49
51{
52 if(!reply)
53 {
54 return false;
55 }
56
57 return reply->isFinished();
58}
59
61{
62 if(!reply)
63 {
64 return false;
65 }
66
67 return reply->isRunning();
68}
69
70
72{
73 if(!reply)
74 {
75 return;
76 }
77
78 if(this->reply->isRunning())
79 {
80 this->reply->abort();
81 this->reply->close();
82 Q_EMIT this->aborted();
83
84 if(m_saveToFile)
85 {
86 if(this->file)
87 {
88 this->file->remove();
89 }
90 }else
91 {
92 this->array->clear();
93 }
94 }
95}
96
97void FMH::Downloader::downloadFile(const QUrl &source, const QUrl &destination)
98{
99 #ifdef KIO_COPYJOB_H
100 KIO::CopyJob *downloadJob = KIO::copy(source, destination);
101
102 connect(downloadJob, &KIO::CopyJob::warning, [=](KJob *job, QString message)
103 {
104 Q_UNUSED(job)
105 Q_EMIT this->warning(message);
106 });
107
108 connect(downloadJob, &KIO::CopyJob::processedSize, [=](KJob *job, qulonglong size)
109 {
110 Q_EMIT this->progress(size, job->percent());
111 });
112
113 connect(downloadJob, &KIO::CopyJob::finished, [=](KJob *job)
114 {
115 Q_EMIT this->downloadReady();
116 Q_EMIT this->done();
117 });
118
119 #else
120 if (destination.isEmpty() || source.isEmpty())
121 return;
122
123 QNetworkRequest request;
125 request.setUrl(source);
126
127 m_saveToFile = true;
128
129 file = new QFile;
130 file->setFileName(destination.toLocalFile());
131 if (!file->open(QIODevice::WriteOnly))
132 {
133 Q_EMIT this->warning(QStringLiteral("Can not open file to write download"));
134 return;
135 }
136
137 this->reply = manager->get(request);
138 this->setConnections();
139
140 #endif
141}
142
143void FMH::Downloader::getArray(const QUrl &fileURL, const QMap<QString, QString> &headers)
144{
145 if (fileURL.isEmpty())
146 return;
147
148 QNetworkRequest request;
149 request.setUrl(fileURL);
150 if (!headers.isEmpty())
151 {
152 const auto keys = headers.keys();
153 for (const auto &key : keys)
154 request.setRawHeader(key.toLocal8Bit(), headers[key].toLocal8Bit());
155 }
156
157 m_saveToFile = false;
158 reply = manager->get(request);
159 this->setConnections();
160}
161
162void FMH::Downloader::onDownloadProgress(qint64 bytesRead, qint64 bytesTotal)
163{
164 if(bytesTotal <= 0)
165 {
166 return;
167 }
168
169 qDebug() << "DOWNLOAD PROGRESS" << ((bytesRead * 100) / bytesTotal);
170 Q_EMIT this->progress((bytesRead * 100) / bytesTotal);
171}
172
173void FMH::Downloader::onReadyRead()
174{
175 switch (reply->error())
176 {
178 {
179 if(m_saveToFile)
180 {
181 this->file->write(reply->readAll());
182
183 }else
184 {
185 this->array->append(reply->readAll());
186 }
187
188 break;
189 }
190
191 default:
192 {
193 qDebug() << reply->errorString();
194 Q_EMIT this->warning(reply->errorString());
195 }
196 }
197}
198
199void FMH::Downloader::onReplyFinished()
200{
201 switch (reply->error())
202 {
204 {
205 if(m_saveToFile)
206 {
207 if (file->isOpen())
208 {
209 file->close();
210 }
211
212 Q_EMIT this->fileSaved(this->file->fileName());
213 file->deleteLater();
214
215 }else
216 {
217 Q_EMIT this->dataReady(*this->array);
218 }
219
220 Q_EMIT this->done();
221 Q_EMIT this->downloadReady();
222 break;
223 }
224
225 default:
226 {
227 Q_EMIT this->warning(reply->errorString());
228 }
229 }
230}
void stop()
Force to stop the process of the data request or the downloading of the file.
void downloadFile(const QUrl &source, const QUrl &destination)
Given a source URL to a file it downloads it to a given local destination.
void getArray(const QUrl &fileURL, const QMap< QString, QString > &headers={})
Given a URL make a get request and once the reply is ready emits a signal with the retrieved array da...
void warning(QString warning)
Emitted when there is a warning message, for example when an error has occurred during the process or...
bool isRunning() const
Whether the downloading or current request is still in progress.
bool isFinished() const
Whether the process has finished successfully.
unsigned long percent() const
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void setFileName(const QString &name)
QString errorString() const const
void readyRead()
bool isEmpty() const const
QList< Key > keys() const const
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void errorOccurred(QNetworkReply::NetworkError code)
void setAttribute(Attribute code, const QVariant &value)
void setRawHeader(const QByteArray &headerName, const QByteArray &headerValue)
void setUrl(const QUrl &url)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
bool isEmpty() const const
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.