Convert other enums to int for Python 2.5 (Stephen Watson).
[rox-lib/lack.git] / python / rox / session.py
blobd6f5ad1ecd570a69407cc8affed7dea379d304e9
1 """Contact ROX-Session via the DBus or XMLRPC interface. Using get_session()
2 will return a proxy object on which you can make remote calls to control
3 the session. Similarly get_settings() will return an object to control
4 session settings, e.g.
6 try:
7 settings = session.get_settings()
8 type, value = settings.GetSetting('Gtk/KeyThemeName')
9 except:
10 # No ROX-Session available, do something else
12 In addition the Setting class is provided which derives from rox.options.Option
13 but has two important differences: it is not saved to the options file and
14 it is synchronized with a value of the same name in the ROX-Session settings.
15 """
17 import rox
18 import rox.xxmlrpc
19 # import gobject
21 try:
22 import dbus
23 dbus_ok=(dbus.version>=(0, 42, 0))
24 except:
25 dbus_ok=False
27 if dbus_ok:
28 bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
29 else:
30 bus = None
32 session_service = "net.sf.rox.Session"
33 control_object = '/Session'
34 control_interface = 'net.sf.rox.Session.Control'
35 settings_interface = 'net.sf.rox.Session.Settings'
36 settings_object = '/Settings'
38 def _dbus_get_proxy(bus, service_name, object_name, interface_name):
39 """For internal use. Do not call this, call get_proxy() instead."""
40 try:
41 service = bus.get_service(service_name)
42 obj = service.get_object(object_name, interface_name)
43 proxy = obj
44 except AttributeError:
45 obj = bus.get_object(service_name, object_name)
46 iface = dbus.Interface(obj, interface_name)
47 proxy = iface
48 return proxy
50 class _caller:
51 """For internal use."""
52 def __init__(self, method):
53 self.method=method
55 def __call__(self, *params):
56 client=self.method(*params)
57 return client.get_response()
59 class _RPCProxy:
60 """For internal use."""
61 def __init__(self, obj):
62 self.obj=obj
64 def __getattr__(self, method):
65 invoke=self.obj.__getattr__(method)
66 return _caller(invoke)
68 def _xxmlrpc_get_proxy(service_name, object_name, interface_name):
69 """For internal use. Do not call this, call get_proxy() instead."""
70 proxy=rox.xxmlrpc.XXMLProxy(service_name)
71 return _RPCProxy(proxy.get_object(object_name))
73 def get_proxy(service_name, object_name, interface_name):
74 """Get a proxy object for the required service, object path and interface.
75 This selects an appropriate transport for you, either DBus or XMLRPC."""
76 if dbus_ok and bus:
77 return _dbus_get_proxy(bus, service_name, object_name, interface_name)
78 return _xxmlrpc_get_proxy(service_name, object_name, interface_name)
80 def get_session():
81 """Return a proxy object for the ROX-Session settings interface"""
82 return get_proxy(session_service, control_object,
83 control_interface)
85 def get_settings():
86 """Return a proxy object for the ROX-Session control interface"""
87 return get_proxy(session_service, settings_object,
88 settings_interface)
90 def running():
91 """Return True if ROX-Session is detected as running"""
92 if not dbus_ok or not bus:
93 proxy=_xxmlrpc_get_proxy(session_service, control_object,
94 control_interface)
95 return proxy is not None
96 try:
97 proxy = get_proxy('org.freedesktop.DBus', '/org/freedesktop/DBus',
98 'org.freedesktop.DBus')
99 except:
100 return False
102 try:
103 services = proxy.ListServices()
104 except:
105 services = proxy.ListNames()
107 return session_service in services
109 if bus and not running():
110 bus=None
112 # Test routine
113 if __name__=='__main__':
114 print 'Session running? %s' % running()
115 settings=get_settings()
117 def test_get(name):
118 try:
119 v=settings.GetSetting(name)
120 print '%s = %s' % (name, v)
121 except Exception, exc:
122 print "Can't get %s: %s" % (name, exc)
124 test_get('Gtk/KeyThemeName')
125 test_get('Net/ThemeName')
127 control=get_session()
128 control.ShowMessages()