Development/Tutorials/Plasma4/Web/GettingStarted

From KDE TechBase

This is a 'translation' of the Getting Started article on Python and Plasma. For the Python Tutorial see here.

Abstract

In this tutorial we'll cover setting up a very simple plasma applet using web technologies only such as HTML, Javascript and CSS, and how to install it and run it, basically the edit, install, run and debug cycle. To follow this you will need to have KDE 4.3 or later installed, and also the webkit support for plasma and related dependencies. I'm going to assume that you are using a unix-style operating system such as Linux of *BSD and aren't adverse to the shell.

Project Structure

The first step for any software project is to set up your project's directory on disk. For this tutorial we are going to create a very simple "hello-web" applet. Create a directory on somewhere called hello-web which in turn contains a contents directory which then contains a code directory. This command below will do all this.

mkdir -p hello-web/contents/code

This creates a simple directory structure which also matches the package structure expected by the plasmapkg command. More on this later.

metadata.desktop

In the hello-web directory create a file called metadata.desktop and open it in your text editor. Copy the following code into hello-web/metadata.desktop.

[Desktop Entry]
Encoding=UTF-8
Name=Hello Web
Name[nl]=Hallo Web
Type=Service
ServiceTypes=Plasma/Applet
Icon=chronometer
X-Plasma-API=webkit
X-Plasma-MainScript=code/main.html
X-KDE-PluginInfo-Author=Patrick Aljord
X-KDE-PluginInfo-Email=[email protected]
X-KDE-PluginInfo-Name=hello-web
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Examples
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

This metadata.desktop file list important information needed by Plasma to load the applet, and also information about what the applet is and who created it. The Name field lists the name of the applet. This field can also be listed multiple times as different translations. Here I've just given a Dutch translation of the name. The [nl] indicates the language of the field.

The Type and ServiceType fields identify what kind of thing this desktop file is describing. Here we say that we have a plasma applet.

The Icon field gives the name of the icon to associate with this applet. The icon is typically shown in things such as the "Add widget" dialog in Plasma.

The X-Plasma-API and X-Plasma-MainScript fields are used specifically by Plasma to find the correct script-engine to use when loading and running the applet.

Most of the X-KDE-PluginInfo-* fields give extra information about who created the applet. X-KDE-PluginInfo-Category lists a category which this applet belongs to. The Plasma Inteface Guidelines contains a list of acceptable category names.

Main script

Create a file in the contents/code directory called main.html, open it up in your text editor and put this code in it and save it.

<!-- <Copyright and license information goes here.> -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset=utf-8 />
    <meta name="viewport" content="width=620" />
    <title>Hello World</title>
  </head>
  <body>
    Hello World!
  </body>
</html>

As you may have noticed, I'm using HTML5 here. That's because WebKit supports it and that will allow you to add great features to your plasmoid such as SVG, canvas, geolocation, sql web storage and more, see here for examples. Feel free to use HTML4 though.

That's all there is to it. You can of course add javascript, css, svg, images or whatever have you to the contents/code directory and include them in main.html as you would for any web page.

Packaging, installing & running

Unlike other plasmoids, you can test your plasmoid without packaging it, just point your browser to the main.html file and test from there, such as:

file:///home/pat/plasmoids/hello-web/contents/code/main.html

Plasma applets can be packaged in zip files and installed using the plasmapkg command line tool. The directory structure which we have used for our project matches that need in the zip file. All we have to do is zip it update. Run the following command from inside the hello-web directory.

zip -r ../hello-web.zip .

This will create the hello-web.zip file in the directory just above the hello-web directory. Go to this directory in the shell and run this plasmapkg command to install our little hello-web applet.

plasmapkg -i hello-web.zip

This installs the applet into your home directory. Now we can run it. When developing applets it is more convenient to use the plasmoidviewer. This is a little utility which displays an applet in a window instead of you having to use your desktop. This command below will run our applet.

plasmoidviewer hello-web

You should now be able to see the fruits of your labor.

To uninstall our applet we use plasmapkg again with its -r option.

plasmapkg -r hello-web

Conclusion

This tutorial covers very basics of developing Plasma applets using web technologies. Most of the material discussed here is boring but important "boilerplate" stuff which stays mostly the same between every applet project.

Troubleshooting

If you have this error message instead of plasmoid:

This object could not be created for the following reason:
   Could not create a web-script ScriptEngine for the Hello Web widget.

Then make sure that all "webkit kde" depdendencies are installed. In ubuntu for example you should have the next package:

plasma-scriptengine-webkit

For additional help see here