Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make account saving as atomic as possible #11413

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/gui/folder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,6 @@ bool Folder::isDeployed() const

void Folder::saveToSettings() const
{
// Remove first to make sure we don't get duplicates
removeFromSettings();

auto settings = _accountState->settings();
settings->beginGroup(QStringLiteral("Folders"));

auto definitionToSave = _definition;

// migration
Expand All @@ -813,27 +807,31 @@ void Folder::saveToSettings() const
// we save the dav url nevertheless to have it available during startup
definitionToSave.setWebDavUrl(webDavUrl());

// Note: Each of these groups might have a "version" tag, but that's
// currently unused.
settings->beginGroup(QString::fromUtf8(definitionToSave.id()));
FolderDefinition::save(*settings, definitionToSave);
// keep the scope of the interaction with the settings limited to prevent possible loss of settings
[definitionToSave, settings = _accountState->settings(), id = QString::fromUtf8(definitionToSave.id())] {
// Remove first to make sure we don't get duplicates
removeFromSettings(settings.get(), id);

settings->sync();
qCInfo(lcFolder) << "Saved folder" << definitionToSave.localPath() << "to settings, status" << settings->status();
settings->beginGroup(QStringLiteral("Folders/%1").arg(id));
// Note: Each of these groups might have a "version" tag, but that's
// currently unused.
FolderDefinition::save(*settings.get(), definitionToSave);

settings->sync();
qCInfo(lcFolder) << "Saved folder" << definitionToSave.localPath() << "to settings, status" << settings->status();
}();
}

void Folder::removeFromSettings(QSettings *settings, const QString &id)
{
settings->remove(QStringLiteral("Folders/%1").arg(id));
settings->remove(QStringLiteral("Multifolders/%1").arg(id));
settings->remove(QStringLiteral("FoldersWithPlaceholders/%1").arg(id));
}

void Folder::removeFromSettings() const
{
auto settings = _accountState->settings();
const QString id = QString::fromUtf8(_definition.id());
settings->beginGroup(QStringLiteral("Folders"));
settings->remove(id);
settings->endGroup();
settings->beginGroup(QStringLiteral("Multifolders"));
settings->remove(id);
settings->endGroup();
settings->beginGroup(QStringLiteral("FoldersWithPlaceholders"));
settings->remove(id);
Folder::removeFromSettings(_accountState->settings().get(), QString::fromUtf8(_definition.id()));
}

bool Folder::isFileExcludedAbsolute(const QString &fullPath) const
Expand Down
1 change: 1 addition & 0 deletions src/gui/folder.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class Folder : public QObject
/// Saves the folder data in the account's settings.
void saveToSettings() const;
/// Removes the folder from the account's settings.
static void removeFromSettings(QSettings *settings, const QString &id);
void removeFromSettings() const;

/**
Expand Down