summary refs log tree commit diff
path: root/src/timeline/TimelineModel.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-04-23 20:55:28 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-04-23 20:55:28 +0200
commit6a03615413ef90e378df80fa1e3bc4ef031d5ea6 (patch)
treec577c21e1c3cda9f7c50a9545c7a57720de762b9 /src/timeline/TimelineModel.cpp
parentTranslated using Weblate (Ukrainian) (diff)
downloadnheko-6a03615413ef90e378df80fa1e3bc4ef031d5ea6.tar.xz
Copy image to clipboard
Fixes #599
Diffstat (limited to 'src/timeline/TimelineModel.cpp')
-rw-r--r--src/timeline/TimelineModel.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp

index f80f2ee9..5996bea8 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp
@@ -12,6 +12,7 @@ #include <QDesktopServices> #include <QFileDialog> #include <QGuiApplication> +#include <QMimeData> #include <QMimeDatabase> #include <QRegularExpression> #include <QStandardPaths> @@ -1860,6 +1861,60 @@ TimelineModel::saveMedia(const QString &eventId) const return true; } +bool +TimelineModel::copyMedia(const QString &eventId) const +{ + auto event = events.get(eventId.toStdString(), ""); + if (!event) + return false; + + QString mxcUrl = QString::fromStdString(mtx::accessors::url(*event)); + QString mimeType = QString::fromStdString(mtx::accessors::mimetype(*event)); + qml_mtx_events::EventType eventType = toRoomEventType(*event); + + auto encryptionInfo = mtx::accessors::file(*event); + + const auto url = mxcUrl.toStdString(); + + http::client()->download( + url, + [url, mimeType, eventType, encryptionInfo](const std::string &data, + const std::string &, + const std::string &, + mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn("failed to retrieve media {}: {} {}", + url, + err->matrix_error.error, + static_cast<int>(err->status_code)); + return; + } + + try { + auto temp = data; + if (encryptionInfo) + temp = + mtx::crypto::to_string(mtx::crypto::decrypt_file(temp, encryptionInfo.value())); + + auto by = QByteArray(temp.data(), (qsizetype)temp.size()); + QMimeData *clipContents = new QMimeData(); + clipContents->setData(mimeType, by); + + if (eventType == qml_mtx_events::EventType::ImageMessage) { + auto img = utils::readImage(QByteArray(data.data(), (qsizetype)data.size())); + clipContents->setImageData(img); + } + + QGuiApplication::clipboard()->setMimeData(clipContents); + + return; + } catch (const std::exception &e) { + nhlog::ui()->warn("Error while copying file to clipboard: {}", e.what()); + } + }); + return true; +} + void TimelineModel::cacheMedia(const QString &eventId, const std::function<void(const QString)> &callback)