Skip to content

Commit

Permalink
feat: use dbus for notifications
Browse files Browse the repository at this point in the history
Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Oct 18, 2023
1 parent 0972b0b commit 6082df4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
47 changes: 47 additions & 0 deletions src/powerkit_notify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
# PowerKit <https://github.com/rodlie/powerkit>
# Copyright (c) 2018-2022 Ole-André Rodlie <[email protected]> All rights reserved.
#
# Available under the 3-clause BSD license
# See the LICENSE file for full details
*/

#include "powerkit_notify.h"
#include "powerkit_def.h"

SystemNotification::SystemNotification(QObject* parent)
: QObject(parent)
, m_interface(nullptr)
{
m_interface =
new QDBusInterface(QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
QStringLiteral("org.freedesktop.Notifications"),
QDBusConnection::sessionBus(),
this);
}

void SystemNotification::sendMessage(const QString& text,
const QString& title,
const bool critical)
{
QList<QVariant> args;
QVariantMap hintsMap;
int timeout = 5000;

if (critical) {
hintsMap.insert("urgency", QVariant::fromValue(uchar(2)));
timeout = 0;
}

args << (qAppName()) // appname
<< static_cast<unsigned int>(0) // id
<< DEFAULT_NOTIFY_ICON // icon
<< title // summary
<< text // body
<< QStringList() // actions
<< hintsMap // hints
<< timeout; // timeout
m_interface->callWithArgumentList(
QDBus::AutoDetect, QStringLiteral("Notify"), args);
}
35 changes: 35 additions & 0 deletions src/powerkit_notify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
# PowerKit <https://github.com/rodlie/powerkit>
# Copyright (c) 2018-2022 Ole-André Rodlie <[email protected]> All rights reserved.
#
# Available under the 3-clause BSD license
# See the LICENSE file for full details
*/

#ifndef NOTIFY_H
#define NOTIFY_H

#include <QObject>
#include <QApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QVariant>

class QDBusInterface;

class SystemNotification : public QObject
{
Q_OBJECT

public:
explicit SystemNotification(QObject* parent = nullptr);

void sendMessage(const QString& text,
const QString& title,
const bool critical);

private:
QDBusInterface* m_interface;
};

#endif // NOTIFY_H
11 changes: 3 additions & 8 deletions src/powerkit_systray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "powerkit_systray.h"
#include "powerkit_def.h"
#include "powerkit_theme.h"
#include "powerkit_notify.h"
#include "powerkit_settings.h"
#include "powerkit_backlight.h"
#include "powerkit_cpu.h"
Expand Down Expand Up @@ -898,14 +899,8 @@ void SysTray::showMessage(const QString &title,
const QString &msg,
bool critical)
{
if (tray->isVisible() && showNotifications) {
if (critical) {
tray->showMessage(title, msg,
QSystemTrayIcon::Critical,
900000);
} else {
tray->showMessage(title, msg);
}
if (showNotifications) {
SystemNotification().sendMessage(title, msg, critical);
}
}

Expand Down

0 comments on commit 6082df4

Please sign in to comment.