-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigs.cpp
132 lines (101 loc) · 4.03 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
#include "configs.h"
#include <QSettings>
Configs::Configs()
{
// 强制使用 ini 格式的配置文件以避免弄乱系统底层数据(编辑注册表是非常危险的操作)
QSettings::setDefaultFormat(QSettings::IniFormat);
// 初始化一个 QSettings 对象用于读配置
QSettings settings;
// 初始化配置
logLevel = settings.value("LogLevel", ConfigLogLevelWarning).toInt(); // 回报 Warn 和 Critical 的日志
settings.beginGroup("Trace");
traceMaxHops = settings.value("MaxHops", 30).toInt(); // 最大发出 30 跳路由追踪
traceTimeout = settings.value("Timeout", 3000).toLongLong(); // 每个 ICMP 包的超时时间为 3000 毫秒
traceThreadInterval = settings.value("ThreadInterval", 100).toLongLong(); // 每后一个 ICMP 包的发送延迟为 100 毫秒
settings.endGroup();
settings.beginGroup("Auto");
autoOpenMap = settings.value("OpenMap", true).toBool(); // 解析或追踪时自动打开地图
autoStartTrace = settings.value("StartTrace", false).toBool(); // 解析完成后自动开始追踪
settings.endGroup();
settings.beginGroup("Resolve");
resolveDoubleClickAction = settings.value("DoubleClickAction", ConfigResolveDoubleClickActionStartTrace).toInt();
settings.endGroup();
}
void Configs::Save() {
// 保存配置
// 初始化一个 QSettings 对象用于写配置
QSettings settings;
// 转换不支持的类型
long long traceTimeoutLL = traceTimeout;
long long traceThreadIntervalLL = traceThreadInterval;
// 设置值
settings.setValue("LogLevel", logLevel);
settings.beginGroup("Trace");
settings.setValue("MaxHops", traceMaxHops );
settings.setValue("Timeout", traceTimeoutLL );
settings.setValue("ThreadInterval", traceThreadIntervalLL);
settings.endGroup();
settings.beginGroup("Auto");
settings.setValue("OpenMap", autoOpenMap );
settings.setValue("StartTrace", autoStartTrace);
settings.endGroup();
settings.beginGroup("Resolve");
settings.setValue("DoubleClickAction", resolveDoubleClickAction);
settings.endGroup();
}
int Configs::GetLogLevel() {
return logLevel;
}
void Configs::SetLogLevel(int newLogLevel) {
if (ConfigLogLevelDebug <= newLogLevel && newLogLevel <= ConfigLogLevelCritical) {
// 有效的
logLevel = newLogLevel;
} // 否则是无效的
}
int Configs::GetTraceMaxHops() {
return traceMaxHops;
}
void Configs::SetTraceMaxHops(int newTraceMaxHops) {
if (0 < newTraceMaxHops && newTraceMaxHops <= DEF_TRACE_MAX_HOPs) {
// 有效的
traceMaxHops = newTraceMaxHops;
} // 否则是无效的
}
unsigned long Configs::GetTraceTimeout() {
return traceTimeout;
}
void Configs::SetTraceTimeout(unsigned long newTraceTimeout) {
if (0 < newTraceTimeout && newTraceTimeout <= DEF_TRACE_TIMEOUT_MAX) {
traceTimeout = newTraceTimeout;
}
}
unsigned long Configs::GetTraceThreadInterval() {
return traceThreadInterval;
}
void Configs::SetTraceThreadInterval(unsigned long newTraceThreadInterval) {
if (0 < newTraceThreadInterval && newTraceThreadInterval <= DEF_TRACE_THREAD_INTERVAL_MAX) {
traceThreadInterval = newTraceThreadInterval;
}
}
bool Configs::GetAutoOpenMap() {
return autoOpenMap;
}
void Configs::SetAutoOpenMap(bool newAutoOpenMap) {
autoOpenMap = newAutoOpenMap;
}
bool Configs::GetAutoStartTrace() {
return autoStartTrace;
}
void Configs::SetAutoStartTrace(bool newAutoStartTrace) {
autoStartTrace = newAutoStartTrace;
}
int Configs::GetResolveDoubleClickAction() {
return resolveDoubleClickAction;
}
void Configs::SetResolveDoubleClickAction(int newResolveDoubleClickAction) {
if (ConfigResolveDoubleClickActionStartTrace <= resolveDoubleClickAction && resolveDoubleClickAction <= ConfigResolveDoubleClickActionOpenMap) {
// 有效的
resolveDoubleClickAction = newResolveDoubleClickAction;
} // 否则是无效的
}
Configs * gCfg;