forked from RussianMiningCoin/RMCWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iniworker.cpp
79 lines (74 loc) · 2.34 KB
/
iniworker.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
#include "iniworker.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QStandardPaths>
#include <QDir>
#include <QNetworkProxy>
CIniworker::CIniworker()
{
iniFile.setFileName(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + QDir::separator() + "STMSettings.json");
iniFile.open(QIODevice::ReadWrite | QIODevice::Text);
if (iniFile.size() == 0)
{
Write(CIniworker::W_DUMMY);
}
}
CIniworker::~CIniworker()
{
iniFile.close();
}
void CIniworker::Write(const CIniworker::JobType& action)
{
iniFile.seek(0);
QByteArray iniData = iniFile.readAll();
QJsonDocument iniDoc(QJsonDocument::fromJson(iniData));
QJsonObject iniObjects(iniDoc.object());
switch (action)
{
case CIniworker::W_PROXY:
{
QNetworkProxy proxy = QNetworkProxy::applicationProxy();
QJsonObject proxyObj
{
{ "proxyType", proxy.type() },
{ "proxyAddress", proxy.hostName() },
{ "proxyPort", proxy.port() }
};
iniObjects.insert("Proxy", proxyObj);
auto walletSettings = QJsonDocument (iniObjects).toJson();
iniFile.resize(0);
iniFile.write(walletSettings, walletSettings.size());
iniFile.seek(0);
break;
}
case CIniworker::W_DUMMY:
{
iniObjects.insert("Dummy", 0);
auto walletSettings = QJsonDocument (iniObjects).toJson();
iniFile.write(walletSettings, walletSettings.size());
iniFile.seek(0);
break;
}
default: {};
}
}
void CIniworker::Read(const CIniworker::JobType &action)
{
iniFile.seek(0);
QByteArray iniData = iniFile.readAll();
QJsonDocument iniDoc(QJsonDocument::fromJson(iniData));
QJsonObject iniObjects(iniDoc.object());
if ( action == CIniworker::R_PROXY)
{
if ( iniObjects.contains("Proxy") && iniObjects["Proxy"].isObject() )
{
QNetworkProxy proxy = QNetworkProxy::applicationProxy();
QJsonObject proxyObj(iniObjects["Proxy"].toObject());
proxy.setType(static_cast<QNetworkProxy::ProxyType>(proxyObj["proxyType"].toInt()));
proxy.setHostName(proxyObj["proxyAddress"].toString());
proxy.setPort(static_cast<qint16>(proxyObj["proxyPort"].toInt()));
QNetworkProxy::setApplicationProxy(proxy);
};
}
}