forked from eghuro/qtkeychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plaintextstore.cpp
110 lines (90 loc) · 3.43 KB
/
plaintextstore.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
/******************************************************************************
* Copyright (C) 2011-2015 Frank Osterfeld <[email protected]> *
* Copyright (C) 2016 Mathias Hasselmann <[email protected]> *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. For licensing and distribution *
* details, check the accompanying file 'COPYING'. *
*****************************************************************************/
#include "plaintextstore_p.h"
using namespace QKeychain;
namespace {
#ifdef Q_OS_WIN
inline QString dataKey(const QString &key) { return key; }
#else // Q_OS_WIN
inline QString dataKey(const QString &key) { return key + QLatin1String("/data"); }
inline QString typeKey(const QString &key) { return key + QLatin1String("/type"); }
#endif // Q_OS_WIN
}
PlainTextStore::PlainTextStore(const QString &service, QSettings *settings)
: m_localSettings(settings ? 0 : new QSettings(service))
, m_actualSettings(settings ? settings : m_localSettings.data())
, m_error(NoError)
{
}
bool PlainTextStore::contains(const QString &key) const
{
return m_actualSettings->contains(dataKey(key));
}
QByteArray PlainTextStore::readData(const QString &key)
{
return read(dataKey(key)).toByteArray();
}
#ifndef Q_OS_WIN
JobPrivate::Mode PlainTextStore::readMode(const QString &key)
{
return JobPrivate::stringToMode(read(typeKey(key)).toString());
}
#endif // Q_OS_WIN
void PlainTextStore::write(const QString &key, const QByteArray &data, JobPrivate::Mode mode)
{
if (m_actualSettings->status() != QSettings::NoError)
return;
#ifndef Q_OS_WIN
m_actualSettings->setValue(typeKey(key), JobPrivate::modeToString(mode));
#else // Q_OS_WIN
Q_UNUSED(mode);
#endif // Q_OS_WIN
m_actualSettings->setValue(dataKey(key), data);
m_actualSettings->sync();
if (m_actualSettings->status() == QSettings::AccessError) {
setError(AccessDenied, tr("Could not store data in settings: access error"));
} else if (m_actualSettings->status() != QSettings::NoError) {
setError(OtherError, tr("Could not store data in settings: format error"));
} else {
setError(NoError, QString());
}
}
void PlainTextStore::remove(const QString &key)
{
if (m_actualSettings->status() != QSettings::NoError)
return;
#ifndef Q_OS_WIN
m_actualSettings->remove(typeKey(key));
#endif // Q_OS_WIN
m_actualSettings->remove(dataKey(key));
m_actualSettings->sync();
if (m_actualSettings->status() == QSettings::AccessError) {
setError(AccessDenied, tr("Could not delete data from settings: access error"));
} else if (m_actualSettings->status() != QSettings::NoError) {
setError(OtherError, tr("Could not delete data from settings: format error"));
} else {
setError(NoError, QString());
}
}
void PlainTextStore::setError(Error error, const QString &errorString)
{
m_error = error;
m_errorString = errorString;
}
QVariant PlainTextStore::read(const QString &key)
{
const QVariant value = m_actualSettings->value(key);
if (value.isNull()) {
setError(EntryNotFound, tr("Entry not found"));
} else {
setError(NoError, QString());
}
return value;
}