diff --git a/CMakeLists.txt b/CMakeLists.txt index ac9867f..b19a062 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,172 +1,172 @@ cmake_minimum_required(VERSION 3.5) set(KF5_VERSION "5.68.0") # handled by release scripts set(KF5_DEP_VERSION "5.67.0") # handled by release scripts project(KNotifications VERSION ${KF5_VERSION}) # ECM setup include(FeatureSummary) find_package(ECM 5.67.0 NO_MODULE) set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://commits.kde.org/extra-cmake-modules") feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) include(GenerateExportHeader) include(ECMSetupVersion) include(ECMGenerateHeaders) include(ECMQtDeclareLoggingCategory) include(ECMPoQmTools) include(ECMAddQch) include(ECMGenerateExportHeader) set(EXCLUDE_DEPRECATED_BEFORE_AND_AT 0 CACHE STRING "Control the range of deprecated API excluded from the build [default=0].") option(BUILD_QCH "Build API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)" OFF) add_feature_info(QCH ${BUILD_QCH} "API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)") ecm_setup_version(PROJECT VARIABLE_PREFIX KNOTIFICATIONS VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/knotifications_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KF5NotificationsConfigVersion.cmake" SOVERSION 5) # Dependencies set(REQUIRED_QT_VERSION 5.12.0) set(CMAKE_AUTORCC TRUE) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Widgets) if (NOT ANDROID AND NOT WIN32) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED DBus) elseif (ANDROID) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED AndroidExtras) find_package(Gradle REQUIRED) endif() find_package(Qt5 ${REQUIRED_QT_VERSION} QUIET OPTIONAL_COMPONENTS TextToSpeech) set_package_properties(Qt5TextToSpeech PROPERTIES DESCRIPTION "Qt text to speech module" TYPE OPTIONAL PURPOSE "Required to build text to speech notification support") if (Qt5TextToSpeech_FOUND) add_definitions(-DHAVE_SPEECH) endif() include(KDEInstallDirs) include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) include(KDECMakeSettings) if (NOT APPLE AND NOT WIN32) find_package(X11) endif() set(HAVE_X11 ${X11_FOUND}) set(HAVE_XTEST ${X11_XTest_FOUND}) if(X11_FOUND) find_package(Qt5X11Extras ${REQUIRED_QT_VERSION} REQUIRED NO_MODULE) endif() if(APPLE) find_package(Qt5MacExtras ${REQUIRED_QT_VERSION} REQUIRED NO_MODULE) endif() if (WIN32) find_package(LibSnoreToast REQUIRED) set_package_properties(LibSnoreToast PROPERTIES TYPE REQUIRED PURPOSE "for the Windows Toast Notifications" DESCRIPTION "A command line application, capable of creating Windows Toast notifications on Windows (>=)8 " ) find_package(Qt5Network REQUIRED) endif() set(HAVE_KWINDOWSYSTEM FALSE) -if(NOT ANDROID AND NOT WIN32) +if(NOT ANDROID) find_package(KF5WindowSystem ${KF5_DEP_VERSION} REQUIRED) set(HAVE_KWINDOWSYSTEM TRUE) endif() find_package(KF5Config ${KF5_DEP_VERSION} REQUIRED) find_package(KF5CoreAddons ${KF5_DEP_VERSION} REQUIRED) if (NOT WIN32 AND NOT ANDROID) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED DBus) find_package(Canberra) set_package_properties(Canberra PROPERTIES PURPOSE "Needed to build audio notification support" TYPE OPTIONAL) if (Canberra_FOUND) add_definitions(-DHAVE_CANBERRA) else() # This is REQUIRED since you cannot tell CMake "either one of those two optional ones are required" find_package(Phonon4Qt5 4.6.60 NO_MODULE REQUIRED) set_package_properties(Phonon4Qt5 PROPERTIES DESCRIPTION "Qt-based audio library" PURPOSE "Needed to build audio notification support when Canberra isn't available") add_definitions(-DHAVE_PHONON4QT5) endif() endif() set(HAVE_DBUS FALSE) if (TARGET Qt5::DBus) find_package(dbusmenu-qt5 CONFIG) set_package_properties(dbusmenu-qt5 PROPERTIES DESCRIPTION "DBusMenuQt" URL "https://launchpad.net/libdbusmenu-qt" TYPE OPTIONAL PURPOSE "Support for notification area menus via the DBusMenu protocol") set(HAVE_DBUS TRUE) endif() remove_definitions(-DQT_NO_CAST_FROM_BYTEARRAY) #add_definitions(-DQT_NO_FOREACH) if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/po") ecm_install_po_files_as_qm(po) endif() if (NOT APPLE) # QtMac::setBadgeLabelText is deprecated add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x050d00) endif() add_definitions(-DKF_DISABLE_DEPRECATED_BEFORE_AND_AT=0x054300) add_subdirectory(src) if (BUILD_TESTING) add_subdirectory(tests) add_subdirectory(autotests) endif() # create a Config.cmake and a ConfigVersion.cmake file and install them set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KF5Notifications") if (BUILD_QCH) ecm_install_qch_export( TARGETS KF5Notifications_QCH FILE KF5NotificationsQchTargets.cmake DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) set(PACKAGE_INCLUDE_QCHTARGETS "include(\"\${CMAKE_CURRENT_LIST_DIR}/KF5NotificationsQchTargets.cmake\")") endif() include(CMakePackageConfigHelpers) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KF5NotificationsConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KF5NotificationsConfig.cmake" PATH_VARS KDE_INSTALL_DBUSINTERFACEDIR INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KF5NotificationsConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KF5NotificationsConfigVersion.cmake" DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) install(EXPORT KF5NotificationsTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE KF5NotificationsTargets.cmake NAMESPACE KF5:: ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/knotifications_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} COMPONENT Devel ) feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d4cf666..c69bb8b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,242 +1,246 @@ if (Phonon4Qt5_FOUND) include_directories(${PHONON_INCLUDE_DIR}) endif() if (CANBERRA_FOUND) include_directories(${CANBERRA_INCLUDE_DIRS}) endif() ecm_create_qm_loader(knotifications_QM_LOADER knotifications5_qt) set(knotifications_SRCS knotification.cpp knotificationmanager.cpp kpassivepopup.cpp knotifyconfig.cpp knotificationplugin.cpp notifybyexecute.cpp notifybylogfile.cpp notifybytaskbar.cpp ${knotifications_QM_LOADER} ) +if (NOT ANDROID) + list(APPEND knotifications_SRCS + kstatusnotifieritem.cpp) +endif() + if (TARGET Qt5::DBus) list(APPEND knotifications_SRCS - kstatusnotifieritem.cpp kstatusnotifieritemdbus_p.cpp knotificationrestrictions.cpp imageconverter.cpp #needed to marshal images for sending over dbus by NotifyByPopup notifybypopup.cpp notifybyportal.cpp ) endif() if (ANDROID) add_subdirectory(android) list(APPEND knotifications_SRCS notifybyandroid.cpp knotifications.qrc) endif() if (WIN32) list(APPEND knotifications_SRCS notifybysnore.cpp) endif () if (APPLE) list(APPEND knotifications_SRCS notifybymacosnotificationcenter.mm) endif() ecm_qt_declare_logging_category(knotifications_SRCS HEADER debug_p.h IDENTIFIER LOG_KNOTIFICATIONS CATEGORY_NAME org.kde.knotifications DESCRIPTION "KNotifications" EXPORT KNOTIFICATIONS ) if (Canberra_FOUND) set(knotifications_SRCS ${knotifications_SRCS} notifybyaudio_canberra.cpp) elseif (Phonon4Qt5_FOUND) set(knotifications_SRCS ${knotifications_SRCS} notifybyaudio_phonon.cpp) endif() if (Qt5TextToSpeech_FOUND) set(knotifications_SRCS ${knotifications_SRCS} notifybytts.cpp) endif() if (TARGET Qt5::DBus) if (dbusmenu-qt5_FOUND) message("dbusmenu-qt5_FOUND") set(HAVE_DBUSMENUQT 1) include_directories(${dbusmenu-qt5_INCLUDE_DIRS}) else() set(HAVE_DBUSMENUQT 0) endif() qt5_add_dbus_adaptor(knotifications_SRCS org.kde.StatusNotifierItem.xml kstatusnotifieritemdbus_p.h KStatusNotifierItemDBus) set(statusnotifierwatcher_xml org.kde.StatusNotifierWatcher.xml) qt5_add_dbus_interface(knotifications_SRCS ${statusnotifierwatcher_xml} statusnotifierwatcher_interface) set(notifications_xml org.freedesktop.Notifications.xml) qt5_add_dbus_interface(knotifications_SRCS ${notifications_xml} notifications_interface) endif() configure_file(config-knotifications.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-knotifications.h ) add_library(KF5Notifications ${knotifications_SRCS}) add_library(KF5::Notifications ALIAS KF5Notifications) ecm_generate_export_header(KF5Notifications EXPORT_FILE_NAME knotifications_export.h BASE_NAME KNotifications GROUP_BASE_NAME KF VERSION ${KF5_VERSION} DEPRECATED_BASE_VERSION 0 DEPRECATION_VERSIONS 5.67 EXCLUDE_DEPRECATED_BEFORE_AND_AT ${EXCLUDE_DEPRECATED_BEFORE_AND_AT} ) target_include_directories(KF5Notifications INTERFACE "$") target_link_libraries(KF5Notifications PUBLIC Qt5::Widgets ) if (TARGET Qt5::DBus) target_link_libraries(KF5Notifications PUBLIC Qt5::DBus) endif() target_link_libraries(KF5Notifications PRIVATE KF5::CoreAddons KF5::ConfigCore ) if (TARGET KF5::WindowSystem) target_link_libraries(KF5Notifications PRIVATE KF5::WindowSystem) endif() if (TARGET SnoreToast::SnoreToastActions) target_link_libraries(KF5Notifications PRIVATE Qt5::Network SnoreToast::SnoreToastActions) endif () if (Phonon4Qt5_FOUND) target_link_libraries(KF5Notifications PRIVATE ${PHONON_LIBRARIES}) endif() if (Canberra_FOUND) target_link_libraries(KF5Notifications PRIVATE Canberra::Canberra) endif() if (Qt5TextToSpeech_FOUND) target_link_libraries(KF5Notifications PRIVATE Qt5::TextToSpeech) endif() if(X11_FOUND) target_link_libraries(KF5Notifications PRIVATE ${X11_X11_LIB} Qt5::X11Extras) endif() if(APPLE) target_link_libraries(KF5Notifications PRIVATE Qt5::MacExtras "-framework Foundation" "-framework AppKit") endif() if(X11_XTest_FOUND) target_link_libraries(KF5Notifications PRIVATE ${X11_XTest_LIB}) endif() if(HAVE_DBUSMENUQT) target_link_libraries(KF5Notifications PRIVATE dbusmenu-qt5) endif() if (ANDROID) target_link_libraries(KF5Notifications PRIVATE Qt5::AndroidExtras) endif() set_target_properties(KF5Notifications PROPERTIES VERSION ${KNOTIFICATIONS_VERSION_STRING} SOVERSION ${KNOTIFICATIONS_SOVERSION} EXPORT_NAME Notifications ) ecm_generate_headers(KNotifications_HEADERS HEADER_NAMES KNotification KPassivePopup KStatusNotifierItem KNotificationRestrictions KNotificationPlugin KNotifyConfig REQUIRED_HEADERS KNotifications_HEADERS ) install(TARGETS KF5Notifications EXPORT KF5NotificationsTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/knotifications_export.h ${KNotifications_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/KNotifications COMPONENT Devel ) ecm_qt_install_logging_categories( EXPORT KNOTIFICATIONS FILE knotifications.categories DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR} ) if(BUILD_QCH) ecm_add_qch( KF5Notifications_QCH NAME KNotifications BASE_NAME KF5Notifications VERSION ${KF5_VERSION} ORG_DOMAIN org.kde SOURCES # using only public headers, to cover only public API ${KNotifications_HEADERS} MD_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md" IMAGE_DIRS "${CMAKE_SOURCE_DIR}/docs/pics" LINK_QCHS Qt5Widgets_QCH INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR} BLANK_MACROS KNOTIFICATIONS_EXPORT KNOTIFICATIONS_DEPRECATED KNOTIFICATIONS_DEPRECATED_EXPORT "KNOTIFICATIONS_DEPRECATED_VERSION(x, y, t)" TAGFILE_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} QCH_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} COMPONENT Devel ) endif() if (TARGET Qt5::DBus) install(FILES org.kde.StatusNotifierItem.xml DESTINATION ${KDE_INSTALL_DBUSINTERFACEDIR} RENAME kf5_org.kde.StatusNotifierItem.xml) install(FILES org.kde.StatusNotifierWatcher.xml DESTINATION ${KDE_INSTALL_DBUSINTERFACEDIR} RENAME kf5_org.kde.StatusNotifierWatcher.xml) endif() install(FILES knotificationplugin.desktop DESTINATION ${KDE_INSTALL_KSERVICETYPES5DIR}) include(ECMGeneratePriFile) ecm_generate_pri_file(BASE_NAME KNotifications LIB_NAME KF5Notifications DEPS "widgets" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/KNotifications) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) if (ANDROID) if (Qt5Core_VERSION VERSION_LESS 5.14.0) install(FILES KF5Notifications-android-dependencies.xml DESTINATION ${KDE_INSTALL_LIBDIR}) else() install(FILES KF5Notifications-android-dependencies.xml DESTINATION ${KDE_INSTALL_LIBDIR} RENAME KF5Notifications_${CMAKE_ANDROID_ARCH_ABI}-android-dependencies.xml) endif() endif() diff --git a/src/kstatusnotifieritem.cpp b/src/kstatusnotifieritem.cpp index 5aa90d5..dd48fa0 100644 --- a/src/kstatusnotifieritem.cpp +++ b/src/kstatusnotifieritem.cpp @@ -1,1158 +1,1219 @@ /* This file is part of the KDE libraries Copyright 2009 by Marco Martin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kstatusnotifieritem.h" #include "kstatusnotifieritemprivate_p.h" -#include "kstatusnotifieritemdbus_p.h" #include "debug_p.h" -#include #include #include #include #include #include #include #include #include #ifdef Q_OS_MACOS #include #include #endif +#ifdef QT_DBUS_LIB +#include "kstatusnotifieritemdbus_p.h" + +#include + +#if HAVE_DBUSMENUQT +#include +#endif //HAVE_DBUSMENUQT +#endif + +#include #include #include #include #include static const char s_statusNotifierWatcherServiceName[] = "org.kde.StatusNotifierWatcher"; static const int s_legacyTrayIconSize = 24; -#if HAVE_DBUSMENUQT -#include -#endif //HAVE_DBUSMENUQT - KStatusNotifierItem::KStatusNotifierItem(QObject *parent) : QObject(parent), d(new KStatusNotifierItemPrivate(this)) { d->init(QString()); } KStatusNotifierItem::KStatusNotifierItem(const QString &id, QObject *parent) : QObject(parent), d(new KStatusNotifierItemPrivate(this)) { d->init(id); } KStatusNotifierItem::~KStatusNotifierItem() { +#ifdef QT_DBUS_LIB delete d->statusNotifierWatcher; delete d->notificationsClient; +#endif delete d->systemTrayIcon; if (!qApp->closingDown()) { delete d->menu; } if (d->associatedWidget) { KWindowSystem::self()->disconnect(d->associatedWidget); } delete d; } QString KStatusNotifierItem::id() const { //qCDebug(LOG_KNOTIFICATIONS) << "id requested" << d->id; return d->id; } void KStatusNotifierItem::setCategory(const ItemCategory category) { d->category = category; } KStatusNotifierItem::ItemStatus KStatusNotifierItem::status() const { return d->status; } KStatusNotifierItem::ItemCategory KStatusNotifierItem::category() const { return d->category; } void KStatusNotifierItem::setTitle(const QString &title) { d->title = title; } void KStatusNotifierItem::setStatus(const ItemStatus status) { if (d->status == status) { return; } d->status = status; - emit d->statusNotifierItemDBus->NewStatus(QString::fromLatin1(metaObject()->enumerator(metaObject()->indexOfEnumerator("ItemStatus")).valueToKey(d->status))); +#ifdef QT_DBUS_LIB + emit d->statusNotifierItemDBus->NewStatus(QString::fromLatin1(metaObject()->enumerator(metaObject()->indexOfEnumerator("ItemStatus")).valueToKey(d->status))); +#endif if (d->systemTrayIcon) { d->syncLegacySystemTrayIcon(); } } //normal icon void KStatusNotifierItem::setIconByName(const QString &name) { if (d->iconName == name) { return; } - d->serializedIcon = KDbusImageVector(); + d->iconName = name; + +#ifdef QT_DBUS_LIB + d->serializedIcon = KDbusImageVector(); emit d->statusNotifierItemDBus->NewIcon(); +#endif + if (d->systemTrayIcon) { d->systemTrayIcon->setIcon(QIcon::fromTheme(name)); } } QString KStatusNotifierItem::iconName() const { return d->iconName; } void KStatusNotifierItem::setIconByPixmap(const QIcon &icon) { if (d->iconName.isEmpty() && d->icon.cacheKey() == icon.cacheKey()) { return; } d->iconName.clear(); + +#ifdef QT_DBUS_LIB d->serializedIcon = d->iconToVector(icon); emit d->statusNotifierItemDBus->NewIcon(); +#endif d->icon = icon; if (d->systemTrayIcon) { d->systemTrayIcon->setIcon(icon); } } QIcon KStatusNotifierItem::iconPixmap() const { return d->icon; } void KStatusNotifierItem::setOverlayIconByName(const QString &name) { if (d->overlayIconName == name) { return; } d->overlayIconName = name; +#ifdef QT_DBUS_LIB emit d->statusNotifierItemDBus->NewOverlayIcon(); +#endif if (d->systemTrayIcon) { QPixmap iconPixmap = QIcon::fromTheme(d->iconName).pixmap(s_legacyTrayIconSize, s_legacyTrayIconSize); if (!name.isEmpty()) { QPixmap overlayPixmap = QIcon::fromTheme(d->overlayIconName).pixmap(s_legacyTrayIconSize / 2, s_legacyTrayIconSize / 2); QPainter p(&iconPixmap); p.drawPixmap(iconPixmap.width() - overlayPixmap.width(), iconPixmap.height() - overlayPixmap.height(), overlayPixmap); p.end(); } d->systemTrayIcon->setIcon(iconPixmap); } } QString KStatusNotifierItem::overlayIconName() const { return d->overlayIconName; } void KStatusNotifierItem::setOverlayIconByPixmap(const QIcon &icon) { if (d->overlayIconName.isEmpty() && d->overlayIcon.cacheKey() == icon.cacheKey()) { return; } d->overlayIconName.clear(); + +#ifdef QT_DBUS_LIB d->serializedOverlayIcon = d->iconToVector(icon); emit d->statusNotifierItemDBus->NewOverlayIcon(); +#endif d->overlayIcon = icon; if (d->systemTrayIcon) { QPixmap iconPixmap = d->icon.pixmap(s_legacyTrayIconSize, s_legacyTrayIconSize); QPixmap overlayPixmap = d->overlayIcon.pixmap(s_legacyTrayIconSize / 2, s_legacyTrayIconSize / 2); QPainter p(&iconPixmap); p.drawPixmap(iconPixmap.width() - overlayPixmap.width(), iconPixmap.height() - overlayPixmap.height(), overlayPixmap); p.end(); d->systemTrayIcon->setIcon(iconPixmap); } } QIcon KStatusNotifierItem::overlayIconPixmap() const { return d->overlayIcon; } //Icons and movie for requesting attention state void KStatusNotifierItem::setAttentionIconByName(const QString &name) { if (d->attentionIconName == name) { return; } - d->serializedAttentionIcon = KDbusImageVector(); d->attentionIconName = name; + +#ifdef QT_DBUS_LIB + d->serializedAttentionIcon = KDbusImageVector(); emit d->statusNotifierItemDBus->NewAttentionIcon(); +#endif } QString KStatusNotifierItem::attentionIconName() const { return d->attentionIconName; } void KStatusNotifierItem::setAttentionIconByPixmap(const QIcon &icon) { if (d->attentionIconName.isEmpty() && d->attentionIcon.cacheKey() == icon.cacheKey()) { return; } d->attentionIconName.clear(); - d->serializedAttentionIcon = d->iconToVector(icon); d->attentionIcon = icon; + +#ifdef QT_DBUS_LIB + d->serializedAttentionIcon = d->iconToVector(icon); emit d->statusNotifierItemDBus->NewAttentionIcon(); +#endif } QIcon KStatusNotifierItem::attentionIconPixmap() const { return d->attentionIcon; } void KStatusNotifierItem::setAttentionMovieByName(const QString &name) { if (d->movieName == name) { return; } d->movieName = name; delete d->movie; d->movie = nullptr; +#ifdef QT_DBUS_LIB emit d->statusNotifierItemDBus->NewAttentionIcon(); +#endif if (d->systemTrayIcon) { d->movie = new QMovie(d->movieName); d->systemTrayIcon->setMovie(d->movie); } } QString KStatusNotifierItem::attentionMovieName() const { return d->movieName; } //ToolTip #ifdef Q_OS_MACOS static void setTrayToolTip(KStatusNotifierLegacyIcon *systemTrayIcon, const QString &title, const QString &subTitle) { if (systemTrayIcon) { bool tEmpty = title.isEmpty(), stEmpty = subTitle.isEmpty(); if (tEmpty) { if (!stEmpty) { systemTrayIcon->setToolTip(subTitle); } else { systemTrayIcon->setToolTip(title); } } else { if (stEmpty) { systemTrayIcon->setToolTip(title); } else { systemTrayIcon->setToolTip(title + QStringLiteral("\n") + subTitle); } } } } #else static void setTrayToolTip(KStatusNotifierLegacyIcon *systemTrayIcon, const QString &title, const QString &) { if (systemTrayIcon) { systemTrayIcon->setToolTip(title); } } #endif void KStatusNotifierItem::setToolTip(const QString &iconName, const QString &title, const QString &subTitle) { if (d->toolTipIconName == iconName && d->toolTipTitle == title && d->toolTipSubTitle == subTitle) { return; } - d->serializedToolTipIcon = KDbusImageVector(); d->toolTipIconName = iconName; d->toolTipTitle = title; // if (d->systemTrayIcon) { // d->systemTrayIcon->setToolTip(title); // } setTrayToolTip(d->systemTrayIcon, title, subTitle); d->toolTipSubTitle = subTitle; + +#ifdef QT_DBUS_LIB + d->serializedToolTipIcon = KDbusImageVector(); emit d->statusNotifierItemDBus->NewToolTip(); +#endif } void KStatusNotifierItem::setToolTip(const QIcon &icon, const QString &title, const QString &subTitle) { if (d->toolTipIconName.isEmpty() && d->toolTipIcon.cacheKey() == icon.cacheKey() && d->toolTipTitle == title && d->toolTipSubTitle == subTitle) { return; } d->toolTipIconName.clear(); - d->serializedToolTipIcon = d->iconToVector(icon); d->toolTipIcon = icon; d->toolTipTitle = title; // if (d->systemTrayIcon) { // d->systemTrayIcon->setToolTip(title); // } setTrayToolTip(d->systemTrayIcon, title, subTitle); d->toolTipSubTitle = subTitle; +#ifdef QT_DBUS_LIB + d->serializedToolTipIcon = d->iconToVector(icon); emit d->statusNotifierItemDBus->NewToolTip(); +#endif } void KStatusNotifierItem::setToolTipIconByName(const QString &name) { if (d->toolTipIconName == name) { return; } - d->serializedToolTipIcon = KDbusImageVector(); d->toolTipIconName = name; +#ifdef QT_DBUS_LIB + d->serializedToolTipIcon = KDbusImageVector(); emit d->statusNotifierItemDBus->NewToolTip(); +#endif } QString KStatusNotifierItem::toolTipIconName() const { return d->toolTipIconName; } void KStatusNotifierItem::setToolTipIconByPixmap(const QIcon &icon) { if (d->toolTipIconName.isEmpty() && d->toolTipIcon.cacheKey() == icon.cacheKey()) { return; } d->toolTipIconName.clear(); - d->serializedToolTipIcon = d->iconToVector(icon); d->toolTipIcon = icon; + +#ifdef QT_DBUS_LIB + d->serializedToolTipIcon = d->iconToVector(icon); emit d->statusNotifierItemDBus->NewToolTip(); +#endif } QIcon KStatusNotifierItem::toolTipIconPixmap() const { return d->toolTipIcon; } void KStatusNotifierItem::setToolTipTitle(const QString &title) { if (d->toolTipTitle == title) { return; } d->toolTipTitle = title; + +#ifdef QT_DBUS_LIB emit d->statusNotifierItemDBus->NewToolTip(); +#endif // if (d->systemTrayIcon) { // d->systemTrayIcon->setToolTip(title); // } setTrayToolTip(d->systemTrayIcon, title, d->toolTipSubTitle); } QString KStatusNotifierItem::toolTipTitle() const { return d->toolTipTitle; } void KStatusNotifierItem::setToolTipSubTitle(const QString &subTitle) { if (d->toolTipSubTitle == subTitle) { return; } d->toolTipSubTitle = subTitle; #ifdef Q_OS_MACOS setTrayToolTip(d->systemTrayIcon, d->toolTipTitle, subTitle); #endif +#ifdef QT_DBUS_LIB emit d->statusNotifierItemDBus->NewToolTip(); +#endif } QString KStatusNotifierItem::toolTipSubTitle() const { return d->toolTipSubTitle; } void KStatusNotifierItem::setContextMenu(QMenu *menu) { if (d->menu && d->menu != menu) { d->menu->removeEventFilter(this); delete d->menu; } if (!menu) { d->menu = nullptr; return; } if (d->systemTrayIcon) { d->systemTrayIcon->setContextMenu(menu); } else if (d->menu != menu) { if (getenv("KSNI_NO_DBUSMENU")) { // This is a hack to make it possible to disable DBusMenu in an // application. The string "/NO_DBUSMENU" must be the same as in // DBusSystemTrayWidget::findDBusMenuInterface() in the Plasma // systemtray applet. d->menuObjectPath = QStringLiteral("/NO_DBUSMENU"); menu->installEventFilter(this); } else { d->menuObjectPath = QStringLiteral("/MenuBar"); #if HAVE_DBUSMENUQT new DBusMenuExporter(d->menuObjectPath, menu, d->statusNotifierItemDBus->dbusConnection()); #endif } connect(menu, SIGNAL(aboutToShow()), this, SLOT(contextMenuAboutToShow())); } d->menu = menu; Qt::WindowFlags oldFlags = d->menu->windowFlags(); d->menu->setParent(nullptr); d->menu->setWindowFlags(oldFlags); } QMenu *KStatusNotifierItem::contextMenu() const { return d->menu; } void KStatusNotifierItem::setAssociatedWidget(QWidget *associatedWidget) { if (associatedWidget) { d->associatedWidget = associatedWidget->window(); d->associatedWidgetPos = QPoint(-1, -1); QObject::connect(KWindowSystem::self(), &KWindowSystem::windowAdded, d->associatedWidget, [this](WId id) { if(d->associatedWidget->winId() == id && d->associatedWidgetPos != QPoint(-1, -1)) { d->associatedWidget->move(d->associatedWidgetPos); } } ); QObject::connect(KWindowSystem::self(), &KWindowSystem::windowRemoved, d->associatedWidget, [this](WId id) { if(d->associatedWidget->winId() == id) { d->associatedWidgetPos = d->associatedWidget->pos(); } } ); } else if (d->associatedWidget) { KWindowSystem::self()->disconnect(d->associatedWidget); d->associatedWidget = nullptr; } if (d->systemTrayIcon) { delete d->systemTrayIcon; d->systemTrayIcon = nullptr; d->setLegacySystemTrayEnabled(true); } if (d->associatedWidget && d->associatedWidget != d->menu) { QAction *action = d->actionCollection.value(QStringLiteral("minimizeRestore")); if (!action) { action = new QAction(this); d->actionCollection.insert(QStringLiteral("minimizeRestore"), action); action->setText(tr("&Minimize")); connect(action, SIGNAL(triggered(bool)), this, SLOT(minimizeRestore())); } KWindowInfo info(d->associatedWidget->winId(), NET::WMDesktop); d->onAllDesktops = info.onAllDesktops(); } else { if (d->menu && d->hasQuit) { QAction *action = d->actionCollection.value(QStringLiteral("minimizeRestore")); if (action) { d->menu->removeAction(action); } } d->onAllDesktops = false; } } QWidget *KStatusNotifierItem::associatedWidget() const { return d->associatedWidget; } QList KStatusNotifierItem::actionCollection() const { return d->actionCollection.values(); } void KStatusNotifierItem::addAction(const QString &name, QAction *action) { d->actionCollection.insert(name, action); } void KStatusNotifierItem::removeAction(const QString &name) { d->actionCollection.remove(name); } QAction* KStatusNotifierItem::action(const QString &name) const { return d->actionCollection.value(name); } void KStatusNotifierItem::setStandardActionsEnabled(bool enabled) { if (d->standardActionsEnabled == enabled) { return; } d->standardActionsEnabled = enabled; if (d->menu && !enabled && d->hasQuit) { QAction *action = d->actionCollection.value(QStringLiteral("minimizeRestore")); if (action) { d->menu->removeAction(action); } action = d->actionCollection.value(QStringLiteral("quit")); if (action) { d->menu->removeAction(action); } d->hasQuit = false; } } bool KStatusNotifierItem::standardActionsEnabled() const { return d->standardActionsEnabled; } void KStatusNotifierItem::showMessage(const QString &title, const QString &message, const QString &icon, int timeout) { - if (!d->notificationsClient) { - d->notificationsClient = new org::freedesktop::Notifications(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), - QDBusConnection::sessionBus()); - } - - uint id = 0; #ifdef Q_OS_MACOS if (d->systemTrayIcon) { // Growl is not needed anymore for QSystemTrayIcon::showMessage() since OS X 10.8 d->systemTrayIcon->showMessage(title, message, QSystemTrayIcon::Information, timeout); } else #endif { +#ifdef QT_DBUS_LIB + if (!d->notificationsClient) { + d->notificationsClient = new org::freedesktop::Notifications(QStringLiteral("org.freedesktop.Notifications"), QStringLiteral("/org/freedesktop/Notifications"), + QDBusConnection::sessionBus()); + } + + uint id = 0; QVariantMap hints; QString desktopFileName = QGuiApplication::desktopFileName(); if (!desktopFileName.isEmpty()) { // handle apps which set the desktopFileName property with filename suffix, // due to unclear API dox (https://bugreports.qt.io/browse/QTBUG-75521) if (desktopFileName.endsWith(QLatin1String(".desktop"))) { desktopFileName.chop(8); } hints.insert(QStringLiteral("desktop-entry"), desktopFileName); } d->notificationsClient->Notify(d->title, id, icon, title, message, QStringList(), hints, timeout); +#endif } } QString KStatusNotifierItem::title() const { return d->title; } void KStatusNotifierItem::activate(const QPoint &pos) { //if the user activated the icon the NeedsAttention state is no longer necessary //FIXME: always true? if (d->status == NeedsAttention) { d->status = Active; #ifdef Q_OS_MACOS QtMac::setBadgeLabelText(QString()); #endif +#ifdef QT_DBUS_LIB emit d->statusNotifierItemDBus->NewStatus(QString::fromLatin1(metaObject()->enumerator(metaObject()->indexOfEnumerator("ItemStatus")).valueToKey(d->status))); +#endif } +#ifdef QT_DBUS_LIB if (d->associatedWidget && d->associatedWidget == d->menu) { d->statusNotifierItemDBus->ContextMenu(pos.x(), pos.y()); return; } +#endif if (d->menu && d->menu->isVisible()) { d->menu->hide(); } if (!d->associatedWidget) { emit activateRequested(true, pos); return; } d->checkVisibility(pos); } bool KStatusNotifierItemPrivate::checkVisibility(QPoint pos, bool perform) { #ifdef Q_OS_WIN #if 0 // the problem is that we lose focus when the systray icon is activated // and we don't know the former active window // therefore we watch for activation event and use our stopwatch :) if (GetTickCount() - dwTickCount < 300) { // we were active in the last 300ms -> hide it minimizeRestore(false); emit activateRequested(false, pos); } else { minimizeRestore(true); emit activateRequested(true, pos); } #endif #else // mapped = visible (but possibly obscured) const bool mapped = associatedWidget->isVisible() && !associatedWidget->isMinimized(); // - not mapped -> show, raise, focus // - mapped // - obscured -> raise, focus // - not obscured -> hide //info1.mappingState() != NET::Visible -> window on another desktop? if (!mapped) { if (perform) { minimizeRestore(true); emit q->activateRequested(true, pos); } return true; } else if (QGuiApplication::platformName() == QLatin1String("xcb")) { const KWindowInfo info1(associatedWidget->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop); QListIterator< WId > it(KWindowSystem::stackingOrder()); it.toBack(); while (it.hasPrevious()) { WId id = it.previous(); if (id == associatedWidget->winId()) { break; } KWindowInfo info2(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType); if (info2.mappingState() != NET::Visible) { continue; // not visible on current desktop -> ignore } if (!info2.geometry().intersects(associatedWidget->geometry())) { continue; // not obscuring the window -> ignore } if (!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove)) { continue; // obscured by window kept above -> ignore } NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask); if (type == NET::Dock || type == NET::TopMenu) { continue; // obscured by dock or topmenu -> ignore } if (perform) { KWindowSystem::raiseWindow(associatedWidget->winId()); KWindowSystem::forceActiveWindow(associatedWidget->winId()); emit q->activateRequested(true, pos); } return true; } //not on current desktop? if (!info1.isOnCurrentDesktop()) { if (perform) { KWindowSystem::activateWindow(associatedWidget->winId()); emit q->activateRequested(true, pos); } return true; } if (perform) { minimizeRestore(false); // hide emit q->activateRequested(false, pos); } return false; } else { if (perform) { minimizeRestore(false); // hide emit q->activateRequested(false, pos); } return false; } #endif return true; } bool KStatusNotifierItem::eventFilter(QObject *watched, QEvent *event) { if (d->systemTrayIcon == nullptr) { //FIXME: ugly ugly workaround to weird QMenu's focus problems if (watched == d->menu && (event->type() == QEvent::WindowDeactivate || (event->type() == QEvent::MouseButtonRelease && static_cast(event)->button() == Qt::LeftButton))) { //put at the back of even queue to let the action activate anyways QTimer::singleShot(0, this, [this]() { d->hideMenu(); }); } } return false; } //KStatusNotifierItemPrivate const int KStatusNotifierItemPrivate::s_protocolVersion = 0; KStatusNotifierItemPrivate::KStatusNotifierItemPrivate(KStatusNotifierItem *item) : q(item), category(KStatusNotifierItem::ApplicationStatus), status(KStatusNotifierItem::Passive), movie(nullptr), menu(nullptr), associatedWidget(nullptr), titleAction(nullptr), - statusNotifierWatcher(nullptr), - notificationsClient(nullptr), systemTrayIcon(nullptr), hasQuit(false), onAllDesktops(false), standardActionsEnabled(true) { } void KStatusNotifierItemPrivate::init(const QString &extraId) { + q->setAssociatedWidget(qobject_cast(q->parent())); +#ifdef QT_DBUS_LIB qDBusRegisterMetaType(); qDBusRegisterMetaType(); qDBusRegisterMetaType(); statusNotifierItemDBus = new KStatusNotifierItemDBus(q); - q->setAssociatedWidget(qobject_cast(q->parent())); QDBusServiceWatcher *watcher = new QDBusServiceWatcher(QString::fromLatin1(s_statusNotifierWatcherServiceName), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, q); QObject::connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), q, SLOT(serviceChange(QString,QString,QString))); +#endif //create a default menu, just like in KSystemtrayIcon QMenu *m = new QMenu(associatedWidget); title = QGuiApplication::applicationDisplayName(); if (title.isEmpty()) { title = QCoreApplication::applicationName(); } #ifdef Q_OS_MACOS // OS X doesn't have texted separators so we emulate QAction::addSection(): // we first add an action with the desired text (title) and icon titleAction = m->addAction(qApp->windowIcon(), title); // this action should be disabled titleAction->setEnabled(false); // Give the titleAction a visible menu icon: // Systray icon and menu ("menu extra") are often used by applications that provide no other interface. // It is thus reasonable to show the application icon in the menu; Finder, Dock and App Switcher // all show it in addition to the application name (and Apple's input "menu extra" also shows icons). titleAction->setIconVisibleInMenu(true); m->addAction(titleAction); // now add a regular separator m->addSeparator(); #else titleAction = m->addSection(qApp->windowIcon(), title); m->setTitle(title); #endif q->setContextMenu(m); QAction *action = new QAction(q); action->setText(KStatusNotifierItem::tr("Quit")); action->setIcon(QIcon::fromTheme(QStringLiteral("application-exit"))); // cannot yet convert to function-pointer-based connect: // some apps like kalarm or korgac have a hack to rewire the connection // of the "quit" action to a own slot, and rely on the name-based slot to disconnect // TODO: extend KStatusNotifierItem API to support such needs QObject::connect(action, SIGNAL(triggered()), q, SLOT(maybeQuit())); actionCollection.insert(QStringLiteral("quit"), action); id = title; if (!extraId.isEmpty()) { id.append(QLatin1Char('_')).append(extraId); } // Init iconThemePath to the app folder for now iconThemePath = QStandardPaths::locate(QStandardPaths::DataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory); registerToDaemon(); } void KStatusNotifierItemPrivate::registerToDaemon() { + bool useLegacy = false; +#ifdef QT_DBUS_LIB qCDebug(LOG_KNOTIFICATIONS) << "Registering a client interface to the KStatusNotifierWatcher"; if (!statusNotifierWatcher) { statusNotifierWatcher = new org::kde::StatusNotifierWatcher(QString::fromLatin1(s_statusNotifierWatcherServiceName), QStringLiteral("/StatusNotifierWatcher"), QDBusConnection::sessionBus()); } if (statusNotifierWatcher->isValid()) { // get protocol version in async way QDBusMessage msg = QDBusMessage::createMethodCall(QString::fromLatin1(s_statusNotifierWatcherServiceName), QStringLiteral("/StatusNotifierWatcher"), QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("Get")); msg.setArguments(QVariantList{QStringLiteral("org.kde.StatusNotifierWatcher"), QStringLiteral("ProtocolVersion")}); QDBusPendingCall async = QDBusConnection::sessionBus().asyncCall(msg); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, q); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, q, [this, watcher] { watcher->deleteLater(); QDBusPendingReply reply = *watcher; if (reply.isError()) { qCDebug(LOG_KNOTIFICATIONS) << "Failed to read protocol version of KStatusNotifierWatcher"; setLegacySystemTrayEnabled(true); } else { bool ok = false; const int protocolVersion = reply.value().toInt(&ok); if (ok && protocolVersion == s_protocolVersion) { statusNotifierWatcher->RegisterStatusNotifierItem(statusNotifierItemDBus->service()); setLegacySystemTrayEnabled(false); } else { qCDebug(LOG_KNOTIFICATIONS) << "KStatusNotifierWatcher has incorrect protocol version"; setLegacySystemTrayEnabled(true); } } } ); } else { qCDebug(LOG_KNOTIFICATIONS) << "KStatusNotifierWatcher not reachable"; - setLegacySystemTrayEnabled(true); + useLegacy = true; } +#endif + setLegacySystemTrayEnabled(useLegacy); } void KStatusNotifierItemPrivate::serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner) { Q_UNUSED(name) if (newOwner.isEmpty()) { //unregistered qCDebug(LOG_KNOTIFICATIONS) << "Connection to the KStatusNotifierWatcher lost"; setLegacyMode(true); +#ifdef QT_DBUS_LIB delete statusNotifierWatcher; statusNotifierWatcher = nullptr; +#endif } else if (oldOwner.isEmpty()) { //registered setLegacyMode(false); } } void KStatusNotifierItemPrivate::setLegacyMode(bool legacy) { if (legacy) { //unregistered setLegacySystemTrayEnabled(true); } else { //registered registerToDaemon(); } } void KStatusNotifierItemPrivate::legacyWheelEvent(int delta) { +#ifdef QT_DBUS_LIB statusNotifierItemDBus->Scroll(delta, QStringLiteral("vertical")); +#endif } void KStatusNotifierItemPrivate::legacyActivated(QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::MiddleClick) { emit q->secondaryActivateRequested(systemTrayIcon->geometry().topLeft()); } else if (reason == QSystemTrayIcon::Trigger) { q->activate(systemTrayIcon->geometry().topLeft()); } } void KStatusNotifierItemPrivate::setLegacySystemTrayEnabled(bool enabled) { if (enabled == (systemTrayIcon != nullptr)) { // already in the correct state return; } if (enabled) { bool isKde = !qEnvironmentVariableIsEmpty("KDE_FULL_SESSION") || qgetenv("XDG_CURRENT_DESKTOP") == "KDE"; if (!systemTrayIcon && !isKde) { if (!QSystemTrayIcon::isSystemTrayAvailable()) { return; } systemTrayIcon = new KStatusNotifierLegacyIcon(associatedWidget); syncLegacySystemTrayIcon(); systemTrayIcon->setToolTip(toolTipTitle); systemTrayIcon->show(); QObject::connect(systemTrayIcon, SIGNAL(wheel(int)), q, SLOT(legacyWheelEvent(int))); QObject::connect(systemTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), q, SLOT(legacyActivated(QSystemTrayIcon::ActivationReason))); } else if (isKde) { // prevent infinite recursion if the KDE platform plugin is loaded // but SNI is not available; see bug 350785 qCWarning(LOG_KNOTIFICATIONS) << "env says KDE is running but SNI unavailable -- check " "KDE_FULL_SESSION and XDG_CURRENT_DESKTOP"; return; } if (menu) { menu->setWindowFlags(Qt::Popup); } } else { delete systemTrayIcon; systemTrayIcon = nullptr; if (menu) { menu->setWindowFlags(Qt::Window); } } if (menu) { QMenu *m = menu; menu = nullptr; q->setContextMenu(m); } } void KStatusNotifierItemPrivate::syncLegacySystemTrayIcon() { if (status == KStatusNotifierItem::NeedsAttention) { #ifdef Q_OS_MACOS QtMac::setBadgeLabelText(QString(QChar(0x26a0))/*QStringLiteral("!")*/); if (attentionIconName.isNull() && attentionIcon.isNull()) { // code adapted from kmail's KMSystemTray::updateCount() int overlaySize = 22; QIcon attnIcon = qApp->windowIcon(); if (!attnIcon.availableSizes().isEmpty()) { overlaySize = attnIcon.availableSizes().at(0).width(); } QFont labelFont = QFontDatabase::systemFont(QFontDatabase::GeneralFont); labelFont.setBold(true); QFontMetrics qfm(labelFont); float attnHeight = overlaySize * 0.667; if (qfm.height() > attnHeight) { float labelSize = attnHeight; labelFont.setPointSizeF(labelSize); } // Paint the label in a pixmap QPixmap overlayPixmap(overlaySize, overlaySize); overlayPixmap.fill(Qt::transparent); QPainter p(&overlayPixmap); p.setFont(labelFont); p.setBrush(Qt::NoBrush); // this sort of badge/label is red on OS X p.setPen(QColor(224,0,0)); p.setOpacity(1.0); // use U+2022, the Unicode bullet p.drawText(overlayPixmap.rect(), Qt::AlignRight|Qt::AlignTop, QString(QChar(0x2022))); p.end(); QPixmap iconPixmap = attnIcon.pixmap(overlaySize, overlaySize); QPainter pp(&iconPixmap); pp.drawPixmap(0, 0, overlayPixmap); pp.end(); systemTrayIcon->setIcon(iconPixmap); } else #endif { if (!movieName.isNull()) { if (!movie) { movie = new QMovie(movieName); } systemTrayIcon->setMovie(movie); } else if (!attentionIconName.isNull()) { systemTrayIcon->setIcon(QIcon::fromTheme(attentionIconName)); } else { systemTrayIcon->setIcon(attentionIcon); } } } else { #ifdef Q_OS_MACOS if (!iconName.isNull()) { QIcon theIcon = QIcon::fromTheme(iconName); systemTrayIcon->setIconWithMask(theIcon, status==KStatusNotifierItem::Passive); } else { systemTrayIcon->setIconWithMask(icon, status==KStatusNotifierItem::Passive); } QtMac::setBadgeLabelText(QString()); #else if (!iconName.isNull()) { systemTrayIcon->setIcon(QIcon::fromTheme(iconName)); } else { systemTrayIcon->setIcon(icon); } #endif } systemTrayIcon->setToolTip(toolTipTitle); } void KStatusNotifierItemPrivate::contextMenuAboutToShow() { if (!hasQuit && standardActionsEnabled) { // we need to add the actions to the menu afterwards so that these items // appear at the _END_ of the menu menu->addSeparator(); if (associatedWidget && associatedWidget != menu) { QAction *action = actionCollection.value(QStringLiteral("minimizeRestore")); if (action) { menu->addAction(action); } } QAction *action = actionCollection.value(QStringLiteral("quit")); if (action) { menu->addAction(action); } hasQuit = true; } if (associatedWidget && associatedWidget != menu) { QAction *action = actionCollection.value(QStringLiteral("minimizeRestore")); if (checkVisibility(QPoint(0, 0), false)) { action->setText(KStatusNotifierItem::tr("&Restore")); } else { action->setText(KStatusNotifierItem::tr("&Minimize")); } } } void KStatusNotifierItemPrivate::maybeQuit() { QString caption = QGuiApplication::applicationDisplayName(); if (caption.isEmpty()) { caption = QCoreApplication::applicationName(); } QString query = KStatusNotifierItem::tr("Are you sure you want to quit %1?").arg(caption); if (QMessageBox::question(associatedWidget, KStatusNotifierItem::tr("Confirm Quit From System Tray"), query) == QMessageBox::Yes) { qApp->quit(); } } void KStatusNotifierItemPrivate::minimizeRestore() { q->activate(systemTrayIcon ? systemTrayIcon->geometry().topLeft() : QPoint(0, 0)); } void KStatusNotifierItemPrivate::hideMenu() { menu->hide(); } void KStatusNotifierItemPrivate::minimizeRestore(bool show) { KWindowInfo info(associatedWidget->winId(), NET::WMDesktop); if (show) { if (onAllDesktops) { KWindowSystem::setOnAllDesktops(associatedWidget->winId(), true); } else { KWindowSystem::setCurrentDesktop(info.desktop()); } auto state = associatedWidget->windowState() & ~Qt::WindowMinimized; associatedWidget->setWindowState(state); associatedWidget->show(); associatedWidget->raise(); } else { onAllDesktops = info.onAllDesktops(); associatedWidget->hide(); } } +#ifdef QT_DBUS_LIB KDbusImageStruct KStatusNotifierItemPrivate::imageToStruct(const QImage &image) { KDbusImageStruct icon; icon.width = image.size().width(); icon.height = image.size().height(); if (image.format() == QImage::Format_ARGB32) { icon.data = QByteArray((char *)image.bits(), image.sizeInBytes()); } else { QImage image32 = image.convertToFormat(QImage::Format_ARGB32); icon.data = QByteArray((char *)image32.bits(), image32.sizeInBytes()); } //swap to network byte order if we are little endian if (QSysInfo::ByteOrder == QSysInfo::LittleEndian) { quint32 *uintBuf = (quint32 *) icon.data.data(); for (uint i = 0; i < icon.data.size() / sizeof(quint32); ++i) { *uintBuf = qToBigEndian(*uintBuf); ++uintBuf; } } return icon; } KDbusImageVector KStatusNotifierItemPrivate::iconToVector(const QIcon &icon) { KDbusImageVector iconVector; QPixmap iconPixmap; //if an icon exactly that size wasn't found don't add it to the vector const auto lstSizes = icon.availableSizes(); for (QSize size : lstSizes) { iconPixmap = icon.pixmap(size); iconVector.append(imageToStruct(iconPixmap.toImage())); } return iconVector; } +#endif #include "moc_kstatusnotifieritem.cpp" #include "moc_kstatusnotifieritemprivate_p.cpp" diff --git a/src/kstatusnotifieritemprivate_p.h b/src/kstatusnotifieritemprivate_p.h index f3f18f4..f500e2a 100644 --- a/src/kstatusnotifieritemprivate_p.h +++ b/src/kstatusnotifieritemprivate_p.h @@ -1,178 +1,189 @@ /* This file is part of the KDE libraries Copyright 2009 by Marco Martin This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License (LGPL) as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KSTATUSNOTIFIERITEMPRIVATE_H #define KSTATUSNOTIFIERITEMPRIVATE_H #include #include #include #include #include #include #include "kstatusnotifieritem.h" + +#ifdef QT_DBUS_LIB #include "kstatusnotifieritemdbus_p.h" #include "statusnotifierwatcher_interface.h" #include "notifications_interface.h" +#endif + class KSystemTrayIcon; class QMenu; class QAction; // this class is needed because we can't just put an event filter on it: // the events that are passed to QSystemTrayIcon are done so in a way that // bypasses the usual event filtering mechanisms *sigh* class KStatusNotifierLegacyIcon : public QSystemTrayIcon { Q_OBJECT public: KStatusNotifierLegacyIcon(QObject *parent) : QSystemTrayIcon(parent) { } bool event(QEvent *e) override { if (e->type() == QEvent::Wheel) { QWheelEvent *wheelEvent = static_cast(e); emit wheel(wheelEvent->angleDelta().y()); } return false; } void setMovie(QMovie *movie) { if (m_movie.data() == movie) { return; } delete m_movie.data(); m_movie = movie; if (!movie) { return; } movie->setParent(this); movie->setCacheMode(QMovie::CacheAll); connect(movie, &QMovie::frameChanged, this, &KStatusNotifierLegacyIcon::slotNewFrame); } void setIconWithMask(QIcon &icon, bool isMask) { icon.setIsMask(isMask); QSystemTrayIcon::setIcon(icon); } Q_SIGNALS: void wheel(int); private Q_SLOTS: void slotNewFrame() { if (m_movie) { setIcon(QIcon(m_movie.data()->currentPixmap())); } } private: QPointer m_movie; }; class KStatusNotifierItemPrivate { public: KStatusNotifierItemPrivate(KStatusNotifierItem *item); void init(const QString &extraId); void registerToDaemon(); void serviceChange(const QString &name, const QString &oldOwner, const QString &newOwner); void setLegacySystemTrayEnabled(bool enabled); void syncLegacySystemTrayIcon(); void contextMenuAboutToShow(); void maybeQuit(); void minimizeRestore(); void minimizeRestore(bool show); void hideMenu(); void setLegacyMode(bool legacy); void checkForRegisteredHosts(); void legacyWheelEvent(int delta); void legacyActivated(QSystemTrayIcon::ActivationReason reason); - KDbusImageStruct imageToStruct(const QImage &image); - KDbusImageVector iconToVector(const QIcon &icon); bool checkVisibility(QPoint pos, bool perform = true); static const int s_protocolVersion; KStatusNotifierItem *q; +#ifdef QT_DBUS_LIB + KDbusImageStruct imageToStruct(const QImage &image); + KDbusImageVector iconToVector(const QIcon &icon); + + KDbusImageVector serializedIcon; + KDbusImageVector serializedAttentionIcon; + KDbusImageVector serializedOverlayIcon; + KDbusImageVector serializedToolTipIcon; + + + org::kde::StatusNotifierWatcher *statusNotifierWatcher = nullptr; + org::freedesktop::Notifications *notificationsClient = nullptr; + + KStatusNotifierItemDBus *statusNotifierItemDBus; +#endif + KStatusNotifierItem::ItemCategory category; QString id; QString title; KStatusNotifierItem::ItemStatus status; QString iconName; - KDbusImageVector serializedIcon; QIcon icon; QString overlayIconName; - KDbusImageVector serializedOverlayIcon; QIcon overlayIcon; QString attentionIconName; QIcon attentionIcon; - KDbusImageVector serializedAttentionIcon; QString movieName; QPointer movie; QString toolTipIconName; - KDbusImageVector serializedToolTipIcon; QIcon toolTipIcon; QString toolTipTitle; QString toolTipSubTitle; QString iconThemePath; QString menuObjectPath; + KStatusNotifierLegacyIcon *systemTrayIcon; QMenu *menu; QHash actionCollection; QWidget *associatedWidget; QPoint associatedWidgetPos; QAction *titleAction; - org::kde::StatusNotifierWatcher *statusNotifierWatcher; - org::freedesktop::Notifications *notificationsClient; - KStatusNotifierLegacyIcon *systemTrayIcon; - KStatusNotifierItemDBus *statusNotifierItemDBus; // Ensure that closing the last KMainWindow doesn't exit the application // if a system tray icon is still present. QEventLoopLocker eventLoopLocker; bool hasQuit : 1; bool onAllDesktops : 1; bool standardActionsEnabled : 1; }; #endif