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

View File

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

View File

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

View File

@@ -25,11 +25,11 @@
#include "cpasterplugin.h" #include "cpasterplugin.h"
#include "pasteview.h" #include "dpastedotcomprotocol.h"
#include "pastebindotcomprotocol.h"
#include "pastecodedotxyzprotocol.h"
#include "fileshareprotocol.h" #include "fileshareprotocol.h"
#include "pastebindotcomprotocol.h"
#include "pasteselectdialog.h" #include "pasteselectdialog.h"
#include "pasteview.h"
#include "settingspage.h" #include "settingspage.h"
#include "settings.h" #include "settings.h"
#include "urlopenprotocol.h" #include "urlopenprotocol.h"
@@ -91,12 +91,12 @@ public:
PasteBinDotComProtocol pasteBinProto; PasteBinDotComProtocol pasteBinProto;
FileShareProtocol fileShareProto; FileShareProtocol fileShareProto;
PasteCodeDotXyzProtocol pasteCodeProto; DPasteDotComProtocol dpasteProto;
const QList<Protocol *> m_protocols { const QList<Protocol *> m_protocols {
&pasteBinProto, &pasteBinProto,
&fileShareProto, &fileShareProto,
&pasteCodeProto &dpasteProto
}; };
SettingsPage m_settingsPage { 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/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -23,33 +23,28 @@
** **
****************************************************************************/ ****************************************************************************/
#include "pastecodedotxyzprotocol.h" #include "dpastedotcomprotocol.h"
#include <coreplugin/messagemanager.h> #include <coreplugin/messagemanager.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QNetworkReply> #include <QNetworkReply>
#include <QUrl> #include <QUrl>
namespace CodePaster { namespace CodePaster {
static QString baseUrl() { return QString("https://pastecode.xyz"); } static QString baseUrl() { return QString("http://dpaste.com"); }
static QString apiUrl() { return baseUrl() + "/api"; } 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] { connect(reply, &QNetworkReply::finished, this, [this, id, reply] {
QString title; QString title;
QString content; 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, const QString &text,
Protocol::ContentType ct, ContentType ct,
int expiryDays, int expiryDays,
bool publicPaste, bool publicPaste,
const QString &username, const QString &username,
@@ -75,32 +89,23 @@ void PasteCodeDotXyzProtocol::paste(
const QString &description const QString &description
) )
{ {
QByteArray data; Q_UNUSED(publicPaste)
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(comment) 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] { connect(reply, &QNetworkReply::finished, this, [this, reply] {
QString data; QString data;
if (reply->error()) { if (reply->error()) {
reportError(reply->errorString()); // FIXME: Why can't we properly emit an error here? reportError(reply->errorString()); // FIXME: Why can't we properly emit an error here?
reportError(QString::fromUtf8(reply->readAll()));
} else { } else {
data = QString::fromUtf8(reply->readAll()); data = QString::fromUtf8(reply->readAll());
if (!data.startsWith(baseUrl())) { if (!data.startsWith(baseUrl())) {
@@ -113,40 +118,14 @@ void PasteCodeDotXyzProtocol::paste(
}); });
} }
void PasteCodeDotXyzProtocol::list() bool DPasteDotComProtocol::checkConfiguration(QString *errorMessage)
{
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)
{ {
if (!m_hostKnownOk) if (!m_hostKnownOk)
m_hostKnownOk = httpStatus(apiUrl(), errorMessage); m_hostKnownOk = httpStatus(baseUrl(), errorMessage);
return m_hostKnownOk; return m_hostKnownOk;
} }
void PasteCodeDotXyzProtocol::reportError(const QString &message) void DPasteDotComProtocol::reportError(const QString &message)
{ {
const QString fullMessage = tr("%1: %2").arg(protocolName(), message); const QString fullMessage = tr("%1: %2").arg(protocolName(), message);
Core::MessageManager::write(fullMessage, Core::MessageManager::ModeSwitch); 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/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of Qt Creator. ** This file is part of Qt Creator.
@@ -29,7 +29,7 @@
namespace CodePaster { namespace CodePaster {
class PasteCodeDotXyzProtocol : public NetworkProtocol class DPasteDotComProtocol : public NetworkProtocol
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -47,7 +47,6 @@ private:
const QString &username = QString(), const QString &username = QString(),
const QString &comment = QString(), const QString &comment = QString(),
const QString &description = QString()) override; const QString &description = QString()) override;
void list() override;
bool checkConfiguration(QString *errorMessage) override; bool checkConfiguration(QString *errorMessage) override;
static void reportError(const QString &message); static void reportError(const QString &message);

View File

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

View File

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

View File

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

View File

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