| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/gui/authenticationdetails.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | |
| 7 | #include "ui_authenticationdetails.h" |
| 8 | |
| 9 | AuthenticationDetails::AuthenticationDetails(bool only_basic, QWidget* parent) |
| 10 | : QWidget(parent), m_ui(new Ui::AuthenticationDetails()) { |
| 11 | m_ui->setupUi(this); |
| 12 | |
| 13 | // Set text boxes. |
| 14 | m_ui->m_txtPassword->lineEdit()->setPasswordMode(true); |
| 15 | m_ui->m_txtUsername->lineEdit()->setPlaceholderText(tr("Username")); |
| 16 | m_ui->m_txtUsername->lineEdit()->setToolTip(tr("Set username to access the feed.")); |
| 17 | m_ui->m_txtPassword->lineEdit()->setPlaceholderText(tr("Password")); |
| 18 | m_ui->m_txtPassword->lineEdit()->setToolTip(tr("Set password to access the feed.")); |
| 19 | |
| 20 | m_ui->m_cbAuthType->addItem(tr("No authentication"), |
| 21 | QVariant::fromValue(NetworkFactory::NetworkAuthentication::NoAuthentication)); |
| 22 | m_ui->m_cbAuthType->addItem(tr("HTTP Basic"), QVariant::fromValue(NetworkFactory::NetworkAuthentication::Basic)); |
| 23 | |
| 24 | if (!only_basic) { |
| 25 | m_ui->m_cbAuthType->addItem(tr("Token"), QVariant::fromValue(NetworkFactory::NetworkAuthentication::Token)); |
| 26 | } |
| 27 | |
| 28 | connect(m_ui->m_txtUsername->lineEdit(), &BaseLineEdit::textChanged, this, &AuthenticationDetails::onUsernameChanged); |
| 29 | connect(m_ui->m_txtPassword->lineEdit(), &BaseLineEdit::textChanged, this, &AuthenticationDetails::onPasswordChanged); |
| 30 | connect(m_ui->m_cbAuthType, |
| 31 | QOverload<int>::of(&QComboBox::currentIndexChanged), |
| 32 | this, |
| 33 | &AuthenticationDetails::onAuthenticationSwitched); |
| 34 | |
| 35 | onAuthenticationSwitched(); |
| 36 | } |
| 37 | |
| 38 | QString AuthenticationDetails::username() const { |
| 39 | return m_ui->m_txtUsername->lineEdit()->text(); |
| 40 | } |
| 41 | |
| 42 | void AuthenticationDetails::setUsername(const QString& username) { |
| 43 | m_ui->m_txtUsername->lineEdit()->setText(username); |
| 44 | } |
| 45 | |
| 46 | QString AuthenticationDetails::password() const { |
| 47 | return m_ui->m_txtPassword->lineEdit()->text(); |
| 48 | } |
| 49 | |
| 50 | void AuthenticationDetails::setPassword(const QString& password) { |
| 51 | m_ui->m_txtPassword->lineEdit()->setText(password); |
| 52 | } |
| 53 | |
| 54 | AuthenticationDetails::~AuthenticationDetails() = default; |
| 55 | |
| 56 | void AuthenticationDetails::setAuthenticationType(NetworkFactory::NetworkAuthentication protect) { |
| 57 | auto fnd = m_ui->m_cbAuthType->findData(QVariant::fromValue(protect)); |
| 58 | |
| 59 | if (fnd >= 0) { |
| 60 | m_ui->m_cbAuthType->setCurrentIndex(fnd); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | NetworkFactory::NetworkFactory::NetworkAuthentication AuthenticationDetails::authenticationType() const { |
| 65 | return m_ui->m_cbAuthType->currentData().value<NetworkFactory::NetworkAuthentication>(); |
| 66 | } |
| 67 | |
| 68 | void AuthenticationDetails::onUsernameChanged(const QString& new_username) { |
| 69 | bool is_username_ok = authenticationType() == NetworkFactory::NetworkAuthentication::NoAuthentication || |
| 70 | !new_username.simplified().isEmpty(); |
| 71 | |
| 72 | m_ui->m_txtUsername->setStatus(is_username_ok ? LineEditWithStatus::StatusType::Ok |
| 73 | : LineEditWithStatus::StatusType::Warning, |
| 74 | is_username_ok ? tr("Username/token is ok or it is not needed.") |
| 75 | : tr("Username/token is empty.")); |
| 76 | } |
| 77 | |
| 78 | void AuthenticationDetails::onPasswordChanged(const QString& new_password) { |
| 79 | bool is_password_ok = authenticationType() == NetworkFactory::NetworkAuthentication::NoAuthentication || |
| 80 | !new_password.simplified().isEmpty(); |
| 81 | |
| 82 | m_ui->m_txtPassword->setStatus(is_password_ok ? LineEditWithStatus::StatusType::Ok |
| 83 | : LineEditWithStatus::StatusType::Warning, |
| 84 | is_password_ok ? tr("Password is ok or it is not needed.") : tr("Password is empty.")); |
| 85 | } |
| 86 | |
| 87 | void AuthenticationDetails::onAuthenticationSwitched() { |
| 88 | onUsernameChanged(m_ui->m_txtUsername->lineEdit()->text()); |
| 89 | onPasswordChanged(m_ui->m_txtPassword->lineEdit()->text()); |
| 90 | |
| 91 | auto prot = authenticationType(); |
| 92 | |
| 93 | m_ui->m_lblPassword->setVisible(prot != NetworkFactory::NetworkAuthentication::Token); |
| 94 | m_ui->m_txtPassword->setVisible(prot != NetworkFactory::NetworkAuthentication::Token); |
| 95 | |
| 96 | if (prot == NetworkFactory::NetworkAuthentication::Token) { |
| 97 | m_ui->m_lblUsername->setText(tr("Access token")); |
| 98 | } |
| 99 | else { |
| 100 | m_ui->m_lblUsername->setText(tr("Username")); |
| 101 | } |
| 102 | |
| 103 | m_ui->m_gbAuthentication->setEnabled(prot != NetworkFactory::NetworkAuthentication::NoAuthentication); |
| 104 | } |