diff --git a/plugins/notification/notification.cpp b/plugins/notification/notification.cpp index d51939c78..04e68a4e8 100644 --- a/plugins/notification/notification.cpp +++ b/plugins/notification/notification.cpp @@ -23,6 +23,7 @@ DCORE_USE_NAMESPACE; Notification::Notification(QWidget *parent) : QWidget(parent) , m_icon(QIcon::fromTheme("notification")) + , m_notificationCount(0) , m_dbus(nullptr) , m_dndMode(false) { @@ -36,10 +37,10 @@ Notification::Notification(QWidget *parent) QDBusReply dnd = m_dbus->call(QLatin1String("GetSystemInfo"), QVariant::fromValue(0u)); if (!dnd.isValid()) { qCWarning(qLcPluginNotification) << dnd.error(); - return ; + } else { + m_dndMode = dnd.value().variant().toBool(); + refreshIcon(); } - m_dndMode = dnd.value().variant().toBool(); - refreshIcon(); QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1", "/org/deepin/dde/Notification1", "org.deepin.dde.Notification1", @@ -47,6 +48,19 @@ Notification::Notification(QWidget *parent) this, SLOT(onSystemInfoChanged(quint32,QDBusVariant)) ); + auto recordCountVariant = m_dbus->property("recordCount"); + if (!recordCountVariant.isValid()) { + qCWarning(qLcPluginNotification) << dnd.error(); + } else { + setNotificationCount(recordCountVariant.toUInt()); + } + QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1", + "/org/deepin/dde/Notification1", + "org.deepin.dde.Notification1", + "recordCountChanged", + this, + SLOT(setNotificationCount(uint)) + ); }); } @@ -57,7 +71,7 @@ QIcon Notification::icon() const void Notification::refreshIcon() { - m_icon = QIcon::fromTheme(m_dndMode ? "notification-off" : "notification"); + m_icon = QIcon::fromTheme(dndMode() ? "notification-off" : "notification"); Q_EMIT iconRefreshed(); } @@ -73,6 +87,11 @@ void Notification::setDndMode(bool dnd) } } +uint Notification::notificationCount() const +{ + return m_notificationCount; +} + void Notification::paintEvent(QPaintEvent *e) { Q_UNUSED(e) @@ -88,3 +107,12 @@ void Notification::onSystemInfoChanged(quint32 info, QDBusVariant value) Q_EMIT dndModeChanged(m_dndMode); } } + +void Notification::setNotificationCount(uint count) +{ + if (m_notificationCount == count) { + return; + } + m_notificationCount = count; + Q_EMIT this->notificationCountChanged(count); +} diff --git a/plugins/notification/notification.h b/plugins/notification/notification.h index 02722fe1c..e5bc42d35 100644 --- a/plugins/notification/notification.h +++ b/plugins/notification/notification.h @@ -15,28 +15,35 @@ class Notification : public QWidget { Q_OBJECT + Q_PROPERTY(bool dndMode READ dndMode WRITE setDndMode NOTIFY dndModeChanged) + Q_PROPERTY(uint notificationCount READ notificationCount NOTIFY notificationCountChanged) + public: explicit Notification(QWidget *parent = nullptr); QIcon icon() const; bool dndMode() const; void setDndMode(bool dnd); + uint notificationCount() const; Q_SIGNALS: void iconRefreshed(); void dndModeChanged(bool dnd); + void notificationCountChanged(uint count); public Q_SLOTS: void refreshIcon(); private Q_SLOTS: void onSystemInfoChanged(quint32 info, QDBusVariant value); + void setNotificationCount(uint count); protected: void paintEvent(QPaintEvent *e) override; private: QIcon m_icon; + uint m_notificationCount; QScopedPointer m_dbus; bool m_dndMode; }; diff --git a/plugins/notification/notificationplugin.cpp b/plugins/notification/notificationplugin.cpp index 31cde80c5..7d0988d2e 100644 --- a/plugins/notification/notificationplugin.cpp +++ b/plugins/notification/notificationplugin.cpp @@ -4,6 +4,7 @@ #include "notificationplugin.h" #include +#include #include #include @@ -11,8 +12,9 @@ Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification") -#define PLUGIN_STATE_KEY "enable" -#define TOGGLE_DND "toggle-dnd" +#define PLUGIN_STATE_KEY "enable" +#define TOGGLE_DND "toggle-dnd" +#define NOTIFICATION_SETTINGS "notification-settings" DGUI_USE_NAMESPACE using namespace Dock; @@ -23,7 +25,7 @@ NotificationPlugin::NotificationPlugin(QObject *parent) , m_notification(nullptr) , m_tipsLabel(new TipsWidget) { - m_tipsLabel->setText(tr("Notification")); + m_tipsLabel->setText(tr("No messages")); m_tipsLabel->setVisible(false); m_tipsLabel->setAccessibleName("Notification"); m_tipsLabel->setObjectName("NotificationTipsLabel"); @@ -72,14 +74,19 @@ const QString NotificationPlugin::itemContextMenu(const QString &itemKey) QList items; QMap toggleDnd; toggleDnd["itemId"] = TOGGLE_DND; - toggleDnd["itemText"] = tr("Do Not Disturb"); - toggleDnd["isCheckable"] = true; + toggleDnd["itemText"] = toggleDndText(); + toggleDnd["isCheckable"] = false; toggleDnd["isActive"] = true; - toggleDnd["checked"] = m_notification->dndMode(); items.push_back(toggleDnd); + QMap notificationSettings; + notificationSettings["itemId"] = NOTIFICATION_SETTINGS; + notificationSettings["itemText"] = tr("Notification settings"); + notificationSettings["isCheckable"] = false; + notificationSettings["isActive"] = true; + items.push_back(notificationSettings); QMap menu; menu["items"] = items; - menu["checkableMenu"] = true; + menu["checkableMenu"] = false; menu["singleCheck"] = false; return QJsonDocument::fromVariant(menu).toJson(); } @@ -87,8 +94,15 @@ const QString NotificationPlugin::itemContextMenu(const QString &itemKey) void NotificationPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) { Q_UNUSED(itemKey) + Q_UNUSED(checked) if (menuId == TOGGLE_DND) { - m_notification->setDndMode(checked); + m_notification->setDndMode(!m_notification->dndMode()); + } else if (menuId == NOTIFICATION_SETTINGS) { + DDBusSender().service("org.deepin.dde.ControlCenter1") + .path("/org/deepin/dde/ControlCenter1") + .interface("org.deepin.dde.ControlCenter1") + .method("ShowPage") + .arg(QString("notification")).call(); } } @@ -125,6 +139,7 @@ void NotificationPlugin::loadPlugin() m_pluginLoaded = true; m_notification.reset(new Notification); connect(m_notification.data(), &Notification::iconRefreshed, this, [this]() { m_proxyInter->itemUpdate(this, pluginName()); }); + connect(m_notification.data(), &Notification::notificationCountChanged, this, &NotificationPlugin::updateTipsText); m_proxyInter->itemAdded(this, pluginName()); } @@ -142,6 +157,24 @@ void NotificationPlugin::refreshPluginItemsVisible() } } +void NotificationPlugin::updateTipsText(uint notificationCount) +{ + if (notificationCount == 0) { + m_tipsLabel->setText(tr("No messages")); + } else { + m_tipsLabel->setText(QString("%1 %2").arg(notificationCount).arg(tr("Notifications"))); + } +} + +QString NotificationPlugin::toggleDndText() const +{ + if (m_notification->dndMode()) { + return tr("Turn off DND mode"); + } else { + return tr("Turn on DND mode"); + } +} + void NotificationPlugin::refreshIcon(const QString &itemKey) { Q_UNUSED(itemKey) diff --git a/plugins/notification/notificationplugin.h b/plugins/notification/notificationplugin.h index 184113c72..5e56e16be 100644 --- a/plugins/notification/notificationplugin.h +++ b/plugins/notification/notificationplugin.h @@ -43,6 +43,8 @@ public: private: void loadPlugin(); void refreshPluginItemsVisible(); + void updateTipsText(uint notificationCount); + QString toggleDndText() const; private: bool m_pluginLoaded; diff --git a/translations/dde-dock.ts b/translations/dde-dock.ts index f6f6ef44f..a1dec3002 100644 --- a/translations/dde-dock.ts +++ b/translations/dde-dock.ts @@ -336,8 +336,24 @@ Notification - Do Not Disturb - Do Not Disturb + No messages + No messages + + + Notification settings + Notification settings + + + Notifications + Notifications + + + Turn off DND mode + Turn off DND mode + + + Turn on DND mode + Turn on DND mode diff --git a/translations/dde-dock_en_US.ts b/translations/dde-dock_en_US.ts index 00154c336..c4c52f77f 100644 --- a/translations/dde-dock_en_US.ts +++ b/translations/dde-dock_en_US.ts @@ -308,8 +308,24 @@ Notification - Do Not Disturb - Do Not Disturb + No messages + No messages + + + Notification settings + Notification settings + + + Notifications + Notifications + + + Turn off DND mode + Turn off DND mode + + + Turn on DND mode + Turn on DND mode diff --git a/translations/dde-dock_zh_CN.ts b/translations/dde-dock_zh_CN.ts index 78de7a197..d9555a78e 100644 --- a/translations/dde-dock_zh_CN.ts +++ b/translations/dde-dock_zh_CN.ts @@ -336,8 +336,24 @@ 通知 - Do Not Disturb - 勿扰模式 + No messages + 暂无新消息 + + + Notification settings + 通知设置 + + + Notifications + 条通知 + + + Turn off DND mode + 关闭勿扰模式 + + + Turn on DND mode + 开启勿扰模式