From 4bfe26902951d7e3d34cac7205f9074c4f31bcd8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Mon, 25 Oct 2021 16:18:06 -0400
Subject: [PATCH] datatransfer: add context menu to open location, export to
 downloads

Change-Id: I90fcbc735f084ab37abfa3b466b97cf56c640247
---
 qml.qrc                                       |  1 +
 .../DataTransferMessageDelegate.qml           |  6 ++
 src/commoncomponents/SBSContextMenu.qml       | 56 +++++++++++++++++++
 src/commoncomponents/SBSMessageBase.qml       | 23 +++++++-
 src/constant/JamiStrings.qml                  |  4 ++
 src/messagesadapter.cpp                       | 17 ++++++
 src/messagesadapter.h                         |  1 +
 7 files changed, 105 insertions(+), 3 deletions(-)
 create mode 100644 src/commoncomponents/SBSContextMenu.qml

diff --git a/qml.qrc b/qml.qrc
index 5abf8aece..04b8a564e 100644
--- a/qml.qrc
+++ b/qml.qrc
@@ -165,6 +165,7 @@
         <file>src/mainview/components/MessageListView.qml</file>
         <file>src/commoncomponents/MessageBubble.qml</file>
         <file>src/constant/MsgSeq.qml</file>
+        <file>src/commoncomponents/SBSContextMenu.qml</file>
         <file>src/commoncomponents/SBSMessageBase.qml</file>
         <file>src/commoncomponents/GeneratedMessageDelegate.qml</file>
         <file>src/commoncomponents/DataTransferMessageDelegate.qml</file>
diff --git a/src/commoncomponents/DataTransferMessageDelegate.qml b/src/commoncomponents/DataTransferMessageDelegate.qml
index 93595f9ad..8296579f5 100644
--- a/src/commoncomponents/DataTransferMessageDelegate.qml
+++ b/src/commoncomponents/DataTransferMessageDelegate.qml
@@ -66,6 +66,9 @@ Loader {
             showTime: root.showTime
             seq: root.seq
             author: Author
+            location: Body
+            transferName: TransferName
+            transferId: Id
             formattedTime: MessagesAdapter.getFormattedTime(Timestamp)
             extraHeight: progressBar.visible ? 18 : 0
             innerContent.children: [
@@ -232,6 +235,9 @@ Loader {
             showTime: root.showTime
             seq: root.seq
             author: Author
+            location: Body
+            transferName: TransferName
+            transferId: Id
             formattedTime: MessagesAdapter.getFormattedTime(Timestamp)
             bubble.visible: false
             innerContent.children: [
diff --git a/src/commoncomponents/SBSContextMenu.qml b/src/commoncomponents/SBSContextMenu.qml
new file mode 100644
index 000000000..5c49755b9
--- /dev/null
+++ b/src/commoncomponents/SBSContextMenu.qml
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2021 by Savoir-faire Linux
+ * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import QtQuick 2.15
+
+import net.jami.Models 1.1
+import net.jami.Adapters 1.1
+import net.jami.Constants 1.1
+
+import "../commoncomponents"
+import "../commoncomponents/contextmenu"
+
+ContextMenuAutoLoader {
+    id: root
+
+    property string location
+    property string transferName
+    property string transferId
+
+    property list<GeneralMenuItem> menuItems: [
+        GeneralMenuItem {
+            id: saveFile
+
+            itemName: JamiStrings.saveFile
+            onClicked: {
+                MessagesAdapter.copyToDownloads(root.transferId, root.transferName)
+            }
+        },
+        GeneralMenuItem {
+            id: openLocation
+
+            itemName: JamiStrings.openLocation
+            onClicked: {
+                MessagesAdapter.openDirectory(root.location)
+            }
+        }
+    ]
+
+    Component.onCompleted: menuItemsToLoad = menuItems
+}
+
diff --git a/src/commoncomponents/SBSMessageBase.qml b/src/commoncomponents/SBSMessageBase.qml
index c186add2b..669b5ba31 100644
--- a/src/commoncomponents/SBSMessageBase.qml
+++ b/src/commoncomponents/SBSMessageBase.qml
@@ -38,7 +38,10 @@ Control {
     property bool showTime
     property int seq
     property string author
+    property string transferId
+    property string transferName
     property string formattedTime
+    property string location
     property string hoveredLink
 
     readonly property real senderMargin: 64
@@ -124,13 +127,27 @@ Control {
         }
     }
 
+    SBSContextMenu {
+        id: ctxMenu
+
+        location: root.location
+        transferId: root.transferId
+        transferName: root.transferName
+    }
+
     MouseArea {
         id: itemMouseArea
         anchors.fill: parent
         z: -1
-        acceptedButtons: Qt.LeftButton
-        onClicked: {
-            if (root.hoveredLink)
+        acceptedButtons: Qt.LeftButton | Qt.RightButton
+        onClicked: function (mouse) {
+
+            if (mouse.button === Qt.RightButton && transferId !== "") {
+                // Context Menu for Transfers
+                ctxMenu.x = mouse.x
+                ctxMenu.y = mouse.y
+                ctxMenu.openMenu()
+            } else if (root.hoveredLink)
                 MessagesAdapter.openUrl(root.hoveredLink)
         }
     }
diff --git a/src/constant/JamiStrings.qml b/src/constant/JamiStrings.qml
index 5725172a9..5e6cb5bd4 100644
--- a/src/constant/JamiStrings.qml
+++ b/src/constant/JamiStrings.qml
@@ -368,6 +368,10 @@ Item {
     property string networkError: qsTr("Network error")
     property string somethingWentWrong: qsTr("Something went wrong")
 
+    // Context Menu
+    property string saveFile: qsTr("Save file")
+    property string openLocation: qsTr("Open location")
+
     // Updates
     property string betaInstall: qsTr("Install beta version")
     property string checkForUpdates: qsTr("Check for updates now")
diff --git a/src/messagesadapter.cpp b/src/messagesadapter.cpp
index efe43d182..84dd0b184 100644
--- a/src/messagesadapter.cpp
+++ b/src/messagesadapter.cpp
@@ -195,6 +195,23 @@ MessagesAdapter::openUrl(const QString& url)
     }
 }
 
+void
+MessagesAdapter::openDirectory(const QString& path)
+{
+    QString p = path;
+    QFileInfo f(p);
+    if (f.exists()) {
+        if (!f.isDir())
+            p = f.dir().absolutePath();
+        QString url;
+        if (!p.startsWith("file://"))
+            url = "file://" + p;
+        else
+            url = p;
+        openUrl(url);
+    }
+}
+
 void
 MessagesAdapter::acceptFile(const QString& interactionId)
 {
diff --git a/src/messagesadapter.h b/src/messagesadapter.h
index 7e7138ac3..7049e0b5a 100644
--- a/src/messagesadapter.h
+++ b/src/messagesadapter.h
@@ -97,6 +97,7 @@ protected:
     Q_INVOKABLE void cancelFile(const QString& arg);
     Q_INVOKABLE void openUrl(const QString& url);
     Q_INVOKABLE void openFile(const QString& arg);
+    Q_INVOKABLE void openDirectory(const QString& arg);
     Q_INVOKABLE void retryInteraction(const QString& interactionId);
     Q_INVOKABLE void deleteInteraction(const QString& interactionId);
     Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName);
-- 
GitLab