1 /*
2     SPDX-FileCopyrightText: 2005-2006 Olivier Goffart <ogoffart at kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "notifybyexecute.h"
8 
9 #include <QGuiApplication>
10 #include <QHash>
11 #include <QWidget>
12 
13 #include "debug_p.h"
14 #include "knotification.h"
15 #include <KProcess>
16 #include <knotifyconfig.h>
17 
18 #include <KMacroExpander>
19 
NotifyByExecute(QObject * parent)20 NotifyByExecute::NotifyByExecute(QObject *parent)
21     : KNotificationPlugin(parent)
22 {
23 }
24 
~NotifyByExecute()25 NotifyByExecute::~NotifyByExecute()
26 {
27 }
28 
notify(KNotification * notification,KNotifyConfig * config)29 void NotifyByExecute::notify(KNotification *notification, KNotifyConfig *config)
30 {
31     QString command = config->readEntry(QStringLiteral("Execute"));
32 
33     if (!command.isEmpty()) {
34         QHash<QChar, QString> subst;
35         subst.insert(QLatin1Char('e'), notification->eventId());
36         subst.insert(QLatin1Char('a'), notification->appName());
37         subst.insert(QLatin1Char('s'), notification->text());
38         if (notification->widget()) {
39             subst.insert(QLatin1Char('w'), QString::number(notification->widget()->topLevelWidget()->winId()));
40             subst.insert(QLatin1Char('t'), notification->widget()->topLevelWidget()->windowTitle());
41         } else {
42             subst.insert(QLatin1Char('w'), QStringLiteral("0"));
43         }
44         subst.insert(QLatin1Char('i'), QString::number(notification->id()));
45         subst.insert(QLatin1Char('d'), QGuiApplication::applicationDisplayName());
46 
47         QString execLine = KMacroExpander::expandMacrosShellQuote(command, subst);
48         if (execLine.isEmpty()) {
49             execLine = command; // fallback
50         }
51 
52         KProcess proc;
53         proc.setShellCommand(execLine.trimmed());
54         if (!proc.startDetached()) {
55             qCDebug(LOG_KNOTIFICATIONS) << "KProcess returned an error while trying to execute this command:" << execLine;
56         }
57     }
58 
59     finish(notification);
60 }
61