-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalconfig.cpp
More file actions
97 lines (85 loc) · 2.34 KB
/
localconfig.cpp
File metadata and controls
97 lines (85 loc) · 2.34 KB
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
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QStandardPaths>
#include <QDebug>
#include "localconfig.h"
#define __CONFIG_NAME__ "/.Dian-Captrue-config"
LocalConfig::LocalConfig()
: hotkey("F1"), copyWithMd(true), runWhenLogin(false), language("中文") {
loadConfig();
}
QString LocalConfig::configFilePath()
{
// 获取当前目录的路径
QString dirPath = QDir::currentPath();
return dirPath + __CONFIG_NAME__;
}
void LocalConfig::saveConfig()
{
QString path = configFilePath();
QFile configFile(path);
if (configFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&configFile);
out << "hotkey=" << hotkey << "\n";
out << "copyWithMd=" << (copyWithMd ? "true" : "false") << "\n";
out << "runWhenLogin=" << (runWhenLogin ? "true" : "false") << "\n";
out << "language=" << language << "\n";
configFile.close();
}
else
{
qDebug() << "写出配置失败";
}
}
void LocalConfig::loadConfig()
{
QString path = configFilePath();
QFile configFile(path);
if (!configFile.exists())
{
// 如果文件不存在,初始化为默认值
hotkey = "F1";
copyWithMd = true;
runWhenLogin = false;
saveConfig(); // 初始化时保存默认值
return;
}
if (configFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&configFile);
while (!in.atEnd())
{
QString line = in.readLine();
QStringList parts = line.split("=");
if (parts.size() == 2)
{
QString key = parts[0].trimmed();
QString value = parts[1].trimmed();
if (key == "hotkey")
{
hotkey = value;
}
else if (key == "copyWithMd")
{
copyWithMd = (value == "true");
}
else if (key == "runWhenLogin")
{
runWhenLogin = (value == "true");
}
else if (key == "language")
{
language = value;
}
}
}
configFile.close();
}
else
{
qDebug() << "读取配置失败";
}
}
LocalConfig localConfig;