delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / knotify / knotify.cpp
blob1b20b7dbeb7ef8b9a829967baa11742b6c8802b2
1 /*
2 Copyright (C) 2005-2006 by Olivier Goffart <ogoffart at kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "knotify.h"
23 // KDE headers
24 #include <kapplication.h>
25 #include <kconfig.h>
26 #include <kdebug.h>
27 #include <kglobal.h>
28 #include <klocale.h>
30 #include <config-runtime.h>
32 #include "knotifyconfig.h"
33 #include "notifybysound.h"
34 #include "notifybypopup.h"
35 #include "notifybyexecute.h"
36 #include "notifybylogfile.h"
37 #include "notifybytaskbar.h"
38 #include "notifybyktts.h"
42 KNotify::KNotify( QObject *parent )
43 : QObject( parent ),
44 m_counter(0)
46 loadConfig();
47 (void)new KNotifyAdaptor(this);
48 QDBusConnection::sessionBus().registerObject("/Notify", this, QDBusConnection::ExportAdaptors
49 /*| QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals*/ );
52 KNotify::~KNotify()
54 qDeleteAll(m_notifications);
58 void KNotify::loadConfig()
60 qDeleteAll(m_plugins);
61 m_plugins.clear();
62 addPlugin(new NotifyBySound(this));
63 addPlugin(new NotifyByPopup(this));
64 addPlugin(new NotifyByExecute(this));
65 addPlugin(new NotifyByLogfile(this));
66 //TODO reactivate on Mac/Win when KWindowSystem::demandAttention will implemented on this system.
67 #ifdef Q_WS_X11
68 addPlugin(new NotifyByTaskbar(this));
69 #endif
70 addPlugin(new NotifyByKTTS(this));
73 void KNotify::addPlugin( KNotifyPlugin * p )
75 m_plugins[p->optionName()]=p;
76 connect(p,SIGNAL(finished( int )) , this , SLOT(slotPluginFinished( int ) ));
77 connect(p,SIGNAL(actionInvoked( int , int )) , this , SIGNAL(notificationActivated( int , int ) ));
82 void KNotify::reconfigure()
84 KGlobal::config()->reparseConfiguration();
85 KNotifyConfig::clearCache();
86 loadConfig();
89 void KNotify::closeNotification(int id)
91 if(!m_notifications.contains(id))
92 return;
93 Event *e=m_notifications[id];
95 kDebug(300) << e->id << " ref=" << e->ref;
97 //this has to be called before plugin->close or we will get double deletion because of slotPluginFinished
98 m_notifications.remove(id);
100 if(e->ref>0)
102 e->ref++;
103 KNotifyPlugin *plugin;
104 foreach(plugin , m_plugins)
106 plugin->close( id );
109 notificationClosed(id);
110 delete e;
113 int KNotify::event( const QString & event, const QString & appname, const ContextList & contexts, const QString & text, const QPixmap & pixmap, const QStringList & actions, WId winId )
115 m_counter++;
116 Event *e=new Event(appname , contexts , event );
117 e->id = m_counter;
118 e->ref = 1;
120 e->config.text=text;
121 e->config.actions=actions;
122 e->config.pix=pixmap;
123 e->config.winId=(WId)winId;
125 m_notifications.insert(m_counter,e);
126 emitEvent(e);
128 e->ref--;
129 kDebug(300) << e->id << " ref=" << e->ref;
130 if(e->ref==0)
132 m_notifications.remove(e->id);
133 delete e;
134 return 0;
136 return m_counter;
139 void KNotify::update(int id, const QString &text, const QPixmap& pixmap, const QStringList& actions)
141 if(!m_notifications.contains(id))
142 return;
144 Event *e=m_notifications[id];
146 e->config.text=text;
147 e->config.pix = pixmap;
148 e->config.actions = actions;
150 foreach(KNotifyPlugin *p, m_plugins)
152 p->update(id, &e->config);
155 void KNotify::reemit(int id, const ContextList& contexts)
157 if(!m_notifications.contains(id))
158 return;
159 Event *e=m_notifications[id];
160 e->config.contexts=contexts;
162 emitEvent(e);
165 void KNotify::emitEvent(Event *e)
167 QString presentstring=e->config.readEntry("Action");
168 QStringList presents=presentstring.split ("|");
170 foreach(const QString & action , presents)
172 if(!m_plugins.contains(action))
173 continue;
174 KNotifyPlugin *p=m_plugins[action];
175 e->ref++;
176 p->notify(e->id,&e->config);
180 void KNotify::slotPluginFinished( int id )
182 if(!m_notifications.contains(id))
183 return;
184 Event *e=m_notifications[id];
185 kDebug(300) << e->id << " ref=" << e->ref ;
186 e->ref--;
187 if(e->ref==0)
188 closeNotification( id );
191 KNotifyAdaptor::KNotifyAdaptor(QObject *parent)
192 : QDBusAbstractAdaptor(parent)
194 setAutoRelaySignals(true);
197 void KNotifyAdaptor::reconfigure()
199 static_cast<KNotify *>(parent())->reconfigure();
202 void KNotifyAdaptor::closeNotification(int id)
204 static_cast<KNotify *>(parent())->closeNotification(id);
207 int KNotifyAdaptor::event(const QString &event, const QString &fromApp, const QVariantList& contexts,
208 const QString &text, const QByteArray& image, const QStringList& actions , qlonglong winId)
209 // const QDBusMessage & , int _return )
212 /* I'm not sure this is the right way to read a a(ss) type, but it seems to work */
213 ContextList contextlist;
214 QString context_key;
215 foreach( const QVariant &v , contexts)
217 /* this code doesn't work
218 QVariantList vl=v.toList();
219 if(vl.count() != 2)
221 kWarning(300) << "Bad structure passed as argument" ;
222 continue;
224 contextlist << qMakePair(vl[0].toString() , vl[1].toString());*/
225 QString s=v.toString();
226 if(context_key.isEmpty())
227 context_key=s;
228 else
229 contextlist << qMakePair(context_key , s);
232 QPixmap pixmap;
233 QDataStream in(image);
234 in >> pixmap;
235 return static_cast<KNotify *>(parent())->event(event, fromApp, contextlist, text, pixmap, actions, WId(winId));
238 void KNotifyAdaptor::reemit(int id, const QVariantList& contexts)
240 ContextList contextlist;
241 QString context_key;
242 foreach( const QVariant &v , contexts)
244 QString s=v.toString();
245 if(context_key.isEmpty())
246 context_key=s;
247 else
248 contextlist << qMakePair(context_key , s);
250 static_cast<KNotify *>(parent())->reemit(id, contextlist);
254 void KNotifyAdaptor::update(int id, const QString &text, const QByteArray& image, const QStringList& actions )
256 QPixmap pixmap;
257 pixmap.loadFromData(image);
258 static_cast<KNotify *>(parent())->update(id, text, pixmap, actions);
261 #include "knotify.moc"
263 // vim: sw=4 sts=4 ts=8 et