Skip to content

Commit

Permalink
feat: add AppAttributeUpgradeUnit to upgrade unit list
Browse files Browse the repository at this point in the history
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
Lighto-Ku committed Dec 3, 2024
1 parent a2f2bcd commit a427ac2
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 2 deletions.
141 changes: 141 additions & 0 deletions src/tools/upgrade/units/appattributeupgradeunit.cpp
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

View workflow job for this annotation

GitHub Actions / cppcheck

Member variable 'AppAttributeUpgradeUnit::oldIconSizeLevel' is not initialized in the constructor.

Check warning on line 27 in src/tools/upgrade/units/appattributeupgradeunit.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Member variable 'AppAttributeUpgradeUnit::oldIconSizeLevel' is not initialized in the constructor.
: 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;
}
36 changes: 36 additions & 0 deletions src/tools/upgrade/units/appattributeupgradeunit.h
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
5 changes: 3 additions & 2 deletions src/tools/upgrade/units/unitlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "smbvirtualentryupgradeunit.h"
#include "vaultupgradeunit.h"
#include "desktoporganizeupgradeunit.h"

#include "appattributeupgradeunit.h"
// units end

#include <QList>
Expand All @@ -35,7 +35,8 @@ inline QList<QSharedPointer<UpgradeUnit>> createUnits()
RegUnit(dfm_upgrade::TagDbUpgradeUnit),
RegUnit(dfm_upgrade::SmbVirtualEntryUpgradeUnit),
RegUnit(dfm_upgrade::VaultUpgradeUnit),
RegUnit(dfm_upgrade::DesktopOrganizeUpgradeUnit)
RegUnit(dfm_upgrade::DesktopOrganizeUpgradeUnit),
RegUnit(dfm_upgrade::AppAttributeUpgradeUnit)
};
}

Expand Down

0 comments on commit a427ac2

Please sign in to comment.