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

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

−

<code ini>

+

<syntaxhighlight lang="ini">

[Desktop Entry]

[Desktop Entry]

Encoding=UTF-8

Encoding=UTF-8

Revision as of 20:37, 29 June 2011

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-8Name=Hello Web
Name[nl]=Hallo WebType=ServiceServiceTypes=Plasma/AppletIcon=chronometerX-Plasma-API=webkitX-Plasma-MainScript=code/main.htmlX-KDE-PluginInfo-Author=Patrick AljordX-KDE-PluginInfo-Email=patcito@gmail.comX-KDE-PluginInfo-Name=hello-webX-KDE-PluginInfo-Version=1.0X-KDE-PluginInfo-Website=http://plasma.kde.org/X-KDE-PluginInfo-Category=ExamplesX-KDE-PluginInfo-Depends=X-KDE-PluginInfo-License=GPLX-KDE-PluginInfo-EnabledByDefault=true
</code>
This <tt>metadata.desktop</tt> file list important information needed by Plasma to load the applet, and also information about what the applet is and who created it. The <tt>Name</tt> 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 <tt>[nl]</tt> indicates the language of the field.
The <tt>Type</tt> and <tt>ServiceType</tt> fields identify what kind of thing this desktop file is describing. Here we say that we have a plasma applet.
The <tt>Icon</tt> 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 <tt>X-Plasma-API</tt> and <tt>X-Plasma-MainScript</tt> fields are used specifically by Plasma to find the correct script-engine to use when loading and running the applet.
Most of the <tt>X-KDE-PluginInfo-*</tt> fields give extra information about who created the applet. <tt>X-KDE-PluginInfo-Category</tt> lists a category which this applet belongs to. [[Projects/Plasma/PIG|The Plasma Inteface Guidelines]] contains a list of acceptable category names.
== Main script ==
Create a file in the <tt>contents/code</tt> directory called <tt>main.html</tt>, open it up in your text editor and put this code in it and save it.
<code html4strict>
<!-- <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>
</code>
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 [http://html5demos.com/ 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 <tt>contents/code</tt> 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:
<code>
file:///home/pat/plasmoids/hello-web/contents/code/main.html
</code>
Plasma applets can be packaged in zip files and installed using the <tt>plasmapkg</tt> 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.
<code bash>
zip -r ../hello-web.zip .
</code>
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 <tt>plasmapkg</tt> command to install our little hello-web applet.
<code bash>
plasmapkg -i hello-web.zip
</code>
This installs the applet into your home directory. Now we can run it. When developing applets it is more convenient to use the <tt>plasmoidviewer</tt>. 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.
<code bash>
plasmoidviewer hello-web
</code>
You should now be able to see the fruits of your labor.
To uninstall our applet we use <tt>plasmapkg</tt> again with its -r option.
<code bash>
plasmapkg -r hello-web
</code>
== 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:
<code>
This object could not be created for the following reason:
Could not create a web-script ScriptEngine for the Hello Web widget.
</code>
Then make sure that all "webkit kde" depdendencies are installed. In ubuntu for example you should have the next package:
<code>
plasma-scriptengine-webkit
</code>
For additional help see [http://techbase.kde.org/Development/Tutorials/Plasma/Python/GettingStarted#Troubleshooting here]