forked from jrywu/NTUMEMSOpenVPN
/
configs.cpp
223 lines (193 loc) · 6.74 KB
/
configs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "configs.h"
#include "appfunc.h"
Configs *Configs::mInst = NULL;
Configs *Configs::getInstance()
{
if (!mInst) {
mInst = new Configs ();
}
return mInst;
}
Configs::Configs ()
: refreshConnectionPath (""),
id (1)
{
}
QList<QPair<int, OpenVpn*> > Configs::getConfigsObjects ()
{
return this->myList;
}
void Configs::clearConfigs()
{
this->refreshConnectionPath.clear();
// OD: Mutable List statt foreach
QMutableListIterator<QPair<int, OpenVpn*> > i(this->myList);
while (i.hasNext()) {
QPair <int, OpenVpn*> entry = i.next();
OpenVpn *obj = entry.second;
if (!obj->isConnectionStable() && !obj->isConnecting() && !obj->isError() && obj->getAdvName().isEmpty() && !obj->isDelayed()) {
// Objekt löschen
delete obj;
obj = 0;
// Nun aus der Liste entfernen
i.remove();
} else {
// Pfad merken
this->refreshConnectionPath = obj->getConfigFullPath();
}
}
}
bool Configs::isConfigInList(const QString &path)
{
//
// Ist die Konfiguration bereits in der Liste
//
if (path.isEmpty()) {
return false;
}
QListIterator<QPair<int, OpenVpn*> > i(this->myList);
while (i.hasNext()) {
QPair <int, OpenVpn*> entry = i.next();
OpenVpn *obj = entry.second;
// Den Pfad kontrollieren
if (path == obj->getConfigFullPath()) {
return true;
}
}
return false;
}
void Configs::searchLinkedConfigs()
{
QFile linkedOvpn (AppFunc::getAppSavePath() + QLatin1String ("/configs.txt"));
if (linkedOvpn.exists()) {
if (!linkedOvpn.open(QIODevice::ReadOnly | QIODevice::Text)) {
return;
}
// Offen einlesen
QTextStream in (&linkedOvpn);
while (!in.atEnd()) {
QString line = in.readLine();
if (!this->isConfigInList(line)) {
// Add to list
QFile linkFileExists (line.trimmed());
if (linkFileExists.exists()) {
int _id (this->id++);
QString configName = line.trimmed().right(line.trimmed().size() - line.trimmed().lastIndexOf("/") - 1);
configName = configName.left(configName.size()-5);
OpenVpn* myObj = new OpenVpn();
myObj->setConfigStable(false);
myObj->setConfigLinked(true);
myObj->setConfigName(configName);
myObj->setConfigPath(line.trimmed().left(line.lastIndexOf("/")));
myObj->setId(_id);
// An die Liste anfügen
this->myList.append(qMakePair(_id, myObj));
}
}
}
linkedOvpn.close();
}
}
void Configs::changeConfigNameInLinkedList (const QString &oldName, const QString &newName)
{
if (this->isConfigInList(newName)) {
// ist bereits drin
return;
}
this->removeConfigFromList(oldName);
QFile linkedWriteOvpn (AppFunc::getAppSavePath() + QLatin1String ("/configs.txt"));
if (!linkedWriteOvpn.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) {
return;
}
QTextStream out (&linkedWriteOvpn);
out << newName + QLatin1String ("\n");
linkedWriteOvpn.close();
}
bool Configs::isConfigLinked(const QString &config)
{
bool retVal = false;
QFile linkedOvpn (AppFunc::getAppSavePath() + QLatin1String ("/configs.txt"));
if (linkedOvpn.exists()) {
if (!linkedOvpn.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
// Offen einlesen
QTextStream in (&linkedOvpn);
while (!in.atEnd()) {
QString line = in.readLine();
if (line.trimmed() == config) {
retVal = true;
break;
}
}
linkedOvpn.close();
}
return retVal;
}
void Configs::removeConfigFromList(const QString &config)
{
QString configClean (config);
configClean = configClean.replace("\\", "/");
QFile linkedOvpn (AppFunc::getAppSavePath() + QLatin1String ("/configs.txt"));
if (linkedOvpn.exists()) {
if (!linkedOvpn.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// Offen einlesen
QStringList fileContent;
QTextStream in (&linkedOvpn);
while (!in.atEnd()) {
QString line = in.readLine();
if (line.trimmed() != configClean) {
fileContent.append(line.trimmed());
}
}
linkedOvpn.close();
// Datei schreiben
QFile linkedWriteOvpn (AppFunc::getAppSavePath() + QLatin1String ("/configs.txt"));
if (!linkedWriteOvpn.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
return;
QTextStream out (&linkedWriteOvpn);
foreach (QString str, fileContent)
out << str + QLatin1String ("\n");
linkedWriteOvpn.close();
}
}
void Configs::searchConfigs (QString sDir)
{
// Dateien im aktuellen Verzeichnis suchen
QDir dir (sDir);
//qDebug() << "searchConfigs() sDir =" << sDir;
dir.setFilter(QDir::Files | QDir::AllDirs | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
//qDebug() << "searchConfigs() fileInfo.filename =" << fileInfo.fileName();
if (fileInfo.isDir()) {
// Verzeichnis auf Configs durchsuchen
if (fileInfo.fileName() != "." && fileInfo.fileName() != QLatin1String("..")) {
this->searchConfigs(sDir + QLatin1String ("/") + fileInfo.fileName());
}
} else {
// Ist eine Datei, auch eine VPN Config?
if (fileInfo.suffix() == QLatin1String("ovpn")){
if (!this->isConfigInList(sDir.replace("\\", "/") + QLatin1String ("/") + fileInfo.fileName())) {
int _id (this->id++);
//qDebug("searchConfigs() OVPN config #%d",i);
//qDebug() << " found:" << fileInfo.fileName();
OpenVpn* myObj = new OpenVpn();
myObj->setConfigStable(false);
myObj->setConfigLinked(false);
myObj->setConfigName(fileInfo.fileName().replace(QLatin1String(".ovpn"), QLatin1String("")));
myObj->setConfigPath(sDir);
myObj->setId(_id);
this->myList.append(qMakePair(_id, myObj));
}
}
}
}
}
void Configs::appendConfigToList(OpenVpn *obj)
{
int _id (this->id++);
obj->setId(_id);
this->myList.append(qMakePair(_id, obj));
}