From 1c698bff836e36e76ad43642d6ec7411d97b0a0d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 1 Feb 2018 14:31:07 +0100 Subject: [PATCH] Qml App add option to control DHT behaviour As bitDHT is very noisy and keeps the radio always on on mobile thus having huge impact on the battery life, DHT is now turned on only when the App is interacting with the user byt default, on the option tab it is possible to change this behaviour to set it always off, always on or active depending on user interaction. To handle settings AppSettings singleton has been added and a button on the sidebar to edit configurations too --- retroshare-qml-app/src/AppSettings.qml | 27 +++++++++ retroshare-qml-app/src/Options.qml | 53 +++++++++++++++++ retroshare-qml-app/src/components/SideBar.qml | 11 +++- retroshare-qml-app/src/main-app.qml | 59 +++++++++++++++++++ retroshare-qml-app/src/qml.qrc | 2 + retroshare-qml-app/src/qmldir | 1 + 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 retroshare-qml-app/src/AppSettings.qml create mode 100644 retroshare-qml-app/src/Options.qml diff --git a/retroshare-qml-app/src/AppSettings.qml b/retroshare-qml-app/src/AppSettings.qml new file mode 100644 index 000000000..72325e4b2 --- /dev/null +++ b/retroshare-qml-app/src/AppSettings.qml @@ -0,0 +1,27 @@ +/* + * RetroShare Android QML App + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +pragma Singleton + +import Qt.labs.settings 1.0 + +Settings +{ + property string dhtMode: "Interactive" +} + diff --git a/retroshare-qml-app/src/Options.qml b/retroshare-qml-app/src/Options.qml new file mode 100644 index 000000000..46943b597 --- /dev/null +++ b/retroshare-qml-app/src/Options.qml @@ -0,0 +1,53 @@ +/* + * RetroShare Android QML App + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import QtQuick 2.7 +import QtQuick.Controls 2.0 +import "." //Needed for AppSettings singleton + + +Item +{ + Column + { + spacing: 5 + padding: 10 + + Row + { + spacing: 10 + + Label + { + text: "DHT mode:" + anchors.verticalCenter: parent.verticalCenter + } + + ComboBox + { + model: ["On", "Off", "Interactive"] + Component.onCompleted: currentIndex = find(AppSettings.dhtMode) + onActivated: + { + AppSettings.dhtMode = displayText + focus = false + } + } + } + } +} diff --git a/retroshare-qml-app/src/components/SideBar.qml b/retroshare-qml-app/src/components/SideBar.qml index 7e7c536fc..e1f605d5c 100644 --- a/retroshare-qml-app/src/components/SideBar.qml +++ b/retroshare-qml-app/src/components/SideBar.qml @@ -214,6 +214,10 @@ Drawer platformGW.shareUrl(nodeUrl); }) }, + "Options": function() + { + stackView.push("qrc:/Options.qml") + }, "Terminate Core": function() { rsApi.request("/control/shutdown"); @@ -245,12 +249,17 @@ Drawer icon: "/icons/share.svg" } ListElement + { + title: "Options" + showOnOsAndroid: false + icon: "/icons/options.svg" + } + ListElement { title: "Terminate Core" showOnOsAndroid: false icon: "/icons/exit.svg" } - } ScrollIndicator.vertical: ScrollIndicator { } diff --git a/retroshare-qml-app/src/main-app.qml b/retroshare-qml-app/src/main-app.qml index 844da7e1e..ff04ab205 100644 --- a/retroshare-qml-app/src/main-app.qml +++ b/retroshare-qml-app/src/main-app.qml @@ -627,4 +627,63 @@ ApplicationWindow } } + property var netStatus: ({}) + function refreshNetStatus(optCallback) + { + console.log("refreshNetStatus(optCallback)", optCallback) + rsApi.request("/peers/get_network_options", "", function(par) + { + var json = JSON.parse(par.response) + console.log("got", par.response) + mainWindow.netStatus = json.data + if(typeof(optCallback) === "function") optCallback(); + }) + } + + function updateDhtMode(stopping) + { + console.log("updateDhtMode(stopping)", stopping) + + switch(AppSettings.dhtMode) + { + case "On": mainWindow.netStatus.discovery_mode = 0; break; + case "Off": mainWindow.netStatus.discovery_mode = 1; break; + case "Interactive": mainWindow.netStatus.discovery_mode = stopping ? 1:0; break + } + + console.log("updateDhtMode(stopping)", stopping, netStatus.discovery_mode) + + rsApi.request("/peers/set_network_options", JSON.stringify(netStatus)) + } + + Component.onDestruction: if(coreReady) updateDhtMode(true) + + + Connections + { + target: AppSettings + onDhtModeChanged: + { + Qt.application.state + console.log("onDhtModeChanged", AppSettings.dhtMode) + if(coreReady) updateDhtMode(false) + } + } + + Connections + { + target: stackView + onStateChanged: if(coreReady) refreshNetStatus(function() {updateDhtMode(false)}) + } + + Connections + { + target: Qt.application + onStateChanged: + { + console.log("Qt.application.state changed to", Qt.ApplicationSuspended) + if(coreReady && (Qt.application.state === Qt.ApplicationSuspended)) + updateDhtMode(true) + } + } } diff --git a/retroshare-qml-app/src/qml.qrc b/retroshare-qml-app/src/qml.qrc index b03daf161..8d681e0ae 100644 --- a/retroshare-qml-app/src/qml.qrc +++ b/retroshare-qml-app/src/qml.qrc @@ -744,5 +744,7 @@ icons/faces/male64/mouth9.png components/Faces.qml fonts/OpenSansEmoji.ttf + Options.qml + AppSettings.qml diff --git a/retroshare-qml-app/src/qmldir b/retroshare-qml-app/src/qmldir index 5cd29454b..f4613a5de 100644 --- a/retroshare-qml-app/src/qmldir +++ b/retroshare-qml-app/src/qmldir @@ -3,4 +3,5 @@ singleton ClipboardWrapper 1.0 ClipboardWrapper.qml singleton ChatCache ChatCache.qml singleton StyleChat styles/ChatStyle.qml singleton StyleSideBar styles/SideBarStyle.qml +singleton AppSettings 1.0 AppSettings.qml