Commit 338524ba authored by Konstantin Tokarev's avatar Konstantin Tokarev Committed by Tobias Hunger
Browse files

Added "Open URL" action.



It was implemented in CodepasterPlugin because it provides facility
to fetch files from network into editor with proper error reporting.

Change-Id: I1ea7f1799dfa3ca8423a06a64b43b419f23046eb
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent f17ad979
......@@ -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 \
......
......@@ -42,6 +42,7 @@
#include "pasteselectdialog.h"
#include "settingspage.h"
#include "settings.h"
#include "urlopenprotocol.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
......@@ -66,6 +67,8 @@
#include <QClipboard>
#include <QMenu>
#include <QMainWindow>
#include <QInputDialog>
#include <QUrl>
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());
......
......@@ -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<Protocol*> m_protocols;
QStringList m_fetchedSnippets;
Protocol *m_urlOpen;
};
} // namespace CodePaster
......
#include "urlopenprotocol.h"
#include <utils/qtcassert.h>
#include <QFileInfo>
#include <QNetworkReply>
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 &)
{
}
#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
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