File indexing completed on 2024-04-28 05:27:02
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Kai Uwe Broulik <kde@privat.broulik.de> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include <QCommandLineParser> 0008 #include <QCoreApplication> 0009 #include <QDBusConnection> 0010 #include <QDBusMessage> 0011 #include <QTimer> 0012 0013 #include <KAboutData> 0014 #include <KLocalizedString> 0015 0016 int main(int argc, char *argv[]) 0017 { 0018 QCoreApplication app(argc, argv); 0019 KLocalizedString::setApplicationDomain(QByteArrayLiteral("kbroadcastnotification")); 0020 0021 KAboutData aboutData(QStringLiteral("kbroadcastnotification"), 0022 i18n("Broadcast Notifications"), 0023 QLatin1String(PROJECT_VERSION), 0024 i18n("A tool that emits a notification for all users by sending it on the system DBus"), 0025 KAboutLicense::GPL, 0026 i18n("(c) 2016 Kai Uwe Broulik")); 0027 KAboutData::setApplicationData(aboutData); 0028 0029 QCommandLineParser parser; 0030 aboutData.setupCommandLine(&parser); 0031 0032 QCommandLineOption applicationNameOption(QStringLiteral("application"), 0033 i18n("Name of the application that should be associated with this notification"), 0034 QStringLiteral("application")); 0035 parser.addOption(applicationNameOption); 0036 QCommandLineOption summaryOption(QStringLiteral("summary"), i18n("A brief one-line summary of the notification"), QStringLiteral("summary")); 0037 parser.addOption(summaryOption); 0038 QCommandLineOption iconOption(QStringLiteral("icon"), i18n("Icon for the notification"), QStringLiteral("icon")); 0039 parser.addOption(iconOption); 0040 QCommandLineOption uidsOption( 0041 QStringLiteral("uids"), 0042 i18n("A comma-separated list of user IDs this notification should be sent to. If omitted, the notification will be sent to all users."), 0043 QStringLiteral("uids")); 0044 parser.addOption(uidsOption); 0045 QCommandLineOption timeoutOption(QStringLiteral("timeout"), i18n("Timeout for the notification"), QStringLiteral("timeout")); 0046 parser.addOption(timeoutOption); 0047 QCommandLineOption persistentOption(QStringLiteral("persistent"), i18n("Keep the notification in the history until the user closes it")); 0048 parser.addOption(persistentOption); 0049 0050 parser.addPositionalArgument(QStringLiteral("body"), i18n("The actual notification body text")); 0051 0052 parser.process(app); 0053 aboutData.processCommandLine(&parser); 0054 0055 QVariantMap properties; 0056 if (parser.isSet(applicationNameOption)) { 0057 properties.insert(QStringLiteral("appName"), parser.value(applicationNameOption)); 0058 } 0059 if (parser.isSet(summaryOption)) { 0060 properties.insert(QStringLiteral("summary"), parser.value(summaryOption)); 0061 } 0062 if (parser.isSet(iconOption)) { 0063 properties.insert(QStringLiteral("appIcon"), parser.value(iconOption)); 0064 } 0065 0066 if (parser.isSet(uidsOption)) { 0067 const QStringList &uids = parser.value(uidsOption).split(QLatin1Char(','), Qt::SkipEmptyParts); 0068 if (!uids.isEmpty()) { 0069 properties.insert(QStringLiteral("uids"), uids); 0070 } 0071 } 0072 0073 if (parser.isSet(timeoutOption)) { 0074 bool ok; 0075 const int timeout = parser.value(timeoutOption).toInt(&ok); 0076 if (ok) { 0077 properties.insert(QStringLiteral("timeout"), timeout); 0078 } 0079 } 0080 if (parser.isSet(persistentOption)) { // takes precedence over timeout if both are set 0081 properties.insert(QStringLiteral("timeout"), 0); // 0 = persistent, -1 = server default 0082 } 0083 0084 const auto &positionalArgs = parser.positionalArguments(); 0085 if (positionalArgs.count() == 1) { 0086 properties.insert(QStringLiteral("body"), positionalArgs.first()); 0087 } 0088 0089 if (properties.isEmpty()) { 0090 parser.showHelp(1); // never returns 0091 } 0092 0093 QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/org/kde/kbroadcastnotification"), 0094 QStringLiteral("org.kde.BroadcastNotifications"), 0095 QStringLiteral("Notify")); 0096 message.setArguments({properties}); 0097 QDBusConnection::systemBus().send(message); 0098 0099 // FIXME can we detect that the message was sent to the bus? 0100 QTimer::singleShot(500, &app, QCoreApplication::quit); 0101 0102 return app.exec(); 0103 }