From f6bc879d2c8811a190eb57904de369f19122073f Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Fri, 23 Apr 2010 15:06:53 +0200
Subject: [PATCH] CodePaster: Clean up configuration checks.

Point user to settings if something goes wrong.
---
 src/plugins/cpaster/cpaster.pro               | 11 +++-
 src/plugins/cpaster/cpasterplugin.cpp         | 11 +++-
 .../cpaster/pastebindotcomprotocol.cpp        |  2 +-
 src/plugins/cpaster/pastebindotcomprotocol.h  |  2 +-
 src/plugins/cpaster/pasteselectdialog.cpp     |  9 ++-
 src/plugins/cpaster/protocol.cpp              | 58 ++++++++++++++++---
 src/plugins/cpaster/protocol.h                | 18 ++++--
 7 files changed, 86 insertions(+), 25 deletions(-)

diff --git a/src/plugins/cpaster/cpaster.pro b/src/plugins/cpaster/cpaster.pro
index 6abf90914cf..38c0a6e9d13 100644
--- a/src/plugins/cpaster/cpaster.pro
+++ b/src/plugins/cpaster/cpaster.pro
@@ -15,7 +15,9 @@ HEADERS += cpasterplugin.h \
     pastebindotcaprotocol.h \
     settings.h \
     pasteselectdialog.h \
-    columnindicatortextedit.h
+    columnindicatortextedit.h \
+    fileshareprotocol.h \
+    fileshareprotocolsettingspage.h
 SOURCES += cpasterplugin.cpp \
     settingspage.cpp \
     protocol.cpp \
@@ -27,9 +29,12 @@ SOURCES += cpasterplugin.cpp \
     pastebindotcaprotocol.cpp \
     settings.cpp \
     pasteselectdialog.cpp \
-    columnindicatortextedit.cpp
+    columnindicatortextedit.cpp \
+    fileshareprotocol.cpp \
+    fileshareprotocolsettingspage.cpp
 FORMS += settingspage.ui \
     pasteselect.ui \
     pasteview.ui \
-    pastebindotcomsettings.ui
+    pastebindotcomsettings.ui \
+    fileshareprotocolsettingswidget.ui
 include(../../shared/cpaster/cpaster.pri)
diff --git a/src/plugins/cpaster/cpasterplugin.cpp b/src/plugins/cpaster/cpasterplugin.cpp
index 104008a8f00..71b3e6af482 100644
--- a/src/plugins/cpaster/cpasterplugin.cpp
+++ b/src/plugins/cpaster/cpasterplugin.cpp
@@ -34,6 +34,7 @@
 #include "codepasterprotocol.h"
 #include "pastebindotcomprotocol.h"
 #include "pastebindotcaprotocol.h"
+#include "fileshareprotocol.h"
 #include "pasteselectdialog.h"
 #include "settingspage.h"
 #include "settings.h"
@@ -94,7 +95,8 @@ bool CodepasterPlugin::initialize(const QStringList &arguments, QString *error_m
     const QSharedPointer<NetworkAccessManagerProxy> networkAccessMgrProxy(new NetworkAccessManagerProxy);
     Protocol *protos[] =  { new PasteBinDotComProtocol(networkAccessMgrProxy),
                             new PasteBinDotCaProtocol(networkAccessMgrProxy),
-                            new CodePasterProtocol(networkAccessMgrProxy)
+                            new CodePasterProtocol(networkAccessMgrProxy),
+                            new FileShareProtocol
                            };
     const int count = sizeof(protos) / sizeof(Protocol *);
     for(int i = 0; i < count; ++i) {
@@ -221,7 +223,8 @@ void CodepasterPlugin::post(QString data, const QString &mimeType)
     foreach(Protocol *protocol, m_protocols) {
         if (protocol->name() == protocolName) {
             const Protocol::ContentType ct = Protocol::contentType(mimeType);
-            protocol->paste(data, ct, username, comment, description);
+            if (Protocol::ensureConfiguration(protocol))
+                protocol->paste(data, ct, username, comment, description);
             break;
         }
     }
@@ -237,7 +240,9 @@ void CodepasterPlugin::fetch()
     const QString pasteID = dialog.pasteId();
     if (pasteID.isEmpty())
         return;
-    m_protocols[dialog.protocolIndex()]->fetch(pasteID);
+    Protocol *protocol = m_protocols[dialog.protocolIndex()];
+    if (Protocol::ensureConfiguration(protocol))
+        protocol->fetch(pasteID);
 }
 
 void CodepasterPlugin::finishPost(const QString &link)
diff --git a/src/plugins/cpaster/pastebindotcomprotocol.cpp b/src/plugins/cpaster/pastebindotcomprotocol.cpp
index 6ca770871bc..2e7876b9eb0 100644
--- a/src/plugins/cpaster/pastebindotcomprotocol.cpp
+++ b/src/plugins/cpaster/pastebindotcomprotocol.cpp
@@ -320,7 +320,7 @@ void PasteBinDotComProtocol::listFinished()
     m_listReply = 0;
 }
 
-Core::IOptionsPage *PasteBinDotComProtocol::settingsPage()
+Core::IOptionsPage *PasteBinDotComProtocol::settingsPage() const
 {
     return m_settings;
 }
diff --git a/src/plugins/cpaster/pastebindotcomprotocol.h b/src/plugins/cpaster/pastebindotcomprotocol.h
index 5545e15ab70..e137bc14b2a 100644
--- a/src/plugins/cpaster/pastebindotcomprotocol.h
+++ b/src/plugins/cpaster/pastebindotcomprotocol.h
@@ -46,7 +46,7 @@ public:
 
     virtual unsigned capabilities() const;
     bool hasSettings() const { return true; }
-    Core::IOptionsPage *settingsPage();
+    Core::IOptionsPage *settingsPage() const;
 
     virtual void fetch(const QString &id);
     virtual void paste(const QString &text,
diff --git a/src/plugins/cpaster/pasteselectdialog.cpp b/src/plugins/cpaster/pasteselectdialog.cpp
index 8f19b776370..3bc643d51b8 100644
--- a/src/plugins/cpaster/pasteselectdialog.cpp
+++ b/src/plugins/cpaster/pasteselectdialog.cpp
@@ -114,11 +114,14 @@ void PasteSelectDialog::list()
 {
     const int index = protocolIndex();
 
-    QTC_ASSERT((m_protocols.at(index)->capabilities() & Protocol::ListCapability), return);
+    Protocol *protocol = m_protocols[index];
+    QTC_ASSERT((protocol->capabilities() & Protocol::ListCapability), return);
 
     m_ui.listWidget->clear();
-    m_ui.listWidget->addItem(new QListWidgetItem(tr("Waiting for items")));
-    m_protocols[index]->list();
+    if (Protocol::ensureConfiguration(protocol, this)) {
+        m_ui.listWidget->addItem(new QListWidgetItem(tr("Waiting for items")));
+        protocol->list();
+    }
 }
 
 void PasteSelectDialog::protocolChanged(int i)
diff --git a/src/plugins/cpaster/protocol.cpp b/src/plugins/cpaster/protocol.cpp
index 42429a38367..68aca2f58c7 100644
--- a/src/plugins/cpaster/protocol.cpp
+++ b/src/plugins/cpaster/protocol.cpp
@@ -30,11 +30,16 @@
 
 #include <cpptools/cpptoolsconstants.h>
 #include <qmljseditor/qmljseditorconstants.h>
+#include <coreplugin/icore.h>
+#include <coreplugin/dialogs/ioptionspage.h>
 
 #include <QtNetwork/QNetworkAccessManager>
 #include <QtNetwork/QNetworkRequest>
 
 #include <QtCore/QUrl>
+#include <QtGui/QMessageBox>
+#include <QtGui/QMainWindow>
+#include <QtGui/QPushButton>
 
 namespace CodePaster {
 
@@ -47,22 +52,17 @@ Protocol::~Protocol()
 {
 }
 
-bool Protocol::canFetch() const
+bool Protocol::hasSettings() const
 {
-    return true;
+    return false;
 }
 
-bool Protocol::canPost() const
+bool Protocol::checkConfiguration(QString *) const
 {
     return true;
 }
 
-bool Protocol::hasSettings() const
-{
-    return false;
-}
-
-Core::IOptionsPage *Protocol::settingsPage()
+Core::IOptionsPage *Protocol::settingsPage() const
 {
     return 0;
 }
@@ -115,6 +115,46 @@ QString Protocol::textFromHtml(QString data)
     return data;
 }
 
+bool Protocol::ensureConfiguration(const Protocol *p, QWidget *parent)
+{
+    QString errorMessage;
+    bool ok = false;
+    while (true) {
+        if (p->checkConfiguration(&errorMessage)) {
+            ok = true;
+            break;
+        }
+        if (!showConfigurationError(p, errorMessage, parent))
+            break;
+    }
+    return ok;
+}
+
+bool Protocol::showConfigurationError(const Protocol *p,
+                                      const QString &message,
+                                      QWidget *parent,
+                                      bool showConfig)
+{
+    if (!p->settingsPage())
+        showConfig = false;
+
+    if (!parent)
+        parent = Core::ICore::instance()->mainWindow();
+    const QString title = tr("%1 - Configuration Error").arg(p->name());
+    QMessageBox mb(QMessageBox::Warning, title, message, QMessageBox::Cancel, parent);
+    QPushButton *settingsButton = 0;
+    if (showConfig)
+        settingsButton = mb.addButton(tr("Settings..."), QMessageBox::AcceptRole);
+    mb.exec();
+    bool rc = false;
+    if (mb.clickedButton() == settingsButton)
+        rc = Core::ICore::instance()->showOptionsDialog(p->settingsPage()->category(),
+                                                        p->settingsPage()->id(),
+                                                        parent);
+    return rc;
+}
+
+
 // ------------ NetworkAccessManagerProxy
 NetworkAccessManagerProxy::NetworkAccessManagerProxy()
 {
diff --git a/src/plugins/cpaster/protocol.h b/src/plugins/cpaster/protocol.h
index 6ee95f9fdd6..43ecc6a823c 100644
--- a/src/plugins/cpaster/protocol.h
+++ b/src/plugins/cpaster/protocol.h
@@ -37,6 +37,7 @@
 QT_BEGIN_NAMESPACE
 class QNetworkAccessManager;
 class QNetworkReply;
+class QWidget;
 QT_END_NAMESPACE
 
 namespace Core {
@@ -62,14 +63,11 @@ public:
 
     virtual QString name() const = 0;
 
-    bool canFetch() const;
-    bool canPost() const;
-
-
     virtual unsigned capabilities() const = 0;
     virtual bool hasSettings() const;
-    virtual Core::IOptionsPage *settingsPage();
+    virtual Core::IOptionsPage *settingsPage() const;
 
+    virtual bool checkConfiguration(QString *errorMessage = 0) const;
     virtual void fetch(const QString &id) = 0;
     virtual void list();
     virtual void paste(const QString &text,
@@ -81,6 +79,16 @@ public:
     // Convenience to determine content type from mime type
     static ContentType contentType(const QString &mimeType);
 
+    // Show a configuration error and point user to settings.
+    // Return true when settings changed.
+    static bool showConfigurationError(const Protocol *p,
+                                       const QString &message,
+                                       QWidget *parent = 0,
+                                       bool showConfig = true);
+    // Ensure configuration is correct
+    static bool ensureConfiguration(const Protocol *p,
+                                    QWidget *parent = 0);
+
 signals:
     void pasteDone(const QString &link);
     void fetchDone(const QString &titleDescription,
-- 
GitLab