File indexing completed on 2024-05-12 05:04:13

0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.1-or-later
0003 
0004 #include "networkrequestprogress.h"
0005 
0006 NetworkRequestProgress::NetworkRequestProgress(QObject *parent)
0007     : QObject(parent)
0008 {
0009 }
0010 
0011 QNetworkReply *NetworkRequestProgress::reply() const
0012 {
0013     return m_reply;
0014 }
0015 
0016 void NetworkRequestProgress::setReply(QNetworkReply *reply)
0017 {
0018     if (reply == m_reply) {
0019         return;
0020     }
0021     m_reply = reply;
0022     Q_EMIT replyChanged();
0023 
0024     if (m_reply) {
0025         connect(reply, &QNetworkReply::uploadProgress, this, [this](qint64 bytesSent, qint64 bytesTotal) {
0026             const int progress = static_cast<double>(bytesSent) / static_cast<double>(bytesTotal) * 100.0;
0027             if (bytesTotal != 0) {
0028                 setProgress(progress);
0029             } else {
0030                 Q_EMIT progressChanged();
0031             }
0032         });
0033         connect(reply, &QNetworkReply::finished, this, [this] {
0034             m_reply = nullptr;
0035             Q_EMIT progressChanged();
0036         });
0037         setProgress(reply->isFinished() ? 100 : 0);
0038     }
0039 }
0040 
0041 bool NetworkRequestProgress::uploading() const
0042 {
0043     return m_reply && !m_reply->isFinished();
0044 }
0045 
0046 int NetworkRequestProgress::progress() const
0047 {
0048     return m_progress;
0049 }
0050 void NetworkRequestProgress::setProgress(int progress)
0051 {
0052     if (m_progress == progress) {
0053         return;
0054     }
0055     m_progress = progress;
0056     Q_EMIT progressChanged();
0057 }
0058 
0059 #include "moc_networkrequestprogress.cpp"