File indexing completed on 2024-05-12 04:42:39

0001 /*
0002     SPDX-FileCopyrightText: 2024 Jonah BrĂ¼chert <jbb@kaidan.im>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 #include <QNetworkReply>
0011 
0012 /// Tracks completion and errors of a number of QNetworkReplies
0013 class NetworkReplyCollection : public QObject {
0014     Q_OBJECT
0015 
0016 public:
0017     NetworkReplyCollection(std::vector<QNetworkReply *> replies, QObject *parent = nullptr);
0018 
0019     /// Emitted when all tracked replies finished
0020     Q_SIGNAL void allFinished();
0021 
0022     /// Emitted every time one of the replies has an error
0023     Q_SIGNAL void errorOccured(QNetworkReply *, QNetworkReply::NetworkError error);
0024 
0025     std::vector<QNetworkReply *> replies() {
0026         return m_replies;
0027     }
0028 
0029 private:
0030     void checkFinished() {
0031         bool finished = std::all_of(m_replies.begin(), m_replies.end(), [](QNetworkReply *reply) {
0032             return reply->isFinished();
0033         });
0034         if (finished) {
0035             Q_EMIT allFinished();
0036         }
0037     }
0038 
0039     std::vector<QNetworkReply *> m_replies;
0040 };