-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AppAttributeUpgradeUnit to upgrade unit list
Add AppAttributeUpgradeUnit to handle application attribute upgrades. This change: - Includes the appattributeupgradeunit.h header - Registers AppAttributeUpgradeUnit in the upgrade units list for execution Log: add upgrade unit for app attribute config
- Loading branch information
Showing
3 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#include "appattributeupgradeunit.h" | ||
|
||
#include <dfm-base/dfm_log_defines.h> | ||
|
||
#include <QFile> | ||
#include <QDir> | ||
#include <QJsonDocument> | ||
#include <QStandardPaths> | ||
#include <QJsonArray> | ||
|
||
Q_DECLARE_LOGGING_CATEGORY(logToolUpgrade) | ||
|
||
using namespace dfm_upgrade; | ||
|
||
static constexpr char kConfigGroupAppAttribute[] { "ApplicationAttribute" }; | ||
static constexpr char kConfigKeyIconSizeLevel[] { "IconSizeLevel" }; | ||
static constexpr char kAppAttributeVersion[] { "v1.0" }; | ||
static constexpr int kOldMaxIconSizeLevel { 4 }; | ||
|
||
static QString kConfigurationPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first() + "/deepin/dde-file-manager/dde-file-manager.json"; | ||
static QString kBackupDirPath = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).first() + "/deepin/dde-file-manager/old"; | ||
|
||
AppAttributeUpgradeUnit::AppAttributeUpgradeUnit() | ||
Check warning on line 27 in src/tools/upgrade/units/appattributeupgradeunit.cpp GitHub Actions / cppcheck
|
||
: UpgradeUnit() | ||
{ | ||
} | ||
|
||
QString AppAttributeUpgradeUnit::name() | ||
{ | ||
return "AppAttributeUpgradeUnit"; | ||
} | ||
|
||
bool AppAttributeUpgradeUnit::initialize(const QMap<QString, QString> &args) | ||
{ | ||
Q_UNUSED(args) | ||
|
||
QFile file(kConfigurationPath); | ||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
return false; | ||
|
||
if (!backupAppAttribute()) | ||
return false; | ||
|
||
QByteArray data = file.readAll(); | ||
file.close(); | ||
QJsonDocument doc = QJsonDocument::fromJson(data); | ||
configObject = doc.object(); | ||
|
||
if (!configObject.contains(kConfigGroupAppAttribute)) | ||
return false; | ||
|
||
auto appAttribute = configObject.value(kConfigGroupAppAttribute).toObject(); | ||
if (!appAttribute.keys().contains(kConfigKeyIconSizeLevel)) | ||
return false; | ||
|
||
oldIconSizeLevel = appAttribute.value(kConfigKeyIconSizeLevel).toInt(); | ||
|
||
if (oldIconSizeLevel < 0) { | ||
qCWarning(logToolUpgrade) << "upgrade: iconSizeLevel is invalid: " << oldIconSizeLevel; | ||
return false; | ||
} | ||
|
||
if (oldIconSizeLevel > kOldMaxIconSizeLevel) { | ||
qCWarning(logToolUpgrade) << "upgrade: iconSizeLevel is already have new value."; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool AppAttributeUpgradeUnit::upgrade() | ||
{ | ||
int newIconSizeLevel = transIconSizeLevel(oldIconSizeLevel); | ||
auto appAttr = configObject[kConfigGroupAppAttribute].toObject(); | ||
appAttr[kConfigKeyIconSizeLevel] = newIconSizeLevel; | ||
configObject[kConfigGroupAppAttribute] = appAttr; | ||
|
||
return writeConfigFile(); | ||
} | ||
|
||
void AppAttributeUpgradeUnit::completed() | ||
{ | ||
qCInfo(logToolUpgrade) << "application attribute upgrade completed."; | ||
} | ||
|
||
int AppAttributeUpgradeUnit::transIconSizeLevel(int oldIconSizeLevel) const | ||
{ | ||
switch (oldIconSizeLevel) { | ||
case 0: | ||
return 3; | ||
case 1: | ||
return 5; | ||
case 2: | ||
return 9; | ||
case 3: | ||
return 13; | ||
case 4: | ||
return 17; | ||
default: | ||
return oldIconSizeLevel; | ||
} | ||
} | ||
|
||
bool AppAttributeUpgradeUnit::backupAppAttribute() const | ||
{ | ||
QDir backupDir(kBackupDirPath); | ||
if (!backupDir.exists()) { | ||
if (!backupDir.mkpath(".")) | ||
return false; | ||
} | ||
|
||
QString backupFilePath { kBackupDirPath + "/" + kConfigGroupAppAttribute + "_" + kAppAttributeVersion + ".backup" }; | ||
if (QFile::exists(backupFilePath)) | ||
return false; | ||
|
||
if (!QFile::copy(kConfigurationPath, backupFilePath)) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
bool AppAttributeUpgradeUnit::writeConfigFile() const | ||
{ | ||
QJsonDocument newDoc(configObject); | ||
QByteArray data = newDoc.toJson(); | ||
|
||
QFile writeFile(kConfigurationPath); | ||
if (!writeFile.open(QIODevice::WriteOnly | QIODevice::Text)) { | ||
qCWarning(logToolUpgrade) << "upgrade: open file failed: " << kConfigurationPath; | ||
return false; | ||
} | ||
|
||
writeFile.write(data); | ||
writeFile.close(); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#ifndef APPATTRIBUTEUPGRADEUNIT_H | ||
#define APPATTRIBUTEUPGRADEUNIT_H | ||
|
||
#include "core/upgradeunit.h" | ||
|
||
#include <QJsonObject> | ||
|
||
namespace dfm_upgrade { | ||
|
||
class AppAttributeUpgradeUnit : public UpgradeUnit | ||
{ | ||
public: | ||
AppAttributeUpgradeUnit(); | ||
|
||
QString name() override; | ||
bool initialize(const QMap<QString, QString> &args) override; | ||
bool upgrade() override; | ||
void completed() override; | ||
|
||
private: | ||
int transIconSizeLevel(int oldIconSizeLevel) const; | ||
bool backupAppAttribute() const; | ||
bool writeConfigFile() const; | ||
|
||
private: | ||
QJsonObject configObject; | ||
int oldIconSizeLevel; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters