-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopMostRecord.cpp
More file actions
122 lines (100 loc) · 2.49 KB
/
TopMostRecord.cpp
File metadata and controls
122 lines (100 loc) · 2.49 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
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
#include "TopMostRecord.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "LogFile.h"
TopMostRecord::TopMostRecord()
{
}
bool TopMostRecord::load()
{
QFile file("Settings\\TopMostRecord.json");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
QString value = file.readAll();
file.close();
QJsonParseError parseJsonErr;
QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr);
if (parseJsonErr.error != QJsonParseError::NoError)
{
qLog(QString("Parse Settings.json failed, error : %1").arg(parseJsonErr.errorString()));
return false;
}
QJsonObject jsonObject = document.object();
QJsonArray pluginArray = jsonObject["Records"].toArray();
for (int i = 0; i < pluginArray.size(); i++)
{
QJsonObject oRecord = pluginArray[i].toObject();
QString query = oRecord.keys()[0];
QString path = oRecord[query].toString();
m_records[query] = path;
}
return true;
}
bool TopMostRecord::save()
{
QJsonDocument document;
QJsonObject oRoot;
QJsonArray recordArray;
QMapIterator<QString,QString> mapIter(m_records);
while (mapIter.hasNext())
{
mapIter.next();
QJsonObject record;
record[mapIter.key()] = mapIter.value();
recordArray.append(record);
}
oRoot["Records"] = recordArray;
document.setObject(oRoot);
QByteArray byte_array = document.toJson(QJsonDocument::Indented);
QFile file("Settings\\TopMostRecord.json");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
return false;
}
file.write(byte_array);
file.flush();
file.close();
return true;
}
bool TopMostRecord::isTopMost(Result &result)
{
QString path = m_records[result.extraData];
if (result.action.funcName == "Ra_ActivePlugin")
{
if (path != result.title)
{
return false;
}
}
else
{
if (path != result.subTitle)
{
return false;
}
}
return true;
}
void TopMostRecord::addOrUpdate(QString& query,QString& path)
{
m_records[query] = path;
}
void TopMostRecord::remove(QString& query)
{
m_records.remove(query);
}
TopMostRecord* GetTopMostRecord()
{
static TopMostRecord setting;
static bool s_initialed = false;
if (!s_initialed)
{
s_initialed = true;
setting.load();
}
return &setting;
}