From fb89df3ee609e9f3e63e0679b3635516603092b5 Mon Sep 17 00:00:00 2001
From: "kali kaneko (leap communications)" <kali@leap.se>
Date: Wed, 5 Aug 2020 19:01:12 +0200
Subject: [PATCH] [feat] load provider json in qml

---
 gui/gui.qrc                  |  8 +++++---
 gui/main.cpp                 | 16 +++++++++++-----
 gui/providers/providers.json |  4 ++++
 gui/qml/main.qml             |  3 +++
 4 files changed, 23 insertions(+), 8 deletions(-)
 create mode 100644 gui/providers/providers.json

diff --git a/gui/gui.qrc b/gui/gui.qrc
index 6bc3d8de..112a1ce6 100644
--- a/gui/gui.qrc
+++ b/gui/gui.qrc
@@ -1,11 +1,12 @@
 <RCC>
     <qresource prefix="/">
+
         <file>qml/main.qml</file>
         <file>qml/AboutDialog.qml</file>
         <file>qml/DonateDialog.qml</file>
         <file>qml/LoginDialog.qml</file>
         <file>qml/LoginOKDialog.qml</file>
-         
+
         <file>assets/icon/png/black/vpn_off.png</file>
         <file>assets/icon/png/black/vpn_on.png</file>
         <file>assets/icon/png/black/vpn_wait_0.png</file>
@@ -19,7 +20,8 @@
         <file>assets/icon/png/white/vpn_wait_1.png</file>
         <file>assets/icon/png/white/vpn_wait_2.png</file>
         <file>assets/icon/png/white/vpn_wait_3.png</file>
-    </qresource>
-</RCC>
 
+        <file alias="providers.json">providers/providers.json</file>
 
+    </qresource>
+</RCC>
diff --git a/gui/main.cpp b/gui/main.cpp
index e3b8530f..8c7de4f5 100644
--- a/gui/main.cpp
+++ b/gui/main.cpp
@@ -40,7 +40,6 @@ void signalHandler(int) {
 
 int main(int argc, char **argv) {
     signal(SIGINT, signalHandler);
-    bool debugQml = getEnv("DEBUG_QML_DATA") == "yes";
 
     Backend backend;
 
@@ -104,19 +103,27 @@ int main(int argc, char **argv) {
     QTranslator translator;
     translator.load(QLocale(), QLatin1String("main"), QLatin1String("_"), QLatin1String(":/i18n"));
     app.installTranslator(&translator);
-    
+
     QQmlApplicationEngine engine;
     QQmlContext *ctx = engine.rootContext();
 
     QJsonModel *model = new QJsonModel;
 
+    /* load providers json */
+    QFile providerJson (":/providers.json");
+    providerJson.open(QIODevice::ReadOnly | QIODevice::Text);
+    QJsonModel *providers = new QJsonModel;
+    providers->loadJson(providerJson.readAll());
+
     /* the backend handler has slots for calling back to Go when triggered by
        signals in Qml. */
     ctx->setContextProperty("backend", &backend);
 
-    /* we pass the json model and set some useful flags */
+    /* set the json model, load providers.json */
     ctx->setContextProperty("jsonModel", model);
-    ctx->setContextProperty("debugQml", debugQml);
+    ctx->setContextProperty("providers", providers);
+
+    /* set some useful flags */
     ctx->setContextProperty("systrayVisible", !hideSystray);
 
     engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
@@ -133,7 +140,6 @@ int main(int argc, char **argv) {
             QGuiApplication::quit();
     });
 
-
     /* register statusChanged callback with CGO */
     const char *stCh = "OnStatusChanged";
     GoString statusChangedEvt = {stCh, (long int)strlen(stCh)};
diff --git a/gui/providers/providers.json b/gui/providers/providers.json
new file mode 100644
index 00000000..7ba0ef97
--- /dev/null
+++ b/gui/providers/providers.json
@@ -0,0 +1,4 @@
+[
+    {"name": "provider1", "url": "https://example.com"},
+    {"name": "provider2", "url": "https://foobar.com"}
+]
diff --git a/gui/qml/main.qml b/gui/qml/main.qml
index 9f8bea3f..da6b9ee7 100644
--- a/gui/qml/main.qml
+++ b/gui/qml/main.qml
@@ -56,6 +56,9 @@ ApplicationWindow {
          Loaders as a placeholder for all the many dialogs, or to load
          a nice splash screen etc...  */
 
+        console.debug("Pre-seeded providers:");
+        console.debug(providers.getJson());
+
         app.visible = true;
         show();
         hide();
-- 
GitLab