|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +#include <QLoggingCategory> |
| 7 | + |
| 8 | +#include "migration.h" |
| 9 | +#include "theme.h" |
| 10 | +#include "configfile.h" |
| 11 | +#include "version.h" |
| 12 | + |
| 13 | +namespace OCC { |
| 14 | + |
| 15 | +Q_LOGGING_CATEGORY(lcMigration, "nextcloud.settings.migration", QtInfoMsg) |
| 16 | + |
| 17 | +Migration::Migration() |
| 18 | +{ |
| 19 | + _migrationPhase = MigrationPhase::NotStarted; |
| 20 | + _migrationType = MigrationType::UnbrandedToUnbranded; |
| 21 | + _versionChangeType = VersionChangeType::NoVersionChange; |
| 22 | +} |
| 23 | + |
| 24 | +QVersionNumber Migration::currentVersion() const |
| 25 | +{ |
| 26 | + return QVersionNumber::fromString(MIRALL_VERSION_STRING); |
| 27 | +} |
| 28 | + |
| 29 | +QVersionNumber Migration::previousVersion() const |
| 30 | +{ |
| 31 | + return QVersionNumber::fromString(ConfigFile().clientPreviousVersionString()); |
| 32 | +} |
| 33 | + |
| 34 | +QVersionNumber Migration::configVersion() const |
| 35 | +{ |
| 36 | + return QVersionNumber::fromString(ConfigFile().clientVersionString()); |
| 37 | +} |
| 38 | + |
| 39 | +void Migration::setMigrationPhase(const MigrationPhase phase) |
| 40 | +{ |
| 41 | + // do not rollback |
| 42 | + if (phase > _migrationPhase) { |
| 43 | + _migrationPhase = phase; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +Migration::MigrationPhase Migration::migrationPhase() const |
| 48 | +{ |
| 49 | + return _migrationPhase; |
| 50 | +} |
| 51 | + |
| 52 | +void Migration::setMigrationType(const MigrationType type) |
| 53 | +{ |
| 54 | + _migrationType = type; |
| 55 | +} |
| 56 | + |
| 57 | +Migration::MigrationType Migration::migrationType() const |
| 58 | +{ |
| 59 | + return _migrationType; |
| 60 | +} |
| 61 | + |
| 62 | +Migration::VersionChangeType Migration::versionChangeType() const |
| 63 | +{ |
| 64 | + return _versionChangeType; |
| 65 | +} |
| 66 | + |
| 67 | +void Migration::setVersionChangeType(const VersionChangeType type) |
| 68 | +{ |
| 69 | + _versionChangeType = type; |
| 70 | +} |
| 71 | + |
| 72 | +bool Migration::isUpgrade() |
| 73 | +{ |
| 74 | + const auto isUpgrade = currentVersion() > previousVersion(); |
| 75 | + if (isUpgrade) { |
| 76 | + setVersionChangeType(VersionChangeType::Upgrade); |
| 77 | + } |
| 78 | + return versionChangeType() == VersionChangeType::Upgrade; |
| 79 | +} |
| 80 | + |
| 81 | +bool Migration::isDowngrade() |
| 82 | +{ |
| 83 | + const auto isDowngrade = previousVersion() > currentVersion(); |
| 84 | + if (isDowngrade) { |
| 85 | + setVersionChangeType(VersionChangeType::Downgrade); |
| 86 | + } |
| 87 | + return versionChangeType() == VersionChangeType::Downgrade; |
| 88 | +} |
| 89 | + |
| 90 | +bool Migration::versionChanged() |
| 91 | +{ |
| 92 | + return isUpgrade() || isDowngrade(); |
| 93 | +} |
| 94 | + |
| 95 | +bool Migration::shouldTryUnbrandedToBrandedMigration() const |
| 96 | +{ |
| 97 | + const auto isUnbrandedToBranded = migrationPhase() == Migration::MigrationPhase::SetupFolders |
| 98 | + && Theme::instance()->appName() != ConfigFile::unbrandedAppName; |
| 99 | + if (isUnbrandedToBranded) { |
| 100 | + Migration().setMigrationType(MigrationType::UnbrandedToBranded); |
| 101 | + } |
| 102 | + return migrationType() == MigrationType::UnbrandedToBranded; |
| 103 | +} |
| 104 | + |
| 105 | +bool Migration::isUnbrandedToBrandedMigration() const |
| 106 | +{ |
| 107 | + return isInProgress() && migrationType() == MigrationType::UnbrandedToBranded; |
| 108 | +} |
| 109 | + |
| 110 | +bool Migration::shouldTryToMigrate() |
| 111 | +{ |
| 112 | + return !isClientVersionSet() && (isUpgrade() || isDowngrade()); |
| 113 | +} |
| 114 | + |
| 115 | +bool Migration::isClientVersionSet() const |
| 116 | +{ |
| 117 | + const auto configVersionNumber = configVersion(); |
| 118 | + const auto previousVersionNumber = previousVersion(); |
| 119 | + return !configVersionNumber.isNull() && !previousVersionNumber.isNull() |
| 120 | + && configVersionNumber == previousVersionNumber; |
| 121 | +} |
| 122 | + |
| 123 | +bool Migration::isInProgress() const |
| 124 | +{ |
| 125 | + const auto currentPhase = migrationPhase(); |
| 126 | + return currentPhase != MigrationPhase::NotStarted |
| 127 | + && currentPhase != MigrationPhase::Done; |
| 128 | +} |
| 129 | + |
| 130 | +} |
0 commit comments