Skip to content

Commit

Permalink
refactor: [settings] migrate config storage to unified settings system
Browse files Browse the repository at this point in the history
Refactored configuration storage mechanism across multiple components to use
the unified settings system:

- Migrated language settings from file-based to settings-based storage
- Updated CodeGeeX configuration to use OptionManager
- Refactored profile settings to use centralized storage
- Added compatibility code for smooth transition from old storage format
- Cleaned up deprecated file-based storage methods

The changes improve configuration management consistency and maintainability
by centralizing all settings into a single system.

Log: refactored configuration storage to use unified settings system
  • Loading branch information
Kakueeen committed Nov 14, 2024
1 parent 7b1ba25 commit deee5c8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 90 deletions.
46 changes: 19 additions & 27 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "common/common.h"
#include "common/settings/settings.h"

#include <framework/framework.h>
#include <framework/lifecycle/pluginsetting.h>
Expand Down Expand Up @@ -32,7 +33,7 @@ static bool loadPlugins()

QString pluginsPath = CustomPaths::global(CustomPaths::Plugins);
qInfo() << QString("run application in %0").arg(pluginsPath);
lifeCycle.setPluginPaths({pluginsPath});
lifeCycle.setPluginPaths({ pluginsPath });

qInfo() << "Depend library paths:" << QApplication::libraryPaths();
qInfo() << "Load plugin paths: " << dpf::LifeCycle::pluginPaths();
Expand Down Expand Up @@ -66,27 +67,19 @@ void installTranslator(DApplication &a)
{
QTranslator *translator = new QTranslator(&a);

auto result = CustomPaths::endSeparator(CustomPaths::global(CustomPaths::Translations));
QFile file(CustomPaths::user(CustomPaths::Flags::Configures)
+ QDir::separator() + QString("chooselanguage.support"));

if (!file.exists()) {
if (file.open(QFile::ReadWrite)) {
QLocale locale;
QString fileName = locale.name() + ".qm";
a.loadTranslator(QList<QLocale>() << locale.system());
file.write(fileName.toUtf8());
file.close();
}
}
if (file.open(QFile::ReadOnly)) {
QTextStream txtInput(&file);
QString language = txtInput.readLine();
QString name = language.left(language.indexOf("."));
a.loadTranslator(QList<QLocale>() << QLocale(name));
file.close();
translator->load(result + language);
}
auto translationFileDir = CustomPaths::endSeparator(CustomPaths::global(CustomPaths::Translations));
QString configFile = CustomPaths::user(CustomPaths::Flags::Configures)
+ QDir::separator() + "deepin-unioncode.json";

Settings settings("", configFile);
auto map = settings.value("General", "Language").toMap();
QString language = map.value("path").toString();
if (language.isEmpty())
language = QLocale().name() + ".qm";

QString name = language.left(language.indexOf("."));
a.loadTranslator(QList<QLocale>() << QLocale(name));
translator->load(translationFileDir + language);
a.installTranslator(translator);
}

Expand All @@ -111,22 +104,21 @@ int main(int argc, char *argv[])
format.setRenderableType(QSurfaceFormat::OpenGLES);
format.setDefaultFormat(format);
}

QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
DApplication a(argc, argv);
installTranslator(a);

QString buildDateInfo = a.translate("Application", "<br/>Built on %1 %2 in %3<br/>")
.arg(QLatin1String(__DATE__), QLatin1String(__TIME__), ProcessUtil::localPlatform());
.arg(QLatin1String(__DATE__), QLatin1String(__TIME__), ProcessUtil::localPlatform());
a.setOrganizationName("deepin");
a.setApplicationDisplayName(a.translate("Application", "deepin-unioncode"));
a.setApplicationVersion(version());
a.setProductIcon(QIcon::fromTheme("about_logo"));
a.setApplicationDescription(a.translate("Application",
"Deepin Union Code is a lightweight integrated development environment,\
featured with multilingual and cross platform compatibility."
));
featured with multilingual and cross platform compatibility."));
CommandParser::instance().process();

// TODO(Any): put to command processor
Expand All @@ -152,7 +144,7 @@ int main(int argc, char *argv[])
if (!CommandParser::instance().projectDirectory().isEmpty()) {
auto directory = CommandParser::instance().projectDirectory().first(); // only process first argument
QObject::connect(&dpf::Listener::instance(), &dpf::Listener::pluginsStarted, &a, [directory, &a]() {
QTimer::singleShot(100, &a, [directory](){ openProject(directory); });
QTimer::singleShot(100, &a, [directory]() { openProject(directory); });
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void Settings::setValue(const QString &group, const QString &key, const QVariant
if (d->isRemovable(group, key)) {
changed = d->settingData.value(group, key) != value;
} else {
changed = this->value(group, key, value) != value;
changed = this->value(group, key) != value;
}

if (!changed)
Expand Down
54 changes: 30 additions & 24 deletions src/plugins/codegeex/codegeexmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "services/window/windowelement.h"
#include "services/terminal/terminalservice.h"
#include "services/project/projectservice.h"
#include "services/option/optionmanager.h"

#include <DSpinner>

Expand Down Expand Up @@ -84,33 +85,37 @@ bool CodeGeeXManager::condaHasInstalled()

void CodeGeeXManager::saveConfig(const QString &sessionId, const QString &userId)
{
QJsonObject config;
config["sessionId"] = sessionId;
config["userId"] = userId;
QVariantMap map { { "sessionId", sessionId },
{ "userId", userId } };

QJsonDocument document(config);

QFile file(configFilePath());
file.open(QIODevice::WriteOnly);
file.write(document.toJson());
file.close();
OptionManager::getInstance()->setValue("CodeGeeX", "Id", map);
}

void CodeGeeXManager::loadConfig()
[[deprecated("-------------存在兼容代码需要删除")]] void CodeGeeXManager::loadConfig()
{
QFile file(configFilePath());
if (!file.exists())
return;

file.open(QIODevice::ReadOnly);
QString data = QString::fromUtf8(file.readAll());
file.close();
if (!file.exists()) {
const auto map = OptionManager::getInstance()->getValue("CodeGeeX", "Id").toMap();
if (map.isEmpty())
return;

QJsonDocument document = QJsonDocument::fromJson(data.toUtf8());
QJsonObject config = document.object();
if (!config.empty()) {
sessionId = config["sessionId"].toString();
userId = config["userId"].toString();
sessionId = map.value("sessionId").toString();
userId = map.value("userId").toString();
} else {
// ------------------Deprecated start--------------------
file.open(QIODevice::ReadOnly);
QString data = QString::fromUtf8(file.readAll());
file.close();
file.remove();

QJsonDocument document = QJsonDocument::fromJson(data.toUtf8());
QJsonObject config = document.object();
if (!config.empty()) {
sessionId = config["sessionId"].toString();
userId = config["userId"].toString();
saveConfig(sessionId, userId);
}
// ------------------Deprecated end--------------------
}
}

Expand Down Expand Up @@ -566,7 +571,8 @@ void CodeGeeXManager::generateRag(const QString &projectPath)
QProcess *process = new QProcess;
QObject::connect(QApplication::instance(), &QApplication::aboutToQuit, process, [process]() {
process->kill();
}, Qt::DirectConnection);
},
Qt::DirectConnection);

QObject::connect(process, &QProcess::readyReadStandardError, process, [process]() {
qInfo() << "Error:" << process->readAllStandardError() << "\n";
Expand All @@ -581,7 +587,7 @@ void CodeGeeXManager::generateRag(const QString &projectPath)
auto success = process->readAllStandardError().isEmpty();
emit generateDone(projectPath, !success);
if (!success)
emit notify(2, tr("The error occurred when performing rag on project %1.").arg(projectPath));
emit notify(2, tr("The error occurred when performing rag on project %1.").arg(projectPath));
process->deleteLater();
});

Expand All @@ -595,7 +601,7 @@ void CodeGeeXManager::generateRag(const QString &projectPath)
if (!QFileInfo(pythonPath).exists())
return;
process->start(pythonPath, QStringList() << generatePyPath << modelPath << projectPath);
if (QThread::currentThread() != qApp->thread()) // incase thread exit before process done. cause slot function can`t work
if (QThread::currentThread() != qApp->thread()) // incase thread exit before process done. cause slot function can`t work
process->waitForFinished();
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/codegeex/option/codegeexoptionwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CodeGeeXOptionWidget::CodeGeeXOptionWidget(QWidget *parent)
d->tabWidget->setDocumentMode(true);
layout->addWidget(d->tabWidget);

d->tabWidget->addTab(new DetailWidget(), tr("CodeGeeX"));
d->tabWidget->addTab(new DetailWidget(), "Detail");
QObject::connect(d->tabWidget, &DTabWidget::currentChanged, [this]() {
readConfig();
});
Expand Down
16 changes: 6 additions & 10 deletions src/plugins/codegeex/option/detailwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,22 @@ void DetailWidget::setControlValue(const QMap<QString, QVariant> &map)

bool DetailWidget::dataToMap(const CodeGeeXConfig &config, QMap<QString, QVariant> &map)
{
QMap<QString, QVariant> apiKey;
apiKey.insert(kCodeCompletion, config.codeCompletionEnabled);
apiKey.insert(kGlobalLanguage, config.globalLanguage);
apiKey.insert(kCommitsLanguage, config.commitsLanguage);

map.insert("Detail", apiKey);
map.insert(kCodeCompletion, config.codeCompletionEnabled);
map.insert(kGlobalLanguage, config.globalLanguage);
map.insert(kCommitsLanguage, config.commitsLanguage);

return true;
}

bool DetailWidget::mapToData(const QMap<QString, QVariant> &map, CodeGeeXConfig &config)
{
QMap<QString, QVariant> detail = map.value("Detail").toMap();
auto var = detail.value(kCodeCompletion);
auto var = map.value(kCodeCompletion);
if (var.isValid())
config.codeCompletionEnabled = var.toBool();
var = detail.value(kGlobalLanguage);
var = map.value(kGlobalLanguage);
if (var.isValid())
config.globalLanguage = var.value<CodeGeeX::locale>();
var = detail.value(kCommitsLanguage);
var = map.value(kCommitsLanguage);
if (var.isValid())
config.commitsLanguage = var.value<CodeGeeX::locale>();

Expand Down
57 changes: 30 additions & 27 deletions src/plugins/option/optioncore/mainframe/profilesettingwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "profilesettingwidget.h"
#include "common/common.h"
#include "services/option/optionmanager.h"

#include <DLabel>
#include <DComboBox>
Expand All @@ -14,6 +15,11 @@

DWIDGET_USE_NAMESPACE

constexpr char kGeneralGroup[] { "General" };
constexpr char kLanguageKey[] { "Language" };
constexpr char kName[] { "name" };
constexpr char kPath[] { "path" };

class ProfileSettingWidgetPrivate
{
friend class ProfileSettingWidget;
Expand Down Expand Up @@ -44,7 +50,7 @@ QString ProfileSettingWidget::translateFilePath()
+ QDir::separator() + QString("translate.support");
}

QString ProfileSettingWidget::languageFilePath()
[[deprecated("-------------存在兼容代码需要删除")]] QString ProfileSettingWidget::languageFilePath()
{
return CustomPaths::user(CustomPaths::Flags::Configures)
+ QDir::separator() + QString("chooselanguage.support");
Expand All @@ -57,23 +63,14 @@ const LanguagePaths &ProfileSettingWidget::getLanguagePaths() const

void ProfileSettingWidget::saveConfig()
{
QFile file(languageFilePath());
QTextStream txtInput(&file);
QString chooseFileName = d->languagePaths.value(d->cbChooseLanguage->currentText());
QString currentFileName;
if (file.open(QIODevice::ReadOnly)) {
currentFileName = txtInput.readLine();
file.close();
}
if (chooseFileName == currentFileName) {
QVariantMap map = OptionManager::getInstance()->getValue(kGeneralGroup, kLanguageKey).toMap();
auto languageName = d->cbChooseLanguage->currentText();
if (map.value(kName) == languageName)
return;
}

if (file.open(QFile::WriteOnly)) {

file.write(chooseFileName.toUtf8());
file.close();
}
map.insert(kName, languageName);
map.insert(kPath, d->languagePaths.value(languageName));
OptionManager::getInstance()->setValue(kGeneralGroup, kLanguageKey, map);

DDialog msgBox;
msgBox.addButton(tr("Ok"));
Expand All @@ -84,20 +81,26 @@ void ProfileSettingWidget::saveConfig()

void ProfileSettingWidget::readConfig()
{
QString languageName;
QFile file(languageFilePath());
QTextStream txtInput(&file);
QString fileName;
if (file.open(QIODevice::ReadOnly)) {
fileName = txtInput.readLine();
file.close();
}
d->cbChooseLanguage->setCurrentIndex(0);
for (int i = 0; i < d->cbChooseLanguage->count(); i++) {
if (d->languagePaths.value(d->cbChooseLanguage->itemText(i)) == fileName) {
d->cbChooseLanguage->setCurrentIndex(i);
break;
if (file.exists()) {
QTextStream txtInput(&file);
if (file.open(QIODevice::ReadOnly)) {
languageName = d->languagePaths.key(txtInput.readLine());
file.close();
file.remove();
}
QVariantMap map;
map.insert(kName, languageName);
map.insert(kPath, d->languagePaths.value(languageName));

OptionManager::getInstance()->setValue(kGeneralGroup, kLanguageKey, map);
} else {
const auto &map = OptionManager::getInstance()->getValue(kGeneralGroup, kLanguageKey).toMap();
languageName = map.value(kName).toString();
}

d->cbChooseLanguage->setCurrentText(languageName);
}

void ProfileSettingWidget::setupUi()
Expand Down

0 comments on commit deee5c8

Please sign in to comment.