From ce96a8b80ade646b77594deaee893eab6ab92897 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 29 Jun 2015 14:22:17 +0200 Subject: CodePaster: Register type safe service object Use Q_DECLARE_INTERFACE et al. This provides a way to have the dependency on code pasting optional in the using plugins (VCS, diff editor), while still being able to use a nice API to perform the pasting itself. Change-Id: Ia61e0066d552e45031f4aa7fd1f6693b68f92384 Reviewed-by: Christian Kandeler Reviewed-by: Tobias Hunger Reviewed-by: Friedemann Kleint --- src/plugins/cpaster/codepasterservice.h | 57 ++++++++++++++++++++++ src/plugins/cpaster/cpaster.pro | 3 +- src/plugins/cpaster/cpasterplugin.cpp | 17 +++---- src/plugins/cpaster/cpasterplugin.h | 13 +++-- src/plugins/diffeditor/diffeditor.qbs | 4 ++ src/plugins/diffeditor/diffeditor_dependencies.pri | 2 + .../diffeditor/sidebysidediffeditorwidget.cpp | 45 +++++++++-------- src/plugins/diffeditor/unifieddiffeditorwidget.cpp | 31 ++++++------ src/plugins/vcsbase/vcsbase.qbs | 4 ++ src/plugins/vcsbase/vcsbase_dependencies.pri | 2 + src/plugins/vcsbase/vcsbaseeditor.cpp | 21 ++++---- 11 files changed, 135 insertions(+), 64 deletions(-) create mode 100644 src/plugins/cpaster/codepasterservice.h (limited to 'src') diff --git a/src/plugins/cpaster/codepasterservice.h b/src/plugins/cpaster/codepasterservice.h new file mode 100644 index 0000000000..b8a0c43848 --- /dev/null +++ b/src/plugins/cpaster/codepasterservice.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + + +#ifndef CODEPASTERSERVICE_H +#define CODEPASTERSERVICE_H + +#include +#include + +namespace CodePaster { + +class Service +{ +public: + virtual ~Service() {} + + virtual void postText(const QString &text, const QString &mimeType) = 0; + virtual void postCurrentEditor() = 0; + virtual void postClipboard() = 0; +}; + +} // CodePaster + +QT_BEGIN_NAMESPACE +Q_DECLARE_INTERFACE(CodePaster::Service, "CodePaster::Service") +QT_END_NAMESPACE + +#endif // CODEPASTERSERVICE_H + diff --git a/src/plugins/cpaster/cpaster.pro b/src/plugins/cpaster/cpaster.pro index 5bdc8e2545..cc30094162 100644 --- a/src/plugins/cpaster/cpaster.pro +++ b/src/plugins/cpaster/cpaster.pro @@ -13,7 +13,8 @@ HEADERS += cpasterplugin.h \ fileshareprotocol.h \ fileshareprotocolsettingspage.h \ kdepasteprotocol.h \ - urlopenprotocol.h + urlopenprotocol.h \ + codepasterservice.h SOURCES += cpasterplugin.cpp \ settingspage.cpp \ diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp index 462d949ea4..e51b533777 100644 --- a/src/plugins/cpaster/cpasterplugin.cpp +++ b/src/plugins/cpaster/cpasterplugin.cpp @@ -70,32 +70,29 @@ using namespace TextEditor; namespace CodePaster { /*! - \class CodePaster::CodePasterService - \brief The CodePasterService class is a service registered with PluginManager + \class CodePaster::Service + \brief The CodePaster::Service class is a service registered with PluginManager that provides CodePaster \c post() functionality. - - \sa ExtensionSystem::PluginManager::getObjectByClassName, ExtensionSystem::invoke - \sa VcsBase::VcsBaseEditorWidget */ -CodePasterService::CodePasterService(QObject *parent) : +CodePasterServiceImpl::CodePasterServiceImpl(QObject *parent) : QObject(parent) { } -void CodePasterService::postText(const QString &text, const QString &mimeType) +void CodePasterServiceImpl::postText(const QString &text, const QString &mimeType) { QTC_ASSERT(CodepasterPlugin::instance(), return); CodepasterPlugin::instance()->post(text, mimeType); } -void CodePasterService::postCurrentEditor() +void CodePasterServiceImpl::postCurrentEditor() { QTC_ASSERT(CodepasterPlugin::instance(), return); CodepasterPlugin::instance()->post(CodepasterPlugin::PasteEditor); } -void CodePasterService::postClipboard() +void CodePasterServiceImpl::postClipboard() { QTC_ASSERT(CodepasterPlugin::instance(), return); CodepasterPlugin::instance()->post(CodepasterPlugin::PasteClipboard); @@ -176,7 +173,7 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *errorMe connect(m_fetchUrlAction, &QAction::triggered, this, &CodepasterPlugin::fetchUrl); cpContainer->addAction(command); - addAutoReleasedObject(new CodePasterService); + addAutoReleasedObject(new CodePasterServiceImpl); return true; } diff --git a/src/plugins/cpaster/cpasterplugin.h b/src/plugins/cpaster/cpasterplugin.h index 632503543a..c764b28df3 100644 --- a/src/plugins/cpaster/cpasterplugin.h +++ b/src/plugins/cpaster/cpasterplugin.h @@ -31,6 +31,8 @@ #ifndef CPASTERPLUGIN_H #define CPASTERPLUGIN_H +#include "codepasterservice.h" + #include #include @@ -46,16 +48,17 @@ class CustomPoster; struct Settings; class Protocol; -class CodePasterService : public QObject +class CodePasterServiceImpl : public QObject, public CodePaster::Service { Q_OBJECT + Q_INTERFACES(CodePaster::Service) public: - explicit CodePasterService(QObject *parent = 0); + explicit CodePasterServiceImpl(QObject *parent = 0); public slots: - void postText(const QString &text, const QString &mimeType); - void postCurrentEditor(); - void postClipboard(); + void postText(const QString &text, const QString &mimeType) override; + void postCurrentEditor() override; + void postClipboard() override; }; class CodepasterPlugin : public ExtensionSystem::IPlugin diff --git a/src/plugins/diffeditor/diffeditor.qbs b/src/plugins/diffeditor/diffeditor.qbs index 8a23861f95..d7266c7dfc 100644 --- a/src/plugins/diffeditor/diffeditor.qbs +++ b/src/plugins/diffeditor/diffeditor.qbs @@ -9,6 +9,10 @@ QtcPlugin { Depends { name: "Core" } Depends { name: "TextEditor" } + pluginRecommends: [ + "CodePaster" + ] + files: [ "diffeditor.cpp", "diffeditor.h", diff --git a/src/plugins/diffeditor/diffeditor_dependencies.pri b/src/plugins/diffeditor/diffeditor_dependencies.pri index 0458f03fbe..abe8944152 100644 --- a/src/plugins/diffeditor/diffeditor_dependencies.pri +++ b/src/plugins/diffeditor/diffeditor_dependencies.pri @@ -5,3 +5,5 @@ QTC_LIB_DEPENDS += \ QTC_PLUGIN_DEPENDS += \ texteditor \ coreplugin +QTC_PLUGIN_RECOMMENDS += \ + cpaster diff --git a/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp b/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp index ce218a52fb..9a5fc0abc8 100644 --- a/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp +++ b/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp @@ -56,8 +56,11 @@ #include #include +#include + #include +#include #include using namespace Core; @@ -894,11 +897,14 @@ void SideBySideDiffEditorWidget::slotLeftContextMenuRequested(QMenu *menu, int chunkIndex) { menu->addSeparator(); - QAction *sendChunkToCodePasterAction = - menu->addAction(tr("Send Chunk to CodePaster...")); - connect(sendChunkToCodePasterAction, &QAction::triggered, - this, &SideBySideDiffEditorWidget::slotSendChunkToCodePaster); - menu->addSeparator(); + if (ExtensionSystem::PluginManager::getObject()) { + // optional code pasting service + QAction *sendChunkToCodePasterAction = + menu->addAction(tr("Send Chunk to CodePaster...")); + connect(sendChunkToCodePasterAction, &QAction::triggered, + this, &SideBySideDiffEditorWidget::slotSendChunkToCodePaster); + menu->addSeparator(); + } QAction *applyAction = menu->addAction(tr("Apply Chunk...")); connect(applyAction, &QAction::triggered, this, &SideBySideDiffEditorWidget::slotApplyChunk); applyAction->setEnabled(false); @@ -929,11 +935,14 @@ void SideBySideDiffEditorWidget::slotRightContextMenuRequested(QMenu *menu, int chunkIndex) { menu->addSeparator(); - QAction *sendChunkToCodePasterAction = - menu->addAction(tr("Send Chunk to CodePaster...")); - connect(sendChunkToCodePasterAction, &QAction::triggered, - this, &SideBySideDiffEditorWidget::slotSendChunkToCodePaster); - menu->addSeparator(); + if (ExtensionSystem::PluginManager::getObject()) { + // optional code pasting service + QAction *sendChunkToCodePasterAction = + menu->addAction(tr("Send Chunk to CodePaster...")); + connect(sendChunkToCodePasterAction, &QAction::triggered, + this, &SideBySideDiffEditorWidget::slotSendChunkToCodePaster); + menu->addSeparator(); + } QAction *revertAction = menu->addAction(tr("Revert Chunk...")); connect(revertAction, &QAction::triggered, this, &SideBySideDiffEditorWidget::slotRevertChunk); revertAction->setEnabled(false); @@ -961,21 +970,15 @@ void SideBySideDiffEditorWidget::slotSendChunkToCodePaster() if (!m_document) return; + // Retrieve service by soft dependency. + auto pasteService = ExtensionSystem::PluginManager::getObject(); + QTC_ASSERT(pasteService, return); + const QString patch = m_document->makePatch(m_contextMenuFileIndex, m_contextMenuChunkIndex, false); if (patch.isEmpty()) return; - // Retrieve service by soft dependency. - QObject *pasteService - = ExtensionSystem::PluginManager::getObjectByClassName(QLatin1String("CodePaster::CodePasterService")); - if (pasteService) { - QMetaObject::invokeMethod(pasteService, "postText", - Q_ARG(QString, patch), - Q_ARG(QString, QLatin1String(DiffEditor::Constants::DIFF_EDITOR_MIMETYPE))); - } else { - QMessageBox::information(this, tr("Unable to Paste"), - tr("Code pasting services are not available.")); - } + pasteService->postText(patch, QLatin1String(Constants::DIFF_EDITOR_MIMETYPE)); } void SideBySideDiffEditorWidget::slotApplyChunk() diff --git a/src/plugins/diffeditor/unifieddiffeditorwidget.cpp b/src/plugins/diffeditor/unifieddiffeditorwidget.cpp index fbc3a22064..3810a4052c 100644 --- a/src/plugins/diffeditor/unifieddiffeditorwidget.cpp +++ b/src/plugins/diffeditor/unifieddiffeditorwidget.cpp @@ -54,8 +54,11 @@ #include #include +#include + #include +#include #include //static const int FILE_LEVEL = 1; @@ -189,10 +192,13 @@ void UnifiedDiffEditorWidget::addContextMenuActions(QMenu *menu, menu->addSeparator(); menu->addSeparator(); - QAction *sendChunkToCodePasterAction = - menu->addAction(tr("Send Chunk to CodePaster...")); - connect(sendChunkToCodePasterAction, &QAction::triggered, - this, &UnifiedDiffEditorWidget::slotSendChunkToCodePaster); + if (ExtensionSystem::PluginManager::getObject()) { + // optional code pasting service + QAction *sendChunkToCodePasterAction = + menu->addAction(tr("Send Chunk to CodePaster...")); + connect(sendChunkToCodePasterAction, &QAction::triggered, + this, &UnifiedDiffEditorWidget::slotSendChunkToCodePaster); + } QAction *applyAction = menu->addAction(tr("Apply Chunk...")); connect(applyAction, &QAction::triggered, this, &UnifiedDiffEditorWidget::slotApplyChunk); QAction *revertAction = menu->addAction(tr("Revert Chunk...")); @@ -229,23 +235,16 @@ void UnifiedDiffEditorWidget::slotSendChunkToCodePaster() if (!m_document) return; + // Retrieve service by soft dependency. + auto pasteService = ExtensionSystem::PluginManager::getObject(); + QTC_ASSERT(pasteService, return); + const QString patch = m_document->makePatch(m_contextMenuFileIndex, m_contextMenuChunkIndex, false); if (patch.isEmpty()) return; - // Retrieve service by soft dependency. - QObject *pasteService = - ExtensionSystem::PluginManager::getObjectByClassName( - QLatin1String("CodePaster::CodePasterService")); - if (pasteService) { - QMetaObject::invokeMethod(pasteService, "postText", - Q_ARG(QString, patch), - Q_ARG(QString, QLatin1String(DiffEditor::Constants::DIFF_EDITOR_MIMETYPE))); - } else { - QMessageBox::information(this, tr("Unable to Paste"), - tr("Code pasting services are not available.")); - } + pasteService->postText(patch, QLatin1String(Constants::DIFF_EDITOR_MIMETYPE)); } void UnifiedDiffEditorWidget::slotApplyChunk() diff --git a/src/plugins/vcsbase/vcsbase.qbs b/src/plugins/vcsbase/vcsbase.qbs index d278208224..857577a46a 100644 --- a/src/plugins/vcsbase/vcsbase.qbs +++ b/src/plugins/vcsbase/vcsbase.qbs @@ -13,6 +13,10 @@ QtcPlugin { Depends { name: "ProjectExplorer" } Depends { name: "CppTools" } + pluginRecommends: [ + "CodePaster" + ] + files: [ "baseannotationhighlighter.cpp", "baseannotationhighlighter.h", diff --git a/src/plugins/vcsbase/vcsbase_dependencies.pri b/src/plugins/vcsbase/vcsbase_dependencies.pri index 95a11af074..bff0f1dcf4 100644 --- a/src/plugins/vcsbase/vcsbase_dependencies.pri +++ b/src/plugins/vcsbase/vcsbase_dependencies.pri @@ -9,3 +9,5 @@ QTC_PLUGIN_DEPENDS += \ texteditor \ projectexplorer \ cpptools +QTC_PLUGIN_RECOMMENDS += \ + cpaster diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index 8fe872caed..b5f3a848f4 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -953,9 +954,12 @@ void VcsBaseEditorWidget::contextMenuEvent(QContextMenuEvent *e) switch (d->m_parameters->type) { case LogOutput: // log might have diff case DiffOutput: { - menu->addSeparator(); - connect(menu->addAction(tr("Send to CodePaster...")), &QAction::triggered, - this, &VcsBaseEditorWidget::slotPaste); + if (ExtensionSystem::PluginManager::getObject()) { + // optional code pasting service + menu->addSeparator(); + connect(menu->addAction(tr("Send to CodePaster...")), &QAction::triggered, + this, &VcsBaseEditorWidget::slotPaste); + } menu->addSeparator(); // Apply/revert diff chunk. const DiffChunk chunk = diffChunk(cursorForPosition(e->pos())); @@ -1472,14 +1476,9 @@ QStringList VcsBaseEditorWidget::annotationPreviousVersions(const QString &) con void VcsBaseEditorWidget::slotPaste() { // Retrieve service by soft dependency. - QObject *pasteService = - ExtensionSystem::PluginManager::getObjectByClassName(QLatin1String("CodePaster::CodePasterService")); - if (pasteService) { - QMetaObject::invokeMethod(pasteService, "postCurrentEditor"); - } else { - QMessageBox::information(this, tr("Unable to Paste"), - tr("Code pasting services are not available.")); - } + auto pasteService = ExtensionSystem::PluginManager::getObject(); + QTC_ASSERT(pasteService, return); + pasteService->postCurrentEditor(); } void VcsBaseEditorWidget::showProgressIndicator() -- cgit v1.2.1