Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [settings] migrate config storage to unified settings system #991

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -83,33 +84,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()
Q_DECL_DEPRECATED_X("-------------存在兼容代码需要删除") 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 @@ -565,7 +570,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 @@ -580,7 +586,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 @@ -594,7 +600,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 @@
+ QDir::separator() + QString("translate.support");
}

QString ProfileSettingWidget::languageFilePath()
Q_DECL_DEPRECATED_X("-------------存在兼容代码需要删除") QString ProfileSettingWidget::languageFilePath()

Check warning on line 53 in src/plugins/option/optioncore/mainframe/profilesettingwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_DECL_DEPRECATED_X is a macro then please configure it.
{
return CustomPaths::user(CustomPaths::Flags::Configures)
+ QDir::separator() + QString("chooselanguage.support");
Expand All @@ -57,23 +63,14 @@

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::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
Loading