Notifications in KDE4 with KNotify
mercredi 30 août 2006 à 14:33 :: #18 :: rss
In KDE3, KNotify was already a very powerfull tool used for desktop notification. From the developer point of view this is done in few lines of code. And the advanced user may do almost everything.
Anyway there was still few limitations: The application had no control over the notification once it is fired. No way to add link on the popup or having persistant notification. In Kopete, we had to fork knotify and do some hack in order to add the "chat" link in the popup.
That's why, for KDE4 , I rewritten KNotify almost entirely. It has already more feature than the previous one. I did not anyway redesigned the UI yet. The popup is still as ugly as before, and the configure dialog as complex. But this will probably be improved later.
I've decided to blog about it in order to let developper know about possible stuff in their application.
Base
The basic stuff did not really change from KDE3. The event description file has just changed a bit for readability. I've let the event() function for convenience, although there is one more readable API using several function, as it seems the policy in KDE4 API.
I've made some examples to show the difference and how to port.
Actions
So it is now possible to add one or several action on the notification.
Example, when a contact goes on-line, the user may want to chat with it. When we receive a message, of course we want to read it.
The way to do this is pretty simple, look this example inspired from Kopete.
KNotification *notify=new KNotification("new_message");
notify->setText(i18n("<qt>You received a new message from <em>%1</em></qt>") , message->from() );
notify->setActions( i18n("View,Ignore").split(',') );
connect(notify,SIGNAL(action1Activated()), message , SLOT(read()) );
connect(notify,SIGNAL(action2Activated()), message , SLOT(ignore()) );
notify->sendEvent();
Persistant Notifications
Another novelty is the possibility to have persistant popup, which only get closed when the notification is finished.
There are indeed two kind of notifications, those that are just a single shot event (typically, when a contact goes on-line, or a window get minimised) and more persistant notification which are valid until the user performs a specific action. Example, the new email notification may stay alive until the email has been read.
Again, that's really simple. just pass the KNotification::Persistant flag in the constructor, and don't forget to call notification->close() when the notification is finished. So you need to keep a pointer to the KNotification object.
It's also possible, if you keep the pointer, to modify the text of the popup, or re-emit the notification (eg. If new mails has been received we can use the same popup)
Contexts
Contexts is a novelty I'm proud about. It allows to the user to configure differently the notification depending some contexts. For example, in Kopete, this will be used to let choose another sound for a particular contact or group of contact. [1]
You first have to define some contexts name. In the case of Kopete, they are contact and group. [2]
And before emitting the notification you just need to add context to it
notification->addContext( "contact" , message->from()->contactId() );
foreach(QString group , message->from()->groups())
notification->addContext( "group" , group );
You have to add a Context key in the description file in order to let KNotify know which event are suitable for the given context.
So configuration of notifications for a context may be done very easily also.
In the property dialog, add a KNotifyConfigWidget using the following code
notifyWidget = new KNotifyConfigWidget( parent ); notifyWidget->setApplication(QString::null , "contact" , contact->contactId() );
You can see the result on this screenshot of the meta-contact property dialog . I know this looks pretty ugly, but remember it's just a technology preview, and I have not worked on the UI now.

One of my plan is to make this also configurable from the global configure notifications panel, although that has not been coded yet.
Future
With this powerfull API, almost everything is possible. I've also constructed the daemon in a way that would eventually allow plugins later. But that API is not finished
I have also planed to magically handle the widget, by automatically raising a given widget, or closing the notification if the widget got the focus. Anyway, I don't know if the benefit is worth and I will maybe remove that API.
So I still need to do a great configuration dialog that present all options in a usable way. Hint from usability people are welcome.
And lot of cool possible stuff may be added:
- replay the sound which tell you that we have a new mail every minutes until the mail has been read;
- to specify if the popup has to stay or to be closed after a timeout;
- to make blink a LED (eg. the scroll lock one);
- have different beautiful kind of popup or balloon
- ...
Of course, it need to comes with good defaults.
I'm listening to wish or hints.







Commentaires
1. Commentaire de FrankOsterfeld
Le jeudi 31 août 2006 à 03:27
2. Commentaire de Version4
Le jeudi 31 août 2006 à 04:45
3. Commentaire de Thomas Beinicke
Le jeudi 31 août 2006 à 06:12
4. Commentaire de Lubos Lunak
Le jeudi 31 août 2006 à 07:55
5. Commentaire de Gof
Le jeudi 31 août 2006 à 09:50
6. Commentaire de Michal
Le jeudi 31 août 2006 à 14:30
7. Commentaire de Stefan
Le jeudi 31 août 2006 à 16:28
8. Commentaire de chat-loupe
Le jeudi 31 août 2006 à 17:50
9. Commentaire de FACORAT Fabrice
Le jeudi 31 août 2006 à 20:11
10. Commentaire de Fred
Le vendredi 1 septembre 2006 à 12:22
11. Commentaire de S.F.
Le samedi 2 septembre 2006 à 12:05
12. Commentaire de Antonio
Le mardi 5 septembre 2006 à 19:19
Ajouter un commentaire
Les commentaires pour ce billet sont fermés.