Friday, February 10, 2012

Creating KDE's Dolphin services and integrating AES Crypt into it

The main objective of this post is to give you a quick glance at how you can create service menus and plugins for use under Dolphin, the default file manager in KDE.

In order to achieve the goal, you need to know what service you want to create for Dolphin. That's where I present you AES Crypt, a nice simple application that allows you to encrypt/decrypt your files in an easy and cross-platform way. Unfortunately, the linux version lacks some features, like UI integration (for obvious reasons), and it's up to us users to make something about it.



As such, I will show you how to integrate AES Crypt with one specific file manager you can use on Linux, KDE's Dolphin, as I mentioned before. That way, you'll be able to quickly call AES Crypt inside Dolphin, just like the Windows version, no need to pop out the terminal and type aescrypt -e or aescrypt -d everytime.

Dolphin Preferences, showing the AES service installed.


Let's start then:

Create the service file:

The whole service goes into just one file, and that is the one you will be creating. Create an empty text file and save it with the .desktop extension. The full name I used was aes.desktop.


Define the basic structure:

Like in any other .desktop file, you must define the desktop entry section. You start it with:

[Desktop Entry]

Inside this section, you will put the pairs of keys and values that will tell Dolphin what this .desktop file is all about. However, let's postpone that task for now.


Below the desktop entry line, add the following two lines, which define desktop actions:

[Desktop Action encrypt]

[Desktop Action decrypt]

The first line will tell Dolphin what to do when you ask it to encrypt a file, the second one will tell it what to do when you want to decrypt a file. You are free to name the actions as you wish. I named them encrypt and decrypt.

The basic structure is done. A desktop entry section and one desktop action section for each atomic thing you want to do. Now you just need to populate these sections with usefulness.

Populate the desktop entry:

Inside this section you will put the pairs of keys and values that will make the file recognizable as a Dolphin service as well as what actions does it offer. The way I'll show you will create an option with a submenu. The submenu will then let you encrypt or decrypt the file you have selected under Dolphin.

So, add the following lines:

Type=Service
This line tells that the desktop file describes a service.

X-KDE-ServiceTypes=KonqPopupMenu/Plugin
This one will make Dolphin know that it must show an option on the context menu with our service.

MimeType=all/all
We want the context menu to appear in all kinds of files, so we set the mime type to everything (all/all).

Icon=kgpg
What icon should our desktop file have. More importantly, what icon will appear in our option of the context menu. I used the kgpg icon that requires having kgpg installed.

Actions=encrypt;decrypt;
This key is crucial because it lists all the actions supported (semicolon separated). The names you gave to the desktop actions before must be the same as the ones you list in this line.

X-KDE-Submenu=AES
Finally, what should be the name of our context menu option? I named it just "AES".


Populate the desktop actions:

For each action you define, you will give a name, an icon (to show in the submenu option) and a command to execute (for calling AES Crypt). So, put the following pairs of keys next to the beginning of encrypt desktop action:

Name=Encrypt
Icon=kgpg
Here I call the encrypt action "Encrypt" and set the kgpg icon, just like the main context menu option.

Exec=aescrypt -e -p `kdialog --password "Insert desired password:" --title "AES Encryption"` "%f"
The key Exec is where all the magic happens. When the user selects "Encrypt" in the context menu submenu, the above command will automatically run. What it does is call AES Crypt on the file currently selected ("%f") and encrypt it with a password provided by KDialog. Using KDialog, you can quickly create simple window dialogs to show or input information. The KDialog I used will ask the user for a password, as shown in the picture below:


Now, for the second action (decrypt), it's pretty much the same:
Name=Decrypt
Icon=kgpg
Exec=aescrypt -d -p `kdialog --password "Provide required password:" --title "AES Decryption"` "%f"

Install the plugin:

Now that you have a full service written, it's time for Dolphin to make use of it. To accomplish that, you can either copy the file to ~/.kde4/share/kde4/services/ServiceMenus/ directory (create it if it doesn't exist) or to /usr/share/kde4/services/ServiceMenus/ (create it if it doesn't exist), assuming a typical installation of GNU/Linux. Remember that you should avoid messing with the global directories (like /usr) to maintain your system consistent with your package manager and to avoid sad lucks, so I recommend putting the file in your home. By the way, the tilde (~) represents you home directory.

Give it a try:

Assuming that you have AES Crypt installed as well as KDE, you should now be able to fire up Dolphin, select a file, right click on it, select AES and finally select Encrypt. A KDialog will appear, input your password, and a new file will be created with the name of the non-encrypted plus the .aes extension. Check the contents of the file, you can't understand a thing! It's encrypted. Now go to the AES submenu again and select Decrypt. You will be asked for the password, type it and you will recover the file.


As you can see, creating a service plugin for Dolphin is pretty straightforward and can help ease and fasten some tasks that would be boring otherwise, especially daily ones.

Give AES Crypt a try as it works very flawlessly and is compatible both with Windows, Linux and Mac OS. You can easily secure and move your files from system to system (oh Android and iOS...).



You can download the whole service file: Here.


A few useful links:
AES Crypt
KonqPopupMenu
KDE Dialogs

Atualização em 2013/05/12:
Uma versão deste artigo em português pode agora ser consultada no website do Grupo de Linux da Universidade de Aveiro.