|
| 1 | +// SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include "util/Backup.hpp" |
| 6 | + |
| 7 | +#include "common/QLogging.hpp" |
| 8 | +#include "util/Expected.hpp" |
| 9 | +#include "util/FilesystemHelpers.hpp" |
| 10 | +#include "widgets/dialogs/RestoreBackupsDialog.hpp" |
| 11 | + |
| 12 | +#include <pajlada/settings/settingmanager.hpp> |
| 13 | +#include <QDir> |
| 14 | +#include <QRegularExpression> |
| 15 | + |
| 16 | +#include <algorithm> |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +QRegularExpression regexForFile(const QString &file) |
| 21 | +{ |
| 22 | + return QRegularExpression( |
| 23 | + QStringView(u"^%1\\.bkp-\\d+$").arg(QRegularExpression::escape(file))); |
| 24 | +} |
| 25 | + |
| 26 | +bool anyBackupsOf(const QString &directory, const QString &filename) |
| 27 | +{ |
| 28 | + QDir fileDir(directory); |
| 29 | + if (!fileDir.exists()) |
| 30 | + { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + auto regex = regexForFile(filename); |
| 35 | + return std::ranges::any_of(fileDir.entryList(QDir::Files), |
| 36 | + [&](const auto &entry) { |
| 37 | + return regex.match(entry).hasMatch(); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace |
| 42 | + |
| 43 | +namespace chatterino::backup { |
| 44 | + |
| 45 | +std::vector<BackupFile> findBackupsFor(const QString &directory, |
| 46 | + const QString &filename) |
| 47 | +{ |
| 48 | + QDir fileDir(directory); |
| 49 | + if (!fileDir.exists()) |
| 50 | + { |
| 51 | + return {}; |
| 52 | + } |
| 53 | + auto dst = qStringToStdPath(fileDir.filePath(filename)); |
| 54 | + |
| 55 | + auto regex = regexForFile(filename); |
| 56 | + std::vector<BackupFile> backups; |
| 57 | + const auto entries = fileDir.entryInfoList(QDir::Files, QDir::Time); |
| 58 | + auto testSM = pajlada::Settings::SettingManager(); |
| 59 | + testSM.saveMethod = |
| 60 | + pajlada::Settings::SettingManager::SaveMethod::SaveManually; |
| 61 | + |
| 62 | + for (const auto &entry : entries) |
| 63 | + { |
| 64 | + if (!regex.match(entry.fileName()).hasMatch()) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + auto canonicalPath = entry.filesystemCanonicalFilePath(); |
| 69 | + |
| 70 | + BackupState state = BackupState::UnableToRead; |
| 71 | + using LoadError = pajlada::Settings::SettingManager::LoadError; |
| 72 | + |
| 73 | + auto res = testSM.loadFrom(canonicalPath); |
| 74 | + switch (res) |
| 75 | + { |
| 76 | + case LoadError::NoError: |
| 77 | + state = BackupState::Ok; |
| 78 | + break; |
| 79 | + |
| 80 | + case LoadError::CannotOpenFile: |
| 81 | + case LoadError::FileHandleError: |
| 82 | + case LoadError::FileReadError: |
| 83 | + case LoadError::FileSeekError: |
| 84 | + state = BackupState::UnableToRead; |
| 85 | + break; |
| 86 | + |
| 87 | + case LoadError::JSONParseError: |
| 88 | + state = BackupState::BadContents; |
| 89 | + break; |
| 90 | + |
| 91 | + case LoadError::SavingFromTemporaryFileFailed: |
| 92 | + // should never happen, temporary file loading/saving is not enabled |
| 93 | + assert(false); |
| 94 | + break; |
| 95 | + } |
| 96 | + |
| 97 | + backups.emplace_back(BackupFile{ |
| 98 | + .path = canonicalPath, |
| 99 | + .dstPath = dst, |
| 100 | + .lastModified = entry.lastModified(), |
| 101 | + .fileSize = entry.size(), |
| 102 | + .state = state, |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + return backups; |
| 107 | +} |
| 108 | + |
| 109 | +void loadWithBackups(const FileData &fileData, |
| 110 | + const std::function<ExpectedStr<void>()> &load) |
| 111 | +{ |
| 112 | + while (true) |
| 113 | + { |
| 114 | + auto loadResult = load(); |
| 115 | + if (loadResult) |
| 116 | + { |
| 117 | + return; |
| 118 | + } |
| 119 | + qCDebug(chatterinoSettings) |
| 120 | + << fileData.fileKind << "failed to load:" << loadResult.error(); |
| 121 | + |
| 122 | + if (!anyBackupsOf(fileData.directory, fileData.fileName)) |
| 123 | + { |
| 124 | + qCDebug(chatterinoSettings) |
| 125 | + << "No backups for" << fileData.fileKind; |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + auto *diag = new RestoreBackupsDialog(fileData, loadResult.error()); |
| 130 | + auto ret = diag->exec(); // we need to use exec here to block |
| 131 | + if (ret != QDialog::Accepted) |
| 132 | + { |
| 133 | + return; // rejected -> don't retry |
| 134 | + } |
| 135 | + |
| 136 | + qCDebug(chatterinoSettings) << "Retrying to load" << fileData.fileKind; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace chatterino::backup |
0 commit comments