1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "profilesettingwidget.h"
6#include "common/common.h"
7
8#include <QBoxLayout>
9#include <QLabel>
10#include <QComboBox>
11#include <QPushButton>
12
13class ProfileSettingWidgetPrivate
14{
15 friend class ProfileSettingWidget;
16 QVBoxLayout *vLayout = nullptr;
17 QHBoxLayout *hlayout = nullptr;
18 QLabel *languageEdit = nullptr;
19 QComboBox *cbChooseLanguage = nullptr;
20 LanguagePaths languagePaths;
21};
22
23ProfileSettingWidget::ProfileSettingWidget(QWidget *parent)
24 : PageWidget(parent)
25 , d(new ProfileSettingWidgetPrivate)
26{
27 readTranslate();
28 setupUi();
29 readConfig();
30}
31
32ProfileSettingWidget::~ProfileSettingWidget()
33{
34 if(d)
35 delete d;
36}
37
38QString ProfileSettingWidget::translateFilePath()
39{
40 return CustomPaths::global(CustomPaths::Flags::Configures)
41 + QDir::separator() + QString("translate.support");
42}
43
44QString ProfileSettingWidget::languageFilePath()
45{
46 return CustomPaths::user(CustomPaths::Flags::Configures)
47 + QDir::separator() + QString("chooselanguage.support");
48}
49
50const LanguagePaths &ProfileSettingWidget::getLanguagePaths() const
51{
52 return d->languagePaths;
53}
54
55void ProfileSettingWidget::saveConfig()
56{
57 QFile file(languageFilePath());
58 QTextStream txtInput(&file);
59 QString chooseFileName = d->languagePaths.value(d->cbChooseLanguage->currentText());
60 QString currentFileName;
61 if (file.open(QIODevice::ReadOnly)) {
62 currentFileName = txtInput.readLine();
63 file.close();
64 }
65 if (chooseFileName == currentFileName) {
66 return;
67 }
68
69 if (file.open(QFile::WriteOnly)) {
70
71 file.write(chooseFileName.toUtf8());
72 file.close();
73 }
74 QMessageBox msgBox;
75 QPushButton *okButton = new QPushButton(tr("Ok"));
76 msgBox.addButton(okButton, QMessageBox::ButtonRole::NoRole);
77 msgBox.setWindowTitle(tr("Restart Required--deep-in unioncode"));
78 msgBox.setText(tr("The language change will take effect after restart."));
79 msgBox.exec();
80}
81
82void ProfileSettingWidget::readConfig()
83{
84 QFile file(languageFilePath());
85 QTextStream txtInput(&file);
86 QString fileName;
87 if (file.open(QIODevice::ReadOnly)) {
88 fileName = txtInput.readLine();
89 file.close();
90 }
91 d->cbChooseLanguage->setCurrentIndex(0);
92 for (int i = 0; i < d->cbChooseLanguage->count(); i++) {
93 if (d->languagePaths.value(d->cbChooseLanguage->itemText(i)) == fileName) {
94 d->cbChooseLanguage->setCurrentIndex(i);
95 break;
96 }
97 }
98}
99
100void ProfileSettingWidget::setupUi()
101{
102 if (!d->vLayout)
103 d->vLayout = new QVBoxLayout();
104 this->setLayout(d->vLayout);
105
106 if (!d->hlayout)
107 d->hlayout = new QHBoxLayout();
108
109 if (!d->languageEdit) {
110 d->languageEdit = new QLabel(tr("language:"));
111 }
112
113 if (!d->cbChooseLanguage)
114 d->cbChooseLanguage = new QComboBox();
115 d->cbChooseLanguage->setFixedWidth(200);
116
117 QStringList nameList = d->languagePaths.keys();
118 int i = 0;
119 for (auto name : nameList) {
120 d->cbChooseLanguage->insertItem(i, name);
121 i++;
122 }
123
124 d->hlayout->setMargin(10);
125 d->hlayout->setSpacing(10);
126 d->hlayout->addWidget(d->languageEdit);
127 d->hlayout->addWidget(d->cbChooseLanguage, 5, Qt::AlignmentFlag::AlignLeft);
128
129 d->vLayout->setAlignment(Qt::AlignmentFlag::AlignTop);
130 d->vLayout->addLayout(d->hlayout);
131}
132
133void ProfileSettingWidget::readTranslate()
134{
135 QFile file(translateFilePath());
136 if (file.open(QIODevice::ReadOnly)) {
137 QByteArray data = file.readAll();
138 file.close();
139
140 QJsonDocument doc = QJsonDocument::fromJson(data);
141 QJsonArray array = doc.array();
142 for (auto node : array) {
143 auto obj = node.toObject();
144 QJsonValue nameVal = obj.value(lNameItem);
145 QString name = nameVal.toString();
146
147 QJsonValue pathVal = obj.value(lPathItem);
148 QString path = pathVal.toString();
149 d->languagePaths.insert(name, path);
150 }
151 }
152}
153