2018-02-27 05:10:08 +00:00
|
|
|
#include "controller.h"
|
|
|
|
|
2018-07-08 05:25:46 +00:00
|
|
|
#include "connection.h"
|
2018-07-15 08:02:26 +00:00
|
|
|
#include "events/eventcontent.h"
|
|
|
|
#include "events/roommessageevent.h"
|
2018-02-27 05:10:08 +00:00
|
|
|
|
2018-07-17 08:18:50 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QImage>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
2018-07-15 08:02:26 +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-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
|
|
|
|
2018-07-07 09:38:20 +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-07 09:38:20 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
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() {
|
2018-07-09 02:45:26 +00:00
|
|
|
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
|
|
|
}
|
2018-07-15 08:02:26 +00:00
|
|
|
|
2018-07-16 14:05:34 +00:00
|
|
|
void Controller::postFile(QMatrixClient::Room* room, const QUrl& localFile,
|
|
|
|
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";
|
2018-07-29 13:46:57 +00:00
|
|
|
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;
|
2018-07-15 08:02:26 +00:00
|
|
|
}
|