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 *
20 from factory import create_accessible
23 "AccessibleObjectNoLongerExists",
28 class AccessibleObjectNoLongerExists(Exception):
31 #------------------------------------------------------------------------------
35 return self._enum_lookup[int(self)]
37 #------------------------------------------------------------------------------
40 class BaseProxyMeta(type):
41 def __new__(meta, *args, **kwargs):
42 cls = type.__new__(meta, *args, **kwargs)
44 queryable_interfaces = {
45 'Accessible':interfaces.ATSPI_ACCESSIBLE,
46 'Action':interfaces.ATSPI_ACTION,
47 'Application':interfaces.ATSPI_APPLICATION,
48 'Collection':interfaces.ATSPI_COLLECTION,
49 'Component':interfaces.ATSPI_COMPONENT,
50 'Desktop':interfaces.ATSPI_DESKTOP,
51 'Document':interfaces.ATSPI_DOCUMENT,
52 'EditableText':interfaces.ATSPI_EDITABLE_TEXT,
53 'Hypertext':interfaces.ATSPI_HYPERTEXT,
54 'Hyperlink':interfaces.ATSPI_HYPERLINK,
55 'Image':interfaces.ATSPI_IMAGE,
56 'Selection':interfaces.ATSPI_SELECTION,
57 'StreamableContent':interfaces.ATSPI_STREAMABLE_CONTENT,
58 'Table':interfaces.ATSPI_TABLE,
59 'Text':interfaces.ATSPI_TEXT,
60 'Value':interfaces.ATSPI_VALUE,
63 def return_query(interface):
65 return self.queryInterface(interface)
68 for interface in queryable_interfaces.keys():
69 name = 'query%s' % interface
70 setattr(cls, name, return_query(queryable_interfaces[interface]))
74 #------------------------------------------------------------------------------
76 class BaseProxy(Interface):
78 The base D-Bus proxy for a remote object that implements one or more
79 of the AT-SPI interfaces.
82 __metaclass__ = BaseProxyMeta
84 def __init__(self, cache, app_name, acc_path, interface, dbus_object=None, connection=None, parent=None):
86 Create a D-Bus Proxy for an ATSPI interface.
88 cache - ApplicationCache, where the cached data for the accessible can be obtained.
89 app_name - D-Bus bus name of the application this accessible belongs to.
90 acc_path - D-Bus object path of the server side accessible object.
91 parent - Parent accessible.
92 interface - D-Bus interface of the object. Used to decide which accessible class to instanciate.
93 dbus_object(kwarg) - The D-Bus proxy object used by the accessible for D-Bus method calls.
96 self._app_name = app_name
97 self._acc_path = acc_path
101 dbus_object = connection.get_object(self._app_name, self._acc_path, introspect=False)
102 self._dbus_object = dbus_object
104 Interface.__init__(self, self._dbus_object, interface)
106 self._pgetter = self.get_dbus_method("Get", dbus_interface="org.freedesktop.DBus.Properties")
107 self._psetter = self.get_dbus_method("Set", dbus_interface="org.freedesktop.DBus.Properties")
109 def __getattr__(*args, **kwargs):
110 return object.__getattr__(*args, **kwargs)
112 def get_dbus_method(self, *args, **kwargs):
113 method = Interface.get_dbus_method(self, *args, **kwargs)
115 def dbus_method_func(*args, **kwargs):
116 # TODO Need to throw an AccessibleObjectNoLongerExists exception
117 # on D-Bus error of the same type.
119 return method(*args, **kwargs)
120 except UnknownMethodException, e:
121 raise NotImplementedError(e)
122 except DBusException, e:
125 return dbus_method_func
128 def cached_data(self):
130 return self._cache[self._app_name][self._acc_path]
132 raise AccessibleObjectNoLongerExists, \
133 'Cache data cannot be found for path %s in app %s' % (self._acc_path, self._app_name)
136 def interfaces(self):
137 return self.cached_data.interfaces
139 def queryInterface(self, interface):
141 Gets a different accessible interface for this object
142 or raises a NotImplemented error if the given interface
145 if interface in self.interfaces:
146 return create_accessible(self._cache,
150 dbus_object=self._dbus_object)
152 raise NotImplementedError(
153 "%s not supported by accessible object at path %s"
154 % (interface, self.path))
156 #END----------------------------------------------------------------------------