aboutsummaryrefslogtreecommitdiff
path: root/examples/exportactiongroup.py
blob: df05b0dd954cc2242fb0854b419217eef5e3ad36 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
#
#  Copyright 2013 Canonical Ltd.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; version 3.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

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

import sys

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


BUS_NAME = 'org.ayatana.testactiongroup'
BUS_OBJECT_PATH = '/org/ayatana/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()