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)
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.
23 #include <kapplication.h>
29 #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
)
47 (void)new KNotifyAdaptor(this);
48 QDBusConnection::sessionBus().registerObject("/Notify", this, QDBusConnection::ExportAdaptors
49 /*| QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals*/ );
54 qDeleteAll(m_notifications
);
58 void KNotify::loadConfig()
60 qDeleteAll(m_plugins
);
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.
68 addPlugin(new NotifyByTaskbar(this));
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();
88 void KNotify::closeNotification(int id
)
90 if(!m_notifications
.contains(id
))
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
);
102 KNotifyPlugin
*plugin
;
103 foreach(plugin
, m_plugins
)
108 notificationClosed(id
);
112 int KNotify::event( const QString
& event
, const QString
& appname
, const ContextList
& contexts
, const QString
& text
, const QPixmap
& pixmap
, const QStringList
& actions
, WId winId
)
115 Event
*e
=new Event(appname
, contexts
, event
);
120 e
->config
.actions
=actions
;
121 e
->config
.pix
=pixmap
;
122 e
->config
.winId
=(WId
)winId
;
124 m_notifications
.insert(m_counter
,e
);
128 kDebug(300) << e
->id
<< " ref=" << e
->ref
;
131 m_notifications
.remove(e
->id
);
138 void KNotify::update(int id
, const QString
&text
, const QPixmap
& pixmap
, const QStringList
& actions
)
140 if(!m_notifications
.contains(id
))
143 Event
*e
=m_notifications
[id
];
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
))
158 Event
*e
=m_notifications
[id
];
159 e
->config
.contexts
=contexts
;
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
))
173 KNotifyPlugin
*p
=m_plugins
[action
];
175 p
->notify(e
->id
,&e
->config
);
179 void KNotify::slotPluginFinished( int id
)
181 if(!m_notifications
.contains(id
))
183 Event
*e
=m_notifications
[id
];
184 kDebug(300) << e
->id
<< " ref=" << e
->ref
;
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
;
214 foreach( QVariant v
, contexts
)
216 /* this code doesn't work
217 QVariantList vl=v.toList();
220 kWarning(300) << "Bad structure passed as argument" ;
223 contextlist << qMakePair(vl[0].toString() , vl[1].toString());*/
224 QString s
=v
.toString();
225 if(context_key
.isEmpty())
228 contextlist
<< qMakePair(context_key
, s
);
232 QDataStream
in(image
);
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
;
241 foreach( QVariant v
, contexts
)
243 QString s
=v
.toString();
244 if(context_key
.isEmpty())
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
)
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