From 6f07ab433e252dd3615cc1633aee856d8ebd83ed Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Tue, 10 Jul 2018 23:31:51 +0300 Subject: [PATCH] Send image dimensions in m.image event fixes #215 --- include/ChatPage.h | 3 +- include/timeline/TimelineView.h | 8 ++- include/timeline/TimelineViewManager.h | 3 +- src/ChatPage.cc | 91 +++++++++++++++----------- src/timeline/TimelineView.cc | 2 + src/timeline/TimelineViewManager.cc | 6 +- 6 files changed, 67 insertions(+), 46 deletions(-) diff --git a/include/ChatPage.h b/include/ChatPage.h index faabd6db..3b1562b3 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -93,7 +93,8 @@ signals: const QString &filename, const QString &url, const QString &mime, - qint64 dsize); + qint64 dsize, + const QSize &dimensions); void fileUploaded(const QString &roomid, const QString &filename, const QString &url, diff --git a/include/timeline/TimelineView.h b/include/timeline/TimelineView.h index 5af359c5..c6ddafd6 100644 --- a/include/timeline/TimelineView.h +++ b/include/timeline/TimelineView.h @@ -69,6 +69,7 @@ struct PendingMessage uint64_t media_size; QString event_id; TimelineItem *widget; + QSize dimensions; bool is_encrypted = false; }; @@ -125,7 +126,8 @@ public: void addUserMessage(const QString &url, const QString &filename, const QString &mime, - uint64_t size); + uint64_t size, + const QSize &dimensions = QSize()); void updatePendingMessage(const std::string &txn_id, const QString &event_id); void scrollDown(); @@ -321,7 +323,8 @@ void TimelineView::addUserMessage(const QString &url, const QString &filename, const QString &mime, - uint64_t size) + uint64_t size, + const QSize &dimensions) { auto with_sender = (lastSender_ != local_user_) || isDateDifference(lastMsgTimestamp_); auto trimmed = QFileInfo{filename}.fileName(); // Trim file path. @@ -346,6 +349,7 @@ TimelineView::addUserMessage(const QString &url, message.mime = mime; message.media_size = size; message.widget = view_item; + message.dimensions = dimensions; handleNewUserMessage(message); } diff --git a/include/timeline/TimelineViewManager.h b/include/timeline/TimelineViewManager.h index 590adb2b..f3c099c1 100644 --- a/include/timeline/TimelineViewManager.h +++ b/include/timeline/TimelineViewManager.h @@ -67,7 +67,8 @@ public slots: const QString &filename, const QString &url, const QString &mime, - uint64_t dsize); + uint64_t dsize, + const QSize &dimensions); void queueFileMessage(const QString &roomid, const QString &filename, const QString &url, diff --git a/src/ChatPage.cc b/src/ChatPage.cc index a14c7a93..23469da5 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -283,48 +284,52 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(text_input_, &TextInputWidget::sendJoinRoomRequest, this, &ChatPage::joinRoom); - connect(text_input_, - &TextInputWidget::uploadImage, - this, - [this](QSharedPointer dev, const QString &fn) { - QMimeDatabase db; - QMimeType mime = db.mimeTypeForData(dev.data()); + connect( + text_input_, + &TextInputWidget::uploadImage, + this, + [this](QSharedPointer dev, const QString &fn) { + QMimeDatabase db; + QMimeType mime = db.mimeTypeForData(dev.data()); - if (!dev->open(QIODevice::ReadOnly)) { - emit uploadFailed( - QString("Error while reading media: %1").arg(dev->errorString())); - return; - } + if (!dev->open(QIODevice::ReadOnly)) { + emit uploadFailed( + QString("Error while reading media: %1").arg(dev->errorString())); + return; + } - auto bin = dev->readAll(); - auto payload = std::string(bin.data(), bin.size()); + auto bin = dev->peek(dev->size()); + auto payload = std::string(bin.data(), bin.size()); + auto dimensions = QImageReader(dev.get()).size(); - http::v2::client()->upload( - payload, - mime.name().toStdString(), - QFileInfo(fn).fileName().toStdString(), - [this, - room_id = current_room_, - filename = fn, - mime = mime.name(), - size = payload.size()](const mtx::responses::ContentURI &res, - mtx::http::RequestErr err) { - if (err) { - emit uploadFailed( - tr("Failed to upload image. Please try again.")); - nhlog::net()->warn("failed to upload image: {} ({})", - err->matrix_error.error, - static_cast(err->status_code)); - return; - } + http::v2::client()->upload( + payload, + mime.name().toStdString(), + QFileInfo(fn).fileName().toStdString(), + [this, + room_id = current_room_, + filename = fn, + mime = mime.name(), + size = payload.size(), + dimensions](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) { + if (err) { + emit uploadFailed( + tr("Failed to upload image. Please try again.")); + nhlog::net()->warn("failed to upload image: {} {} ({})", + err->matrix_error.error, + to_string(err->matrix_error.errcode), + static_cast(err->status_code)); + return; + } - emit imageUploaded(room_id, - filename, - QString::fromStdString(res.content_uri), - mime, - size); - }); - }); + emit imageUploaded(room_id, + filename, + QString::fromStdString(res.content_uri), + mime, + size, + dimensions); + }); + }); connect(text_input_, &TextInputWidget::uploadFile, @@ -461,9 +466,15 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(this, &ChatPage::imageUploaded, this, - [this](QString roomid, QString filename, QString url, QString mime, qint64 dsize) { + [this](QString roomid, + QString filename, + QString url, + QString mime, + qint64 dsize, + QSize dimensions) { text_input_->hideUploadSpinner(); - view_manager_->queueImageMessage(roomid, filename, url, mime, dsize); + view_manager_->queueImageMessage( + roomid, filename, url, mime, dsize, dimensions); }); connect(this, &ChatPage::fileUploaded, diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc index b156b732..ca2f6211 100644 --- a/src/timeline/TimelineView.cc +++ b/src/timeline/TimelineView.cc @@ -1097,6 +1097,8 @@ toRoomMessage(const PendingMessage &m) image.info.size = m.media_size; image.body = m.filename.toStdString(); image.url = m.body.toStdString(); + image.info.h = m.dimensions.height(); + image.info.w = m.dimensions.width(); return image; } diff --git a/src/timeline/TimelineViewManager.cc b/src/timeline/TimelineViewManager.cc index dda71d2f..c8e00b66 100644 --- a/src/timeline/TimelineViewManager.cc +++ b/src/timeline/TimelineViewManager.cc @@ -74,7 +74,8 @@ TimelineViewManager::queueImageMessage(const QString &roomid, const QString &filename, const QString &url, const QString &mime, - uint64_t size) + uint64_t size, + const QSize &dimensions) { if (!timelineViewExists(roomid)) { nhlog::ui()->warn("Cannot send m.image message to a non-managed view"); @@ -83,7 +84,8 @@ TimelineViewManager::queueImageMessage(const QString &roomid, auto view = views_[roomid]; - view->addUserMessage(url, filename, mime, size); + view->addUserMessage( + url, filename, mime, size, dimensions); } void