forked from linuxdeepin/deepin-update-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginupdateplugin.cpp
More file actions
392 lines (328 loc) · 12.6 KB
/
Copy pathpluginupdateplugin.cpp
File metadata and controls
392 lines (328 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "pluginupdateplugin.h"
#include <DDBusSender>
#include <QIcon>
#include <QSettings>
#include <QJsonDocument>
#include <QProcess>
#include <QLocale>
#include <QStandardPaths>
#include <QDir>
#include <DGuiApplicationHelper>
#include <DStandardPaths>
#define PLUGIN_STATE_KEY "enable"
Q_LOGGING_CATEGORY(dockUpdatePlugin, "org.deepin.dde.dock.update")
constexpr auto UPDATE_STATE_KEY = "update-state-key";
constexpr auto MENU_OPEN_UPDATES = "OpenUpdates";
constexpr auto MENU_RESTART = "Restart";
DCORE_USE_NAMESPACE
using namespace DockUpdatePlugin;
PluginUpdatePlugin::PluginUpdatePlugin(QObject *parent)
: QObject(parent)
, m_pluginLoaded(false)
, m_dockIcon(nullptr)
, m_tipsLabel(new TipsWidget)
, m_dconfig(DConfig::create("org.deepin.dde.lastore", "org.deepin.dde.lastore", QString(), this))
, m_dockTrayConfig(DConfig::create("org.deepin.dde.shell", "org.deepin.ds.dock.tray", QString(), this))
, m_currentState(UpdateState::UpdatesAvailable)
, m_updateMode(0)
, m_shouldShow(false)
, m_isPrivateUpdate(false)
{
m_tipsLabel->setVisible(false);
m_tipsLabel->setAccessibleName("plugin-update");
QList<QString> translationDirs;
const auto dataDirs = DStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
for (const auto &path : dataDirs) {
translationDirs << QDir(path).filePath("dock-update-plugin/translations");
}
const QList<QLocale> localeFallback{QLocale::system(), QLocale::c()};
if (!DGuiApplicationHelper::loadTranslator("dock-update-plugin", translationDirs, localeFallback)) {
qCWarning(dockUpdatePlugin) << "Failed to load dock-update-plugin translations via DGuiApplicationHelper";
}
}
const QString PluginUpdatePlugin::pluginName() const
{
return "plugin-update";
}
const QString PluginUpdatePlugin::pluginDisplayName() const
{
return tr("Update status");
}
QWidget *PluginUpdatePlugin::itemWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
return m_dockIcon.data();
}
QWidget *PluginUpdatePlugin::itemTipsWidget(const QString &itemKey)
{
Q_UNUSED(itemKey);
switch (m_currentState) {
case UpdateState::UpdatesAvailable:
m_tipsLabel->setText(tr("Install updates to get the latest features and security improvements."));
break;
case UpdateState::UpdatesInstalled:
m_tipsLabel->setText(tr("New updates are available, Restart to finish installing."));
break;
}
return m_tipsLabel.data();
}
void PluginUpdatePlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;
if (!pluginIsDisable()) {
loadPlugin();
}
if (!m_dconfig) {
qCWarning(dockUpdatePlugin) << "Failed to create DConfig for org.deepin.dde.lastore";
return;
}
if (!m_dockTrayConfig) {
qCWarning(dockUpdatePlugin) << "Failed to create DConfig for org.deepin.ds.dock.tray";
}
m_updateMode = m_dconfig->value("update-mode").toInt();
m_updateStatus = m_dconfig->value("update-status");
m_isPrivateUpdate = m_dconfig->value("intranet-update", false).toBool();
connect(m_dconfig.data(), &DConfig::valueChanged, this, &PluginUpdatePlugin::onConfigChanged);
// 初始化时隐藏插件,将 surfaceId 添加到 dockHiddenSurfaceIds
updateDockHiddenSurfaceIds(true);
updateStateFromUpdateStatus();
}
void PluginUpdatePlugin::pluginStateSwitched()
{
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool());
refreshPluginItemsVisible();
}
bool PluginUpdatePlugin::pluginIsDisable()
{
const auto lastoreDisabled = m_dconfig->value("lastore-daemon-status", 0).toInt() == 2;
bool defaultValue = true;
return lastoreDisabled || !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, defaultValue).toBool();
}
const QString PluginUpdatePlugin::itemCommand(const QString &itemKey)
{
Q_UNUSED(itemKey);
// 打开控制中心的更新页面
return QString(
"dbus-send --session --print-reply "
"--dest=org.deepin.dde.ControlCenter1 "
"/org/deepin/dde/ControlCenter1 "
"org.deepin.dde.ControlCenter1.ShowModule "
"string:update"
);
}
const QString PluginUpdatePlugin::itemContextMenu(const QString &itemKey)
{
Q_UNUSED(itemKey);
QList<QVariant> items;
if (m_currentState == UpdateState::UpdatesAvailable) {
QMap<QString, QVariant> openUpdates;
openUpdates["itemId"] = MENU_OPEN_UPDATES;
openUpdates["itemText"] = tr("Open Update");
openUpdates["isActive"] = true;
items.push_back(openUpdates);
} else if (m_currentState == UpdateState::UpdatesInstalled) {
QMap<QString, QVariant> restart;
restart["itemId"] = MENU_RESTART;
restart["itemText"] = tr("Restart to finish installing");
restart["isActive"] = true;
items.push_back(restart);
QMap<QString, QVariant> openUpdates;
openUpdates["itemId"] = MENU_OPEN_UPDATES;
openUpdates["itemText"] = tr("Open Update");
openUpdates["isActive"] = true;
items.push_back(openUpdates);
}
QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = false;
menu["singleCheck"] = false;
return QJsonDocument::fromVariant(menu).toJson();
}
void PluginUpdatePlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
{
Q_UNUSED(itemKey)
Q_UNUSED(checked)
if (menuId == MENU_OPEN_UPDATES) {
// 更新设置
DDBusSender()
.service("org.deepin.dde.ControlCenter1")
.interface("org.deepin.dde.ControlCenter1")
.path("/org/deepin/dde/ControlCenter1")
.method("ShowModule")
.arg(QString("update"))
.call();
} else if (menuId == MENU_RESTART) {
// 重启系统
if (Dtk::Gui::DGuiApplicationHelper::testAttribute(Dtk::Gui::DGuiApplicationHelper::IsWaylandPlatform)) {
DDBusSender()
.service("org.deepin.dde.SessionManager1")
.interface("org.deepin.dde.SessionManager1")
.path("/org/deepin/dde/SessionManager1")
.method("RequestReboot")
.call();
} else {
DDBusSender()
.service("org.deepin.dde.ShutdownFront1")
.interface("org.deepin.dde.ShutdownFront1")
.path("/org/deepin/dde/ShutdownFront1")
.method("Restart")
.call();
}
}
}
void PluginUpdatePlugin::displayModeChanged(const Dock::DisplayMode displayMode)
{
Q_UNUSED(displayMode);
if (!pluginIsDisable() && !m_dockIcon.isNull()) {
m_dockIcon->update();
}
}
int PluginUpdatePlugin::itemSortKey(const QString &itemKey)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
return m_proxyInter->getValue(this, key, 7).toInt();
}
void PluginUpdatePlugin::setSortKey(const QString &itemKey, const int order)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);
m_proxyInter->saveValue(this, key, order);
}
void PluginUpdatePlugin::pluginSettingsChanged()
{
refreshPluginItemsVisible();
}
void PluginUpdatePlugin::loadPlugin()
{
if (m_pluginLoaded) {
return;
}
m_dockIcon.reset(new DockIconWidget);
m_dockIcon->setFixedSize(Dock::DOCK_PLUGIN_ITEM_FIXED_SIZE);
updateIconState();
m_proxyInter->itemAdded(this, UPDATE_STATE_KEY);
m_pluginLoaded = true;
displayModeChanged(displayMode());
}
void PluginUpdatePlugin::refreshPluginItemsVisible()
{
bool shouldShow = !pluginIsDisable() && m_shouldShow && !m_isPrivateUpdate;
if (!shouldShow) {
if (m_pluginLoaded) {
updateDockHiddenSurfaceIds(true);
}
} else {
if (!m_pluginLoaded) {
loadPlugin();
return;
}
// m_shouldShow 为 true 时,从 dockHiddenSurfaceIds 移除插件 ID
updateDockHiddenSurfaceIds(false);
m_proxyInter->itemAdded(this, UPDATE_STATE_KEY);
}
}
void PluginUpdatePlugin::updateIconState()
{
if (!m_dockIcon) {
return;
}
QString iconPath;
switch (m_currentState) {
case UpdateState::UpdatesAvailable:
iconPath = "status-updates-available";
break;
case UpdateState::UpdatesInstalled:
iconPath = "status-installed-restart-pending";
break;
}
m_dockIcon->setIconPath(iconPath);
}
void PluginUpdatePlugin::saveUpdateState(UpdateState state)
{
if (m_proxyInter) {
m_proxyInter->saveValue(this, UPDATE_STATE_KEY, static_cast<int>(state));
}
}
void PluginUpdatePlugin::onConfigChanged(const QString &key)
{
if (key == "update-mode") {
m_updateMode = m_dconfig->value("update-mode", 0).toInt();
} else if (key == "update-status") {
m_updateStatus = m_dconfig->value("update-status", QVariant());
qCInfo(dockUpdatePlugin) << "UpdateStatus changed to:" << m_updateStatus;
} else if (key == "intranet-update") {
m_isPrivateUpdate = m_dconfig->value("intranet-update", false).toBool();
}
//对dconfig获取的数据进行解析,从而处理更新的状态和是否显示
updateStateFromUpdateStatus();
//根据是否显示,刷新插件的显示状态
refreshPluginItemsVisible();
}
void PluginUpdatePlugin::updateStateFromUpdateStatus()
{
if (m_updateStatus.isNull() || !m_updateStatus.isValid()) {
qCWarning(dockUpdatePlugin) << "UpdateStatus data is null or invalid, cannot update state";
return;
}
QJsonDocument jsonDoc;
QJsonObject jsonObj;
QJsonParseError error;
jsonDoc = QJsonDocument::fromJson(m_updateStatus.toString().toUtf8(), &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dockUpdatePlugin) << "Failed to parse UpdateStatus JSON:" << error.errorString();
return;
}
jsonObj = jsonDoc.object();
if (!jsonObj.contains("UpdateStatus")) {
qCWarning(dockUpdatePlugin) << "UpdateStatus field not found in data";
return;
}
QJsonObject updateStatusObj = jsonObj["UpdateStatus"].toObject();
QString systemUpgrade = updateStatusObj["system_upgrade"].toString();
QString securityUpgrade = updateStatusObj["security_upgrade"].toString();
qCInfo(dockUpdatePlugin) << "Parsing UpdateStatus - system_upgrade:" << systemUpgrade << "security_upgrade:" << securityUpgrade;
const bool systemEnabled = (m_updateMode == 1 || m_updateMode == 5 || m_updateMode == 9 || m_updateMode == 13);
const bool securityEnabled = (m_updateMode == 4 || m_updateMode == 5 || m_updateMode == 12 || m_updateMode == 13);
const bool systemHasUpdate = systemEnabled && (systemUpgrade == "needReboot" || systemUpgrade == "notDownload");
const bool securityHasUpdate = securityEnabled && (securityUpgrade == "needReboot" || securityUpgrade == "notDownload");
m_shouldShow = systemHasUpdate || securityHasUpdate;
UpdateState newState = m_currentState;
if (systemUpgrade == "needReboot" || securityUpgrade == "needReboot") {
newState = UpdateState::UpdatesInstalled;
} else if (systemUpgrade == "notDownload" || securityUpgrade == "notDownload") {
newState = UpdateState::UpdatesAvailable;
}
if (m_currentState != newState) {
m_currentState = newState;
saveUpdateState(m_currentState);
updateIconState();
}
qCInfo(dockUpdatePlugin) << "Plugin should show:" << m_shouldShow;
return;
}
void PluginUpdatePlugin::updateDockHiddenSurfaceIds(bool shouldHide)
{
if (!m_dockTrayConfig) {
qCWarning(dockUpdatePlugin) << "m_dockTrayConfig is null, cannot update dockHiddenSurfaceIds";
return;
}
const QString surfaceId = "plugin-update::update-state-key";
QStringList hiddenIds = m_dockTrayConfig->value("dockHiddenSurfaceIds").toStringList();
if (shouldHide) {
// 添加到隐藏列表
if (!hiddenIds.contains(surfaceId)) {
hiddenIds.append(surfaceId);
m_dockTrayConfig->setValue("dockHiddenSurfaceIds", hiddenIds);
qCInfo(dockUpdatePlugin) << "Added" << surfaceId << "to dockHiddenSurfaceIds";
}
} else {
// 从隐藏列表移除
if (hiddenIds.contains(surfaceId)) {
hiddenIds.removeAll(surfaceId);
m_dockTrayConfig->setValue("dockHiddenSurfaceIds", hiddenIds);
qCInfo(dockUpdatePlugin) << "Removed" << surfaceId << "from dockHiddenSurfaceIds";
}
}
}