2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / pyatspi / event.py
index d5a31e7..948535f 100644 (file)
@@ -13,8 +13,8 @@
 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 import interfaces
-from factory import create_accessible, add_accessible_class
 from accessible import BoundingBox
+from base import AccessibleObjectNotAvailable
 
 __all__ = [
                 "Event",
@@ -28,6 +28,7 @@ _interface_to_klass = {
                 "org.freedesktop.atspi.Event.Object":"object",
                 "org.freedesktop.atspi.Event.Window":"window",
                 "org.freedesktop.atspi.Event.Mouse":"mouse",
+                "org.freedesktop.atspi.Event.Keyboard":"keyboard",
                 "org.freedesktop.atspi.Event.Terminal":"terminal",
                 "org.freedesktop.atspi.Event.Document":"document",
                 "org.freedesktop.atspi.Event.Focus":"focus",
@@ -37,6 +38,7 @@ _klass_to_interface = {
                 "object":"org.freedesktop.atspi.Event.Object",
                 "window":"org.freedesktop.atspi.Event.Window",
                 "mouse":"org.freedesktop.atspi.Event.Mouse",
+                "keyboard":"org.freedesktop.atspi.Event.Keyboard",
                 "terminal":"org.freedesktop.atspi.Event.Terminal",
                 "document":"org.freedesktop.atspi.Event.Document",
                 "focus":"org.freedesktop.atspi.Event.Focus",
@@ -86,7 +88,7 @@ class EventType(str):
                 @raise AttributeError: When the given event name is not a valid string 
                 """
                 stripped = name.strip(self._SEPARATOR)
-                separated = stripped.split(self._SEPARATOR, 3) 
+                separated = stripped.split(self._SEPARATOR, 3)
                 self._separated = _ELessList(separated)
 
                 self.klass = self._separated[0]
@@ -94,8 +96,6 @@ class EventType(str):
                 self.minor = self._separated[2]
                 self.detail = self._separated[3]
 
-                self._name = ":".join(separated)
-
         def is_subtype(self, event_type):
                 """
                 Determines if the passed event type is a subtype
@@ -113,11 +113,11 @@ class EventType(str):
 
         @property
         def name(self):
-                return self._name
+                return str(self)
 
         @property
         def value(self):
-                return self._name
+                return str(self)
 
 #------------------------------------------------------------------------------
 
@@ -137,12 +137,28 @@ def event_type_to_signal_reciever(bus, cache, event_handler, event_type):
         if event_type.minor:
                 kwargs['arg0'] = event_type.minor
 
-        def handler_wrapper(minor, detail1, detail2, any_data, 
+        def handler_wrapper(minor, detail1, detail2, any_data,
                             sender=None, interface=None, member=None, path=None):
                 event = Event(cache, path, sender, interface, member, (minor, detail1, detail2, any_data))
                 return event_handler(event)
 
-        return bus.add_signal_receiver(handler_wrapper, **kwargs) 
+        return bus.add_signal_receiver(handler_wrapper, **kwargs)
+
+#------------------------------------------------------------------------------
+
+def signal_spec_to_event_string (interface, name, minor):
+        interface = _interface_to_klass[interface]
+        name = name.replace('_', '-')
+
+        if interface == "focus":
+                return "focus:"
+
+        result = interface + ':'
+        if name:
+                result += name + ':'
+        if minor:
+                result += minor
+        return result
 
 #------------------------------------------------------------------------------
 
@@ -192,12 +208,8 @@ class Event(object):
                 self._source = None
                 self._application = None
 
-                self._klass = _interface_to_klass[interface]
-                # The replace is neccessary as '-' not legal as signal name
-                # so translated on the server side.
-                self._major = name.replace('_', '-')
-                self._minor = event[0]
-                self.type = EventType(':'.join([self._klass, self._major, self._minor]))
+                self.type = EventType(signal_spec_to_event_string(interface, name, event[0]))
+
                 self.detail1 = event[1]
                 self.detail2 = event[2]
 
@@ -210,22 +222,21 @@ class Event(object):
         @property
         def host_application(self):
                 if not self._application:
-                        application_root = self._cache[self._source_application]._get_root()
-                        return create_accessible(self._cache,
-                                                  self._source_application,
-                                                 application_root,
-                                                 interfaces.ATSPI_APPLICATION,
-                                                   connection=self._cache._connection)
+                        try:
+                                return self._cache.create_application(self._source_application)
+                        except AccessibleObjectNotAvailable:
+                                pass
                 return self._application
 
         @property
         def source(self):
                 if not self._source:
-                        self._source = create_accessible(self._cache,
-                                                            self._source_application,
-                                                           self._source_path,
-                                                           interfaces.ATSPI_ACCESSIBLE,
-                                                           connection=self._cache._connection)
+                        try:
+                                self._source = self._cache.create_accessible(self._source_application,
+                                                                             self._source_path,
+                                                                             interfaces.ATSPI_ACCESSIBLE)
+                        except AccessibleObjectNotAvailable:
+                                pass
                 return self._source
 
         @property