2018-02-27 05:10:08 +00:00
|
|
|
#include "controller.h"
|
|
|
|
|
2018-08-18 08:02:47 +00:00
|
|
|
#include "matriqueroom.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-08-01 12:26:29 +00:00
|
|
|
#include "csapi/joining.h"
|
|
|
|
|
2018-08-04 20:35:31 +00:00
|
|
|
#include <QClipboard>
|
2018-07-17 08:18:50 +00:00
|
|
|
|
2018-07-15 08:02:26 +00:00
|
|
|
Controller::Controller(QObject* parent) : QObject(parent) {
|
2018-08-18 08:02:47 +00:00
|
|
|
Connection::setRoomType<MatriqueRoom>();
|
|
|
|
|
2018-08-01 12:26:29 +00:00
|
|
|
connect(m_connection, &Connection::connected, this, &Controller::connected);
|
2018-07-29 16:00:41 +00:00
|
|
|
connect(m_connection, &Connection::resolveError, this,
|
2018-07-09 02:45:26 +00:00
|
|
|
&Controller::reconnect);
|
2018-08-01 12:26:29 +00:00
|
|
|
connect(m_connection, &Connection::syncError, this, &Controller::reconnect);
|
|
|
|
connect(m_connection, &Connection::syncDone, this, &Controller::resync);
|
2018-07-29 16:00:41 +00:00
|
|
|
connect(m_connection, &Connection::connected, this,
|
2018-07-09 02:45:26 +00:00
|
|
|
&Controller::connectionChanged);
|
2018-07-12 06:40:51 +00:00
|
|
|
|
2018-08-01 12:26:29 +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
|
|
|
|
2018-07-07 09:38:20 +00:00
|
|
|
void Controller::login() {
|
2018-08-18 08:02:47 +00:00
|
|
|
if (!m_isLogin) {
|
|
|
|
m_connection->setHomeserver(QUrl(m_homeserver));
|
|
|
|
m_connection->connectWithToken(m_userID, m_token, "");
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
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) {
|
2018-08-18 08:02:47 +00:00
|
|
|
if (!m_isLogin) {
|
2018-07-09 02:45:26 +00:00
|
|
|
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-08-01 12:26:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-01 12:26:29 +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...";
|
2018-08-18 08:02:47 +00:00
|
|
|
m_connection->connectWithToken(m_userID, m_token, "");
|
2018-08-01 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-08-18 08:02:47 +00:00
|
|
|
CreateRoomJob* createRoomJob =
|
|
|
|
((Connection*)m_connection)
|
|
|
|
->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
2018-08-01 12:26:29 +00:00
|
|
|
setBusy(true);
|
|
|
|
createRoomJob->connect(createRoomJob, &CreateRoomJob::finished,
|
|
|
|
[=] { setBusy(false); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::createDirectChat(const QString& userID) {
|
|
|
|
m_connection->requestDirectChat(userID);
|
|
|
|
}
|
2018-08-04 20:35:31 +00:00
|
|
|
|
|
|
|
void Controller::copyToClipboard(const QString& text) {
|
|
|
|
m_clipboard->setText(text);
|
|
|
|
}
|
2018-08-14 06:05:41 +00:00
|
|
|
|
2018-08-18 08:02:47 +00:00
|
|
|
void Controller::playAudio(QUrl localFile) {
|
|
|
|
QMediaPlayer* player = new QMediaPlayer;
|
|
|
|
player->setMedia(localFile);
|
|
|
|
player->play();
|
|
|
|
connect(player, &QMediaPlayer::stateChanged, [=] { player->deleteLater(); });
|
2018-08-17 04:55:57 +00:00
|
|
|
}
|