]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/HttpAuthenticationDialog.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[PrivacyBrowserPC.git] / src / dialogs / HttpAuthenticationDialog.cpp
1 /*
2  * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers.
21 #include "HttpAuthenticationDialog.h"
22 #include "ui_HttpAuthenticationDialog.h"
23
24 // Qt toolkit headers.
25 #include <QPushButton>
26 #include <QUrl>
27
28 // Construct the class.
29 HttpAuthenticationDialog::HttpAuthenticationDialog(QWidget *parentWidgetPointer, const QUrl &requestUrl, QAuthenticator *authenticatorPointer) :
30                                                    QDialog(parentWidgetPointer), authenticatorPointer(authenticatorPointer)
31 {
32     // Set the window title.
33     setWindowTitle(i18nc("The HTTP authentication dialog window title.", "HTTP Authentication"));
34
35     // Set the window modality.
36     setWindowModality(Qt::WindowModality::ApplicationModal);
37
38     // Instantiate the HTTP authentication dialog UI.
39     Ui::HttpAuthenticationDialog httpAuthenticationDialogUi;
40
41     // Setup the UI.
42     httpAuthenticationDialogUi.setupUi(this);
43
44     // Get handles for the widgets.
45     QLabel *realmLabelPointer = httpAuthenticationDialogUi.realmLabel;
46     QLabel *hostLabelPointer = httpAuthenticationDialogUi.hostLabel;
47     usernameLineEditPointer = httpAuthenticationDialogUi.usernameLineEdit;
48     passwordLineEditPointer = httpAuthenticationDialogUi.passwordLineEdit;
49     QDialogButtonBox *dialogButtonBoxPointer = httpAuthenticationDialogUi.dialogButtonBox;
50     okButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Ok);
51
52     // Display the labels.
53     realmLabelPointer->setText(authenticatorPointer->realm());
54     hostLabelPointer->setText(requestUrl.host());
55
56     // Connect the buttons.
57     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(authenticate()));
58     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
59
60     // Update the UI when the line edits change.
61     connect(usernameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
62     connect(passwordLineEditPointer, SIGNAL(passwordChanged(const QString&)), this, SLOT(updateUi()));
63
64     // Initially disable the OK button.
65     okButtonPointer->setEnabled(false);
66 }
67
68 void HttpAuthenticationDialog::authenticate()
69 {
70     // Populate the authenticator.
71     authenticatorPointer->setUser(usernameLineEditPointer->text());
72     authenticatorPointer->setPassword(passwordLineEditPointer->password());
73
74     // Close the dialog.
75     close();
76 }
77
78 void HttpAuthenticationDialog::updateUi()
79 {
80     // Update the OK button status
81     if (usernameLineEditPointer->text().isEmpty() || passwordLineEditPointer->password().isEmpty())  // At least one of the line edits is empty.
82     {
83         // Disable the OK button.
84         okButtonPointer->setEnabled(false);
85     }
86     else  // Both of the line edits are populated.
87     {
88         // Enable the OK button.
89         okButtonPointer->setEnabled(true);
90     }
91 }