|Note: This page deals with content related to KDE 3. If you are developing for KDE 4, this information might not be valid anymore.|
The ability to select mimetype-specific actions from Konqueror's context menu is an often requested feature. The pleasant surprise is that this is already possible. The even more pleasant surprise is that you don't need to be a software developer to create new actions. This article details step-by-step how to quickly and easily add new actions to Konqueror's context menu.
In KDE-speak a "servicemenu" is a special entry that appears in Konqueror's context menu depending on the file(s) that are selected. You may have seen them in action without even knowing it. For instance, if you have the KDE file archive utility "ark" installed you will see a menu entry to "Extract here..." whenever you right click on a tarball or zip file. The option to "Extract here..." is a servicemenu.
Servicemenus are defined using .desktop files, which are the same kind of files that are used to create entries in the K Menu or on the KDE desktop. These servicemenu files are found in $KDEHOME/share/apps/konqueror/servicemenus, the directory KDE was installed to or any directory listed in the $KDEDIRS environment variable.
In the case of my home machine that means that servicemenu files can be found in the following places:
/opt/kde3/share/apps/konqueror/servicemenus/ /usr/share/apps/konqueror/servicemenus/ /home/aaron/.kde3/share/apps/konqueror/servicemenus/
|$KDEHOME defaults to ~/.kde when it isn't set or defined. You can confirm this by running |
from a Konsole window. To locate where KDE was installed to, run
from a Konsole window.
We will begin creating our wallpaper servicemenu by choosing a name for the file: setAsWallpaper.desktop sounds good. The only thing that really matters with regards to the name is that it is unique and that it ends with .desktop. Next we'll open up the file in a text editor. The first thing we will put in the file is the "Desktop Entry" section:
[Desktop Entry] ServiceTypes=image/png,image/jpeg Actions=setAsWallpaper
Every servicemenu file must have these three lines. Let's examine each of these lines one at a time.
KDE configuration files, including .desktop files, seperate the individual settings into sections. A section starts with a heading made up of letters, numbers and spaces in between square brackets on a line by itself. This first line means that all the options that follow, up until the next heading, belong to the "Desktop Entry" group.
The ServiceTypes entry refers to the mimetypes that this servicemenu applies to. You can define more than one mimetype by providing a list seperated by commas (but no spaces). In this case we our servicemenu will show up when we select PNG or JPEG images. The File Associations control panel is a good place to look for mimetype definitions.
|To create a servicemenu for directories use the |
mimetype. To create a servicemenu for all files, use the special
mimetype. To create a servicemenu for all files, but not directories, use the
You can also specify an entire group of mimetypes using "typeglobs". To make our servicemenu apply not only to PNGs and JPEGs but to all images we would simply change the ServiceTypes entry to be:
Now when we right click on any image file in Konqueror we can select it as our background.
[Desktop Action setAsWallpaper]
Name=Set As Background Image
To translate the name, we add another Name entry followed by the language code. For instance, the French translation for the "Open Terminal Here" service is provided by an entry that looks like this:
Name[fr]=Ouvrir un terminal ici
Next let's add an icon:
Exec=dcop kdesktop KBackgroundIface setWallpaper %u 6
The Exec line defines what is run when the user selects the action from the menu. We can put any command we want there. The magic in this line is the "%u" which gets replaced with the URL of the image file before the command is run. If our command can accept more than one file at a time we can use "%U" instead. There are other special %values but %u and %U are probably the most useful for servicemenus.
Our file now looks like this:
[Desktop Entry] ServiceTypes=image/* Actions=setAsWallpaper [Desktop Action setAsWallpaper] Name=Use As Wallpaper Icon=background Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
|If you have a complex task that requires more than one command (for example if we wanted to copy the image file somewhere first and then use dcop to set it as the background) use a shell:|
Exec=/bin/sh -c "<YOUR COMMANDS HERE>"
Before we explore with our servicemenu a bit more, let's take a look at that exec line:
Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6
It may look complicated but it really isn't. The command takes advantage of the KDE DCOP service which is used by KDE programs to talk to each other. Fortunately for us, we can also use it for scripting.The first thing I did when formulating the Exec line was to run
Using it you can see every DCOP interface that is available for use at that moment. Browsing through the listing I found the kdesktop entry and expanded it. Under kdesktop I quickly found the KBackgroundIface, and expanding that node of the treeI spotted what I was looking for:
With setAsWallpaper.desktop open in KWrite I simply dragged and dropped the setWallpaper entry into Exec line! From there I replaced the parameters "wallpaper mode" with "%U 6".
Presto! Blamo! It worked! Now you're probably wondering where I got the number 6 from. The answer is I tried a bunch of numbers starting at 0 and working my way up until I achieved the results I wanted. I could've looked at the source code or read the developer's documentation, but I opted for the guess-and-test method instead.
Back from the land of DCOP, we have produced a working servicemenu. Now what? We improve it, of course!
Our current servicemenu scales the image to the size of the desktop and sets it as the wallpaper. But this isn't appropriate for wallpaper tiles which not be scaled but should be, well, tiled. So let's add an action for tiles. First we'll need to change the Actions line to say something like this:
Note the use of a semicolon. While the ServiceTypes are separated by commas, Actions are separated with semicolons. Quirks-R-Us. Moving right along, we'll add a new action section to the end of the file that looks something like this:
[Desktop Action tileAsWallpaper] Name=Use As Wallpaper Tile Icon=background Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
X-KDE-Submenu=Set As Background
This will create a submenu called "Set As Background" and put all of the servicemenu's actions into it. Our servicemenu .desktop file now looks like this:
[Desktop Entry] ServiceTypes=image/* Actions=setAsWallpaper;tileAsWallpaper X-KDE-Submenu=Use As Wallpaper [Desktop Action setAsWallpaper] Name=Scaled Icon=background Exec=dcop kdesktop KBackgroundIface setWallpaper %U 6 [Desktop Action tileAsWallpaper] Name=Tiled Icon=background Exec=dcop kdesktop KBackgroundIface setWallpaper %U 2
Pretty simple, huh?
Now it's time to strap on your imagination helmets: What sort of cool and useful servicemenus can you dream up? Open up a text editor and let the mayhem begin! Just don't forget to share the spoils of your adventures with the rest of us! ;-)