7#include "autocryptstorage.h"
8#include "autocryptrecipient_p.h"
9#include "autocryptstorage_p.h"
13#include <QStandardPaths>
14#include <QTemporaryFile>
16#include <autocrypt_debug.h>
18using namespace MessageCore;
20AutocryptStoragePrivate::AutocryptStoragePrivate()
25AutocryptStorage::Ptr AutocryptStorage::mSelf =
nullptr;
27AutocryptStorage::Ptr AutocryptStorage::self()
30 mSelf = AutocryptStorage::Ptr(
new AutocryptStorage());
36QString address2Filename(
const QByteArray &addr)
42AutocryptStorage::AutocryptStorage()
43 : d_ptr(new AutocryptStoragePrivate())
47AutocryptRecipient::Ptr AutocryptStorage::getRecipient(
const QByteArray &addr)
49 Q_D(AutocryptStorage);
50 if (d->recipients.contains(addr)) {
51 return d->recipients.value(addr);
54 const QString fileName(address2Filename(addr));
55 if (d->basePath.exists(fileName)) {
56 QFile file(d->basePath.filePath(fileName));
57 auto recipient = AutocryptRecipient::Ptr(
new AutocryptRecipient);
61 recipient->fromJson(file.readAll());
62 d->recipients[addr] = recipient;
68AutocryptRecipient::Ptr AutocryptStorage::addRecipient(
const QByteArray &addr)
70 Q_D(AutocryptStorage);
72 auto recipient = getRecipient(addr);
77 recipient = AutocryptRecipient::Ptr(
new AutocryptRecipient);
78 recipient->d_func()->addr = addr;
79 d->recipients[addr] = recipient;
83void AutocryptStorage::deleteRecipient(
const QByteArray &addr)
85 Q_D(AutocryptStorage);
86 const QString fileName(address2Filename(addr));
87 d->basePath.remove(fileName);
88 d->recipients.remove(addr);
91AutocryptStorage::~AutocryptStorage() =
default;
93void AutocryptStorage::save()
95 Q_D(AutocryptStorage);
96 if (!d->basePath.exists()) {
97 QDir parent = d->basePath;
99 qCWarning(AUTOCRYPT_LOG) << parent.
absolutePath() <<
"does not exist. Cancel saving Autocrypt storage.";
103 if (!parent.
mkdir(d->basePath.dirName())) {
104 qCWarning(AUTOCRYPT_LOG) <<
"Cancel saving Autocrypt storage, because failed to create" << d->basePath.absolutePath();
108 const auto keys = d->recipients.keys();
109 for (
const auto &addr : keys) {
110 const auto recipient = d->recipients.value(addr);
111 const QString fileName(address2Filename(addr));
112 if (recipient->hasChanged() || !d->basePath.exists(fileName)) {
113 QTemporaryFile file(d->basePath.path() + QLatin1Char(
'/'));
119 d->basePath.remove(fileName);
120 file.rename(d->basePath.filePath(fileName));
121 file.setAutoRemove(
false);
122 recipient->setChangedFlag(
false);
QString absolutePath() const const
bool mkdir(const QString &dirName) const const
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)