• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • notifications
knotificationmanager.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2005 Olivier Goffart <ogoffart at kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "knotificationmanager_p.h"
20 #include <ktoolinvocation.h>
21 #include "knotification.h"
22 
23 #include <QHash>
24 #include <QWidget>
25 #include <QtDBus/QtDBus>
26 #include <QPointer>
27 
28 #include <kdebug.h>
29 #include <kapplication.h>
30 #include <kiconloader.h>
31 #include <kconfig.h>
32 #include <klocale.h>
33 
34 #include "knotify_interface.h"
35 
36 typedef QHash<QString,QString> Dict;
37 
38 struct KNotificationManager::Private
39 {
40  QHash<int , KNotification*> notifications;
41  org::kde::KNotify *knotify;
42 };
43 
44 KNotificationManager * KNotificationManager::self()
45 {
46  K_GLOBAL_STATIC(KNotificationManager, s_self)
47  return s_self;
48 }
49 
50 
51 KNotificationManager::KNotificationManager()
52  : d(new Private)
53 {
54  if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.knotify")) {
55  QString error;
56  int ret = KToolInvocation::startServiceByDesktopPath("knotify4.desktop",
57  QStringList(), &error);
58  if (ret > 0) {
59  kError() << "Couldn't start knotify from knotify4.desktop: " << error << endl;
60  }
61  }
62  d->knotify =
63  new org::kde::KNotify(QLatin1String("org.kde.knotify"), QLatin1String("/Notify"), QDBusConnection::sessionBus(), this);
64  connect(d->knotify, SIGNAL(notificationClosed(int)),
65  this, SLOT(notificationClosed(int)));
66  connect(d->knotify, SIGNAL(notificationActivated(int,int)),
67  this, SLOT(notificationActivated(int,int)));
68 }
69 
70 
71 KNotificationManager::~KNotificationManager()
72 {
73  delete d->knotify;
74  delete d;
75 }
76 
77 void KNotificationManager::notificationActivated( int id, int action )
78 {
79  if(d->notifications.contains(id))
80  {
81  kDebug(299) << id << " " << action;
82  KNotification *n = d->notifications[id];
83  d->notifications.remove(id);
84  n->activate( action );
85  }
86 }
87 
88 void KNotificationManager::notificationClosed( int id )
89 {
90  if(d->notifications.contains(id))
91  {
92  kDebug( 299 ) << id;
93  KNotification *n = d->notifications[id];
94  d->notifications.remove(id);
95  n->close();
96  }
97 }
98 
99 
100 void KNotificationManager::close( int id, bool force )
101 {
102  if(force || d->notifications.contains(id)) {
103  d->notifications.remove(id);
104  kDebug( 299 ) << id;
105  d->knotify->closeNotification(id);
106  }
107 }
108 
109 bool KNotificationManager::notify( KNotification* n, const QPixmap &pix,
110  const QStringList &actions,
111  const KNotification::ContextList & contexts,
112  const QString &appname)
113 {
114  WId winId=n->widget() ? n->widget()->topLevelWidget()->winId() : 0;
115 
116  QByteArray pixmapData;
117  {
118  QBuffer buffer(&pixmapData);
119  buffer.open(QIODevice::WriteOnly);
120  pix.save(&buffer, "PNG");
121  }
122 
123  QVariantList contextList;
124  typedef QPair<QString,QString> Context;
125  foreach (const Context& ctx, contexts)
126  {
127  QVariantList vl;
128  vl << ctx.first << ctx.second;
129  contextList << vl;
130  }
131 
132  // Persistent => 0 == infinite timeout
133  // CloseOnTimeout => -1 == let the server decide
134  int timeout = (n->flags() & KNotification::Persistent) ? 0 : -1;
135 
136  QList<QVariant> args;
137  args << n->eventId() << (appname.isEmpty() ? KGlobal::mainComponent().componentName() : appname);
138  args.append(QVariant(contextList));
139  args << n->title() << n->text() << pixmapData << QVariant(actions) << timeout << qlonglong(winId) ;
140  return d->knotify->callWithCallback( "event", args, n, SLOT(slotReceivedId(int)), SLOT(slotReceivedIdError(QDBusError)));
141 }
142 
143 void KNotificationManager::insert(KNotification *n, int id)
144 {
145  d->notifications.insert(id, n);
146 }
147 
148 void KNotificationManager::update(KNotification * n, int id)
149 {
150  if(id <= 0)
151  return;
152 
153  QByteArray pixmapData;
154  if(!n->pixmap().isNull())
155  {
156  QBuffer buffer(&pixmapData);
157  buffer.open(QIODevice::WriteOnly);
158  n->pixmap().save(&buffer, "PNG");
159  }
160 
161  d->knotify->update(id, n->title(), n->text(), pixmapData , n->actions() );
162 }
163 
164 void KNotificationManager::reemit(KNotification * n, int id)
165 {
166  QVariantList contextList;
167  typedef QPair<QString,QString> Context;
168  foreach (const Context& ctx, n->contexts())
169  {
170 // kDebug(299) << "add context " << ctx.first << "-" << ctx.second;
171  QVariantList vl;
172  vl << ctx.first << ctx.second;
173  contextList << vl;
174  }
175 
176  d->knotify->reemit(id, contextList);
177 }
178 
179 
180 #include "knotificationmanager_p.moc"
QVariant
Dict
QHash< QString, QString > Dict
Definition: knotificationmanager.cpp:36
kdebug.h
KNotification::title
QString title() const
Definition: knotification.cpp:120
kapplication.h
timeout
int timeout
K_GLOBAL_STATIC
#define K_GLOBAL_STATIC(TYPE, NAME)
kconfig.h
KNotification::close
void close()
Close the notification without activating it.
Definition: knotification.cpp:246
KNotification::flags
NotificationFlags flags() const
Definition: knotification.cpp:206
KNotification::text
QString text() const
Definition: knotification.cpp:125
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
kiconloader.h
KNotification::activate
void activate(unsigned int action=0)
Activate the action specified action If the action is zero, then the default action is activated...
Definition: knotification.cpp:222
QString
QHash< QString, QString >
ktoolinvocation.h
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
KNotification::actions
QStringList actions() const
Definition: knotification.cpp:173
knotification.h
KToolInvocation::startServiceByDesktopPath
static int startServiceByDesktopPath(const QString &_name, const QString &URL, QString *error=0, QString *serviceName=0, int *pid=0, const QByteArray &startup_id=QByteArray(), bool noWait=false)
KStandardShortcut::insert
const KShortcut & insert()
Toggle insert/overwrite (with visual feedback, e.g.
Definition: kstandardguiitem.cpp:264
QStringList
KNotification::Persistent
The notification will NOT be automatically closed after a timeout.
Definition: knotification.h:226
KNotification::eventId
QString eventId() const
Definition: knotification.cpp:115
KComponentData::componentName
QString componentName() const
KNotification
KNotification is used to notify the user of an event.
Definition: knotification.h:180
KNotification::pixmap
QPixmap pixmap() const
Definition: knotification.cpp:160
KGlobal::mainComponent
const KComponentData & mainComponent()
QPair
KNotification::widget
QWidget * widget() const
the widget associated to the notification
Definition: knotification.cpp:130
KStandardAction::close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.
Definition: kstandardaction.cpp:269
KNotification::contexts
ContextList contexts() const
Definition: knotification.cpp:186
QList< Context >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal