Configuration values are passed between the interface and main code.
[openvpnui.git] / src / vpncontrol.cpp
1 #include "vpncontrol.h"
2 #include "stdio.h"
3
4 VPNControl::VPNControl(QObject *parent) :
5 QObject(parent),
6 vpnProcess(NULL),
7 vpnStatus(VPNSTATUS_INVALID),
8 server(""),
9 port(1174),
10 compressed(true),
11 useTLS(true),
12 tlsDirection(1)
13 {
14 }
15
16 void VPNControl::initialise()
17 {
18 setStatus(VPNSTATUS_UNINITIALISED);
19 }
20
21 void VPNControl::setStatus(VPNSTATUS newStatus)
22 {
23 if (vpnStatus != newStatus) {
24 vpnStatus = newStatus;
25 emit statusChanged(newStatus);
26 printf ("Emitting status %d\n", newStatus);
27 }
28 }
29 int VPNControl::getTlsDirection() const
30 {
31 return tlsDirection;
32 }
33
34 void VPNControl::setTlsDirection(int value)
35 {
36 printf ("TLS direction set to %d\n", value);
37 tlsDirection = value;
38 emit tlsDirectionChanged (value);
39 }
40
41 bool VPNControl::getUseTLS() const
42 {
43 return useTLS;
44 }
45
46 void VPNControl::setUseTLS(bool value)
47 {
48 printf ("Use TLS set to %d\n", value);
49 useTLS = value;
50 emit useTLSChanged(useTLS);
51 }
52
53 bool VPNControl::getCompressed() const
54 {
55 return compressed;
56 }
57
58 void VPNControl::setCompressed(bool value)
59 {
60 printf ("Use compression set to %d\n", value);
61 compressed = value;
62 emit compressedChanged(compressed);
63 }
64
65 unsigned int VPNControl::getPort() const
66 {
67 return port;
68 }
69
70 void VPNControl::setPort(unsigned int value)
71 {
72 printf ("Port set to %d\n", value);
73 port = value;
74 emit portChanged(port);
75 }
76
77 QString VPNControl::getServer() const
78 {
79 return server;
80 }
81
82 void VPNControl::setServer(const QString &value)
83 {
84 printf ("Server set to %s\n", value.toUtf8().constData());
85 server = value;
86 emit serverChanged(server);
87 }
88
89
90 void VPNControl::vpnConnect() {
91 if (vpnProcess != NULL) {
92 printf ("Process already running.\n");
93 }
94 else {
95 printf ("Connect\n");
96
97 vpnProcess = new QProcess();
98 //QString program = "/home/nemo/Documents/Development/Projects/Stooge/stooge";
99 QString program = "openvpn";
100 QStringList arguments;
101 arguments << "/home/nemo/Documents/Configure/OpenVPN/client.ovpn";
102
103 vpnProcess->setReadChannel(QProcess::StandardOutput);
104 connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError)));
105 connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData()));
106 connect(vpnProcess, SIGNAL(started()), this, SLOT(started()));
107 connect(vpnProcess, SIGNAL(finished(int)), this, SLOT(finished(int)));
108
109 vpnProcess->start(program, arguments);
110 vpnProcess->closeWriteChannel();
111 setStatus(VPNSTATUS_INITIALISING);
112 }
113 }
114
115 void VPNControl::vpnDisconnect() {
116 if (vpnProcess != NULL) {
117 printf ("Disconnect\n");
118
119 vpnProcess->terminate();
120 setStatus(VPNSTATUS_DISCONNECTING);
121 }
122 }
123
124 void VPNControl::readData() {
125 while (vpnProcess->canReadLine()) {
126 QByteArray read = vpnProcess->readLine();
127 printf ("Output: %s", read.data());
128 if (read.endsWith("Initialization Sequence Completed\n")) {
129 printf ("We're connected!\n");
130 setStatus(VPNSTATUS_CONNECTED);
131 }
132 }
133 }
134
135 void VPNControl::started() {
136 printf ("Started\n");
137 setStatus(VPNSTATUS_CONNECTING);
138 }
139
140 void VPNControl::finished(int code) {
141 printf ("Finished with code %d\n", code);
142 if (vpnProcess != NULL) {
143 //delete vpnProcess;
144 vpnProcess = NULL;
145 }
146 setStatus(VPNSTATUS_UNINITIALISED);
147 }
148
149 void VPNControl::readError(QProcess::ProcessError error)
150 {
151 printf ("Error: %d\n", error);
152 if (vpnProcess != NULL) {
153 QByteArray dataOut = vpnProcess->readAllStandardOutput();
154 QByteArray errorOut = vpnProcess->readAllStandardError();
155
156 printf ("Output text: %s\n", dataOut.data());
157 printf ("Error text: %s\n", errorOut.data());
158 }
159
160 // Disconnect
161 vpnDisconnect();
162 }
163
164 void VPNControl::updateConfiguration()
165 {
166 printf ("Update configuration\n");
167 }