7#include "kwalletfreedesktopattributes.h"
10#include "kwalletd_debug.h"
11#include "kwalletfreedesktopcollection.h"
14#include <QJsonDocument>
17KWalletFreedesktopAttributes::KWalletFreedesktopAttributes(
const QString &walletName)
20 _path = writeLocation +
QChar::fromLatin1(
'/') + KWalletD::encodeWalletName(walletName) + QStringLiteral(
"_attributes.json");
24 if (!_params.contains(FDO_KEY_CREATED)) {
26 _params[FDO_KEY_CREATED] = currentTime;
27 _params[FDO_KEY_MODIFIED] = currentTime;
31void KWalletFreedesktopAttributes::read()
38 qCDebug(KWALLETD_LOG) <<
"Can't read attributes file " << _path;
41 content = file.readAll();
45 if (jsonDoc.isObject()) {
46 _params = jsonDoc.object();
48 qCWarning(KWALLETD_LOG) <<
"Can't read attributes: the root element must be an JSON-object: " << _path;
49 _params = QJsonObject();
53void KWalletFreedesktopAttributes::write()
55 if (_params.empty()) {
64 qCWarning(KWALLETD_LOG) <<
"Can't write attributes file: " << _path;
69 const QJsonDocument saveDoc(_params);
71 const QByteArray jsonBytes = saveDoc.toJson();
72 if (sf.write(jsonBytes) != jsonBytes.
size()) {
74 qCWarning(KWALLETD_LOG) <<
"Cannot write attributes file " << _path;
78 qCWarning(KWALLETD_LOG) <<
"Cannot commit attributes file " << _path;
82static QString entryLocationToStr(
const EntryLocation &entryLocation)
87static EntryLocation splitToEntryLocation(
const QString &entryLocation)
91 qCWarning(KWALLETD_LOG) <<
"Entry location '" << entryLocation <<
"' has no slash '/'";
94 return {entryLocation.
left(slashPos), entryLocation.
right((entryLocation.
size() - slashPos) - 1)};
98void KWalletFreedesktopAttributes::remove(
const EntryLocation &entryLocation)
100 _params.remove(entryLocationToStr(entryLocation));
101 if (_params.empty()) {
108void KWalletFreedesktopAttributes::deleteFile()
113void KWalletFreedesktopAttributes::renameLabel(
const EntryLocation &oldLocation,
const EntryLocation &newLocation)
115 const QString oldLoc = entryLocationToStr(oldLocation);
117 const auto found = _params.find(oldLoc);
118 if (found == _params.end() || !found->isObject()) {
119 qCWarning(KWALLETD_LOG) <<
"Can't rename label (!?)";
122 const auto obj = found->toObject();
123 _params.erase(found);
124 _params.insert(entryLocationToStr(newLocation), obj);
129void KWalletFreedesktopAttributes::renameWallet(
const QString &newName)
132 const QString newPath = writeLocation +
QChar::fromLatin1(
'/') + newName + QStringLiteral(
"_attributes.json");
138void KWalletFreedesktopAttributes::newItem(
const EntryLocation &entryLocation)
140 _params[entryLocationToStr(entryLocation)] = QJsonObject();
143QList<EntryLocation> KWalletFreedesktopAttributes::matchAttributes(
const StrStrMap &attributes)
const
145 QList<EntryLocation> items;
147 for (
auto i = _params.constBegin(); i != _params.constEnd(); ++i) {
148 if (!i->isObject()) {
153 const auto itemParams = i->toObject();
154 const auto foundItemAttribs = itemParams.find(QStringLiteral(
"attributes"));
155 if (foundItemAttribs == itemParams.end() || !foundItemAttribs->isObject()) {
158 const auto itemAttribs = foundItemAttribs->toObject();
161 const auto foundKey = itemAttribs.find(i.key());
162 if (foundKey == itemAttribs.end() || !foundKey->isString() || foundKey->toString() != i.value()) {
169 items += splitToEntryLocation(i.key());
176void KWalletFreedesktopAttributes::setAttributes(
const EntryLocation &entryLocation,
const StrStrMap &attributes)
178 QJsonObject jsonAttrs;
180 jsonAttrs.
insert(i.key(), i.value());
183 const QString strLocation = entryLocationToStr(entryLocation);
185 const auto foundParams = _params.find(strLocation);
187 if (foundParams != _params.end() && foundParams->isObject()) {
188 params = foundParams->toObject();
193 if (jsonAttrs.
empty()) {
194 params.
remove(QStringLiteral(
"attributes"));
196 params[QStringLiteral(
"attributes")] = jsonAttrs;
199 _params[strLocation] = params;
204StrStrMap KWalletFreedesktopAttributes::getAttributes(
const EntryLocation &entryLocation)
const
206 const auto foundObj = _params.find(entryLocationToStr(entryLocation));
207 if (foundObj == _params.end() || !foundObj->isObject()) {
210 const auto jsonParams = foundObj->toObject();
212 const auto foundAttrs = jsonParams.find(QStringLiteral(
"attributes"));
213 if (foundAttrs == jsonParams.end() || !foundAttrs->isObject()) {
216 const auto jsonAttrs = foundAttrs->toObject();
221 if (i.value().isString()) {
222 itemAttrs.
insert(i.key(), i.value().toString());
229QString KWalletFreedesktopAttributes::getStringParam(
const EntryLocation &entryLocation,
const QString ¶mName,
const QString &defaultParam)
const
231 const auto foundParams = _params.find(entryLocationToStr(entryLocation));
232 if (foundParams == _params.end() || !foundParams->isObject()) {
235 const auto params = foundParams->toObject();
237 const auto foundParam = params.
find(paramName);
238 if (foundParam == params.
end() || !foundParam->isString()) {
242 return foundParam->toString();
245qulonglong KWalletFreedesktopAttributes::getULongLongParam(
const EntryLocation &entryLocation,
const QString ¶mName, qulonglong defaultParam)
const
247 const auto str = getStringParam(entryLocation, paramName,
QString::number(defaultParam));
249 const auto result = str.toULongLong(&ok);
250 return ok ? result : defaultParam;
253void KWalletFreedesktopAttributes::setParam(
const EntryLocation &entryLocation,
const QString ¶mName,
const QString ¶m)
255 const auto entryLoc = entryLocationToStr(entryLocation);
256 const auto foundParams = _params.find(entryLoc);
257 if (foundParams == _params.end() || !foundParams->isObject()) {
261 auto params = foundParams->toObject();
263 params[paramName] = param;
264 _params[entryLoc] = params;
269void KWalletFreedesktopAttributes::setParam(
const EntryLocation &entryLocation,
const QString ¶mName, qulonglong param)
274void KWalletFreedesktopAttributes::remove(
const FdoUniqueLabel &itemUniqLabel)
276 remove(itemUniqLabel.toEntryLocation());
279void KWalletFreedesktopAttributes::setAttributes(
const FdoUniqueLabel &itemUniqLabel,
const StrStrMap &attributes)
281 setAttributes(itemUniqLabel.toEntryLocation(), attributes);
284StrStrMap KWalletFreedesktopAttributes::getAttributes(
const FdoUniqueLabel &itemUniqLabel)
const
286 return getAttributes(itemUniqLabel.toEntryLocation());
289QString KWalletFreedesktopAttributes::getStringParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName,
const QString &defaultParam)
const
291 return getStringParam(itemUniqLabel.toEntryLocation(), paramName, defaultParam);
294qulonglong KWalletFreedesktopAttributes::getULongLongParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName, qulonglong defaultParam)
const
296 return getULongLongParam(itemUniqLabel.toEntryLocation(), paramName, defaultParam);
299void KWalletFreedesktopAttributes::setParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName,
const QString ¶m)
301 setParam(itemUniqLabel.toEntryLocation(), paramName, param);
304void KWalletFreedesktopAttributes::setParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName, qulonglong param)
306 setParam(itemUniqLabel.toEntryLocation(), paramName, param);
311 QList<EntryLocation> items;
312 for (
auto i = _params.constBegin(); i != _params.constEnd(); ++i) {
314 items.
push_back(splitToEntryLocation(i.key()));
320qulonglong KWalletFreedesktopAttributes::lastModified()
const
322 auto found = _params.constFind(FDO_KEY_MODIFIED);
323 if (found == _params.constEnd()) {
326 return found->toString().toULongLong();
329qulonglong KWalletFreedesktopAttributes::birthTime()
const
331 auto found = _params.constFind(FDO_KEY_CREATED);
332 if (found == _params.constEnd()) {
335 return found->toString().toULongLong();
338void KWalletFreedesktopAttributes::updateLastModified()
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
qsizetype size() const const
qint64 currentSecsSinceEpoch()
bool rename(const QString &newName)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator find(QLatin1StringView key)
iterator insert(QLatin1StringView key, const QJsonValue &value)
void remove(QLatin1StringView key)
void push_back(parameter_type value)
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator insert(const Key &key, const T &value)
QString writableLocation(StandardLocation type)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
QString left(qsizetype n) const const
QString number(double n, char format, int precision)
QString right(qsizetype n) const const
qsizetype size() const const