Spectral/src/controller.cpp

123 lines
3.7 KiB
C++
Raw Normal View History

2018-02-27 05:10:08 +00:00
#include "controller.h"
#include "matriqueroom.h"
#include "events/eventcontent.h"
#include "events/roommessageevent.h"
2018-02-27 05:10:08 +00:00
#include "csapi/joining.h"
2018-08-04 20:35:31 +00:00
#include <QClipboard>
2018-08-19 06:32:18 +00:00
#include <QSystemTrayIcon>
2018-07-17 08:18:50 +00:00
Controller::Controller(QObject* parent) : QObject(parent) {
2018-08-19 06:32:18 +00:00
tray->setIcon(QIcon(":/asset/img/icon.png"));
tray->setToolTip("Matrique");
2018-09-06 11:34:14 +00:00
connect(tray, &QSystemTrayIcon::activated,
[this](QSystemTrayIcon::ActivationReason r) {
if (r != QSystemTrayIcon::Context) emit toggleWindow();
});
connect(tray, &QSystemTrayIcon::messageClicked, [=] { emit toggleWindow(); });
2018-08-19 06:51:09 +00:00
trayMenu->addAction("Toggle Window", [=] { emit toggleWindow(); });
2018-08-19 06:32:18 +00:00
trayMenu->addAction("Quit", [=] { QApplication::quit(); });
tray->setContextMenu(trayMenu);
tray->show();
Connection::setRoomType<MatriqueRoom>();
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() {
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-09 02:45:26 +00:00
void Controller::loginWithCredentials(QString serverAddr, QString user,
QString pass) {
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() {
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(m_userID, m_token, "");
}
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 =
((Connection*)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);
}
2018-08-04 20:35:31 +00:00
void Controller::copyToClipboard(const QString& text) {
m_clipboard->setText(text);
}
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
}
2018-08-19 06:32:18 +00:00
void Controller::showMessage(const QString& title, const QString& msg,
const QIcon& icon) {
tray->showMessage(title, msg, icon);
}