Activity tutorial

From OLPC
Revision as of 16:41, 7 August 2006 by Blizzard (talk | contribs) (add the sugar category)
Jump to: navigation, search
  This page is monitored by the OLPC team.

Here is a short tutorial about writing a sugar activity. It's a step by step guide to get you started quickly, without going in details of the activity system. Download the example sources

Write the build system

Create the autogen.sh script:

#!/bin/sh
# Run this to generate all the initial makefiles, etc.

srcdir=`dirname $0`
test -z "$srcdir" && srcdir=.

PKG_NAME="sugar-drawing"

(test -f $srcdir/drawing.activity) || {
    echo -n "**Error**: Directory "\`$srcdir\'" does not look like the"
    echo " top-level $PKG_NAME directory"
    exit 1
}

which gnome-autogen.sh || {
    echo "You need to install gnome-common from the GNOME CVS"
    exit 1
}

REQUIRED_AUTOMAKE_VERSION=1.9 USE_GNOME2_MACROS=1 . gnome-autogen.sh

Create a configure.ac file:

AC_INIT([Sugar Drawing],[0.1],[],[sugar-drawing])
AM_INIT_AUTOMAKE

AM_PATH_PYTHON

AC_OUTPUT([
Makefile
])

Automake check for the existence of a few required files, you have to create them even if empty:

touch NEWS README AUTHORS ChangeLog

Write the activity Makefile.am:

activitydir = $(datadir)/sugar/activities/drawing
activity_PYTHON =               \
        __init__.py             \
        DrawingActivity.py

EXTRA_DIST = drawing.activity

install-data-local:
        sugar-setup-activity $(srcdir)/drawing.activity

Write the activity code

Write a subclass of sugar.activity.Activity. It's a GtkWindow so you can use the "add" method to insert your own widgets. The source of DrawingActivity.py demonstrates it:

import gtk

from sugar.activity.Activity import Activity

class DrawingActivity(Activity):
        def __init__(self, service, args):
                Activity.__init__(self, service)

                button = gtk.Button('Drawing')
                self.add(button)
                button.show()

Create the package initialization file:

touch __init__.py

Write drawing.activity with the necessary informations about the activity:

[Activity]
name = Drawing
id = org.laptop.sugar.Drawing
python_module = drawing.DrawingActivity.DrawingActivity
default_type = _drawing_olpc._udp
show_launcher = yes

Build and install

Initialize the build system. The value of prefix depends on the path of sugar-jhbuild:

./autogen.sh --prefix=[SUGAR-JHBUILD]/build

Build and install:

make
make install