7#include "kwalletfreedesktopattributes.h"
10#include "ksecretd_debug.h"
11#include "kwalletfreedesktopcollection.h"
14#include <QJsonDocument>
17KWalletFreedesktopAttributes::KWalletFreedesktopAttributes(
const QString &walletName)
20 _path = writeLocation +
QChar::fromLatin1(
'/') + KSecretD::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(KSECRETD_LOG) <<
"Can't read attributes file " << _path;
41 content = file.readAll();
45 if (jsonDoc.isObject()) {
46 _params = jsonDoc.object();
48 qCWarning(KSECRETD_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(KSECRETD_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(KSECRETD_LOG) <<
"Cannot write attributes file " << _path;
78 qCWarning(KSECRETD_LOG) <<
"Cannot commit attributes file " << _path;
82static QString entryLocationToStr(
const EntryLocation &entryLocation)
87static EntryLocation splitToEntryLocation(
const QString &entryLocation)
91 qCWarning(KSECRETD_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(KSECRETD_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);
184 const auto foundParams = _params.find(strLocation);
186 if (foundParams != _params.end() && foundParams->isObject()) {
187 params = foundParams->toObject();
192 if (jsonAttrs.
empty()) {
193 params.
remove(QStringLiteral(
"attributes"));
195 params[QStringLiteral(
"attributes")] = jsonAttrs;
198 _params[strLocation] = params;
203StrStrMap KWalletFreedesktopAttributes::getAttributes(
const EntryLocation &entryLocation)
const
205 const auto foundObj = _params.find(entryLocationToStr(entryLocation));
206 if (foundObj == _params.end() || !foundObj->isObject()) {
209 const auto jsonParams = foundObj->toObject();
211 const auto foundAttrs = jsonParams.find(QStringLiteral(
"attributes"));
212 if (foundAttrs == jsonParams.end() || !foundAttrs->isObject()) {
215 const auto jsonAttrs = foundAttrs->toObject();
220 if (i.value().isString()) {
221 itemAttrs.
insert(i.key(), i.value().toString());
228QString KWalletFreedesktopAttributes::getStringParam(
const EntryLocation &entryLocation,
const QString ¶mName,
const QString &defaultParam)
const
230 const auto foundParams = _params.find(entryLocationToStr(entryLocation));
231 if (foundParams == _params.end() || !foundParams->isObject()) {
234 const auto params = foundParams->toObject();
236 const auto foundParam = params.
find(paramName);
237 if (foundParam == params.
end() || !foundParam->isString()) {
241 return foundParam->toString();
244qulonglong KWalletFreedesktopAttributes::getULongLongParam(
const EntryLocation &entryLocation,
const QString ¶mName, qulonglong defaultParam)
const
246 const auto str = getStringParam(entryLocation, paramName,
QString::number(defaultParam));
248 const auto result = str.toULongLong(&ok);
249 return ok ? result : defaultParam;
252void KWalletFreedesktopAttributes::setParam(
const EntryLocation &entryLocation,
const QString ¶mName,
const QString ¶m)
254 const auto entryLoc = entryLocationToStr(entryLocation);
255 const auto foundParams = _params.find(entryLoc);
256 if (foundParams == _params.end() || !foundParams->isObject()) {
260 auto params = foundParams->toObject();
262 params[paramName] = param;
263 _params[entryLoc] = params;
268void KWalletFreedesktopAttributes::setParam(
const EntryLocation &entryLocation,
const QString ¶mName, qulonglong param)
273void KWalletFreedesktopAttributes::remove(
const FdoUniqueLabel &itemUniqLabel)
275 remove(itemUniqLabel.toEntryLocation());
278void KWalletFreedesktopAttributes::setAttributes(
const FdoUniqueLabel &itemUniqLabel,
const StrStrMap &attributes)
280 setAttributes(itemUniqLabel.toEntryLocation(), attributes);
283StrStrMap KWalletFreedesktopAttributes::getAttributes(
const FdoUniqueLabel &itemUniqLabel)
const
285 return getAttributes(itemUniqLabel.toEntryLocation());
288QString KWalletFreedesktopAttributes::getStringParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName,
const QString &defaultParam)
const
290 return getStringParam(itemUniqLabel.toEntryLocation(), paramName, defaultParam);
293qulonglong KWalletFreedesktopAttributes::getULongLongParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName, qulonglong defaultParam)
const
295 return getULongLongParam(itemUniqLabel.toEntryLocation(), paramName, defaultParam);
298void KWalletFreedesktopAttributes::setParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName,
const QString ¶m)
300 setParam(itemUniqLabel.toEntryLocation(), paramName, param);
303void KWalletFreedesktopAttributes::setParam(
const FdoUniqueLabel &itemUniqLabel,
const QString ¶mName, qulonglong param)
305 setParam(itemUniqLabel.toEntryLocation(), paramName, param);
310 QList<EntryLocation> items;
311 for (
auto i = _params.constBegin(); i != _params.constEnd(); ++i) {
313 items.
push_back(splitToEntryLocation(i.key()));
319qulonglong KWalletFreedesktopAttributes::lastModified()
const
321 auto found = _params.constFind(FDO_KEY_MODIFIED);
322 if (found == _params.constEnd()) {
325 return found->toString().toULongLong();
328qulonglong KWalletFreedesktopAttributes::birthTime()
const
330 auto found = _params.constFind(FDO_KEY_CREATED);
331 if (found == _params.constEnd()) {
334 return found->toString().toULongLong();
337void 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