mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-10-30 09:30:47 +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
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
#####=== CMake ===#####
|
||||
|
||||
|
|
|
@ -375,7 +375,6 @@ qt5_wrap_cpp(MOC_HEADERS
|
|||
src/CommunitiesList.h
|
||||
src/LoginPage.h
|
||||
src/MainWindow.h
|
||||
src/MatrixClient.h
|
||||
src/InviteeItem.h
|
||||
src/QuickSwitcher.h
|
||||
src/RegisterPage.h
|
||||
|
|
|
@ -187,18 +187,23 @@ Rectangle {
|
|||
id: contextMenu
|
||||
|
||||
MenuItem {
|
||||
text: "Read receipts"
|
||||
text: qsTr("Read receipts")
|
||||
onTriggered: chat.model.readReceiptsAction(model.id)
|
||||
}
|
||||
MenuItem {
|
||||
text: "Mark as read"
|
||||
text: qsTr("Mark as read")
|
||||
}
|
||||
MenuItem {
|
||||
text: "View raw message"
|
||||
text: qsTr("View raw message")
|
||||
onTriggered: chat.model.viewRawMessage(model.id)
|
||||
}
|
||||
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 {
|
||||
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<QString>)
|
||||
|
||||
class MediaProxy : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
signals:
|
||||
void imageDownloaded(const QPixmap &);
|
||||
void imageSaved(const QString &, const QByteArray &);
|
||||
void fileDownloaded(const QByteArray &);
|
||||
};
|
||||
|
||||
namespace http {
|
||||
mtx::http::Client *
|
||||
client();
|
||||
|
|
|
@ -104,6 +104,53 @@ eventUrl(const mtx::events::RoomEvent<T> &e)
|
|||
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>
|
||||
qml_mtx_events::EventType
|
||||
toRoomEventType(const mtx::events::Event<T> &e)
|
||||
|
@ -288,6 +335,8 @@ TimelineModel::roleNames() const
|
|||
{UserName, "userName"},
|
||||
{Timestamp, "timestamp"},
|
||||
{Url, "url"},
|
||||
{Filename, "filename"},
|
||||
{MimeType, "mimetype"},
|
||||
{Height, "height"},
|
||||
{Width, "width"},
|
||||
{ProportionalHeight, "proportionalHeight"},
|
||||
|
@ -366,6 +415,12 @@ TimelineModel::data(const QModelIndex &index, int role) const
|
|||
case Url:
|
||||
return QVariant(boost::apply_visitor(
|
||||
[](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:
|
||||
return QVariant(boost::apply_visitor(
|
||||
[](const auto &e) -> qulonglong { return eventHeight(e); }, event));
|
||||
|
|
|
@ -127,6 +127,8 @@ public:
|
|||
UserName,
|
||||
Timestamp,
|
||||
Url,
|
||||
Filename,
|
||||
MimeType,
|
||||
Height,
|
||||
Width,
|
||||
ProportionalHeight,
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "TimelineViewManager.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QMetaType>
|
||||
#include <QMimeDatabase>
|
||||
#include <QQmlContext>
|
||||
|
||||
#include "Logging.h"
|
||||
|
@ -55,24 +57,88 @@ TimelineViewManager::setHistoryView(const QString &room_id)
|
|||
}
|
||||
|
||||
void
|
||||
TimelineViewManager::openImageOverlay(QString url) const
|
||||
TimelineViewManager::openImageOverlay(QString mxcUrl,
|
||||
QString originalFilename,
|
||||
QString mimeType,
|
||||
qml_mtx_events::EventType eventType) const
|
||||
{
|
||||
QQuickImageResponse *imgResponse =
|
||||
imgProvider->requestImageResponse(url.remove("image://mxcimage/"), QSize());
|
||||
connect(imgResponse, &QQuickImageResponse::finished, this, [imgResponse]() {
|
||||
if (!imgResponse->errorString().isEmpty()) {
|
||||
nhlog::ui()->error("Error when retrieving image for overlay: {}",
|
||||
imgResponse->errorString().toStdString());
|
||||
return;
|
||||
}
|
||||
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
|
||||
imgProvider->requestImageResponse(mxcUrl.remove("mxc://"), QSize());
|
||||
connect(imgResponse,
|
||||
&QQuickImageResponse::finished,
|
||||
this,
|
||||
[this, mxcUrl, originalFilename, mimeType, eventType, imgResponse]() {
|
||||
if (!imgResponse->errorString().isEmpty()) {
|
||||
nhlog::ui()->error("Error when retrieving image for overlay: {}",
|
||||
imgResponse->errorString().toStdString());
|
||||
return;
|
||||
}
|
||||
auto pixmap = QPixmap::fromImage(imgResponse->textureFactory()->image());
|
||||
|
||||
auto imgDialog = new dialogs::ImageOverlay(pixmap);
|
||||
imgDialog->show();
|
||||
// connect(imgDialog, &dialogs::ImageOverlay::saving, this,
|
||||
// &ImageItem::saveAs);
|
||||
Q_UNUSED(imgDialog);
|
||||
});
|
||||
auto imgDialog = new dialogs::ImageOverlay(pixmap);
|
||||
imgDialog->show();
|
||||
connect(imgDialog,
|
||||
&dialogs::ImageOverlay::saving,
|
||||
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
|
||||
|
|
|
@ -35,7 +35,30 @@ public:
|
|||
void clearAll() { models.clear(); }
|
||||
|
||||
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:
|
||||
void clearRoomMessageCount(QString roomid);
|
||||
|
|
Loading…
Reference in a new issue