Commit 7c3caca0 authored by Friedemann Kleint's avatar Friedemann Kleint
Browse files

CPaster: Introduce Proxy for QNetworkAccessManager

that is shared and provides delayed creation.
Add "Paste clipboard" option.
parent e499d7eb
......@@ -48,7 +48,8 @@ enum { debug = 0 };
namespace CodePaster {
CodePasterProtocol::CodePasterProtocol() :
CodePasterProtocol::CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
m_page(new CodePaster::CodePasterSettingsPage),
m_pasteReply(0),
m_fetchReply(0),
......@@ -98,10 +99,7 @@ void CodePasterProtocol::fetch(const QString &id)
link.append(hostName);
link.append("/?format=raw&id=");
link.append(id);
QUrl url(link);
QNetworkRequest r(url);
m_fetchReply = m_manager.get(r);
m_fetchReply = httpGet(link);
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
m_fetchId = id;
}
......@@ -116,9 +114,7 @@ void CodePasterProtocol::list()
QString link = QLatin1String("http://");
link += hostName;
link += QLatin1String("/?command=browse&format=raw");
QUrl url(link);
QNetworkRequest r(url);
m_listReply = m_manager.get(r);
m_listReply = httpGet(link);
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
}
......@@ -142,9 +138,7 @@ void CodePasterProtocol::paste(const QString &text,
data += "&poster=";
data += CGI::encodeURL(username).toLatin1();
QUrl url(QLatin1String("http://") + hostName);
QNetworkRequest r(url);
m_pasteReply = m_manager.post(r, data);
m_pasteReply = httpPost(QLatin1String("http://") + hostName, data);
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
}
......
......@@ -32,8 +32,6 @@
#include "protocol.h"
#include <QtNetwork/QNetworkAccessManager>
QT_BEGIN_NAMESPACE
class QNetworkReply;
QT_END_NAMESPACE
......@@ -42,11 +40,11 @@ namespace CodePaster {
class CodePasterSettingsPage;
class CodePasterProtocol : public Protocol
class CodePasterProtocol : public NetworkProtocol
{
Q_OBJECT
public:
CodePasterProtocol();
explicit CodePasterProtocol(const NetworkAccessManagerProxyPtr &nw);
~CodePasterProtocol();
QString name() const;
......@@ -70,7 +68,6 @@ public slots:
private:
bool isValidHostName(const QString& hostName);
CodePasterSettingsPage *m_page;
QNetworkAccessManager m_manager;
QNetworkReply *m_pasteReply;
QNetworkReply *m_fetchReply;
QNetworkReply *m_listReply;
......
......@@ -65,7 +65,9 @@ using namespace CodePaster;
using namespace Core;
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);
// Create the protocols and append them to the Settings
Protocol *protos[] = { new CodePasterProtocol(),
new PasteBinDotComProtocol(),
new PasteBinDotCaProtocol(),
const QSharedPointer<NetworkAccessManagerProxy> networkAccessMgrProxy(new NetworkAccessManagerProxy);
Protocol *protos[] = { new CodePasterProtocol(networkAccessMgrProxy),
new PasteBinDotComProtocol(networkAccessMgrProxy),
new PasteBinDotCaProtocol(networkAccessMgrProxy),
0};
for(int i=0; protos[i] != 0; ++i) {
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;
m_postAction = new QAction(tr("Paste Snippet..."), this);
command = actionManager->registerAction(m_postAction, "CodePaster.Post", globalcontext);
m_postEditorAction = new QAction(tr("Paste Snippet..."), this);
command = actionManager->registerAction(m_postEditorAction, "CodePaster.Post", globalcontext);
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);
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)
{
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);
FileDataList lst = splitDiffToFiles(data.toLatin1());
QString username = m_settings->username;
......@@ -199,7 +219,7 @@ void CodepasterPlugin::post()
protocolName = view.protocol();
foreach(Protocol *protocol, m_protocols) {
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);
break;
}
......
......@@ -58,7 +58,8 @@ public:
virtual void shutdown();
public slots:
void post();
void postEditor();
void postClipboard();
void fetch();
void finishPost(const QString &link);
void finishFetch(const QString &titleDescription,
......@@ -66,8 +67,11 @@ public slots:
bool error);
private:
void post(QString data, const QString &mimeType);
const QSharedPointer<Settings> m_settings;
QAction *m_postAction;
QAction *m_postEditorAction;
QAction *m_postClipboardAction;
QAction *m_fetchAction;
QList<Protocol*> m_protocols;
QStringList m_fetchedSnippets;
......
......@@ -32,11 +32,13 @@
#include <QtNetwork/QNetworkReply>
using namespace Core;
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)));
}
......@@ -44,12 +46,9 @@ void PasteBinDotCaProtocol::fetch(const QString &id)
{
QString link = QLatin1String("http://pastebin.ca/raw/");
link.append(id);
QUrl url(link);
QNetworkRequest r(url);
reply = manager.get(r);
connect(reply, SIGNAL(finished()), this, SLOT(fetchFinished()));
fetchId = id;
m_fetchReply = httpGet(link);
connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished()));
m_fetchId = id;
}
void PasteBinDotCaProtocol::paste(const QString &text,
......@@ -69,20 +68,20 @@ void PasteBinDotCaProtocol::paste(const QString &text,
QHttpRequestHeader header("POST", "/quiet-paste.php");
header.setValue("host", "pastebin.ca" );
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");
postId = http.request(header, data.toAscii());
m_postId = m_http.request(header, data.toAscii());
}
void PasteBinDotCaProtocol::postRequestFinished(int id, bool error)
{
QString link;
if (id == postId) {
if (id == m_postId) {
if (!error) {
QByteArray data = http.readAll();
QByteArray data = m_http.readAll();
link = QString::fromLatin1("http://pastebin.ca/") + QString(data).remove("SUCCESS:");
} else
link = http.errorString();
link = m_http.errorString();
emit pasteDone(link);
}
}
......@@ -91,15 +90,15 @@ void PasteBinDotCaProtocol::fetchFinished()
{
QString title;
QString content;
bool error = reply->error();
bool error = m_fetchReply->error();
if (error) {
content = reply->errorString();
content = m_fetchReply->errorString();
} else {
title = QString::fromLatin1("Pastebin.ca: %1").arg(fetchId);
content = reply->readAll();
title = QString::fromLatin1("Pastebin.ca: %1").arg(m_fetchId);
content = m_fetchReply->readAll();
}
reply->deleteLater();
reply = 0;
m_fetchReply->deleteLater();
m_fetchReply = 0;
emit fetchDone(title, content, error);
}
} // namespace CodePaster
......@@ -32,15 +32,14 @@
#include "protocol.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QHttp>
namespace CodePaster {
class PasteBinDotCaProtocol : public Protocol
class PasteBinDotCaProtocol : public NetworkProtocol
{
Q_OBJECT
public:
PasteBinDotCaProtocol();
explicit PasteBinDotCaProtocol(const NetworkAccessManagerProxyPtr &nw);
QString name() const { return QLatin1String("Pastebin.Ca"); }
bool hasSettings() const { return false; }
......@@ -58,12 +57,11 @@ public slots:
void postRequestFinished(int id, bool error);
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
QString fetchId;
QNetworkReply *m_fetchReply;
QString m_fetchId;
QHttp http;
int postId;
QHttp m_http;
int m_postId;
};
} // namespace CodePaster
......
......@@ -38,6 +38,7 @@
#include <QtCore/QTextStream>
#include <QtCore/QXmlStreamReader>
#include <QtCore/QXmlStreamAttributes>
#include <QtCore/QByteArray>
#include <QtNetwork/QNetworkReply>
......@@ -47,7 +48,8 @@ static const char pastePhpScriptpC[] = "api_public.php";
static const char fetchPhpScriptpC[] = "raw.php";
namespace CodePaster {
PasteBinDotComProtocol::PasteBinDotComProtocol() :
PasteBinDotComProtocol::PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw) :
NetworkProtocol(nw),
m_settings(new PasteBinDotComSettings),
m_fetchReply(0),
m_pasteReply(0),
......@@ -105,31 +107,29 @@ void PasteBinDotComProtocol::paste(const QString &text,
QTC_ASSERT(!m_pasteReply, return;)
// Format body
m_pasteData = format(ct);
if (!m_pasteData.isEmpty())
m_pasteData.append('&');
m_pasteData += "paste_name=";
m_pasteData += QUrl::toPercentEncoding(username);
QByteArray pasteData = format(ct);
if (!pasteData.isEmpty())
pasteData.append('&');
pasteData += "paste_name=";
pasteData += QUrl::toPercentEncoding(username);
const QString subDomain = m_settings->hostPrefix();
if (!subDomain.isEmpty()) {
m_pasteData += "&paste_subdomain=";
m_pasteData += QUrl::toPercentEncoding(subDomain);
pasteData += "&paste_subdomain=";
pasteData += QUrl::toPercentEncoding(subDomain);
}
m_pasteData += "&paste_code=";
m_pasteData += QUrl::toPercentEncoding(fixNewLines(text));
pasteData += "&paste_code=";
pasteData += QUrl::toPercentEncoding(fixNewLines(text));
// fire request
QString link;
QTextStream(&link) << "http://" << hostName(false) << '/' << pastePhpScriptpC;
QUrl url(link);
QNetworkRequest r(url);
m_pasteReply = m_manager.post(r, m_pasteData);
m_pasteReply = httpPost(link, pasteData);
connect(m_pasteReply, SIGNAL(finished()), this, SLOT(pasteFinished()));
if (debug)
qDebug() << "paste: sending " << m_pasteReply << link << m_pasteData;
qDebug() << "paste: sending " << m_pasteReply << link << pasteData;
}
void PasteBinDotComProtocol::pasteFinished()
......@@ -164,10 +164,8 @@ void PasteBinDotComProtocol::fetch(const QString &id)
if (debug)
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()));
m_fetchId = id;
}
......@@ -212,9 +210,7 @@ void PasteBinDotComProtocol::list()
QTC_ASSERT(!m_listReply, return;)
// fire request
QUrl url(QLatin1String("http://") + hostName(true));
QNetworkRequest r(url);
m_listReply = m_manager.get(r);
m_listReply = httpGet(QLatin1String("http://") + hostName(true));
connect(m_listReply, SIGNAL(finished()), this, SLOT(listFinished()));
if (debug)
qDebug() << "list: sending " << m_listReply;
......
......@@ -32,19 +32,14 @@
#include "protocol.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QHttp>
#include <QtCore/QByteArray>
namespace CodePaster {
class PasteBinDotComSettings;
class PasteBinDotComProtocol : public Protocol
class PasteBinDotComProtocol : public NetworkProtocol
{
Q_OBJECT
public:
PasteBinDotComProtocol();
explicit PasteBinDotComProtocol(const NetworkAccessManagerProxyPtr &nw);
QString name() const { return QLatin1String("Pastebin.Com"); }
......@@ -69,11 +64,9 @@ private:
QString hostName(bool withSubDomain) const;
PasteBinDotComSettings *m_settings;
QNetworkAccessManager m_manager;
QNetworkReply *m_fetchReply;
QNetworkReply *m_pasteReply;
QNetworkReply *m_listReply;
QByteArray m_pasteData;
QString m_fetchId;
int m_postId;
......
......@@ -31,6 +31,11 @@
#include <cpptools/cpptoolsconstants.h>
#include <qmljseditor/qmljseditorconstants.h>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtCore/QUrl>
namespace CodePaster {
Protocol::Protocol()
......@@ -108,4 +113,46 @@ QString Protocol::textFromHtml(QString data)
data.replace(QLatin1String("&amp;"), QString(QLatin1Char('&')));
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
......@@ -31,6 +31,13 @@
#define PROTOCOL_H
#include <QtCore/QObject>
#include <QtCore/QScopedPointer>
#include <QtCore/QSharedPointer>
QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
QT_END_NAMESPACE
namespace Core {
class IOptionsPage;
......@@ -50,7 +57,7 @@ public:
PostCommentCapability = 0x2,
PostDescriptionCapability = 0x4
};
Protocol();
virtual ~Protocol();
virtual QString name() const = 0;
......@@ -82,9 +89,54 @@ signals:
void listDone(const QString &name, const QStringList &result);
protected:
Protocol();
static QString textFromHtml(QString data);
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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment