-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathnotificationmanager.cpp
More file actions
720 lines (609 loc) · 26.7 KB
/
notificationmanager.cpp
File metadata and controls
720 lines (609 loc) · 26.7 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "notificationmanager.h"
#include "dataaccessorproxy.h"
#include "dbaccessor.h"
#include "notificationsetting.h"
#include "notifyentity.h"
#include <DDesktopServices>
#include <DSGApplication>
#include <QDBusConnectionInterface>
#include <DConfig>
#include <QAbstractItemModel>
#include <QDBusInterface>
#include <QProcess>
#include <QTimer>
#include <QLoggingCategory>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QGuiApplication>
#include <appletbridge.h>
#include <pluginloader.h>
DCORE_USE_NAMESPACE
DS_USE_NAMESPACE
namespace notification {
Q_DECLARE_LOGGING_CATEGORY(notifyLog)
}
namespace notification {
static const uint NoReplacesId = 0;
static const int DefaultTimeOutMSecs = 5000;
static const QString NotificationsDBusService = "org.freedesktop.Notifications";
static const QString NotificationsDBusPath = "/org/freedesktop/Notifications";
static const QString DDENotifyDBusServer = "org.deepin.dde.Notification1";
static const QString DDENotifyDBusPath = "/org/deepin/dde/Notification1";
static const QString SessionDBusService = "org.deepin.dde.SessionManager1";
static const QString SessionDaemonDBusPath = "/org/deepin/dde/SessionManager1";
NotificationManager::NotificationManager(QObject *parent)
: QObject(parent)
, m_persistence(DataAccessorProxy::instance())
, m_setting(new NotificationSetting(this))
, m_pendingTimeout(new QTimer(this))
{
m_pendingTimeout->setSingleShot(true);
connect(m_pendingTimeout, &QTimer::timeout, this, &NotificationManager::onHandingPendingEntities);
DataAccessorProxy::instance()->setSource(DBAccessor::instance());
DAppletBridge bridge("org.deepin.ds.dde-apps");
if (auto apps = bridge.applet()) {
if (auto model = apps->property("appModel").value<QAbstractItemModel *>()) {
m_setting->setAppAccessor(model);
}
}
if (!m_setting->appAccessor()) {
qWarning(notifyLog) << "It's not exist appModel for the applet:" << bridge.pluginId();
}
connect(m_setting, &NotificationSetting::appAdded, this, &NotificationManager::AppAdded);
connect(m_setting, &NotificationSetting::appRemoved, this, &NotificationManager::AppRemoved);
connect(m_setting, &NotificationSetting::appValueChanged, this, [this] (const QString &appId, uint configItem, const QVariant &value) {
Q_EMIT AppInfoChanged(appId, configItem, QDBusVariant(value));
});
connect(m_setting, &NotificationSetting::systemValueChanged, this, [this] (uint configItem, const QVariant &value) {
Q_EMIT SystemInfoChanged(configItem, QDBusVariant(value));
});
QScopedPointer<DConfig> config(DConfig::create("org.deepin.dde.shell", "org.deepin.dde.shell.notification"));
m_systemApps = config->value("systemApps").toStringList();
// TODO temporary fix for AppNamesMap
m_appNamesMap = config->value("AppNamesMap").toMap();
if(!config->value("notificationCleanupDays").isNull()) {
m_cleanupDays = config->value("notificationCleanupDays").toInt();
}
if (QStringLiteral("wayland") != QGuiApplication::platformName()) {
initScreenLockedState();
}
}
NotificationManager::~NotificationManager()
{
}
bool NotificationManager::registerDbusService()
{
QDBusConnection connection = QDBusConnection::sessionBus();
connection.interface()->registerService(NotificationsDBusService,
QDBusConnectionInterface::ReplaceExistingService,
QDBusConnectionInterface::AllowReplacement);
if (!connection.registerObject(NotificationsDBusPath, this)) {
return false;
}
QDBusConnection ddeNotifyConnect = QDBusConnection::sessionBus();
ddeNotifyConnect.interface()->registerService(DDENotifyDBusServer,
QDBusConnectionInterface::ReplaceExistingService,
QDBusConnectionInterface::AllowReplacement);
if (!ddeNotifyConnect.registerObject(DDENotifyDBusPath, this)) {
return false;
}
return true;
}
uint NotificationManager::recordCount() const
{
return m_persistence->fetchEntityCount(DataAccessor::AllApp(), NotifyEntity::Processed);
}
void NotificationManager::actionInvoked(qint64 id, const QString &actionKey)
{
qInfo(notifyLog) << "Action invoked, id:" << id << ", actionKey" << actionKey;
auto entity = m_persistence->fetchEntity(id);
if (entity.isValid()) {
doActionInvoked(entity, actionKey);
entity.setProcessedType(NotifyEntity::Removed);
updateEntityProcessed(entity);
}
}
void NotificationManager::actionInvoked(qint64 id, uint bubbleId, const QString &actionKey)
{
qInfo(notifyLog) << "Action invoked, bubbleId:" << bubbleId << ", id:" << id << ", actionKey" << actionKey;
actionInvoked(id, actionKey);
Q_EMIT ActionInvoked(bubbleId, actionKey);
Q_EMIT NotificationClosed(bubbleId, NotifyEntity::Closed);
}
void NotificationManager::notificationClosed(qint64 id, uint bubbleId, uint reason)
{
qDebug(notifyLog) << "Close notification id" << id << ", reason" << reason;
updateEntityProcessed(id, reason);
Q_EMIT NotificationClosed(bubbleId, reason);
}
void NotificationManager::notificationReplaced(qint64 id)
{
updateEntityProcessed(id, NotifyEntity::Closed);
}
void NotificationManager::removeNotification(qint64 id)
{
m_persistence->removeEntity(id);
emitRecordCountChanged();
}
void NotificationManager::removeNotifications(const QString &appName)
{
m_persistence->removeEntityByApp(appName);
emitRecordCountChanged();
}
void NotificationManager::removeNotifications()
{
m_persistence->clear();
emitRecordCountChanged();
}
void NotificationManager::removeExpiredNotifications()
{
qDebug(notifyLog) << "Remove expired notifications.";
const qint64 cutoffTime = QDateTime::currentDateTime().addDays(-m_cleanupDays).toMSecsSinceEpoch();
m_persistence->removeEntitiesByExpiredTime(cutoffTime);
emitRecordCountChanged();
}
QStringList NotificationManager::GetCapabilities()
{
QStringList result;
result << "action-icons" << "actions" << "body" << "body-hyperlinks" << "body-markup" << "body-image" << "enable-sound" << "persistence";
return result;
}
uint NotificationManager::Notify(const QString &appName, uint replacesId, const QString &appIcon, const QString &summary,
const QString &body, const QStringList &actions, const QVariantMap &hints,
int expireTimeout)
{
qInfo(notifyLog) << "Notify"
<< ", appName:" << appName << ", summary:" << summary << ", appIcon:" << appIcon << ", body size:" << body.size()
<< ", actions:" << actions << ", hint: " << hints << ", replaceId:" << replacesId << ", expireTimeout:" << expireTimeout;
if (calledFromDBus() && m_setting->systemValue(NotificationSetting::CloseNotification).toBool()) {
qDebug(notifyLog) << "Notify has been disabled by CloseNotification setting.";
return 0;
}
QString appId = appIdByAppName(appName);
if (appId.isEmpty() && calledFromDBus()) {
QDBusReply<uint> reply = connection().interface()->servicePid(message().service());
appId = DSGApplication::getId(reply.value());
}
if (appId.isEmpty())
appId = appName;
bool enableAppNotification = m_setting->appValue(appId, NotificationSetting::EnableNotification).toBool();
if (!enableAppNotification && !m_systemApps.contains(appId)) {
return 0;
}
auto tsAppName = m_setting->appValue(appId, NotificationSetting::AppName).toString();
if (tsAppName.isEmpty()) {
tsAppName = appName;
} else {
qCDebug(notifyLog) << "AppName is translated from AM, which appId is:" << appId;
}
QString strBody = body;
// Unescape backslashes and quotes from %q formatted strings (e.g., \\ -> \, \" -> ", \' -> ')
strBody.replace(QRegularExpression("\\\\(\\\\|['\"])"), "\\1");
QString strIcon = appIcon;
if (strIcon.isEmpty())
strIcon = m_setting->appValue(appId, NotificationSetting::AppIcon).toString();
NotifyEntity entity(tsAppName, replacesId, strIcon, summary, strBody, actions, hints, expireTimeout);
entity.setAppId(appId);
entity.setProcessedType(NotifyEntity::None);
entity.setReplacesId(replacesId);
bool lockScreenShow = true;
bool dndMode = isDoNotDisturb();
bool systemNotification = m_systemApps.contains(appId);
const bool desktopScreen = !m_screenLocked;
if (!systemNotification) {
lockScreenShow = m_setting->appValue(appId, NotificationSetting::ShowOnLockScreen).toBool();
}
const bool onDesktopShow = m_setting->appValue(appId, NotificationSetting::ShowOnDesktop).toBool();
tryPlayNotificationSound(entity, appId, dndMode);
// new one
if (replacesId == NoReplacesId) {
entity.setBubbleId(++m_replacesCount);
if (systemNotification) { // 系统通知
entity.setProcessedType(NotifyEntity::NotProcessed);
} else if (m_screenLocked && !lockScreenShow) { // 锁屏不显示通知
entity.setProcessedType(NotifyEntity::Processed);
} else if (desktopScreen && !onDesktopShow) { // 桌面不显示通知
entity.setProcessedType(NotifyEntity::Processed);
} else { // 锁屏显示通知或者未锁屏状态
if (!systemNotification && !dndMode && enableAppNotification) { // 普通应用非勿扰模式并且开启通知选项
entity.setProcessedType(NotifyEntity::NotProcessed);
} else {
entity.setProcessedType(NotifyEntity::Processed);
}
}
} else { // maybe replace one
entity.setBubbleId(replacesId);
entity.setProcessedType(NotifyEntity::NotProcessed);
}
if (entity.processedType() != NotifyEntity::None) {
if (!recordNotification(entity)) {
return 0;
}
if (entity.isReplace() && m_persistence->fetchLastEntity(entity.bubbleId()).isValid()) {
removePendingEntity(entity);
}
emitRecordCountChanged();
Q_EMIT NotificationStateChanged(entity.id(), entity.processedType());
bool critical = false;
if (auto iter = hints.find("urgency"); iter != hints.end()) {
critical = iter.value().toUInt() == NotifyEntity::Critical;
}
// 0: never expire. -1: DefaultTimeOutMSecs
if (expireTimeout != 0 && !critical) {
pushPendingEntity(entity, expireTimeout);
}
}
qInfo(notifyLog) << "Notify done, bubbleId:" << entity.bubbleId() << ", id:" << entity.id() << ", type:" << entity.processedType();
// If replaces_id is 0, the return value is a UINT32 that represent the notification.
// If replaces_id is not 0, the returned value is the same value as replaces_id.
return entity.bubbleId();
}
void NotificationManager::CloseNotification(uint id)
{
auto entity = m_persistence->fetchLastEntity(id);
if (entity.isValid()) {
entity.setProcessedType(NotifyEntity::Removed);
updateEntityProcessed(entity);
}
Q_EMIT NotificationClosed(id, NotifyEntity::Closed);
qDebug(notifyLog) << "Close notify, bubbleId" << id << ", id:" << entity.id();
}
void NotificationManager::GetServerInformation(QString &name, QString &vendor, QString &version, QString &specVersion)
{
name = QString("DeepinNotifications");
vendor = QString("Deepin");
version = QString("3.0");
specVersion = QString("1.2");
}
QStringList NotificationManager::GetAppList()
{
qDebug(notifyLog) << "Get appList";
return m_setting->apps();
}
QVariant NotificationManager::GetAppInfo(const QString &appId, uint configItem)
{
return m_setting->appValue(appId, static_cast<NotificationSetting::AppConfigItem>(configItem));
}
void NotificationManager::SetAppInfo(const QString &appId, uint configItem, const QVariant &value)
{
qDebug(notifyLog) << "Set appInfo" << appId << configItem << value;
return m_setting->setAppValue(appId, static_cast<NotificationSetting::AppConfigItem>(configItem), value);
}
void NotificationManager::SetSystemInfo(uint configItem, const QVariant &value)
{
qDebug(notifyLog) << "Set systemInfo" << configItem << value;
return m_setting->setSystemValue(static_cast<NotificationSetting::SystemConfigItem>(configItem), value);
}
QVariant NotificationManager::GetSystemInfo(uint configItem)
{
qDebug(notifyLog) << "Get systemInfo" << configItem;
return m_setting->systemValue(static_cast<NotificationSetting::SystemConfigItem>(configItem));
}
void NotificationManager::setBlockClosedId(qint64 id)
{
auto findIter = std::find_if(m_pendingTimeoutEntities.begin(), m_pendingTimeoutEntities.end(), [this](const NotifyEntity &entity) {
return entity.id() == m_blockClosedId;
});
const auto current = QDateTime::currentMSecsSinceEpoch();
if(m_blockClosedId != NotifyEntity::InvalidId && findIter != m_pendingTimeoutEntities.end() && current > findIter.key()) {
qDebug(notifyLog) << "Block close bubble id:" << m_blockClosedId << "for the new block bubble id:" << id;
m_pendingTimeoutEntities.insert(QDateTime::currentMSecsSinceEpoch() + 1000, findIter.value());
m_pendingTimeoutEntities.erase(findIter);
}
m_blockClosedId = id;
onHandingPendingEntities();
}
bool NotificationManager::isDoNotDisturb() const
{
if (!m_setting->systemValue(NotificationSetting::DNDMode).toBool())
return false;
// 未点击按钮 任何时候都勿扰模式
if (!m_setting->systemValue(NotificationSetting::OpenByTimeInterval).toBool() &&
!m_setting->systemValue(NotificationSetting::LockScreenOpenDNDMode).toBool()) {
return true;
}
// 点击锁屏时 并且 锁屏状态 任何时候都勿扰模式
if (m_setting->systemValue(NotificationSetting::LockScreenOpenDNDMode).toBool() && m_screenLocked)
return true;
QTime currentTime = QTime::fromString(QDateTime::currentDateTime().toString("hh:mm"));
QTime startTime = QTime::fromString(m_setting->systemValue(NotificationSetting::StartTime).toString());
QTime endTime = QTime::fromString(m_setting->systemValue(NotificationSetting::EndTime).toString());
bool dndMode = true;
if (startTime < endTime) {
dndMode = startTime <= currentTime && endTime >= currentTime;
} else if (startTime > endTime) {
dndMode = startTime <= currentTime || endTime >= currentTime;
}
return dndMode && m_setting->systemValue(NotificationSetting::OpenByTimeInterval).toBool();
}
bool NotificationManager::recordNotification(NotifyEntity &entity)
{
qint64 id = NotifyEntity::InvalidId;
if (entity.isReplace()) {
auto lastEntity = m_persistence->fetchLastEntity(entity.bubbleId());
if (lastEntity.isValid()) {
bool showInNotifyCenter = true;
if (entity.hints().contains("x-deepin-ShowInNotifyCenter")) {
showInNotifyCenter = entity.hints()["x-deepin-ShowInNotifyCenter"].toBool();
}
if (showInNotifyCenter) {
m_persistence->updateEntityProcessedType(lastEntity.id(), NotifyEntity::Processed);
} else {
id = m_persistence->replaceEntity(lastEntity.id(), entity);
}
} else {
qWarning() << "Not exist notification to replace for the replaceId" << entity.replacesId();
return false;
}
}
if (id == NotifyEntity::InvalidId) {
id = m_persistence->addEntity(entity);
}
if (id == NotifyEntity::InvalidId) {
qWarning(notifyLog) << "Failed on saving DB, bubbleId:" << entity.bubbleId() << ", appName" << entity.appName();
return false;
}
entity.setId(id);
return true;
}
void NotificationManager::tryPlayNotificationSound(const NotifyEntity &entity, const QString &appId, bool dndMode) const
{
const auto hints = entity.hints();
if (!hints.isEmpty() && (!hints.value("enable-sound", true).toBool() ||
!hints.value("x-deepin-PlaySound", true).toBool())) {
return;
}
bool playSoundTip = false;
bool playSound = true;
bool systemNotification = m_systemApps.contains(appId);
if (!systemNotification)
playSound = m_setting->appValue(appId, NotificationSetting::EnableSound).toBool();
if (playSound && !dndMode) {
const auto actions = entity.actions();
//接收蓝牙文件时,只在发送完成后才有提示音,"cancel"表示正在发送文件
if (actions.contains("cancel")) {
if (auto iter = hints.find("x-deepin-action-_view"); iter != hints.end()) {
const auto action = iter.value().toString();
if (action.contains("xdg-open"))
playSoundTip = true;
}
} else {
playSoundTip = true;
}
}
if (playSoundTip) {
Dtk::Gui::DDesktopServices::playSystemSoundEffect(Dtk::Gui::DDesktopServices::SSE_Notifications);
}
}
void NotificationManager::emitRecordCountChanged()
{
const auto count = m_persistence->fetchEntityCount(DataAccessor::AllApp(), NotifyEntity::Processed);
emit RecordCountChanged(count);
}
void NotificationManager::pushPendingEntity(const NotifyEntity &entity, int expireTimeout)
{
const int interval = expireTimeout == -1 ? DefaultTimeOutMSecs : expireTimeout;
qint64 point = QDateTime::currentMSecsSinceEpoch() + interval;
m_pendingTimeoutEntities.insert(point, entity);
if (m_lastTimeoutPoint > point) {
m_lastTimeoutPoint = point;
auto newInterval = m_lastTimeoutPoint - QDateTime::currentMSecsSinceEpoch();
m_pendingTimeout->setInterval(newInterval);
m_pendingTimeout->start();
}
}
void NotificationManager::updateEntityProcessed(qint64 id, uint reason)
{
auto entity = m_persistence->fetchEntity(id);
if (entity.isValid()) {
if ((reason == NotifyEntity::Closed || reason == NotifyEntity::Dismissed) && entity.processedType() == NotifyEntity::NotProcessed) {
entity.setProcessedType(NotifyEntity::Removed);
} else if (reason == NotifyEntity::Timeout) {
entity.setProcessedType(NotifyEntity::Removed);
} else {
entity.setProcessedType(NotifyEntity::Processed);
}
updateEntityProcessed(entity);
}
}
void NotificationManager::updateEntityProcessed(const NotifyEntity &entity)
{
const auto id = entity.id();
const bool removed = entity.processedType() == NotifyEntity::Removed;
bool showInCenter = m_setting->appValue(entity.appId(), NotificationSetting::ShowInCenter).toBool();
if (entity.hints().contains("x-deepin-ShowInNotifyCenter")) {
showInCenter = entity.hints()["x-deepin-ShowInNotifyCenter"].toBool();
}
if (removed || !showInCenter) {
// remove it from memory
m_persistence->removeEntity(id);
} else {
// add to db and remove it form memory
m_persistence->updateEntityProcessedType(id, entity.processedType());
}
Q_EMIT NotificationStateChanged(entity.id(), entity.processedType());
removePendingEntity(entity);
emitRecordCountChanged();
}
QString NotificationManager::appIdByAppName(const QString &appName) const
{
const auto items = m_setting->appItems();
for (const auto &item: items) {
if (item.id == appName || item.appName == appName)
return item.id;
}
if (m_appNamesMap.contains(appName)) {
return m_appNamesMap.value(appName).toString();
}
return QString();
}
void NotificationManager::doActionInvoked(const NotifyEntity &entity, const QString &actionId)
{
qDebug(notifyLog) << "Invoke the notification:" << entity.id() << entity.appName() << actionId;
QMap<QString, QVariant> hints = entity.hints();
QMap<QString, QVariant>::const_iterator i = hints.constBegin();
while (i != hints.constEnd()) {
if (i.key() == "x-deepin-action-" + actionId) {
QStringList args;
if (i.value().typeId() == QMetaType::QStringList) {
args = i.value().toStringList();
} else {
qDebug(notifyLog) << "Deprecate hint format, use string list instead of string."
<< "actionId:" << actionId << ", value:" << i.value();
args = i.value().toString().split(",");
}
if (!args.isEmpty()) {
QString cmd = args.takeFirst(); // 命令
QStringList amArgs;
amArgs << "-c" << cmd;
if (!args.isEmpty()) {
amArgs << "--" << args;
}
QProcess pro;
pro.setProgram("dde-am");
pro.setArguments(amArgs);
QProcessEnvironment proEnv = QProcessEnvironment::systemEnvironment();
proEnv.remove("DSG_APP_ID");
pro.setProcessEnvironment(proEnv);
pro.startDetached();
}
} else if (i.key() == "deepin-dde-shell-action-" + actionId) {
const QString data(i.value().toString());
if (!invokeShellAction(data)) {
qWarning(notifyLog) << "Failed to invoke the action" << actionId;
}
}
++i;
}
}
// Helper function to convert QList to variadic parameters.
template<typename... Args, std::size_t... Is>
static bool invokeMethodWithListImpl(QObject *obj, const char *member, Qt::ConnectionType c, const QList<QGenericArgument> &list, std::index_sequence<Is...>)
{
return QMetaObject::invokeMethod(obj, member, c, list[Is]...);
}
static bool invokeMethodWithList(QObject *obj, const char *member, Qt::ConnectionType c, const QVariantList &list)
{
const int MaxArgumentCount = 9;
QList<QGenericArgument> args;
args.resize(MaxArgumentCount);
for (int i = 0; i < list.size(); i++) {
const auto &item = list.at(i);
args[i] = QGenericArgument{item.typeName(), item.data()};
}
return invokeMethodWithListImpl(obj, member, c, args, std::make_index_sequence<MaxArgumentCount>());
}
bool NotificationManager::invokeShellAction(const QString &data)
{
QJsonParseError error;
auto doc = QJsonDocument::fromJson(data.toLocal8Bit(), &error);
if (error.error != QJsonParseError::NoError || doc.isNull() || !doc.isObject()) {
qWarning(notifyLog) << "Failed to parse data" << error.errorString();
return false;
}
const QString PluginId("pluginId");
const QString Method("method");
QJsonObject root(doc.object());
if (!root.contains(PluginId) || !root.contains(Method)) {
qWarning(notifyLog) << "Incorrect format, missing 'pluginId' or 'method' keyword";
return false;
}
const auto pluginId = root[PluginId].toString();
DAppletBridge bridge(pluginId);
if (!bridge.isValid()) {
qWarning(notifyLog) << "Doesn't exit the applet" << pluginId;
return false;
}
auto meta = DS_NAMESPACE::DPluginLoader::instance()->plugin(pluginId);
const auto visiblity = meta.value("Visibility");
if (visiblity != "Public") {
qWarning(notifyLog) << "Can't access the applet" << pluginId;
return false;
}
const auto method = root[Method].toString();
const QVariantList arguments = root["arguments"].toArray().toVariantList();
if (auto applet = bridge.applet()) {
return invokeMethodWithList(applet, method.toStdString().c_str(), Qt::DirectConnection, arguments);
}
return false;
}
void NotificationManager::initScreenLockedState()
{
const QString interfaceAndServiceName = "org.deepin.dde.LockFront1";
const QString path = "/org/deepin/dde/LockFront1";
QDBusInterface interface(interfaceAndServiceName, path,
"org.freedesktop.DBus.Properties", QDBusConnection::sessionBus());
QDBusReply<QDBusVariant> reply = interface.call("Get", "org.deepin.dde.LockFront1", "Visible");
if (reply.isValid()) {
m_screenLocked = reply.value().variant().toBool();
} else {
m_screenLocked = false;
qWarning(notifyLog) << "Failed to get the lock visible property:" << reply.error().message();
}
QDBusConnection::sessionBus().connect(interfaceAndServiceName, path, interfaceAndServiceName,
"Visible", this, SLOT(onScreenLockedChanged(bool)));
}
void NotificationManager::onHandingPendingEntities()
{
QList<NotifyEntity> timeoutEntities;
const auto current = QDateTime::currentMSecsSinceEpoch();
for (auto iter = m_pendingTimeoutEntities.begin(); iter != m_pendingTimeoutEntities.end();) {
const auto point = iter.key();
if (point > current) {
iter++;
continue;
}
const auto entity = iter.value();;
timeoutEntities << entity;
iter = m_pendingTimeoutEntities.erase(iter);
}
// update pendingTimeout to deal with m_pendingTimeoutEntities
if (!m_pendingTimeoutEntities.isEmpty()) {
auto points = m_pendingTimeoutEntities.keys();
std::sort(points.begin(), points.end());
// find last point to restart pendingTimeout
m_lastTimeoutPoint = points.first();
auto newInterval = m_lastTimeoutPoint - current;
// let timer start in main thread
QMetaObject::invokeMethod(m_pendingTimeout, "start", Qt::QueuedConnection, Q_ARG(int, newInterval));
} else {
// reset m_lastTimeoutPoint
m_lastTimeoutPoint = std::numeric_limits<qint64>::max();
}
for (const auto &item : timeoutEntities) {
// Validate entity before processing timeout to prevent race conditions
if (!item.isValid()) {
qWarning(notifyLog) << "Skipping timeout processing for invalid entity id:" << item.id() << "appName:" << item.appName()
<< "cTime:" << item.cTime();
continue;
}
if (item.id() == m_blockClosedId) {
qDebug(notifyLog) << "bubble id:" << item.bubbleId() << "entity id:" << item.id();
m_pendingTimeoutEntities.insert(current + 1000, item);
continue;
}
qDebug(notifyLog) << "Expired for the notification " << item.id() << item.appName();
notificationClosed(item.id(), item.bubbleId(), NotifyEntity::Expired);
}
}
void NotificationManager::removePendingEntity(const NotifyEntity &entity)
{
for (auto iter = m_pendingTimeoutEntities.begin(); iter != m_pendingTimeoutEntities.end();) {
const auto item = iter.value();
if (item == entity || (entity.isReplace() && item.bubbleId() == entity.bubbleId())) {
m_pendingTimeoutEntities.erase(iter);
onHandingPendingEntities();
break;
}
++iter;
}
}
void NotificationManager::onScreenLockedChanged(bool screenLocked)
{
m_screenLocked = screenLocked;
}
} // notification