Settings stored persistently using QSettings.
[openvpnui.git] / src / vpncontrol.cpp
index a519dce..7bd002f 100644 (file)
@@ -1,16 +1,26 @@
 #include "vpncontrol.h"
 #include "stdio.h"
+#include <QSettings>
+
 
 VPNControl::VPNControl(QObject *parent) :
     QObject(parent),
     vpnProcess(NULL),
     vpnStatus(VPNSTATUS_INVALID),
     server(""),
-    port(1174),
+    port(1194),
     compressed(true),
     useTLS(true),
     tlsDirection(1)
 {
+    // Read in the settings
+    QSettings settings;
+
+    server = settings.value("server", "127.0.0.1").toString();
+    port = settings.value("port", 1194).toInt();
+    compressed = settings.value("compressed", true).toBool();
+    useTLS = settings.value("useTLS", true).toBool();
+    tlsDirection = settings.value("tlsDirection", 1).toInt();
 }
 
 void VPNControl::initialise()
@@ -33,9 +43,12 @@ int VPNControl::getTlsDirection() const
 
 void VPNControl::setTlsDirection(int value)
 {
-    printf ("TLS direction set to %d\n", value);
-    tlsDirection = value;
-    emit tlsDirectionChanged (value);
+    if (value != tlsDirection) {
+        printf ("TLS direction set to %d\n", value);
+        tlsDirection = value;
+        settingsSetValue("tlsDirection", value);
+        emit tlsDirectionChanged (value);
+    }
 }
 
 bool VPNControl::getUseTLS() const
@@ -45,9 +58,12 @@ bool VPNControl::getUseTLS() const
 
 void VPNControl::setUseTLS(bool value)
 {
-    printf ("Use TLS set to %d\n", value);
-    useTLS = value;
-    emit useTLSChanged(useTLS);
+    if (value != useTLS) {
+        printf ("Use TLS set to %d\n", value);
+        useTLS = value;
+        settingsSetValue("useTLS", value);
+        emit useTLSChanged(useTLS);
+    }
 }
 
 bool VPNControl::getCompressed() const
@@ -57,9 +73,12 @@ bool VPNControl::getCompressed() const
 
 void VPNControl::setCompressed(bool value)
 {
-    printf ("Use compression set to %d\n", value);
-    compressed = value;
-    emit compressedChanged(compressed);
+    if (value != compressed) {
+        printf ("Use compression set to %d\n", value);
+        compressed = value;
+        settingsSetValue("compressed", value);
+        emit compressedChanged(compressed);
+    }
 }
 
 unsigned int VPNControl::getPort() const
@@ -69,9 +88,12 @@ unsigned int VPNControl::getPort() const
 
 void VPNControl::setPort(unsigned int value)
 {
-    printf ("Port set to %d\n", value);
-    port = value;
-    emit portChanged(port);
+    if (value != port) {
+        printf ("Port set to %d\n", value);
+        port = value;
+        settingsSetValue("port", value);
+        emit portChanged(port);
+    }
 }
 
 QString VPNControl::getServer() const
@@ -81,11 +103,25 @@ QString VPNControl::getServer() const
 
 void VPNControl::setServer(const QString &value)
 {
-    printf ("Server set to %s\n", value.toUtf8().constData());
-    server = value;
-    emit serverChanged(server);
+    if (value != server) {
+        printf ("Server set to %s\n", value.toUtf8().constData());
+        server = value;
+        settingsSetValue("server", value);
+        emit serverChanged(server);
+    }
 }
 
+void VPNControl::settingsSetValue (QString key, QString value) {
+    QSettings settings;
+
+    settings.setValue(key, value);
+}
+
+void VPNControl::settingsSetValue (QString key, int value) {
+    QSettings settings;
+
+    settings.setValue(key, value);
+}
 
 void VPNControl::vpnConnect() {
     if (vpnProcess != NULL) {
@@ -95,11 +131,8 @@ void VPNControl::vpnConnect() {
         printf ("Connect\n");
 
         vpnProcess = new QProcess();
-        //QString program = "/home/nemo/Documents/Development/Projects/Stooge/stooge";
         QString program = "openvpn";
-        QStringList arguments;
-        arguments << "/home/nemo/Documents/Configure/OpenVPN/client.ovpn";
-
+        collectArguments ();
         vpnProcess->setReadChannel(QProcess::StandardOutput);
         connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError)));
         connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData()));
@@ -109,9 +142,53 @@ void VPNControl::vpnConnect() {
         vpnProcess->start(program, arguments);
         vpnProcess->closeWriteChannel();
         setStatus(VPNSTATUS_INITIALISING);
+        arguments.clear();
     }
 }
 
+void VPNControl::collectArguments () {
+    arguments.clear();
+
+    addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
+    addArgument("remote", server);
+    addArgument("port", QString::number(port));
+    addOption("comp-lzo", compressed);
+    if (useTLS) {
+        addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
+        addValue(QString::number(tlsDirection));
+    }
+    addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt");
+    addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt");
+    addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key");
+}
+
+void VPNControl::addArgument (QString key, QString value) {
+    QString argument;
+
+    argument = "--" + key;
+    arguments.append(argument);
+    if (value != "") {
+        arguments.append(value);
+    }
+}
+
+void VPNControl::addArgument (QString key) {
+    QString argument;
+
+    argument = "--" + key;
+    arguments.append(argument);
+}
+
+void VPNControl::addOption (QString key, bool add) {
+    if (add) {
+        addArgument (key);
+    }
+}
+
+void VPNControl::addValue (QString key) {
+    arguments.append(key);
+}
+
 void VPNControl::vpnDisconnect() {
     if (vpnProcess != NULL) {
         printf ("Disconnect\n");