diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp index c1953a97d66..7eeb8d847e8 100644 --- a/src/plugins/cpaster/cpasterplugin.cpp +++ b/src/plugins/cpaster/cpasterplugin.cpp @@ -66,19 +66,57 @@ #include #include -using namespace CodePaster; using namespace Core; using namespace TextEditor; +namespace CodePaster { + +/*! + \class CodePaster::CodePasterService + \brief Service registered with PluginManager providing CodePaster + post() functionality. + + \sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke + \sa VCSBase::VCSBaseEditorWidget +*/ + +CodePasterService::CodePasterService(QObject *parent) : + QObject(parent) +{ +} + +void CodePasterService::postText(const QString &text, const QString &mimeType) +{ + QTC_ASSERT(CodepasterPlugin::instance(), return; ) + CodepasterPlugin::instance()->post(text, mimeType); +} + +void CodePasterService::postCurrentEditor() +{ + QTC_ASSERT(CodepasterPlugin::instance(), return; ) + CodepasterPlugin::instance()->postEditor(); +} + +void CodePasterService::postClipboard() +{ + QTC_ASSERT(CodepasterPlugin::instance(), return; ) + CodepasterPlugin::instance()->postClipboard(); +} + +// ---------- CodepasterPlugin +CodepasterPlugin *CodepasterPlugin::m_instance = 0; + CodepasterPlugin::CodepasterPlugin() : m_settings(new Settings), m_postEditorAction(0), m_postClipboardAction(0), m_fetchAction(0) { + CodepasterPlugin::m_instance = this; } CodepasterPlugin::~CodepasterPlugin() { qDeleteAll(m_protocols); + CodepasterPlugin::m_instance = 0; } bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_message) @@ -142,6 +180,8 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m connect(m_fetchAction, SIGNAL(triggered()), this, SLOT(fetch())); cpContainer->addAction(command); + addAutoReleasedObject(new CodePasterService); + return true; } @@ -215,10 +255,14 @@ void CodepasterPlugin::post(QString data, const QString &mimeType) view.setProtocol(m_settings->protocol); const FileDataList diffChunks = splitDiffToFiles(data.toLatin1()); - if (diffChunks.isEmpty()) { - view.show(username, QString(), QString(), data); - } else { + const int dialogResult = diffChunks.isEmpty() ? + view.show(username, QString(), QString(), data) : view.show(username, QString(), QString(), diffChunks); + // Save new protocol in case user changed it. + if (dialogResult == QDialog::Accepted + && m_settings->protocol != view.protocol()) { + m_settings->protocol = view.protocol(); + m_settings->toSettings(Core::ICore::instance()->settings()); } } @@ -229,6 +273,12 @@ void CodepasterPlugin::fetch() if (dialog.exec() != QDialog::Accepted) return; + // Save new protocol in case user changed it. + if (m_settings->protocol != dialog.protocol()) { + m_settings->protocol = dialog.protocol(); + m_settings->toSettings(Core::ICore::instance()->settings()); + } + const QString pasteID = dialog.pasteId(); if (pasteID.isEmpty()) return; @@ -319,4 +369,11 @@ void CodepasterPlugin::finishFetch(const QString &titleDescription, editor->setDisplayName(titleDescription); } -Q_EXPORT_PLUGIN(CodepasterPlugin) +CodepasterPlugin *CodepasterPlugin::instance() +{ + return m_instance; +} + +} // namespace CodePaster + +Q_EXPORT_PLUGIN(CodePaster::CodepasterPlugin) diff --git a/src/plugins/cpaster/cpasterplugin.h b/src/plugins/cpaster/cpasterplugin.h index 887f478ba46..beab63b088e 100644 --- a/src/plugins/cpaster/cpasterplugin.h +++ b/src/plugins/cpaster/cpasterplugin.h @@ -48,6 +48,18 @@ class CustomPoster; struct Settings; class Protocol; +class CodePasterService : public QObject +{ + Q_OBJECT +public: + explicit CodePasterService(QObject *parent = 0); + +public slots: + void postText(const QString &text, const QString &mimeType); + void postCurrentEditor(); + void postClipboard(); +}; + class CodepasterPlugin : public ExtensionSystem::IPlugin { Q_OBJECT @@ -60,6 +72,8 @@ public: virtual void extensionsInitialized(); virtual ShutdownFlag aboutToShutdown(); + static CodepasterPlugin *instance(); + public slots: void postEditor(); void postClipboard(); @@ -69,9 +83,10 @@ public slots: const QString &content, bool error); -private: void post(QString data, const QString &mimeType); +private: + static CodepasterPlugin *m_instance; const QSharedPointer m_settings; QAction *m_postEditorAction; QAction *m_postClipboardAction; diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index 46ffc6d1ac0..9c40b8474e2 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -52,6 +52,8 @@ #include #include #include +#include +#include #include #include @@ -547,6 +549,9 @@ void VCSBaseEditorWidget::contextMenuEvent(QContextMenuEvent *e) break; case DiffOutput: { menu->addSeparator(); + connect(menu->addAction(tr("Send to CodePaster...")), SIGNAL(triggered()), + this, SLOT(slotPaste())); + menu->addSeparator(); QAction *revertAction = menu->addAction(tr("Revert Chunk...")); const DiffChunk chunk = diffChunk(cursorForPosition(e->pos())); revertAction->setEnabled(canRevertDiffChunk(chunk)); @@ -1038,6 +1043,20 @@ QStringList VCSBaseEditorWidget::annotationPreviousVersions(const QString &) con return QStringList(); } +void VCSBaseEditorWidget::slotPaste() +{ + // Retrieve service by soft dependency. + QObject *pasteService = + ExtensionSystem::PluginManager::instance() + ->getObjectByClassName("CodePaster::CodePasterService"); + if (pasteService) { + QMetaObject::invokeMethod(pasteService, "postCurrentEditor"); + } else { + QMessageBox::information(this, tr("Unable to Paste"), + tr("Code pasting services are not available.")); + } +} + bool VCSBaseEditorWidget::isRevertDiffChunkEnabled() const { return d->m_revertChunkEnabled; diff --git a/src/plugins/vcsbase/vcsbaseeditor.h b/src/plugins/vcsbase/vcsbaseeditor.h index 0c9e2468507..da43881a7e7 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.h +++ b/src/plugins/vcsbase/vcsbaseeditor.h @@ -227,6 +227,7 @@ private slots: void slotAnnotateRevision(); void slotCopyRevision(); void slotRevertDiffChunk(); + void slotPaste(); protected: /* A helper that can be used to locate a file in a diff in case it