Spectral/matrix/controller.cpp

69 lines
2.0 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) {
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-07-07 11:06:13 +00:00
m_connection->stopSync();
2018-02-27 11:07:50 +00:00
}
void Controller::login() {
if (!isLogin) {
2018-03-05 11:11:55 +00:00
qDebug() << "UserID:" << userID;
qDebug() << "Token:" << token;
m_connection->setHomeserver(QUrl(homeserver));
m_connection->connectWithToken(userID, token, "");
}
}
void Controller::loginWithCredentials(QString serverAddr, QString user, QString pass) {
if(!isLogin) {
qDebug() << "Server:" << serverAddr;
2018-03-05 11:11:55 +00:00
qDebug() << "User:" << user;
qDebug() << "Pass:" << pass;
if(!user.isEmpty() && !pass.isEmpty()) {
2018-03-05 11:11:55 +00:00
qDebug() << "Using given credential.";
m_connection->setHomeserver(QUrl(serverAddr));
m_connection->connectToServer(user, pass, "");
2018-03-05 11:11:55 +00:00
}
} else {
qDebug() << "You are already logged in.";
2018-02-27 11:07:50 +00:00
}
}
void Controller::logout() {
qDebug() << "Logging out.";
setUserID("");
setToken("");
2018-02-27 11:37:53 +00:00
setIsLogin(false);
}
void Controller::connected() {
qDebug() << "Logged in.";
setHomeserver(m_connection->homeserver().toString());
2018-02-28 09:10:42 +00:00
setUserID(m_connection->userId());
setToken(m_connection->accessToken());
2018-03-03 14:14:04 +00:00
m_connection->loadState();
2018-02-28 09:10:42 +00:00
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-03-03 14:14:04 +00:00
m_connection->saveState();
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
}