From ffa8fbf365aaf5e951d78804bc61eb7d3d776a2f Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 16 Nov 2022 20:44:34 +0100 Subject: [PATCH] Allow to open file directly Fix #506 Signed-off-by: Carl Schwan --- src/neochatroom.cpp | 13 ++++++++ src/neochatroom.h | 4 +-- src/qml/Component/Timeline/FileDelegate.qml | 37 +++++++++++++++++---- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 7bfcc4e42..205985bed 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -1241,3 +1242,15 @@ PollHandler *NeoChatRoom::poll(const QString &eventId) return m_polls[eventId]; } #endif + +bool NeoChatRoom::downloadTempFile(const QString &eventId) +{ + QTemporaryFile file; + file.setAutoRemove(false); + if (!file.open()) { + return false; + } + + downloadFile(eventId, file.fileName()); + return true; +} diff --git a/src/neochatroom.h b/src/neochatroom.h index b21cdfd08..48b9d16c0 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -39,8 +39,6 @@ struct Mention { class NeoChatRoom : public Quotient::Room { - - Q_OBJECT Q_PROPERTY(QVariantList usersTyping READ getUsersTyping NOTIFY typingChanged) Q_PROPERTY(bool hasFileUploading READ hasFileUploading WRITE setHasFileUploading NOTIFY hasFileUploadingChanged) @@ -195,6 +193,8 @@ public: bool canEncryptRoom() const; + Q_INVOKABLE bool downloadTempFile(const QString &eventId); + #ifdef QUOTIENT_07 Q_INVOKABLE PollHandler *poll(const QString &eventId); #endif diff --git a/src/qml/Component/Timeline/FileDelegate.qml b/src/qml/Component/Timeline/FileDelegate.qml index b62f1022d..9d34df954 100644 --- a/src/qml/Component/Timeline/FileDelegate.qml +++ b/src/qml/Component/Timeline/FileDelegate.qml @@ -16,6 +16,11 @@ TimelineContainer { onOpenContextMenu: openFileContext(model, fileDelegate) readonly property bool downloaded: progressInfo && progressInfo.completed + property bool autoOpenFile: false + + onDownloadedChanged: if (autoOpenFile) { + openSavedFile(); + } function saveFileAs() { const dialog = fileDialog.createObject(QQC2.ApplicationWindow.overlay) @@ -41,13 +46,17 @@ TimelineContainer { name: "downloaded" when: progressInfo.completed + PropertyChanges { + target: openButton + visible: false + } + PropertyChanges { target: downloadButton icon.name: "document-open" QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to open its downloaded file with an appropriate application", "Open File") - QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay onClicked: openSavedFile() } @@ -56,6 +65,11 @@ TimelineContainer { name: "downloading" when: progressInfo.active + PropertyChanges { + target: openButton + visible: false + } + PropertyChanges { target: sizeLabel text: i18nc("file download progress", "%1 / %2", Controller.formatByteSize(progressInfo.progress), Controller.formatByteSize(progressInfo.total)) @@ -65,7 +79,6 @@ TimelineContainer { icon.name: "media-playback-stop" QQC2.ToolTip.text: i18nc("tooltip for a button on a message; stops downloading the message's file", "Stop Download") - QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay onClicked: currentRoom.cancelFileTransfer(eventId) } }, @@ -82,10 +95,10 @@ TimelineContainer { ] Kirigami.Icon { - id: ikon source: model.fileMimetypeIcon fallback: "unknown" } + ColumnLayout { Layout.alignment: Qt.AlignVCenter Layout.fillWidth: true @@ -108,12 +121,26 @@ TimelineContainer { } } + QQC2.Button { + id: openButton + icon.name: "document-open" + onClicked: { + autoOpenFile = true; + currentRoom.downloadTempFile(eventId); + } + + QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to open its downloaded file with an appropriate application", "Open File") + QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay + } + QQC2.Button { id: downloadButton icon.name: "download" QQC2.ToolTip.text: i18nc("tooltip for a button on a message; offers ability to download its file", "Download") QQC2.ToolTip.visible: hovered + QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay } Component { @@ -122,9 +149,7 @@ TimelineContainer { FileDialog { fileMode: FileDialog.SaveFile folder: StandardPaths.writableLocation(StandardPaths.DownloadLocation) - onAccepted: { - currentRoom.downloadFile(eventId, file) - } + onAccepted: currentRoom.downloadFile(eventId, file) } } }