Spectral/src/controller.cpp

123 lines
3.8 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
#include "csapi/create_room.h"
#include "csapi/joining.h"
#include "csapi/leaving.h"
2018-07-17 08:18:50 +00:00
#include <QFile>
#include <QImage>
#include <QMimeDatabase>
Controller::Controller(QObject* parent) : QObject(parent) {
connect(m_connection, &Connection::connected, this, &Controller::connected);
connect(m_connection, &Connection::resolveError, this,
2018-07-09 02:45:26 +00:00
&Controller::reconnect);
connect(m_connection, &Connection::syncError, this, &Controller::reconnect);
connect(m_connection, &Connection::syncDone, this, &Controller::resync);
connect(m_connection, &Connection::connected, this,
2018-07-09 02:45:26 +00:00
&Controller::connectionChanged);
2018-07-12 06:40:51 +00:00
connect(m_connection, &Connection::connected, [=] { setBusy(true); });
connect(m_connection, &Connection::syncDone, [=] { setBusy(false); });
2018-02-27 11:07:50 +00:00
}
2018-07-17 08:18:50 +00:00
Controller::~Controller() {
m_connection->saveState();
m_connection->stopSync();
m_connection->deleteLater();
}
2018-02-27 11:07:50 +00:00
void Controller::login() {
2018-07-09 02:45:26 +00:00
if (!isLogin) {
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) {
if (!user.isEmpty() && !pass.isEmpty()) {
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 {
2018-07-19 08:29:50 +00:00
qCritical() << "You are already logged in.";
2018-07-09 02:45:26 +00:00
}
2018-02-27 11:07:50 +00:00
}
void Controller::logout() {
m_connection->logout();
2018-07-09 02:45:26 +00:00
setUserID("");
setToken("");
setIsLogin(false);
2018-02-27 11:37:53 +00:00
}
void Controller::connected() {
2018-07-09 02:45:26 +00:00
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() { m_connection->sync(30000); }
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(Room* room, const QUrl& localFile,
2018-07-16 14:05:34 +00:00
const QUrl& mxcUrl) {
2018-07-17 08:18:50 +00:00
const QString mime = getMIME(localFile);
const QString fileName = localFile.toLocalFile();
QString msgType = "m.file";
if (mime.startsWith("image")) msgType = "m.image";
if (mime.startsWith("video")) msgType = "m.video";
if (mime.startsWith("audio")) msgType = "m.audio";
QJsonObject json{QJsonObject{{"msgtype", msgType},
{"body", fileName},
{"filename", fileName},
{"url", mxcUrl.url()}}};
room->postMessage("m.room.message", json);
2018-07-17 08:18:50 +00:00
}
QString Controller::getMIME(const QUrl& fileUrl) const {
QMimeDatabase* db = new QMimeDatabase();
const QString mime = db->mimeTypeForFile(fileUrl.toLocalFile()).name();
2018-07-17 12:41:43 +00:00
delete db;
2018-07-17 08:18:50 +00:00
return mime;
}
void Controller::forgetRoom(const QString& roomID) {
ForgetRoomJob* forgetRoomJob = m_connection->forgetRoom(roomID);
setBusy(true);
forgetRoomJob->connect(forgetRoomJob, &ForgetRoomJob::finished,
[=] { setBusy(false); });
}
void Controller::joinRoom(const QString& alias) {
JoinRoomJob* joinRoomJob = m_connection->joinRoom(alias);
setBusy(true);
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::finished,
[=] { setBusy(false); });
}
void Controller::createRoom(const QString& name, const QString& topic) {
CreateRoomJob* createRoomJob = m_connection->createRoom(
Connection::PublishRoom, "", name, topic, QStringList());
setBusy(true);
createRoomJob->connect(createRoomJob, &CreateRoomJob::finished,
[=] { setBusy(false); });
}
void Controller::createDirectChat(const QString& userID) {
m_connection->requestDirectChat(userID);
}