diff --git a/src/plugins/cpaster/cpaster.pro b/src/plugins/cpaster/cpaster.pro index 407075b1df9..a06c2c74d58 100644 --- a/src/plugins/cpaster/cpaster.pro +++ b/src/plugins/cpaster/cpaster.pro @@ -18,7 +18,8 @@ HEADERS += cpasterplugin.h \ columnindicatortextedit.h \ fileshareprotocol.h \ fileshareprotocolsettingspage.h \ - kdepasteprotocol.h + kdepasteprotocol.h \ + urlopenprotocol.h SOURCES += cpasterplugin.cpp \ settingspage.cpp \ @@ -33,7 +34,8 @@ SOURCES += cpasterplugin.cpp \ columnindicatortextedit.cpp \ fileshareprotocol.cpp \ fileshareprotocolsettingspage.cpp \ - kdepasteprotocol.cpp + kdepasteprotocol.cpp \ + urlopenprotocol.cpp FORMS += settingspage.ui \ pasteselect.ui \ diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp index 0f3e93607fc..ef5e5fb69d7 100644 --- a/src/plugins/cpaster/cpasterplugin.cpp +++ b/src/plugins/cpaster/cpasterplugin.cpp @@ -42,6 +42,7 @@ #include "pasteselectdialog.h" #include "settingspage.h" #include "settings.h" +#include "urlopenprotocol.h" #include #include @@ -66,6 +67,8 @@ #include #include #include +#include +#include using namespace Core; using namespace TextEditor; @@ -152,6 +155,10 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe m_protocols.append(protos[i]); } + m_urlOpen = new UrlOpenProtocol(networkAccessMgrProxy); + connect(m_urlOpen, SIGNAL(fetchDone(QString,QString,bool)), + this, SLOT(finishFetch(QString,QString,bool))); + //register actions Core::ActionManager *actionManager = ICore::actionManager(); @@ -182,6 +189,11 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe connect(m_fetchAction, SIGNAL(triggered()), this, SLOT(fetch())); cpContainer->addAction(command); + m_fetchUrlAction = new QAction(tr("Fetch from URL..."), this); + command = actionManager->registerAction(m_fetchUrlAction, "CodePaster.FetchUrl", globalcontext); + connect(m_fetchUrlAction, SIGNAL(triggered()), this, SLOT(fetchUrl())); + cpContainer->addAction(command); + addAutoReleasedObject(new CodePasterService); return true; @@ -268,6 +280,18 @@ void CodepasterPlugin::post(QString data, const QString &mimeType) } } +void CodepasterPlugin::fetchUrl() +{ + QUrl url; + do { + bool ok = true; + url = QUrl(QInputDialog::getText(0, tr("Fetch from URL"), tr("Enter URL:"), QLineEdit::Normal, QString(), &ok)); + if (!ok) + return; + } while (!url.isValid()); + m_urlOpen->fetch(url.toString()); +} + void CodepasterPlugin::fetch() { PasteSelectDialog dialog(m_protocols, ICore::mainWindow()); diff --git a/src/plugins/cpaster/cpasterplugin.h b/src/plugins/cpaster/cpasterplugin.h index f7c93526b53..1c0cba8a0c3 100644 --- a/src/plugins/cpaster/cpasterplugin.h +++ b/src/plugins/cpaster/cpasterplugin.h @@ -85,6 +85,7 @@ public slots: bool error); void post(QString data, const QString &mimeType); + void fetchUrl(); private: static CodepasterPlugin *m_instance; @@ -92,8 +93,10 @@ private: QAction *m_postEditorAction; QAction *m_postClipboardAction; QAction *m_fetchAction; + QAction *m_fetchUrlAction; QList m_protocols; QStringList m_fetchedSnippets; + Protocol *m_urlOpen; }; } // namespace CodePaster diff --git a/src/plugins/cpaster/urlopenprotocol.cpp b/src/plugins/cpaster/urlopenprotocol.cpp new file mode 100644 index 00000000000..710c60b3012 --- /dev/null +++ b/src/plugins/cpaster/urlopenprotocol.cpp @@ -0,0 +1,50 @@ +#include "urlopenprotocol.h" + +#include + +#include +#include + +using namespace CodePaster; + +UrlOpenProtocol::UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw) + : NetworkProtocol(nw), m_fetchReply(0) +{ +} + +QString UrlOpenProtocol::name() const +{ + return QLatin1String("Open URL"); // unused +} + +unsigned UrlOpenProtocol::capabilities() const +{ + return 0; +} + +void UrlOpenProtocol::fetch(const QString &url) +{ + QTC_ASSERT(!m_fetchReply, return); + m_fetchReply = httpGet(url); + connect(m_fetchReply, SIGNAL(finished()), this, SLOT(fetchFinished())); +} + +void UrlOpenProtocol::fetchFinished() +{ + const QString title = m_fetchReply->url().toString(); + QString content; + const bool error = m_fetchReply->error(); + if (error) { + content = m_fetchReply->errorString(); + } else { + content = QString::fromUtf8(m_fetchReply->readAll()); + } + m_fetchReply->deleteLater(); + m_fetchReply = 0; + emit fetchDone(title, content, error); +} + +void UrlOpenProtocol::paste(const QString &, ContentType, const QString &, + const QString &, const QString &) +{ +} diff --git a/src/plugins/cpaster/urlopenprotocol.h b/src/plugins/cpaster/urlopenprotocol.h new file mode 100644 index 00000000000..046bc863668 --- /dev/null +++ b/src/plugins/cpaster/urlopenprotocol.h @@ -0,0 +1,28 @@ +#ifndef SIMPLENETWORKPROTOCOL_H +#define SIMPLENETWORKPROTOCOL_H + +#include "protocol.h" + +namespace CodePaster { + +class UrlOpenProtocol : public NetworkProtocol +{ + Q_OBJECT +public: + UrlOpenProtocol(const NetworkAccessManagerProxyPtr &nw); + + QString name() const; + unsigned capabilities() const; + void fetch(const QString &url); + void paste(const QString &, ContentType, const QString &, const QString &, const QString &); + +private slots: + void fetchFinished(); + +private: + QNetworkReply *m_fetchReply; +}; + +} + +#endif // SIMPLENETWORKPROTOCOL_H