From 00c2e3e4a5279abdd60dd6bba3910575dc5cb9a6 Mon Sep 17 00:00:00 2001 From: jpetso Date: Sun, 18 Jan 2009 23:44:47 +0000 Subject: [PATCH] Make kdialog use Plasma's notification bling (via D-Bus) if possible, keeping KPassivePopup as fallback in case the D-Bus invocation fails. The invocation code is a heavily simplified version of dimsuz's code in NotifyByPopup of KNotify, therefore his copyright instead of mine. Ok to backport to 4.2, or rather not? git-svn-id: svn+ssh://svn.kde.org/home/kde/trunk/KDE/kdebase@913253 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- apps/config-apps.h.cmake | 3 ++ apps/kdialog/CMakeLists.txt | 3 ++ apps/kdialog/kdialog.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/apps/config-apps.h.cmake b/apps/config-apps.h.cmake index 62dbbbf6..dc2e294e 100644 --- a/apps/config-apps.h.cmake +++ b/apps/config-apps.h.cmake @@ -69,6 +69,9 @@ /* Define to 1 if you have the `vsnprintf' function. */ #cmakedefine HAVE_VSNPRINTF 1 +/* Define to 1 if the QtDBus module is present. */ +#cmakedefine QT_QTDBUS_FOUND 1 + /* KDE's binaries directory */ #define KDE_BINDIR "${BIN_INSTALL_DIR}" diff --git a/apps/kdialog/CMakeLists.txt b/apps/kdialog/CMakeLists.txt index cb398a0b..7a90fa08 100644 --- a/apps/kdialog/CMakeLists.txt +++ b/apps/kdialog/CMakeLists.txt @@ -12,6 +12,9 @@ kde4_add_executable(kdialog ${kdialog_SRCS}) target_link_libraries(kdialog ${KDE4_KFILE_LIBS} ${KDE4_KIO_LIBS}) if (Q_WS_X11) target_link_libraries(kdialog ${X11_X11_LIB}) + if (QT_QTDBUS_FOUND) + target_link_libraries(kdialog ${QT_QTDBUS_LIBRARY}) + endif (QT_QTDBUS_FOUND) endif (Q_WS_X11) install(TARGETS kdialog ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/apps/kdialog/kdialog.cpp b/apps/kdialog/kdialog.cpp index 3e564d0c..977aaaa2 100644 --- a/apps/kdialog/kdialog.cpp +++ b/apps/kdialog/kdialog.cpp @@ -2,6 +2,7 @@ // Copyright (C) 1998 Matthias Hoelzer // Copyright (C) 2002 David Faure // Copyright (C) 2005 Brad Hards +// Copyright (C) 2008 by Dmitry Suzdalev // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -43,6 +44,12 @@ #include #endif +#include "../config-apps.h" +#ifdef QT_QTDBUS_FOUND +#include +#include +#endif + #ifdef Q_WS_WIN #include #endif @@ -85,6 +92,63 @@ bool WinIdEmbedder::eventFilter(QObject *o, QEvent *e) return QObject::eventFilter(o, e); } +/** + * Display a passive notification popup using the D-Bus interface, if possible. + * @return true if the notification was successfully sent, false otherwise. + */ +bool sendVisualNotification(QString text, QString title, int timeout) +{ +#ifdef QT_QTDBUS_FOUND + const QString dbusServiceName = "org.kde.VisualNotifications"; + const QString dbusInterfaceName = "org.kde.VisualNotifications"; + const QString dbusPath = "/VisualNotifications"; + + // check if service already exists on plugin instantiation + QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface(); + + if (!interface || !interface->isServiceRegistered(dbusServiceName)) { + //kDebug() << dbusServiceName << "D-Bus service not registered"; + return false; + } + + if (timeout == 0) + timeout = 10 * 1000; + + QDBusMessage m = QDBusMessage::createMethodCall(dbusServiceName, dbusPath, dbusInterfaceName, "Notify"); + QList args; + + args.append("kdialog"); // app_name + args.append(0U); // replaces_id + args.append("kdialogPassivePopup"); // event_id + args.append("dialog-information"); // app_icon + args.append(title); // summary + args.append(text); // body + args.append(QStringList()); // actions - unused for plain passive popups + args.append(QVariantMap()); // hints - unused atm + args.append(timeout); // expire timout + + m.setArguments(args); + + QDBusMessage replyMsg = QDBusConnection::sessionBus().call(m); + if(replyMsg.type() == QDBusMessage::ReplyMessage) { + if (!replyMsg.arguments().isEmpty()) { + return true; + } + // Not displaying any error messages as this is optional for kdialog + // and KPassivePopup is a perfectly valid fallback. + //else { + // kDebug() << "Error: received reply with no arguments."; + //} + } else if (replyMsg.type() == QDBusMessage::ErrorMessage) { + //kDebug() << "Error: failed to send D-Bus message"; + //kDebug() << replyMsg; + } else { + //kDebug() << "Unexpected reply type"; + } +#endif + return false; +} + static void outputStringList(const QStringList &list, bool separateOutput) { if ( separateOutput) { @@ -252,6 +316,12 @@ static int directCommand(KCmdLineArgs *args) duration = 1000 * args->arg(0).toInt(); if (duration == 0) duration = 10000; + + // try to use more stylish notifications + if (sendVisualNotification(args->getOption("passivepopup"), title, duration)) + return 0; + + // ...did not work, use KPassivePopup as fallback KPassivePopup *popup = KPassivePopup::message( KPassivePopup::Boxed, // style title, args->getOption("passivepopup"), -- 2.11.4.GIT