-
Notifications
You must be signed in to change notification settings - Fork 0
/
useriptable.cpp
63 lines (57 loc) · 1.57 KB
/
useriptable.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
#include <QMap>
#include <QFile>
#include <QList>
#include <QString>
#include <QStringList>
#include <QTextStream>
#include "useriptable.h"
UserIPTable::UserIPTable(QString filename) {
this->filename = filename;
QFile infile(filename);
if(infile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&infile);
while(!in.atEnd()) {
QString line = in.readLine();
QStringList args = line.split("\t");
nameTable[args.takeFirst()] = args;
}
infile.close();
}
}
QString UserIPTable::getRealName(QString IPHash, QString name) {
if(!nameTable.contains(IPHash)) {
QStringList newlist;
newlist.append(name);
nameTable[IPHash] = newlist;
writeTable();
}
if(!nameTable[IPHash].contains(name)) {
nameTable[IPHash].append(name);
writeTable();
}
return nameTable[IPHash][0];
}
QStringList UserIPTable::getAllNames(QString IPHash) {
return nameTable[IPHash];
}
void UserIPTable::setRealName(QString IPHash, QString name) {
if(name != getRealName(IPHash, name)) {
nameTable[IPHash].removeAll(name);
nameTable[IPHash].insert(0, name);
writeTable();
}
}
void UserIPTable::writeTable() {
QFile outfile(filename);
outfile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&outfile);
foreach(QString key, nameTable.keys()) {
out << key;
foreach(QString name, nameTable[key]) {
out << "\t";
out << name;
}
out << "\n";
}
outfile.close();
}