1 #Copyright (C) 2008 Codethink Ltd
3 #This library is free software; you can redistribute it and/or
4 #modify it under the terms of the GNU Lesser General Public
5 #License version 2 as published by the Free Software Foundation.
7 #This program is distributed in the hope that it will be useful,
8 #but WITHOUT ANY WARRANTY; without even the implied warranty of
9 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 #GNU General Public License for more details.
11 #You should have received a copy of the GNU Lesser General Public License
12 #along with this program; if not, write to the Free Software
13 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 from dbus.proxies import Interface
17 from dbus.exceptions import *
22 "AccessibleObjectNoLongerExists",
23 "AccessibleObjectNotAvailable",
29 class AccessibleObjectNoLongerExists(Exception):
32 class AccessibleObjectNotAvailable(Exception):
35 #------------------------------------------------------------------------------
37 def _repack_tuple (tup):
39 Re-packs a tuple moving the last element to the beginning.
41 return (tup[-1] ,) + tup[:-1]
43 #------------------------------------------------------------------------------
45 class Enum(dbus.UInt32):
47 return self._enum_lookup[int(self)]
49 #------------------------------------------------------------------------------
52 class BaseProxyMeta(type):
53 def __new__(meta, *args, **kwargs):
54 cls = type.__new__(meta, *args, **kwargs)
56 queryable_interfaces = {
57 'Accessible':interfaces.ATSPI_ACCESSIBLE,
58 'Action':interfaces.ATSPI_ACTION,
59 'Application':interfaces.ATSPI_APPLICATION,
60 'Collection':interfaces.ATSPI_COLLECTION,
61 'Component':interfaces.ATSPI_COMPONENT,
62 'Desktop':interfaces.ATSPI_DESKTOP,
63 'Document':interfaces.ATSPI_DOCUMENT,
64 'EditableText':interfaces.ATSPI_EDITABLE_TEXT,
65 'Hypertext':interfaces.ATSPI_HYPERTEXT,
66 'Hyperlink':interfaces.ATSPI_HYPERLINK,
67 'Image':interfaces.ATSPI_IMAGE,
68 'Selection':interfaces.ATSPI_SELECTION,
69 'StreamableContent':interfaces.ATSPI_STREAMABLE_CONTENT,
70 'Table':interfaces.ATSPI_TABLE,
71 'Text':interfaces.ATSPI_TEXT,
72 'Value':interfaces.ATSPI_VALUE,
75 def return_query(interface):
77 return self.queryInterface(interface)
80 for interface in queryable_interfaces.keys():
81 name = 'query%s' % interface
82 setattr(cls, name, return_query(queryable_interfaces[interface]))
86 #------------------------------------------------------------------------------
88 class BaseProxy(object):
90 The base D-Bus proxy for a remote object that implements one or more
91 of the AT-SPI interfaces.
94 __metaclass__ = BaseProxyMeta
96 def __init__(self, app_name, acc_path, cache, interface, dbus_object=None):
98 Create a D-Bus Proxy for an ATSPI interface.
100 cache - ApplicationCache, where the cached data for the accessible can be obtained.
101 app_name - D-Bus bus name of the application this accessible belongs to.
102 acc_path - D-Bus object path of the server side accessible object.
103 parent - Parent accessible.
104 dbus_object(kwarg) - The D-Bus proxy object used by the accessible for D-Bus method calls.
107 self._app_name = app_name
108 self._acc_path = acc_path
109 self._dbus_interface = interface
112 dbus_object = cache.connection.get_object(self._app_name,
115 self._dbus_object = dbus_object
117 self._pgetter = self.get_dbus_method("Get",
118 dbus_interface="org.freedesktop.DBus.Properties")
119 self._psetter = self.get_dbus_method("Set",
120 dbus_interface="org.freedesktop.DBus.Properties")
124 return '[%s | %s]' % (self.getRoleName(), self.name)
128 def __eq__(self, other):
132 if self._app_name == other._app_name and \
133 self._acc_path == other._acc_path:
137 except AttributeError:
140 def __ne__(self, other):
141 return not self.__eq__(other)
144 return hash(self._app_name + self._acc_path)
146 def get_dbus_method(self, *args, **kwargs):
147 method = self._dbus_object.get_dbus_method(*args, **kwargs)
149 def dbus_method_func(*iargs, **ikwargs):
150 # TODO Need to throw an AccessibleObjectNoLongerExists exception
151 # on D-Bus error of the same type.
153 return method(*iargs, **ikwargs)
154 except UnknownMethodException, e:
155 raise NotImplementedError(e)
156 except DBusException, e:
159 return dbus_method_func
162 def cached_data(self):
164 return self._cache.get_cache_data(self._app_name, self._acc_path)
166 raise AccessibleObjectNoLongerExists, \
167 'Cache data cannot be found for path %s in app %s' % (self._acc_path, self._app_name)
170 def interfaces(self):
171 return self.cached_data.interfaces
173 def queryInterface(self, interface):
175 Gets a different accessible interface for this object
176 or raises a NotImplemented error if the given interface
179 if interface in self.interfaces:
180 return self._cache.create_accessible(self._app_name,
183 dbus_object=self._dbus_object)
185 raise NotImplementedError(
186 "%s not supported by accessible object at path %s"
187 % (interface, self._acc_path))
189 #END----------------------------------------------------------------------------