2008-08-21 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / pyatspi / registry.py
1 #Copyright (C) 2008 Codethink Ltd
2 #copyright: Copyright (c) 2005, 2007 IBM Corporation
3
4 #This library is free software; you can redistribute it and/or
5 #modify it under the terms of the GNU Lesser General Public
6 #License version 2 as published by the Free Software Foundation.
7
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #GNU General Public License for more details.
12 #You should have received a copy of the GNU Lesser General Public License
13 #along with this program; if not, write to the Free Software
14 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
16 #Portions of this code originally licensed and copyright (c) 2005, 2007
17 #IBM Corporation under the BSD license, available at
18 #U{http://www.opensource.org/licenses/bsd-license.php}
19
20 #authors: Peter Parente, Mark Doffman
21
22 import dbus
23
24 from dbus.mainloop.glib import DBusGMainLoop
25 DBusGMainLoop(set_as_default=True)
26
27 from test import TestApplicationCache
28 from desktop import Desktop
29
30 class Registry(object):
31         """
32         Wraps the Accessibility.Registry to provide more Pythonic registration for
33         events. 
34         
35         This object should be treated as a singleton, but such treatment is not
36         enforced. You can construct another instance of this object and give it a
37         reference to the Accessibility.Registry singleton. Doing so is harmless and
38         has no point.
39         
40         @ivar async: Should event dispatch to local listeners be decoupled from event
41                 receiving from the registry?
42         @type async: boolean
43         @ivar reg: Reference to the real, wrapped registry object
44         @type reg: Accessibility.Registry
45         @ivar dev: Reference to the device controller
46         @type dev: Accessibility.DeviceEventController
47         @ivar queue: Queue of events awaiting local dispatch
48         @type queue: Queue.Queue
49         @ivar clients: Map of event names to client listeners
50         @type clients: dictionary
51         @ivar observers: Map of event names to AT-SPI L{_Observer} objects
52         @type observers: dictionary
53         """
54
55         _REGISTRY_NAME = 'org.freedesktop.atspi.Registry'
56
57         def __init__(self, app_name=None):
58                 """
59                 Stores a reference to the AT-SPI registry. Gets and stores a reference
60                 to the DeviceEventController.
61                 
62                 @param reg: Reference to the AT-SPI registry daemon
63                 @type reg: Accessibility.Registry
64                 """
65                 self._bus = dbus.SessionBus()
66                 if app_name:
67                         self._app_name = app_name
68                         self._cache = TestApplicationCache(self._bus, app_name)
69                 
70         def __call__(self):
71                 """
72                 @return: This instance of the registry
73                 @rtype: L{Registry}
74                 """
75                 return self
76         
77         def start(self, async=False, gil=True):
78                 """
79                 Enter the main loop to start receiving and dispatching events.
80                 
81                 @param async: Should event dispatch be asynchronous (decoupled) from 
82                         event receiving from the AT-SPI registry?
83                 @type async: boolean
84                 @param gil: Add an idle callback which releases the Python GIL for a few
85                         milliseconds to allow other threads to run? Necessary if other threads
86                         will be used in this process.
87                         Note - No Longer used.
88                 @type gil: boolean
89                 """
90                 self._loop = gobject.MainLoop()
91                 self._loop.run()
92
93         def stop(self, *args):
94                 """Quits the main loop."""
95                 self._loop.quit()
96                 self.flushEvents()
97                 
98         def getDesktopCount(self):
99                 """
100                 Gets the number of available desktops.
101                 
102                 @return: Number of desktops
103                 @rtype: integer
104                 """
105                 return 1
106                 
107         def getDesktop(self, i):
108                 """
109                 Gets a reference to the i-th desktop.
110                 
111                 @param i: Which desktop to get
112                 @type i: integer
113                 @return: Desktop reference
114                 @rtype: Accessibility.Desktop
115                 """
116                 return Desktop(self._cache)