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.
24 #include <kapplication.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
)
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();
85 KNotifyConfig::clearCache();
89 void KNotify::closeNotification(int id
)
91 if(!m_notifications
.contains(id
))
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
);
103 KNotifyPlugin
*plugin
;
104 foreach(plugin
, m_plugins
)
109 notificationClosed(id
);
113 int KNotify::event( const QString
& event
, const QString
& appname
, const ContextList
& contexts
, const QString
& text
, const QPixmap
& pixmap
, const QStringList
& actions
, WId winId
)
116 Event
*e
=new Event(appname
, contexts
, event
);
121 e
->config
.actions
=actions
;
122 e
->config
.pix
=pixmap
;
123 e
->config
.winId
=(WId
)winId
;
125 m_notifications
.insert(m_counter
,e
);
129 kDebug(300) << e
->id
<< " ref=" << e
->ref
;
132 m_notifications
.remove(e
->id
);
139 void KNotify::update(int id
, const QString
&text
, const QPixmap
& pixmap
, const QStringList
& actions
)
141 if(!m_notifications
.contains(id
))
144 Event
*e
=m_notifications
[id
];
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
))
159 Event
*e
=m_notifications
[id
];
160 e
->config
.contexts
=contexts
;
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
))
174 KNotifyPlugin
*p
=m_plugins
[action
];
176 p
->notify(e
->id
,&e
->config
);
180 void KNotify::slotPluginFinished( int id
)
182 if(!m_notifications
.contains(id
))
184 Event
*e
=m_notifications
[id
];
185 kDebug(300) << e
->id
<< " ref=" << e
->ref
;
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
;
215 foreach( const QVariant
&v
, contexts
)
217 /* this code doesn't work
218 QVariantList vl=v.toList();
221 kWarning(300) << "Bad structure passed as argument" ;
224 contextlist << qMakePair(vl[0].toString() , vl[1].toString());*/
225 QString s
=v
.toString();
226 if(context_key
.isEmpty())
229 contextlist
<< qMakePair(context_key
, s
);
233 QDataStream
in(image
);
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
;
242 foreach( const QVariant
&v
, contexts
)
244 QString s
=v
.toString();
245 if(context_key
.isEmpty())
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
)
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