aboutsummaryrefslogtreecommitdiff
path: root/lib/networkaccessmanager.cpp
blob: 710ade4efa3ff77697dd3a4298c1cf0ea76f5318 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "networkaccessmanager.h"

#include <QtCore/QCoreApplication>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QThreadStorage>
#include "accountregistry.h"
#include "mxcreply.h"
#include "connection.h"

#include "room.h"

using namespace Quotient;

class NetworkAccessManager::Private {
public:
    QList<QSslError> ignoredSslErrors;
};

NetworkAccessManager::NetworkAccessManager(QObject* parent)
    : QNetworkAccessManager(parent), d(std::make_unique<Private>())
{}

QList<QSslError> NetworkAccessManager::ignoredSslErrors() const
{
    return d->ignoredSslErrors;
}

void NetworkAccessManager::addIgnoredSslError(const QSslError& error)
{
    d->ignoredSslErrors << error;
}

void NetworkAccessManager::clearIgnoredSslErrors()
{
    d->ignoredSslErrors.clear();
}

static NetworkAccessManager* createNam()
{
    auto nam = new NetworkAccessManager(QCoreApplication::instance());
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
    // See #109; in newer Qt, bearer management is deprecated altogether
    NetworkAccessManager::connect(nam,
        &QNetworkAccessManager::networkAccessibleChanged, [nam] {
            nam->setNetworkAccessible(QNetworkAccessManager::Accessible);
        });
#endif
    return nam;
}

NetworkAccessManager* NetworkAccessManager::instance()
{
    static QThreadStorage<NetworkAccessManager*> storage;
    if(!storage.hasLocalData()) {
        storage.setLocalData(createNam());
    }
    return storage.localData();
}

NetworkAccessManager::~NetworkAccessManager() = default;

QNetworkReply* NetworkAccessManager::createRequest(
    Operation op, const QNetworkRequest& request, QIODevice* outgoingData)
{
    if(request.url().scheme() == QStringLiteral("mxc")) {
        const auto fragment = request.url().fragment();
        const auto fragmentParts = fragment.split(QLatin1Char('/'));
        const auto mediaId = request.url().toString(QUrl::RemoveScheme | QUrl::RemoveFragment);
        if (fragmentParts.size() == 3) {
            auto connection = AccountRegistry::instance().get(fragmentParts[0]);
            if(!connection) {
                qWarning() << "Connection not found";
                return nullptr;
            }
            auto room = connection->room(fragmentParts[1]);
            if(!room) {
                qWarning() << "Room not found";
                return nullptr;
            }
            QNetworkRequest r(request);
            r.setUrl(QUrl(QStringLiteral("%1/_matrix/media/r0/download/%2").arg(connection->homeserver().toString(), mediaId)));
            auto reply = createRequest(QNetworkAccessManager::GetOperation, r);
            return new MxcReply(reply, room, fragmentParts[2]);
        } else if(fragmentParts.size() == 1) {
            auto connection = AccountRegistry::instance().get(fragment);
            if(!connection) {
                qWarning() << "Connection not found";
                return nullptr;
            }
            QNetworkRequest r(request);
            r.setUrl(QUrl(QStringLiteral("%1/_matrix/media/r0/download/%2").arg(connection->homeserver().toString(), mediaId)));
            auto reply = createRequest(QNetworkAccessManager::GetOperation, r);
            return new MxcReply(reply);
        } else {
            qWarning() << "Invalid request";
            return nullptr;
        }
    }
    auto reply = QNetworkAccessManager::createRequest(op, request, outgoingData);
    reply->ignoreSslErrors(d->ignoredSslErrors);
    return reply;
}

QStringList NetworkAccessManager::supportedSchemesImplementation() const
{
    auto schemes = QNetworkAccessManager::supportedSchemesImplementation();
    schemes += QStringLiteral("mxc");
    return schemes;
}

QUrl NetworkAccessManager::urlForRoomEvent(Room *room, const QString &eventId, const QString &mediaId)
{
    return QUrl(QStringLiteral("mxc:%1#%2/%3/%4").arg(mediaId, room->connection()->userId(), room->id(), eventId));
}

QUrl NetworkAccessManager::urlForFile(Connection *connection, const QString &mediaId)
{
    return QUrl(QStringLiteral("mxc:%1#%2").arg(mediaId, connection->userId()));
}