mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-25 12:38:48 +03:00
Restore saving of media
This commit is contained in:
parent
9b18440b4f
commit
e2d733a01a
9 changed files with 173 additions and 32 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -54,6 +54,7 @@ ui_*.h
|
||||||
|
|
||||||
# Vim
|
# Vim
|
||||||
*.swp
|
*.swp
|
||||||
|
*.swo
|
||||||
|
|
||||||
#####=== CMake ===#####
|
#####=== CMake ===#####
|
||||||
|
|
||||||
|
|
|
@ -375,7 +375,6 @@ qt5_wrap_cpp(MOC_HEADERS
|
||||||
src/CommunitiesList.h
|
src/CommunitiesList.h
|
||||||
src/LoginPage.h
|
src/LoginPage.h
|
||||||
src/MainWindow.h
|
src/MainWindow.h
|
||||||
src/MatrixClient.h
|
|
||||||
src/InviteeItem.h
|
src/InviteeItem.h
|
||||||
src/QuickSwitcher.h
|
src/QuickSwitcher.h
|
||||||
src/RegisterPage.h
|
src/RegisterPage.h
|
||||||
|
|
|
@ -187,18 +187,23 @@ Rectangle {
|
||||||
id: contextMenu
|
id: contextMenu
|
||||||
|
|
||||||
MenuItem {
|
MenuItem {
|
||||||
text: "Read receipts"
|
text: qsTr("Read receipts")
|
||||||
onTriggered: chat.model.readReceiptsAction(model.id)
|
onTriggered: chat.model.readReceiptsAction(model.id)
|
||||||
}
|
}
|
||||||
MenuItem {
|
MenuItem {
|
||||||
text: "Mark as read"
|
text: qsTr("Mark as read")
|
||||||
}
|
}
|
||||||
MenuItem {
|
MenuItem {
|
||||||
text: "View raw message"
|
text: qsTr("View raw message")
|
||||||
onTriggered: chat.model.viewRawMessage(model.id)
|
onTriggered: chat.model.viewRawMessage(model.id)
|
||||||
}
|
}
|
||||||
MenuItem {
|
MenuItem {
|
||||||
text: "Redact message"
|
text: qsTr("Redact message")
|
||||||
|
}
|
||||||
|
MenuItem {
|
||||||
|
visible: model.type == MtxEvent.ImageMessage || model.type == MtxEvent.VideoMessage || model.type == MtxEvent.AudioMessage || model.type == MtxEvent.FileMessage
|
||||||
|
text: qsTr("Save as")
|
||||||
|
onTriggered: timelineManager.saveMedia(model.url, model.filename, model.mimetype, model.type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ Item {
|
||||||
|
|
||||||
MouseArea {
|
MouseArea {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
onClicked: timelineManager.openImageOverlay(img.source)
|
onClicked: timelineManager.openImageOverlay(eventData.url, eventData.filename, eventData.mimetype, eventData.type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,16 +20,6 @@ Q_DECLARE_METATYPE(nlohmann::json)
|
||||||
Q_DECLARE_METATYPE(std::vector<std::string>)
|
Q_DECLARE_METATYPE(std::vector<std::string>)
|
||||||
Q_DECLARE_METATYPE(std::vector<QString>)
|
Q_DECLARE_METATYPE(std::vector<QString>)
|
||||||
|
|
||||||
class MediaProxy : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void imageDownloaded(const QPixmap &);
|
|
||||||
void imageSaved(const QString &, const QByteArray &);
|
|
||||||
void fileDownloaded(const QByteArray &);
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace http {
|
namespace http {
|
||||||
mtx::http::Client *
|
mtx::http::Client *
|
||||||
client();
|
client();
|
||||||
|
|
|
@ -104,6 +104,53 @@ eventUrl(const mtx::events::RoomEvent<T> &e)
|
||||||
return QString::fromStdString(e.content.url);
|
return QString::fromStdString(e.content.url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
QString
|
||||||
|
eventFilename(const T &)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
QString
|
||||||
|
eventFilename(const mtx::events::RoomEvent<mtx::events::msg::Audio> &e)
|
||||||
|
{
|
||||||
|
// body may be the original filename
|
||||||
|
return QString::fromStdString(e.content.body);
|
||||||
|
}
|
||||||
|
QString
|
||||||
|
eventFilename(const mtx::events::RoomEvent<mtx::events::msg::Video> &e)
|
||||||
|
{
|
||||||
|
// body may be the original filename
|
||||||
|
return QString::fromStdString(e.content.body);
|
||||||
|
}
|
||||||
|
QString
|
||||||
|
eventFilename(const mtx::events::RoomEvent<mtx::events::msg::Image> &e)
|
||||||
|
{
|
||||||
|
// body may be the original filename
|
||||||
|
return QString::fromStdString(e.content.body);
|
||||||
|
}
|
||||||
|
QString
|
||||||
|
eventFilename(const mtx::events::RoomEvent<mtx::events::msg::File> &e)
|
||||||
|
{
|
||||||
|
// body may be the original filename
|
||||||
|
if (!e.content.filename.empty())
|
||||||
|
return QString::fromStdString(e.content.filename);
|
||||||
|
return QString::fromStdString(e.content.body);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
QString
|
||||||
|
eventMimeType(const T &)
|
||||||
|
{
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
auto
|
||||||
|
eventMimeType(const mtx::events::RoomEvent<T> &e)
|
||||||
|
-> std::enable_if_t<std::is_same<decltype(e.content.info.mimetype), std::string>::value, QString>
|
||||||
|
{
|
||||||
|
return QString::fromStdString(e.content.info.mimetype);
|
||||||
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
qml_mtx_events::EventType
|
qml_mtx_events::EventType
|
||||||
toRoomEventType(const mtx::events::Event<T> &e)
|
toRoomEventType(const mtx::events::Event<T> &e)
|
||||||
|
@ -288,6 +335,8 @@ TimelineModel::roleNames() const
|
||||||
{UserName, "userName"},
|
{UserName, "userName"},
|
||||||
{Timestamp, "timestamp"},
|
{Timestamp, "timestamp"},
|
||||||
{Url, "url"},
|
{Url, "url"},
|
||||||
|
{Filename, "filename"},
|
||||||
|
{MimeType, "mimetype"},
|
||||||
{Height, "height"},
|
{Height, "height"},
|
||||||
{Width, "width"},
|
{Width, "width"},
|
||||||
{ProportionalHeight, "proportionalHeight"},
|
{ProportionalHeight, "proportionalHeight"},
|
||||||
|
@ -366,6 +415,12 @@ TimelineModel::data(const QModelIndex &index, int role) const
|
||||||
case Url:
|
case Url:
|
||||||
return QVariant(boost::apply_visitor(
|
return QVariant(boost::apply_visitor(
|
||||||
[](const auto &e) -> QString { return eventUrl(e); }, event));
|
[](const auto &e) -> QString { return eventUrl(e); }, event));
|
||||||
|
case Filename:
|
||||||
|
return QVariant(boost::apply_visitor(
|
||||||
|
[](const auto &e) -> QString { return eventFilename(e); }, event));
|
||||||
|
case MimeType:
|
||||||
|
return QVariant(boost::apply_visitor(
|
||||||
|
[](const auto &e) -> QString { return eventMimeType(e); }, event));
|
||||||
case Height:
|
case Height:
|
||||||
return QVariant(boost::apply_visitor(
|
return QVariant(boost::apply_visitor(
|
||||||
[](const auto &e) -> qulonglong { return eventHeight(e); }, event));
|
[](const auto &e) -> qulonglong { return eventHeight(e); }, event));
|
||||||
|
|
|
@ -127,6 +127,8 @@ public:
|
||||||
UserName,
|
UserName,
|
||||||
Timestamp,
|
Timestamp,
|
||||||
Url,
|
Url,
|
||||||
|
Filename,
|
||||||
|
MimeType,
|
||||||
Height,
|
Height,
|
||||||
Width,
|
Width,
|
||||||
ProportionalHeight,
|
ProportionalHeight,
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "TimelineViewManager.h"
|
#include "TimelineViewManager.h"
|
||||||
|
|
||||||
|
#include <QFileDialog>
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
|
#include <QMimeDatabase>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
|
|
||||||
#include "Logging.h"
|
#include "Logging.h"
|
||||||
|
@ -55,24 +57,88 @@ TimelineViewManager::setHistoryView(const QString &room_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
TimelineViewManager::openImageOverlay(QString url) const
|
TimelineViewManager::openImageOverlay(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
qml_mtx_events::EventType eventType) const
|
||||||
{
|
{
|
||||||
QQuickImageResponse *imgResponse =
|
QQuickImageResponse *imgResponse =
|
||||||
imgProvider->requestImageResponse(url.remove("image://mxcimage/"), QSize());
|
imgProvider->requestImageResponse(mxcUrl.remove("mxc://"), QSize());
|
||||||
connect(imgResponse, &QQuickImageResponse::finished, this, [imgResponse]() {
|
connect(imgResponse,
|
||||||
if (!imgResponse->errorString().isEmpty()) {
|
&QQuickImageResponse::finished,
|
||||||
nhlog::ui()->error("Error when retrieving image for overlay: {}",
|
this,
|
||||||
imgResponse->errorString().toStdString());
|
[this, mxcUrl, originalFilename, mimeType, eventType, imgResponse]() {
|
||||||
return;
|
if (!imgResponse->errorString().isEmpty()) {
|
||||||
}
|
nhlog::ui()->error("Error when retrieving image for overlay: {}",
|
||||||
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
|
imgResponse->errorString().toStdString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
|
||||||
|
|
||||||
auto imgDialog = new dialogs::ImageOverlay(pixmap);
|
auto imgDialog = new dialogs::ImageOverlay(pixmap);
|
||||||
imgDialog->show();
|
imgDialog->show();
|
||||||
// connect(imgDialog, &dialogs::ImageOverlay::saving, this,
|
connect(imgDialog,
|
||||||
// &ImageItem::saveAs);
|
&dialogs::ImageOverlay::saving,
|
||||||
Q_UNUSED(imgDialog);
|
this,
|
||||||
});
|
[this, mxcUrl, originalFilename, mimeType, eventType]() {
|
||||||
|
saveMedia(mxcUrl, originalFilename, mimeType, eventType);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
TimelineViewManager::saveMedia(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
qml_mtx_events::EventType eventType) const
|
||||||
|
{
|
||||||
|
QString dialogTitle;
|
||||||
|
if (eventType == qml_mtx_events::EventType::ImageMessage) {
|
||||||
|
dialogTitle = tr("Save image");
|
||||||
|
} else if (eventType == qml_mtx_events::EventType::VideoMessage) {
|
||||||
|
dialogTitle = tr("Save video");
|
||||||
|
} else if (eventType == qml_mtx_events::EventType::AudioMessage) {
|
||||||
|
dialogTitle = tr("Save audio");
|
||||||
|
} else {
|
||||||
|
dialogTitle = tr("Save file");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString filterString = QMimeDatabase().mimeTypeForName(mimeType).filterString();
|
||||||
|
|
||||||
|
auto filename =
|
||||||
|
QFileDialog::getSaveFileName(container, dialogTitle, originalFilename, filterString);
|
||||||
|
|
||||||
|
if (filename.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
const auto url = mxcUrl.toStdString();
|
||||||
|
|
||||||
|
http::client()->download(
|
||||||
|
url,
|
||||||
|
[filename, url](const std::string &data,
|
||||||
|
const std::string &,
|
||||||
|
const std::string &,
|
||||||
|
mtx::http::RequestErr err) {
|
||||||
|
if (err) {
|
||||||
|
nhlog::net()->warn("failed to retrieve image {}: {} {}",
|
||||||
|
url,
|
||||||
|
err->matrix_error.error,
|
||||||
|
static_cast<int>(err->status_code));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
QFile file(filename);
|
||||||
|
|
||||||
|
if (!file.open(QIODevice::WriteOnly))
|
||||||
|
return;
|
||||||
|
|
||||||
|
file.write(QByteArray(data.data(), data.size()));
|
||||||
|
file.close();
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
nhlog::ui()->warn("Error while saving file to: {}", e.what());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -35,7 +35,30 @@ public:
|
||||||
void clearAll() { models.clear(); }
|
void clearAll() { models.clear(); }
|
||||||
|
|
||||||
Q_INVOKABLE TimelineModel *activeTimeline() const { return timeline_; }
|
Q_INVOKABLE TimelineModel *activeTimeline() const { return timeline_; }
|
||||||
Q_INVOKABLE void openImageOverlay(QString url) const;
|
void openImageOverlay(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
qml_mtx_events::EventType eventType) const;
|
||||||
|
void saveMedia(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
qml_mtx_events::EventType eventType) const;
|
||||||
|
// Qml can only pass enum as int
|
||||||
|
Q_INVOKABLE void openImageOverlay(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
int eventType) const
|
||||||
|
{
|
||||||
|
openImageOverlay(
|
||||||
|
mxcUrl, originalFilename, mimeType, (qml_mtx_events::EventType)eventType);
|
||||||
|
}
|
||||||
|
Q_INVOKABLE void saveMedia(QString mxcUrl,
|
||||||
|
QString originalFilename,
|
||||||
|
QString mimeType,
|
||||||
|
int eventType) const
|
||||||
|
{
|
||||||
|
saveMedia(mxcUrl, originalFilename, mimeType, (qml_mtx_events::EventType)eventType);
|
||||||
|
}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clearRoomMessageCount(QString roomid);
|
void clearRoomMessageCount(QString roomid);
|
||||||
|
|
Loading…
Reference in a new issue