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) {
|
|
|
|
qDebug() << "UserID:" << userID;
|
|
|
|
qDebug() << "Token:" << token;
|
2018-07-07 09:38:20 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
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";
|
|
|
|
QJsonObject json{{"content", QJsonObject{{"msgtype", msgType},
|
|
|
|
{"body", fileName},
|
|
|
|
{"filename", fileName},
|
|
|
|
{"url", mxcUrl.url()}}}};
|
|
|
|
room->postMessage(QMatrixClient::RoomMessageEvent(json));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Controller::getMIME(const QUrl& fileUrl) const {
|
|
|
|
QMimeDatabase* db = new QMimeDatabase();
|
|
|
|
const QString mime = db->mimeTypeForFile(fileUrl.toLocalFile()).name();
|
|
|
|
free(db);
|
|
|
|
return mime;
|
2018-07-15 08:02:26 +00:00
|
|
|
}
|