HowToWritePlugins

Revision 10 as of 2011-06-01 09:07:54

Clear message

Intro

Plugin is a simple python script which use the API from Ubuntu Tweak. It is easy to write and well integrated with Ubuntu Tweak.

Currently the API is not stable yet, but I hope you can start to write something and give some feedback to me. So I can improve Ubuntu Tweak.

Prerequisite

Overview

As you can see of the screenshot, it consists of two parts: 1. Tweaks, 2. Admins (And Jantor will be added soon)

If your plugin is doing some tweaks, you should add it to "Tweaks", if it is doing some other tasks (backup, update and so on), it can be put under "Admins"

Ubuntu Tweak 0.6.0.png

Let's start with an example.

Example 1

In the first example, I will add some options to do something of bluetooth file sharing.

If you've ran Ubuntu Tweak 0.6 before, some folder will be created under $HOME/.config/ubuntu-tweak, they are "tweaks", "admins". These are the user plugin folders.

Create a file named "bluetoothsettings.py(or click to download it)" and put it into $HOME/.config/ubuntu-tweak/tweaks/, fill with these contents:

# Your name, email and other copyright information

from gi.repository import Gtk, GConf #Don't support PyGtk

from ubuntutweak.gui.containers import TablePack # TablePack is a container to pack any numbers of widgets
from ubuntutweak.modules  import TweakModule # Subclass this Module then it will become a plugin of Ubuntu Tweak
from ubuntutweak.factory import WidgetFactory # Use to create the setting related widget easily

class BluetoothSettings(TweakModule):
    __title__ = _('Bluetooth Settings')
    __desc__ = _('Tweak some hidden bluetooth settings')
    __icon__ = 'bluetooth' # The icon name, currently don't support path to image
    __category__ = 'system' # the category

    __author__ = 'Your Name <your@mail.com>'
    __url__ = 'Your home page'
    __url_title__ = 'The title of Home page'

    def __init__(self):
        TweakModule.__init__(self)

        # WidgetFactory will return a tuple of widgets 
        box = TablePack(_("Bluetooth Options"), (
                    WidgetFactory.create("CheckButton",
                                         label=_('Share Public directory over Bluetooth'),
                                         key="/desktop/gnome/file_sharing/bluetooth_enabled",
                                         backend=GConf, # GSettings will support in the future
                                         enable_reset=True),
                    WidgetFactory.create("CheckButton",
                                         label=_("Whether to notify about newly received files"),
                                         key="/desktop/gnome/file_sharing/bluetooth_notify",
                                         backend=GConf,
                                         enable_reset=True),
                    WidgetFactory.create("CheckButton",
                                         label=_("Whether to allow Bluetooth clients to write files."),
                                         key="/desktop/gnome/file_sharing/bluetooth_allow_write",
                                         backend=GConf,
                                         enable_reset=True),
                    WidgetFactory.create('ComboBox',
                                         label=_('When to accept files sent over Bluetooth'),
                                         key='/desktop/gnome/file_sharing/bluetooth_accept_files',
                                         enable_reset=True,
                                         backend=GConf,
                                         texts=[_('Always'), _('Bonded'), _('Ask')],
                                         values=["always", "bonded", "ask"]),
            ))

        self.add_start(box, False, False, 0)

Now save the file, open Ubuntu Tweak, see what has been added? Yes, the Bluetooth Settings!

BluetoothSettings-01.png

Click it! See what you have done with a few lines of code, a completed tweaks feature about Bluetooth is also most there.

And each option has a "Reset" button at the right side, if you want to reset to original setting but you forget it, just click the "Reset" button.

BluetoothSettings-02.png

I'm not good at the API design, so if you have some suggestions, please tell me, let's make it better. You can find my contact information here: http://blog.ubuntu-tweak.com/about