Commit e0446798 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[KMSystemTray] Expose unread email count via Unity launcher API

This will show the number of unread emails in the Task Manager.

Differential Revision: https://phabricator.kde.org/D9841
parent e0d4e861
......@@ -32,6 +32,13 @@
#include <QMenu>
#include <KLocalizedString>
#include <QAction>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QDBusPendingReply>
#include <QDBusServiceWatcher>
#include "widgets/kactionmenutransport.h"
#include <AkonadiCore/ChangeRecorder>
......@@ -55,6 +62,7 @@ using namespace KMail;
KMSystemTray::KMSystemTray(QObject *parent)
: KStatusNotifierItem(parent)
, mUnityServiceWatcher(new QDBusServiceWatcher(this))
{
qCDebug(KMAIL_LOG) << "Initting systray";
setToolTipTitle(i18n("KMail"));
......@@ -81,6 +89,8 @@ KMSystemTray::KMSystemTray(QObject *parent)
connect(kmkernel->folderCollectionMonitor(), &Akonadi::Monitor::collectionUnsubscribed, this, &KMSystemTray::initListOfCollection);
initListOfCollection();
initUnity();
}
bool KMSystemTray::buildPopupMenu()
......@@ -149,6 +159,21 @@ void KMSystemTray::updateCount()
} 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 @@ void KMSystemTray::initListOfCollection()
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<QStringList> 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);
......
......@@ -24,6 +24,7 @@
#include <QAction>
#include <QAbstractItemModel>
class QDBusServiceWatcher;
class QMenu;
/**
......@@ -57,6 +58,7 @@ private:
void slotContextMenuAboutToShow();
void slotSelectCollection(QAction *act);
void initListOfCollection();
void initUnity();
void slotCollectionStatisticsChanged(Akonadi::Collection::Id, const Akonadi::CollectionStatistics &);
void slotGeneralFontChanged();
......@@ -75,6 +77,9 @@ private:
QMenu *mNewMessagesPopup = nullptr;
QAction *mSendQueued = nullptr;
QDBusServiceWatcher *mUnityServiceWatcher = nullptr;
bool mUnityServiceAvailable = false;
};
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment