aboutsummaryrefslogtreecommitdiff
path: root/examples/exportactiongroup.py
blob: 0f2d6755d86e6bd19ab127380bc01b6a2f51de89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

"""
This example script exports an action group on the session bus under the name
com.canonical.testactiongroup and at the object path
/com/canonical/testactiongroup.
"""

import sys

from gi.repository import Gio
from gi.repository import GLib


BUS_NAME = 'com.canonical.testactiongroup'
BUS_OBJECT_PATH = '/com/canonical/testactiongroup'


def action_activated(action, data):
    name = action.get_name()
    if action.get_state_type() is None:
        print('stateless action activated: %s' % name)
    else:
        print('stateful action activated: %s (current state: %s)' %
              (name, action.get_state()))


if __name__ == '__main__':
    bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
    # Claim well-known bus name and ensure only one instance of self is running
    # at any given time.
    # http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-names
    proxy = Gio.DBusProxy.new_sync(bus, 0, None,
                                   'org.freedesktop.DBus',
                                   '/org/freedesktop/DBus',
                                   'org.freedesktop.DBus', None)
    result = proxy.RequestName('(su)', BUS_NAME, 0x4)
    if result != 1 :
        print >> sys.stderr, ("Name '%s' is already owned on the session bus."
                              "Aborting.") % BUS_NAME
        sys.exit(1)

    group = Gio.SimpleActionGroup()
    foo = Gio.SimpleAction.new('foo', None)
    group.insert(foo)
    bar = Gio.SimpleAction.new_stateful('bar', None, GLib.Variant.new_boolean(False))
    group.insert(bar)
    bleh = Gio.SimpleAction.new_stateful('bleh', None, GLib.Variant.new_string('bleh'))
    group.insert(bleh)
    for name in group.list_actions():
        action = group.lookup_action(name)
        action.connect('activate', action_activated)
    bus.export_action_group(BUS_OBJECT_PATH, group)

    GLib.MainLoop().run()