forked from qt-creator/qt-creator
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: Tobias Hunger <tobias.hunger@nokia.com>
This commit is contained in:
committed by
Tobias Hunger
parent
f17ad97997
commit
338524baab
@@ -18,7 +18,8 @@ HEADERS += cpasterplugin.h \
|
|||||||
columnindicatortextedit.h \
|
columnindicatortextedit.h \
|
||||||
fileshareprotocol.h \
|
fileshareprotocol.h \
|
||||||
fileshareprotocolsettingspage.h \
|
fileshareprotocolsettingspage.h \
|
||||||
kdepasteprotocol.h
|
kdepasteprotocol.h \
|
||||||
|
urlopenprotocol.h
|
||||||
|
|
||||||
SOURCES += cpasterplugin.cpp \
|
SOURCES += cpasterplugin.cpp \
|
||||||
settingspage.cpp \
|
settingspage.cpp \
|
||||||
@@ -33,7 +34,8 @@ SOURCES += cpasterplugin.cpp \
|
|||||||
columnindicatortextedit.cpp \
|
columnindicatortextedit.cpp \
|
||||||
fileshareprotocol.cpp \
|
fileshareprotocol.cpp \
|
||||||
fileshareprotocolsettingspage.cpp \
|
fileshareprotocolsettingspage.cpp \
|
||||||
kdepasteprotocol.cpp
|
kdepasteprotocol.cpp \
|
||||||
|
urlopenprotocol.cpp
|
||||||
|
|
||||||
FORMS += settingspage.ui \
|
FORMS += settingspage.ui \
|
||||||
pasteselect.ui \
|
pasteselect.ui \
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
#include "pasteselectdialog.h"
|
#include "pasteselectdialog.h"
|
||||||
#include "settingspage.h"
|
#include "settingspage.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "urlopenprotocol.h"
|
||||||
|
|
||||||
#include <coreplugin/actionmanager/actionmanager.h>
|
#include <coreplugin/actionmanager/actionmanager.h>
|
||||||
#include <coreplugin/actionmanager/actioncontainer.h>
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
||||||
@@ -66,6 +67,8 @@
|
|||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QInputDialog>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
@@ -152,6 +155,10 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe
|
|||||||
m_protocols.append(protos[i]);
|
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
|
//register actions
|
||||||
Core::ActionManager *actionManager = ICore::actionManager();
|
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()));
|
connect(m_fetchAction, SIGNAL(triggered()), this, SLOT(fetch()));
|
||||||
cpContainer->addAction(command);
|
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);
|
addAutoReleasedObject(new CodePasterService);
|
||||||
|
|
||||||
return true;
|
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()
|
void CodepasterPlugin::fetch()
|
||||||
{
|
{
|
||||||
PasteSelectDialog dialog(m_protocols, ICore::mainWindow());
|
PasteSelectDialog dialog(m_protocols, ICore::mainWindow());
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ public slots:
|
|||||||
bool error);
|
bool error);
|
||||||
|
|
||||||
void post(QString data, const QString &mimeType);
|
void post(QString data, const QString &mimeType);
|
||||||
|
void fetchUrl();
|
||||||
private:
|
private:
|
||||||
|
|
||||||
static CodepasterPlugin *m_instance;
|
static CodepasterPlugin *m_instance;
|
||||||
@@ -92,8 +93,10 @@ private:
|
|||||||
QAction *m_postEditorAction;
|
QAction *m_postEditorAction;
|
||||||
QAction *m_postClipboardAction;
|
QAction *m_postClipboardAction;
|
||||||
QAction *m_fetchAction;
|
QAction *m_fetchAction;
|
||||||
|
QAction *m_fetchUrlAction;
|
||||||
QList<Protocol*> m_protocols;
|
QList<Protocol*> m_protocols;
|
||||||
QStringList m_fetchedSnippets;
|
QStringList m_fetchedSnippets;
|
||||||
|
Protocol *m_urlOpen;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CodePaster
|
} // namespace CodePaster
|
||||||
|
|||||||
50
src/plugins/cpaster/urlopenprotocol.cpp
Normal file
50
src/plugins/cpaster/urlopenprotocol.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#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 &)
|
||||||
|
{
|
||||||
|
}
|
||||||
28
src/plugins/cpaster/urlopenprotocol.h
Normal file
28
src/plugins/cpaster/urlopenprotocol.h
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user