Adding actions to the Device Notifier widget in KDE 4.3

The default behaviour

The KDE device notifier is a widget that can be used to managed plugged-in devices like e.g. usb pen-drives. In a default Kubuntu installation this is shown as an icon in the taskbar. When clicking on it it shows e.g. all partitions of plugged in devices.

device_notifier_taskbar

When you click on a partition it offers a couple of actions that can be started for that particular partition (e.g. downloading pictures to digikam or open the drive in dolphin).

device_notifier_default

Why I needed an additional action

A task I have to do every day is to move downloaded podcast on a usb pen-drive, so I listen to the podcasts on my car radio. In the beginning I moved the files manually to the drive and then I wrote a script that did this job for me.
The Script created a directory in the format ddmmyyyy (e.g. /media/disk/05112009) on the pen-drive and moved the files in there. Afterwards it did a sync to ensure that all data had been flushed to the drive.

As I always used the device notifier to mount the drive, I wanted to have my script as an action in there.

Where do I find the actions in the filesystem?

The actions of the device notifier are placed in the directory /usr/share/kde4/apps/solid/actions. There is one file for each action.
Custom actions for a specific user can be placed in ~/.kde/share/apps/solid/.

There are a couple of paramterers that describe the action e.g.:

  • X-KDE-Solid-Predicate determines for which devices the action should be available
  • Name is the label of the action shown in the device notifier
  • Exec is the command that will be executed.

There are some variables that can be passed to the exec-command (there might be more):

  • %f gives StorageAccess.filePath
  • %d gives Block.device
  • %i gives UDI

Adding an action to device notifier

To execute my mp3 moving script I created the file /usr/share/kde4/apps/solid/actions/movemp3.desktop:

[Desktop Entry]
X-KDE-Solid-Predicate=[[ StorageVolume.ignored == false AND StorageVolume.usage == ‘FileSystem’ ] OR [ IS StorageAccess AND StorageDrive.driveType == ‘Floppy’ ]]
Type=Service
Actions=open;

[Desktop Action open]
Name=Move MP3 files
Exec=/usr/local/bin/copy_mp3_files.sh %f
Icon=system-file-manager
X-Ubuntu-Gettext-Domain=desktop_kdebase-workspace

After restarting KDE the device notifier now contains a new entry:

device_notifier_new

Formerly I started the script from the terminal, but now I start it from the gui. Therefor I added some gui interaction to the script to make it more comfortable:

device_notifier_move1

device_notifier_move2

device_notifier_move3

Just in case anyone is interested in the simple script:

path=$1
mp3path=/home/$USER/pod
datum=`date +”%d%m%y”`
newpath=$path/$datum

kdialog –yesno “Move MP3-files to drive ($newpath)?”
ret=$?

if [ “$ret” -eq “0” ]
then

kdialog –yesno “Delete old files? ( `ls $path/[0-9][0-9][0-9][0-9][0-9][0-9]` )”
if [ “$?” -eq “0” ]
then
rm -r $path/[0-9][0-9][0-9][0-9][0-9][0-9]
fi
mkdir -p $newpath
mv $mp3path/*mp3 $newpath/
sync
sync
sync
kdialog –msgbox “Files were moved, you may now disconnect the drive by device notifier!”

else
kdialog –msgbox “cancelled…”
fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: