Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / vpncontrol.cpp
1 #include "vpncontrol.h"
2 #include "stdio.h"
3 #include <QSettings>
4
5
6 VPNControl::VPNControl(QObject *parent) :
7 QObject(parent),
8 vpnProcess(NULL),
9 vpnStatus(VPNSTATUS_INVALID),
10 server(""),
11 port(1194),
12 compressed(true),
13 useTLS(true),
14 tlsDirection(1)
15 {
16 // Read in the settings
17 QSettings settings;
18
19 server = settings.value("server", "127.0.0.1").toString();
20 port = settings.value("port", 1194).toInt();
21 compressed = settings.value("compressed", true).toBool();
22 useTLS = settings.value("useTLS", true).toBool();
23 tlsDirection = settings.value("tlsDirection", 1).toInt();
24 settings.setValue("showAll", false);
25 }
26
27 void VPNControl::initialise()
28 {
29 setStatus(VPNSTATUS_UNINITIALISED);
30 }
31
32 void VPNControl::setStatus(VPNSTATUS newStatus)
33 {
34 if (vpnStatus != newStatus) {
35 vpnStatus = newStatus;
36 emit statusChanged(newStatus);
37 }
38 }
39 int VPNControl::getTlsDirection() const
40 {
41 return tlsDirection;
42 }
43
44 void VPNControl::setTlsDirection(int value)
45 {
46 if (value != tlsDirection) {
47 tlsDirection = value;
48 settingsSetValue("tlsDirection", value);
49 emit tlsDirectionChanged (value);
50 }
51 }
52
53 bool VPNControl::getUseTLS() const
54 {
55 return useTLS;
56 }
57
58 void VPNControl::setUseTLS(bool value)
59 {
60 if (value != useTLS) {
61 useTLS = value;
62 settingsSetValue("useTLS", value);
63 emit useTLSChanged(useTLS);
64 }
65 }
66
67 bool VPNControl::getCompressed() const
68 {
69 return compressed;
70 }
71
72 void VPNControl::setCompressed(bool value)
73 {
74 if (value != compressed) {
75 compressed = value;
76 settingsSetValue("compressed", value);
77 emit compressedChanged(compressed);
78 }
79 }
80
81 unsigned int VPNControl::getPort() const
82 {
83 return port;
84 }
85
86 void VPNControl::setPort(unsigned int value)
87 {
88 if (value != port) {
89 port = value;
90 settingsSetValue("port", value);
91 emit portChanged(port);
92 }
93 }
94
95 QString VPNControl::getServer() const
96 {
97 return server;
98 }
99
100 void VPNControl::setServer(const QString &value)
101 {
102 if (value != server) {
103 server = value;
104 settingsSetValue("server", value);
105 emit serverChanged(server);
106 }
107 }
108
109 QString VPNControl::getLogText() const
110 {
111 return logText;
112 }
113
114 void VPNControl::setLogText(const QString &value)
115 {
116 logText = value;
117 emit logTextChanged(value);
118 }
119
120 void VPNControl::settingsSetValue (QString key, QString value) {
121 QSettings settings;
122
123 settings.setValue(key, value);
124 }
125
126 void VPNControl::settingsSetValue (QString key, int value) {
127 QSettings settings;
128
129 settings.setValue(key, value);
130 }
131
132 void VPNControl::vpnConnect() {
133 if (vpnProcess != NULL) {
134 printf ("Process already running.\n");
135 }
136 else {
137 vpnProcess = new QProcess();
138 QString program = "openvpn";
139 collectArguments ();
140 vpnProcess->setReadChannel(QProcess::StandardOutput);
141 connect(vpnProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(readError(QProcess::ProcessError)));
142 connect(vpnProcess, SIGNAL(readyRead()), this, SLOT(readData()));
143 connect(vpnProcess, SIGNAL(started()), this, SLOT(started()));
144 connect(vpnProcess, SIGNAL(finished(int)), this, SLOT(finished(int)));
145
146 vpnProcess->start(program, arguments);
147 vpnProcess->closeWriteChannel();
148 setStatus(VPNSTATUS_INITIALISING);
149 arguments.clear();
150 }
151 }
152
153 void VPNControl::collectArguments () {
154 arguments.clear();
155
156 addArgument("config", "/home/nemo/Documents/Configure/OpenVPN/config.ovpn");
157 addArgument("remote", server);
158 addArgument("port", QString::number(port));
159 addOption("comp-lzo", compressed);
160 if (useTLS) {
161 addArgument("tls-auth", "/home/nemo/Documents/Configure/OpenVPN/ta.key");
162 addValue(QString::number(tlsDirection));
163 }
164 addArgument("ca", "/home/nemo/Documents/Configure/OpenVPN/ca.crt");
165 addArgument("cert", "/home/nemo/Documents/Configure/OpenVPN/Jolla.crt");
166 addArgument("key", "/home/nemo/Documents/Configure/OpenVPN/Jolla.key");
167 }
168
169 void VPNControl::addArgument (QString key, QString value) {
170 QString argument;
171
172 argument = "--" + key;
173 arguments.append(argument);
174 if (value != "") {
175 arguments.append(value);
176 }
177 }
178
179 void VPNControl::addArgument (QString key) {
180 QString argument;
181
182 argument = "--" + key;
183 arguments.append(argument);
184 }
185
186 void VPNControl::addOption (QString key, bool add) {
187 if (add) {
188 addArgument (key);
189 }
190 }
191
192 void VPNControl::addValue (QString key) {
193 arguments.append(key);
194 }
195
196 void VPNControl::vpnDisconnect() {
197 if (vpnProcess != NULL) {
198
199 vpnProcess->terminate();
200 setStatus(VPNSTATUS_DISCONNECTING);
201 }
202 }
203
204 void VPNControl::readData() {
205 while (vpnProcess->canReadLine()) {
206 QByteArray read = vpnProcess->readLine();
207 //printf ("Output: %s", read.data());
208
209 logAppend(read);
210
211 if (read.endsWith("Initialization Sequence Completed\n")) {
212 setStatus(VPNSTATUS_CONNECTED);
213 }
214 }
215 }
216
217 void VPNControl::started() {
218 setStatus(VPNSTATUS_CONNECTING);
219 }
220
221 void VPNControl::finished(int code) {
222 if (vpnProcess != NULL) {
223 //delete vpnProcess;
224 vpnProcess = NULL;
225 }
226 setStatus(VPNSTATUS_UNINITIALISED);
227 }
228
229 void VPNControl::readError(QProcess::ProcessError error)
230 {
231 printf ("Error: %d\n", error);
232 if (vpnProcess != NULL) {
233 QByteArray dataOut = vpnProcess->readAllStandardOutput();
234 QByteArray errorOut = vpnProcess->readAllStandardError();
235
236 printf ("Output text: %s\n", dataOut.data());
237 printf ("Error text: %s\n", errorOut.data());
238 }
239
240 // Disconnect
241 vpnDisconnect();
242 }
243
244 void VPNControl::updateConfiguration()
245 {
246 printf ("Update configuration\n");
247 }
248
249 void VPNControl::logAppend(const QString &text)
250 {
251 if (!text.isEmpty()) {
252 // How many lines to add
253 int newLines = text.count('\n');
254 int currentLines = logText.count('\n');
255 int removeLines = currentLines + newLines - 18;
256
257 // Remove excess lines
258 while (removeLines > 0) {
259 int nextLine = logText.lastIndexOf('\n');
260 if (nextLine > 0) {
261 logText = logText.left(nextLine);
262 }
263 removeLines--;
264 }
265
266 // Add new lines
267 logText.prepend(text);
268 emit logTextChanged(logText);
269 }
270 }