aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cpaster/dpastedotcomprotocol.cpp
blob: c5927c58f33d28cf0902b2d4dba8c2e6ec83cac9 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "dpastedotcomprotocol.h"

#include <coreplugin/messagemanager.h>

#include <QNetworkReply>
#include <QUrl>

namespace CodePaster {

static QString baseUrl() { return QString("http://dpaste.com"); }
static QString apiUrl() { return baseUrl() + "/api/v2/"; }

QString DPasteDotComProtocol::protocolName() { return QString("DPaste.Com"); }

unsigned DPasteDotComProtocol::capabilities() const
{
    return PostDescriptionCapability | PostUserNameCapability;
}

void DPasteDotComProtocol::fetch(const QString &id)
{
    QNetworkReply * const reply = httpGet(baseUrl() + '/' + id + ".txt");
    connect(reply, &QNetworkReply::finished, this, [this, id, reply] {
        fetchFinished(id, reply, false);
    });
}

void DPasteDotComProtocol::fetchFinished(const QString &id, QNetworkReply * const reply,
                                         bool alreadyRedirected)
{
    const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    if (status >= 300 && status <= 308 && status != 306) {
        if (!alreadyRedirected) {
            const QString location = QString::fromUtf8(reply->rawHeader("Location"));
            if (status == 301 || status == 308) {
                const QString m = QString("HTTP redirect (%1) to \"%2\"").arg(status).arg(location);
                Core::MessageManager::write(m, Core::MessageManager::ModeSwitch);
            }
            QNetworkReply * const newRep = httpGet(location);
            connect(newRep, &QNetworkReply::finished, this, [this, id, newRep] {
                fetchFinished(id, newRep, true);
            });
            reply->deleteLater();
            return;
        }
    }
    QString title;
    QString content;
    const bool error = reply->error();
    if (error) {
        content = reply->errorString();
    } else {
        title = name() + ": " + id;
        content = QString::fromUtf8(reply->readAll());
    }
    reply->deleteLater();
    emit fetchDone(title, content, error);
}

static QByteArray typeToString(Protocol::ContentType type)
{
    switch (type) {
    case Protocol::C:
        return "c";
    case Protocol::Cpp:
        return "cpp";
    case Protocol::Diff:
        return "diff";
    case Protocol::JavaScript:
        return "js";
    case Protocol::Text:
        return "text";
    case Protocol::Xml:
        return "xml";
    }
    return {}; // For dumb compilers.
}

void DPasteDotComProtocol::paste(
        const QString &text,
        ContentType ct,
        int expiryDays,
        bool publicPaste,
        const QString &username,
        const QString &comment,
        const QString &description
        )
{
    Q_UNUSED(publicPaste)
    Q_UNUSED(comment)

    // See http://dpaste.com/api/v2/
    QByteArray data;
    data += "content=" + QUrl::toPercentEncoding(fixNewLines(text));
    data += "&expiry_days=" + QByteArray::number(expiryDays);
    data += "&syntax=" + typeToString(ct);
    data += "&title=" + QUrl::toPercentEncoding(description);
    data += "&poster=" + QUrl::toPercentEncoding(username);

    QNetworkReply * const reply = httpPost(apiUrl(), data);
    connect(reply, &QNetworkReply::finished, this, [this, reply] {
        QString data;
        if (reply->error()) {
            reportError(reply->errorString()); // FIXME: Why can't we properly emit an error here?
            reportError(QString::fromUtf8(reply->readAll()));
        } else {
            data = QString::fromUtf8(reply->readAll());
            if (!data.startsWith(baseUrl())) {
                reportError(data);
                data.clear();
            }
        }
        reply->deleteLater();
        emit pasteDone(data);
    });
}

bool DPasteDotComProtocol::checkConfiguration(QString *errorMessage)
{
    if (!m_hostKnownOk)
        m_hostKnownOk = httpStatus(baseUrl(), errorMessage);
    return m_hostKnownOk;
}

void DPasteDotComProtocol::reportError(const QString &message)
{
    const QString fullMessage = tr("%1: %2").arg(protocolName(), message);
    Core::MessageManager::write(fullMessage, Core::MessageManager::ModeSwitch);
}

} // namespace CodePaster