-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin notification to show notification center. Notification is a fixed plugin. Log: add plugin notification Issue: linuxdeepin/developer-center#6695
- Loading branch information
Showing
16 changed files
with
459 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
set(PLUGIN_NAME "notification") | ||
find_package(Qt5 REQUIRED COMPONENTS DBus) | ||
find_package(Dtk REQUIRED COMPONENTS Widget) | ||
|
||
add_library(${PLUGIN_NAME} SHARED | ||
notification.h | ||
notification.cpp | ||
notificationplugin.h | ||
notificationplugin.cpp | ||
${CMAKE_SOURCE_DIR}/widgets/tipswidget.h | ||
${CMAKE_SOURCE_DIR}/widgets/tipswidget.cpp | ||
notification.qrc | ||
) | ||
target_compile_definitions(${PLUGIN_NAME} PRIVATE QT_PLUGIN) | ||
set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins) | ||
|
||
target_include_directories(${PLUGIN_NAME} PRIVATE | ||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/interfaces> | ||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/widgets> | ||
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/frame/qtdbusextended> | ||
) | ||
|
||
target_link_libraries(${PLUGIN_NAME} PRIVATE | ||
Dtk::Widget | ||
Qt5::DBus | ||
) | ||
|
||
install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
#include "notification.h" | ||
#include "constants.h" | ||
|
||
#include <QPainter> | ||
#include <QPainterPath> | ||
#include <QMouseEvent> | ||
#include <QApplication> | ||
#include <QIcon> | ||
#include <QDBusInterface> | ||
#include <QDBusReply> | ||
#include <QtConcurrent/QtConcurrent> | ||
|
||
#include <DStyle> | ||
#include <DGuiApplicationHelper> | ||
|
||
Q_DECLARE_LOGGING_CATEGORY(qLcPluginNotification) | ||
|
||
DWIDGET_USE_NAMESPACE; | ||
DCORE_USE_NAMESPACE; | ||
Notification::Notification(QWidget *parent) | ||
: QWidget(parent) | ||
, m_icon(QIcon::fromTheme("notification")) | ||
, m_dbus(nullptr) | ||
, m_dndMode(false) | ||
{ | ||
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE); | ||
connect(this, &Notification::dndModeChanged, this, &Notification::refreshIcon); | ||
QtConcurrent::run([this](){ | ||
m_dbus = new QDBusInterface("org.deepin.dde.Notification1", "/org/deepin/dde/Notification1", "org.deepin.dde.Notification1", QDBusConnection::sessionBus(), this); | ||
// Refresh icon for the first time, cause org.deepin.dde.Notification1 might depend on dock's DBus, | ||
// we should not call org.deepin.dde.Notification1 in the main thread before dock's dbus is initialized. | ||
// Just refresh icon in the other thread. | ||
QDBusReply<QDBusVariant> dnd = m_dbus->call(QLatin1String("GetSystemInfo"), QVariant::fromValue(0u)); | ||
if (!dnd.isValid()) { | ||
qCWarning(qLcPluginNotification) << dnd.error(); | ||
return ; | ||
} | ||
m_dndMode = dnd.value().variant().toBool(); | ||
refreshIcon(); | ||
QDBusConnection::sessionBus().connect("org.deepin.dde.Notification1", | ||
"/org/deepin/dde/Notification1", | ||
"org.deepin.dde.Notification1", | ||
"SystemInfoChanged", | ||
this, | ||
SLOT(onSystemInfoChanged(quint32,QDBusVariant)) | ||
); | ||
}); | ||
} | ||
|
||
QIcon Notification::icon() const | ||
{ | ||
return m_icon; | ||
} | ||
|
||
void Notification::refreshIcon() | ||
{ | ||
m_icon = QIcon::fromTheme(m_dndMode ? "notification-off" : "notification"); | ||
Q_EMIT iconRefreshed(); | ||
} | ||
|
||
bool Notification::dndMode() const | ||
{ | ||
return m_dndMode; | ||
} | ||
|
||
void Notification::setDndMode(bool dnd) | ||
{ | ||
if (m_dbus) { | ||
m_dbus->call(QLatin1String("SetSystemInfo"), QVariant::fromValue(0u), QVariant::fromValue(QDBusVariant(dnd))); | ||
} | ||
} | ||
|
||
void Notification::paintEvent(QPaintEvent *e) | ||
{ | ||
Q_UNUSED(e) | ||
QPainter p(this); | ||
m_icon.paint(&p, rect()); | ||
} | ||
|
||
void Notification::onSystemInfoChanged(quint32 info, QDBusVariant value) | ||
{ | ||
if (info == 0) { | ||
// DND mode | ||
m_dndMode = value.variant().toBool(); | ||
Q_EMIT dndModeChanged(m_dndMode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
#ifndef NOTIFICATION_H | ||
#define NOTIFICATION_H | ||
|
||
#include <DGuiApplicationHelper> | ||
|
||
#include <QWidget> | ||
#include <QIcon> | ||
#include <QDBusVariant> | ||
#include <QDBusInterface> | ||
|
||
class Notification : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit Notification(QWidget *parent = nullptr); | ||
QIcon icon() const; | ||
|
||
bool dndMode() const; | ||
void setDndMode(bool dnd); | ||
|
||
Q_SIGNALS: | ||
void iconRefreshed(); | ||
void dndModeChanged(bool dnd); | ||
|
||
public Q_SLOTS: | ||
void refreshIcon(); | ||
|
||
private Q_SLOTS: | ||
void onSystemInfoChanged(quint32 info, QDBusVariant value); | ||
|
||
protected: | ||
void paintEvent(QPaintEvent *e) override; | ||
|
||
private: | ||
QIcon m_icon; | ||
QDBusInterface *m_dbus; | ||
bool m_dndMode; | ||
}; | ||
|
||
#endif // NOTIFICATION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"api": "2.0.0", | ||
"depends-daemon-dbus-service": "org.deepin.dde.Notification1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<RCC> | ||
<qresource prefix="/dsg/built-in-icons"> | ||
<file alias="notification.dci">icons/notification.dci</file> | ||
<file alias="notification-off.dci">icons/notification-off.dci</file> | ||
</qresource> | ||
</RCC> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd. | ||
// | ||
// SPDX-License-Identifier: LGPL-3.0-or-later | ||
#include "notificationplugin.h" | ||
|
||
#include <DGuiApplicationHelper> | ||
|
||
#include <QIcon> | ||
#include <QSettings> | ||
#include <QPainter> | ||
|
||
Q_LOGGING_CATEGORY(qLcPluginNotification, "dock.plugin.notification") | ||
|
||
#define PLUGIN_STATE_KEY "enable" | ||
#define TOGGLE_DND "toggle-dnd" | ||
|
||
DGUI_USE_NAMESPACE | ||
using namespace Dock; | ||
|
||
NotificationPlugin::NotificationPlugin(QObject *parent) | ||
: QObject(parent) | ||
, m_pluginLoaded(false) | ||
, m_notification(nullptr) | ||
, m_tipsLabel(new TipsWidget) | ||
{ | ||
m_tipsLabel->setText(tr("Notification")); | ||
m_tipsLabel->setVisible(false); | ||
m_tipsLabel->setAccessibleName("Notification"); | ||
m_tipsLabel->setObjectName("NotificationTipsLabel"); | ||
} | ||
|
||
QWidget *NotificationPlugin::itemWidget(const QString &itemKey) | ||
{ | ||
Q_UNUSED(itemKey) | ||
return m_notification.data(); | ||
} | ||
|
||
QWidget *NotificationPlugin::itemTipsWidget(const QString &itemKey) | ||
{ | ||
Q_UNUSED(itemKey); | ||
return m_tipsLabel.data(); | ||
} | ||
|
||
void NotificationPlugin::init(PluginProxyInterface *proxyInter) | ||
{ | ||
m_proxyInter = proxyInter; | ||
|
||
if (!pluginIsDisable()) { | ||
loadPlugin(); | ||
} | ||
} | ||
|
||
void NotificationPlugin::pluginStateSwitched() | ||
{ | ||
m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable()); | ||
refreshPluginItemsVisible(); | ||
} | ||
|
||
bool NotificationPlugin::pluginIsDisable() | ||
{ | ||
return !(m_proxyInter->getValue(this, PLUGIN_STATE_KEY, true).toBool()); | ||
} | ||
|
||
const QString NotificationPlugin::itemCommand(const QString &itemKey) | ||
{ | ||
Q_UNUSED(itemKey); | ||
return QString("dbus-send --session --print-reply --dest=org.deepin.dde.Widgets1 /org/deepin/dde/Widgets1 org.deepin.dde.Widgets1.Toggle"); | ||
} | ||
|
||
const QString NotificationPlugin::itemContextMenu(const QString &itemKey) | ||
{ | ||
QList<QVariant> items; | ||
QMap<QString, QVariant> toggleDnd; | ||
toggleDnd["itemId"] = TOGGLE_DND; | ||
toggleDnd["itemText"] = tr("Do Not Disturb"); | ||
toggleDnd["isCheckable"] = true; | ||
toggleDnd["isActive"] = true; | ||
toggleDnd["checked"] = m_notification->dndMode(); | ||
items.push_back(toggleDnd); | ||
QMap<QString, QVariant> menu; | ||
menu["items"] = items; | ||
menu["checkableMenu"] = true; | ||
menu["singleCheck"] = false; | ||
return QJsonDocument::fromVariant(menu).toJson(); | ||
} | ||
|
||
void NotificationPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) | ||
{ | ||
Q_UNUSED(itemKey) | ||
if (menuId == TOGGLE_DND) { | ||
m_notification->setDndMode(checked); | ||
} | ||
} | ||
|
||
int NotificationPlugin::itemSortKey(const QString &itemKey) | ||
{ | ||
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient); | ||
return m_proxyInter->getValue(this, key, 3).toInt(); | ||
} | ||
|
||
void NotificationPlugin::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 NotificationPlugin::pluginSettingsChanged() | ||
{ | ||
refreshPluginItemsVisible(); | ||
} | ||
|
||
QIcon NotificationPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::ColorType themeType) | ||
{ | ||
Q_UNUSED(themeType) | ||
if (dockPart == DockPart::DCCSetting) | ||
return QIcon::fromTheme("notification"); | ||
return m_notification->icon(); | ||
} | ||
|
||
void NotificationPlugin::loadPlugin() | ||
{ | ||
if (m_pluginLoaded) { | ||
return; | ||
} | ||
m_pluginLoaded = true; | ||
m_notification.reset(new Notification); | ||
connect(m_notification.data(), &Notification::iconRefreshed, this, [this]() { m_proxyInter->itemUpdate(this, pluginName()); }); | ||
m_proxyInter->itemAdded(this, pluginName()); | ||
} | ||
|
||
void NotificationPlugin::refreshPluginItemsVisible() | ||
{ | ||
if (pluginIsDisable()) | ||
{ | ||
m_proxyInter->itemRemoved(this, pluginName()); | ||
} else { | ||
if (!m_pluginLoaded) { | ||
loadPlugin(); | ||
return; | ||
} | ||
m_proxyInter->itemAdded(this, pluginName()); | ||
} | ||
} | ||
|
||
void NotificationPlugin::refreshIcon(const QString &itemKey) | ||
{ | ||
Q_UNUSED(itemKey) | ||
m_notification->refreshIcon(); | ||
} |
Oops, something went wrong.