Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / knotify / knotify.cpp
blob851fdbd4cb3f80e2cdc0e0dae3c2a31c934c7bf4
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.
22 // KDE headers
23 #include <kapplication.h>
24 #include <kconfig.h>
25 #include <kdebug.h>
26 #include <kglobal.h>
27 #include <klocale.h>
29 #include <config-runtime.h>
31 #include "knotify.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 loadConfig();
88 void KNotify::closeNotification(int id)
90 if(!m_notifications.contains(id))
91 return;
92 Event *e=m_notifications[id];
94 kDebug(300) << e->id << " ref=" << e->ref;
96 //this has to be called before plugin->close or we will get double deletion because of slotPluginFinished
97 m_notifications.remove(id);
99 if(e->ref>0)
101 e->ref++;
102 KNotifyPlugin *plugin;
103 foreach(plugin , m_plugins)
105 plugin->close( id );
108 notificationClosed(id);
109 delete e;
112 int KNotify::event( const QString & event, const QString & appname, const ContextList & contexts, const QString & text, const QPixmap & pixmap, const QStringList & actions, WId winId )
114 m_counter++;
115 Event *e=new Event(appname , contexts , event );
116 e->id = m_counter;
117 e->ref = 1;
119 e->config.text=text;
120 e->config.actions=actions;
121 e->config.pix=pixmap;
122 e->config.winId=(WId)winId;
124 m_notifications.insert(m_counter,e);
125 emitEvent(e);
127 e->ref--;
128 kDebug(300) << e->id << " ref=" << e->ref;
129 if(e->ref==0)
131 m_notifications.remove(e->id);
132 delete e;
133 return 0;
135 return m_counter;
138 void KNotify::update(int id, const QString &text, const QPixmap& pixmap, const QStringList& actions)
140 if(!m_notifications.contains(id))
141 return;
143 Event *e=m_notifications[id];
145 e->config.text=text;
146 e->config.pix = pixmap;
147 e->config.actions = actions;
149 foreach(KNotifyPlugin *p, m_plugins)
151 p->update(id, &e->config);
154 void KNotify::reemit(int id, const ContextList& contexts)
156 if(!m_notifications.contains(id))
157 return;
158 Event *e=m_notifications[id];
159 e->config.contexts=contexts;
161 emitEvent(e);
164 void KNotify::emitEvent(Event *e)
166 QString presentstring=e->config.readEntry("Action");
167 QStringList presents=presentstring.split ("|");
169 foreach(const QString & action , presents)
171 if(!m_plugins.contains(action))
172 continue;
173 KNotifyPlugin *p=m_plugins[action];
174 e->ref++;
175 p->notify(e->id,&e->config);
179 void KNotify::slotPluginFinished( int id )
181 if(!m_notifications.contains(id))
182 return;
183 Event *e=m_notifications[id];
184 kDebug(300) << e->id << " ref=" << e->ref ;
185 e->ref--;
186 if(e->ref==0)
187 closeNotification( id );
190 KNotifyAdaptor::KNotifyAdaptor(QObject *parent)
191 : QDBusAbstractAdaptor(parent)
193 setAutoRelaySignals(true);
196 void KNotifyAdaptor::reconfigure()
198 static_cast<KNotify *>(parent())->reconfigure();
201 void KNotifyAdaptor::closeNotification(int id)
203 static_cast<KNotify *>(parent())->closeNotification(id);
206 int KNotifyAdaptor::event(const QString &event, const QString &fromApp, const QVariantList& contexts,
207 const QString &text, const QByteArray& image, const QStringList& actions , qlonglong winId)
208 // const QDBusMessage & , int _return )
211 /* I'm not sure this is the right way to read a a(ss) type, but it seems to work */
212 ContextList contextlist;
213 QString context_key;
214 foreach( QVariant v , contexts)
216 /* this code doesn't work
217 QVariantList vl=v.toList();
218 if(vl.count() != 2)
220 kWarning(300) << "Bad structure passed as argument" ;
221 continue;
223 contextlist << qMakePair(vl[0].toString() , vl[1].toString());*/
224 QString s=v.toString();
225 if(context_key.isEmpty())
226 context_key=s;
227 else
228 contextlist << qMakePair(context_key , s);
231 QPixmap pixmap;
232 QDataStream in(image);
233 in >> pixmap;
234 return static_cast<KNotify *>(parent())->event(event, fromApp, contextlist, text, pixmap, actions, WId(winId));
237 void KNotifyAdaptor::reemit(int id, const QVariantList& contexts)
239 ContextList contextlist;
240 QString context_key;
241 foreach( QVariant v , contexts)
243 QString s=v.toString();
244 if(context_key.isEmpty())
245 context_key=s;
246 else
247 contextlist << qMakePair(context_key , s);
249 static_cast<KNotify *>(parent())->reemit(id, contextlist);
253 void KNotifyAdaptor::update(int id, const QString &text, const QByteArray& image, const QStringList& actions )
255 QPixmap pixmap;
256 pixmap.loadFromData(image);
257 static_cast<KNotify *>(parent())->update(id, text, pixmap, actions);
260 #include "knotify.moc"
262 // vim: sw=4 sts=4 ts=8 et