1# Orca
2#
3# Copyright 2010 Joanmarie Diggs.
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# This library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this library; if not, write to the
17# Free Software Foundation, Inc., Franklin Street, Fifth Floor,
18# Boston MA  02110-1301 USA.
19
20"""Custom script for Empathy."""
21
22__id__        = "$Id$"
23__version__   = "$Revision$"
24__date__      = "$Date$"
25__copyright__ = "Copyright (c) 2010 Joanmarie Diggs."
26__license__   = "LGPL"
27
28import pyatspi
29
30import orca.chat as chat
31import orca.scripts.toolkits.gtk as gtk
32
33from .script_utilities import Utilities
34
35########################################################################
36#                                                                      #
37# The Empathy script class.                                            #
38#                                                                      #
39########################################################################
40
41class Script(gtk.Script):
42
43    def __init__(self, app):
44        """Creates a new script for the given application."""
45
46        # So we can take an educated guess at identifying the buddy list.
47        #
48        self._buddyListAncestries = [[pyatspi.ROLE_TREE_TABLE,
49                                      pyatspi.ROLE_SCROLL_PANE,
50                                      pyatspi.ROLE_FILLER,
51                                      pyatspi.ROLE_FRAME]]
52
53        gtk.Script.__init__(self, app)
54
55    def getChat(self):
56        """Returns the 'chat' class for this script."""
57
58        return chat.Chat(self, self._buddyListAncestries)
59
60    def getUtilities(self):
61        """Returns the utilites for this script."""
62
63        return Utilities(self)
64
65    def setupInputEventHandlers(self):
66        """Defines InputEventHandler fields for this script that can be
67        called by the key and braille bindings. Here we need to add the
68        handlers for chat functionality.
69        """
70
71        gtk.Script.setupInputEventHandlers(self)
72        self.inputEventHandlers.update(self.chat.inputEventHandlers)
73
74    def getAppKeyBindings(self):
75        """Returns the application-specific keybindings for this script."""
76
77        return self.chat.keyBindings
78
79    def getAppPreferencesGUI(self):
80        """Return a GtkGrid containing the application unique configuration
81        GUI items for the current application. The chat-related options get
82        created by the chat module."""
83
84        return self.chat.getAppPreferencesGUI()
85
86    def getPreferencesFromGUI(self):
87        """Returns a dictionary with the app-specific preferences."""
88
89        return self.chat.getPreferencesFromGUI()
90
91    def skipObjectEvent(self, event):
92        """Gives us, and scripts, the ability to decide an event isn't
93        worth taking the time to process under the current circumstances.
94
95        Arguments:
96        - event: the Event
97
98        Returns True if we shouldn't bother processing this object event.
99        """
100
101        if self.chat.isChatRoomMsg(event.source):
102            return False
103
104        return gtk.Script.skipObjectEvent(self, event)
105
106    def onTextInserted(self, event):
107        """Called whenever text is added to an object."""
108
109        if event.source.getRole() == pyatspi.ROLE_LABEL:
110            # The is the timer of a call.
111            #
112            return
113
114        if self.chat.presentInsertedText(event):
115            return
116
117        gtk.Script.onTextInserted(self, event)
118
119    def onWindowActivated(self, event):
120        """Called whenever a toplevel window is activated."""
121
122        # Hack to "tickle" the accessible hierarchy. Otherwise, the
123        # events we need to present text added to the chatroom are
124        # missing.
125        hasRole = lambda x: x and x.getRole() == pyatspi.ROLE_PAGE_TAB
126        allPageTabs = pyatspi.findAllDescendants(event.source, hasRole)
127        gtk.Script.onWindowActivated(self, event)
128
129    def onValueChanged(self, event):
130        """Called whenever an object's value changes.  Currently, the
131        value changes for non-focused objects are ignored.
132
133        Arguments:
134        - event: the Event
135        """
136
137        if event.source.getRole() == pyatspi.ROLE_WINDOW:
138            # The is the timer of a call.
139            #
140            return
141
142        gtk.Script.onValueChanged(self, event)
143