Spectral/src/controller.cpp

87 lines
2.5 KiB
C++
Raw Normal View History

2018-02-27 05:10:08 +00:00
#include "controller.h"
#include "connection.h"
#include "events/eventcontent.h"
#include "events/roommessageevent.h"
2018-02-27 05:10:08 +00:00
Controller::Controller(QObject* parent) : QObject(parent) {
2018-07-09 02:45:26 +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);
connect(m_connection, &QMatrixClient::Connection::connected, this,
&Controller::connectionChanged);
2018-07-12 06:40:51 +00:00
connect(m_connection, &QMatrixClient::Connection::connected,
[=] { setBusy(true); });
connect(m_connection, &QMatrixClient::Connection::syncDone,
[=] { setBusy(false); });
2018-02-27 11:07:50 +00:00
}
2018-07-09 02:45:26 +00:00
Controller::~Controller() { m_connection->stopSync(); }
2018-02-27 11:07:50 +00:00
void Controller::login() {
2018-07-09 02:45:26 +00:00
if (!isLogin) {
qDebug() << "UserID:" << userID;
qDebug() << "Token:" << token;
2018-07-09 02:45:26 +00:00
m_connection->setHomeserver(QUrl(homeserver));
m_connection->connectWithToken(userID, token, "");
}
}
2018-07-09 02:45:26 +00:00
void Controller::loginWithCredentials(QString serverAddr, QString user,
QString pass) {
if (!isLogin) {
qDebug() << "Server:" << serverAddr;
qDebug() << "User:" << user;
qDebug() << "Pass:" << pass;
2018-03-05 11:11:55 +00:00
2018-07-09 02:45:26 +00:00
if (!user.isEmpty() && !pass.isEmpty()) {
qDebug() << "Using given credential.";
m_connection->setHomeserver(QUrl(serverAddr));
m_connection->connectToServer(user, pass, "");
2018-02-27 11:07:50 +00:00
}
2018-07-09 02:45:26 +00:00
} else {
qDebug() << "You are already logged in.";
}
2018-02-27 11:07:50 +00:00
}
void Controller::logout() {
2018-07-09 02:45:26 +00:00
qDebug() << "Logging out.";
setUserID("");
setToken("");
setIsLogin(false);
2018-02-27 11:37:53 +00:00
}
void Controller::connected() {
2018-07-09 02:45:26 +00:00
qDebug() << "Logged in.";
setHomeserver(m_connection->homeserver().toString());
setUserID(m_connection->userId());
setToken(m_connection->accessToken());
m_connection->loadState();
resync();
setIsLogin(true);
2018-02-27 11:07:50 +00:00
}
void Controller::resync() {
2018-07-09 02:45:26 +00:00
qDebug() << "Syncing Matrix.";
m_connection->sync(30000);
m_connection->saveState();
2018-02-27 11:07:50 +00:00
}
void Controller::reconnect() {
2018-07-09 02:45:26 +00:00
qDebug() << "Connection lost. Reconnecting...";
m_connection->connectWithToken(userID, token, "");
2018-02-27 05:10:08 +00:00
}
void Controller::postFile(QMatrixClient::Room* room,
const QUrl& localFilename) {
// auto job = m_connection->uploadFile(localFilename.toLocalFile());
// room->fileTransferInfo(localFilename.fileName())
}