|
| 1 | +// SPDX-FileCopyrightText: Team OpenVPI |
| 2 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 3 | + |
| 4 | +#include "Storage.h" |
| 5 | +#include "Storage_p.h" |
| 6 | + |
| 7 | +#include <exception> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include <QScopedValueRollback> |
| 11 | + |
| 12 | +namespace BatchProcess { |
| 13 | + |
| 14 | + StoragePrivate::StoragePrivate(Storage *q) : q_ptr(q) { |
| 15 | + } |
| 16 | + |
| 17 | + void StoragePrivate::setError(StorageError *error, StorageErrorCode code, const QString &operation, const std::optional<QString> &key, const QString &message) const { |
| 18 | + if (!error) { |
| 19 | + return; |
| 20 | + } |
| 21 | + error->code = code; |
| 22 | + error->message = message; |
| 23 | + error->operation = operation; |
| 24 | + error->storageId = id; |
| 25 | + error->key = key; |
| 26 | + } |
| 27 | + |
| 28 | + bool StoragePrivate::commit(const StorageItems &proposedItems, const QString &operation, const std::optional<QString> &key, StorageError *error) { |
| 29 | + if (error) { |
| 30 | + error->clear(); |
| 31 | + } |
| 32 | + if (committing) { |
| 33 | + setError(error, StorageErrorCode::InvalidState, operation, key, Storage::tr("A storage commit callback cannot modify the same storage.")); |
| 34 | + return false; |
| 35 | + } |
| 36 | + if (!commitCallback) { |
| 37 | + items = proposedItems; |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + StorageError localError; |
| 42 | + auto targetError = error ? error : &localError; |
| 43 | + QScopedValueRollback guard(committing, true); |
| 44 | + bool committed = false; |
| 45 | + try { |
| 46 | + committed = commitCallback(proposedItems, targetError); |
| 47 | + } catch (const std::exception &exception) { |
| 48 | + setError(targetError, StorageErrorCode::Unknown, operation, key, Storage::tr("The storage commit callback threw an exception: %1").arg(QString::fromUtf8(exception.what()))); |
| 49 | + return false; |
| 50 | + } catch (...) { |
| 51 | + setError(targetError, StorageErrorCode::Unknown, operation, key, Storage::tr("The storage commit callback threw an unknown exception.")); |
| 52 | + return false; |
| 53 | + } |
| 54 | + if (!committed) { |
| 55 | + if (!targetError->hasError()) { |
| 56 | + setError(targetError, StorageErrorCode::Unknown, operation, key, Storage::tr("The storage commit callback rejected the change.")); |
| 57 | + } else { |
| 58 | + if (targetError->operation.isEmpty()) { |
| 59 | + targetError->operation = operation; |
| 60 | + } |
| 61 | + if (targetError->storageId.isEmpty()) { |
| 62 | + targetError->storageId = id; |
| 63 | + } |
| 64 | + if (!targetError->key && key) { |
| 65 | + targetError->key = key; |
| 66 | + } |
| 67 | + } |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + items = proposedItems; |
| 72 | + if (error) { |
| 73 | + error->clear(); |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + Storage::Storage(const QString &id, const StorageItems &initialItems, StorageCommitCallback commitCallback, QObject *parent) |
| 79 | + : QObject(parent), d_ptr(new StoragePrivate(this)) { |
| 80 | + Q_D(Storage); |
| 81 | + d->id = id; |
| 82 | + d->items = initialItems; |
| 83 | + d->commitCallback = std::move(commitCallback); |
| 84 | + } |
| 85 | + |
| 86 | + Storage::~Storage() = default; |
| 87 | + |
| 88 | + QString Storage::id() const { |
| 89 | + Q_D(const Storage); |
| 90 | + return d->id; |
| 91 | + } |
| 92 | + |
| 93 | + qsizetype Storage::length() const { |
| 94 | + Q_D(const Storage); |
| 95 | + return d->items.size(); |
| 96 | + } |
| 97 | + |
| 98 | + QStringList Storage::keys() const { |
| 99 | + Q_D(const Storage); |
| 100 | + return d->items.keys(); |
| 101 | + } |
| 102 | + |
| 103 | + std::optional<QString> Storage::key(qsizetype index) const { |
| 104 | + Q_D(const Storage); |
| 105 | + if (index < 0 || index >= d->items.size()) { |
| 106 | + return std::nullopt; |
| 107 | + } |
| 108 | + return d->items.keys().at(index); |
| 109 | + } |
| 110 | + |
| 111 | + std::optional<QString> Storage::getItem(const QString &key) const { |
| 112 | + Q_D(const Storage); |
| 113 | + const auto it = d->items.constFind(key); |
| 114 | + if (it == d->items.cend()) { |
| 115 | + return std::nullopt; |
| 116 | + } |
| 117 | + return it.value(); |
| 118 | + } |
| 119 | + |
| 120 | + StorageItems Storage::items() const { |
| 121 | + Q_D(const Storage); |
| 122 | + return d->items; |
| 123 | + } |
| 124 | + |
| 125 | + bool Storage::setItem(const QString &key, const QString &value, StorageError *error) { |
| 126 | + Q_D(Storage); |
| 127 | + const auto it = d->items.constFind(key); |
| 128 | + if (it != d->items.cend() && it.value() == value) { |
| 129 | + if (error) { |
| 130 | + error->clear(); |
| 131 | + } |
| 132 | + return true; |
| 133 | + } |
| 134 | + auto proposedItems = d->items; |
| 135 | + proposedItems.insert(key, value); |
| 136 | + return d->commit(proposedItems, QStringLiteral("setItem"), key, error); |
| 137 | + } |
| 138 | + |
| 139 | + bool Storage::removeItem(const QString &key, StorageError *error) { |
| 140 | + Q_D(Storage); |
| 141 | + if (!d->items.contains(key)) { |
| 142 | + if (error) { |
| 143 | + error->clear(); |
| 144 | + } |
| 145 | + return true; |
| 146 | + } |
| 147 | + auto proposedItems = d->items; |
| 148 | + proposedItems.remove(key); |
| 149 | + return d->commit(proposedItems, QStringLiteral("removeItem"), key, error); |
| 150 | + } |
| 151 | + |
| 152 | + bool Storage::clear(StorageError *error) { |
| 153 | + Q_D(Storage); |
| 154 | + if (d->items.isEmpty()) { |
| 155 | + if (error) { |
| 156 | + error->clear(); |
| 157 | + } |
| 158 | + return true; |
| 159 | + } |
| 160 | + return d->commit({}, QStringLiteral("clear"), std::nullopt, error); |
| 161 | + } |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +#include "moc_Storage.cpp" |
0 commit comments