-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUserMgr.cpp
142 lines (125 loc) · 3.28 KB
/
UserMgr.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
#include "UserMgr.h"
#include "QSqlQuery"
#include "QFile"
#include "QFileInfo"
UserMgr::UserMgr(const QString &dbpath, const QString &connName, const QString &path)
: MySQLite(dbpath, connName)
{
bool isExists = QFile::exists(path);
cfg = new QSettings(path, QSettings::IniFormat);
cfg->setIniCodec("GB18030");
if (!isExists)
{
QFileInfo fd(path);
QString configName = fd.baseName();
if(configName == "Config")
{
initMainConfig();
}
}
}
UserMgr::~UserMgr()
{
delete cfg;
cfg = NULL;
}
bool UserMgr::exists(const QString &userName)
{
QSqlQuery query(QString("select id from UserInfo where userName = '%1'").arg(userName), *db);
return query.next();
}
QVariant UserMgr::getUserInfo(const QString &itemName, const QString &userName)
{
if (exists(userName))
{
QSqlQuery query(QString("select * from UserInfo where userName = '%1'").arg(userName), *db);
query.next();
return query.record().value(itemName);
}
else
{
return NULL;
}
}
QStringList UserMgr::getUserList()
{
QStringList orderResult;
QSqlQuery query("select userName from UserInfo order by lastLoginTime desc", *db);
while (query.next())
{
orderResult << query.record().value("userName").toString();
}
return orderResult;
}
QStringList UserMgr::getUserListOrderById()
{
QStringList orderResult;
QSqlQuery query("select userName from UserInfo order by id desc", *db);
while (query.next())
{
orderResult << query.record().value("userName").toString();
}
return orderResult;
}
void UserMgr::initMainConfig()
{
LISTMAPINFO LMI_DefaultUser;
QMap<QString, QVariant> DefaultUserConfig;
DefaultUserConfig["DefaultUser"] = "default";
DefaultUserConfig["AutoSelect"] = false;
LMI_DefaultUser << DefaultUserConfig;
setLMI("DefaultUserConfig", LMI_DefaultUser);
}
QSettings *UserMgr::getCfg()
{
return cfg;
}
QString UserMgr::getDefaultUser()
{
return cfg->value("DefaultUserConfig/DefaultUser", "default").toString();
}
void UserMgr::setDefaultUser(const QString &userName)
{
cfg->setValue("DefaultUserConfig/DefaultUser", userName);
}
bool UserMgr::getAutoSelect()
{
return cfg->value("DefaultUserConfig/AutoSelect", true).toBool();
}
void UserMgr::setAutoSelect(bool bAuto)
{
cfg->setValue("DefaultUserConfig/AutoSelect", bAuto);
}
LISTMAPINFO UserMgr::getLMI(const QString &prefix)
{
LISTMAPINFO result;
int size = cfg->beginReadArray(prefix);
for(int i = 0; i < size; i++)
{
cfg->setArrayIndex(i);
QStringList keys = cfg->childKeys();
QMap<QString, QVariant> learnWordConfig;
for(int j = 0; j < keys.size(); j++)
{
learnWordConfig[keys[j]] = cfg->value(keys[j]).toString();
}
result << learnWordConfig;
}
cfg->endArray();
return result;
}
void UserMgr::setLMI(const QString &prefix, LISTMAPINFO &lmi)
{
cfg->beginWriteArray(prefix);
for(int i = 0; i < lmi.size(); i++)
{
// cfg->setArrayIndex(i);
QMapIterator<QString, QVariant> iter(lmi.at(i));
while (iter.hasNext())
{
iter.next();
cfg->setValue(iter.key(), iter.value());
}
}
cfg->endArray();
}