Uses MatriqueRoom inherited from QMatrixClient::Room and rewrite related

objects.
This commit is contained in:
Black Hat
2018-08-18 16:02:47 +08:00
parent 6640b172d5
commit 3a7f182c52
16 changed files with 202 additions and 177 deletions

View File

@@ -1,19 +1,17 @@
#include "controller.h"
#include "connection.h"
#include "matriqueroom.h"
#include "events/eventcontent.h"
#include "events/roommessageevent.h"
#include "csapi/create_room.h"
#include "csapi/joining.h"
#include "csapi/leaving.h"
#include <QClipboard>
#include <QFile>
#include <QFileDialog>
#include <QImage>
Controller::Controller(QObject* parent) : QObject(parent) {
Connection::setRoomType<MatriqueRoom>();
connect(m_connection, &Connection::connected, this, &Controller::connected);
connect(m_connection, &Connection::resolveError, this,
&Controller::reconnect);
@@ -33,15 +31,15 @@ Controller::~Controller() {
}
void Controller::login() {
if (!isLogin) {
m_connection->setHomeserver(QUrl(homeserver));
m_connection->connectWithToken(userID, token, "");
if (!m_isLogin) {
m_connection->setHomeserver(QUrl(m_homeserver));
m_connection->connectWithToken(m_userID, m_token, "");
}
}
void Controller::loginWithCredentials(QString serverAddr, QString user,
QString pass) {
if (!isLogin) {
if (!m_isLogin) {
if (!user.isEmpty() && !pass.isEmpty()) {
m_connection->setHomeserver(QUrl(serverAddr));
m_connection->connectToServer(user, pass, "");
@@ -71,48 +69,7 @@ void Controller::resync() { m_connection->sync(30000); }
void Controller::reconnect() {
qDebug() << "Connection lost. Reconnecting...";
m_connection->connectWithToken(userID, token, "");
}
void Controller::uploadFile(Room* room) {
if (!room) return;
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) {
room->uploadFile(localFile.toString(), localFile, getMIME(localFile));
QMetaObject::Connection* const connection = new QMetaObject::Connection;
*connection = connect(room, &Room::fileTransferCompleted,
[=](QString id, QUrl localFile, QUrl mxcUrl) {
disconnect(*connection);
postFile(room, localFile, mxcUrl);
});
}
}
void Controller::postFile(Room* room, const QUrl& localFile,
const QUrl& mxcUrl) {
const QString mime = getMIME(localFile);
const QString fileName = localFile.fileName();
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{QJsonObject{{"msgtype", msgType},
{"body", fileName},
{"filename", fileName},
{"url", mxcUrl.url()}}};
room->postJson("m.room.message", json);
}
QString Controller::getMIME(const QUrl& fileUrl) const {
const QString mime = m_db.mimeTypeForFile(fileUrl.toLocalFile()).name();
return mime;
}
void Controller::forgetRoom(const QString& roomID) {
ForgetRoomJob* forgetRoomJob = m_connection->forgetRoom(roomID);
setBusy(true);
forgetRoomJob->connect(forgetRoomJob, &ForgetRoomJob::finished,
[=] { setBusy(false); });
m_connection->connectWithToken(m_userID, m_token, "");
}
void Controller::joinRoom(const QString& alias) {
@@ -123,8 +80,9 @@ void Controller::joinRoom(const QString& alias) {
}
void Controller::createRoom(const QString& name, const QString& topic) {
CreateRoomJob* createRoomJob = m_connection->createRoom(
Connection::PublishRoom, "", name, topic, QStringList());
CreateRoomJob* createRoomJob =
((Connection*)m_connection)
->createRoom(Connection::PublishRoom, "", name, topic, QStringList());
setBusy(true);
createRoomJob->connect(createRoomJob, &CreateRoomJob::finished,
[=] { setBusy(false); });
@@ -138,17 +96,9 @@ void Controller::copyToClipboard(const QString& text) {
m_clipboard->setText(text);
}
void Controller::saveFileAs(Room* room, QString eventId) {
if (!room) return;
auto fileName = QFileDialog::getSaveFileName(
Q_NULLPTR, tr("Save File as"), room->fileNameToDownload(eventId));
if (!fileName.isEmpty())
room->downloadFile(eventId, QUrl::fromLocalFile(fileName));
}
void Controller::acceptRoom(Room* room) { room->setJoinState(JoinState::Join); }
void Controller::rejectRoom(Room* room) {
room->setJoinState(JoinState::Leave);
forgetRoom(room->id());
void Controller::playAudio(QUrl localFile) {
QMediaPlayer* player = new QMediaPlayer;
player->setMedia(localFile);
player->play();
connect(player, &QMediaPlayer::stateChanged, [=] { player->deleteLater(); });
}