Freevo

- User Documentation

# DevelopPlugins

Site Index:

1. Intro
2. Item plugins
3. Idlebar plugins

## Intro

It is quite simple to write your own plugins. Have a look at the code example below. If that didn't scare you away... try looking at some of the code in `src/plugins or src/(video|audio|image|games)/plugins` and before you blow it you are writing the next fabulous Freevo plugin

## Item plugins

Item plugins give you the possibility to define actions which should be performed on items of a special kind. Those actions can then be accessed from within the item context menu (pressing ENTER not SELECT on the item). You can define every kind of command or script here as an action. An easy example of this kind of plugin is the video.mover plugin. As it's a video plugin it is placed in `src/video/plugins` .

An item plugin is a subclass of the `PluginInterface` class which has at least the following functions:

- actions: This function returns a list of possible actions for this item (or an empty list).

  -         def actions(self,item):
                if SOMECONDITION:
                    return [ (self.COMMAND,'Name of menu entry')]
                return []

    Each action item is a pair function/description. With SOMECONDITION you can finetune for which items this actions should be available

- One or several commands that should be available

  -         def COMMAND(self,arg=None, menuw=None):
                item = self.item
                os.system('command')
                menuw.delete_menu(arg, menuw)

    You can define as many of this commands as you like.

To make this more clear for you, here is another example:

I wrote a small script called `ts2avi.sh` to encode my recordings to avi. We watch most recordings within the next few days and delete the files after that, so not all of them are worth further encoding. Thus I created menu entries for encoding using a item plugin. I have two entries, which call the script with different parameters. The plugin file is called `ts2avi.py` and is placed in `src/video/plugins` .

    import os
    import plugin
    import config

    class PluginInterface(plugin.ItemPlugin):
        """
        Plugin to encode ts files

        Activate:
        plugin.activate('video.ts2avi')

        This plugin creates additional entries in the context menu of ts-files
        for encoding those files to avi.
        One can choose among several methods for different formats.

        """

        def __init__(self):
            plugin.ItemPlugin.__init__(self)

        #Actions:
        def actions(self,item):
            self.item = item
            # Conditions: this actions are displayed for files with the extension 'ts'
            if item.mode == 'file' and os.path.splitext(item.filename)[1] == '.ts':
                return [ (self.encode_to_avi_normal, 'Encode to avi (4:3)'),(self.encode_to_avi_letter,'Encode to avi (letterbox)')]
            return []

        # Commands: The script is called as 'ts2avi <filename.ts> <filename> <bitrate> <norm/letter>'
        def encode_to_avi_normal(self,arg=None, menuw=None):
            item = self.item
            os.system('ts2avi.sh "%s" "%s" 900 norm &' % (item.filename,os.path.splitext(item.filename)[0]))
            menuw.delete_menu(arg, menuw)

        def encode_to_avi_letter(self,arg=None, menuw=None):
            item = self.item
            os.system('ts2avi.sh "%s" "%s" 900 letter &' % (item.filename,os.path.splitext(item.filename)[0]))
            menuw.delete_menu(arg, menuw)

## Idlebar plugins

Idlebar plugins are ideal to display some information about your system. There are already a couple of such plugins, most of them are located in `/src/plugins/idlebar/__init__.py` . If you want to add another one, the easiest way is to add your code to that file, too.

Idlebar plugins consist of one class, which has at least a draw method, which draws its information to the idlebar. Most of them have also one additional method to get the needed information from the system and of course there could be more helper methods. From within the draw function you can call `osd.draw_image` to display a image and `osd.write_text` for text. The draw function must return the width of the displayed information, so that the next idlebar plugin knows where it can start to draw.

Here is a simple example, which displays the free space on a given mount point in the idlebar, which is quite useful in order to monitor the available diskplace for recordings.

    import util.fileops as util

    class diskfree(IdleBarPlugin):
        """
        Displays the amount of free disk space

        Activate with:
        plugin.activate('idlebar.diskfree', level=30, args=('/mountpoint'))

        This plugin displays the total amount of free disk space on a given mount point
        """
        def __init__(self, mount='/'):
            IdleBarPlugin.__init__(self)
            self.mount = mount
            self.time = 0
            self.diskfree = 0

        def getDiskFree(self):
            """
            Determine amount of freedisk space
            Update maximum every 30 seconds

            """
            if (time.time()-self.time)>30:
                self.time = time.time()
                freespace = util.freespace(self.mount)
                self.diskfree = _('%iGb') % (((freespace / 1024) / 1024) / 1024)

        def draw(self, (type,object),x,osd):
            """
            Drawing to idlebar
            """

            self.getDiskFree()
            font = osd.get_font('small0')
            widthdf = 0
            widthdf = font.stringsize(self.diskfree)
            osd.draw_image(os.path.join(config.ICON_DIR, 'misc/chartpie.png' ),(x, osd.y + 7, -1, -1))
            osd.write_text(self.diskfree, font, None, x + 15, osd.y + 55 - font.h, widthdf, font.h, 'left', 'top')

            return widthdf + 15

last edited 2005-05-07 12:14:20 by TomParker
current version: http://freevo.sourceforge.net/cgi-bin/doc/DevelopPlugins