2 * Copyright 2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
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.
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.
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/>.
20 // Application headers.
21 #include "HttpAuthenticationDialog.h"
22 #include "ui_HttpAuthenticationDialog.h"
24 // Qt toolkit headers.
25 #include <QPushButton>
28 // Construct the class.
29 HttpAuthenticationDialog::HttpAuthenticationDialog(QWidget *parentWidgetPointer, const QUrl &requestUrl, QAuthenticator *authenticatorPointer) :
30 QDialog(parentWidgetPointer), authenticatorPointer(authenticatorPointer)
32 // Set the window title.
33 setWindowTitle(i18nc("The HTTP authentication dialog window title.", "HTTP Authentication"));
35 // Set the window modality.
36 setWindowModality(Qt::WindowModality::ApplicationModal);
38 // Instantiate the HTTP authentication dialog UI.
39 Ui::HttpAuthenticationDialog httpAuthenticationDialogUi;
42 httpAuthenticationDialogUi.setupUi(this);
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);
52 // Display the labels.
53 realmLabelPointer->setText(authenticatorPointer->realm());
54 hostLabelPointer->setText(requestUrl.host());
56 // Connect the buttons.
57 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(authenticate()));
58 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
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()));
64 // Initially disable the OK button.
65 okButtonPointer->setEnabled(false);
68 void HttpAuthenticationDialog::authenticate()
70 // Populate the authenticator.
71 authenticatorPointer->setUser(usernameLineEditPointer->text());
72 authenticatorPointer->setPassword(passwordLineEditPointer->password());
78 void HttpAuthenticationDialog::updateUi()
80 // Update the OK button status
81 if (usernameLineEditPointer->text().isEmpty() || passwordLineEditPointer->password().isEmpty()) // At least one of the line edits is empty.
83 // Disable the OK button.
84 okButtonPointer->setEnabled(false);
86 else // Both of the line edits are populated.
88 // Enable the OK button.
89 okButtonPointer->setEnabled(true);