Implement login/logout in controller.

This commit is contained in:
Black Hat
2018-02-27 19:07:50 +08:00
parent 6d34f1042f
commit 5b1047ed98
5 changed files with 145 additions and 110 deletions

View File

@@ -1,6 +1,54 @@
#include "controller.h"
Controller::Controller(QObject *parent) : QObject(parent)
{
#include "libqmatrixclient/connection.h"
Controller::Controller(QObject *parent) : QObject(parent) {
}
Controller::~Controller() {
}
void Controller::init() {
connect(connection, &Connection::connected,
[=](){
qInfo() << "Matrix connected.";
setUserID(connection->userId());
setToken(connection->accessToken());
}
);
connect(connection, &Connection::resolveError, this, &Controller::reconnect);
connect(connection, &Connection::syncError, this, &Controller::reconnect);
connect(connection, &Connection::syncDone, this, &Controller::resync);
}
void Controller::login(QString home, QString user, QString pass) {
qInfo() << "UserID:" << userID;
qInfo() << "Token:" << token;
qInfo() << "Home:" << home;
qInfo() << "User:" << user;
qInfo() << "Pass:" << pass;
if(!userID.isEmpty() && !token.isEmpty()) {
qInfo() << "Using token.";
connection->connectWithToken(userID, token, "");
} else if(!user.isEmpty() && !pass.isEmpty()) {
qInfo() << "Using given credential.";
connection->connectToServer("@"+user+":"+home, pass, "");
}
}
void Controller::logout() {
userID = "";
token = "";
}
void Controller::resync() {
connection->sync(30000);
}
void Controller::reconnect() {
Controller::connection->connectWithToken(userID, token, "");
}

View File

@@ -3,15 +3,57 @@
#include <QObject>
#include "libqmatrixclient/connection.h"
using namespace QMatrixClient;
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
public:
explicit Controller(QObject *parent = nullptr);
~Controller();
// All the Q_INVOKABLEs.
Q_INVOKABLE void init();
Q_INVOKABLE void login(QString, QString, QString);
Q_INVOKABLE void logout();
// All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs.
QString userID;
QString getUserID() { return userID; }
void setUserID(QString n) {
if(n != userID) {
userID = n;
emit userIDChanged();
}
}
QByteArray token;
QByteArray getToken() { return token; }
void setToken(QByteArray n) {
if(n != token) {
token = n;
emit tokenChanged();
}
}
private:
QMatrixClient::Connection *connection = new QMatrixClient::Connection();
void resync();
void reconnect();
signals:
void userIDChanged();
void tokenChanged();
void homeServerChanged();
public slots:
};
#endif // CONTROLLER_H
#endif // CONTROLLER_H