Debian Codegraph @

kysdk-qtwidgets/src/kaboutdialog.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kaboutdialog.h"
24 #include <QApplication>
25 #include <QBoxLayout>
26 #include <QDesktopServices>
27 #include <QDir>
28 #include <QGuiApplication>
29 #include <QIcon>
30 #include <QLabel>
31 #include <QLibraryInfo>
32 #include <QMessageBox>
33 #include <QMouseEvent>
34 #include <QPainter>
35 #include <QPixmap>
36 #include <QProcess>
37 #include <QSettings>
38 #include <QStyleOption>
39 #include <QTextDocument>
40 #include <QTextEdit>
41 #include <QTranslator>
42 #include "accessinfohelper.h"
43
44 namespace kdk
45 {
46 #define MAILTYPE "x-scheme-handler/mailto"
47 #define LOCAL_CONFIG_DIR "/.config/"
48 #define SYSTEM_CONFIG_DIR "/usr/share/applications/"
49
50 class LinkLabel : public QLabel
51 {
52 Q_OBJECT
53
54 public:
55 LinkLabel(QWidget *parent = nullptr);
56
57 protected:
58 void mousePressEvent(QMouseEvent *event);
59 };
60 class KAboutDialogPrivate : public QObject
61 {
62 Q_OBJECT
63 Q_DECLARE_PUBLIC(KAboutDialog)
64
65 public:
66 KAboutDialogPrivate(KAboutDialog *parent);
67 void adjustMiniMode();
68 QString getDefaultAppId(const char *contentType);
69 bool isOpenKylin();
70 void updateAppVersionText();
71 QString ConvertFirstLetterCase(const QString &input);
72 QStringList getSystemConfigDir();
73 void setAccessInfo();
74
75 private:
76 KAboutDialog *q_ptr;
77 QLabel *m_pAppIconLabel;
78 QLabel *m_pAppNameLabel;
79 QLabel *m_pAppVersionLabel;
80 QLabel *m_pAppSupportLabel;
81 LinkLabel *m_pAppPrivacyLabel;
82 QString m_iconName;
83 QString m_bodyText;
84 bool m_appVersionFlag;
85 };
86
87 KAboutDialog::KAboutDialog(QWidget *parent, const QIcon &appIcon, const QString &appName, const QString &appVersion, const QString &appInfo)
88 : KDialog(parent)
89 , d_ptr(new KAboutDialogPrivate(this))
90 {
91 Q_D(KAboutDialog);
92 setProperty("isAboutDialog", true);
93 mainLayout()->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
94 d->m_iconName = appIcon.name();
95 d->m_pAppIconLabel = new QLabel(this);
96 QPixmap pix(appIcon.pixmap(QSize(96, 96)));
97 d->m_pAppIconLabel->setPixmap(pix);
98 d->m_pAppIconLabel->setAlignment(Qt::AlignCenter);
99 d->m_pAppNameLabel = new QLabel(this);
100 d->m_pAppNameLabel->setText(d->ConvertFirstLetterCase(appName));
101 d->m_pAppNameLabel->setAlignment(Qt::AlignCenter);
102
103 if (!(appVersion == "") && (!(appVersion.isNull())))
104 d->m_appVersionFlag = false;
105 d->m_pAppVersionLabel = new QLabel(this);
106 d->m_pAppVersionLabel->setText(appVersion);
107 d->m_pAppVersionLabel->setAlignment(Qt::AlignCenter);
108 d->m_pAppVersionLabel->setFixedWidth(400);
109
110 d->m_pAppSupportLabel = new QLabel(this);
111 d->m_pAppSupportLabel->setMinimumWidth(400);
112
113 d->m_pAppPrivacyLabel = new LinkLabel(this);
114 d->m_pAppPrivacyLabel->setAlignment(Qt::AlignCenter);
115 d->m_pAppPrivacyLabel->setVisible(false);
116
117 d->adjustMiniMode();
118
119 connect(d->m_pAppSupportLabel, &QLabel::linkActivated, this, [=](const QString url) {
120 QString appid = d->getDefaultAppId(MAILTYPE);
121 if (appid.isEmpty()) {
122 QMessageBox msg(this);
123 msg.setIcon(QMessageBox::Icon::Information);
124 msg.setIconPixmap(QIcon::fromTheme("dialog-info").pixmap(24, 24));
125 msg.setInformativeText(tr("Your system does not have any email application installed or the default mail application is not set up"));
126 msg.setText(tr("Unable to open mail application"));
127 msg.exec();
128 } else
129 QDesktopServices::openUrl(QUrl(url));
130 });
131 changeTheme();
132 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
133 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
134 {
135 changeTheme();
136 }
137 });
138
139 QString qtTransPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
140
141 QTranslator *trans_qt = new QTranslator(this);
142 if (trans_qt->load(QLocale(), "qt", "_", qtTransPath))
143 qApp->installTranslator(trans_qt);
144
145 QTranslator *trans = new QTranslator(this);
146 if (trans->load(QString(":/translations/gui_%1.qm").arg(QLocale::system().name())))
147 qApp->installTranslator(trans);
148 if (d->isOpenKylin())
149 d->m_pAppSupportLabel->hide();
150
151 d->setAccessInfo();
152 }
153
154 KAboutDialog::~KAboutDialog()
155 {
156 }
157
158 void KAboutDialog::setAppIcon(const QIcon &icon)
159 {
160 Q_D(KAboutDialog);
161 d->m_iconName = icon.name();
162 QPixmap pix(icon.pixmap(QSize(96, 96)));
163 d->m_pAppIconLabel->setPixmap(pix);
164 d->m_pAppIconLabel->setAlignment(Qt::AlignCenter);
165 update();
166 }
167
168 void KAboutDialog::setAppName(const QString &appName)
169 {
170 Q_D(KAboutDialog);
171 d->m_pAppNameLabel->setText(d->ConvertFirstLetterCase(appName));
172 update();
173 }
174
175 QString KAboutDialog::appName()
176 {
177 Q_D(KAboutDialog);
178 return d->m_pAppNameLabel->text();
179 }
180
181 void KAboutDialog::setAppVersion(const QString &appVersion)
182 {
183 Q_D(KAboutDialog);
184 if (!appVersion.isNull()) {
185 d->m_appVersionFlag = false;
186 d->m_pAppVersionLabel->setText(appVersion);
187 update();
188 }
189 }
190
191 QString KAboutDialog::appVersion()
192 {
193 Q_D(KAboutDialog);
194 return d->m_pAppVersionLabel->text();
195 }
196
197 void KAboutDialog::setBodyText(const QString &bodyText)
198 {
199 Q_D(KAboutDialog);
200 d->m_bodyText = bodyText;
201 }
202
203 QString KAboutDialog::bodyText()
204 {
205 Q_D(KAboutDialog);
206 return d->m_bodyText;
207 }
208
209 void KAboutDialog::setAppSupport(const QString &appSupport)
210 {
211 Q_D(KAboutDialog);
212 d->m_pAppSupportLabel->setText(appSupport);
213 update();
214 }
215
216 QString KAboutDialog::appSupport()
217 {
218 Q_D(KAboutDialog);
219 return d->m_pAppSupportLabel->text();
220 }
221
222 void KAboutDialog::setBodyTextVisiable(bool flag)
223 {
224 Q_D(KAboutDialog);
225 // do nothing
226 }
227
228 void KAboutDialog::setAppPrivacyLabelVisible(bool flag)
229 {
230 Q_D(KAboutDialog);
231 d->m_pAppPrivacyLabel->setVisible(flag);
232 }
233
234 bool KAboutDialog::AppPrivacyLabelIsVisible()
235 {
236 Q_D(KAboutDialog);
237 return d->m_pAppPrivacyLabel->isVisible();
238 }
239
240 void KAboutDialog::changeTheme()
241 {
242 Q_D(KAboutDialog);
243 KDialog::changeTheme();
244
245 auto icon = QIcon::fromTheme(d->m_iconName);
246 if (!icon.isNull()) {
247 QPixmap pix(icon.pixmap(QSize(96, 96)));
248 d->m_pAppIconLabel->setPixmap(pix);
249 }
250 QPalette palette = qApp->palette();
251 QFont font;
252 font.setPixelSize(ThemeController::systemFontSize() * 1.7);
253 font.setWeight(QFont::Medium);
254 QColor fontColor = ThemeController::getCustomColorFromDT("kfont-primary");
255 if (fontColor.isValid()) {
256 palette.setColor(QPalette::Text, fontColor);
257 palette.setColor(QPalette::WindowText, fontColor);
258 d->m_pAppNameLabel->setPalette(palette);
259 }
260 d->m_pAppNameLabel->setFont(font);
261
262 QColor textColor = ThemeController::getCustomColorFromDT("kfont-secondary");
263 palette.setColor(QPalette::Text, textColor);
264 palette.setColor(QPalette::WindowText, textColor);
265 d->m_pAppSupportLabel->setPalette(palette);
266 d->m_pAppVersionLabel->setPalette(palette);
267
268 int r = textColor.red();
269 int g = textColor.green();
270 int b = textColor.blue();
271 float a = textColor.alphaF();
272 QString rgbaColor = QString("rgba(%1, %2, %3, %4)")
273 .arg(r).arg(g).arg(b).arg(a);
274
275 d->m_pAppSupportLabel->setText(tr("Service & Support: ") + QString("<a href=\"mailto://[email protected]\""
276 "style=\"color:%1\">"
277 "[email protected]</a>").arg(rgbaColor));
278 QColor privacyColor = ThemeController::getCustomColorFromDT("highlight-active");
279 palette.setColor(QPalette::WindowText, privacyColor);
280 d->m_pAppPrivacyLabel->setPalette(palette);
281 d->m_pAppPrivacyLabel->setText(tr("Privacy statement"));
282 }
283
284 void KAboutDialog::paintEvent(QPaintEvent *paintEvent)
285 {
286 Q_D(KAboutDialog);
287
288 if (d->m_appVersionFlag)
289 d->updateAppVersionText();
290 QStyleOption opt;
291 opt.initFrom(this);
292 QPainter p(this);
293 style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
294 }
295
296 KAboutDialogPrivate::KAboutDialogPrivate(KAboutDialog *parent)
297 : q_ptr(parent)
298 , m_appVersionFlag(true)
299 {
300 setParent(parent);
301 }
302
303 void KAboutDialogPrivate::adjustMiniMode()
304 {
305 Q_Q(KAboutDialog);
306 if (q->mainWidget()->layout())
307 delete q->mainWidget()->layout();
308 m_pAppSupportLabel->setAlignment(Qt::AlignCenter);
309 QVBoxLayout *vBoxLayout = new QVBoxLayout();
310 vBoxLayout->setSpacing(0);
311 vBoxLayout->addSpacing(20);
312 vBoxLayout->addWidget(m_pAppIconLabel);
313 vBoxLayout->addSpacing(24);
314 vBoxLayout->addWidget(m_pAppNameLabel);
315 vBoxLayout->addSpacing(16);
316 vBoxLayout->addWidget(m_pAppVersionLabel);
317 vBoxLayout->addSpacing(16);
318 vBoxLayout->addWidget(m_pAppSupportLabel);
319 vBoxLayout->addSpacing(16);
320 vBoxLayout->addWidget(m_pAppPrivacyLabel);
321 vBoxLayout->setContentsMargins(25, 0, 25, 0);
322 vBoxLayout->addSpacing(35);
323 q->mainWidget()->setLayout(vBoxLayout);
324 }
325
326 QString KAboutDialogPrivate::getDefaultAppId(const char *contentType)
327 {
328 QString localfile = QDir::homePath() + LOCAL_CONFIG_DIR + "mimeapps.list";
329 QString systemfile = SYSTEM_CONFIG_DIR + QString("ukui-mimeapps.list");
330 if (QFile(localfile).exists()) {
331 QSettings *mimeappFile = new QSettings(localfile, QSettings::IniFormat);
332 // mimeappFile->setIniCodec("utf-8");
333 QString str = mimeappFile->value(QString("Default Applications/%1").arg(contentType)).toString();
334 for(int i = 0; i <getSystemConfigDir().count(); i++)
335 {
336 QString filePath = getSystemConfigDir().at(i) + "/applications/" + str;
337 if (!str.isEmpty()) {
338 if (QFile(filePath).exists()) {
339 delete mimeappFile;
340 mimeappFile = nullptr;
341 return str;
342 }
343 }
344 }
345 delete mimeappFile;
346 mimeappFile = nullptr;
347 return QString("");
348 }
349 if (QFile(systemfile).exists()) {
350 QSettings *mimeappFile = new QSettings(systemfile, QSettings::IniFormat);
351 // mimeappFile->setIniCodec("utf-8");
352 QString str = mimeappFile->value(QString("Default Applications/%1").arg(contentType)).toString();
353
354 for(int i = 0; i <getSystemConfigDir().count(); i++)
355 {
356 QString filePath = getSystemConfigDir().at(i) + "/applications/" + str;
357 if (!str.isEmpty()) {
358 if (QFile(filePath).exists()) {
359 delete mimeappFile;
360 mimeappFile = nullptr;
361 return str;
362 }
363 }
364 }
365 delete mimeappFile;
366 mimeappFile = nullptr;
367 return QString("");
368 }
369 return QString("");
370 }
371
372 bool KAboutDialogPrivate::isOpenKylin()
373 {
374 QString filename = "/etc/os-release";
375 QSettings osSettings(filename, QSettings::IniFormat);
376
377 QString versionID = osSettings.value("ID").toString();
378
379 if (versionID.compare("openkylin", Qt::CaseInsensitive)) {
380 return false;
381 }
382 return true;
383 }
384
385 void KAboutDialogPrivate::updateAppVersionText()
386 {
387 Q_Q(KAboutDialog);
388 QProcess process;
389 if (QCoreApplication::applicationFilePath().contains(QApplication::applicationName())) {
390 QStringList list;
391 QString str("dpkg -l | grep " + QApplication::applicationName() + " | awk '{print $3}'");
392 list << "-c" << str;
393 process.start("/bin/bash", list);
394 process.waitForFinished();
395 QString result(tr("version :"));
396 QString string;
397 string = process.readAllStandardOutput().data();
398 if (string == "" || string.isNull()) {
399 result += QString(tr("Version number not found"));
400 } else {
401 result += string;
402 result.remove("\n"); // 去掉 \n 字符
403 }
404 m_pAppVersionLabel->setText(result);
405 }
406 process.close();
407 }
408
409 QString KAboutDialogPrivate::ConvertFirstLetterCase(const QString &input)
410 {
411 QStringList words;
412 if (input.contains('-')) {
413 words = input.split('-', Qt::SkipEmptyParts);
414 for (int i = 0; i < words.size(); ++i) {
415 QString &word = words[i];
416 word[0] = word[0].toUpper(); // 将每个单词的首字母转换为大写
417 }
418 } else {
419 words = input.split(' ', Qt::SkipEmptyParts);
420 for (int i = 0; i < words.size(); ++i) {
421 QString &word = words[i];
422 word[0] = word[0].toUpper(); // 将每个单词的首字母转换为大写
423 }
424 }
425 return words.join(" ");
426 }
427
428 QStringList KAboutDialogPrivate::getSystemConfigDir()
429 {
430 QString xdgDataDirs = QProcessEnvironment::systemEnvironment().value("XDG_DATA_DIRS");
431
432 if (xdgDataDirs.isEmpty()) {
433 qWarning() << "XDG_DATA_DIRS environment variable is not set!";
434 }
435
436 QStringList paths = xdgDataDirs.split(':', Qt::SkipEmptyParts);
437
438 for (QString &path : paths)
439 {
440 path = path.trimmed();
441 }
442 return paths;
443 }
444
445 void KAboutDialogPrivate::setAccessInfo()
446 {
447 Q_Q(KAboutDialog);
448 KDK_ALL_INFO_FORMAT(m_pAppIconLabel,"");
449 KDK_ALL_INFO_FORMAT(m_pAppNameLabel,"");
450 KDK_ALL_INFO_FORMAT(m_pAppVersionLabel,"");
451 KDK_ALL_INFO_FORMAT(m_pAppSupportLabel,"");
452 KDK_ALL_INFO_FORMAT(m_pAppPrivacyLabel,"");
453 }
454
455 LinkLabel::LinkLabel(QWidget *parent)
456 : QLabel(parent)
457 {
458 }
459
460 void LinkLabel::mousePressEvent(QMouseEvent *event)
461 {
462 if (event->button() == Qt::LeftButton) {
463 QProcess *process = new QProcess(this);
464 QStringList l;
465 l << "-A"
466 << "kylin-os";
467 process->start("kylin-user-guide", l);
468 }
469 QLabel::mousePressEvent(event);
470 }
471
472 }
473
474 #include "kaboutdialog.moc"
475 #include "moc_kaboutdialog.cpp"