OSDN Git Service

CodePaster: Provide service for soft-dependent plugins.
authorFriedemann Kleint <Friedemann.Kleint@nokia.com>
Thu, 21 Apr 2011 13:21:50 +0000 (15:21 +0200)
committerFriedemann Kleint <Friedemann.Kleint@nokia.com>
Thu, 21 Apr 2011 13:21:50 +0000 (15:21 +0200)
Offer 'Paste' in VCSBase-Diff-Editor context menu.
Make new protocol the default one if user changes it
in view/paste dialogs.

src/plugins/cpaster/cpasterplugin.cpp
src/plugins/cpaster/cpasterplugin.h
src/plugins/vcsbase/vcsbaseeditor.cpp
src/plugins/vcsbase/vcsbaseeditor.h

index c1953a9..7eeb8d8 100644 (file)
 #include <QtGui/QMenu>
 #include <QtGui/QMainWindow>
 
-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)
index 887f478..beab63b 100644 (file)
@@ -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<Settings> m_settings;
     QAction *m_postEditorAction;
     QAction *m_postClipboardAction;
index 46ffc6d..9c40b84 100644 (file)
@@ -52,6 +52,8 @@
 #include <texteditor/fontsettings.h>
 #include <texteditor/texteditorconstants.h>
 #include <utils/qtcassert.h>
+#include <extensionsystem/invoker.h>
+#include <extensionsystem/pluginmanager.h>
 
 #include <QtCore/QDebug>
 #include <QtCore/QFileInfo>
@@ -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;
index 0c9e246..da43881 100644 (file)
@@ -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