File: mxcreply.cpp

package info (click to toggle)
libquotient 0.9.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,596 kB
  • sloc: xml: 39,103; cpp: 25,274; sh: 97; makefile: 9
file content (85 lines) | stat: -rw-r--r-- 2,629 bytes parent folder | download
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
// SPDX-FileCopyrightText: Tobias Fella <fella@posteo.de>
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "mxcreply.h"

#include <QtCore/QBuffer>

#include "events/filesourceinfo.h"

using namespace Quotient;

class Q_DECL_HIDDEN MxcReply::Private
{
public:
    QNetworkReply* m_reply;
    QIODevice* m_device;
};

MxcReply::MxcReply(QNetworkReply* reply,
                   const EncryptedFileMetadata& fileMetadata)
    : d(makeImpl<Private>(reply, fileMetadata.isValid() ? nullptr : reply))
{
    reply->setParent(this);
    connect(d->m_reply, &QNetworkReply::finished, this, [this, fileMetadata] {
        setError(d->m_reply->error(), d->m_reply->errorString());

        if (d->m_reply->error() != NoError) {
            const QJsonDocument doc = QJsonDocument::fromJson(d->m_reply->readAll());
            QString errorString;
            if (doc.object().contains("error"_L1)) {
                errorString = doc["error"_L1].toString();
            } else {
                errorString = d->m_reply->errorString();
            }
            setErrorString(QStringLiteral("%1 (%2)").arg(errorString,
                                                         d->m_reply->url().toString()));
        } else if (fileMetadata.isValid()) {
            auto buffer = new QBuffer(this);
            buffer->setData(decryptFile(d->m_reply->readAll(), fileMetadata));
            buffer->open(ReadOnly);
            d->m_device = buffer;
        }
        setOpenMode(ReadOnly);
        emit finished();
    });
}

MxcReply::MxcReply()
    : d(ZeroImpl<Private>())
{
    static const auto BadRequestPhrase = tr("Bad Request");
    QMetaObject::invokeMethod(this, [this]() {
            setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 400);
            setAttribute(QNetworkRequest::HttpReasonPhraseAttribute,
                         BadRequestPhrase);
            setError(QNetworkReply::ProtocolInvalidOperationError,
                     BadRequestPhrase);
            setFinished(true);
            emit errorOccurred(QNetworkReply::ProtocolInvalidOperationError);
            emit finished();
        }, Qt::QueuedConnection);
}

qint64 MxcReply::readData(char *data, qint64 maxlen)
{
    if(d != nullptr && d->m_device != nullptr) {
        return d->m_device->read(data, maxlen);
    }
    return -1;
}

void MxcReply::abort()
{
    if(d != nullptr && d->m_reply != nullptr) {
        d->m_reply->abort();
    }
}

qint64 MxcReply::bytesAvailable() const
{
    if (d != nullptr && d->m_device != nullptr) {
        return d->m_device->bytesAvailable() + QNetworkReply::bytesAvailable();
    }
    return 0;
}