diff --git a/qml/component/ImageBubble.qml b/qml/component/ImageBubble.qml index 2c96cf6..23cf527 100644 --- a/qml/component/ImageBubble.qml +++ b/qml/component/ImageBubble.qml @@ -20,7 +20,7 @@ AvatarContainer { Image { id: messageImage - source: "image://mxc/" + content.url + source: "image://mxc/" + (content.thumbnail_url ? content.thumbnail_url : content.url) MouseArea { anchors.fill: parent diff --git a/qml/form/RoomForm.qml b/qml/form/RoomForm.qml index e93c79b..121aada 100644 --- a/qml/form/RoomForm.qml +++ b/qml/form/RoomForm.qml @@ -150,12 +150,12 @@ Item { folder: shortcuts.home selectMultiple: false onAccepted: { - currentRoom.uploadFile(fileDialog.fileUrl, fileDialog.fileUrl) + currentRoom.uploadFile(fileUrl, fileUrl, matriqueController.getMIME(fileUrl)) var fileTransferProgressCallback = function(id, sent, total) { - if (id == fileDialog.fileUrl) { inputField.progress = sent / total } + if (id == fileUrl) { inputField.progress = sent / total } } var completedCallback = function(id, localFile, mxcUrl) { - if (id == fileDialog.fileUrl) { + if (id == fileUrl) { matriqueController.postFile(currentRoom, localFile, mxcUrl) inputField.progress = 0 currentRoom.fileTransferCompleted.disconnect(fileTransferProgressCallback) diff --git a/src/controller.cpp b/src/controller.cpp index ce11df4..04e7c8a 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -4,6 +4,10 @@ #include "events/eventcontent.h" #include "events/roommessageevent.h" +#include +#include +#include + Controller::Controller(QObject* parent) : QObject(parent) { connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected); @@ -22,7 +26,11 @@ Controller::Controller(QObject* parent) : QObject(parent) { [=] { setBusy(false); }); } -Controller::~Controller() { m_connection->stopSync(); } +Controller::~Controller() { + m_connection->saveState(); + m_connection->stopSync(); + m_connection->deleteLater(); +} void Controller::login() { if (!isLogin) { @@ -81,10 +89,22 @@ void Controller::reconnect() { void Controller::postFile(QMatrixClient::Room* room, const QUrl& localFile, const QUrl& mxcUrl) { - QJsonObject json{{"body", localFile.fileName()}, - {"filename", localFile.fileName()}, - {"url", mxcUrl.url()}}; - room->postMessage(QMatrixClient::RoomMessageEvent{ - localFile.fileName(), "m.file", - new QMatrixClient::EventContent::FileContent(json)}); + const QString mime = getMIME(localFile); + const QString fileName = localFile.toLocalFile(); + 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{{"content", QJsonObject{{"msgtype", msgType}, + {"body", fileName}, + {"filename", fileName}, + {"url", mxcUrl.url()}}}}; + room->postMessage(QMatrixClient::RoomMessageEvent(json)); +} + +QString Controller::getMIME(const QUrl& fileUrl) const { + QMimeDatabase* db = new QMimeDatabase(); + const QString mime = db->mimeTypeForFile(fileUrl.toLocalFile()).name(); + free(db); + return mime; } diff --git a/src/controller.h b/src/controller.h index ef388a2..2562a0f 100644 --- a/src/controller.h +++ b/src/controller.h @@ -99,6 +99,7 @@ class Controller : public QObject { public slots: void postFile(QMatrixClient::Room* room, const QUrl& localFile, const QUrl& mxcUrl); + QString getMIME(const QUrl& fileUrl) const; }; #endif // CONTROLLER_H