Initial QSfera import
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) by Klaas Freitag <freitag@kde.org>
|
||||
* Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "creds/httpcredentialsgui.h"
|
||||
#include "account.h"
|
||||
#include "accountmodalwidget.h"
|
||||
#include "application.h"
|
||||
#include "common/asserts.h"
|
||||
#include "creds/qmlcredentials.h"
|
||||
#include "gui/accountsettings.h"
|
||||
#include "networkjobs.h"
|
||||
#include "settingsdialog.h"
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QDesktopServices>
|
||||
#include <QTimer>
|
||||
|
||||
|
||||
namespace OCC {
|
||||
|
||||
Q_LOGGING_CATEGORY(lcHttpCredentialsGui, "sync.credentials.http.gui", QtInfoMsg)
|
||||
|
||||
HttpCredentialsGui::HttpCredentialsGui(const QString &accessToken, const QString &refreshToken)
|
||||
: HttpCredentials(accessToken)
|
||||
{
|
||||
_refreshToken = refreshToken;
|
||||
}
|
||||
|
||||
void HttpCredentialsGui::restartOauth()
|
||||
{
|
||||
qCDebug(lcHttpCredentialsGui) << u"showing modal dialog asking user to log in again via OAuth2";
|
||||
if (_asyncAuth) {
|
||||
return;
|
||||
}
|
||||
if (!OC_ENSURE_NOT(_modalWidget)) {
|
||||
_modalWidget->deleteLater();
|
||||
}
|
||||
_asyncAuth.reset(new AccountBasedOAuth(_account->sharedFromThis(), this));
|
||||
connect(_asyncAuth.data(), &OAuth::result, this, &HttpCredentialsGui::asyncAuthResult);
|
||||
|
||||
auto *oauthCredentials = new QmlOAuthCredentials(_asyncAuth.data(), _account->url(), _account->davDisplayName());
|
||||
_modalWidget = new AccountModalWidget(tr("Login required"), QUrl(QStringLiteral("qrc:/qt/qml/eu/QSfera/gui/qml/credentials/OAuthCredentials.qml")),
|
||||
oauthCredentials, ocApp()->settingsDialog());
|
||||
|
||||
connect(oauthCredentials, &QmlOAuthCredentials::logOutRequested, _modalWidget, [this] {
|
||||
_modalWidget->reject();
|
||||
_modalWidget.clear();
|
||||
_asyncAuth.reset();
|
||||
requestLogout();
|
||||
});
|
||||
connect(oauthCredentials, &QmlOAuthCredentials::requestRestart, this, [this] {
|
||||
_modalWidget->reject();
|
||||
_modalWidget.clear();
|
||||
_asyncAuth.reset();
|
||||
restartOauth();
|
||||
});
|
||||
connect(this, &HttpCredentialsGui::oAuthLoginAccepted, _modalWidget, &AccountModalWidget::accept);
|
||||
connect(this, &HttpCredentialsGui::oAuthErrorOccurred, oauthCredentials, [this]() {
|
||||
Q_ASSERT(!ready());
|
||||
ocApp()->showSettings();
|
||||
});
|
||||
|
||||
ocApp()->settingsDialog()->accountSettings(_account)->addModalWidget(_modalWidget);
|
||||
_asyncAuth->startAuthentication();
|
||||
}
|
||||
|
||||
void HttpCredentialsGui::asyncAuthResult(OAuth::Result r, const QString &token, const QString &refreshToken)
|
||||
{
|
||||
_asyncAuth.reset();
|
||||
switch (r) {
|
||||
case OAuth::ErrorInsecureUrl:
|
||||
// should not happen after the initial setup
|
||||
Q_ASSERT(false);
|
||||
[[fallthrough]];
|
||||
case OAuth::Error:
|
||||
Q_EMIT oAuthErrorOccurred();
|
||||
return;
|
||||
case OAuth::LoggedIn:
|
||||
Q_EMIT oAuthLoginAccepted();
|
||||
break;
|
||||
}
|
||||
|
||||
_accessToken = token;
|
||||
_refreshToken = refreshToken;
|
||||
_ready = true;
|
||||
persist();
|
||||
Q_EMIT fetched();
|
||||
}
|
||||
|
||||
|
||||
} // namespace OCC
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) by Klaas Freitag <freitag@kde.org>
|
||||
* Copyright (C) by Olivier Goffart <ogoffart@woboq.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "creds/httpcredentials.h"
|
||||
#include "creds/oauth.h"
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
namespace OCC {
|
||||
|
||||
class AccountModalWidget;
|
||||
|
||||
/**
|
||||
* @brief The HttpCredentialsGui class
|
||||
* @ingroup gui
|
||||
*/
|
||||
class HttpCredentialsGui : public HttpCredentials
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
HttpCredentialsGui() = default;
|
||||
|
||||
HttpCredentialsGui(const QString &accessToken, const QString &refreshToken);
|
||||
|
||||
void restartOauth() override;
|
||||
|
||||
|
||||
private Q_SLOTS:
|
||||
void asyncAuthResult(OAuth::Result, const QString &accessToken, const QString &refreshToken);
|
||||
|
||||
Q_SIGNALS:
|
||||
void oAuthLoginAccepted();
|
||||
void oAuthErrorOccurred();
|
||||
|
||||
private:
|
||||
QScopedPointer<AccountBasedOAuth, QScopedPointerObjectDeleteLater<AccountBasedOAuth>> _asyncAuth;
|
||||
QPointer<AccountModalWidget> _modalWidget;
|
||||
};
|
||||
|
||||
} // namespace OCC
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) by Hannah von Reth <hannah.vonreth@owncloud.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "qmlcredentials.h"
|
||||
|
||||
#include "libsync/theme.h"
|
||||
|
||||
#include <QClipboard>
|
||||
#include <QGuiApplication>
|
||||
|
||||
using namespace OCC;
|
||||
|
||||
|
||||
QmlCredentials::QmlCredentials(const QUrl &host, const QString &displayName, QObject *parent)
|
||||
: QObject(parent)
|
||||
, _host(host)
|
||||
, _displayName(displayName)
|
||||
{
|
||||
}
|
||||
|
||||
bool QmlCredentials::isRefresh() const
|
||||
{
|
||||
return _isRefresh;
|
||||
}
|
||||
|
||||
void QmlCredentials::setIsRefresh(bool newIsRefresh)
|
||||
{
|
||||
if (_isRefresh != newIsRefresh) {
|
||||
_isRefresh = newIsRefresh;
|
||||
Q_EMIT isRefreshChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QmlOAuthCredentials::QmlOAuthCredentials(OAuth *oauth, const QUrl &host, const QString &displayName, QObject *parent)
|
||||
: QmlCredentials(host, displayName, parent)
|
||||
, _oauth(oauth)
|
||||
{
|
||||
connect(_oauth, &OAuth::authorisationLinkChanged, this, [this] {
|
||||
_ready = true;
|
||||
Q_EMIT readyChanged();
|
||||
});
|
||||
connect(_oauth, &QObject::destroyed, this, &QmlOAuthCredentials::readyChanged);
|
||||
}
|
||||
|
||||
void QmlOAuthCredentials::copyAuthenticationUrlToClipboard()
|
||||
{
|
||||
qApp->clipboard()->setText(_oauth->authorisationLink().toString(QUrl::FullyEncoded));
|
||||
}
|
||||
|
||||
void QmlOAuthCredentials::openAuthenticationUrlInBrowser()
|
||||
{
|
||||
_oauth->openBrowser();
|
||||
}
|
||||
|
||||
bool QmlOAuthCredentials::isReady() const
|
||||
{
|
||||
return isValid() && _ready;
|
||||
}
|
||||
|
||||
bool QmlOAuthCredentials::isValid() const
|
||||
{
|
||||
return _oauth;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) by Hannah von Reth <hannah.vonreth@owncloud.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "libsync/creds/oauth.h"
|
||||
|
||||
#include <QtQmlIntegration/QtQmlIntegration>
|
||||
|
||||
namespace OCC {
|
||||
|
||||
class QmlCredentials : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QUrl host MEMBER _host CONSTANT FINAL)
|
||||
Q_PROPERTY(QString displayName MEMBER _displayName CONSTANT FINAL)
|
||||
Q_PROPERTY(bool ready READ isReady NOTIFY readyChanged FINAL)
|
||||
Q_PROPERTY(bool isRefresh READ isRefresh WRITE setIsRefresh NOTIFY isRefreshChanged FINAL)
|
||||
QML_ELEMENT
|
||||
QML_UNCREATABLE("Abstract class")
|
||||
|
||||
public:
|
||||
QmlCredentials(const QUrl &host, const QString &displayName, QObject *parent = nullptr);
|
||||
|
||||
virtual bool isReady() const = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Whether we refresh the credentials or are in the wizard
|
||||
* @default true
|
||||
*/
|
||||
bool isRefresh() const;
|
||||
void setIsRefresh(bool newIsRefresh);
|
||||
|
||||
Q_SIGNALS:
|
||||
void readyChanged() const;
|
||||
void logOutRequested() const;
|
||||
|
||||
void isRefreshChanged();
|
||||
|
||||
private:
|
||||
QUrl _host;
|
||||
QString _displayName;
|
||||
bool _isRefresh = true;
|
||||
};
|
||||
|
||||
|
||||
class QmlOAuthCredentials : public QmlCredentials
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(bool isValid READ isValid NOTIFY readyChanged FINAL)
|
||||
QML_ELEMENT
|
||||
QML_UNCREATABLE("C++ only")
|
||||
|
||||
public:
|
||||
QmlOAuthCredentials(OAuth *oauth, const QUrl &host, const QString &displayName, QObject *parent = nullptr);
|
||||
|
||||
Q_INVOKABLE void copyAuthenticationUrlToClipboard();
|
||||
Q_INVOKABLE void openAuthenticationUrlInBrowser();
|
||||
|
||||
bool isReady() const override;
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void requestRestart();
|
||||
|
||||
|
||||
private:
|
||||
QPointer<OAuth> _oauth = nullptr;
|
||||
bool _ready = false;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user