00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "knotify.h"
00022
00023
00024 #include <kapplication.h>
00025 #include <kconfig.h>
00026 #include <kdebug.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029
00030 #include <config-runtime.h>
00031
00032 #include "knotifyconfig.h"
00033 #include "notifybysound.h"
00034 #include "notifybypopup.h"
00035 #include "notifybyexecute.h"
00036 #include "notifybylogfile.h"
00037 #include "notifybytaskbar.h"
00038 #include "notifybyktts.h"
00039
00040
00041
00042 KNotify::KNotify( QObject *parent )
00043 : QObject( parent ),
00044 m_counter(0)
00045 {
00046 loadConfig();
00047 (void)new KNotifyAdaptor(this);
00048 QDBusConnection::sessionBus().registerObject("/Notify", this, QDBusConnection::ExportAdaptors
00049 );
00050 }
00051
00052 KNotify::~KNotify()
00053 {
00054 qDeleteAll(m_notifications);
00055 }
00056
00057
00058 void KNotify::loadConfig()
00059 {
00060 qDeleteAll(m_plugins);
00061 m_plugins.clear();
00062 addPlugin(new NotifyBySound(this));
00063 addPlugin(new NotifyByPopup(this));
00064 addPlugin(new NotifyByExecute(this));
00065 addPlugin(new NotifyByLogfile(this));
00066
00067 #ifdef Q_WS_X11
00068 addPlugin(new NotifyByTaskbar(this));
00069 #endif
00070 addPlugin(new NotifyByKTTS(this));
00071 }
00072
00073 void KNotify::addPlugin( KNotifyPlugin * p )
00074 {
00075 m_plugins[p->optionName()]=p;
00076 connect(p,SIGNAL(finished( int )) , this , SLOT(slotPluginFinished( int ) ));
00077 connect(p,SIGNAL(actionInvoked( int , int )) , this , SIGNAL(notificationActivated( int , int ) ));
00078 }
00079
00080
00081
00082 void KNotify::reconfigure()
00083 {
00084 KGlobal::config()->reparseConfiguration();
00085 KNotifyConfig::clearCache();
00086 loadConfig();
00087 }
00088
00089 void KNotify::closeNotification(int id)
00090 {
00091 if(!m_notifications.contains(id))
00092 return;
00093 Event *e=m_notifications[id];
00094
00095 kDebug(300) << e->id << " ref=" << e->ref;
00096
00097
00098 m_notifications.remove(id);
00099
00100 if(e->ref>0)
00101 {
00102 e->ref++;
00103 KNotifyPlugin *plugin;
00104 foreach(plugin , m_plugins)
00105 {
00106 plugin->close( id );
00107 }
00108 }
00109 notificationClosed(id);
00110 delete e;
00111 }
00112
00113 int KNotify::event( const QString & event, const QString & appname, const ContextList & contexts, const QString & text, const QPixmap & pixmap, const QStringList & actions, WId winId )
00114 {
00115 m_counter++;
00116 Event *e=new Event(appname , contexts , event );
00117 e->id = m_counter;
00118 e->ref = 1;
00119
00120 e->config.text=text;
00121 e->config.actions=actions;
00122 e->config.pix=pixmap;
00123 e->config.winId=(WId)winId;
00124
00125 m_notifications.insert(m_counter,e);
00126 emitEvent(e);
00127
00128 e->ref--;
00129 kDebug(300) << e->id << " ref=" << e->ref;
00130 if(e->ref==0)
00131 {
00132 m_notifications.remove(e->id);
00133 delete e;
00134 return 0;
00135 }
00136 return m_counter;
00137 }
00138
00139 void KNotify::update(int id, const QString &text, const QPixmap& pixmap, const QStringList& actions)
00140 {
00141 if(!m_notifications.contains(id))
00142 return;
00143
00144 Event *e=m_notifications[id];
00145
00146 e->config.text=text;
00147 e->config.pix = pixmap;
00148 e->config.actions = actions;
00149
00150 foreach(KNotifyPlugin *p, m_plugins)
00151 {
00152 p->update(id, &e->config);
00153 }
00154 }
00155 void KNotify::reemit(int id, const ContextList& contexts)
00156 {
00157 if(!m_notifications.contains(id))
00158 return;
00159 Event *e=m_notifications[id];
00160 e->config.contexts=contexts;
00161
00162 emitEvent(e);
00163 }
00164
00165 void KNotify::emitEvent(Event *e)
00166 {
00167 QString presentstring=e->config.readEntry("Action");
00168 QStringList presents=presentstring.split ("|");
00169
00170 foreach(const QString & action , presents)
00171 {
00172 if(!m_plugins.contains(action))
00173 continue;
00174 KNotifyPlugin *p=m_plugins[action];
00175 e->ref++;
00176 p->notify(e->id,&e->config);
00177 }
00178 }
00179
00180 void KNotify::slotPluginFinished( int id )
00181 {
00182 if(!m_notifications.contains(id))
00183 return;
00184 Event *e=m_notifications[id];
00185 kDebug(300) << e->id << " ref=" << e->ref ;
00186 e->ref--;
00187 if(e->ref==0)
00188 closeNotification( id );
00189 }
00190
00191 KNotifyAdaptor::KNotifyAdaptor(QObject *parent)
00192 : QDBusAbstractAdaptor(parent)
00193 {
00194 setAutoRelaySignals(true);
00195 }
00196
00197 void KNotifyAdaptor::reconfigure()
00198 {
00199 static_cast<KNotify *>(parent())->reconfigure();
00200 }
00201
00202 void KNotifyAdaptor::closeNotification(int id)
00203 {
00204 static_cast<KNotify *>(parent())->closeNotification(id);
00205 }
00206
00207 int KNotifyAdaptor::event(const QString &event, const QString &fromApp, const QVariantList& contexts,
00208 const QString &text, const QByteArray& image, const QStringList& actions , qlonglong winId)
00209
00210
00211 {
00212
00213 ContextList contextlist;
00214 QString context_key;
00215 foreach( const QVariant &v , contexts)
00216 {
00217
00218
00219
00220
00221
00222
00223
00224
00225 QString s=v.toString();
00226 if(context_key.isEmpty())
00227 context_key=s;
00228 else
00229 contextlist << qMakePair(context_key , s);
00230 }
00231
00232 QPixmap pixmap;
00233 QDataStream in(image);
00234 in >> pixmap;
00235 return static_cast<KNotify *>(parent())->event(event, fromApp, contextlist, text, pixmap, actions, WId(winId));
00236 }
00237
00238 void KNotifyAdaptor::reemit(int id, const QVariantList& contexts)
00239 {
00240 ContextList contextlist;
00241 QString context_key;
00242 foreach( const QVariant &v , contexts)
00243 {
00244 QString s=v.toString();
00245 if(context_key.isEmpty())
00246 context_key=s;
00247 else
00248 contextlist << qMakePair(context_key , s);
00249 }
00250 static_cast<KNotify *>(parent())->reemit(id, contextlist);
00251 }
00252
00253
00254 void KNotifyAdaptor::update(int id, const QString &text, const QByteArray& image, const QStringList& actions )
00255 {
00256 QPixmap pixmap;
00257 pixmap.loadFromData(image);
00258 static_cast<KNotify *>(parent())->update(id, text, pixmap, actions);
00259 }
00260
00261 #include "knotify.moc"
00262
00263
00264
00265