Skip to content

Commit

Permalink
feat: add DNotifySender
Browse files Browse the repository at this point in the history
Notify encapsulation of org.freedesktop.Notifications for easy callers
to send simple notifications

Change-Id: If13a6b0051535e62049f3425682a41eee81857ef
  • Loading branch information
justforlxz committed Mar 5, 2019
1 parent 62c6912 commit 89bbcd7
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/util/DNotifySender
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "dnotifysender.h"
114 changes: 114 additions & 0 deletions src/util/dnotifysender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2017 ~ 2019 Deepin Technology Co., Ltd.
*
* Author: justforlxz <zhangdingyuan@deepin.com>
*
* Maintainer: justforlxz <zhangdingyuan@deepin.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/

#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<DNotifyData>())
{
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
32 changes: 32 additions & 0 deletions src/util/dnotifysender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DNOTIFYSENDER_H
#define DNOTIFYSENDER_H

#include "dtkcore_global.h"

#include <QDBusPendingCall>
#include <memory>

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<DNotifyData> m_dbusData;
};
} // namespace DUtil

DCORE_END_NAMESPACE

#endif // DNOTIFYSENDER_H
9 changes: 6 additions & 3 deletions src/util/util.pri
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,7 +15,8 @@ includes.files += \
$$PWD/DUtil \
$$PWD/DPinyin \
$$PWD/DDBusSender \
$$PWD/DRecentManager
$$PWD/DRecentManager \
$$PWD/DNotifySender

RESOURCES += \
$$PWD/util.qrc
Expand All @@ -24,4 +26,5 @@ SOURCES += \
$$PWD/dabstractunitformatter.cpp \
$$PWD/ddisksizeformatter.cpp \
$$PWD/ddbussender.cpp \
$$PWD/drecentmanager.cpp
$$PWD/drecentmanager.cpp \
$$PWD/dnotifysender.cpp

0 comments on commit 89bbcd7

Please sign in to comment.