summaryrefslogtreecommitdiff
path: root/pyatspi/accessiblecache.py
blob: 747747a8360799899fb90c2511c51c57d9e7740a (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#Copyright (C) 2008 Codethink Ltd

#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License version 2 as published by the Free Software Foundation.

#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 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, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import dbus as _dbus

from event import Event as _Event

#------------------------------------------------------------------------------

class _CacheData(object):
        __slots__ = [
                        'path',
                        'parent',
                        'interfaces',
                        'children',
                        'role',
                        'name',
                        'description',
                        'state',
                    ]

        def __init__(self, data):
                self._update(data)

        def _update(self, data):
                (self.path,
                 self.parent,
                 self.children,
                 self.interfaces,
                 self.name,
                 self.role,
                 self.description,
                 self.state) = data

#------------------------------------------------------------------------------

def _list_items_added_removed (l1, l2):
        """
        Returns a tuple (boolean, boolean).
        The first value indicates if, when
        moving from l1 to l2, any items have been added.
        The second value indicates whether any items have
        been removed.
        """
        l1notl2 = [item for item in l1 if item not in l2]
        l2notl1 = [item for item in l2 if item not in l1]
        return ((len(l1notl2) > 0), (len(l2notl1) > 0))

#------------------------------------------------------------------------------

class AccessibleCache(object):
        """
        There is one accessible cache per application.
        For each application the accessible cache stores
        data on every accessible object within the app.

        It also acts as the factory for creating client
        side proxies for these accessible objects.

        connection - DBus connection.
        busName    - Name of DBus connection where cache interface resides.
        """

        _PATH = '/org/freedesktop/atspi/tree'
        _INTERFACE = 'org.freedesktop.atspi.Tree'
        _GET_METHOD = 'getTree'
        _UPDATE_SIGNAL = 'updateAccessible'
        _REMOVE_SIGNAL = 'removeAccessible'

        def __init__(self, registry, connection, bus_name):
                """
                Creates a cache.

                connection - DBus connection.
                busName    - Name of DBus connection where cache interface resides.
                """
                self._registry = registry
                self._connection = connection
                self._bus_name = bus_name

                obj = connection.get_object(bus_name, self._PATH, introspect=False)
                itf = _dbus.Interface(obj, self._INTERFACE)

                self._objects = {}

                get_method = itf.get_dbus_method(self._GET_METHOD)
                self._update_objects(get_method())

                self._updateMatch = itf.connect_to_signal(self._UPDATE_SIGNAL, self._update_single)
                self._removeMatch = itf.connect_to_signal(self._REMOVE_SIGNAL, self._remove_object)

                obj = connection.get_object(self._bus_name, self._PATH, introspect=False)
                itf = _dbus.Interface(obj, self._INTERFACE)

                self._root = itf.getRoot()

        def __getitem__(self, key):
                return self._objects[key]

        def __contains__(self, key):
                return key in self._objects

        def _dispatch_event(self, olddata, newdata):
                if olddata.name != newdata.name:
                        event = _Event(self._registry.cache,
                                       newdata.path,
                                       self._bus_name,
                                       "org.freedesktop.atspi.Event.Object",
                                       "property-change",
                                       ("accessible-name", 0, 0, newdata.name))
                        self._registry._notifyNameChange(event)

                if olddata.description != newdata.description:
                        event = _Event(self._registry.cache,
                                       newdata.path,
                                       self._bus_name,
                                       "org.freedesktop.atspi.Event.Object",
                                       "property-change",
                                       ("accessible-description", 0, 0, description))
                        self._registry._notifyDescriptionChange(event)

                if olddata.parent != newdata.parent:
                        event = _Event(self._registry.cache,
                                       newdata.path,
                                       self._bus_name,
                                       "org.freedesktop.atspi.Event.Object",
                                       "property-change",
                                       ("accessible-parent", 0, 0, ""))
                        self._registry._notifyParentChange(event)

                removed, added = _list_items_added_removed (olddata.children, newdata.children)

                if added:
                        event = _Event(self._registry.cache,
                                       newdata.path,
                                       self._bus_name,
                                       "org.freedesktop.atspi.Event.Object",
                                       "children-changed",
                                       ("add", 0, 0, ""))
                        self._registry._notifyChildrenChange(event)

                if removed:
                        event = _Event(self._registry.cache,
                                       newdata.path,
                                       self._bus_name,
                                       "org.freedesktop.atspi.Event.Object",
                                       "children-changed",
                                       ("remove", 0, 0, ""))
                        self._registry._notifyChildrenChange(event)

        # TODO This should be the other way around. Single is more common than many.
        def _update_single(self, object):
                self._update_objects ([object])

        def _update_objects(self, objects):
                cache_update_objects = []
                for data in objects:
                        #First element is the object path.
                        path = data[0]
                        if path in self._objects:
                                olddata = self._objects[path]
                                newdata = _CacheData(data)
                                cache_update_objects.append((olddata, newdata))
                                self._objects[path] = newdata
                        else:
                                self._objects[path] = _CacheData(data)
                for old, new in cache_update_objects:
                        self._dispatch_event(old, new)

        def _remove_object(self, paths):
                # TODO I'm squashing a possible error here
                # I've seen things appear to be deleted twice
                # which needs investigation
                try:
                        del(self._objects[path])
                except KeyError:
                        pass

        def _get_root(self):
                return self._root

        root = property(fget=_get_root)

#END---------------------------------------------------------------------------