mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-06 10:03:26 -08:00
[Client] DeckSyncProfile + sidecar + DeckDiff with tests
This commit is contained in:
parent
a03f4c2fa4
commit
8f09048bce
12 changed files with 737 additions and 0 deletions
98
cockatrice/src/interface/deck_sync/deck_sync_sidecar.cpp
Normal file
98
cockatrice/src/interface/deck_sync/deck_sync_sidecar.cpp
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#include "deck_sync_sidecar.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr int SIDECAR_FORMAT_VERSION = 1;
|
||||
} // namespace
|
||||
|
||||
DeckSyncSidecar::DeckSyncSidecar(const QString &_filePath) : filePath(_filePath)
|
||||
{
|
||||
}
|
||||
|
||||
bool DeckSyncSidecar::load()
|
||||
{
|
||||
recordMap.clear();
|
||||
|
||||
QFile file(filePath);
|
||||
if (!file.exists()) {
|
||||
return true;
|
||||
}
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
return false;
|
||||
}
|
||||
const QJsonDocument document = QJsonDocument::fromJson(file.readAll());
|
||||
if (!document.isObject() || !document.object().value("records").isObject()) {
|
||||
return false;
|
||||
}
|
||||
const QJsonObject records = document.object().value("records").toObject();
|
||||
for (auto it = records.begin(); it != records.end(); ++it) {
|
||||
if (!it.value().isObject()) {
|
||||
continue;
|
||||
}
|
||||
const QJsonObject entry = it.value().toObject();
|
||||
DeckSyncRecord record;
|
||||
record.guid = entry.value("guid").toString();
|
||||
record.remoteId = entry.value("remoteId").toInt(-1);
|
||||
record.baseHash = entry.value("baseHash").toString();
|
||||
record.lastSyncSecs = static_cast<qint64>(entry.value("lastSync").toDouble());
|
||||
record.deleted = entry.value("deleted").toBool(false);
|
||||
recordMap.insert(it.key(), record);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeckSyncSidecar::save() const
|
||||
{
|
||||
QJsonObject records;
|
||||
for (auto it = recordMap.begin(); it != recordMap.end(); ++it) {
|
||||
QJsonObject entry;
|
||||
entry.insert("guid", it.value().guid);
|
||||
entry.insert("remoteId", it.value().remoteId);
|
||||
entry.insert("baseHash", it.value().baseHash);
|
||||
entry.insert("lastSync", static_cast<double>(it.value().lastSyncSecs));
|
||||
entry.insert("deleted", it.value().deleted);
|
||||
records.insert(it.key(), entry);
|
||||
}
|
||||
QJsonObject root;
|
||||
root.insert("version", SIDECAR_FORMAT_VERSION);
|
||||
root.insert("records", records);
|
||||
|
||||
QDir().mkpath(QFileInfo(filePath).absolutePath());
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
return false;
|
||||
}
|
||||
file.write(QJsonDocument(root).toJson(QJsonDocument::Compact));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeckSyncSidecar::contains(const QString &relativePath) const
|
||||
{
|
||||
return recordMap.contains(relativePath);
|
||||
}
|
||||
|
||||
DeckSyncRecord DeckSyncSidecar::get(const QString &relativePath) const
|
||||
{
|
||||
return recordMap.value(relativePath, DeckSyncRecord());
|
||||
}
|
||||
|
||||
void DeckSyncSidecar::set(const QString &relativePath, const DeckSyncRecord &record)
|
||||
{
|
||||
recordMap.insert(relativePath, record);
|
||||
}
|
||||
|
||||
void DeckSyncSidecar::remove(const QString &relativePath)
|
||||
{
|
||||
recordMap.remove(relativePath);
|
||||
}
|
||||
|
||||
QMap<QString, DeckSyncRecord> DeckSyncSidecar::records() const
|
||||
{
|
||||
return recordMap;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue