kdelibs/kdeui/notifications/knotificationmanager.cpp

166 lines
4.9 KiB
C++
Raw Normal View History

2014-11-13 01:04:59 +02:00
/* This file is part of the KDE libraries
Copyright (C) 2005 Olivier Goffart <ogoffart at kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
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.
*/
2015-09-04 21:57:46 +00:00
#include <QWidget>
#include <QImageWriter>
#include <QBuffer>
2015-09-04 21:57:46 +00:00
2014-11-13 01:04:59 +02:00
#include "knotificationmanager_p.h"
#include <ktoolinvocation.h>
#include "knotification.h"
#include <kdebug.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kconfig.h>
#include <klocale.h>
static const QByteArray imageFormat = QImageWriter::defaultImageFormat();
2014-11-13 01:04:59 +02:00
KNotificationManager * KNotificationManager::self()
{
K_GLOBAL_STATIC(KNotificationManager, s_self)
return s_self;
}
KNotificationManager::KNotificationManager()
{
QDBusConnectionInterface* sessionIface = QDBusConnection::sessionBus().interface();
if (!sessionIface->isServiceRegistered("org.kde.knotify")) {
QDBusReply<void> sessionReply = sessionIface->startService("org.kde.knotify");
if (!sessionReply.isValid()) {
kError() << "Couldn't start knotify service" << sessionReply.error();
}
}
m_knotify = new org::kde::KNotify(
QLatin1String("org.kde.knotify"), QLatin1String("/Notify"), QDBusConnection::sessionBus(), this
);
connect(
m_knotify, SIGNAL(notificationClosed(int)),
this, SLOT(notificationClosed(int))
);
connect(
m_knotify, SIGNAL(notificationActivated(int,int)),
this, SLOT(notificationActivated(int,int))
);
2014-11-13 01:04:59 +02:00
}
KNotificationManager::~KNotificationManager()
{
delete m_knotify;
2014-11-13 01:04:59 +02:00
}
void KNotificationManager::notificationActivated(int id, int action)
2014-11-13 01:04:59 +02:00
{
if (m_notifications.contains(id)) {
2014-11-13 01:04:59 +02:00
kDebug(299) << id << " " << action;
KNotification *n = m_notifications[id];
m_notifications.remove(id);
2014-11-13 01:04:59 +02:00
n->activate( action );
}
}
void KNotificationManager::notificationClosed(int id)
2014-11-13 01:04:59 +02:00
{
if (m_notifications.contains(id)) {
2014-11-13 01:04:59 +02:00
kDebug( 299 ) << id;
KNotification *n = m_notifications[id];
m_notifications.remove(id);
2014-11-13 01:04:59 +02:00
n->close();
}
}
void KNotificationManager::close(int id, bool force)
2014-11-13 01:04:59 +02:00
{
if (force || m_notifications.contains(id)) {
m_notifications.remove(id);
2015-09-22 10:34:04 +00:00
kDebug( 299 ) << id;
m_knotify->closeNotification(id);
2015-09-22 10:34:04 +00:00
}
2014-11-13 01:04:59 +02:00
}
bool KNotificationManager::notify(KNotification* n, const QPixmap &pix,
const QStringList &actions,
const KNotification::ContextList & contexts,
const QString &appname)
2014-11-13 01:04:59 +02:00
{
WId winId = n->widget() ? n->widget()->window()->winId() : 0;
2014-11-13 01:04:59 +02:00
QByteArray pixmapData;
QBuffer buffer(&pixmapData);
buffer.open(QIODevice::WriteOnly);
pix.save(&buffer, imageFormat);
2014-11-13 01:04:59 +02:00
QVariantList contextList;
foreach (const KNotification::Context& ctx, contexts) {
2014-11-13 01:04:59 +02:00
QVariantList vl;
vl << ctx.first << ctx.second;
contextList << vl;
}
// Persistent => 0 == infinite timeout
// CloseOnTimeout => -1 == let the server decide
int timeout = (n->flags() & KNotification::Persistent) ? 0 : -1;
QList<QVariant> args;
2014-11-13 01:04:59 +02:00
args << n->eventId() << (appname.isEmpty() ? KGlobal::mainComponent().componentName() : appname);
args.append(QVariant(contextList));
args << n->title() << n->text() << pixmapData << QVariant(actions) << timeout << qlonglong(winId) ;
return m_knotify->callWithCallback( "event", args, n, SLOT(slotReceivedId(int)), SLOT(slotReceivedIdError(QDBusError)));
2014-11-13 01:04:59 +02:00
}
void KNotificationManager::insert(KNotification *n, int id)
{
m_notifications.insert(id, n);
2014-11-13 01:04:59 +02:00
}
void KNotificationManager::update(KNotification * n, int id)
{
if (id <= 0) {
2015-09-22 10:34:04 +00:00
return;
}
2014-11-13 01:04:59 +02:00
QByteArray pixmapData;
if (!n->pixmap().isNull()) {
2014-11-13 01:04:59 +02:00
QBuffer buffer(&pixmapData);
buffer.open(QIODevice::WriteOnly);
n->pixmap().save(&buffer, imageFormat);
2014-11-13 01:04:59 +02:00
}
m_knotify->update(id, n->title(), n->text(), pixmapData , n->actions() );
2014-11-13 01:04:59 +02:00
}
void KNotificationManager::reemit(KNotification * n, int id)
{
2015-09-22 10:34:04 +00:00
QVariantList contextList;
foreach (const KNotification::Context& ctx, n->contexts()) {
// kDebug(299) << "add context " << ctx.first << "-" << ctx.second;
2015-09-22 10:34:04 +00:00
QVariantList vl;
vl << ctx.first << ctx.second;
contextList << vl;
}
m_knotify->reemit(id, contextList);
2014-11-13 01:04:59 +02:00
}
#include "moc_knotificationmanager_p.cpp"