Linked key and ca file configuration to vpn execution. Added option to
[openvpnui.git] / src / vpncontrol.cpp
index 057cb36..3588f7b 100644 (file)
@@ -21,6 +21,11 @@ VPNControl::VPNControl(QObject *parent) :
     compressed = settings.value("compressed", true).toBool();
     useTLS = settings.value("useTLS", true).toBool();
     tlsDirection = settings.value("tlsDirection", 1).toInt();
+    caCertFile = settings.value("caCertFile", "").toString();
+    clientCertFile = settings.value("clientCertFile", "").toString();;
+    clientKeyFile = settings.value("clientKeyFile", "").toString();;
+    tlsKeyFile = settings.value("tlsKeyFile", "").toString();;
+
     settings.setValue("showAll", false);
 }
 
@@ -106,6 +111,62 @@ void VPNControl::setServer(const QString &value)
     }
 }
 
+QString VPNControl::getCaCertFile () const
+{
+    return caCertFile;
+}
+
+void VPNControl::setCaCertFile(const QString &value)
+{
+    if (value != caCertFile) {
+        caCertFile = value;
+        settingsSetValue("caCertFile", value);
+        emit caCertFileChanged(caCertFile);
+    }
+}
+
+void VPNControl::setClientCertFile(const QString &value)
+{
+    if (value != clientCertFile) {
+        clientCertFile = value;
+        settingsSetValue("clientCertFile", value);
+        emit clientCertFileChanged(clientCertFile);
+    }
+}
+
+void VPNControl::setClientKeyFile(const QString &value)
+{
+    if (value != clientKeyFile) {
+        clientKeyFile = value;
+        settingsSetValue("clientKeyFile", value);
+        emit clientKeyFileChanged(clientKeyFile);
+    }
+}
+
+void VPNControl::setTlsKeyFile(const QString &value)
+{
+    if (value != tlsKeyFile) {
+        tlsKeyFile = value;
+        settingsSetValue("tlsKeyFile", value);
+        emit tlsKeyFileChanged(tlsKeyFile);
+    }
+}
+
+QString VPNControl::getClientCertFile () const
+{
+    return clientCertFile;
+}
+
+QString VPNControl::getClientKeyFile () const
+{
+    return clientKeyFile;
+}
+
+QString VPNControl::getTlsKeyFile () const
+{
+    return tlsKeyFile;
+}
+
 QString VPNControl::getLogText() const
 {
     return logText;
@@ -114,7 +175,7 @@ QString VPNControl::getLogText() const
 void VPNControl::setLogText(const QString &value)
 {
     logText = value;
-    emit logTextChanged(value);
+    emit logTextChanged(logText);
 }
 
 void VPNControl::settingsSetValue (QString key, QString value) {
@@ -153,17 +214,17 @@ void VPNControl::vpnConnect() {
 void VPNControl::collectArguments () {
     arguments.clear();
 
-    addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
-    addArgument("remote", server);
-    addArgument("port", QString::number(port));
+    addArgumentNonempty("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
+    addArgumentNonempty("remote", server);
+    addArgumentNonempty("port", QString::number(port));
     addOption("comp-lzo", compressed);
-    if (useTLS) {
-        addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
+    if ((useTLS) && (!tlsKeyFile.isEmpty())) {
+        addArgument("tls-auth", tlsKeyFile);
         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");
+    addArgumentNonempty("ca", caCertFile);
+    addArgumentNonempty("cert", clientCertFile);
+    addArgumentNonempty("key", clientKeyFile);
 }
 
 void VPNControl::addArgument (QString key, QString value) {
@@ -171,7 +232,17 @@ void VPNControl::addArgument (QString key, QString value) {
 
     argument = "--" + key;
     arguments.append(argument);
-    if (value != "") {
+    if (!value.isEmpty()) {
+        arguments.append(value);
+    }
+}
+
+void VPNControl::addArgumentNonempty (QString key, QString value) {
+    QString argument;
+
+    if (!value.isEmpty()) {
+        argument = "--" + key;
+        arguments.append(argument);
         arguments.append(value);
     }
 }
@@ -255,16 +326,17 @@ void VPNControl::logAppend(const QString &text)
         int removeLines = currentLines + newLines - 18;
 
         // Remove excess lines
-        while (removeLines > 0) {
-            int nextLine = logText.lastIndexOf('\n');
-            if (nextLine > 0) {
-                logText = logText.left(nextLine);
-            }
-            removeLines--;
-        }
+//        while (removeLines > 0) {
+//            int nextLine = logText.indexOf('\n');
+//            if (nextLine > 0) {
+//                logText = logText.right(nextLine);
+//            }
+//            removeLines--;
+//        }
 
         // Add new lines
-        logText.prepend(text);
+        logText.append(text);
         emit logTextChanged(logText);
     }
 }
+