dde-dock/plugins/system-tray/sni/statusnotifierwatcher.cpp
listenerri 68a4332ad1 feet(plugin): StatusNotifierItem system tray protocol support
Change-Id: I3b879b052e46f2eda758085dfb4701d23923d445
2018-10-13 13:30:42 +08:00

132 lines
4.4 KiB
C++

/*
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: listenerri <listenerri@gmail.com>
*
* Maintainer: listenerri <listenerri@gmail.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "statusnotifierwatcher.h"
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
#include <QDBusMessage>
#include <QDebug>
#include "dbus/sni/statusnotifierwatcheradapter.h"
#include "dbus/sni/statusnotifieritem_interface.h"
StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent)
{
new StatusNotifierWatcherAdaptor(this);
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.registerObject(QStringLiteral("/StatusNotifierWatcher"), this);
dbus.registerService(QStringLiteral("org.kde.StatusNotifierWatcher"));
m_serviceWatcher = new QDBusServiceWatcher(this);
m_serviceWatcher->setConnection(dbus);
m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &StatusNotifierWatcher::serviceUnregistered);
}
StatusNotifierWatcher::~StatusNotifierWatcher()
{
QDBusConnection dbus = QDBusConnection::sessionBus();
dbus.unregisterService(QStringLiteral("org.kde.StatusNotifierWatcher"));
}
void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &serviceOrPath)
{
QString service;
QString path;
if (serviceOrPath.startsWith(QLatin1Char('/'))) {
service = message().service();
path = serviceOrPath;
} else {
service = serviceOrPath;
path = QStringLiteral("/StatusNotifierItem");
}
QString notifierItemId = service + path;
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value() &&
!m_registeredServices.contains(notifierItemId)) {
qDebug()<<"Registering" << notifierItemId << "to system tray";
//check if the service has registered a SystemTray object
com::deepin::dde::StatusNotifierItem trayclient(service, path,
QDBusConnection::sessionBus());
if (trayclient.isValid()) {
m_registeredServices.append(notifierItemId);
m_serviceWatcher->addWatchedService(service);
emit StatusNotifierItemRegistered(notifierItemId);
}
}
}
QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const
{
return m_registeredServices;
}
void StatusNotifierWatcher::serviceUnregistered(const QString& name)
{
qDebug()<<"Service "<< name << "unregistered";
m_serviceWatcher->removeWatchedService(name);
QString match = name + QLatin1Char('/');
QStringList::Iterator it = m_registeredServices.begin();
while (it != m_registeredServices.end()) {
if (it->startsWith(match)) {
QString name = *it;
it = m_registeredServices.erase(it);
emit StatusNotifierItemUnregistered(name);
} else {
++it;
}
}
if (m_statusNotifierHostServices.contains(name)) {
m_statusNotifierHostServices.remove(name);
emit StatusNotifierHostUnregistered();
}
}
void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service)
{
if (service.contains(QStringLiteral("org.kde.StatusNotifierHost-")) &&
QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value() &&
!m_statusNotifierHostServices.contains(service)) {
qDebug()<<"Registering"<<service<<"as system tray";
m_statusNotifierHostServices.insert(service);
m_serviceWatcher->addWatchedService(service);
emit StatusNotifierHostRegistered();
}
}
bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const
{
return !m_statusNotifierHostServices.isEmpty();
}
int StatusNotifierWatcher::ProtocolVersion() const
{
return 0;
}