From 971c28f6563de35b1d66401d6919f86787af0611 Mon Sep 17 00:00:00 2001
From: "kali kaneko (leap communications)" <kali@leap.se>
Date: Fri, 12 Jun 2020 12:40:59 +0200
Subject: [PATCH] [feat] working donate dialog

Signed-off-by: kali kaneko (leap communications) <kali@leap.se>
---
 bitmask.pro              |  2 +-
 gui/backend.go           | 60 +++++++++++++++++++++++++++++++---------
 gui/gui.qrc              |  1 +
 gui/handlers.cpp         |  6 ++++
 gui/handlers.h           |  1 +
 gui/main.cpp             |  4 +--
 gui/qml/DonateDialog.qml | 27 ++++++++++++++++++
 gui/qml/main.qml         | 23 +++++++++------
 8 files changed, 99 insertions(+), 25 deletions(-)
 create mode 100644 gui/qml/DonateDialog.qml

diff --git a/bitmask.pro b/bitmask.pro
index 910ba23d..115aec71 100644
--- a/bitmask.pro
+++ b/bitmask.pro
@@ -1,7 +1,7 @@
 # FIXME: this should be overwritten by build templates
 TARGET=riseup-vpn
 
-CONFIG += qt static
+CONFIG += qt staticlib
 windows:CONFIG += console
 unix:DEBUG:CONFIG += debug
 lessThan(QT_MAJOR_VERSION, 5): error("requires Qt 5")
diff --git a/gui/backend.go b/gui/backend.go
index 43f7b67c..d05ba7a9 100644
--- a/gui/backend.go
+++ b/gui/backend.go
@@ -6,14 +6,18 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
+	"io"
 	"log"
 	"net/http"
 	"os"
+	"path"
 	"reflect"
 	"sync"
+	//"time"
 	"unsafe"
 
 	"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
+	"0xacab.org/leap/bitmask-vpn/pkg/config"
 	"0xacab.org/leap/bitmask-vpn/pkg/pickle"
 	"0xacab.org/leap/bitmask-vpn/pkg/systray2"
 	"github.com/jmshal/go-locale"
@@ -71,8 +75,6 @@ func trigger(event string) {
 
 /* connection status */
 
-const logFile = "systray.log"
-
 const (
 	offStr      = "off"
 	startingStr = "starting"
@@ -130,6 +132,7 @@ func (s status) fromString(st string) status {
 type connectionCtx struct {
 	AppName  string `json:"appName"`
 	Provider string `json:"provider"`
+	Donate   bool   `json:"donate"`
 	Status   status `json:"status"`
 	bm       bitmask.Bitmask
 }
@@ -170,8 +173,14 @@ func setStatus(st status) {
 	go trigger(OnStatusChanged)
 }
 
+func toggleDonate() {
+	stmut.Lock()
+	defer stmut.Unlock()
+	ctx.Donate = !ctx.Donate
+	go trigger(OnStatusChanged)
+}
+
 func setStatusFromStr(stStr string) {
-	log.Println("status:", stStr)
 	setStatus(unknown.fromString(stStr))
 }
 
@@ -184,8 +193,18 @@ func initPrinter() *message.Printer {
 	return message.NewPrinter(message.MatchLanguage(locale, "en"))
 }
 
+const logFile = "systray.log"
+
+var logger io.Closer
+
 // initializeBitmask instantiates a bitmask connection
 func initializeBitmask() {
+	_, err := config.ConfigureLogger(path.Join(config.Path, logFile))
+
+	if err != nil {
+		log.Println("Can't configure logger: ", err)
+	}
+
 	if ctx == nil {
 		log.Println("error: cannot initialize bitmask, ctx is nil")
 		os.Exit(1)
@@ -223,6 +242,7 @@ func initializeContext(provider, appName string) {
 	ctx = &connectionCtx{
 		AppName:  appName,
 		Provider: provider,
+		Donate:   false,
 		Status:   st,
 	}
 	go trigger(OnStatusChanged)
@@ -263,27 +283,32 @@ func mockUI() {
 
 //export SwitchOn
 func SwitchOn() {
-	setStatus(starting)
-	startVPN()
+	go setStatus(starting)
+	go startVPN()
 }
 
 //export SwitchOff
 func SwitchOff() {
-	setStatus(stopping)
-	stopVPN()
+	go setStatus(stopping)
+	go stopVPN()
+}
+
+//export Unblock
+func Unblock() {
+	fmt.Println("unblock... [not implemented]")
 }
 
 //export Quit
 func Quit() {
 	if ctx.Status != off {
-		setStatus(stopping)
+		go setStatus(stopping)
 		stopVPN()
 	}
 }
 
-//export Unblock
-func Unblock() {
-	fmt.Println("unblock... [not implemented]")
+//export ToggleDonate
+func ToggleDonate() {
+	toggleDonate()
 }
 
 //export SubscribeToEvent
@@ -293,12 +318,21 @@ func SubscribeToEvent(event string, f unsafe.Pointer) {
 
 //export InitializeBitmaskContext
 func InitializeBitmaskContext() {
-	provider := "black.riseup.net"
-	appName := "RiseupVPN"
+	provider := config.Provider
+	appName := config.ApplicationName
 	initOnce.Do(func() {
 		initializeContext(provider, appName)
 	})
 	go ctx.updateStatus()
+
+	/* DEBUG
+	timer := time.NewTimer(time.Second * 3)
+	go func() {
+		<-timer.C
+		fmt.Println("donate timer fired")
+		toggleDonate()
+	}()
+	*/
 }
 
 //export RefreshContext
diff --git a/gui/gui.qrc b/gui/gui.qrc
index 705c2b74..b0cd72c9 100644
--- a/gui/gui.qrc
+++ b/gui/gui.qrc
@@ -1,6 +1,7 @@
 <RCC>
     <qresource prefix="/">
         <file>qml/main.qml</file>
+        <file>qml/DonateDialog.qml</file>
          
         <file>assets/icon/png/black/vpn_off.png</file>
         <file>assets/icon/png/black/vpn_on.png</file>
diff --git a/gui/handlers.cpp b/gui/handlers.cpp
index 79fbb582..e37de2d1 100644
--- a/gui/handlers.cpp
+++ b/gui/handlers.cpp
@@ -23,8 +23,14 @@ void Backend::unblock()
     Unblock();
 }
 
+void Backend::toggleDonate()
+{
+    ToggleDonate();
+}
+
 void Backend::quit()
 {
     Quit();
     emit this->quitDone();
 }
+
diff --git a/gui/handlers.h b/gui/handlers.h
index 0a8782d7..3fe9eed5 100644
--- a/gui/handlers.h
+++ b/gui/handlers.h
@@ -33,6 +33,7 @@ public slots:
     void switchOn();
     void switchOff();
     void unblock();
+    void toggleDonate();
     void quit();
 };
 
diff --git a/gui/main.cpp b/gui/main.cpp
index c55b538b..5ecd4add 100644
--- a/gui/main.cpp
+++ b/gui/main.cpp
@@ -42,13 +42,11 @@ int main(int argc, char **argv) {
         exit(0);
     }
 
-
     QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
     QApplication app(argc, argv);
 
     if (!QSystemTrayIcon::isSystemTrayAvailable()) {
-        qDebug() << "No systray icon available. Things won't work for now, sorry...";
-        exit(1);
+        qDebug() << "No systray icon available. Things might not work for now, sorry...";
     }
     
     app.setQuitOnLastWindowClosed(false);
diff --git a/gui/qml/DonateDialog.qml b/gui/qml/DonateDialog.qml
new file mode 100644
index 00000000..b7431abe
--- /dev/null
+++ b/gui/qml/DonateDialog.qml
@@ -0,0 +1,27 @@
+import QtQuick 2.0
+import QtQuick.Dialogs 1.2
+
+MessageDialog {
+    standardButtons: StandardButton.No | StandardButton.Yes
+    title: "Donate"
+    icon: StandardIcon.Warning
+    text: getText()
+
+    function getText() {
+        var _name = ctx ? ctx.appName : "vpn"
+        var donateTxt = qsTr(
+            "The %1 service is expensive to run. Because we don't want to store personal information about you, there are no accounts or billing for this service. But if you want the service to continue, donate at least $5 each month.\n\nDo you want to donate now?").arg(_name)
+        return donateTxt
+    }
+
+    onAccepted: {
+        if (backend) {
+            backend.donateAccepted(true)
+        }
+    }
+    onRejected: {
+        if (backend) {
+            backend.donateAccepted(false)
+        }
+    }
+}
diff --git a/gui/qml/main.qml b/gui/qml/main.qml
index 80e5e629..98eac800 100644
--- a/gui/qml/main.qml
+++ b/gui/qml/main.qml
@@ -11,10 +11,16 @@ ApplicationWindow {
 
     property var     ctx
 
+
     Connections {
         target: jsonModel
         onDataChanged: {
             ctx = JSON.parse(jsonModel.getJson());
+            if (ctx.donate == 'true') {
+                console.debug(jsonModel.getJson())
+                donate.visible = true
+                backend.toggleDonate()
+            }
         }
     }
 
@@ -92,6 +98,7 @@ ApplicationWindow {
             StateGroup {
                 id: vpn
                 state: ctx ? ctx.status : ""
+            
                 states: [
                     State { name: "initializing" },
                     State {
@@ -106,7 +113,7 @@ ApplicationWindow {
                     },
                     State {
                         name: "starting"
-                       PropertyChanges { target: systray; tooltip: toHuman("connecting"); icon.source: icons["wait"] }
+                        PropertyChanges { target: systray; tooltip: toHuman("connecting"); icon.source: icons["wait"] }
                         PropertyChanges { target: statusItem; text: toHuman("connecting") }
                     },
                     State {
@@ -181,13 +188,17 @@ ApplicationWindow {
         }
     }
 
+    DonateDialog {
+        visible: false
+        id: donate
+    }
+}
+
+
     /*
     LoginDialog {
         id: login
     }
-    DonateDialog {
-        id: donate
-    }
     MessageDialog {
         id: about
         buttons: MessageDialog.Ok
@@ -223,7 +234,3 @@ ApplicationWindow {
         visible: ctxSystray.errorInitMsg != ""
     }
     */
-
-
-
-}
-- 
GitLab