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