diff --git a/src/kmsystemtray.h b/src/kmsystemtray.h --- a/src/kmsystemtray.h +++ b/src/kmsystemtray.h @@ -24,6 +24,7 @@ #include #include +class QDBusServiceWatcher; class QMenu; /** @@ -57,6 +58,7 @@ void slotContextMenuAboutToShow(); void slotSelectCollection(QAction *act); void initListOfCollection(); + void initUnity(); void slotCollectionStatisticsChanged(Akonadi::Collection::Id, const Akonadi::CollectionStatistics &); void slotGeneralFontChanged(); @@ -75,6 +77,9 @@ QMenu *mNewMessagesPopup = nullptr; QAction *mSendQueued = nullptr; + + QDBusServiceWatcher *mUnityServiceWatcher = nullptr; + bool mUnityServiceAvailable = false; }; } #endif diff --git a/src/kmsystemtray.cpp b/src/kmsystemtray.cpp --- a/src/kmsystemtray.cpp +++ b/src/kmsystemtray.cpp @@ -32,6 +32,13 @@ #include #include #include + +#include +#include +#include +#include +#include + #include "widgets/kactionmenutransport.h" #include @@ -55,6 +62,7 @@ KMSystemTray::KMSystemTray(QObject *parent) : KStatusNotifierItem(parent) + , mUnityServiceWatcher(new QDBusServiceWatcher(this)) { qCDebug(KMAIL_LOG) << "Initting systray"; setToolTipTitle(i18n("KMail")); @@ -81,6 +89,8 @@ connect(kmkernel->folderCollectionMonitor(), &Akonadi::Monitor::collectionUnsubscribed, this, &KMSystemTray::initListOfCollection); initListOfCollection(); + + initUnity(); } bool KMSystemTray::buildPopupMenu() @@ -149,6 +159,21 @@ } else { setIconByName(QStringLiteral("mail-mark-unread-new")); } + + if (mUnityServiceAvailable) { + const QString launcherId = qApp->desktopFileName() + QLatin1String(".desktop"); + + const QVariantMap properties{ + {QStringLiteral("count-visible"), mCount > 0}, + {QStringLiteral("count"), mCount} + }; + + QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/org/kmail2/UnityLauncher"), + QStringLiteral("com.canonical.Unity.LauncherEntry"), + QStringLiteral("Update")); + message.setArguments({launcherId, properties}); + QDBusConnection::sessionBus().send(message); + } } void KMSystemTray::setSystrayIconNotificationsEnabled(bool enabled) @@ -291,6 +316,43 @@ updateCount(); } +void KMSystemTray::initUnity() +{ + mUnityServiceWatcher->setConnection(QDBusConnection::sessionBus()); + mUnityServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration | QDBusServiceWatcher::WatchForRegistration); + mUnityServiceWatcher->addWatchedService(QStringLiteral("com.canonical.Unity")); + connect(mUnityServiceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this](const QString &service) { + Q_UNUSED(service); + mUnityServiceAvailable = true; + updateCount(); + }); + + connect(mUnityServiceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](const QString &service) { + Q_UNUSED(service); + mUnityServiceAvailable = false; + }); + + // QDBusConnectionInterface::isServiceRegistered blocks + QDBusPendingCall listNamesCall = QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames")); + QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(listNamesCall, this); + connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { + QDBusPendingReply reply = *watcher; + watcher->deleteLater(); + + if (reply.isError()) { + return; + } + + const QStringList &services = reply.value(); + + mUnityServiceAvailable = services.contains(QLatin1String("com.canonical.Unity")); + if (mUnityServiceAvailable) { + updateCount(); + } + }); + +} + void KMSystemTray::unreadMail(const QAbstractItemModel *model, const QModelIndex &parentIndex) { const int rowCount = model->rowCount(parentIndex);