-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjbpreferences.cpp
122 lines (104 loc) · 3.64 KB
/
jbpreferences.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
//---------------------------------------------------------------------------------
// App preferences (w) 2025 Jan Buchholz
//---------------------------------------------------------------------------------
#include "jbpreferences.h"
#include <QJsonDocument>
#include <QJsonParseError>
#include <QByteArray>
#include <QString>
#include <QFont>
#include <QIODevice>
#include <QDir>
#include <QStandardPaths>
#include <QApplication>
JBPreferences::JBPreferences(QObject *parent) : QObject{parent} {
prefs.clear();
}
JBPreferences::~JBPreferences() {}
QString JBPreferences::GetPreferencesDefaultLocation() {
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
}
void JBPreferences::PushArray(QString key, QByteArray values) {
prefs.insert(key, QString(values.toBase64()));
}
void JBPreferences::PushString(QString key, QString value) {
prefs.insert(key, QString(value.toUtf8().toBase64()));
}
void JBPreferences::PushNumber(QString key, quint64 value) {
PushString(key, QString::number(value));
}
void JBPreferences::PushBoolean(QString key, bool value) {
PushString(key, (value) ? "1" : "0");
}
void JBPreferences::PushFont(QString key, QFont font) {
PushString(key, font.toString());
}
QByteArray JBPreferences::PopArray(QString key) {
QByteArray ba = {};
QString tmp = prefs.find(key).value().toString();
if (!tmp.isEmpty()) {
ba = QByteArray::fromBase64(tmp.toUtf8());
}
return ba;
}
QString JBPreferences::PopString(QString key) {
return QString(PopArray(key));
}
quint64 JBPreferences::PopNumber(QString key) {
return PopString(key).toULongLong();
}
bool JBPreferences::PopBoolean(QString key) {
return PopString(key) == "1";
}
QFont JBPreferences::PopFont(QString key) {
QFont font;
QString tmp = PopString(key);
if (!tmp.isEmpty()) font.fromString(tmp); else font = QApplication::font();
return font;
}
bool JBPreferences::SavePreferences(QString filePath, QString orgName, QString appName) {
bool b = false;
QJsonObject jprefs = QJsonObject::fromVariantMap(prefs);
QJsonDocument doc(jprefs);
if (!QDir(filePath).exists()) QDir().mkpath(filePath);
if (!QDir(filePath).exists()) return false;
QFile file(filePath + "/" + orgName + "." + appName);
b = file.open(QIODevice::WriteOnly);
if (b) {
QTextStream out(&file);
out << doc.toJson() << Qt::endl;
file.close();
}
return b;
}
bool JBPreferences::LoadPreferences(QString filePath, QString orgName, QString appName) {
prefs.clear();
QJsonObject jprefs;
QJsonDocument doc;
QJsonParseError err;
QString data = "";
QFile file(filePath + "/" + orgName + "." + appName);
if (!file.exists()) return false;
bool b = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (b) {
QTextStream in(&file);
while (!in.atEnd()) {
data += in.readLine();
}
file.close();
doc = QJsonDocument::fromJson(data.toLocal8Bit(), &err);
if (err.error != QJsonParseError::NoError) return false;
b = !doc.isNull();
if (b) {
jprefs = doc.object();
prefs = jprefs.toVariantMap();
}
}
return b;
}
bool JBPreferences::SavePreferencesToDefaultLocation(QString orgName, QString appName) {
return SavePreferences(GetPreferencesDefaultLocation(), orgName, appName);
}
bool JBPreferences::LoadPreferencesFromDefaultLocation(QString orgName, QString appName) {
return LoadPreferences(GetPreferencesDefaultLocation(), orgName, appName);
}