140 lines
4.1 KiB
C++
140 lines
4.1 KiB
C++
#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(jid);
|
|
|
|
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);
|
|
}
|