From 417a0fe042da5363fe6679b5ec54a42c78b9173b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 9 Jul 2021 16:05:36 -0400
Subject: [PATCH] generalsettings: add file transfer settings

+ Auto accept files from trusted sources (default: true)
+ Auto accept files from untrusted sources (default: false)
+ Size limit (default: 20Mb)

Change-Id: I0e1068e3996786b23ba9a7797d02b7f8bb0f89d1
GitLab: #160
---
 qml.qrc                                       |  1 +
 src/appsettingsmanager.h                      |  3 +
 src/constant/JamiStrings.qml                  |  7 ++
 src/mainapplication.cpp                       |  8 ++
 src/settingsadapter.cpp                       | 21 +++++
 src/settingsadapter.h                         |  4 +
 src/settingsview/SettingsView.qml             |  1 +
 .../components/FileTransferSettings.qml       | 94 +++++++++++++++++++
 .../components/GeneralSettingsPage.qml        | 15 +++
 .../components/SettingSpinBox.qml             |  6 ++
 10 files changed, 160 insertions(+)
 create mode 100644 src/settingsview/components/FileTransferSettings.qml

diff --git a/qml.qrc b/qml.qrc
index c33ffabbe..233a6e2f4 100644
--- a/qml.qrc
+++ b/qml.qrc
@@ -33,6 +33,7 @@
         <file>src/commoncomponents/SpinningAnimation.qml</file>
         <file>src/settingsview/SettingsView.qml</file>
         <file>src/settingsview/components/ChatviewSettings.qml</file>
+        <file>src/settingsview/components/FileTransferSettings.qml</file>
         <file>src/settingsview/components/SettingsMenu.qml</file>
         <file>src/settingsview/components/SettingsHeader.qml</file>
         <file>src/settingsview/components/SystemSettings.qml</file>
diff --git a/src/appsettingsmanager.h b/src/appsettingsmanager.h
index d3b081110..6ff083ff1 100644
--- a/src/appsettingsmanager.h
+++ b/src/appsettingsmanager.h
@@ -36,6 +36,9 @@ const QString defaultDownloadPath = QStandardPaths::writableLocation(
     X(DownloadPath, defaultDownloadPath) \
     X(EnableNotifications, true) \
     X(EnableTypingIndicator, true) \
+    X(AllowFromUntrusted, false) \
+    X(AcceptTransferBelow, 20) \
+    X(AutoAcceptFiles, true) \
     X(DisplayHyperlinkPreviews, true) \
     X(EnableDarkTheme, false) \
     X(AutoUpdate, true) \
diff --git a/src/constant/JamiStrings.qml b/src/constant/JamiStrings.qml
index b62233279..dfc4c83d6 100644
--- a/src/constant/JamiStrings.qml
+++ b/src/constant/JamiStrings.qml
@@ -321,6 +321,13 @@ Item {
     property string enableTypingIndicator: qsTr("Enable typing indicators")
     property string displayHyperlinkPreviews: qsTr("Display hyperlink previews in the chatview")
 
+    // File transfer settings
+    property string fileTransfer: qsTr("File transfer")
+    property string allowFromUntrusted: qsTr("Allow incoming files from unknown contacts")
+    property string autoAcceptFiles: qsTr("Automatically accept incoming files")
+    property string acceptTransferBelow: qsTr("Accept transfer limit")
+    property string acceptTransferTooltip: qsTr("in MB, 0 = unlimited")
+
     // Updates
     property string betaInstall: qsTr("Install beta version")
     property string checkForUpdates: qsTr("Check for updates now")
diff --git a/src/mainapplication.cpp b/src/mainapplication.cpp
index 2e07c911b..26d8200c9 100644
--- a/src/mainapplication.cpp
+++ b/src/mainapplication.cpp
@@ -263,7 +263,15 @@ MainApplication::init()
     }
 
     auto downloadPath = settingsManager_->getValue(Settings::Key::DownloadPath);
+    auto allowTransferFromUntrusted = settingsManager_->getValue(Settings::Key::AllowFromUntrusted)
+                                          .toBool();
+    auto allowTransferFromTrusted = settingsManager_->getValue(Settings::Key::AutoAcceptFiles)
+                                        .toBool();
+    auto acceptTransferBelow = settingsManager_->getValue(Settings::Key::AcceptTransferBelow).toInt();
     lrcInstance_->accountModel().downloadDirectory = downloadPath.toString() + "/";
+    lrcInstance_->accountModel().autoTransferFromUntrusted = allowTransferFromUntrusted;
+    lrcInstance_->accountModel().autoTransferFromTrusted = allowTransferFromTrusted;
+    lrcInstance_->accountModel().autoTransferSizeThreshold = acceptTransferBelow;
 
     initQmlLayer();
     initSystray();
diff --git a/src/settingsadapter.cpp b/src/settingsadapter.cpp
index 1702aaeed..3c60c2435 100644
--- a/src/settingsadapter.cpp
+++ b/src/settingsadapter.cpp
@@ -897,6 +897,27 @@ SettingsAdapter::registrationExpirationTimeSpinBoxValueChanged(int value)
     lrcInstance_->accountModel().setAccountConfig(lrcInstance_->get_currentAccountId(), confProps);
 }
 
+void
+SettingsAdapter::autoAcceptFiles(bool value)
+{
+    lrcInstance_->accountModel().autoTransferFromTrusted = value;
+    setAppValue(Settings::Key::AutoAcceptFiles, value);
+}
+
+void
+SettingsAdapter::allowFromUntrusted(bool value)
+{
+    lrcInstance_->accountModel().autoTransferFromUntrusted = value;
+    setAppValue(Settings::Key::AllowFromUntrusted, value);
+}
+
+void
+SettingsAdapter::acceptTransferBelow(int value)
+{
+    lrcInstance_->accountModel().autoTransferSizeThreshold = value;
+    setAppValue(Settings::Key::AcceptTransferBelow, value);
+}
+
 void
 SettingsAdapter::networkInterfaceSpinBoxValueChanged(int value)
 {
diff --git a/src/settingsadapter.h b/src/settingsadapter.h
index cc06b3070..7b5d9efca 100644
--- a/src/settingsadapter.h
+++ b/src/settingsadapter.h
@@ -202,6 +202,10 @@ public:
     Q_INVOKABLE void videoRTPMinPortSpinBoxEditFinished(int value);
     Q_INVOKABLE void videoRTPMaxPortSpinBoxEditFinished(int value);
 
+    Q_INVOKABLE void autoAcceptFiles(bool value);
+    Q_INVOKABLE void allowFromUntrusted(bool value);
+    Q_INVOKABLE void acceptTransferBelow(int value);
+
     Q_INVOKABLE void tlsProtocolComboBoxIndexChanged(const int& index);
 
     Q_INVOKABLE void setDeviceName(QString text);
diff --git a/src/settingsview/SettingsView.qml b/src/settingsview/SettingsView.qml
index 75e783acd..1c11c5ea8 100644
--- a/src/settingsview/SettingsView.qml
+++ b/src/settingsview/SettingsView.qml
@@ -61,6 +61,7 @@ Rectangle {
                 pageIdCurrentAccountSettings.updateAccountInfoDisplayed()
                 break
             case SettingsView.General:
+                generalSettings.updateValues()
                 AccountAdapter.stopPreviewing()
                 selectedMenu = sel
                 break
diff --git a/src/settingsview/components/FileTransferSettings.qml b/src/settingsview/components/FileTransferSettings.qml
new file mode 100644
index 000000000..894e28b51
--- /dev/null
+++ b/src/settingsview/components/FileTransferSettings.qml
@@ -0,0 +1,94 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+import QtQuick 2.14
+import QtQuick.Controls 2.14
+import QtQuick.Layouts 1.14
+
+import net.jami.Adapters 1.0
+import net.jami.Enums 1.0
+import net.jami.Constants 1.0
+
+ColumnLayout {
+    id:root
+
+    property int itemWidth
+
+    function updateValues() {
+        acceptTransferBelowSpinBox.setValue(SettingsAdapter.getAppValue(Settings.AcceptTransferBelow))
+        allowFromUntrustedCheckbox.checked = SettingsAdapter.getAppValue(Settings.AllowFromUntrusted)
+        autoAcceptFilesCheckbox.checked = SettingsAdapter.getAppValue(Settings.AutoAcceptFiles)
+    }
+
+    Label {
+        Layout.fillWidth: true
+
+        text: JamiStrings.fileTransfer
+        font.pointSize: JamiTheme.headerFontSize
+        font.kerning: true
+        color: JamiTheme.textColor
+
+        horizontalAlignment: Text.AlignLeft
+        verticalAlignment: Text.AlignVCenter
+    }
+
+    ToggleSwitch {
+        id: allowFromUntrustedCheckbox
+        Layout.fillWidth: true
+        Layout.leftMargin: JamiTheme.preferredMarginSize
+
+        checked: SettingsAdapter.getAppValue(Settings.AllowFromUntrusted)
+
+        labelText: JamiStrings.allowFromUntrusted
+        fontPointSize: JamiTheme.settingsFontSize
+
+        tooltipText: JamiStrings.allowFromUntrusted
+
+        onSwitchToggled: SettingsAdapter.allowFromUntrusted(checked)
+    }
+
+    ToggleSwitch {
+        id: autoAcceptFilesCheckbox
+        Layout.fillWidth: true
+        Layout.leftMargin: JamiTheme.preferredMarginSize
+
+        checked: SettingsAdapter.getAppValue(Settings.AutoAcceptFiles)
+
+        labelText: JamiStrings.autoAcceptFiles
+        fontPointSize: JamiTheme.settingsFontSize
+
+        tooltipText: JamiStrings.autoAcceptFiles
+
+        onSwitchToggled: SettingsAdapter.autoAcceptFiles(checked)
+    }
+
+    SettingSpinBox {
+        id: acceptTransferBelowSpinBox
+        Layout.fillWidth: true
+        Layout.leftMargin: JamiTheme.preferredMarginSize
+
+        title: JamiStrings.acceptTransferBelow
+        tooltipText: JamiStrings.acceptTransferTooltip
+        itemWidth: root.itemWidth
+        bottomValue: 0
+        topValue: 99999999
+        step: 1
+
+        onNewValue: SettingsAdapter.acceptTransferBelow(valueField)
+    }
+}
diff --git a/src/settingsview/components/GeneralSettingsPage.qml b/src/settingsview/components/GeneralSettingsPage.qml
index b14be3899..de6328b7f 100644
--- a/src/settingsview/components/GeneralSettingsPage.qml
+++ b/src/settingsview/components/GeneralSettingsPage.qml
@@ -39,6 +39,10 @@ Rectangle {
 
     color: JamiTheme.secondaryBackgroundColor
 
+    function updateValues() {
+        fileTransferSettings.updateValues()
+    }
+
     ColumnLayout {
         id: generalSettingsColumnLayout
 
@@ -66,6 +70,17 @@ Rectangle {
             itemWidth: preferredColumnWidth
         }
 
+        // file transfer setting panel
+        FileTransferSettings {
+            id: fileTransferSettings
+            Layout.fillWidth: true
+            Layout.topMargin: JamiTheme.preferredMarginSize
+            Layout.leftMargin: JamiTheme.preferredMarginSize
+            Layout.rightMargin: JamiTheme.preferredMarginSize
+
+            itemWidth: preferredColumnWidth
+        }
+
         // call recording setting panel
         RecordingSettings {
             Layout.fillWidth: true
diff --git a/src/settingsview/components/SettingSpinBox.qml b/src/settingsview/components/SettingSpinBox.qml
index 58d4d67c8..e749e728d 100644
--- a/src/settingsview/components/SettingSpinBox.qml
+++ b/src/settingsview/components/SettingSpinBox.qml
@@ -39,6 +39,7 @@ RowLayout {
     property int topValue
     property int step
     property int valueField
+    property string tooltipText: ""
 
     signal newValue
 
@@ -87,5 +88,10 @@ RowLayout {
             border.color: enabled? root.borderColor : "transparent"
             color: JamiTheme.editBackgroundColor
         }
+
+        hoverEnabled: true
+        ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
+        ToolTip.visible: hovered && (root.tooltipText.length > 0)
+        ToolTip.text: root.tooltipText
     }
 }
-- 
GitLab