Debian Codegraph @

kysdk-qtwidgets/src/kuninstalldialog.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 "kuninstalldialog.h"
24 #include <QByteArray>
25 #include <QFileDialog>
26 #include <QFont>
27 #include <QLocale>
28 #include <QStandardPaths>
29 #include <QVariant>
30 #include <QtSql/QSqlDatabase>
31 #include <QtSql/QSqlQuery>
32 #include <QtSql/QSqlRecord>
33 #include "accessinfohelper.h"
34
35 namespace kdk
36 {
37 class KUninstallDialogPrivate : public QObject
38 {
39 Q_DECLARE_PUBLIC(KUninstallDialog)
40 Q_OBJECT
41
42 public:
43 KUninstallDialogPrivate(KUninstallDialog *parent);
44 QString getIconPath(QString appName);
45 QString setLabelStringBody(QString text, QLabel *lable);
46 QString getAppCnName(QString appName);
47 void resetAppInfo(QString appName, QString appVersion);
48 void fontGsettingInit();
49 void setLabelText();
50 void setAccessInfo();
51
52 private:
53 KUninstallDialog *q_ptr;
54 QString m_appName;
55 QString m_appVersion;
56 QVBoxLayout *debInfoLayout;
57 QWidget *IconAndAppNameWidget;
58 QHBoxLayout *IconAndAppNameLayout;
59 QWidget *debNameAndVersionWidget;
60 QHBoxLayout *debNameAndVersionLayout;
61 QWidget *nameVersionWidget;
62 QVBoxLayout *nameVersionVLayout;
63 QGSettings *m_pGsettingFontSize;
64 QLabel *debIconLabel;
65 QLabel *debAppNameLabel;
66 QLabel *debNameLabel;
67 QLabel *debVersionLabel;
68 QPushButton *m_pUninstallBtn;
69 };
70 KUninstallDialog::~KUninstallDialog()
71 {
72 }
73
74 QLabel *KUninstallDialog::debAppNameLabel()
75 {
76 Q_D(KUninstallDialog);
77 return d->debAppNameLabel;
78 }
79
80 QLabel *KUninstallDialog::debNameLabel()
81 {
82 Q_D(KUninstallDialog);
83 return d->debNameLabel;
84 }
85
86 QLabel *KUninstallDialog::debIconLabel()
87 {
88 Q_D(KUninstallDialog);
89 return d->debIconLabel;
90 }
91
92 QLabel *KUninstallDialog::debVersionLabel()
93 {
94 Q_D(KUninstallDialog);
95 return d->debVersionLabel;
96 }
97
98 QPushButton *KUninstallDialog::uninstallButtton()
99 {
100 Q_D(KUninstallDialog);
101 return d->m_pUninstallBtn;
102 }
103
104 void KUninstallDialog::changeTheme()
105 {
106 KDialog::changeTheme();
107 if (ThemeController::themeMode() == LightTheme) {
108 // to do 还没有具体的设计图 只有截图
109 } else {
110 // to do 还没有具体的设计图 只有截图
111 }
112 }
113
114 KUninstallDialog::KUninstallDialog(QString appName, QString appVersion, QWidget *parent)
115 : KDialog(parent)
116 , d_ptr(new KUninstallDialogPrivate(this))
117 {
118 Q_D(KUninstallDialog);
119 d->m_appName = appName;
120 d->m_appVersion = appVersion;
121
122 setWindowTitle(tr("uninstall"));
123 this->setFixedSize(550, 450);
124
125 d->debInfoLayout = new QVBoxLayout();
126 d->debInfoLayout->setContentsMargins(0, 0, 0, 0);
127
128 d->IconAndAppNameWidget = new QWidget();
129 d->IconAndAppNameWidget->setContentsMargins(0, 0, 0, 0);
130
131 d->IconAndAppNameLayout = new QHBoxLayout();
132 d->IconAndAppNameLayout->setContentsMargins(0, 0, 0, 0);
133 d->IconAndAppNameLayout->setSpacing(0);
134 d->debIconLabel = new QLabel();
135 d->debIconLabel->setFixedSize(48, 48);
136
137 /*先判断主题中是否有icon*/
138 if (QIcon::fromTheme(d->m_appName).isNull()) {
139 QPixmap pixmap = QPixmap(d->getIconPath(d->m_appName));
140 pixmap.scaled(QSize(48, 48), Qt::KeepAspectRatio);
141 d->debIconLabel->setPixmap(QIcon(QPixmap(d->getIconPath(d->m_appName))).pixmap(QSize(48, 48)));
142 setWindowIcon(QIcon(QPixmap(d->getIconPath(d->m_appName))).pixmap(QSize(24, 24)));
143 } else {
144 d->debIconLabel->setPixmap(QIcon::fromTheme(d->m_appName).pixmap(QSize(48, 48)));
145 setWindowIcon(QIcon::fromTheme(d->m_appName).pixmap(QSize(24, 24)));
146 }
147 d->debIconLabel->adjustSize();
148 d->fontGsettingInit();
149 d->debAppNameLabel = new QLabel();
150 QFont font2 = d->debAppNameLabel->font();
151 if (d->m_pGsettingFontSize != nullptr && d->m_pGsettingFontSize->keys().contains(GSETTING_FONT_KEY))
152 font2.setPixelSize(d->m_pGsettingFontSize->get(GSETTING_FONT_KEY).toInt() * 2.5);
153 d->debAppNameLabel->setFont(font2);
154 d->debAppNameLabel->setFixedWidth(331);
155 QLocale locale;
156 if (locale.language() == QLocale::Chinese) {
157 if (d->getAppCnName(d->m_appName).isNull()) {
158 d->debAppNameLabel->setText(d->setLabelStringBody(d->m_appName, d->debAppNameLabel));
159 } else {
160 d->debAppNameLabel->setText(d->setLabelStringBody(d->getAppCnName(d->m_appName), d->debAppNameLabel));
161 }
162 } else {
163 d->debAppNameLabel->setText(d->setLabelStringBody(d->m_appName, d->debAppNameLabel));
164 }
165 d->debAppNameLabel->adjustSize();
166
167 d->IconAndAppNameLayout->addItem(new QSpacerItem(124, 10, QSizePolicy::Fixed));
168 d->IconAndAppNameLayout->addWidget(d->debIconLabel);
169 d->IconAndAppNameLayout->addItem(new QSpacerItem(12, 10, QSizePolicy::Fixed));
170 d->IconAndAppNameLayout->addWidget(d->debAppNameLabel);
171 d->IconAndAppNameLayout->addItem(new QSpacerItem(400, 10, QSizePolicy::Expanding));
172 d->IconAndAppNameWidget->setLayout(d->IconAndAppNameLayout);
173
174 d->debNameAndVersionWidget = new QWidget();
175 d->debNameAndVersionLayout = new QHBoxLayout();
176 d->debNameAndVersionLayout->setContentsMargins(0, 0, 0, 0);
177 d->debNameAndVersionLayout->setSpacing(0);
178
179 d->debNameLabel = new QLabel();
180 QString debName = tr("deb name:");
181 debName.append(appName);
182 d->debNameLabel->setText(debName);
183 d->debNameLabel->adjustSize();
184 d->debVersionLabel = new QLabel();
185 QString debVersion = tr("deb version:");
186 debVersion.append(appVersion);
187 d->debVersionLabel->setText(debVersion);
188 d->debVersionLabel->adjustSize();
189 if (d->debVersionLabel->width() > 329) {
190 d->debVersionLabel->setText(debVersion.left(20) + "...");
191 d->debVersionLabel->setToolTip(debVersion);
192 }
193 d->nameVersionWidget = new QWidget();
194 d->nameVersionVLayout = new QVBoxLayout();
195 d->nameVersionVLayout->setContentsMargins(0, 0, 0, 0);
196 d->nameVersionVLayout->addWidget(d->debNameLabel);
197 d->nameVersionVLayout->addWidget(d->debVersionLabel);
198 d->nameVersionVLayout->setSpacing(0);
199 d->nameVersionWidget->setLayout(d->nameVersionVLayout);
200
201 d->debNameAndVersionLayout->addItem(new QSpacerItem(184, 10, QSizePolicy::Fixed));
202 d->debNameAndVersionLayout->addWidget(d->nameVersionWidget);
203 d->debNameAndVersionLayout->addItem(new QSpacerItem(330, 10, QSizePolicy::Expanding));
204 d->debNameAndVersionWidget->setLayout(d->debNameAndVersionLayout);
205
206 d->debInfoLayout->addStretch();
207 d->debInfoLayout->addWidget(d->IconAndAppNameWidget);
208 d->debInfoLayout->addWidget(d->debNameAndVersionWidget);
209 d->debInfoLayout->addSpacing(25);
210
211 d->m_pUninstallBtn = new QPushButton(tr("uninstall"), this);
212 d->m_pUninstallBtn->setFixedSize(122, 40);
213 QWidget *pBtnWidget = new QWidget();
214 QHBoxLayout *hLayout = new QHBoxLayout();
215 hLayout->addItem(new QSpacerItem(184, 10, QSizePolicy::Fixed));
216 hLayout->addWidget(d->m_pUninstallBtn);
217 hLayout->addStretch();
218 pBtnWidget->setLayout(hLayout);
219 d->debInfoLayout->addWidget(pBtnWidget);
220 d->debInfoLayout->addStretch();
221 mainWidget()->setLayout(d->debInfoLayout);
222 changeTheme();
223
224 d->setAccessInfo();
225 }
226
227 KUninstallDialogPrivate::KUninstallDialogPrivate(KUninstallDialog *parent)
228 : q_ptr(parent)
229 {
230 setParent(parent);
231 }
232
233 QString KUninstallDialogPrivate::getIconPath(QString appName)
234 {
235 QString iconFilePath;
236 /*cr判断~/.cache/icons下是否有icon*/
237 iconFilePath.clear();
238 iconFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.cache/uksc/icons/" + appName + ".png";
239 if (QFile::exists(iconFilePath)) {
240 return iconFilePath;
241 }
242 /*最后判断/usr/share/ubuntu-kylin-software-center/data/icons下面是否有*/
243 iconFilePath.clear();
244 iconFilePath = "/usr/share/kylin-software-center/data/icons/" + appName + ".png";
245 if (QFile::exists(iconFilePath)) {
246 return iconFilePath;
247 }
248 return nullptr;
249 }
250
251 QString KUninstallDialogPrivate::setLabelStringBody(QString text, QLabel *lable)
252 {
253 QFontMetrics fontMetrics(lable->font());
254 int LableWidth = lable->width();
255 int fontSize = fontMetrics.horizontalAdvance(text);
256 QString formatBody = text;
257 if (fontSize > (LableWidth - 10)) {
258 formatBody = fontMetrics.elidedText(formatBody, Qt::ElideRight, LableWidth - 10);
259 return formatBody;
260 }
261 return formatBody;
262 }
263
264 QString KUninstallDialogPrivate::getAppCnName(QString appName)
265 {
266 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
267 db.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.cache/uksc/uksc.db");
268 if (!db.open()) {
269 return nullptr;
270 }
271 QSqlQuery query;
272 query.exec(QString("SELECT * FROM application WHERE app_name IS '%1'").arg(appName)); // 执行
273 while (query.next()) {
274 return query.value(4).toString();
275 }
276 db.close();
277 return nullptr;
278 }
279
280 void KUninstallDialogPrivate::resetAppInfo(QString appName, QString appVersion)
281 {
282 Q_Q(KUninstallDialog);
283
284 m_appName = appName;
285 m_appVersion = appVersion;
286 /*先判断主题中是否有icon*/
287 if (QIcon::fromTheme(m_appName).isNull()) {
288 QPixmap pixmap = QPixmap(getIconPath(m_appName));
289 pixmap.scaled(QSize(48, 48), Qt::KeepAspectRatio);
290 debIconLabel->setPixmap(QIcon(QPixmap(getIconPath(m_appName))).pixmap(QSize(48, 48)));
291 } else {
292 debIconLabel->setPixmap(QIcon::fromTheme(m_appName).pixmap(QSize(48, 48)));
293 }
294
295 if (getAppCnName(m_appName).isNull())
296 debAppNameLabel->setText(m_appName);
297 else
298 debAppNameLabel->setText(getAppCnName(m_appName));
299
300 QString debName = tr("deb name:");
301 debName.append(appName);
302 debNameLabel->setText(debName);
303
304 QString debVersion = tr("deb version:");
305 debVersion.append(appVersion);
306 debVersionLabel->setText(debVersion);
307 debVersionLabel->adjustSize();
308 if (debVersionLabel->width() > 329) {
309 debVersionLabel->setText(debVersion.left(20) + "...");
310 debVersionLabel->setToolTip(debVersion);
311 }
312 }
313
314 void KUninstallDialogPrivate::fontGsettingInit()
315 {
316 Q_Q(KUninstallDialog);
317 m_pGsettingFontSize = nullptr;
318 const QByteArray style_id(ORG_UKUI_STYLE_FONT);
319 if (QGSettings::isSchemaInstalled(style_id)) {
320 m_pGsettingFontSize = new QGSettings(style_id);
321 connect(m_pGsettingFontSize, &QGSettings::changed, this, [=](const QString &key) {
322 if (key == GSETTING_FONT_KEY) {
323 setLabelText();
324 }
325 });
326 }
327 }
328
329 void KUninstallDialogPrivate::setLabelText()
330 {
331 Q_Q(KUninstallDialog);
332 QFont font;
333 if (m_pGsettingFontSize != nullptr && m_pGsettingFontSize->keys().contains(GSETTING_FONT_KEY))
334 font.setPixelSize(m_pGsettingFontSize->get(GSETTING_FONT_KEY).toDouble() * 2.5);
335 debAppNameLabel->setFont(font);
336 QLocale locale;
337 if (locale.language() == QLocale::Chinese) {
338 if (getAppCnName(m_appName).isNull()) {
339 q->setWindowTitle(setLabelStringBody(m_appName, debAppNameLabel));
340 } else {
341 q->setWindowTitle(setLabelStringBody(getAppCnName(m_appName), debAppNameLabel));
342 }
343 } else {
344 q->setWindowTitle(setLabelStringBody(m_appName, debAppNameLabel));
345 }
346 }
347
348 void KUninstallDialogPrivate::setAccessInfo()
349 {
350 Q_Q(KUninstallDialog);
351 KDK_ALL_INFO_FORMAT(IconAndAppNameWidget,"");
352 KDK_ALL_INFO_FORMAT(debNameAndVersionWidget,"");
353 KDK_ALL_INFO_FORMAT(nameVersionWidget,"");
354 if (m_pGsettingFontSize)
355 KDK_OBJ_INFO_FORMAT(m_pGsettingFontSize);
356 KDK_ALL_INFO_FORMAT(debIconLabel,"");
357 KDK_ALL_INFO_FORMAT(debAppNameLabel,"");
358 KDK_ALL_INFO_FORMAT(debNameLabel,"");
359 KDK_ALL_INFO_FORMAT(debVersionLabel,"");
360 KDK_ALL_INFO_FORMAT(m_pUninstallBtn,"");
361 }
362
363 }
364
365 #include "kuninstalldialog.moc";
366 #include "moc_kuninstalldialog.cpp";