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

@ -25,7 +25,8 @@ SOURCES += src/main.cpp \
src/imageprovider.cpp \
src/messageeventmodel.cpp \
src/imageproviderconnection.cpp \
src/emojimodel.cpp
src/emojimodel.cpp \
src/matriqueroom.cpp
RESOURCES += \
res.qrc
@ -66,7 +67,8 @@ HEADERS += \
src/imageprovider.h \
src/messageeventmodel.h \
src/imageproviderconnection.h \
src/emojimodel.h
src/emojimodel.h \
src/matriqueroom.h
unix:!mac {
metainfo.files = $$PWD/org.eu.encom.matrique.appdata.xml

View File

@ -11,7 +11,7 @@
Matrique is a glossy cross-platform client for Matrix, the decentralized communication protocol for instant messaging.
</p>
<p>
The source code is generally available at <a href="https://gitlab.com/b0/matrique" target="_blank">Gitlab</a>
The source code is generally available at https://gitlab.com/b0/matrique
</p>
</description>
<screenshots>

View File

@ -12,8 +12,6 @@ AvatarContainer {
id: messageRow
Audio { id: audioPlayer }
DownloadableContent {
id: downloadable
@ -37,7 +35,7 @@ AvatarContainer {
onClicked: {
if (downloadable.downloaded)
play()
matriqueController.playAudio(progressInfo.localPath)
else
{
playOnFinished = true
@ -46,11 +44,7 @@ AvatarContainer {
}
}
}
onDownloadedChanged: downloaded && playOnFinished ? play() : {}
onDownloadedChanged: downloaded && playOnFinished ? matriqueController.playAudio(progressInfo.localPath) : {}
}
function play() {
audioPlayer.source = progressInfo.localPath
audioPlayer.play()
}
}

View File

@ -18,7 +18,7 @@ Item {
onDownloadedChanged: downloaded && openOnFinished ? openSavedFile() : {}
function saveFileAs() {
matriqueController.saveFileAs(currentRoom, eventId)
currentRoom.saveFileAs(eventId)
}
function downloadAndOpen()

View File

@ -303,20 +303,7 @@ Item {
contentItem: MaterialIcon { icon: "\ue226" }
onClicked: {
var fileTransferProgressCallback = function(id, sent, total) {
inputField.progress = sent / total
}
var completedCallback = function(id, localFile, mxcUrl) {
inputField.progress = 0
currentRoom.fileTransferCompleted.disconnect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.disconnect(completedCallback)
}
currentRoom.fileTransferProgress.connect(fileTransferProgressCallback)
currentRoom.fileTransferCompleted.connect(completedCallback)
matriqueController.uploadFile(currentRoom)
}
onClicked: currentRoom.chooseAndUploadFile()
}
TextField {
@ -340,14 +327,6 @@ Item {
background: Rectangle {
color: Material.theme == Material.Light ? "#eaeaea" : "#242424"
Rectangle {
z: 5
width: inputField.width * inputField.progress
height: parent.height
color: Material.accent
opacity: 0.4
}
}
function postMessage(text) {

View File

@ -217,8 +217,8 @@ Item {
contentItem: Label { text: "Accept this invitation?" }
onAccepted: matriqueController.acceptRoom(currentRoom)
onRejected: matriqueController.rejectRoom(currentRoom)
onAccepted: currentRoom.acceptInvitation()
onRejected: currentRoom.rejectInvitation()
}
Menu {
@ -243,7 +243,7 @@ Item {
}
MenuItem {
text: "Leave Room"
onTriggered: matriqueController.forgetRoom(currentRoom.id)
onTriggered: currentRoom.forget()
}
}
}

View File

@ -36,10 +36,13 @@ Page {
}
Button {
text: "Logout"
highlighted: true
text: "Logout"
highlighted: true
onClicked: matriqueController.logout()
onClicked: {
matriqueController.logout()
Qt.quit()
}
}
}
}

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(); });
}

View File

@ -2,11 +2,10 @@
#define CONTROLLER_H
#include "connection.h"
#include "roomlistmodel.h"
#include "user.h"
#include <QApplication>
#include <QMimeDatabase>
#include <QMediaPlayer>
#include <QObject>
using namespace QMatrixClient;
@ -14,14 +13,13 @@ using namespace QMatrixClient;
class Controller : public QObject {
Q_OBJECT
Q_PROPERTY(Connection* connection READ getConnection CONSTANT)
Q_PROPERTY(
bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString homeserver READ getHomeserver WRITE setHomeserver NOTIFY
Q_PROPERTY(Connection* connection READ connection CONSTANT)
Q_PROPERTY(bool isLogin READ isLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString homeserver READ homeserver WRITE setHomeserver NOTIFY
homeserverChanged)
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
Q_PROPERTY(bool busy READ getBusy WRITE setBusy NOTIFY busyChanged)
Q_PROPERTY(QString userID READ userID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ token WRITE setToken NOTIFY tokenChanged)
Q_PROPERTY(bool busy READ busy WRITE setBusy NOTIFY busyChanged)
public:
explicit Controller(QObject* parent = nullptr);
@ -36,64 +34,61 @@ class Controller : public QObject {
// All the Q_PROPERTYs.
Connection* m_connection = new Connection();
Connection* getConnection() { return m_connection; }
Connection* connection() { return m_connection; }
bool isLogin = false;
bool getIsLogin() { return isLogin; }
bool isLogin() { return m_isLogin; }
void setIsLogin(bool n) {
if (n != isLogin) {
isLogin = n;
if (n != m_isLogin) {
m_isLogin = n;
emit isLoginChanged();
}
}
QString userID;
QString getUserID() { return userID; }
QString userID() { return m_userID; }
void setUserID(QString n) {
if (n != userID) {
userID = n;
if (n != m_userID) {
m_userID = n;
emit userIDChanged();
}
}
QByteArray token;
QByteArray getToken() { return token; }
QByteArray token() { return m_token; }
void setToken(QByteArray n) {
if (n != token) {
token = n;
if (n != m_token) {
m_token = n;
emit tokenChanged();
}
}
QString homeserver;
QString getHomeserver() { return homeserver; }
QString homeserver() { return m_homeserver; }
void setHomeserver(QString n) {
if (n != homeserver) {
homeserver = n;
if (n != m_homeserver) {
m_homeserver = n;
emit homeserverChanged();
}
}
bool busy = false;
bool getBusy() { return busy; }
bool busy() { return m_busy; }
void setBusy(bool b) {
if (b != busy) {
busy = b;
if (b != m_busy) {
m_busy = b;
emit busyChanged();
}
}
private:
QClipboard* m_clipboard = QApplication::clipboard();
QMimeDatabase m_db;
bool m_isLogin = false;
QString m_userID;
QByteArray m_token;
QString m_homeserver;
bool m_busy = false;
void connected();
void resync();
void reconnect();
QString getMIME(const QUrl& fileUrl) const;
void postFile(Room* room, const QUrl& localFile, const QUrl& mxcUrl);
signals:
void connectionChanged();
void isLoginChanged();
@ -104,15 +99,11 @@ class Controller : public QObject {
void errorOccured();
public slots:
void uploadFile(Room* room);
void forgetRoom(const QString& roomID);
void joinRoom(const QString& alias);
void createRoom(const QString& name, const QString& topic);
void createDirectChat(const QString& userID);
void copyToClipboard(const QString& text);
void saveFileAs(Room* room, QString eventId);
void acceptRoom(Room* room);
void rejectRoom(Room* room);
void playAudio(QUrl localFile);
};
#endif // CONTROLLER_H

View File

@ -9,6 +9,7 @@
#include "messageeventmodel.h"
#include "room.h"
#include "roomlistmodel.h"
#include "matriqueroom.h"
#include "csapi/joining.h"
#include "csapi/leaving.h"
@ -22,7 +23,7 @@ int main(int argc, char *argv[]) {
QApplication app(argc, argv);
qRegisterMetaType<Room *>("Room*");
qRegisterMetaType<MatriqueRoom *>("MatriqueRoom*");
qRegisterMetaType<User *>("User*");
qRegisterMetaType<MessageEventType>("MessageEventType");

57
src/matriqueroom.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "matriqueroom.h"
#include "connection.h"
#include "csapi/leaving.h"
#include <QFileDialog>
#include <QMimeDatabase>
MatriqueRoom::MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState)
: Room(connection, std::move(roomId), joinState) {}
void MatriqueRoom::chooseAndUploadFile() {
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) {
uploadFile(localFile.toString(), localFile, getMIME(localFile));
QMetaObject::Connection* const connection = new QMetaObject::Connection;
*connection = connect(this, &Room::fileTransferCompleted,
[=](QString id, QUrl localFile, QUrl mxcUrl) {
disconnect(*connection);
postFile(localFile, mxcUrl);
});
}
}
void MatriqueRoom::postFile(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()}}};
postJson("m.room.message", json);
}
QString MatriqueRoom::getMIME(const QUrl& fileUrl) const {
return QMimeDatabase().mimeTypeForFile(fileUrl.toLocalFile()).name();
}
void MatriqueRoom::saveFileAs(QString eventId) {
auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"),
fileNameToDownload(eventId));
if (!fileName.isEmpty()) downloadFile(eventId, QUrl::fromLocalFile(fileName));
}
void MatriqueRoom::acceptInvitation() { setJoinState(JoinState::Join); }
void MatriqueRoom::rejectInvitation() {
setJoinState(JoinState::Leave);
forget();
}
void MatriqueRoom::forget() { connection()->forgetRoom(id()); }

43
src/matriqueroom.h Normal file
View File

@ -0,0 +1,43 @@
#ifndef MATRIQUEROOM_H
#define MATRIQUEROOM_H
#include "room.h"
#include <QObject>
using namespace QMatrixClient;
class MatriqueRoom : public Room {
Q_OBJECT
Q_PROPERTY(QString cachedInput READ cachedInput WRITE setCachedInput NOTIFY
cachedInputChanged)
public:
explicit MatriqueRoom(Connection* connection, QString roomId,
JoinState joinState = {});
const QString& cachedInput() const { return m_cachedInput; }
void setCachedInput(const QString& input) {
if (input != m_cachedInput) {
m_cachedInput = input;
emit cachedInputChanged();
}
}
private:
QString m_cachedInput = "";
QString getMIME(const QUrl& fileUrl) const;
void postFile(const QUrl& localFile, const QUrl& mxcUrl);
signals:
void cachedInputChanged();
public slots:
void chooseAndUploadFile();
void saveFileAs(QString eventId);
void acceptInvitation();
void rejectInvitation();
void forget();
};
#endif // MATRIQUEROOM_H

View File

@ -1,15 +1,16 @@
#include "messageeventmodel.h"
#include <QtCore/QDebug>
#include <QtQml> // for qmlRegisterType()
#include <connection.h>
#include <settings.h>
#include <user.h>
#include <events/redactionevent.h>
#include <events/roomavatarevent.h>
#include <events/roommemberevent.h>
#include <events/simplestateevents.h>
#include <settings.h>
#include <user.h>
#include <QtCore/QDebug>
#include <QtQml> // for qmlRegisterType()
QHash<int, QByteArray> MessageEventModel::roleNames() const {
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();

View File

@ -1,9 +1,10 @@
#ifndef MESSAGEEVENTMODEL_H
#define MESSAGEEVENTMODEL_H
#include <QtCore/QAbstractListModel>
#include "room.h"
#include <QtCore/QAbstractListModel>
class MessageEventModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(

View File

@ -37,12 +37,13 @@ void RoomListModel::doResetModel() {
endResetModel();
}
Room* RoomListModel::roomAt(int row) { return m_rooms.at(row); }
MatriqueRoom* RoomListModel::roomAt(int row) { return m_rooms.at(row); }
void RoomListModel::doAddRoom(Room* r) {
if (auto* room = r) {
if (auto* room = static_cast<MatriqueRoom*>(r)) {
m_rooms.append(room);
connectRoomSignals(room);
// qCritical() << room->cachedInput();
emit roomAdded(room);
} else {
qCritical() << "Attempt to add nullptr to the room list";
@ -50,7 +51,7 @@ void RoomListModel::doAddRoom(Room* r) {
}
}
void RoomListModel::connectRoomSignals(Room* room) {
void RoomListModel::connectRoomSignals(MatriqueRoom* room) {
connect(room, &Room::displaynameChanged, this, [=] { namesChanged(room); });
connect(room, &Room::unreadMessagesChanged, this,
[=] { unreadMessagesChanged(room); });
@ -62,7 +63,7 @@ void RoomListModel::connectRoomSignals(Room* room) {
[=] { refresh(room, {AvatarRole}); });
connect(room, &Room::unreadMessagesChanged, this, [=](Room* r) {
if (r->hasUnreadMessages()) emit newMessage(r);
if (r->hasUnreadMessages()) emit newMessage(static_cast<MatriqueRoom*>(r));
});
// connect(
// room, &QMatrixClient::Room::aboutToAddNewMessages, this,
@ -84,7 +85,7 @@ void RoomListModel::updateRoom(Room* room, Room* prev) {
// the previously left room (in both cases prev has the previous state).
if (prev == room) {
qCritical() << "RoomListModel::updateRoom: room tried to replace itself";
refresh(static_cast<Room*>(room));
refresh(static_cast<MatriqueRoom*>(room));
return;
}
if (prev && room->id() != prev->id()) {
@ -93,10 +94,10 @@ void RoomListModel::updateRoom(Room* room, Room* prev) {
// That doesn't look right but technically we still can do it.
}
// Ok, we're through with pre-checks, now for the real thing.
auto* newRoom = room;
const auto it =
std::find_if(m_rooms.begin(), m_rooms.end(),
[=](const Room* r) { return r == prev || r == newRoom; });
auto* newRoom = static_cast<MatriqueRoom*>(room);
const auto it = std::find_if(
m_rooms.begin(), m_rooms.end(),
[=](const MatriqueRoom* r) { return r == prev || r == newRoom; });
if (it != m_rooms.end()) {
const int row = it - m_rooms.begin();
// There's no guarantee that prev != newRoom
@ -136,7 +137,7 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const {
qDebug() << "UserListModel: something wrong here...";
return QVariant();
}
Room* room = m_rooms.at(index.row());
MatriqueRoom* room = m_rooms.at(index.row());
if (role == NameRole) return room->displayName();
if (role == AvatarRole) {
if (room->avatarUrl().toString() != "") {
@ -156,12 +157,12 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const {
return QVariant();
}
void RoomListModel::namesChanged(Room* room) {
void RoomListModel::namesChanged(MatriqueRoom* room) {
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
}
void RoomListModel::refresh(Room* room, const QVector<int>& roles) {
void RoomListModel::refresh(MatriqueRoom* room, const QVector<int>& roles) {
const auto it = std::find(m_rooms.begin(), m_rooms.end(), room);
if (it == m_rooms.end()) {
qCritical() << "Room" << room->id() << "not found in the room list";
@ -171,7 +172,7 @@ void RoomListModel::refresh(Room* room, const QVector<int>& roles) {
emit dataChanged(idx, idx, roles);
}
void RoomListModel::unreadMessagesChanged(Room* room) {
void RoomListModel::unreadMessagesChanged(MatriqueRoom* room) {
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
}

View File

@ -1,9 +1,11 @@
#ifndef ROOMLISTMODEL_H
#define ROOMLISTMODEL_H
#include <QtCore/QAbstractListModel>
#include "connection.h"
#include "room.h"
#include "matriqueroom.h"
#include <QtCore/QAbstractListModel>
using namespace QMatrixClient;
@ -23,7 +25,7 @@ class RoomType : public QObject {
class RoomListModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(Connection* connection READ getConnection WRITE setConnection)
Q_PROPERTY(Connection* connection READ connection WRITE setConnection)
public:
enum EventRoles {
@ -37,11 +39,11 @@ class RoomListModel : public QAbstractListModel {
RoomListModel(QObject* parent = 0);
virtual ~RoomListModel();
Connection* getConnection() { return m_connection; }
Connection* connection() { return m_connection; }
void setConnection(Connection* connection);
void doResetModel();
Q_INVOKABLE Room* roomAt(int row);
Q_INVOKABLE MatriqueRoom* roomAt(int row);
QVariant data(const QModelIndex& index,
int role = Qt::DisplayRole) const override;
@ -51,23 +53,23 @@ class RoomListModel : public QAbstractListModel {
QHash<int, QByteArray> roleNames() const;
private slots:
void namesChanged(Room* room);
void unreadMessagesChanged(Room* room);
void namesChanged(MatriqueRoom* room);
void unreadMessagesChanged(MatriqueRoom* room);
void doAddRoom(Room* room);
void updateRoom(Room* room, Room* prev);
void deleteRoom(Room* room);
void refresh(Room* room, const QVector<int>& roles = {});
void refresh(MatriqueRoom* room, const QVector<int>& roles = {});
private:
Connection* m_connection = nullptr;
QList<Room*> m_rooms;
void connectRoomSignals(Room* room);
QList<MatriqueRoom*> m_rooms;
void connectRoomSignals(MatriqueRoom* room);
signals:
void connectionChanged();
void roomAdded(Room* room);
void newMessage(Room* room);
void roomAdded(MatriqueRoom* room);
void newMessage(MatriqueRoom* room);
};
#endif // ROOMLISTMODEL_H