Skip to content

Commit

Permalink
Port LoginRequiredDialog to an AccountModalWidget
Browse files Browse the repository at this point in the history
And thereby not embed a complete `QDialog` in the settings window.

Fixes: #11592
  • Loading branch information
erikjv committed Apr 23, 2024
1 parent 815f10f commit 8fe2be4
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 93 deletions.
34 changes: 26 additions & 8 deletions src/gui/accountmodalwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "ui_accountmodalwidget.h"

namespace OCC {

AccountModalWidget::AccountModalWidget(const QString &title, QWidget *widget, QWidget *parent)
: QWidget(parent)
, ui(new Ui::AccountModalWidget)
Expand All @@ -24,13 +25,30 @@ AccountModalWidget::AccountModalWidget(const QString &title, QWidget *widget, QW
ui->groupBox->setTitle(title);
ui->groupBox->layout()->addWidget(widget);

connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [this] {
Q_EMIT accepted();
Q_EMIT finished();
});
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, [this] {
Q_EMIT rejected();
Q_EMIT finished();
});
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &AccountModalWidget::accept);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &AccountModalWidget::reject);
}

void AccountModalWidget::setStandardButtons(QDialogButtonBox::StandardButtons buttons)
{
ui->buttonBox->setStandardButtons(buttons);
}

QPushButton *AccountModalWidget::addButton(const QString &text, QDialogButtonBox::ButtonRole role)
{
return ui->buttonBox->addButton(text, role);
}

void AccountModalWidget::accept()
{
Q_EMIT accepted();
Q_EMIT finished(Accepted);
}

void AccountModalWidget::reject()
{
Q_EMIT rejected();
Q_EMIT finished(Rejected);
}

} // OCC
14 changes: 12 additions & 2 deletions src/gui/accountmodalwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/

#pragma once
#include <QWidget>

#include <QDialogButtonBox>

namespace OCC {

Expand All @@ -27,10 +28,19 @@ class AccountModalWidget : public QWidget
public:
AccountModalWidget(const QString &title, QWidget *widget, QWidget *parent);

enum DialogCode { Rejected, Accepted };

void setStandardButtons(QDialogButtonBox::StandardButtons buttons);
QPushButton *addButton(const QString &text, QDialogButtonBox::ButtonRole role);

public Q_SLOTS:
void accept();
void reject();

Q_SIGNALS:
void accepted();
void rejected();
void finished();
void finished(DialogCode result);

private:
Ui::AccountModalWidget *ui;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/accountmodalwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::NoButton</set>
</property>
</widget>
</item>
Expand Down
1 change: 1 addition & 0 deletions src/gui/accountsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ void AccountSettings::showSelectiveSyncDialog(Folder *folder)
Q_ASSERT(ok);

auto *modalWidget = new AccountModalWidget(tr("Choose what to sync"), selectiveSync, this);
modalWidget->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
connect(modalWidget, &AccountModalWidget::accepted, this, [selectiveSync, folder, this] {
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, selectiveSync->createBlackList());
doForceSyncCurrentFolder(folder);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/accountsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class OWNCLOUDGUI_EXPORT AccountSettings : public QWidget

public:
enum class ModalWidgetSizePolicy { Minimum = QSizePolicy::Minimum, Expanding = QSizePolicy::Expanding };
Q_ENUM(ModalWidgetSizePolicy);
Q_ENUM(ModalWidgetSizePolicy)

explicit AccountSettings(const AccountStatePtr &accountState, QWidget *parent = nullptr);
~AccountSettings() override;
Expand Down
30 changes: 17 additions & 13 deletions src/gui/creds/httpcredentialsgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "creds/httpcredentialsgui.h"
#include "account.h"
#include "accountmodalwidget.h"
#include "application.h"
#include "common/asserts.h"
#include "gui/accountsettings.h"
Expand All @@ -23,7 +24,6 @@
#include "gui/loginrequireddialog/oauthloginwidget.h"
#include "networkjobs.h"
#include "settingsdialog.h"
#include "theme.h"

#include <QClipboard>
#include <QDesktopServices>
Expand Down Expand Up @@ -96,9 +96,7 @@ void HttpCredentialsGui::asyncAuthResult(OAuth::Result r, const QString &token,
Q_EMIT oAuthErrorOccurred();
return;
case OAuth::LoggedIn:
if (_loginRequiredDialog) {
_loginRequiredDialog->accept();
}
Q_EMIT oAuthLoginAccepted();
break;
}

Expand All @@ -125,11 +123,13 @@ void HttpCredentialsGui::showDialog()
auto *contentWidget = qobject_cast<BasicLoginWidget *>(dialog->contentWidget());
contentWidget->forceUsername(user());

// in this case, we want to use the login button
dialog->addLogInButton();

connect(dialog, &LoginRequiredDialog::finished, ocApp()->gui()->settingsDialog(), [this, contentWidget](const int result) {
if (result == QDialog::Accepted) {
auto *modalWidget = new AccountModalWidget(tr("Login required"), dialog, ocApp()->gui()->settingsDialog());
modalWidget->setAttribute(Qt::WA_DeleteOnClose);
modalWidget->addButton(tr("Log out"), QDialogButtonBox::RejectRole);
modalWidget->addButton(tr("Log in"), QDialogButtonBox::AcceptRole); // in this case, we want to use the login button
connect(this, &HttpCredentialsGui::oAuthLoginAccepted, modalWidget, &AccountModalWidget::accept);
connect(modalWidget, &AccountModalWidget::finished, ocApp()->gui()->settingsDialog(), [this, contentWidget](AccountModalWidget::DialogCode result) {
if (result == AccountModalWidget::Accepted) {
_password = contentWidget->password();
_refreshToken.clear();
_ready = true;
Expand All @@ -140,7 +140,7 @@ void HttpCredentialsGui::showDialog()
Q_EMIT fetched();
});

ocApp()->gui()->settingsDialog()->accountSettings(_account)->addModalLegacyDialog(dialog, AccountSettings::ModalWidgetSizePolicy::Minimum);
ocApp()->gui()->settingsDialog()->accountSettings(_account)->addModalWidget(modalWidget);
_loginRequiredDialog = dialog;
}

Expand Down Expand Up @@ -173,7 +173,6 @@ void HttpCredentialsGui::restartOAuth()

auto *contentWidget = qobject_cast<OAuthLoginWidget *>(_loginRequiredDialog->contentWidget());
connect(contentWidget, &OAuthLoginWidget::openBrowserButtonClicked, this, &HttpCredentialsGui::openBrowser);
connect(_loginRequiredDialog, &LoginRequiredDialog::rejected, this, &HttpCredentials::requestLogout);

connect(contentWidget, &OAuthLoginWidget::retryButtonClicked, _loginRequiredDialog, [contentWidget, this]() {
restartOAuth();
Expand All @@ -186,8 +185,13 @@ void HttpCredentialsGui::restartOAuth()
contentWidget->showRetryFrame();
});

ocApp()->gui()->settingsDialog()->accountSettings(_account)->addModalLegacyDialog(
_loginRequiredDialog, AccountSettings::ModalWidgetSizePolicy::Minimum);
auto *modalWidget = new AccountModalWidget(tr("Login required"), _loginRequiredDialog, ocApp()->gui()->settingsDialog());
modalWidget->setAttribute(Qt::WA_DeleteOnClose);
modalWidget->addButton(tr("Log out"), QDialogButtonBox::RejectRole);
connect(this, &HttpCredentialsGui::oAuthLoginAccepted, modalWidget, &AccountModalWidget::accept);
connect(modalWidget, &AccountModalWidget::rejected, this, &HttpCredentials::requestLogout);

ocApp()->gui()->settingsDialog()->accountSettings(_account)->addModalWidget(modalWidget);
}

_asyncAuth.reset(new AccountBasedOAuth(_account->sharedFromThis(), this));
Expand Down
3 changes: 3 additions & 0 deletions src/gui/creds/httpcredentialsgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <QPointer>

namespace OCC {

class LoginRequiredDialog;

/**
Expand All @@ -34,6 +35,7 @@ class HttpCredentialsGui : public HttpCredentials
: HttpCredentials()
{
}

HttpCredentialsGui(const QString &loginUser, const QString &password)
: HttpCredentials(DetermineAuthTypeJob::AuthType::Basic, loginUser, password)
{
Expand Down Expand Up @@ -65,6 +67,7 @@ private Q_SLOTS:
void showDialog();

Q_SIGNALS:
void oAuthLoginAccepted();
void oAuthErrorOccurred();

private:
Expand Down
17 changes: 1 addition & 16 deletions src/gui/loginrequireddialog/loginrequireddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,20 @@
#include "loginrequireddialog.h"
#include "ui_loginrequireddialog.h"

#include "gui/application.h"
#include "gui/creds/httpcredentialsgui.h"
#include "gui/guiutility.h"
#include "theme.h"

#include <QClipboard>

namespace OCC {

LoginRequiredDialog::LoginRequiredDialog(Mode mode, QWidget *parent)
: QDialog(parent)
: QWidget(parent)
, _ui(new ::Ui::LoginRequiredDialog)
{
_ui->setupUi(this);

_ui->iconLabel->setPixmap(Theme::instance()->applicationIcon().pixmap(128, 128));

// we want a custom text, but we make use of the button box's built-in reject role
_ui->rightButtonBox->addButton(tr("Log out"), QDialogButtonBox::RejectRole);

// make plain reject/accept buttons work w/o additional effort on the caller side
connect(_ui->rightButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(_ui->rightButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);

// using a stacked widget appears to work better than a plain widget
// we do this in the setup wizard as well
_ui->contentWidget->setCurrentWidget([this, mode]() -> QWidget * {
Expand All @@ -63,11 +53,6 @@ void LoginRequiredDialog::setTopLabelText(const QString &newText)
_ui->topLabel->setText(newText);
}

void LoginRequiredDialog::addLogInButton()
{
_ui->rightButtonBox->addButton(tr("Log in"), QDialogButtonBox::AcceptRole);
}

QWidget *LoginRequiredDialog::contentWidget() const
{
return _ui->contentWidget->currentWidget();
Expand Down
11 changes: 3 additions & 8 deletions src/gui/loginrequireddialog/loginrequireddialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#include "abstractloginwidget.h"
#include "account.h"
#include <QDialog>

#include <QWidget>

namespace Ui {
class LoginRequiredDialog;
Expand All @@ -28,7 +29,7 @@ namespace OCC {
* This dialog is used to ask users to re-authenticate in case an existing account's credentials no longer work or the user logged out.
* It is one of two locations in the code where we have users log in to their accounts (the other one is the setup wizard).
*/
class LoginRequiredDialog : public QDialog
class LoginRequiredDialog : public QWidget
{
Q_OBJECT

Expand All @@ -47,12 +48,6 @@ class LoginRequiredDialog : public QDialog

void setTopLabelText(const QString &newText);

/**
* Add a "log in" button to the dialog. When clicked, the dialog is accepted.
* For use with HTTP basic authentication.
*/
void addLogInButton();

/**
* Form widget currently shown to the user.
* @return form widget
Expand Down
47 changes: 3 additions & 44 deletions src/gui/loginrequireddialog/loginrequireddialog.ui
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginRequiredDialog</class>
<widget class="QDialog" name="LoginRequiredDialog">
<widget class="QWidget" name="LoginRequiredDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>408</width>
<height>188</height>
<height>156</height>
</rect>
</property>
<property name="sizePolicy">
Expand All @@ -16,9 +16,6 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Login required</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>12</number>
Expand Down Expand Up @@ -112,42 +109,6 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="rightButtonBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>10</width>
<height>24</height>
</size>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::NoButton</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
Expand All @@ -164,8 +125,6 @@
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../../resources/client.qrc"/>
</resources>
<resources/>
<connections/>
</ui>

0 comments on commit 8fe2be4

Please sign in to comment.