diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-12 23:47:17 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-06-29 14:09:46 +0200 |
| commit | 05b2584fa4d773f5a88ed3ce98f5dd8304e11c34 (patch) | |
| tree | f72e73c3259b8100e886f49f67ecc669b7667502 /login.cpp | |
| parent | 3b8fafc4122848219898245d52dabd669cacb4ba (diff) | |
First commit
Diffstat (limited to 'login.cpp')
| -rw-r--r-- | login.cpp | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/login.cpp b/login.cpp new file mode 100644 index 0000000..d762f48 --- /dev/null +++ b/login.cpp @@ -0,0 +1,139 @@ +#include "login.h" +#include "ui_login.h" +#include <QString> +#include "QXmppStanza.h" +#include <memory> + +static QString error_to_str(const QXmppStanza::Error::Condition c) +{ + switch (c) + { + case QXmppStanza::Error::BadRequest: + return "BadRequest"; + case QXmppStanza::Error::Conflict: + return "Conflict"; + case QXmppStanza::Error::FeatureNotImplemented: + return "FeatureNotImplemented"; + case QXmppStanza::Error::Forbidden: + return "Forbidden"; + case QXmppStanza::Error::Gone: + return "Gone"; + case QXmppStanza::Error::InternalServerError: + return "internal server error"; + case QXmppStanza::Error::ItemNotFound: + return "item not found"; + case QXmppStanza::Error::JidMalformed: + return "JID Malformed"; + case QXmppStanza::Error::NotAcceptable: + return "not acceptable"; + case QXmppStanza::Error::NotAllowed: + return "not allowed"; + case QXmppStanza::Error::NotAuthorized: + return "not authorized"; + case QXmppStanza::Error::RecipientUnavailable: + return "recipient unavailable"; + case QXmppStanza::Error::Redirect: + return "Redirect"; + case QXmppStanza::Error::RegistrationRequired: + return "RegistrationRequired"; + case QXmppStanza::Error::RemoteServerNotFound: + return "RemoteServerNotFound"; + case QXmppStanza::Error::RemoteServerTimeout: + return "RemoteServerTimeout"; + case QXmppStanza::Error::ResourceConstraint: + return "ResourceConstraint"; + case QXmppStanza::Error::ServiceUnavailable: + return "ServiceUnavailable"; + case QXmppStanza::Error::SubscriptionRequired: + return "SubscriptionRequired"; + case QXmppStanza::Error::UndefinedCondition: + return "UndefinedCondition"; + case QXmppStanza::Error::UnexpectedRequest: + return "UnexpectedRequest"; + case QXmppStanza::Error::PolicyViolation: + return "PolicyViolation"; + + default: + break; + } + + return "UnknownError"; +} + +Login::Login(QDialog *const parent) : + QDialog(parent) +{ + ui.setupUi(this); + ui.error->setVisible(false); + + connect(ui.login_b, &QPushButton::released, + [this] + { + const QString jid = ui.username->text(), pwd = ui.password->text(); + QString domain, error; + + if (!jid_is_valid(jid, domain)) + error = tr("Invalid username"); + else if (pwd.isEmpty()) + error = tr("Invalid password"); + else + setup(jid, pwd, domain); + + ui.error->setText(error); + ui.error->setVisible(!error.isEmpty()); + }); + + showMaximized(); +} + +bool Login::jid_is_valid(const QString &jid, QString &domain) +{ + if (jid.isEmpty() || jid.count('@') != 1) + return false; + else if ((domain = jid.mid(jid.indexOf('@') + 1)).isEmpty()) + return false; + + return true; +} + +void Login::setup(const QString &jid, const QString pwd, const QString &domain) +{ + QXmppConfiguration cfg; + + cfg.setStreamSecurityMode(QXmppConfiguration::TLSRequired); + cfg.setJid(jid); + cfg.setPassword(pwd); + cfg.setAutoReconnectionEnabled(false); + + const auto client = new Client; + + connect(client, &Client::stateChanged, + [d = domain, e = ui.error] (const Client::State state) + { + if (state == Client::ConnectingState) + { + e->setVisible(true); + e->setText(tr("Connecting to ") + d + "..."); + } + }); + + connect(client, &Client::connected, this, + [this, client] + { + Q_EMIT auth_success(client); + close(); + }); + + connect(client, &Client::disconnected, this, + [this, client, e = ui.error] + { + e->setText("Disconnected from server: " + + error_to_str(client->xmppStreamError())); + e->setVisible(true); + ui.login_b->setEnabled(true); + delete client; + }); + + ui.login_b->setEnabled(false); + client->connectToServer(cfg); +} |
