From 6a91b63af9bc9d4a3319f41bf2a0652b525a3f2d Mon Sep 17 00:00:00 2001
From: Maurice Kalinowski <maurice.kalinowski@nokia.com>
Date: Fri, 31 Jul 2009 11:17:57 +0200
Subject: [PATCH] move editor creation to plugin

each protocol needed to create the editor on their own.
Instead emit fetchDone and let the plugin instantiate it for
every protocol
---
 src/plugins/cpaster/codepasterprotocol.cpp    | 19 ++++++++++++-------
 src/plugins/cpaster/cpasterplugin.cpp         | 14 ++++++++++++++
 src/plugins/cpaster/cpasterplugin.h           |  3 +++
 .../cpaster/pastebindotcomprotocol.cpp        | 14 +++++++++-----
 src/plugins/cpaster/protocol.h                |  3 +++
 5 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/src/plugins/cpaster/codepasterprotocol.cpp b/src/plugins/cpaster/codepasterprotocol.cpp
index 26406a3150c..57a21dee219 100644
--- a/src/plugins/cpaster/codepasterprotocol.cpp
+++ b/src/plugins/cpaster/codepasterprotocol.cpp
@@ -118,17 +118,22 @@ Core::IOptionsPage* CodePasterProtocol::settingsPage()
 
 void CodePasterProtocol::fetchFinished()
 {
-    if (reply->error()) {
-        ICore::instance()->messageManager()->printToOutputPane(reply->errorString(), true);
+    QString title;
+    QString content;
+    bool error = reply->error();
+    if (error) {
+        content = reply->errorString();
     } else {
-        QString data = reply->readAll();
-        if (data.contains("<B>No such paste!</B>"))
-            ICore::instance()->messageManager()->printToOutputPane(tr("No such paste"), true);
-        QString title = QString::fromLatin1("Codepaster: %1").arg(fetchId);
-        EditorManager::instance()->newFile(Core::Constants::K_DEFAULT_TEXT_EDITOR, &title, data);
+        content = reply->readAll();
+        if (content.contains("<B>No such paste!</B>")) {
+            content = tr("No such paste");
+            error = true;
+        }
+        title = QString::fromLatin1("Codepaster: %1").arg(fetchId);
     }
     reply->deleteLater();
     reply = 0;
+    emit fetchDone(title, content, error);
 }
 
 void CodePasterProtocol::listFinished()
diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp
index d4ca9a4daf6..3dffbcadd3e 100644
--- a/src/plugins/cpaster/cpasterplugin.cpp
+++ b/src/plugins/cpaster/cpasterplugin.cpp
@@ -95,6 +95,8 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
                             0};
     for(int i=0; protos[i] != 0; ++i) {
         connect(protos[i], SIGNAL(pasteDone(QString)), this, SLOT(finishPost(QString)));
+        connect(protos[i], SIGNAL(fetchDone(QString,QString,bool)),
+                this, SLOT(finishFetch(QString,QString,bool)));
         m_settingsPage->addProtocol(protos[i]->name());
         if (protos[i]->hasSettings())
             addObject(protos[i]->settingsPage());
@@ -251,4 +253,16 @@ void CodepasterPlugin::finishPost(const QString &link)
                                                            m_settingsPage->displayOutput());
 }
 
+void CodepasterPlugin::finishFetch(const QString &titleDescription,
+                                   const QString &content,
+                                   bool error)
+{
+    QString title = titleDescription;
+    if (error) {
+        ICore::instance()->messageManager()->printToOutputPane(content, true);
+    } else {
+        EditorManager::instance()->newFile(Core::Constants::K_DEFAULT_TEXT_EDITOR, &title, content);
+    }
+}
+
 Q_EXPORT_PLUGIN(CodepasterPlugin)
diff --git a/src/plugins/cpaster/cpasterplugin.h b/src/plugins/cpaster/cpasterplugin.h
index ea2af4a7e8b..210a26d1f10 100644
--- a/src/plugins/cpaster/cpasterplugin.h
+++ b/src/plugins/cpaster/cpasterplugin.h
@@ -64,6 +64,9 @@ public slots:
     void post();
     void fetch();
     void finishPost(const QString &link);
+    void finishFetch(const QString &titleDescription,
+                     const QString &content,
+                     bool error);
 
 private:
     QAction *m_postAction;
diff --git a/src/plugins/cpaster/pastebindotcomprotocol.cpp b/src/plugins/cpaster/pastebindotcomprotocol.cpp
index 166fb936325..4dc047cd512 100644
--- a/src/plugins/cpaster/pastebindotcomprotocol.cpp
+++ b/src/plugins/cpaster/pastebindotcomprotocol.cpp
@@ -115,14 +115,18 @@ void PasteBinDotComProtocol::postRequestFinished(int id, bool error)
 
 void PasteBinDotComProtocol::fetchFinished()
 {
-    if (reply->error()) {
-        ICore::instance()->messageManager()->printToOutputPane(reply->errorString(), true);
+    QString title;
+    QString content;
+    bool error = reply->error();
+    if (error) {
+        content = reply->errorString();
     } else {
-        QString title = QString::fromLatin1("Pastebin.com: %1").arg(fetchId);
-        QString data = reply->readAll();
-        EditorManager::instance()->newFile(Core::Constants::K_DEFAULT_TEXT_EDITOR, &title, data);
+        title = QString::fromLatin1("Pastebin.com: %1").arg(fetchId);
+        content = reply->readAll();
     }
     reply->deleteLater();
+    reply = 0;
+    emit fetchDone(title, content, error);
 }
 
 Core::IOptionsPage* PasteBinDotComProtocol::settingsPage()
diff --git a/src/plugins/cpaster/protocol.h b/src/plugins/cpaster/protocol.h
index a45c04790c6..5b45326c06c 100644
--- a/src/plugins/cpaster/protocol.h
+++ b/src/plugins/cpaster/protocol.h
@@ -61,6 +61,9 @@ public:
 
 signals:
     void pasteDone(const QString &link);
+    void fetchDone(const QString &titleDescription,
+                   const QString &content,
+                   bool error);
 };
 
 #endif // PROTOCOL_H
-- 
GitLab