Skip to content

Commit

Permalink
Merge remote-tracking branch 'pr1/dbus-notif' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Jan 28, 2024
2 parents 111e49f + a8ddabd commit b6467d1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Dependencies
run: sudo apt-get install tree cmake qtbase5-dev libxss-dev libxrandr-dev
- name: Build
Expand Down
5 changes: 4 additions & 1 deletion src/powerkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#define LOGIND_MANAGER "org.freedesktop.login1.Manager"
#define LOGIND_DOCKED "Docked"

#define NOTIFY_SERVICE "org.freedesktop.Notifications"
#define NOTIFY_PATH "/org/freedesktop/Notifications"

#define UPOWER_PATH "/org/freedesktop/UPower"
#define UPOWER_MANAGER "org.freedesktop.UPower"
#define UPOWER_DEVICES "/org/freedesktop/UPower/devices/"
Expand Down Expand Up @@ -179,7 +182,7 @@ private slots:
quint32 cookie);
void handleDelInhibitScreenSaver(quint32 cookie);
void handleDelInhibitPowerManagement(quint32 cookie);

bool registerSuspendLock();
void setWakeAlarmFromSettings();

Expand Down
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"
#include "powerkit.h"

SystemNotification::SystemNotification(QObject* parent)
: QObject(parent)
{
dbus =
new QDBusInterface(NOTIFY_SERVICE,
NOTIFY_PATH,
NOTIFY_SERVICE,
QDBusConnection::sessionBus(),
this);
valid = dbus->isValid();
}

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
dbus->callWithArgumentList( QDBus::AutoDetect, QString("Notify"), args);
}
36 changes: 36 additions & 0 deletions src/powerkit_notify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
# 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);
bool valid;

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

private:
QDBusInterface *dbus;
};

#endif // NOTIFY_H
20 changes: 13 additions & 7 deletions src/powerkit_systray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -897,13 +898,18 @@ 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 notifier;
if (notifier.valid) {
notifier.sendMessage(title, msg, critical);
} else if (tray->isVisible()) {
if (critical) {
tray->showMessage(title, msg,
QSystemTrayIcon::Critical,
900000);
} else {
tray->showMessage(title, msg);
}
}
}
}
Expand Down

0 comments on commit b6467d1

Please sign in to comment.