From 1a8b1d4dfb1af936ec68b30af475dcfbeff6453c Mon Sep 17 00:00:00 2001 From: Miquel Lionel Date: Wed, 20 Oct 2021 12:48:09 +0100 Subject: Implement saving attachment to filesystem --- message.cpp | 19 ++++++++++++++++++ message.h | 2 ++ qml/Message.qml | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/message.cpp b/message.cpp index 4da0fd1..6d02db1 100644 --- a/message.cpp +++ b/message.cpp @@ -52,6 +52,25 @@ DcMessage::getTimestamp() return QDateTime::fromSecsSinceEpoch (dc_msg_get_timestamp(m_message), Qt::UTC); } +bool +DcMessage::saveAttach(QString dest) +{ + if(!dest.isNull()){ + char *file = dc_msg_get_file(m_message); + QString fileUrl{file}; + dc_str_unref(file); + // we need to replace, so we delete file first + if (QFile::exists(dest)){ + if(!QFile::remove(dest)) + return false; + } + return QFile::copy(fileUrl, dest); + } + else{ + return false; + } +} + void DcMessage::setText(QString text) { diff --git a/message.h b/message.h index 217bba4..5389124 100644 --- a/message.h +++ b/message.h @@ -2,6 +2,7 @@ #include #include +#include #include @@ -44,6 +45,7 @@ public: Q_INVOKABLE QDateTime getTimestamp(); //Q_INVOKABLE int64_t getReceivedTimestamp(); //Q_INVOKABLE int64_t getSortTimestamp(); + Q_INVOKABLE bool saveAttach(QString); Q_INVOKABLE void setText(QString); Q_INVOKABLE void setFile(QString); Q_INVOKABLE QString getText(); diff --git a/qml/Message.qml b/qml/Message.qml index 9d8f377..fc69e8f 100644 --- a/qml/Message.qml +++ b/qml/Message.qml @@ -12,6 +12,8 @@ RowLayout { property DcMessage message property DcContext context + property var saveAsUrl: "" + property var saveSuccess: false readonly property DcContact from: context.getContact(message.fromId) readonly property DcMessage quoteMessage: message.quotedMessage readonly property DcContact quoteFrom: quoteMessage ? context.getContact(quoteMessage.fromId) : null @@ -33,6 +35,49 @@ RowLayout { } } + Popup { + id: saveAsPopup + + modal: true + focus: true + anchors.centerIn: parent + width: 400 + height: 100 + padding: 10 + onClosed: saveAsUrl = "" + contentChildren: [ + Text { + text: saveSuccess == true ? "Success !" : "Failure !" + bottomPadding: 10 + font.bold: true + font.pixelSize: 14 + }, + Text { + text: saveSuccess == true ? "The file has been saved locally at
" + saveAsUrl + "" : "An error was detected. Maybe you
dont have enough permissions to copy the file to
" + saveAsUrl + "" + topPadding: 20 + leftPadding: 10 + bottomPadding: 20 + } + ] + } + + FileDialog { + id: saveAsDialog + + title: "Save attachment `" + root.message.filename + "` as ..." + folder: shortcuts.home + selectFolder: false + selectExisting: false + onAccepted: { + var url = saveAsDialog.fileUrl.toString(); + if (url.startsWith("file://")) { + saveAsUrl = url.substring(7); + saveSuccess = root.message.saveAttach(saveAsUrl); + saveAsPopup.open(); + } + } + } + Rectangle { Layout.leftMargin: Kirigami.Units.largeSpacing Layout.preferredWidth: messageContents.width @@ -174,7 +219,7 @@ RowLayout { padding: 5 icon.name: "document-save-as" text: "Save attachment" - onClicked: console.log("dummy") + onClicked: saveAsDialog.open() } } @@ -220,11 +265,24 @@ RowLayout { Menu { id: contextMenu + Component.onCompleted: { + if (!root.message.filename.length > 0) + contextMenu.removeAction(saveAsContext); + + } + Action { text: "Info" onTriggered: messageDialog.open() } + Action { + id: saveAsContext + + text: "Save attachment as ..." + onTriggered: saveAsDialog.open() + } + } } -- cgit v1.2.3-70-g09d2