File indexing completed on 2024-04-21 11:35:05

0001 /*
0002     SPDX-FileCopyrightText: 2005-2006 Olivier Goffart <ogoffart at kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "notifybyexecute.h"
0008 
0009 #include <QGuiApplication>
0010 #include <QHash>
0011 #include <QWidget>
0012 
0013 #include "debug_p.h"
0014 #include "knotification.h"
0015 #include <KProcess>
0016 #include <knotifyconfig.h>
0017 
0018 #include <KMacroExpander>
0019 
0020 NotifyByExecute::NotifyByExecute(QObject *parent)
0021     : KNotificationPlugin(parent)
0022 {
0023 }
0024 
0025 NotifyByExecute::~NotifyByExecute()
0026 {
0027 }
0028 
0029 void NotifyByExecute::notify(KNotification *notification, KNotifyConfig *config)
0030 {
0031     QString command = config->readEntry(QStringLiteral("Execute"));
0032 
0033     if (!command.isEmpty()) {
0034         QHash<QChar, QString> subst;
0035         subst.insert(QLatin1Char('e'), notification->eventId());
0036         subst.insert(QLatin1Char('a'), notification->appName());
0037         subst.insert(QLatin1Char('s'), notification->text());
0038         if (notification->widget()) {
0039             subst.insert(QLatin1Char('w'), QString::number(notification->widget()->topLevelWidget()->winId()));
0040             subst.insert(QLatin1Char('t'), notification->widget()->topLevelWidget()->windowTitle());
0041         } else {
0042             subst.insert(QLatin1Char('w'), QStringLiteral("0"));
0043         }
0044         subst.insert(QLatin1Char('i'), QString::number(notification->id()));
0045         subst.insert(QLatin1Char('d'), QGuiApplication::applicationDisplayName());
0046 
0047         QString execLine = KMacroExpander::expandMacrosShellQuote(command, subst);
0048         if (execLine.isEmpty()) {
0049             execLine = command; // fallback
0050         }
0051 
0052         KProcess proc;
0053         proc.setShellCommand(execLine.trimmed());
0054         if (!proc.startDetached()) {
0055             qCDebug(LOG_KNOTIFICATIONS) << "KProcess returned an error while trying to execute this command:" << execLine;
0056         }
0057     }
0058 
0059     finish(notification);
0060 }
0061 
0062 #include "moc_notifybyexecute.cpp"