OAuth: Error handling in the wizard
authorOlivier Goffart <ogoffart@woboq.com>
Wed, 7 Jun 2017 14:19:34 +0000 (16:19 +0200)
committerOlivier Goffart <olivier@woboq.com>
Mon, 12 Jun 2017 10:58:20 +0000 (12:58 +0200)
Issues: #5813 and #5811

src/gui/CMakeLists.txt
src/gui/creds/oauth.h
src/gui/wizard/owncloudoauthcredspage.cpp
src/gui/wizard/owncloudoauthcredspage.h
src/gui/wizard/owncloudoauthcredspage.ui [new file with mode: 0644]

index 80b8b940a2aa6a7666c14b008a15ee7f2f62f011..7dc82bb7cb547e81c5588197c371f31ec6037c9d 100644 (file)
@@ -35,6 +35,7 @@ set(client_UI
     wizard/owncloudadvancedsetuppage.ui
     wizard/owncloudconnectionmethoddialog.ui
     wizard/owncloudhttpcredspage.ui
+    wizard/owncloudoauthcredspage.ui
     wizard/owncloudsetupnocredspage.ui
     wizard/owncloudwizardresultpage.ui
 )
index 93e7ac209f65643a9e35d7d83e614b5973d67e8f..fe7fd1c402650b9c9f20d5cfd9a03e7705ee1bfa 100644 (file)
@@ -50,6 +50,7 @@ public:
     enum Result { NotSupported,
         LoggedIn,
         Error };
+    Q_ENUM(Result);
     void start();
     bool openBrowser();
 
index 50f498a924e5fa62a04c8fdd0578bb8b0b3b7cbb..a4bf5988e7b5ff1f72ff15cb186371f7e366db8b 100644 (file)
@@ -27,43 +27,62 @@ namespace OCC {
 
 OwncloudOAuthCredsPage::OwncloudOAuthCredsPage()
     : AbstractCredentialsWizardPage()
-    , _afterInitialSetup(false)
-
 {
+    _ui.setupUi(this);
+
+    Theme *theme = Theme::instance();
+    _ui.topLabel->hide();
+    _ui.bottomLabel->hide();
+    QVariant variant = theme->customMedia(Theme::oCSetupTop);
+    WizardCommon::setupCustomMedia(variant, _ui.topLabel);
+    variant = theme->customMedia(Theme::oCSetupBottom);
+    WizardCommon::setupCustomMedia(variant, _ui.bottomLabel);
+
+    WizardCommon::initErrorLabel(_ui.errorLabel);
+
+    setTitle(WizardCommon::titleTemplate().arg(tr("Connect to %1").arg(Theme::instance()->appNameGUI())));
+    setSubTitle(WizardCommon::subTitleTemplate().arg(tr("Login in your browser")));
+
+    connect(_ui.openLinkButton, &QCommandLinkButton::clicked, [this] {
+        _ui.errorLabel->hide();
+        if (_asyncAuth)
+            _asyncAuth->openBrowser();
+    });
 }
 
-void OwncloudOAuthCredsPage::setVisible(bool visible)
+void OwncloudOAuthCredsPage::initializePage()
 {
-    if (!_afterInitialSetup) {
-        QWizardPage::setVisible(visible);
-        return;
-    }
+    OwncloudWizard *ocWizard = qobject_cast<OwncloudWizard *>(wizard());
+    Q_ASSERT(ocWizard);
+    ocWizard->account()->setCredentials(CredentialsFactory::create("http"));
+    _asyncAuth.reset(new OAuth(ocWizard->account().data(), this));
+    connect(_asyncAuth.data(), &OAuth::result, this, &OwncloudOAuthCredsPage::asyncAuthResult, Qt::QueuedConnection);
+    _asyncAuth->start();
+    wizard()->hide();
+}
 
-    if (isVisible() == visible) {
-        return;
-    }
-    if (visible) {
-        OwncloudWizard *ocWizard = qobject_cast<OwncloudWizard *>(wizard());
-        Q_ASSERT(ocWizard);
-        ocWizard->account()->setCredentials(CredentialsFactory::create("http"));
-        _asyncAuth.reset(new OAuth(ocWizard->account().data(), this));
-        connect(_asyncAuth.data(), SIGNAL(result(OAuth::Result, QString, QString, QString)),
-            this, SLOT(asyncAuthResult(OAuth::Result, QString, QString, QString)));
-        _asyncAuth->start();
-        wizard()->hide();
-    } else {
-        // The next or back button was activated, show the wizard again
-        wizard()->show();
-    }
+void OCC::OwncloudOAuthCredsPage::cleanupPage()
+{
+    // The next or back button was activated, show the wizard again
+    wizard()->show();
+    _asyncAuth.reset();
 }
 
 void OwncloudOAuthCredsPage::asyncAuthResult(OAuth::Result r, const QString &user,
     const QString &token, const QString &refreshToken)
 {
     switch (r) {
-    case OAuth::NotSupported:
+    case OAuth::NotSupported: {
+        /* OAuth not supported (can't open browser), fallback to HTTP credentials */
+        OwncloudWizard *ocWizard = qobject_cast<OwncloudWizard *>(wizard());
+        ocWizard->back();
+        ocWizard->setAuthType(WizardCommon::HttpCreds);
+        break;
+    }
     case OAuth::Error:
-        qWarning() << "FIXME!!!";
+        /* Error while getting the access token.  (Timeout, or the server did not accept our client credentials */
+        _ui.errorLabel->show();
+        wizard()->show();
         break;
     case OAuth::LoggedIn: {
         _token = token;
@@ -77,11 +96,6 @@ void OwncloudOAuthCredsPage::asyncAuthResult(OAuth::Result r, const QString &use
     }
 }
 
-void OwncloudOAuthCredsPage::initializePage()
-{
-    _afterInitialSetup = true;
-}
-
 int OwncloudOAuthCredsPage::nextId() const
 {
     return WizardCommon::Page_AdvancedSetup;
@@ -100,4 +114,9 @@ AbstractCredentials *OwncloudOAuthCredsPage::getCredentials() const
         ocWizard->_clientSslCertificate, ocWizard->_clientSslKey);
 }
 
+bool OwncloudOAuthCredsPage::isComplete() const
+{
+    return false; /* We can never go forward manually */
+}
+
 } // namespace OCC
index 2ef6365ddb759f7c4dd904a0dd52443bb9902259..f51a1896a00ff3de1788b34141ccaab2bb403574 100644 (file)
@@ -24,6 +24,9 @@
 #include "accountfwd.h"
 #include "creds/oauth.h"
 
+#include "ui_owncloudoauthcredspage.h"
+
+
 namespace OCC {
 
 
@@ -36,25 +39,24 @@ public:
     AbstractCredentials *getCredentials() const Q_DECL_OVERRIDE;
 
     void initializePage() Q_DECL_OVERRIDE;
+    void cleanupPage() override;
     int nextId() const Q_DECL_OVERRIDE;
     void setConnected();
+    bool isComplete() const override;
 
 public Q_SLOTS:
-    void setVisible(bool visible) Q_DECL_OVERRIDE;
     void asyncAuthResult(OAuth::Result, const QString &user, const QString &token,
         const QString &reniewToken);
 
 signals:
     void connectToOCUrl(const QString &);
 
-private:
-    bool _afterInitialSetup;
-
 public:
     QString _user;
     QString _token;
     QString _refreshToken;
     QScopedPointer<OAuth> _asyncAuth;
+    Ui_OwncloudOAuthCredsPage _ui;
 };
 
 } // namespace OCC
diff --git a/src/gui/wizard/owncloudoauthcredspage.ui b/src/gui/wizard/owncloudoauthcredspage.ui
new file mode 100644 (file)
index 0000000..7b20b6c
--- /dev/null
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>OwncloudOAuthCredsPage</class>
+ <widget class="QWidget" name="OwncloudOAuthCredsPage">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>424</width>
+    <height>373</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <widget class="QLabel" name="topLabel">
+     <property name="text">
+      <string notr="true">TextLabel</string>
+     </property>
+     <property name="textFormat">
+      <enum>Qt::RichText</enum>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignCenter</set>
+     </property>
+     <property name="wordWrap">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QLabel" name="label">
+     <property name="text">
+      <string>Please switch to your browser to proceed.</string>
+     </property>
+     <property name="wordWrap">
+      <bool>true</bool>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QLabel" name="errorLabel">
+     <property name="text">
+      <string>An error occured while connecting. Please try again.</string>
+     </property>
+     <property name="textFormat">
+      <enum>Qt::PlainText</enum>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QCommandLinkButton" name="openLinkButton">
+     <property name="text">
+      <string>Re-open Browser</string>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <spacer name="verticalSpacer">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>20</width>
+       <height>127</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <widget class="QLabel" name="bottomLabel">
+     <property name="text">
+      <string notr="true">TextLabel</string>
+     </property>
+     <property name="textFormat">
+      <enum>Qt::RichText</enum>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>