aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/solutions/tasking/networkquery.cpp
blob: b02bb9cea1c571231d7801e9feafdb3de4738cfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "networkquery.h"

#include <QNetworkAccessManager>

namespace Tasking {

void NetworkQuery::start()
{
    if (m_reply) {
        qWarning("The NetworkQuery is already running. Ignoring the call to start().");
        return;
    }
    if (!m_manager) {
        qWarning("Can't start the NetworkQuery without the QNetworkAccessManager. "
                 "Stopping with an error.");
        emit done(DoneResult::Error);
        return;
    }
    m_reply.reset(m_manager->get(m_request));
    connect(m_reply.get(), &QNetworkReply::finished, this, [this] {
        disconnect(m_reply.get(), &QNetworkReply::finished, this, nullptr);
        emit done(toDoneResult(m_reply->error() == QNetworkReply::NoError));
        m_reply.release()->deleteLater();
    });
    if (m_reply->isRunning())
        emit started();
}

NetworkQuery::~NetworkQuery()
{
    if (m_reply) {
        disconnect(m_reply.get(), &QNetworkReply::finished, this, nullptr);
        m_reply->abort();
    }
}

} // namespace Tasking