forked from qt-creator/qt-creator
CPaster: Introduce Proxy for QNetworkAccessManager
that is shared and provides delayed creation. Add "Paste clipboard" option.
This commit is contained in:
@@ -48,7 +48,8 @@ enum { debug = 0 };
|
|||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
|
|
||||||
CodePasterProtocol::CodePasterProtocol() :
|
CodePasterProtocol::CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw) :
|
||||||
|
NetworkProtocol(nw),
|
||||||
m_page(new CodePaster::CodePasterSettingsPage),
|
m_page(new CodePaster::CodePasterSettingsPage),
|
||||||
m_pasteReply(0),
|
m_pasteReply(0),
|
||||||
m_fetchReply(0),
|
m_fetchReply(0),
|
||||||
@@ -98,10 +99,7 @@ void CodePasterProtocol::fetch(const QString &id)
|
|||||||
link.append(hostName);
|
link.append(hostName);
|
||||||
link.append("/?format=raw&id=");
|
link.append("/?format=raw&id=");
|
||||||
link.append(id);
|
link.append(id);
|
||||||
QUrl url(link);
|
m_fetchReply = httpGet(link);
|
||||||
QNetworkRequest r(url);
|
|
||||||
|
|
||||||
m_fetchReply = m_manager.get(r);
|
|
||||||
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
||||||
m_fetchId = id;
|
m_fetchId = id;
|
||||||
}
|
}
|
||||||
@@ -116,9 +114,7 @@ void CodePasterProtocol::list()
|
|||||||
QString link = QLatin1String("http://");
|
QString link = QLatin1String("http://");
|
||||||
link += hostName;
|
link += hostName;
|
||||||
link += QLatin1String("/?command=browse&format=raw");
|
link += QLatin1String("/?command=browse&format=raw");
|
||||||
QUrl url(link);
|
m_listReply = httpGet(link);
|
||||||
QNetworkRequest r(url);
|
|
||||||
m_listReply = m_manager.get(r);
|
|
||||||
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
|
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,9 +138,7 @@ void CodePasterProtocol::paste(const QString &text,
|
|||||||
data += "&poster=";
|
data += "&poster=";
|
||||||
data += CGI::encodeURL(username).toLatin1();
|
data += CGI::encodeURL(username).toLatin1();
|
||||||
|
|
||||||
QUrl url(QLatin1String("http://") + hostName);
|
m_pasteReply = httpPost(QLatin1String("http://") + hostName, data);
|
||||||
QNetworkRequest r(url);
|
|
||||||
m_pasteReply = m_manager.post(r, data);
|
|
||||||
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
|
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,6 @@
|
|||||||
|
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
#include <QtNetwork/QNetworkAccessManager>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QNetworkReply;
|
class QNetworkReply;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
@@ -42,11 +40,11 @@ namespace CodePaster {
|
|||||||
|
|
||||||
class CodePasterSettingsPage;
|
class CodePasterSettingsPage;
|
||||||
|
|
||||||
class CodePasterProtocol : public Protocol
|
class CodePasterProtocol : public NetworkProtocol
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
CodePasterProtocol();
|
explicit CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw);
|
||||||
~CodePasterProtocol();
|
~CodePasterProtocol();
|
||||||
|
|
||||||
QString name() const;
|
QString name() const;
|
||||||
@@ -70,7 +68,6 @@ public slots:
|
|||||||
private:
|
private:
|
||||||
bool isValidHostName(const QString& hostName);
|
bool isValidHostName(const QString& hostName);
|
||||||
CodePasterSettingsPage *m_page;
|
CodePasterSettingsPage *m_page;
|
||||||
QNetworkAccessManager m_manager;
|
|
||||||
QNetworkReply *m_pasteReply;
|
QNetworkReply *m_pasteReply;
|
||||||
QNetworkReply *m_fetchReply;
|
QNetworkReply *m_fetchReply;
|
||||||
QNetworkReply *m_listReply;
|
QNetworkReply *m_listReply;
|
||||||
|
|||||||
@@ -65,7 +65,9 @@ using namespace CodePaster;
|
|||||||
using namespace Core;
|
using namespace Core;
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
|
||||||
CodepasterPlugin::CodepasterPlugin() : m_settings(new Settings)
|
CodepasterPlugin::CodepasterPlugin() :
|
||||||
|
m_settings(new Settings),
|
||||||
|
m_postEditorAction(0), m_postClipboardAction(0), m_fetchAction(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,9 +91,10 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
|
|||||||
addAutoReleasedObject(settingsPage);
|
addAutoReleasedObject(settingsPage);
|
||||||
|
|
||||||
// Create the protocols and append them to the Settings
|
// Create the protocols and append them to the Settings
|
||||||
Protocol *protos[] = { new CodePasterProtocol(),
|
const QSharedPointer<NetworkAccessManagerProxy> networkAccessMgrProxy(new NetworkAccessManagerProxy);
|
||||||
new PasteBinDotComProtocol(),
|
Protocol *protos[] = { new CodePasterProtocol(networkAccessMgrProxy),
|
||||||
new PasteBinDotCaProtocol(),
|
new PasteBinDotComProtocol(networkAccessMgrProxy),
|
||||||
|
new PasteBinDotCaProtocol(networkAccessMgrProxy),
|
||||||
0};
|
0};
|
||||||
for(int i=0; protos[i] != 0; ++i) {
|
for(int i=0; protos[i] != 0; ++i) {
|
||||||
connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
|
connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
|
||||||
@@ -116,10 +119,15 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
|
|||||||
|
|
||||||
Core::Command *command;
|
Core::Command *command;
|
||||||
|
|
||||||
m_postAction = new QAction(tr("Paste Snippet..."), this);
|
m_postEditorAction = new QAction(tr("Paste Snippet..."), this);
|
||||||
command = actionManager->registerAction(m_postAction, "CodePaster.Post", globalcontext);
|
command = actionManager->registerAction(m_postEditorAction, "CodePaster.Post", globalcontext);
|
||||||
command->setDefaultKeySequence(QKeySequence(tr("Alt+C,Alt+P")));
|
command->setDefaultKeySequence(QKeySequence(tr("Alt+C,Alt+P")));
|
||||||
connect(m_postAction, SIGNAL(triggered()), this, SLOT(post()));
|
connect(m_postEditorAction, SIGNAL(triggered()), this, SLOT(postEditor()));
|
||||||
|
cpContainer->addAction(command);
|
||||||
|
|
||||||
|
m_postClipboardAction = new QAction(tr("Paste Clipboard..."), this);
|
||||||
|
command = actionManager->registerAction(m_postClipboardAction, "CodePaster.PostClipboard", globalcontext);
|
||||||
|
connect(m_postClipboardAction, SIGNAL(triggered()), this, SLOT(postClipboard()));
|
||||||
cpContainer->addAction(command);
|
cpContainer->addAction(command);
|
||||||
|
|
||||||
m_fetchAction = new QAction(tr("Fetch Snippet..."), this);
|
m_fetchAction = new QAction(tr("Fetch Snippet..."), this);
|
||||||
@@ -145,6 +153,28 @@ void CodepasterPlugin::shutdown()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CodepasterPlugin::postEditor()
|
||||||
|
{
|
||||||
|
const IEditor* editor = EditorManager::instance()->currentEditor();
|
||||||
|
const BaseTextEditorEditable *textEditor = qobject_cast<const BaseTextEditorEditable *>(editor);
|
||||||
|
if (!textEditor)
|
||||||
|
return;
|
||||||
|
|
||||||
|
QString data = textEditor->selectedText();
|
||||||
|
if (data.isEmpty())
|
||||||
|
data = textEditor->contents();
|
||||||
|
if (!data.isEmpty())
|
||||||
|
post(data, textEditor->editor()->mimeType());
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodepasterPlugin::postClipboard()
|
||||||
|
{
|
||||||
|
QString subtype = QLatin1String("plain");
|
||||||
|
const QString text = qApp->clipboard()->text(subtype, QClipboard::Clipboard);
|
||||||
|
if (!text.isEmpty())
|
||||||
|
post(text, QString());
|
||||||
|
}
|
||||||
|
|
||||||
static inline void fixSpecialCharacters(QString &data)
|
static inline void fixSpecialCharacters(QString &data)
|
||||||
{
|
{
|
||||||
QChar *uc = data.data();
|
QChar *uc = data.data();
|
||||||
@@ -167,18 +197,8 @@ static inline void fixSpecialCharacters(QString &data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CodepasterPlugin::post()
|
void CodepasterPlugin::post(QString data, const QString &mimeType)
|
||||||
{
|
{
|
||||||
const IEditor* editor = EditorManager::instance()->currentEditor();
|
|
||||||
const BaseTextEditorEditable *textEditor = qobject_cast<const BaseTextEditorEditable *>(editor);
|
|
||||||
if (!textEditor)
|
|
||||||
return;
|
|
||||||
|
|
||||||
QString data = textEditor->selectedText();
|
|
||||||
if (data.isEmpty())
|
|
||||||
data = textEditor->contents();
|
|
||||||
if (data.isEmpty())
|
|
||||||
return;
|
|
||||||
fixSpecialCharacters(data);
|
fixSpecialCharacters(data);
|
||||||
FileDataList lst = splitDiffToFiles(data.toLatin1());
|
FileDataList lst = splitDiffToFiles(data.toLatin1());
|
||||||
QString username = m_settings->username;
|
QString username = m_settings->username;
|
||||||
@@ -199,7 +219,7 @@ void CodepasterPlugin::post()
|
|||||||
protocolName = view.protocol();
|
protocolName = view.protocol();
|
||||||
foreach(Protocol *protocol, m_protocols) {
|
foreach(Protocol *protocol, m_protocols) {
|
||||||
if (protocol->name() == protocolName) {
|
if (protocol->name() == protocolName) {
|
||||||
const Protocol::ContentType ct = Protocol::contentType(textEditor->editor()->mimeType());
|
const Protocol::ContentType ct = Protocol::contentType(mimeType);
|
||||||
protocol->paste(data, ct, username, comment, description);
|
protocol->paste(data, ct, username, comment, description);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ public:
|
|||||||
virtual void shutdown();
|
virtual void shutdown();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void post();
|
void postEditor();
|
||||||
|
void postClipboard();
|
||||||
void fetch();
|
void fetch();
|
||||||
void finishPost(const QString &link);
|
void finishPost(const QString &link);
|
||||||
void finishFetch(const QString &titleDescription,
|
void finishFetch(const QString &titleDescription,
|
||||||
@@ -66,8 +67,11 @@ public slots:
|
|||||||
bool error);
|
bool error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void post(QString data, const QString &mimeType);
|
||||||
|
|
||||||
const QSharedPointer<Settings> m_settings;
|
const QSharedPointer<Settings> m_settings;
|
||||||
QAction *m_postAction;
|
QAction *m_postEditorAction;
|
||||||
|
QAction *m_postClipboardAction;
|
||||||
QAction *m_fetchAction;
|
QAction *m_fetchAction;
|
||||||
QList<Protocol*> m_protocols;
|
QList<Protocol*> m_protocols;
|
||||||
QStringList m_fetchedSnippets;
|
QStringList m_fetchedSnippets;
|
||||||
|
|||||||
@@ -32,11 +32,13 @@
|
|||||||
|
|
||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
using namespace Core;
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
PasteBinDotCaProtocol::PasteBinDotCaProtocol()
|
PasteBinDotCaProtocol::PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw) :
|
||||||
|
NetworkProtocol(nw),
|
||||||
|
m_fetchReply(0),
|
||||||
|
m_postId(-1)
|
||||||
{
|
{
|
||||||
connect(&http, SIGNAL(requestFinished(int,bool)),
|
connect(&m_http, SIGNAL(requestFinished(int,bool)),
|
||||||
this, SLOT(postRequestFinished(int,bool)));
|
this, SLOT(postRequestFinished(int,bool)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,12 +46,9 @@ void PasteBinDotCaProtocol::fetch(const QString &id)
|
|||||||
{
|
{
|
||||||
QString link = QLatin1String("http://pastebin.ca/raw/");
|
QString link = QLatin1String("http://pastebin.ca/raw/");
|
||||||
link.append(id);
|
link.append(id);
|
||||||
QUrl url(link);
|
m_fetchReply = httpGet(link);
|
||||||
QNetworkRequest r(url);
|
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
||||||
|
m_fetchId = id;
|
||||||
reply = manager.get(r);
|
|
||||||
connect(reply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
|
||||||
fetchId = id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteBinDotCaProtocol::paste(const QString &text,
|
void PasteBinDotCaProtocol::paste(const QString &text,
|
||||||
@@ -69,20 +68,20 @@ void PasteBinDotCaProtocol::paste(const QString &text,
|
|||||||
QHttpRequestHeader header("POST", "/quiet-paste.php");
|
QHttpRequestHeader header("POST", "/quiet-paste.php");
|
||||||
header.setValue("host", "pastebin.ca" );
|
header.setValue("host", "pastebin.ca" );
|
||||||
header.setContentType("application/x-www-form-urlencoded");
|
header.setContentType("application/x-www-form-urlencoded");
|
||||||
http.setHost("pastebin.ca", QHttp::ConnectionModeHttp);
|
m_http.setHost("pastebin.ca", QHttp::ConnectionModeHttp);
|
||||||
header.setValue("User-Agent", "CreatorPastebin");
|
header.setValue("User-Agent", "CreatorPastebin");
|
||||||
postId = http.request(header, data.toAscii());
|
m_postId = m_http.request(header, data.toAscii());
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteBinDotCaProtocol::postRequestFinished(int id, bool error)
|
void PasteBinDotCaProtocol::postRequestFinished(int id, bool error)
|
||||||
{
|
{
|
||||||
QString link;
|
QString link;
|
||||||
if (id == postId) {
|
if (id == m_postId) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
QByteArray data = http.readAll();
|
QByteArray data = m_http.readAll();
|
||||||
link = QString::fromLatin1("http://pastebin.ca/") + QString(data).remove("SUCCESS:");
|
link = QString::fromLatin1("http://pastebin.ca/") + QString(data).remove("SUCCESS:");
|
||||||
} else
|
} else
|
||||||
link = http.errorString();
|
link = m_http.errorString();
|
||||||
emit pasteDone(link);
|
emit pasteDone(link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,15 +90,15 @@ void PasteBinDotCaProtocol::fetchFinished()
|
|||||||
{
|
{
|
||||||
QString title;
|
QString title;
|
||||||
QString content;
|
QString content;
|
||||||
bool error = reply->error();
|
bool error = m_fetchReply->error();
|
||||||
if (error) {
|
if (error) {
|
||||||
content = reply->errorString();
|
content = m_fetchReply->errorString();
|
||||||
} else {
|
} else {
|
||||||
title = QString::fromLatin1("Pastebin.ca: %1").arg(fetchId);
|
title = QString::fromLatin1("Pastebin.ca: %1").arg(m_fetchId);
|
||||||
content = reply->readAll();
|
content = m_fetchReply->readAll();
|
||||||
}
|
}
|
||||||
reply->deleteLater();
|
m_fetchReply->deleteLater();
|
||||||
reply = 0;
|
m_fetchReply = 0;
|
||||||
emit fetchDone(title, content, error);
|
emit fetchDone(title, content, error);
|
||||||
}
|
}
|
||||||
} // namespace CodePaster
|
} // namespace CodePaster
|
||||||
|
|||||||
@@ -32,15 +32,14 @@
|
|||||||
|
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
#include <QtNetwork/QNetworkAccessManager>
|
|
||||||
#include <QtNetwork/QHttp>
|
#include <QtNetwork/QHttp>
|
||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
class PasteBinDotCaProtocol : public Protocol
|
class PasteBinDotCaProtocol : public NetworkProtocol
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
PasteBinDotCaProtocol();
|
explicit PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw);
|
||||||
QString name() const { return QLatin1String("Pastebin.Ca"); }
|
QString name() const { return QLatin1String("Pastebin.Ca"); }
|
||||||
|
|
||||||
bool hasSettings() const { return false; }
|
bool hasSettings() const { return false; }
|
||||||
@@ -58,12 +57,11 @@ public slots:
|
|||||||
void postRequestFinished(int id, bool error);
|
void postRequestFinished(int id, bool error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager manager;
|
QNetworkReply *m_fetchReply;
|
||||||
QNetworkReply *reply;
|
QString m_fetchId;
|
||||||
QString fetchId;
|
|
||||||
|
|
||||||
QHttp http;
|
QHttp m_http;
|
||||||
int postId;
|
int m_postId;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CodePaster
|
} // namespace CodePaster
|
||||||
|
|||||||
@@ -38,6 +38,7 @@
|
|||||||
#include <QtCore/QTextStream>
|
#include <QtCore/QTextStream>
|
||||||
#include <QtCore/QXmlStreamReader>
|
#include <QtCore/QXmlStreamReader>
|
||||||
#include <QtCore/QXmlStreamAttributes>
|
#include <QtCore/QXmlStreamAttributes>
|
||||||
|
#include <QtCore/QByteArray>
|
||||||
|
|
||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
|
|
||||||
@@ -47,7 +48,8 @@ static const char pastePhpScriptpC[] = "api_public.php";
|
|||||||
static const char fetchPhpScriptpC[] = "raw.php";
|
static const char fetchPhpScriptpC[] = "raw.php";
|
||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
PasteBinDotComProtocol::PasteBinDotComProtocol() :
|
PasteBinDotComProtocol::PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw) :
|
||||||
|
NetworkProtocol(nw),
|
||||||
m_settings(new PasteBinDotComSettings),
|
m_settings(new PasteBinDotComSettings),
|
||||||
m_fetchReply(0),
|
m_fetchReply(0),
|
||||||
m_pasteReply(0),
|
m_pasteReply(0),
|
||||||
@@ -105,31 +107,29 @@ void PasteBinDotComProtocol::paste(const QString &text,
|
|||||||
QTC_ASSERT(!m_pasteReply, return;)
|
QTC_ASSERT(!m_pasteReply, return;)
|
||||||
|
|
||||||
// Format body
|
// Format body
|
||||||
m_pasteData = format(ct);
|
QByteArray pasteData = format(ct);
|
||||||
if (!m_pasteData.isEmpty())
|
if (!pasteData.isEmpty())
|
||||||
m_pasteData.append('&');
|
pasteData.append('&');
|
||||||
m_pasteData += "paste_name=";
|
pasteData += "paste_name=";
|
||||||
m_pasteData += QUrl::toPercentEncoding(username);
|
pasteData += QUrl::toPercentEncoding(username);
|
||||||
|
|
||||||
const QString subDomain = m_settings->hostPrefix();
|
const QString subDomain = m_settings->hostPrefix();
|
||||||
if (!subDomain.isEmpty()) {
|
if (!subDomain.isEmpty()) {
|
||||||
m_pasteData += "&paste_subdomain=";
|
pasteData += "&paste_subdomain=";
|
||||||
m_pasteData += QUrl::toPercentEncoding(subDomain);
|
pasteData += QUrl::toPercentEncoding(subDomain);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_pasteData += "&paste_code=";
|
pasteData += "&paste_code=";
|
||||||
m_pasteData += QUrl::toPercentEncoding(fixNewLines(text));
|
pasteData += QUrl::toPercentEncoding(fixNewLines(text));
|
||||||
|
|
||||||
// fire request
|
// fire request
|
||||||
QString link;
|
QString link;
|
||||||
QTextStream(&link) << "http://" << hostName(false) << '/' << pastePhpScriptpC;
|
QTextStream(&link) << "http://" << hostName(false) << '/' << pastePhpScriptpC;
|
||||||
|
|
||||||
QUrl url(link);
|
m_pasteReply = httpPost(link, pasteData);
|
||||||
QNetworkRequest r(url);
|
|
||||||
m_pasteReply = m_manager.post(r, m_pasteData);
|
|
||||||
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
|
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
|
||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "paste: sending " << m_pasteReply << link << m_pasteData;
|
qDebug() << "paste: sending " << m_pasteReply << link << pasteData;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PasteBinDotComProtocol::pasteFinished()
|
void PasteBinDotComProtocol::pasteFinished()
|
||||||
@@ -164,10 +164,8 @@ void PasteBinDotComProtocol::fetch(const QString &id)
|
|||||||
|
|
||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "fetch: sending " << link;
|
qDebug() << "fetch: sending " << link;
|
||||||
QUrl url(link);
|
|
||||||
QNetworkRequest r(url);
|
|
||||||
|
|
||||||
m_fetchReply = m_manager.get(r);
|
m_fetchReply = httpGet(link);
|
||||||
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
|
||||||
m_fetchId = id;
|
m_fetchId = id;
|
||||||
}
|
}
|
||||||
@@ -212,9 +210,7 @@ void PasteBinDotComProtocol::list()
|
|||||||
QTC_ASSERT(!m_listReply, return;)
|
QTC_ASSERT(!m_listReply, return;)
|
||||||
|
|
||||||
// fire request
|
// fire request
|
||||||
QUrl url(QLatin1String("http://") + hostName(true));
|
m_listReply = httpGet(QLatin1String("http://") + hostName(true));
|
||||||
QNetworkRequest r(url);
|
|
||||||
m_listReply = m_manager.get(r);
|
|
||||||
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
|
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
|
||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "list: sending " << m_listReply;
|
qDebug() << "list: sending " << m_listReply;
|
||||||
|
|||||||
@@ -32,19 +32,14 @@
|
|||||||
|
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
|
|
||||||
#include <QtNetwork/QNetworkAccessManager>
|
|
||||||
#include <QtNetwork/QHttp>
|
|
||||||
|
|
||||||
#include <QtCore/QByteArray>
|
|
||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
class PasteBinDotComSettings;
|
class PasteBinDotComSettings;
|
||||||
|
|
||||||
class PasteBinDotComProtocol : public Protocol
|
class PasteBinDotComProtocol : public NetworkProtocol
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
PasteBinDotComProtocol();
|
explicit PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw);
|
||||||
|
|
||||||
QString name() const { return QLatin1String("Pastebin.Com"); }
|
QString name() const { return QLatin1String("Pastebin.Com"); }
|
||||||
|
|
||||||
@@ -69,11 +64,9 @@ private:
|
|||||||
QString hostName(bool withSubDomain) const;
|
QString hostName(bool withSubDomain) const;
|
||||||
|
|
||||||
PasteBinDotComSettings *m_settings;
|
PasteBinDotComSettings *m_settings;
|
||||||
QNetworkAccessManager m_manager;
|
|
||||||
QNetworkReply *m_fetchReply;
|
QNetworkReply *m_fetchReply;
|
||||||
QNetworkReply *m_pasteReply;
|
QNetworkReply *m_pasteReply;
|
||||||
QNetworkReply *m_listReply;
|
QNetworkReply *m_listReply;
|
||||||
QByteArray m_pasteData;
|
|
||||||
|
|
||||||
QString m_fetchId;
|
QString m_fetchId;
|
||||||
int m_postId;
|
int m_postId;
|
||||||
|
|||||||
@@ -31,6 +31,11 @@
|
|||||||
#include <cpptools/cpptoolsconstants.h>
|
#include <cpptools/cpptoolsconstants.h>
|
||||||
#include <qmljseditor/qmljseditorconstants.h>
|
#include <qmljseditor/qmljseditorconstants.h>
|
||||||
|
|
||||||
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
|
#include <QtNetwork/QNetworkRequest>
|
||||||
|
|
||||||
|
#include <QtCore/QUrl>
|
||||||
|
|
||||||
namespace CodePaster {
|
namespace CodePaster {
|
||||||
|
|
||||||
Protocol::Protocol()
|
Protocol::Protocol()
|
||||||
@@ -108,4 +113,46 @@ QString Protocol::textFromHtml(QString data)
|
|||||||
data.replace(QLatin1String("&"), QString(QLatin1Char('&')));
|
data.replace(QLatin1String("&"), QString(QLatin1Char('&')));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------ NetworkAccessManagerProxy
|
||||||
|
NetworkAccessManagerProxy::NetworkAccessManagerProxy()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NetworkAccessManagerProxy::~NetworkAccessManagerProxy()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkReply *NetworkAccessManagerProxy::httpGet(const QString &link)
|
||||||
|
{
|
||||||
|
QUrl url(link);
|
||||||
|
QNetworkRequest r(url);
|
||||||
|
return networkAccessManager()->get(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkReply *NetworkAccessManagerProxy::httpPost(const QString &link, const QByteArray &data)
|
||||||
|
{
|
||||||
|
QUrl url(link);
|
||||||
|
QNetworkRequest r(url);
|
||||||
|
return networkAccessManager()->post(r, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkAccessManager *NetworkAccessManagerProxy::networkAccessManager()
|
||||||
|
{
|
||||||
|
if (m_networkAccessManager.isNull())
|
||||||
|
m_networkAccessManager.reset(new QNetworkAccessManager);
|
||||||
|
return m_networkAccessManager.data();
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------- NetworkProtocol
|
||||||
|
|
||||||
|
NetworkProtocol::NetworkProtocol(const NetworkAccessManagerProxyPtr &nw) :
|
||||||
|
m_networkAccessManager(nw)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NetworkProtocol::~NetworkProtocol()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
} //namespace CodePaster
|
} //namespace CodePaster
|
||||||
|
|||||||
@@ -31,6 +31,13 @@
|
|||||||
#define PROTOCOL_H
|
#define PROTOCOL_H
|
||||||
|
|
||||||
#include <QtCore/QObject>
|
#include <QtCore/QObject>
|
||||||
|
#include <QtCore/QScopedPointer>
|
||||||
|
#include <QtCore/QSharedPointer>
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
class QNetworkAccessManager;
|
||||||
|
class QNetworkReply;
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
class IOptionsPage;
|
class IOptionsPage;
|
||||||
@@ -50,7 +57,7 @@ public:
|
|||||||
PostCommentCapability = 0x2,
|
PostCommentCapability = 0x2,
|
||||||
PostDescriptionCapability = 0x4
|
PostDescriptionCapability = 0x4
|
||||||
};
|
};
|
||||||
Protocol();
|
|
||||||
virtual ~Protocol();
|
virtual ~Protocol();
|
||||||
|
|
||||||
virtual QString name() const = 0;
|
virtual QString name() const = 0;
|
||||||
@@ -82,9 +89,54 @@ signals:
|
|||||||
void listDone(const QString &name, const QStringList &result);
|
void listDone(const QString &name, const QStringList &result);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
Protocol();
|
||||||
static QString textFromHtml(QString data);
|
static QString textFromHtml(QString data);
|
||||||
static QString fixNewLines(QString in);
|
static QString fixNewLines(QString in);
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Proxy for NetworkAccessManager that can be shared with
|
||||||
|
* delayed initialization and conveniences
|
||||||
|
* for HTTP-requests. */
|
||||||
|
|
||||||
|
class NetworkAccessManagerProxy {
|
||||||
|
Q_DISABLE_COPY(NetworkAccessManagerProxy)
|
||||||
|
public:
|
||||||
|
NetworkAccessManagerProxy();
|
||||||
|
~NetworkAccessManagerProxy();
|
||||||
|
|
||||||
|
QNetworkReply *httpGet(const QString &url);
|
||||||
|
QNetworkReply *httpPost(const QString &link, const QByteArray &data);
|
||||||
|
QNetworkAccessManager *networkAccessManager();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QScopedPointer<QNetworkAccessManager> m_networkAccessManager;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Network-based protocol: Provides access with delayed
|
||||||
|
* initialization to a QNetworkAccessManager and conveniences
|
||||||
|
* for HTTP-requests. */
|
||||||
|
|
||||||
|
class NetworkProtocol : public Protocol {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
virtual ~NetworkProtocol();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
typedef QSharedPointer<NetworkAccessManagerProxy> NetworkAccessManagerProxyPtr;
|
||||||
|
|
||||||
|
explicit NetworkProtocol(const NetworkAccessManagerProxyPtr &nw);
|
||||||
|
|
||||||
|
inline QNetworkReply *httpGet(const QString &url)
|
||||||
|
{ return m_networkAccessManager->httpGet(url); }
|
||||||
|
|
||||||
|
inline QNetworkReply *httpPost(const QString &link, const QByteArray &data)
|
||||||
|
{ return m_networkAccessManager->httpPost(link, data); }
|
||||||
|
|
||||||
|
inline QNetworkAccessManager *networkAccessManager()
|
||||||
|
{ return m_networkAccessManager->networkAccessManager(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const NetworkAccessManagerProxyPtr m_networkAccessManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
} //namespace CodePaster
|
} //namespace CodePaster
|
||||||
|
|||||||
Reference in New Issue
Block a user