Spectral/matrix/controller.cpp

57 lines
1.6 KiB
C++
Raw Normal View History

2018-02-27 05:10:08 +00:00
#include "controller.h"
2018-02-27 11:07:50 +00:00
#include "libqmatrixclient/connection.h"
2018-02-27 05:10:08 +00:00
2018-02-27 11:07:50 +00:00
Controller::Controller(QObject *parent) : QObject(parent) {
2018-02-28 09:10:42 +00:00
connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected);
connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect);
connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect);
connect(m_connection, &QMatrixClient::Connection::syncDone, this, &Controller::resync);
2018-02-27 11:07:50 +00:00
}
Controller::~Controller() {
}
2018-02-27 11:37:53 +00:00
void Controller::login(QString home, QString user, QString pass) {
if(home.isEmpty()) home = "matrix.org";
2018-02-27 11:07:50 +00:00
2018-02-27 11:37:53 +00:00
qDebug() << "UserID:" << userID;
qDebug() << "Token:" << token;
qDebug() << "Home:" << home;
qDebug() << "User:" << user;
qDebug() << "Pass:" << pass;
2018-02-27 11:07:50 +00:00
if(!userID.isEmpty() && !token.isEmpty()) {
2018-02-27 11:37:53 +00:00
qDebug() << "Using token.";
2018-02-28 09:10:42 +00:00
m_connection->connectWithToken(userID, token, "");
2018-02-27 11:07:50 +00:00
} else if(!user.isEmpty() && !pass.isEmpty()) {
2018-02-27 11:37:53 +00:00
qDebug() << "Using given credential.";
2018-02-28 09:10:42 +00:00
m_connection->connectToServer("@"+user+":"+home, pass, "");
2018-02-27 11:07:50 +00:00
}
}
void Controller::logout() {
userID = "";
token = "";
2018-02-27 11:37:53 +00:00
setIsLogin(false);
}
void Controller::connected() {
2018-02-28 09:10:42 +00:00
setUserID(m_connection->userId());
setToken(m_connection->accessToken());
roomListModel->init(m_connection);
resync();
2018-02-27 11:37:53 +00:00
setIsLogin(true);
2018-02-27 11:07:50 +00:00
}
void Controller::resync() {
2018-02-28 13:11:42 +00:00
qDebug() << "Syncing Matrix.";
2018-02-28 09:10:42 +00:00
m_connection->sync(30000);
2018-02-27 11:07:50 +00:00
}
void Controller::reconnect() {
2018-02-28 13:11:42 +00:00
qDebug() << "Connection lost. Reconnecting...";
2018-02-28 09:10:42 +00:00
m_connection->connectWithToken(userID, token, "");
2018-02-27 05:10:08 +00:00
}