aboutsummaryrefslogtreecommitdiff
path: root/login.cpp
blob: dc5af86dbc3459a9d6a149103695025561c4d9e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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(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);
}