CPaster: Replace pastecode.xyz with dpaste.com

The pastecode.xyz service has apparently ceased to exist.

Fixes: QTCREATORBUG-24002
Change-Id: I95fe6ec1388558d4dc176f66b166026619414e89
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2020-05-18 12:00:50 +02:00
parent 3d353424a6
commit 715dfcd3f8
10 changed files with 65 additions and 87 deletions

View File

@@ -11,6 +11,7 @@ add_qtc_plugin(CodePaster
cpasterconstants.h
cpaster.qrc
cpasterplugin.cpp cpasterplugin.h
dpastedotcomprotocol.cpp dpastedotcomprotocol.h
fileshareprotocol.cpp fileshareprotocol.h
fileshareprotocolsettingspage.cpp fileshareprotocolsettingspage.h
fileshareprotocolsettingswidget.ui
@@ -18,7 +19,6 @@ add_qtc_plugin(CodePaster
frontend/main.cpp
pastebindotcomprotocol.cpp pastebindotcomprotocol.h
pastebindotcomsettings.ui
pastecodedotxyzprotocol.cpp pastecodedotxyzprotocol.h
pasteselect.ui
pasteselectdialog.cpp pasteselectdialog.h
pasteview.cpp pasteview.h pasteview.ui

View File

@@ -1,12 +1,12 @@
QT += network
include(../../qtcreatorplugin.pri)
HEADERS += cpasterplugin.h \
dpastedotcomprotocol.h \
settingspage.h \
protocol.h \
pasteview.h \
cpasterconstants.h \
pastebindotcomprotocol.h \
pastecodedotxyzprotocol.h \
settings.h \
pasteselectdialog.h \
columnindicatortextedit.h \
@@ -17,11 +17,11 @@ HEADERS += cpasterplugin.h \
codepasterservice.h
SOURCES += cpasterplugin.cpp \
dpastedotcomprotocol.cpp \
settingspage.cpp \
protocol.cpp \
pasteview.cpp \
pastebindotcomprotocol.cpp \
pastecodedotxyzprotocol.cpp \
settings.cpp \
pasteselectdialog.cpp \
columnindicatortextedit.cpp \

View File

@@ -19,6 +19,8 @@ QtcPlugin {
"cpaster.qrc",
"cpasterplugin.cpp",
"cpasterplugin.h",
"dpastedotcomprotocol.cpp",
"dpastedotcomprotocol.h",
"fileshareprotocol.cpp",
"fileshareprotocol.h",
"fileshareprotocolsettingspage.cpp",
@@ -27,8 +29,6 @@ QtcPlugin {
"pastebindotcomprotocol.cpp",
"pastebindotcomprotocol.h",
"pastebindotcomsettings.ui",
"pastecodedotxyzprotocol.cpp",
"pastecodedotxyzprotocol.h",
"pasteselect.ui",
"pasteselectdialog.cpp",
"pasteselectdialog.h",

View File

@@ -25,11 +25,11 @@
#include "cpasterplugin.h"
#include "pasteview.h"
#include "pastebindotcomprotocol.h"
#include "pastecodedotxyzprotocol.h"
#include "dpastedotcomprotocol.h"
#include "fileshareprotocol.h"
#include "pastebindotcomprotocol.h"
#include "pasteselectdialog.h"
#include "pasteview.h"
#include "settingspage.h"
#include "settings.h"
#include "urlopenprotocol.h"
@@ -91,12 +91,12 @@ public:
PasteBinDotComProtocol pasteBinProto;
FileShareProtocol fileShareProto;
PasteCodeDotXyzProtocol pasteCodeProto;
DPasteDotComProtocol dpasteProto;
const QList<Protocol *> m_protocols {
&pasteBinProto,
&fileShareProto,
&pasteCodeProto
&dpasteProto
};
SettingsPage m_settingsPage {

View File

@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
@@ -23,33 +23,28 @@
**
****************************************************************************/
#include "pastecodedotxyzprotocol.h"
#include "dpastedotcomprotocol.h"
#include <coreplugin/messagemanager.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QNetworkReply>
#include <QUrl>
namespace CodePaster {
static QString baseUrl() { return QString("https://pastecode.xyz"); }
static QString apiUrl() { return baseUrl() + "/api"; }
static QString baseUrl() { return QString("http://dpaste.com"); }
static QString apiUrl() { return baseUrl() + "/api/v2/"; }
QString PasteCodeDotXyzProtocol::protocolName() { return QString("Pastecode.Xyz"); }
QString DPasteDotComProtocol::protocolName() { return QString("DPaste.Com"); }
unsigned PasteCodeDotXyzProtocol::capabilities() const
unsigned DPasteDotComProtocol::capabilities() const
{
return ListCapability | PostDescriptionCapability | PostUserNameCapability;
return PostDescriptionCapability | PostUserNameCapability;
}
void PasteCodeDotXyzProtocol::fetch(const QString &id)
void DPasteDotComProtocol::fetch(const QString &id)
{
QNetworkReply * const reply = httpGet(baseUrl() + "/view/raw/" + id);
QNetworkReply * const reply = httpGet(baseUrl() + '/' + id + ".txt");
connect(reply, &QNetworkReply::finished, this, [this, id, reply] {
QString title;
QString content;
@@ -65,9 +60,28 @@ void PasteCodeDotXyzProtocol::fetch(const QString &id)
});
}
void PasteCodeDotXyzProtocol::paste(
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,
Protocol::ContentType ct,
ContentType ct,
int expiryDays,
bool publicPaste,
const QString &username,
@@ -75,32 +89,23 @@ void PasteCodeDotXyzProtocol::paste(
const QString &description
)
{
QByteArray data;
data += "text=" + QUrl::toPercentEncoding(fixNewLines(text));
data += "&expire=" + QUrl::toPercentEncoding(QString::number(expiryDays * 24 * 60));
data += "&title=" + QUrl::toPercentEncoding(description);
data += "&name=" + QUrl::toPercentEncoding(username);
if (!publicPaste)
data += "&private=1";
static const auto langValue = [](Protocol::ContentType type) -> QByteArray {
switch (type) {
case Protocol::Text: return "text";
case Protocol::C: return "c";
case Protocol::Cpp: return "cpp";
case Protocol::JavaScript: return "javascript";
case Protocol::Diff: return "diff";
case Protocol::Xml: return "xml";
}
return QByteArray(); // Crutch for compiler.
};
data += "&lang=" + langValue(ct);
Q_UNUSED(publicPaste)
Q_UNUSED(comment)
QNetworkReply * const reply = httpPost(apiUrl() + "/create", data);
// 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())) {
@@ -113,40 +118,14 @@ void PasteCodeDotXyzProtocol::paste(
});
}
void PasteCodeDotXyzProtocol::list()
{
QNetworkReply * const reply = httpGet(apiUrl() + "/recent");
connect(reply, &QNetworkReply::finished, this, [this, reply] {
QStringList ids;
if (reply->error()) {
reportError(reply->errorString());
} else {
QJsonParseError parseError;
const QJsonDocument jsonData = QJsonDocument::fromJson(reply->readAll(), &parseError);
if (parseError.error != QJsonParseError::NoError) {
reportError(parseError.errorString());
} else {
const QJsonArray jsonList = jsonData.array();
for (auto it = jsonList.constBegin(); it != jsonList.constEnd(); ++it) {
const QString id = it->toObject().value("pid").toString();
if (!id.isEmpty())
ids << id;
}
}
}
emit listDone(name(), ids);
reply->deleteLater();
});
}
bool PasteCodeDotXyzProtocol::checkConfiguration(QString *errorMessage)
bool DPasteDotComProtocol::checkConfiguration(QString *errorMessage)
{
if (!m_hostKnownOk)
m_hostKnownOk = httpStatus(apiUrl(), errorMessage);
m_hostKnownOk = httpStatus(baseUrl(), errorMessage);
return m_hostKnownOk;
}
void PasteCodeDotXyzProtocol::reportError(const QString &message)
void DPasteDotComProtocol::reportError(const QString &message)
{
const QString fullMessage = tr("%1: %2").arg(protocolName(), message);
Core::MessageManager::write(fullMessage, Core::MessageManager::ModeSwitch);

View File

@@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
@@ -29,7 +29,7 @@
namespace CodePaster {
class PasteCodeDotXyzProtocol : public NetworkProtocol
class DPasteDotComProtocol : public NetworkProtocol
{
Q_OBJECT
public:
@@ -47,7 +47,6 @@ private:
const QString &username = QString(),
const QString &comment = QString(),
const QString &description = QString()) override;
void list() override;
bool checkConfiguration(QString *errorMessage) override;
static void reportError(const QString &message);

View File

@@ -3,8 +3,8 @@ add_qtc_executable(cpaster
SOURCES
argumentscollector.cpp argumentscollector.h
main.cpp
../dpastedotcomprotocol.cpp ../dpastedotcomprotocol.h
../pastebindotcomprotocol.cpp ../pastebindotcomprotocol.h
../pastecodedotxyzprotocol.cpp ../pastecodedotxyzprotocol.h
../protocol.cpp ../protocol.h
../urlopenprotocol.cpp ../urlopenprotocol.h
)

View File

@@ -12,14 +12,14 @@ QT += network
HEADERS = ../protocol.h \
../cpasterconstants.h \
../dpastedotcomprotocol.h \
../pastebindotcomprotocol.h \
../pastecodedotxyzprotocol.h \
../urlopenprotocol.h \
argumentscollector.h
SOURCES += ../protocol.cpp \
../dpastedotcomprotocol.cpp \
../pastebindotcomprotocol.cpp \
../pastecodedotxyzprotocol.cpp \
../urlopenprotocol.cpp \
argumentscollector.cpp \
main.cpp

View File

@@ -23,8 +23,8 @@ QtcTool {
prefix: "../"
files: [
"cpasterconstants.h",
"dpastedotcomprotocol.h", "dpastedotcomprotocol.cpp",
"pastebindotcomprotocol.h", "pastebindotcomprotocol.cpp",
"pastecodedotxyzprotocol.h", "pastecodedotxyzprotocol.cpp",
"protocol.h", "protocol.cpp",
"urlopenprotocol.h", "urlopenprotocol.cpp",
]

View File

@@ -24,8 +24,8 @@
****************************************************************************/
#include "argumentscollector.h"
#include "../dpastedotcomprotocol.h"
#include "../pastebindotcomprotocol.h"
#include "../pastecodedotxyzprotocol.h"
#include <QFile>
#include <QObject>
@@ -47,8 +47,8 @@ public:
{
if (protocol == PasteBinDotComProtocol::protocolName().toLower())
m_protocol.reset(new PasteBinDotComProtocol);
else if (protocol == PasteCodeDotXyzProtocol::protocolName().toLower())
m_protocol.reset(new PasteCodeDotXyzProtocol);
else if (protocol == DPasteDotComProtocol::protocolName().toLower())
m_protocol.reset(new DPasteDotComProtocol);
else
qFatal("Internal error: Invalid protocol.");
}
@@ -88,8 +88,8 @@ int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
const QStringList protocols = {PasteBinDotComProtocol::protocolName().toLower(),
PasteCodeDotXyzProtocol::protocolName().toLower()};
const QStringList protocols = {DPasteDotComProtocol::protocolName().toLower(),
PasteBinDotComProtocol::protocolName().toLower()};
ArgumentsCollector argsCollector(protocols);
QStringList arguments = QCoreApplication::arguments();
arguments.removeFirst();