[Client+Server] Auto deck sync with change polling

This commit is contained in:
Lukas Brübach 2026-10-11 17:02:30 +02:00 • committed by Lukas Brübach
parent e8ab52a2a3
commit ff695d265d
18 changed files with 448 additions and 11 deletions

View file

@ -8,10 +8,13 @@
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QLoggingCategory>
#include <QSaveFile>
#include <QStringList>
#include <QTimer>
#include <QUuid>
#include <QtGlobal>
#include <libcockatrice/deck_list/deck_diff.h>
#include <libcockatrice/deck_list/deck_file_format.h>
#include <libcockatrice/deck_list/deck_list.h>
@ -20,12 +23,15 @@
#include <libcockatrice/protocol/pb/command_deck_download.pb.h>
#include <libcockatrice/protocol/pb/command_deck_list.pb.h>
#include <libcockatrice/protocol/pb/command_deck_new_dir.pb.h>
#include <libcockatrice/protocol/pb/command_deck_sync_check.pb.h>
#include <libcockatrice/protocol/pb/command_deck_upload.pb.h>
#include <libcockatrice/protocol/pb/response.pb.h>
#include <libcockatrice/protocol/pb/response_deck_download.pb.h>
#include <libcockatrice/protocol/pb/response_deck_list.pb.h>
#include <libcockatrice/protocol/pb/response_deck_sync_status.pb.h>
#include <libcockatrice/protocol/pb/response_deck_upload.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_deckstorage.pb.h>
#include <libcockatrice/protocol/pb/serverinfo_user.pb.h>
#include <libcockatrice/protocol/pending_command.h>
#include <libcockatrice/utility/string_limits.h>
@ -35,6 +41,7 @@ namespace
{
constexpr const char *SYNC_META_DIR = ".sync";
constexpr const char *BACKUP_SUFFIX = ".sync-backup-";
constexpr int WATCH_DEBOUNCE_MS = 2000;
QString serverPathFor(const QString &relativePath)
{
@ -109,6 +116,20 @@ const char *sidecarModeFor(DeckSyncMode mode)
DeckSyncManager::DeckSyncManager(QObject *parent) : QObject(parent)
{
watcher = new QFileSystemWatcher(this);
debounceTimer = new QTimer(this);
debounceTimer->setSingleShot(true);
debounceTimer->setInterval(WATCH_DEBOUNCE_MS);
intervalTimer = new QTimer(this);
connect(watcher, &QFileSystemWatcher::fileChanged, this,
[this](const QString &path) { onWatchedPathChanged(path); });
connect(watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &path) {
refreshWatcher();
onWatchedPathChanged(path);
});
connect(debounceTimer, &QTimer::timeout, this, &DeckSyncManager::onDebounceTimeout);
connect(intervalTimer, &QTimer::timeout, this, [this] { autoSyncSoon(); });
refreshInterval();
}
DeckSyncManager::DeckSyncManager(AbstractClient *_client, ColorIdentityProvider provider, QObject *parent)
@ -116,16 +137,126 @@ DeckSyncManager::DeckSyncManager(AbstractClient *_client, ColorIdentityProvider
{
client = _client;
colorIdentityProvider = std::move(provider);
if (client == nullptr) {
return;
}
connect(
client, &AbstractClient::statusChanged, this,
[this](ClientStatus status) {
if (status == StatusDisconnected) {
offline = true;
debounceTimer->stop();
}
},
Qt::UniqueConnection);
connect(
client, &AbstractClient::userInfoChanged, this,
[this](const ServerInfo_User &userInfo) {
offline = false;
serverUsername = QString::fromStdString(userInfo.name());
autoSyncSoon();
},
Qt::UniqueConnection);
}
void DeckSyncManager::setProfile(const DeckSyncProfile &_profile)
void DeckSyncManager::setServerIdentity(const QString &host, int port, const QString &username)
{
profile = _profile;
serverHost = host;
serverPort = port;
serverUsername = username;
}
void DeckSyncManager::setDeckPath(const QString &path)
{
deckPath = path;
refreshWatcher();
}
void DeckSyncManager::setAutoSyncEnabled(bool enabled)
{
autoSyncEnabled = enabled;
refreshInterval();
}
void DeckSyncManager::setSyncIntervalMinutes(int minutes)
{
syncIntervalMinutes = qBound(1, minutes, 60);
refreshInterval();
}
DeckSyncProfile DeckSyncManager::buildProfile() const
{
return DeckSyncProfile(serverHost, serverPort, serverUsername);
}
void DeckSyncManager::refreshInterval()
{
intervalTimer->stop();
if (autoSyncEnabled) {
intervalTimer->start(syncIntervalMinutes * 60000);
}
}
void DeckSyncManager::refreshWatcher()
{
watcher->removePaths(watcher->files() + watcher->directories());
if (deckPath.isEmpty() || !QDir(deckPath).exists()) {
return;
}
QStringList watched;
watched.append(deckPath);
QDirIterator it(deckPath, QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (it.hasNext()) {
const QString path = it.next();
if (!path.contains("/" + QString(SYNC_META_DIR) + "/") && !path.endsWith("/" + QString(SYNC_META_DIR))) {
watched.append(path);
}
}
watcher->addPaths(watched);
}
void DeckSyncManager::onWatchedPathChanged(const QString &path)
{
if (path.contains("/" + QString(SYNC_META_DIR) + "/")) {
return;
}
autoSyncSoon();
}
void DeckSyncManager::autoSyncSoon()
{
if (!autoSyncEnabled) {
return;
}
debounceTimer->start(WATCH_DEBOUNCE_MS);
}
void DeckSyncManager::onDebounceTimeout()
{
if (syncing) {
debounceTimer->start(WATCH_DEBOUNCE_MS);
return;
}
if (canAutoRun()) {
syncNow();
}
}
bool DeckSyncManager::canAutoRun() const
{
if (!autoSyncEnabled || offline || client == nullptr || deckPath.isEmpty()) {
return false;
}
const DeckSyncProfile autoProfile = buildProfile();
if (!autoProfile.isValid()) {
return false;
}
const QString autoSidecarPath = DeckSyncSidecar::pathForDeckPath(deckPath, autoProfile.key());
if (!QFileInfo::exists(autoSidecarPath)) {
return false;
}
DeckSyncSidecar sidecar(autoSidecarPath);
return sidecar.load() && sidecar.mode == DeckSyncSidecar::MODE_LINKED;
}
void DeckSyncManager::setConflictDefault(int _conflictDefault)
@ -143,7 +274,8 @@ void DeckSyncManager::syncNow()
if (syncing) {
return;
}
if (client == nullptr || !profile.isValid() || deckPath.isEmpty()) {
const DeckSyncProfile runProfile = buildProfile();
if (client == nullptr || !runProfile.isValid() || deckPath.isEmpty()) {
emit syncFinished(SyncSummary{}, tr("Not connected to a server."));
return;
}
@ -172,10 +304,11 @@ void DeckSyncManager::syncNow()
localFiles.clear();
sessionDefault = -1;
firstSyncRun = false;
pendingServerTime = -1;
emit syncStarted();
emit statusMessage(tr("Scanning local decks..."));
sidecarPath = DeckSyncSidecar::pathForDeckPath(deckPath, profile.key());
sidecarPath = DeckSyncSidecar::pathForDeckPath(deckPath, runProfile.key());
DeckSyncSidecar sidecar(sidecarPath);
if (!sidecar.load()) {
failSync(tr("Could not read sync state."));
@ -208,12 +341,80 @@ void DeckSyncManager::syncNow()
.modified = QFileInfo(absolutePath).lastModified()});
}
if (!firstSyncRun && sidecar.lastSyncSecs > 0) {
sendSyncCheck(sidecar.lastSyncSecs);
return;
}
fetchFullList();
}
void DeckSyncManager::fetchFullList()
{
Command_DeckList cmd;
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
connect(pend, &PendingCommand::finished, this, &DeckSyncManager::onListFinished);
client->sendCommand(pend);
}
void DeckSyncManager::sendSyncCheck(qint64 lastSyncSecs)
{
emit statusMessage(tr("Checking for server changes..."));
Command_DeckSyncCheck cmd;
cmd.set_last_sync_time(static_cast<google::protobuf::uint64>(lastSyncSecs));
PendingCommand *pend = AbstractClient::prepareSessionCommand(cmd);
connect(pend, &PendingCommand::finished, this, &DeckSyncManager::onCheckFinished);
client->sendCommand(pend);
}
void DeckSyncManager::onCheckFinished(const Response &response, const CommandContainer & /*commandContainer*/)
{
if (!syncing) {
return;
}
if (response.response_code() != Response::RespOk) {
fetchFullList();
return;
}
const Response_DeckSyncStatus &status = response.GetExtension(Response_DeckSyncStatus::ext);
DeckSyncSidecar sidecar(sidecarPath);
if (!sidecar.load()) {
fetchFullList();
return;
}
bool localClean = status.changed_files_size() == 0;
if (localClean) {
for (auto it = localFiles.begin(); it != localFiles.end(); ++it) {
const DeckSyncRecord record = sidecar.get(it.key());
if (record.remoteId == -1 || record.baseHash != it.value().hash) {
localClean = false;
break;
}
}
}
if (localClean) {
const QMap<QString, DeckSyncRecord> knownRecords = sidecar.records();
for (auto it = knownRecords.begin(); it != knownRecords.end(); ++it) {
if (!localFiles.contains(it.key())) {
localClean = false;
break;
}
}
}
if (!localClean) {
if (status.changed_files_size() > 0) {
emit statusMessage(tr("%1 server changes, syncing...").arg(status.changed_files_size()));
}
fetchFullList();
return;
}
pendingServerTime = status.has_server_time() ? static_cast<qint64>(status.server_time()) : -1;
unchangedCount = localFiles.size();
emit statusMessage(tr("Decks are already in sync."));
finishSync();
}
void DeckSyncManager::collectServerFiles(const ServerInfo_DeckStorage_Folder &folder,
const QString &prefix,
QMap<int, ServerFile> &files,
@ -1173,6 +1374,13 @@ void DeckSyncManager::failSync(const QString &error)
void DeckSyncManager::finishSync()
{
syncing = false;
DeckSyncSidecar sidecar(sidecarPath);
if (sidecar.load()) {
sidecar.lastSyncSecs =
pendingServerTime >= 0 ? pendingServerTime : QDateTime::currentDateTime().toSecsSinceEpoch();
sidecar.save();
}
pendingServerTime = -1;
const SyncSummary summary = currentSummary();
QString message = tr("Sync complete: %1 uploaded, %2 downloaded, %3 removed, %4 unchanged, %5 skipped.")
.arg(summary.uploaded)

View file

@ -22,6 +22,8 @@
class AbstractClient;
class CommandContainer;
class DeckList;
class QFileSystemWatcher;
class QTimer;
class Response;
class ServerInfo_DeckStorage_Folder;
@ -73,9 +75,11 @@ public:
explicit DeckSyncManager(QObject *parent = nullptr);
DeckSyncManager(AbstractClient *client, ColorIdentityProvider provider, QObject *parent = nullptr);
void setProfile(const DeckSyncProfile &_profile);
void setServerIdentity(const QString &host, int port, const QString &username);
void setDeckPath(const QString &path);
void setConflictDefault(int conflictDefault);
void setAutoSyncEnabled(bool enabled);
void setSyncIntervalMinutes(int minutes);
[[nodiscard]] bool isSyncing() const;
public slots:
@ -98,6 +102,7 @@ signals:
private slots:
void onListFinished(const Response &response, const CommandContainer &commandContainer);
void onCheckFinished(const Response &response, const CommandContainer &commandContainer);
void
onEnsureDirFinished(const Response &response, const CommandContainer &commandContainer, const QVariant &extraData);
void
@ -163,6 +168,15 @@ private:
void failSync(const QString &error);
void finishSync();
[[nodiscard]] SyncSummary currentSummary() const;
void fetchFullList();
void sendSyncCheck(qint64 lastSyncSecs);
void autoSyncSoon();
[[nodiscard]] bool canAutoRun() const;
void onDebounceTimeout();
void onWatchedPathChanged(const QString &path);
void refreshWatcher();
void refreshInterval();
[[nodiscard]] DeckSyncProfile buildProfile() const;
void collectServerFiles(const ServerInfo_DeckStorage_Folder &folder,
const QString &prefix,
QMap<int, ServerFile> &files,
@ -200,7 +214,9 @@ private:
QString uniqueLocalPath(const QString &relativePath) const;
AbstractClient *client = nullptr;
DeckSyncProfile profile;
QString serverHost;
int serverPort = 0;
QString serverUsername;
QString deckPath;
ColorIdentityProvider colorIdentityProvider;
bool syncing = false;
@ -218,6 +234,13 @@ private:
QMap<int, ServerFile> stashedServerFiles;
ActiveConflict activeConflict;
DeckSyncMode syncMode = DeckSyncMode::TwoWay; ///< This run's direction, loaded from the sidecar.
QFileSystemWatcher *watcher = nullptr;
QTimer *debounceTimer = nullptr;
QTimer *intervalTimer = nullptr;
bool autoSyncEnabled = true;
int syncIntervalMinutes = 5;
bool offline = false;
qint64 pendingServerTime = -1;
int conflictDefault = DeckSyncConflictAsk;
int sessionDefault = -1;
bool firstSyncRun = false;

View file

@ -25,6 +25,7 @@ bool DeckSyncSidecar::load()
{
recordMap.clear();
mode = MODE_LINKED;
lastSyncSecs = 0;
QFile file(filePath);
if (!file.exists()) {
@ -39,6 +40,7 @@ bool DeckSyncSidecar::load()
}
const QJsonValue modeValue = document.object().value("mode");
mode = modeValue.isString() ? modeValue.toString() : MODE_LINKED;
lastSyncSecs = static_cast<qint64>(document.object().value("lastSync").toDouble());
const QJsonObject records = document.object().value("records").toObject();
for (auto it = records.begin(); it != records.end(); ++it) {
if (!it.value().isObject()) {
@ -70,6 +72,7 @@ bool DeckSyncSidecar::save() const
QJsonObject root;
root.insert("version", SIDECAR_FORMAT_VERSION);
root.insert("mode", mode);
root.insert("lastSync", static_cast<double>(lastSyncSecs));
root.insert("records", records);
QDir().mkpath(QFileInfo(filePath).absolutePath());

View file

@ -40,6 +40,7 @@ public:
/// keeps it out of sync, and the first-sync verdicts narrow it to one
/// direction. An unrecognised value falls back to the two-way sync.
QString mode = MODE_LINKED;
qint64 lastSyncSecs = 0; ///< Last completed sync, seconds since epoch; 0 means never.
explicit DeckSyncSidecar(const QString &_filePath);

View file

@ -373,9 +373,22 @@ GeneralSettingsPage::GeneralSettingsPage()
SettingsCache::instance().deckSync().setConflictDefault(conflictDefaultSelector.itemData(index).toInt());
});
autoSyncCheckBox.setChecked(settings.deckSync().getAutoSync());
connect(&autoSyncCheckBox, &QCheckBox::toggled, &SettingsCache::instance().deckSync(),
&DeckSyncSettings::setAutoSync);
syncIntervalEdit.setMinimum(1);
syncIntervalEdit.setMaximum(60);
syncIntervalEdit.setValue(settings.deckSync().getSyncIntervalMinutes());
connect(&syncIntervalEdit, qOverload<int>(&QSpinBox::valueChanged), &SettingsCache::instance().deckSync(),
&DeckSyncSettings::setSyncIntervalMinutes);
auto *deckSyncGrid = new QGridLayout;
deckSyncGrid->addWidget(&conflictDefaultLabel, 0, 0);
deckSyncGrid->addWidget(&conflictDefaultSelector, 0, 1);
deckSyncGrid->addWidget(&autoSyncCheckBox, 1, 0, 1, 2);
deckSyncGrid->addWidget(&syncIntervalLabel, 2, 0);
deckSyncGrid->addWidget(&syncIntervalEdit, 2, 1);
deckSyncGroupBox = new QGroupBox;
deckSyncGroupBox->setLayout(deckSyncGrid);
@ -676,5 +689,8 @@ void GeneralSettingsPage::retranslateUi()
tr("Keep the server deck"));
conflictDefaultSelector.setItemText(conflictDefaultSelector.findData(DeckSyncConflictKeepNewer),
tr("Keep the newer deck"));
autoSyncCheckBox.setText(tr("Sync decks automatically"));
syncIntervalLabel.setText(tr("Check every:"));
syncIntervalEdit.setSuffix(" " + tr("min"));
resetAllPathsButton->setText(tr("Reset all paths"));
}

View file

@ -108,6 +108,9 @@ private:
QLabel conflictDefaultLabel;
QComboBox conflictDefaultSelector;
QCheckBox autoSyncCheckBox;
QLabel syncIntervalLabel;
QSpinBox syncIntervalEdit;
};
#endif // COCKATRICE_GENERAL_SETTINGS_PAGE_H

View file

@ -267,8 +267,14 @@ TabDeckStorage::TabDeckStorage(TabSupervisor *_tabSupervisor,
syncManager = new DeckSyncManager(
client, [](const DeckList &deck) { return getDeckColorIdentity(deck, CardDatabaseManager::query()); }, this);
syncManager->setConflictDefault(SettingsCache::instance().deckSync().getConflictDefault());
syncManager->setAutoSyncEnabled(SettingsCache::instance().deckSync().getAutoSync());
syncManager->setSyncIntervalMinutes(SettingsCache::instance().deckSync().getSyncIntervalMinutes());
connect(&SettingsCache::instance().deckSync(), &DeckSyncSettings::conflictDefaultChanged, syncManager,
&DeckSyncManager::setConflictDefault);
connect(&SettingsCache::instance().deckSync(), &DeckSyncSettings::autoSyncChanged, syncManager,
&DeckSyncManager::setAutoSyncEnabled);
connect(&SettingsCache::instance().deckSync(), &DeckSyncSettings::syncIntervalMinutesChanged, syncManager,
&DeckSyncManager::setSyncIntervalMinutes);
connect(syncManager, &DeckSyncManager::statusMessage, this, &TabDeckStorage::onSyncStatusMessage);
connect(syncManager, &DeckSyncManager::syncFinished, this, &TabDeckStorage::onSyncFinished);
connect(syncManager, &DeckSyncManager::firstSyncDetected, this, &TabDeckStorage::onFirstSyncDetected);
@ -276,6 +282,7 @@ TabDeckStorage::TabDeckStorage(TabSupervisor *_tabSupervisor,
if (currentUserInfo != nullptr) {
syncUsername = QString::fromStdString(currentUserInfo->name());
}
syncManager->setServerIdentity(client->serverName(), client->serverPort(), syncUsername);
retranslateUi();
@ -336,6 +343,7 @@ QString TabDeckStorage::getTargetPath() const
void TabDeckStorage::handleConnected(const ServerInfo_User &userInfo)
{
syncUsername = QString::fromStdString(userInfo.name());
syncManager->setServerIdentity(client->serverName(), client->serverPort(), syncUsername);
setRemoteEnabled(userInfo.user_level() & ServerInfo_User::IsRegistered);
}
@ -568,8 +576,6 @@ void TabDeckStorage::actOpenDecksFolder()
void TabDeckStorage::actSyncNow()
{
DeckSyncProfile profile(client->serverName(), client->serverPort(), syncUsername);
syncManager->setProfile(profile);
syncManager->setDeckPath(SettingsCache::instance().paths().getDeckPath());
aSyncNow->setEnabled(false);
aSyncReset->setEnabled(false);