Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / vpncontrol.cpp
index 7bd002f..057cb36 100644 (file)
@@ -21,6 +21,7 @@ VPNControl::VPNControl(QObject *parent) :
     compressed = settings.value("compressed", true).toBool();
     useTLS = settings.value("useTLS", true).toBool();
     tlsDirection = settings.value("tlsDirection", 1).toInt();
+    settings.setValue("showAll", false);
 }
 
 void VPNControl::initialise()
@@ -33,7 +34,6 @@ void VPNControl::setStatus(VPNSTATUS newStatus)
     if (vpnStatus != newStatus) {
         vpnStatus = newStatus;
         emit statusChanged(newStatus);
-        printf ("Emitting status %d\n", newStatus);
     }
 }
 int VPNControl::getTlsDirection() const
@@ -44,7 +44,6 @@ int VPNControl::getTlsDirection() const
 void VPNControl::setTlsDirection(int value)
 {
     if (value != tlsDirection) {
-        printf ("TLS direction set to %d\n", value);
         tlsDirection = value;
         settingsSetValue("tlsDirection", value);
         emit tlsDirectionChanged (value);
@@ -59,7 +58,6 @@ bool VPNControl::getUseTLS() const
 void VPNControl::setUseTLS(bool value)
 {
     if (value != useTLS) {
-        printf ("Use TLS set to %d\n", value);
         useTLS = value;
         settingsSetValue("useTLS", value);
         emit useTLSChanged(useTLS);
@@ -74,7 +72,6 @@ bool VPNControl::getCompressed() const
 void VPNControl::setCompressed(bool value)
 {
     if (value != compressed) {
-        printf ("Use compression set to %d\n", value);
         compressed = value;
         settingsSetValue("compressed", value);
         emit compressedChanged(compressed);
@@ -89,7 +86,6 @@ unsigned int VPNControl::getPort() const
 void VPNControl::setPort(unsigned int value)
 {
     if (value != port) {
-        printf ("Port set to %d\n", value);
         port = value;
         settingsSetValue("port", value);
         emit portChanged(port);
@@ -104,13 +100,23 @@ QString VPNControl::getServer() const
 void VPNControl::setServer(const QString &value)
 {
     if (value != server) {
-        printf ("Server set to %s\n", value.toUtf8().constData());
         server = value;
         settingsSetValue("server", value);
         emit serverChanged(server);
     }
 }
 
+QString VPNControl::getLogText() const
+{
+    return logText;
+}
+
+void VPNControl::setLogText(const QString &value)
+{
+    logText = value;
+    emit logTextChanged(value);
+}
+
 void VPNControl::settingsSetValue (QString key, QString value) {
     QSettings settings;
 
@@ -128,8 +134,6 @@ void VPNControl::vpnConnect() {
         printf ("Process already running.\n");
     }
     else {
-        printf ("Connect\n");
-
         vpnProcess = new QProcess();
         QString program = "openvpn";
         collectArguments ();
@@ -191,7 +195,6 @@ void VPNControl::addValue (QString key) {
 
 void VPNControl::vpnDisconnect() {
     if (vpnProcess != NULL) {
-        printf ("Disconnect\n");
 
         vpnProcess->terminate();
         setStatus(VPNSTATUS_DISCONNECTING);
@@ -201,21 +204,21 @@ void VPNControl::vpnDisconnect() {
 void VPNControl::readData() {
     while (vpnProcess->canReadLine()) {
         QByteArray read = vpnProcess->readLine();
-        printf ("Output: %s", read.data());
+        //printf ("Output: %s", read.data());
+
+        logAppend(read);
+
         if (read.endsWith("Initialization Sequence Completed\n")) {
-            printf ("We're connected!\n");
             setStatus(VPNSTATUS_CONNECTED);
         }
     }
 }
 
 void VPNControl::started() {
-    printf ("Started\n");
     setStatus(VPNSTATUS_CONNECTING);
 }
 
 void VPNControl::finished(int code) {
-    printf ("Finished with code %d\n", code);
     if (vpnProcess != NULL) {
         //delete vpnProcess;
         vpnProcess = NULL;
@@ -242,3 +245,26 @@ void VPNControl::updateConfiguration()
 {
     printf ("Update configuration\n");
 }
+
+void VPNControl::logAppend(const QString &text)
+{
+    if (!text.isEmpty()) {
+        // How many lines to add
+        int newLines = text.count('\n');
+        int currentLines = logText.count('\n');
+        int removeLines = currentLines + newLines - 18;
+
+        // Remove excess lines
+        while (removeLines > 0) {
+            int nextLine = logText.lastIndexOf('\n');
+            if (nextLine > 0) {
+                logText = logText.left(nextLine);
+            }
+            removeLines--;
+        }
+
+        // Add new lines
+        logText.prepend(text);
+        emit logTextChanged(logText);
+    }
+}