Welcome to mirror list, hosted at ThFree Co, Russian Federation.

webflowcredentialsdialog.cpp « creds « gui « src - github.com/nextcloud/desktop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6edf9f3fb4634650ec49540d074cd2b18f8b420d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "webflowcredentialsdialog.h"

#include <QVBoxLayout>
#include <QLabel>

#include "theme.h"
#include "application.h"
#include "owncloudgui.h"
#include "headerbanner.h"
#include "wizard/owncloudwizardcommon.h"
#ifdef WITH_WEBENGINE
#include "wizard/webview.h"
#endif // WITH_WEBENGINE
#include "wizard/flow2authwidget.h"

namespace OCC {

WebFlowCredentialsDialog::WebFlowCredentialsDialog(Account *account, bool useFlow2, QWidget *parent)
    : QDialog(parent)
    , _useFlow2(useFlow2)
    , _flow2AuthWidget(nullptr)
#ifdef WITH_WEBENGINE
    , _webView(nullptr)
#endif // WITH_WEBENGINE
{
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    _layout = new QVBoxLayout(this);
    int spacing = _layout->spacing();
    int margin = _layout->margin();
    _layout->setSpacing(0);
    _layout->setMargin(0);

    _containerLayout = new QVBoxLayout(this);
    _containerLayout->setSpacing(spacing);
    _containerLayout->setMargin(margin);

    _infoLabel = new QLabel();
    _infoLabel->setAlignment(Qt::AlignCenter);
    _containerLayout->addWidget(_infoLabel);

    if (_useFlow2) {
        _flow2AuthWidget = new Flow2AuthWidget();
        _containerLayout->addWidget(_flow2AuthWidget);

        connect(_flow2AuthWidget, &Flow2AuthWidget::authResult, this, &WebFlowCredentialsDialog::slotFlow2AuthResult);

        // Connect styleChanged events to our widgets, so they can adapt (Dark-/Light-Mode switching)
        connect(this, &WebFlowCredentialsDialog::styleChanged, _flow2AuthWidget, &Flow2AuthWidget::slotStyleChanged);

        // allow Flow2 page to poll on window activation
        connect(this, &WebFlowCredentialsDialog::onActivate, _flow2AuthWidget, &Flow2AuthWidget::slotPollNow);

        _flow2AuthWidget->startAuth(account);
    } else {
#ifdef WITH_WEBENGINE
        _webView = new WebView();
        _containerLayout->addWidget(_webView, 1);

        connect(_webView, &WebView::urlCatched, this, &WebFlowCredentialsDialog::urlCatched);
#endif // WITH_WEBENGINE
    }

    auto app = static_cast<Application *>(qApp);
    connect(app, &Application::isShowingSettingsDialog, this, &WebFlowCredentialsDialog::slotShowSettingsDialog);

    _errorLabel = new QLabel();
    _errorLabel->hide();
    _containerLayout->addWidget(_errorLabel);

    WizardCommon::initErrorLabel(_errorLabel);

    _layout->addLayout(_containerLayout);
    setLayout(_layout);

    customizeStyle();
}

void WebFlowCredentialsDialog::closeEvent(QCloseEvent* e) {
    Q_UNUSED(e)

#ifdef WITH_WEBENGINE
    if (_webView) {
        // Force calling WebView::~WebView() earlier so that _profile and _page are
        // deleted in the correct order.
        _webView->deleteLater();
        _webView = nullptr;
    }
#endif // WITH_WEBENGINE

    if (_flow2AuthWidget) {
        _flow2AuthWidget->resetAuth();
        _flow2AuthWidget->deleteLater();
        _flow2AuthWidget = nullptr;
    }

    emit onClose();
}

void WebFlowCredentialsDialog::setUrl(const QUrl &url)
{
#ifdef WITH_WEBENGINE
    if (_webView)
        _webView->setUrl(url);
#else // WITH_WEBENGINE
    Q_UNUSED(url);
#endif // WITH_WEBENGINE
}

void WebFlowCredentialsDialog::setInfo(const QString &msg) {
    _infoLabel->setText(msg);
}

void WebFlowCredentialsDialog::setError(const QString &error) {
    // bring window to top
    slotShowSettingsDialog();

    if (_useFlow2 && _flow2AuthWidget) {
        _flow2AuthWidget->setError(error);
        return;
    }

    if (error.isEmpty()) {
        _errorLabel->hide();
    } else {
        _errorLabel->setText(error);
        _errorLabel->show();
    }
}

void WebFlowCredentialsDialog::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::StyleChange:
    case QEvent::PaletteChange:
    case QEvent::ThemeChange:
        customizeStyle();

        // Notify the other widgets (Dark-/Light-Mode switching)
        emit styleChanged();
        break;
    case QEvent::ActivationChange:
        if(isActiveWindow())
            emit onActivate();
        break;
    default:
        break;
    }

    QDialog::changeEvent(e);
}

void WebFlowCredentialsDialog::customizeStyle()
{
    // HINT: Customize dialog's own style here, if necessary in the future (Dark-/Light-Mode switching)
}

void WebFlowCredentialsDialog::slotShowSettingsDialog()
{
    // bring window to top but slightly delay, to avoid being hidden behind the SettingsDialog
    QTimer::singleShot(100, this, [this] {
        ownCloudGui::raiseDialog(this);
    });
}

void WebFlowCredentialsDialog::slotFlow2AuthResult(Flow2Auth::Result r, const QString &errorString, const QString &user, const QString &appPassword)
{
    Q_UNUSED(errorString)
    if(r == Flow2Auth::LoggedIn) {
        emit urlCatched(user, appPassword, QString());
    } else {
        // bring window to top
        slotShowSettingsDialog();
    }
}

} // namespace OCC