16
16
#include " creds/httpcredentialsgui.h"
17
17
#include " account.h"
18
18
#include " application.h"
19
- #include " basicloginwidget.h"
20
19
#include " common/asserts.h"
21
- #include " loginrequireddialog.h"
20
+ #include " gui/loginrequireddialog/basicloginwidget.h"
21
+ #include " gui/loginrequireddialog/loginrequireddialog.h"
22
+ #include " gui/loginrequireddialog/oauthloginwidget.h"
22
23
#include " networkjobs.h"
23
24
#include " settingsdialog.h"
24
25
#include " theme.h"
25
26
26
- #include < QBuffer >
27
+ #include < QClipboard >
27
28
#include < QDesktopServices>
28
- #include < QMessageBox>
29
- #include < QNetworkReply>
29
+ #include < QGuiApplication>
30
30
#include < QTimer>
31
31
32
32
@@ -42,21 +42,12 @@ void HttpCredentialsGui::openBrowser()
42
42
_asyncAuth->openBrowser ();
43
43
} else {
44
44
qCWarning (lcHttpCredentialsGui) << " There is no authentication job running, did the previous attempt fail?" ;
45
- askFromUserAsync ();
45
+ askFromUser ();
46
46
}
47
47
}
48
48
}
49
49
50
50
void HttpCredentialsGui::askFromUser ()
51
- {
52
- // This function can be called from AccountState::slotInvalidCredentials,
53
- // which (indirectly, through HttpCredentials::invalidateToken) schedules
54
- // a cache wipe of the qnam. We can only execute a network job again once
55
- // the cache has been cleared, otherwise we'd interfere with the job.
56
- QTimer::singleShot (0 , this , &HttpCredentialsGui::askFromUserAsync);
57
- }
58
-
59
- void HttpCredentialsGui::askFromUserAsync ()
60
51
{
61
52
if (isUsingOAuth ()) {
62
53
restartOAuth ();
@@ -83,6 +74,11 @@ void HttpCredentialsGui::asyncAuthResult(OAuth::Result r, const QString &token,
83
74
_asyncAuth.reset ();
84
75
switch (r) {
85
76
case OAuth::NotSupported:
77
+ if (_loginRequiredDialog) {
78
+ _loginRequiredDialog->deleteLater ();
79
+ _loginRequiredDialog.clear ();
80
+ }
81
+ // show basic auth dialog for historic reasons
86
82
showDialog ();
87
83
return ;
88
84
case OAuth::ErrorInsecureUrl:
@@ -93,6 +89,9 @@ void HttpCredentialsGui::asyncAuthResult(OAuth::Result r, const QString &token,
93
89
Q_EMIT oAuthErrorOccurred ();
94
90
return ;
95
91
case OAuth::LoggedIn:
92
+ if (_loginRequiredDialog) {
93
+ _loginRequiredDialog->accept ();
94
+ }
96
95
break ;
97
96
}
98
97
@@ -160,10 +159,45 @@ QUrl HttpCredentialsGui::authorisationLink() const
160
159
161
160
void HttpCredentialsGui::restartOAuth ()
162
161
{
162
+ qCDebug (lcHttpCredentialsGui) << " showing modal dialog asking user to log in again via OAuth2" ;
163
+
164
+ if (!_loginRequiredDialog) {
165
+ _loginRequiredDialog = new LoginRequiredDialog (LoginRequiredDialog::Mode::OAuth, ocApp ()->gui ()->settingsDialog ());
166
+
167
+ // make sure it's cleaned up since it's not owned by the account settings (also prevents memory leaks)
168
+ _loginRequiredDialog->setAttribute (Qt::WA_DeleteOnClose);
169
+
170
+ _loginRequiredDialog->setTopLabelText (
171
+ tr (" The account %1 is currently logged out.\n\n Please authenticate using your browser." ).arg (_account->displayName ()));
172
+
173
+ auto *contentWidget = qobject_cast<OAuthLoginWidget *>(_loginRequiredDialog->contentWidget ());
174
+ connect (contentWidget, &OAuthLoginWidget::copyUrlToClipboardButtonClicked, _loginRequiredDialog, [](const QUrl &url) {
175
+ // TODO: use authorisationLinkAsync
176
+ qApp->clipboard ()->setText (url.toString ());
177
+ });
178
+
179
+ connect (contentWidget, &OAuthLoginWidget::openBrowserButtonClicked, this , &HttpCredentialsGui::openBrowser);
180
+ connect (_loginRequiredDialog, &LoginRequiredDialog::rejected, this , &HttpCredentials::requestLogout);
181
+
182
+ connect (contentWidget, &OAuthLoginWidget::retryButtonClicked, _loginRequiredDialog, [contentWidget, this ]() {
183
+ restartOAuth ();
184
+ contentWidget->hideRetryFrame ();
185
+ });
186
+
187
+ connect (this , &HttpCredentialsGui::oAuthErrorOccurred, _loginRequiredDialog, [contentWidget, this ]() {
188
+ Q_ASSERT (!ready ());
189
+ ocApp ()->gui ()->raiseDialog (_loginRequiredDialog);
190
+ contentWidget->showRetryFrame ();
191
+ });
192
+
193
+ _loginRequiredDialog->open ();
194
+ ocApp ()->gui ()->raiseDialog (_loginRequiredDialog);
195
+ }
196
+
163
197
_asyncAuth.reset (new AccountBasedOAuth (_account->sharedFromThis (), this ));
164
- connect (_asyncAuth.data (), &OAuth::result,
165
- this , &HttpCredentialsGui::asyncAuthResult);
166
- connect (_asyncAuth. data (), &OAuth::authorisationLinkChanged, this , &HttpCredentialsGui::authorisationLinkChanged );
198
+ connect (_asyncAuth.data (), &OAuth::result, this , &HttpCredentialsGui::asyncAuthResult);
199
+ connect (_asyncAuth. data () , &OAuth::authorisationLinkChanged, this ,
200
+ [ this ] { qobject_cast<OAuthLoginWidget *>(_loginRequiredDialog-> contentWidget ())-> setUrl ( authorisationLink ()); } );
167
201
_asyncAuth->startAuthentication ();
168
202
}
169
203
0 commit comments