From 89bbcd7c3821985bb2bca51247394fd4a65b25bf Mon Sep 17 00:00:00 2001 From: justforlxz Date: Fri, 1 Mar 2019 13:59:35 +0800 Subject: [PATCH] feat: add DNotifySender Notify encapsulation of org.freedesktop.Notifications for easy callers to send simple notifications Change-Id: If13a6b0051535e62049f3425682a41eee81857ef --- src/util/DNotifySender | 1 + src/util/dnotifysender.cpp | 114 +++++++++++++++++++++++++++++++++++++ src/util/dnotifysender.h | 32 +++++++++++ src/util/util.pri | 9 ++- 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 src/util/DNotifySender create mode 100644 src/util/dnotifysender.cpp create mode 100644 src/util/dnotifysender.h diff --git a/src/util/DNotifySender b/src/util/DNotifySender new file mode 100644 index 00000000..85796fc7 --- /dev/null +++ b/src/util/DNotifySender @@ -0,0 +1 @@ +#include "dnotifysender.h" diff --git a/src/util/dnotifysender.cpp b/src/util/dnotifysender.cpp new file mode 100644 index 00000000..caf1bc71 --- /dev/null +++ b/src/util/dnotifysender.cpp @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2017 ~ 2019 Deepin Technology Co., Ltd. + * + * Author: justforlxz + * + * Maintainer: justforlxz + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "dnotifysender.h" +#include "ddbussender.h" + +DCORE_BEGIN_NAMESPACE + +namespace DUtil { + +struct DNotifyData { + uint m_replaceId; + int m_timeOut; + QString m_body; + QString m_summary; + QString m_appIcon; + QString m_appName; + QStringList m_actions; + QVariantMap m_hints; +}; + +DNotifySender::DNotifySender(const QString &summary) : m_dbusData(std::make_shared()) +{ + m_dbusData->m_summary = summary; +} + +DNotifySender DNotifySender::appName(const QString &appName) +{ + m_dbusData->m_appName = appName; + + return *this; +} + +DNotifySender DNotifySender::appIcon(const QString &appIcon) +{ + m_dbusData->m_appIcon = appIcon; + + return *this; +} + +DNotifySender DNotifySender::appBody(const QString &appBody) +{ + m_dbusData->m_body = appBody; + + return *this; +} + +DNotifySender DNotifySender::replaceId(const uint replaceId) +{ + m_dbusData->m_replaceId = replaceId; + + return *this; +} + +DNotifySender DNotifySender::timeOut(const int timeOut) +{ + m_dbusData->m_timeOut = timeOut; + + return *this; +} + +DNotifySender DNotifySender::actions(const QStringList &actions) +{ + m_dbusData->m_actions = actions; + + return *this; +} + +DNotifySender DNotifySender::hints(const QVariantMap &hints) +{ + m_dbusData->m_hints = hints; + + return *this; +} + +QDBusPendingCall DNotifySender::call() +{ + return DDBusSender() + .service("org.freedesktop.Notifications") + .path("/org/freedesktop/Notifications") + .interface("org.freedesktop.Notifications") + .method(QString("Notify")) + .arg(m_dbusData->m_appName) + .arg(m_dbusData->m_replaceId) + .arg(m_dbusData->m_appIcon) + .arg(m_dbusData->m_summary) + .arg(m_dbusData->m_body) + .arg(m_dbusData->m_actions) + .arg(m_dbusData->m_hints) + .arg(m_dbusData->m_timeOut) + .call(); +} + +} // namespace DUtil + +DCORE_END_NAMESPACE diff --git a/src/util/dnotifysender.h b/src/util/dnotifysender.h new file mode 100644 index 00000000..c05b85c4 --- /dev/null +++ b/src/util/dnotifysender.h @@ -0,0 +1,32 @@ +#ifndef DNOTIFYSENDER_H +#define DNOTIFYSENDER_H + +#include "dtkcore_global.h" + +#include +#include + +DCORE_BEGIN_NAMESPACE + +namespace DUtil { +struct DNotifyData; +class LIBDTKCORESHARED_EXPORT DNotifySender { +public: + DNotifySender(const QString &summary); + DNotifySender appName(const QString &appName = QString()); + DNotifySender appIcon(const QString &appIcon = QString()); + DNotifySender appBody(const QString &appBody = QString()); + DNotifySender replaceId(const uint replaceId = 0); + DNotifySender timeOut(const int timeOut = -1); + DNotifySender actions(const QStringList &actions = QStringList()); + DNotifySender hints(const QVariantMap &hints = QVariantMap()); + QDBusPendingCall call(); + +private: + std::shared_ptr m_dbusData; +}; +} // namespace DUtil + +DCORE_END_NAMESPACE + +#endif // DNOTIFYSENDER_H diff --git a/src/util/util.pri b/src/util/util.pri index d71404ba..f48c43d1 100644 --- a/src/util/util.pri +++ b/src/util/util.pri @@ -5,7 +5,8 @@ HEADERS += \ $$PWD/dabstractunitformatter.h \ $$PWD/ddisksizeformatter.h \ $$PWD/ddbussender.h \ - $$PWD/drecentmanager.h + $$PWD/drecentmanager.h \ + $$PWD/dnotifysender.h INCLUDEPATH += $$PWD @@ -14,7 +15,8 @@ includes.files += \ $$PWD/DUtil \ $$PWD/DPinyin \ $$PWD/DDBusSender \ - $$PWD/DRecentManager + $$PWD/DRecentManager \ + $$PWD/DNotifySender RESOURCES += \ $$PWD/util.qrc @@ -24,4 +26,5 @@ SOURCES += \ $$PWD/dabstractunitformatter.cpp \ $$PWD/ddisksizeformatter.cpp \ $$PWD/ddbussender.cpp \ - $$PWD/drecentmanager.cpp + $$PWD/drecentmanager.cpp \ + $$PWD/dnotifysender.cpp