From eb97dc510c92a2fc1a340cccf4103068699947c4 Mon Sep 17 00:00:00 2001
From: "kali kaneko (leap communications)" <kali@leap.se>
Date: Tue, 23 Jun 2020 17:12:42 +0200
Subject: [PATCH] [refactor] use qt argument parsing

---
 gui/backend.go     | 10 ++++++++++
 gui/handlers.cpp   | 10 ++++++++++
 gui/handlers.h     |  2 ++
 gui/main.cpp       | 31 ++++++++++++++++++++-----------
 pkg/backend/api.go | 15 +++++++++++++++
 5 files changed, 57 insertions(+), 11 deletions(-)

diff --git a/gui/backend.go b/gui/backend.go
index 4a73cc26..faf682a4 100644
--- a/gui/backend.go
+++ b/gui/backend.go
@@ -12,6 +12,16 @@ import (
 	"0xacab.org/leap/bitmask-vpn/pkg/backend"
 )
 
+//export GetVersion
+func GetVersion() *C.char {
+	return (*C.char)(backend.GetVersion())
+}
+
+//export GetAppName
+func GetAppName() *C.char {
+	return (*C.char)(backend.GetAppName())
+}
+
 //export SwitchOn
 func SwitchOn() {
 	backend.SwitchOn()
diff --git a/gui/handlers.cpp b/gui/handlers.cpp
index 9ce268d2..de54161d 100644
--- a/gui/handlers.cpp
+++ b/gui/handlers.cpp
@@ -10,6 +10,16 @@ Backend::Backend(QObject *parent) : QObject(parent)
 {
 }
 
+QString Backend::getAppName()
+{
+    return QString(GetAppName());
+}
+
+QString Backend::getVersion()
+{
+    return QString(GetVersion());
+}
+
 void Backend::switchOn()
 {
     SwitchOn();
diff --git a/gui/handlers.h b/gui/handlers.h
index 0dc4c1ea..8f89279d 100644
--- a/gui/handlers.h
+++ b/gui/handlers.h
@@ -30,6 +30,8 @@ signals:
     void quitDone();
 
 public slots:
+    QString getAppName();
+    QString getVersion();
     void switchOn();
     void switchOff();
     void unblock();
diff --git a/gui/main.cpp b/gui/main.cpp
index 73d55ccd..e5486dfe 100644
--- a/gui/main.cpp
+++ b/gui/main.cpp
@@ -1,13 +1,11 @@
 #include <csignal>
-#include <string>
-
 #include <QApplication>
-#include <QSystemTrayIcon>
 #include <QTimer>
 #include <QTranslator>
-#include <QtQml>
-#include <QQmlApplicationEngine>
+#include <QCommandLineParser>
 #include <QQuickWindow>
+#include <QSystemTrayIcon>
+#include <QtQml>
 
 #include "handlers.h"
 #include "qjsonmodel.h"
@@ -42,18 +40,30 @@ void signalHandler(int) {
 
 int main(int argc, char **argv) {
     signal(SIGINT, signalHandler);
-
     bool debugQml = getEnv("DEBUG_QML_DATA") == "yes";
 
-    if (argc > 1 && strcmp(argv[1], "install-helpers") == 0) {
+    Backend backend;
+
+    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
+    QApplication::setApplicationName(backend.getAppName());
+    QApplication::setApplicationVersion(backend.getVersion());
+    QApplication app(argc, argv);
+
+
+    QCommandLineParser parser;
+    parser.setApplicationDescription(backend.getAppName() + ": a fast and secure VPN. Powered by Bitmask.");
+    parser.addHelpOption();
+    parser.addVersionOption();
+    parser.process(app);
+
+    const QStringList args = parser.positionalArguments();
+
+    if (args.at(0) == "install-helpers") {
         qDebug() << "Will try to install helpers with sudo";
         InstallHelpers();
         exit(0);
     }
 
-    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
-    QApplication app(argc, argv);
-
     if (!QSystemTrayIcon::isSystemTrayAvailable()) {
         qDebug() << "No systray icon available. Things might not work for now, sorry...";
     }
@@ -72,7 +82,6 @@ int main(int argc, char **argv) {
 
     /* the backend handler has slots for calling back to Go when triggered by
        signals in Qml. */
-    Backend backend;
     ctx->setContextProperty("backend", &backend);
 
     /* set the json model, load the qml */
diff --git a/pkg/backend/api.go b/pkg/backend/api.go
index fea38dbc..64dac9d6 100644
--- a/pkg/backend/api.go
+++ b/pkg/backend/api.go
@@ -9,6 +9,7 @@ import (
 	"unsafe"
 
 	"0xacab.org/leap/bitmask-vpn/pkg/bitmask"
+	"0xacab.org/leap/bitmask-vpn/pkg/config/version"
 	"0xacab.org/leap/bitmask-vpn/pkg/pickle"
 )
 
@@ -74,3 +75,17 @@ func EnableMockBackend() {
 	log.Println("[+] Mocking ui interaction on port 8080. \nTry 'curl localhost:8080/{on|off|failed}' to toggle status.")
 	go enableMockBackend()
 }
+
+/* these two are a bit redundant since we already add them to ctx. however, we
+   want to have them available before everything else, to be able to parse cli
+   arguments. In the long run, we probably want to move all vendoring to qt, so
+   this probably should not live in the backend. */
+
+func GetVersion() *C.char {
+	return C.CString(version.VERSION)
+}
+
+func GetAppName() *C.char {
+	p := bitmask.GetConfiguredProvider()
+	return C.CString(p.AppName)
+}
-- 
GitLab