Thursday, June 7, 2012

Using of the xfce4-messenger-plugin

I was looking for a way to display an icon on a xfce desktop panel on some event. One possible solution is to use the xfce4-messenger-plugin. The plugin listens to messages on the D-Bus and displays them in a pop-up window or in the panel. Additionally it can display an icon if the message sent is prefixed with a identifier.
[Image]

Icons to display are added using Icons tab of the messenger's properties dialog:

[Image]

In this example I've added an icon to be shown when there is a new mail in my mailbox.

To send messages to the plugin one can use dbus-send command with the following syntax:
dbus-send --dest=org.xfce.Panel.Plugin.Messenger \
          /org/xfce/panel/plugin/messenger \
          org.xfce.Panel.Plugin.Messenger.Message \
          string:"$MESSAGE"

To display an icon D-Bus messages shall be prefixed with a identifier. It wasn't documented anywhere that a colon shall be added after the identifier to make it work but I've found it in the code.

All messages sent to the plugin are collected in a plugin's queue and are shown sequentially with a configured time out period. This wasn't exactly what I needed because my new mail icon would disappear after defined time out period and there is no possibility to set an infinite one. The solution is to add a cron job that would send a message each minute if there's a new mail whereas plugin's time out period is set to 60 seconds. This ensures that the new mail icon is always shown.

Here is a simple script that is called by cron each minute:

flux@xubuntu:~$ cat ./mailnotif.sh 
#!/bin/bash

/sbin/ifconfig wlan0 | /bin/grep 192.168.1. &>/dev/null && {
  [ "0" != "$(/usr/bin/ssh root@192.168.1.1 'cat /tmp/newMail')" ] && {
    dbus_session_file="${HOME}/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0"
    . "$dbus_session_file"
    export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
    /usr/bin/dbus-send --dest=org.xfce.Panel.Plugin.Messenger /org/xfce/panel/plugin/messenger org.xfce.Panel.Plugin.Messenger.Message string:"newmail:"
  }
}

My router is configured to check for new mail to show an icon on a DPF display. It saves a number of new mail in a file /tmp/newMail. The script checks first if I'm connected to the router and reads this file using ssh (ssh uses public key authentication and doesn't ask for a password). Additionally some D-Bus specific variables shall be exported from a D-Bus session file. Otherwise dbus-send will not work if started by the cron daemon.

2 comments:

Rowan Thorpe said...

Thanks for the great tip. By the way, in the text you mention the use of a semi-colon appended to the identifier, but - as in your code example - it uses a colon (not semi...). Just mentioning it for the benefit of anyone else who gets thrown by that.

alex said...

thanks for pointing that out. I've corrected the typo.