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

KDEUI

  • kdeui
  • notifications
knotification.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2005-2006 Olivier Goffart <ogoffart at kde.org>
3
4 code from KNotify/KNotifyClient
5 Copyright (c) 1997 Christian Esken (esken@kde.org)
6 2000 Charles Samuels (charles@kde.org)
7 2000 Stefan Schimanski (1Stein@gmx.de)
8 2000 Matthias Ettrich (ettrich@kde.org)
9 2000 Waldo Bastian <bastian@kde.org>
10 2000-2003 Carsten Pfeiffer <pfeiffer@kde.org>
11 2005 Allan Sandfeld Jensen <kde@carewolf.com>
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Library General Public
15 License version 2 as published by the Free Software Foundation.
16
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Library General Public License for more details.
21
22 You should have received a copy of the GNU Library General Public License
23 along with this library; see the file COPYING.LIB. If not, write to
24 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 Boston, MA 02110-1301, USA.
26*/
27
28#include "knotification.h"
29#include "knotificationmanager_p.h"
30
31#include <kmessagebox.h>
32#include <klocale.h>
33#include <kiconloader.h>
34#include <kconfig.h>
35#include <kpassivepopup.h>
36#include <kdialog.h>
37#include <kmacroexpander.h>
38#include <kwindowsystem.h>
39#include <kdebug.h>
40#include <kvbox.h>
41#include <kapplication.h>
42
43#include <QMap>
44#include <QPixmap>
45#include <QPointer>
46#include <QLabel>
47#include <QTimer>
48#include <QTabWidget>
49#include <QFile>
50#include <QStringList>
51#include <QTextStream>
52#include <QDateTime>
53#include <QDBusError>
54
55struct KNotification::Private
56{
57 QString eventId;
58 int id;
59 int ref;
60
61 QWidget *widget;
62 QString title;
63 QString text;
64 QStringList actions;
65 QPixmap pixmap;
66 ContextList contexts;
67 NotificationFlags flags;
68 KComponentData componentData;
69
70 QTimer updateTimer;
71 bool needUpdate;
72
73 Private() : id(0), ref(1), widget(0l), needUpdate(false) {}
79 static void raiseWidget(QWidget *w);
80};
81
82KNotification::KNotification(const QString& eventId, QWidget *parent, const NotificationFlags& flags) :
83 QObject(parent) , d(new Private)
84{
85 d->eventId=eventId;
86 d->flags=flags;
87 setWidget(parent);
88 connect(&d->updateTimer,SIGNAL(timeout()), this, SLOT(update()));
89 d->updateTimer.setSingleShot(true);
90 d->updateTimer.setInterval(100);
91}
92
93KNotification::KNotification(
94 const QString& eventId,
95 const NotificationFlags& flags,
96 QObject *parent)
97 : QObject(parent),
98 d(new Private)
99{
100 d->eventId=eventId;
101 d->flags=flags;
102 connect(&d->updateTimer,SIGNAL(timeout()), this, SLOT(update()));
103 d->updateTimer.setSingleShot(true);
104 d->updateTimer.setInterval(100);
105}
106
107
108KNotification::~KNotification()
109{
110 if(d ->id > 0)
111 KNotificationManager::self()->close( d->id );
112 delete d;
113}
114
115QString KNotification::eventId() const
116{
117 return d->eventId;
118}
119
120QString KNotification::title() const
121{
122 return d->title;
123}
124
125QString KNotification::text() const
126{
127 return d->text;
128}
129
130QWidget *KNotification::widget() const
131{
132 return d->widget;
133}
134
135void KNotification::setWidget(QWidget *wid)
136{
137 d->widget = wid;
138 setParent(wid);
139 if ( wid && d->flags & CloseWhenWidgetActivated ) {
140 wid->installEventFilter(this);
141 }
142}
143
144void KNotification::setTitle(const QString &title)
145{
146 d->needUpdate = true;
147 d->title = title;
148 if(d->id > 0)
149 d->updateTimer.start();
150}
151
152void KNotification::setText(const QString &text)
153{
154 d->needUpdate = true;
155 d->text=text;
156 if(d->id > 0)
157 d->updateTimer.start();
158}
159
160QPixmap KNotification::pixmap() const
161{
162 return d->pixmap;
163}
164
165void KNotification::setPixmap(const QPixmap &pix)
166{
167 d->needUpdate = true;
168 d->pixmap=pix;
169 if(d->id > 0)
170 d->updateTimer.start();
171}
172
173QStringList KNotification::actions() const
174{
175 return d->actions;
176}
177
178void KNotification::setActions(const QStringList& as )
179{
180 d->needUpdate = true;
181 d->actions=as;
182 if(d->id > 0)
183 d->updateTimer.start();
184}
185
186KNotification::ContextList KNotification::contexts() const
187{
188 return d->contexts;
189}
190
191void KNotification::setContexts( const KNotification::ContextList &contexts)
192{
193 d->contexts=contexts;
194}
195
196void KNotification::addContext( const KNotification::Context & context)
197{
198 d->contexts << context;
199}
200
201void KNotification::addContext( const QString & context_key, const QString & context_value )
202{
203 d->contexts << qMakePair( context_key , context_value );
204}
205
206KNotification::NotificationFlags KNotification::flags() const
207{
208 return d->flags;
209}
210
211void KNotification::setFlags(const NotificationFlags & flags)
212{
213 d->flags=flags;
214}
215
216
217void KNotification::setComponentData(const KComponentData &c)
218{
219 d->componentData = c;
220}
221
222void KNotification::activate(unsigned int action)
223{
224 switch (action)
225 {
226 case 0:
227 emit activated();
228 break;
229 case 1:
230 emit action1Activated();
231 break;
232 case 2:
233 emit action2Activated();
234 break;
235 case 3:
236 emit action3Activated();
237 break;
238 }
239 emit activated(action);
240 if(d->id != -1)
241 deleteLater();
242 d->id = -2;
243}
244
245
246void KNotification::close()
247{
248 if(d->id >= 0)
249 KNotificationManager::self()->close( d->id );
250 if(d->id != -1) //=-1 mean still waiting for receiving the id
251 deleteLater();
252 d->id = -2;
253 emit closed();
254}
255
256
257void KNotification::raiseWidget()
258{
259 if ( !d->widget ) {
260 return;
261 }
262
263 Private::raiseWidget( d->widget );
264}
265
266
267void KNotification::Private::raiseWidget(QWidget *w)
268{
269 //TODO this function is far from finished.
270 if(w->isTopLevel())
271 {
272 w->raise();
273#if defined(Q_WS_MAC)
274 w->activateWindow();
275#else
276 KWindowSystem::activateWindow( w->winId() );
277#endif
278 }
279 else
280 {
281 QWidget *pw=w->parentWidget();
282 raiseWidget(pw);
283
284 if( QTabWidget *tab_widget=qobject_cast<QTabWidget*>(pw))
285 {
286 tab_widget->setCurrentIndex(tab_widget->indexOf(w));
287 }
288 }
289}
290
291KNotification *KNotification::event( const QString& eventid , const QString& title, const QString& text,
292 const QPixmap& pixmap, QWidget *widget, const NotificationFlags &flags, const KComponentData &componentData)
293{
294 KNotification *notify=new KNotification(eventid, widget, flags);
295 notify->setTitle(title);
296 notify->setText(text);
297 notify->setPixmap(pixmap);
298 notify->setComponentData(componentData);
299
300 QTimer::singleShot(0,notify,SLOT(sendEvent()));
301
302 return notify;
303}
304
305KNotification *KNotification::event( const QString& eventid , const QString& text,
306 const QPixmap& pixmap, QWidget *widget, const NotificationFlags &flags, const KComponentData &componentData)
307{
308 return event( eventid, QString(), text, pixmap, widget, flags, componentData );
309}
310
311
312KNotification *KNotification::event( StandardEvent eventid , const QString& title, const QString& text,
313 const QPixmap& pixmap, QWidget *widget, const NotificationFlags &flags)
314{
315 QString message;
316 switch ( eventid ) {
317 case Warning:
318 message = QLatin1String("warning");
319 break;
320 case Error:
321 message = QLatin1String("fatalerror");
322 break;
323 case Catastrophe:
324 message = QLatin1String("catastrophe");
325 break;
326 case Notification: // fall through
327 default:
328 message = QLatin1String("notification");
329 break;
330 }
331 return event( message, title, text, pixmap, widget , flags | DefaultEvent );
332}
333
334KNotification *KNotification::event( StandardEvent eventid , const QString& text,
335 const QPixmap& pixmap, QWidget *widget, const NotificationFlags &flags)
336{
337 return event( eventid, QString(), text, pixmap, widget , flags );
338}
339
340void KNotification::ref()
341{
342 d->ref++;
343}
344
345void KNotification::deref()
346{
347 d->ref--;
348 if(d->ref==0)
349 close();
350}
351
352void KNotification::beep( const QString & reason, QWidget * widget )
353{
354 event( QLatin1String("beep"), reason, QPixmap(), widget , CloseOnTimeout | DefaultEvent );
355}
356
357void KNotification::sendEvent()
358{
359 d->needUpdate = false;
360 if(d->id == 0)
361 {
362 QString appname;
363
364 if(d->flags & DefaultEvent)
365 appname = QLatin1String("kde");
366 else if(d->componentData.isValid()) {
367 appname = d->componentData.componentName();
368 } else {
369 appname = KGlobal::mainComponent().componentName();
370 }
371
372 if (KNotificationManager::self()->notify( this , d->pixmap , d->actions , d->contexts , appname ))
373 d->id = -1;
374 }
375 else if(d->id > 0)
376 {
377 KNotificationManager::self()->reemit(this , d->id );
378 }
379 else if(d->id == -1)
380 {
381 //schedule an update.
382 d->needUpdate = true;
383 }
384}
385
386void KNotification::slotReceivedId(int id)
387{
388 if(d->id == -2) //we are already closed
389 {
390 KNotificationManager::self()->close( id, /*force=*/ true );
391 deleteLater();
392 return;
393 }
394 d->id=id;
395 if(d->id>0)
396 {
397 KNotificationManager::self()->insert(this,d->id);
398 if (d->needUpdate)
399 sendEvent();
400 }
401 else
402 {
403 //if there is no presentation, delete the object
404 QTimer::singleShot(0, this, SLOT(deref()));
405 }
406
407}
408
409void KNotification::slotReceivedIdError(const QDBusError& error)
410{
411 if(d->id == -2) //we are already closed
412 {
413 deleteLater();
414 return;
415 }
416 kWarning(299) << "Error while contacting notify daemon" << error.message();
417 d->id = -3;
418 QTimer::singleShot(0, this, SLOT(deref()));
419}
420
421
422void KNotification::update()
423{
424 KNotificationManager::self()->update(this, d->id);
425}
426
427bool KNotification::eventFilter( QObject * watched, QEvent * event )
428{
429 if( watched == d->widget )
430 {
431 if( event->type() == QEvent::WindowActivate )
432 {
433 if( d->flags & CloseWhenWidgetActivated )
434 QTimer::singleShot(500, this, SLOT(close()));
435 }
436 //kDebug(299) << event->type();
437 }
438
439 return false;
440}
441
442
443#include "knotification.moc"
KComponentData
KComponentData::componentName
QString componentName() const
KNotification
KNotification is used to notify the user of an event.
Definition: knotification.h:181
KNotification::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
reimplemented for internal reasons
Definition: knotification.cpp:427
KNotification::sendEvent
void sendEvent()
Emit or re-emit the event.
Definition: knotification.cpp:357
KNotification::widget
QWidget * widget() const
the widget associated to the notification
Definition: knotification.cpp:130
KNotification::actions
QStringList actions() const
Definition: knotification.cpp:173
KNotification::setTitle
void setTitle(const QString &title)
Set the title of the notification popup.
Definition: knotification.cpp:144
KNotification::eventId
QString eventId() const
Definition: knotification.cpp:115
KNotification::KNotification
KNotification(const QString &eventId, QWidget *widget=0L, const NotificationFlags &flags=CloseOnTimeout)
Create a new notification.
Definition: knotification.cpp:82
KNotification::title
QString title() const
Definition: knotification.cpp:120
KNotification::closed
void closed()
Emitted when the notification is closed.
KNotification::setContexts
void setContexts(const ContextList &contexts)
set the list of contexts, see KNotification::Context
Definition: knotification.cpp:191
KNotification::pixmap
QPixmap pixmap() const
Definition: knotification.cpp:160
KNotification::update
void update()
Definition: knotification.cpp:422
KNotification::close
void close()
Close the notification without activating it.
Definition: knotification.cpp:246
KNotification::action3Activated
void action3Activated()
This is an overloaded member function, provided for convenience. It differs from the above function o...
KNotification::beep
static void beep(const QString &reason=QString(), QWidget *widget=0L)
This is a simple substitution for QApplication::beep()
Definition: knotification.cpp:352
KNotification::flags
NotificationFlags flags() const
Definition: knotification.cpp:206
KNotification::setFlags
void setFlags(const NotificationFlags &flags)
Set the notification flags.
Definition: knotification.cpp:211
KNotification::event
static KNotification * event(const QString &eventId, const QString &title, const QString &text, const QPixmap &pixmap=QPixmap(), QWidget *widget=0L, const NotificationFlags &flags=CloseOnTimeout, const KComponentData &componentData=KComponentData())
emit an event
Definition: knotification.cpp:291
KNotification::setPixmap
void setPixmap(const QPixmap &pix)
set the pixmap that will be shown in the popup.
Definition: knotification.cpp:165
KNotification::action2Activated
void action2Activated()
This is an overloaded member function, provided for convenience. It differs from the above function o...
KNotification::~KNotification
~KNotification()
Definition: knotification.cpp:108
KNotification::action1Activated
void action1Activated()
Convenience signal that is emitted when the first action is activated.
KNotification::ContextList
QList< Context > ContextList
Definition: knotification.h:204
KNotification::StandardEvent
StandardEvent
default events you can use in the event function
Definition: knotification.h:256
KNotification::Warning
@ Warning
Definition: knotification.h:256
KNotification::Catastrophe
@ Catastrophe
Definition: knotification.h:256
KNotification::Notification
@ Notification
Definition: knotification.h:256
KNotification::Error
@ Error
Definition: knotification.h:256
KNotification::activated
void activated()
Emit only when the default activation has occurred.
KNotification::ref
void ref()
The notification will automatically be closed if all presentations are finished.
Definition: knotification.cpp:340
KNotification::setActions
void setActions(const QStringList &actions)
Set the list of actions link shown in the popup.
Definition: knotification.cpp:178
KNotification::text
QString text() const
Definition: knotification.cpp:125
KNotification::raiseWidget
void raiseWidget()
Raise the widget.
Definition: knotification.cpp:257
KNotification::addContext
void addContext(const Context &context)
append a context at the list of contexts, see KNotificaiton::Context
Definition: knotification.cpp:196
KNotification::CloseOnTimeout
@ CloseOnTimeout
The notification will be automatically closed after a timeout.
Definition: knotification.h:219
KNotification::CloseWhenWidgetActivated
@ CloseWhenWidgetActivated
The notification will be automatically closed if the widget() becomes activated.
Definition: knotification.h:238
KNotification::DefaultEvent
@ DefaultEvent
Definition: knotification.h:247
KNotification::setWidget
void setWidget(QWidget *widget)
Set the widget associated to the notification.
Definition: knotification.cpp:135
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
KNotification::setComponentData
void setComponentData(const KComponentData &componentData)
The componentData is used to determine the location of the config file.
Definition: knotification.cpp:217
KNotification::contexts
ContextList contexts() const
Definition: knotification.cpp:186
KNotification::setText
void setText(const QString &text)
Set the notification text that will appear in the popup.
Definition: knotification.cpp:152
KNotification::deref
void deref()
remove a reference made with ref() the notification may be closed when calling this.
Definition: knotification.cpp:345
KWindowSystem::activateWindow
static void activateWindow(WId win, long time=0)
Requests that window win is activated.
Definition: kwindowsystem_mac.cpp:355
QList< Context >
QObject
QPair
QTabWidget
QWidget
kWarning
#define kWarning
kapplication.h
kconfig.h
kdebug.h
kdialog.h
kiconloader.h
timeout
int timeout
klocale.h
kmacroexpander.h
kmessagebox.h
knotification.h
kpassivepopup.h
kvbox.h
kwindowsystem.h
KGlobal::mainComponent
const KComponentData & mainComponent()
ref
void ref()
message
void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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