2018-02-27 05:10:08 +00:00
|
|
|
#include "controller.h"
|
|
|
|
|
2018-10-09 03:06:15 +00:00
|
|
|
#include "settings.h"
|
2018-09-17 13:01:02 +00:00
|
|
|
#include "spectralroom.h"
|
|
|
|
#include "spectraluser.h"
|
2018-08-18 08:02:47 +00:00
|
|
|
|
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-10-13 10:54:33 +00:00
|
|
|
#include "csapi/logout.h"
|
2018-08-01 12:26:29 +00:00
|
|
|
|
2018-08-04 20:35:31 +00:00
|
|
|
#include <QClipboard>
|
2018-09-09 02:12:45 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
#include <QtCore/QElapsedTimer>
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
#include <QtCore/QStandardPaths>
|
|
|
|
#include <QtCore/QStringBuilder>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QCloseEvent>
|
|
|
|
#include <QtGui/QDesktopServices>
|
|
|
|
#include <QtGui/QMovie>
|
|
|
|
#include <QtGui/QPixmap>
|
|
|
|
#include <QtNetwork/QAuthenticator>
|
|
|
|
#include <QtNetwork/QNetworkReply>
|
2018-07-17 08:18:50 +00:00
|
|
|
|
2018-10-19 14:02:12 +00:00
|
|
|
Controller::Controller(QObject* parent)
|
2018-10-21 02:17:21 +00:00
|
|
|
: QObject(parent), notificationsManager(this) {
|
|
|
|
QApplication::setQuitOnLastWindowClosed(false);
|
|
|
|
|
2018-10-19 14:02:12 +00:00
|
|
|
connect(¬ificationsManager, &NotificationsManager::notificationClicked,
|
|
|
|
this, &Controller::notificationClicked);
|
2018-08-19 06:32:18 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
Connection::setRoomType<SpectralRoom>();
|
|
|
|
Connection::setUserType<SpectralUser>();
|
2018-08-18 08:02:47 +00:00
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(invokeLogin()));
|
2018-02-27 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
inline QString accessTokenFileName(const AccountSettings& account) {
|
|
|
|
QString fileName = account.userId();
|
|
|
|
fileName.replace(':', '_');
|
|
|
|
return QStandardPaths::writableLocation(
|
|
|
|
QStandardPaths::AppLocalDataLocation) +
|
|
|
|
'/' + fileName;
|
2018-07-17 08:18:50 +00:00
|
|
|
}
|
2018-02-27 11:07:50 +00:00
|
|
|
|
2018-09-09 02:12:45 +00:00
|
|
|
void Controller::loginWithCredentials(QString serverAddr, QString user,
|
|
|
|
QString pass) {
|
|
|
|
if (!user.isEmpty() && !pass.isEmpty()) {
|
|
|
|
Connection* m_connection = new Connection(this);
|
|
|
|
m_connection->setHomeserver(QUrl(serverAddr));
|
|
|
|
m_connection->connectToServer(user, pass, "");
|
|
|
|
connect(m_connection, &Connection::connected, [=] {
|
|
|
|
AccountSettings account(m_connection->userId());
|
|
|
|
account.setKeepLoggedIn(true);
|
|
|
|
account.clearAccessToken(); // Drop the legacy - just in case
|
|
|
|
account.setHomeserver(m_connection->homeserver());
|
|
|
|
account.setDeviceId(m_connection->deviceId());
|
2018-09-17 13:01:02 +00:00
|
|
|
account.setDeviceName("Spectral");
|
2018-09-09 02:12:45 +00:00
|
|
|
if (!saveAccessToken(account, m_connection->accessToken()))
|
|
|
|
qWarning() << "Couldn't save access token";
|
|
|
|
account.sync();
|
|
|
|
addConnection(m_connection);
|
|
|
|
});
|
2018-10-14 22:49:30 +00:00
|
|
|
connect(m_connection, &Connection::networkError,
|
|
|
|
[=](QString error, QByteArray detail) {
|
|
|
|
emit errorOccured("Network", error);
|
|
|
|
});
|
2018-10-09 03:06:15 +00:00
|
|
|
connect(m_connection, &Connection::loginError,
|
|
|
|
[=](QString error, QByteArray detail) {
|
2018-10-13 10:54:33 +00:00
|
|
|
emit errorOccured("Login Failed", error);
|
2018-10-09 03:06:15 +00:00
|
|
|
});
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-07-07 09:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
void Controller::logout(Connection* conn) {
|
|
|
|
if (!conn) {
|
|
|
|
qCritical() << "Attempt to logout null connection";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SettingsGroup("Accounts").remove(conn->userId());
|
|
|
|
QFile(accessTokenFileName(AccountSettings(conn->userId()))).remove();
|
|
|
|
|
2018-10-13 10:54:33 +00:00
|
|
|
auto job = conn->callApi<LogoutJob>();
|
|
|
|
connect(job, &LogoutJob::finished, conn, [=] {
|
|
|
|
conn->stopSync();
|
|
|
|
emit conn->stateChanged();
|
|
|
|
emit conn->loggedOut();
|
|
|
|
});
|
|
|
|
connect(job, &LogoutJob::failure, this, [=] {
|
|
|
|
emit errorOccured("Server-side Logout Failed", job->errorString());
|
|
|
|
});
|
2018-09-09 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 02:12:45 +00:00
|
|
|
void Controller::addConnection(Connection* c) {
|
|
|
|
Q_ASSERT_X(c, __FUNCTION__, "Attempt to add a null connection");
|
|
|
|
|
|
|
|
m_connections.push_back(c);
|
|
|
|
|
2018-11-07 08:29:24 +00:00
|
|
|
connect(c, &Connection::syncDone, this, [=] {
|
|
|
|
c->saveState();
|
|
|
|
c->sync(30000);
|
|
|
|
});
|
2018-09-09 02:12:45 +00:00
|
|
|
connect(c, &Connection::loggedOut, this, [=] { dropConnection(c); });
|
|
|
|
|
|
|
|
using namespace QMatrixClient;
|
|
|
|
|
|
|
|
c->sync(30000);
|
|
|
|
|
|
|
|
emit connectionAdded(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::dropConnection(Connection* c) {
|
|
|
|
Q_ASSERT_X(c, __FUNCTION__, "Attempt to drop a null connection");
|
|
|
|
m_connections.removeOne(c);
|
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
emit connectionDropped(c);
|
2018-09-09 02:12:45 +00:00
|
|
|
c->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::invokeLogin() {
|
|
|
|
using namespace QMatrixClient;
|
|
|
|
const auto accounts = SettingsGroup("Accounts").childGroups();
|
|
|
|
for (const auto& accountId : accounts) {
|
|
|
|
AccountSettings account{accountId};
|
|
|
|
if (!account.homeserver().isEmpty()) {
|
|
|
|
auto accessToken = loadAccessToken(account);
|
|
|
|
|
|
|
|
auto c = new Connection(account.homeserver(), this);
|
|
|
|
auto deviceName = account.deviceName();
|
|
|
|
connect(c, &Connection::connected, this, [=] {
|
|
|
|
c->loadState();
|
|
|
|
addConnection(c);
|
|
|
|
});
|
2018-10-14 22:49:30 +00:00
|
|
|
connect(c, &Connection::loginError,
|
|
|
|
[=](QString error, QByteArray detail) {
|
|
|
|
emit errorOccured("Login Failed", error);
|
|
|
|
});
|
|
|
|
connect(c, &Connection::networkError,
|
|
|
|
[=](QString error, QByteArray detail) {
|
|
|
|
emit errorOccured("Network", error);
|
|
|
|
});
|
2018-09-09 02:12:45 +00:00
|
|
|
c->connectWithToken(account.userId(), accessToken, account.deviceId());
|
2018-02-27 11:07:50 +00:00
|
|
|
}
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-09-13 00:22:41 +00:00
|
|
|
emit initiated();
|
2018-02-27 11:07:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 02:12:45 +00:00
|
|
|
QByteArray Controller::loadAccessToken(const AccountSettings& account) {
|
|
|
|
QFile accountTokenFile{accessTokenFileName(account)};
|
|
|
|
if (accountTokenFile.open(QFile::ReadOnly)) {
|
|
|
|
if (accountTokenFile.size() < 1024) return accountTokenFile.readAll();
|
|
|
|
|
|
|
|
qWarning() << "File" << accountTokenFile.fileName() << "is"
|
|
|
|
<< accountTokenFile.size()
|
|
|
|
<< "bytes long - too long for a token, ignoring it.";
|
|
|
|
}
|
|
|
|
qWarning() << "Could not open access token file"
|
|
|
|
<< accountTokenFile.fileName();
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Controller::saveAccessToken(const AccountSettings& account,
|
|
|
|
const QByteArray& accessToken) {
|
|
|
|
// (Re-)Make a dedicated file for access_token.
|
|
|
|
QFile accountTokenFile{accessTokenFileName(account)};
|
|
|
|
accountTokenFile.remove(); // Just in case
|
|
|
|
|
|
|
|
auto fileDir = QFileInfo(accountTokenFile).dir();
|
|
|
|
if (!((fileDir.exists() || fileDir.mkpath(".")) &&
|
|
|
|
accountTokenFile.open(QFile::WriteOnly))) {
|
2018-09-20 05:43:47 +00:00
|
|
|
emit errorOccured("Token", "Cannot save access token.");
|
2018-09-09 02:12:45 +00:00
|
|
|
} else {
|
2018-09-17 04:27:00 +00:00
|
|
|
accountTokenFile.write(accessToken);
|
|
|
|
return true;
|
2018-09-09 02:12:45 +00:00
|
|
|
}
|
|
|
|
return false;
|
2018-02-27 11:37:53 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
void Controller::joinRoom(Connection* c, const QString& alias) {
|
|
|
|
JoinRoomJob* joinRoomJob = c->joinRoom(alias);
|
2018-10-13 10:54:33 +00:00
|
|
|
joinRoomJob->connect(joinRoomJob, &JoinRoomJob::failure, [=] {
|
|
|
|
emit errorOccured("Join Room Failed", joinRoomJob->errorString());
|
|
|
|
});
|
2018-08-01 12:26:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 13:13:43 +00:00
|
|
|
void Controller::createRoom(Connection* c, const QString& name,
|
|
|
|
const QString& topic) {
|
|
|
|
CreateRoomJob* createRoomJob =
|
|
|
|
c->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
|
2018-10-13 10:54:33 +00:00
|
|
|
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
|
|
|
emit errorOccured("Create Room Failed", createRoomJob->errorString());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::createDirectChat(Connection* c, const QString& userID) {
|
|
|
|
CreateRoomJob* createRoomJob = c->createDirectChat(userID);
|
|
|
|
createRoomJob->connect(createRoomJob, &CreateRoomJob::failure, [=] {
|
|
|
|
emit errorOccured("Create Direct Chat Failed",
|
|
|
|
createRoomJob->errorString());
|
|
|
|
});
|
2018-08-01 12:26:29 +00:00
|
|
|
}
|
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
|
|
|
}
|
2018-08-19 06:32:18 +00:00
|
|
|
|
2018-09-10 07:01:01 +00:00
|
|
|
QColor Controller::color(QString userId) {
|
|
|
|
return QColor(SettingsGroup("UI/Color").value(userId, "#498882").toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Controller::setColor(QString userId, QColor newColor) {
|
|
|
|
SettingsGroup("UI/Color").setValue(userId, newColor.name());
|
|
|
|
}
|
2018-10-19 14:02:12 +00:00
|
|
|
|
|
|
|
void Controller::postNotification(const QString& roomId, const QString& eventId,
|
|
|
|
const QString& roomName,
|
|
|
|
const QString& senderName,
|
2018-10-20 06:59:37 +00:00
|
|
|
const QString& text, const QImage& icon,
|
|
|
|
const QUrl& iconPath) {
|
2018-10-19 14:02:12 +00:00
|
|
|
notificationsManager.postNotification(roomId, eventId, roomName, senderName,
|
2018-10-20 06:59:37 +00:00
|
|
|
text, icon, iconPath);
|
2018-10-19 14:02:12 +00:00
|
|
|
}
|