forked from qt-creator/qt-creator
Further love for Cpaster plugin
Introduce capabilities flags for Protocols, disable controls in Pasteview accordingly. Work towards making PasteBin.com work.
This commit is contained in:
@@ -60,9 +60,9 @@ QString CodePasterProtocol::name() const
|
|||||||
return QLatin1String("CodePaster");
|
return QLatin1String("CodePaster");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CodePasterProtocol::canList() const
|
unsigned CodePasterProtocol::capabilities() const
|
||||||
{
|
{
|
||||||
return true;
|
return ListCapability|PostCommentCapability|PostDescriptionCapability;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CodePasterProtocol::isValidHostName(const QString& hostName)
|
bool CodePasterProtocol::isValidHostName(const QString& hostName)
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ public:
|
|||||||
|
|
||||||
QString name() const;
|
QString name() const;
|
||||||
|
|
||||||
bool canList() const;
|
virtual unsigned capabilities() const;
|
||||||
bool hasSettings() const;
|
bool hasSettings() const;
|
||||||
Core::IOptionsPage *settingsPage();
|
Core::IOptionsPage *settingsPage();
|
||||||
|
|
||||||
|
|||||||
@@ -181,10 +181,8 @@ void CodepasterPlugin::post()
|
|||||||
QString comment;
|
QString comment;
|
||||||
QString protocolName;
|
QString protocolName;
|
||||||
|
|
||||||
PasteView view(0);
|
PasteView view(m_protocols, 0);
|
||||||
foreach (Protocol *p, m_protocols) {
|
view.setProtocol(m_settings->protocol);
|
||||||
view.addProtocol(p->name(), p->name() == m_settings->protocol);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!view.show(username, description, comment, lst))
|
if (!view.show(username, description, comment, lst))
|
||||||
return; // User canceled post
|
return; // User canceled post
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public:
|
|||||||
QString name() const { return QLatin1String("Pastebin.Ca"); }
|
QString name() const { return QLatin1String("Pastebin.Ca"); }
|
||||||
|
|
||||||
bool hasSettings() const { return false; }
|
bool hasSettings() const { return false; }
|
||||||
bool canList() const { return false; }
|
virtual unsigned capabilities() const { return 0; }
|
||||||
|
|
||||||
void fetch(const QString &id);
|
void fetch(const QString &id);
|
||||||
void paste(const QString &text,
|
void paste(const QString &text,
|
||||||
|
|||||||
@@ -29,13 +29,20 @@
|
|||||||
|
|
||||||
#include "pastebindotcomprotocol.h"
|
#include "pastebindotcomprotocol.h"
|
||||||
#include "pastebindotcomsettings.h"
|
#include "pastebindotcomsettings.h"
|
||||||
|
#include "cgi.h"
|
||||||
|
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
#include <QtCore/QTextStream>
|
||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
|
|
||||||
|
enum { debug = 0 };
|
||||||
|
|
||||||
|
static const char phpScriptpC[] = "api_public.php";
|
||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
PasteBinDotComProtocol::PasteBinDotComProtocol()
|
PasteBinDotComProtocol::PasteBinDotComProtocol()
|
||||||
{
|
{
|
||||||
@@ -46,13 +53,22 @@ PasteBinDotComProtocol::PasteBinDotComProtocol()
|
|||||||
this, SLOT(readPostResponseHeader(const QHttpResponseHeader&)));
|
this, SLOT(readPostResponseHeader(const QHttpResponseHeader&)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString PasteBinDotComProtocol::hostName() const
|
||||||
|
{
|
||||||
|
|
||||||
|
QString rc = settings->hostPrefix();
|
||||||
|
if (!rc.isEmpty())
|
||||||
|
rc.append(QLatin1Char('.'));
|
||||||
|
rc.append(QLatin1String("pastebin.com"));
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
void PasteBinDotComProtocol::fetch(const QString &id)
|
void PasteBinDotComProtocol::fetch(const QString &id)
|
||||||
{
|
{
|
||||||
QString link = QLatin1String("http://");
|
QString link;
|
||||||
if (!settings->hostPrefix().isEmpty())
|
QTextStream(&link) << "http://" << hostName() << '/' << phpScriptpC << "?dl=" << id;
|
||||||
link.append(QString("%1.").arg(settings->hostPrefix()));
|
if (debug)
|
||||||
link.append("pastebin.com/pastebin.php?dl=");
|
qDebug() << "fetch: sending " << link;
|
||||||
link.append(id);
|
|
||||||
QUrl url(link);
|
QUrl url(link);
|
||||||
QNetworkRequest r(url);
|
QNetworkRequest r(url);
|
||||||
|
|
||||||
@@ -63,35 +79,36 @@ void PasteBinDotComProtocol::fetch(const QString &id)
|
|||||||
|
|
||||||
void PasteBinDotComProtocol::paste(const QString &text,
|
void PasteBinDotComProtocol::paste(const QString &text,
|
||||||
const QString &username,
|
const QString &username,
|
||||||
const QString &comment,
|
const QString & /* comment */,
|
||||||
const QString &description)
|
const QString & /* description */)
|
||||||
{
|
{
|
||||||
Q_UNUSED(comment);
|
QString data;
|
||||||
Q_UNUSED(description);
|
QTextStream str(&data);
|
||||||
QString data = "code2=";
|
str << "paste_code=" << CGI::encodeURL(text) << "&paste_name="
|
||||||
data += text;
|
<< CGI::encodeURL(username);
|
||||||
data += "&parent_pid=&format=text&expiry=d&poster=";
|
QHttpRequestHeader header(QLatin1String("POST"), QLatin1String(phpScriptpC));
|
||||||
data += username;
|
|
||||||
data += "&paste=Send";
|
const QString host = hostName();
|
||||||
QHttpRequestHeader header("POST", "/pastebin.php");
|
header.setValue(QLatin1String("host"), host);
|
||||||
header.setValue("host", "qt.pastebin.com" );
|
header.setContentType(QLatin1String("application/x-www-form-urlencoded"));
|
||||||
header.setContentType("application/x-www-form-urlencoded");
|
http.setHost(host, QHttp::ConnectionModeHttp);
|
||||||
http.setHost("qt.pastebin.com", QHttp::ConnectionModeHttp);
|
header.setValue(QLatin1String("User-Agent"), QLatin1String("CreatorPastebin"));
|
||||||
header.setValue("User-Agent", "CreatorPastebin");
|
|
||||||
postId = http.request(header, data.toAscii());
|
postId = http.request(header, data.toAscii());
|
||||||
|
if (debug)
|
||||||
|
qDebug() << "paste" << data << postId << host;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteBinDotComProtocol::readPostResponseHeader(const QHttpResponseHeader &header)
|
void PasteBinDotComProtocol::readPostResponseHeader(const QHttpResponseHeader &header)
|
||||||
{
|
{
|
||||||
switch (header.statusCode())
|
const int code = header.statusCode();
|
||||||
{
|
if (debug)
|
||||||
|
qDebug() << "readPostResponseHeader" << code << header.toString() << header.values();
|
||||||
|
switch (code) {
|
||||||
// If we receive any of those, everything is bon.
|
// If we receive any of those, everything is bon.
|
||||||
case 200:
|
|
||||||
case 301:
|
case 301:
|
||||||
case 303:
|
case 303:
|
||||||
case 307:
|
case 307:
|
||||||
break;
|
case 200:
|
||||||
|
|
||||||
case 302: {
|
case 302: {
|
||||||
QString link = header.value("Location");
|
QString link = header.value("Location");
|
||||||
emit pasteDone(link);
|
emit pasteDone(link);
|
||||||
@@ -104,20 +121,28 @@ void PasteBinDotComProtocol::readPostResponseHeader(const QHttpResponseHeader &h
|
|||||||
|
|
||||||
void PasteBinDotComProtocol::postRequestFinished(int id, bool error)
|
void PasteBinDotComProtocol::postRequestFinished(int id, bool error)
|
||||||
{
|
{
|
||||||
if (id == postId && error)
|
if (id == postId && error) {
|
||||||
emit pasteDone(http.errorString());
|
const QString errorMessage = http.errorString();
|
||||||
|
if (debug)
|
||||||
|
qDebug() << "postRequestFinished" << id << errorMessage;
|
||||||
|
emit pasteDone(errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteBinDotComProtocol::fetchFinished()
|
void PasteBinDotComProtocol::fetchFinished()
|
||||||
{
|
{
|
||||||
QString title;
|
QString title;
|
||||||
QString content;
|
QString content;
|
||||||
bool error = reply->error();
|
const bool error = reply->error();
|
||||||
if (error) {
|
if (error) {
|
||||||
content = reply->errorString();
|
content = reply->errorString();
|
||||||
|
if (debug)
|
||||||
|
qDebug() << "fetchFinished: error" << fetchId << content;
|
||||||
} else {
|
} else {
|
||||||
title = QString::fromLatin1("Pastebin.com: %1").arg(fetchId);
|
title = QString::fromLatin1("Pastebin.com: %1").arg(fetchId);
|
||||||
content = reply->readAll();
|
content = QString::fromAscii(reply->readAll());
|
||||||
|
if (debug)
|
||||||
|
qDebug() << "fetchFinished: " << content.size();
|
||||||
}
|
}
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
reply = 0;
|
reply = 0;
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ public:
|
|||||||
|
|
||||||
QString name() const { return QLatin1String("Pastebin.Com"); }
|
QString name() const { return QLatin1String("Pastebin.Com"); }
|
||||||
|
|
||||||
|
virtual unsigned capabilities() const { return 0; }
|
||||||
bool hasSettings() const { return true; }
|
bool hasSettings() const { return true; }
|
||||||
Core::IOptionsPage* settingsPage();
|
Core::IOptionsPage* settingsPage();
|
||||||
|
|
||||||
@@ -63,6 +64,8 @@ public slots:
|
|||||||
void readPostResponseHeader(const QHttpResponseHeader &);
|
void readPostResponseHeader(const QHttpResponseHeader &);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QString hostName() const;
|
||||||
|
|
||||||
PasteBinDotComSettings *settings;
|
PasteBinDotComSettings *settings;
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
QNetworkReply *reply;
|
QNetworkReply *reply;
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ void PasteSelectDialog::list()
|
|||||||
{
|
{
|
||||||
const int index = protocolIndex();
|
const int index = protocolIndex();
|
||||||
|
|
||||||
QTC_ASSERT(m_protocols.at(index)->canList(), return);
|
QTC_ASSERT((m_protocols.at(index)->capabilities() & Protocol::ListCapability), return);
|
||||||
|
|
||||||
m_ui.listWidget->clear();
|
m_ui.listWidget->clear();
|
||||||
m_ui.listWidget->addItem(new QListWidgetItem(tr("Waiting for items")));
|
m_ui.listWidget->addItem(new QListWidgetItem(tr("Waiting for items")));
|
||||||
@@ -123,7 +123,7 @@ void PasteSelectDialog::list()
|
|||||||
|
|
||||||
void PasteSelectDialog::protocolChanged(int i)
|
void PasteSelectDialog::protocolChanged(int i)
|
||||||
{
|
{
|
||||||
const bool canList = m_protocols.at(i)->canList();
|
const bool canList = m_protocols.at(i)->capabilities() & Protocol::ListCapability;
|
||||||
m_refreshButton->setEnabled(canList);
|
m_refreshButton->setEnabled(canList);
|
||||||
if (canList) {
|
if (canList) {
|
||||||
list();
|
list();
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
#include "pasteview.h"
|
#include "pasteview.h"
|
||||||
|
#include "protocol.h"
|
||||||
|
|
||||||
#include <QtGui/QFontMetrics>
|
#include <QtGui/QFontMetrics>
|
||||||
#include <QtGui/QPainter>
|
#include <QtGui/QPainter>
|
||||||
@@ -36,6 +37,7 @@
|
|||||||
#include <QtCore/QSettings>
|
#include <QtCore/QSettings>
|
||||||
#include <QtCore/QByteArray>
|
#include <QtCore/QByteArray>
|
||||||
|
|
||||||
|
namespace CodePaster {
|
||||||
class ColumnIndicatorTextEdit : public QTextEdit
|
class ColumnIndicatorTextEdit : public QTextEdit
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -78,8 +80,9 @@ void ColumnIndicatorTextEdit::paintEvent(QPaintEvent *event)
|
|||||||
// -------------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
PasteView::PasteView(QWidget *parent)
|
PasteView::PasteView(const QList<Protocol *> protocols,
|
||||||
: QDialog(parent)
|
QWidget *parent)
|
||||||
|
: QDialog(parent), m_protocols(protocols)
|
||||||
{
|
{
|
||||||
m_ui.setupUi(this);
|
m_ui.setupUi(this);
|
||||||
|
|
||||||
@@ -89,6 +92,11 @@ PasteView::PasteView(QWidget *parent)
|
|||||||
m_ui.vboxLayout1->addWidget(m_ui.uiPatchView);
|
m_ui.vboxLayout1->addWidget(m_ui.uiPatchView);
|
||||||
m_ui.buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Paste"));
|
m_ui.buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Paste"));
|
||||||
connect(m_ui.uiPatchList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(contentChanged()));
|
connect(m_ui.uiPatchList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(contentChanged()));
|
||||||
|
|
||||||
|
foreach(const Protocol *p, protocols)
|
||||||
|
m_ui.protocolBox->addItem(p->name());
|
||||||
|
connect(m_ui.protocolBox, SIGNAL(currentIndexChanged(int)),
|
||||||
|
this, SLOT(protocolChanged(int)));
|
||||||
}
|
}
|
||||||
|
|
||||||
PasteView::~PasteView()
|
PasteView::~PasteView()
|
||||||
@@ -140,6 +148,13 @@ void PasteView::contentChanged()
|
|||||||
m_ui.uiPatchView->setPlainText(content());
|
m_ui.uiPatchView->setPlainText(content());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PasteView::protocolChanged(int p)
|
||||||
|
{
|
||||||
|
const unsigned caps = m_protocols.at(p)->capabilities();
|
||||||
|
m_ui.uiDescription->setEnabled(caps & Protocol::PostDescriptionCapability);
|
||||||
|
m_ui.uiComment->setEnabled(caps & Protocol::PostCommentCapability);
|
||||||
|
}
|
||||||
|
|
||||||
int PasteView::show(const QString &user, const QString &description, const QString &comment,
|
int PasteView::show(const QString &user, const QString &description, const QString &comment,
|
||||||
const FileDataList &parts)
|
const FileDataList &parts)
|
||||||
{
|
{
|
||||||
@@ -186,9 +201,15 @@ int PasteView::show(const QString &user, const QString &description, const QStri
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteView::addProtocol(const QString &protocol, bool defaultProtocol)
|
void PasteView::setProtocol(const QString &protocol)
|
||||||
{
|
{
|
||||||
m_ui.protocolBox->addItem(protocol);
|
const int index = m_ui.protocolBox->findText(protocol);
|
||||||
if (defaultProtocol)
|
m_ui.protocolBox->setCurrentIndex(index);
|
||||||
m_ui.protocolBox->setCurrentIndex(m_ui.protocolBox->findText(protocol));
|
if (index == m_ui.protocolBox->currentIndex()) {
|
||||||
|
protocolChanged(index); // Force enabling
|
||||||
|
} else {
|
||||||
|
m_ui.protocolBox->setCurrentIndex(index);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} //namespace CodePaster
|
||||||
|
|||||||
@@ -35,17 +35,20 @@
|
|||||||
|
|
||||||
#include <QtGui/QDialog>
|
#include <QtGui/QDialog>
|
||||||
|
|
||||||
|
namespace CodePaster {
|
||||||
|
class Protocol;
|
||||||
class PasteView : public QDialog
|
class PasteView : public QDialog
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit PasteView(QWidget *parent);
|
explicit PasteView(const QList<Protocol *> protocols,
|
||||||
|
QWidget *parent);
|
||||||
~PasteView();
|
~PasteView();
|
||||||
|
|
||||||
int show(const QString &user, const QString &description, const QString &comment,
|
int show(const QString &user, const QString &description, const QString &comment,
|
||||||
const FileDataList &parts);
|
const FileDataList &parts);
|
||||||
|
|
||||||
void addProtocol(const QString &protocol, bool defaultProtocol = false);
|
void setProtocol(const QString &protocol);
|
||||||
|
|
||||||
QString user() const;
|
QString user() const;
|
||||||
QString description() const;
|
QString description() const;
|
||||||
@@ -55,10 +58,13 @@ public:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void contentChanged();
|
void contentChanged();
|
||||||
|
void protocolChanged(int);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
const QList<Protocol *> m_protocols;
|
||||||
|
|
||||||
Ui::ViewDialog m_ui;
|
Ui::ViewDialog m_ui;
|
||||||
FileDataList m_parts;
|
FileDataList m_parts;
|
||||||
};
|
};
|
||||||
|
} // namespace CodePaster
|
||||||
#endif // VIEW_H
|
#endif // VIEW_H
|
||||||
|
|||||||
@@ -41,6 +41,11 @@ class Protocol : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
enum Capabilities {
|
||||||
|
ListCapability = 0x1,
|
||||||
|
PostCommentCapability = 0x2,
|
||||||
|
PostDescriptionCapability = 0x4
|
||||||
|
};
|
||||||
Protocol();
|
Protocol();
|
||||||
virtual ~Protocol();
|
virtual ~Protocol();
|
||||||
|
|
||||||
@@ -48,7 +53,9 @@ public:
|
|||||||
|
|
||||||
bool canFetch() const;
|
bool canFetch() const;
|
||||||
bool canPost() const;
|
bool canPost() const;
|
||||||
virtual bool canList() const = 0;
|
|
||||||
|
|
||||||
|
virtual unsigned capabilities() const = 0;
|
||||||
virtual bool hasSettings() const;
|
virtual bool hasSettings() const;
|
||||||
virtual Core::IOptionsPage* settingsPage();
|
virtual Core::IOptionsPage* settingsPage();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user