Allow caching of data sent with events and requesting such data
[platform/upstream/at-spi2-core.git] / atspi / atspi-matchrule.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  * Copyright 2010, 2011 Novell, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "atspi-private.h"
26
27 G_DEFINE_TYPE (AtspiMatchRule, atspi_match_rule, G_TYPE_OBJECT)
28
29 static void
30 atspi_match_rule_init (AtspiMatchRule *match_rule)
31 {
32 }
33
34 static void
35 atspi_match_rule_dispose (GObject *object)
36 {
37   AtspiMatchRule *rule = ATSPI_MATCH_RULE (object);
38
39   if (rule->states)
40   {
41     g_object_unref (rule->states);
42     rule->states = NULL;
43   }
44
45   if (rule->attributes)
46   {
47     g_hash_table_unref (rule->attributes);
48     rule->attributes = NULL;
49   }
50
51   G_OBJECT_CLASS (atspi_match_rule_parent_class)->dispose (object);
52 }
53
54 static void
55 atspi_match_rule_finalize (GObject *object)
56 {
57   AtspiMatchRule *rule = ATSPI_MATCH_RULE (object);
58
59   /* TODO: Check that interfaces don't leak */
60   if (rule->interfaces)
61     g_array_free (rule->interfaces, TRUE);
62
63   if (rule->attributes)
64     g_hash_table_unref (rule->attributes);
65
66   G_OBJECT_CLASS (atspi_match_rule_parent_class)->finalize (object);
67 }
68
69 static void
70 atspi_match_rule_class_init (AtspiMatchRuleClass *klass)
71 {
72   GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74   object_class->dispose = atspi_match_rule_dispose;
75   object_class->finalize = atspi_match_rule_finalize;
76 }
77
78 /**
79  * atspi_match_rule_new:
80  * @states: An #AtspiStateSet specifying the states to match or NULL if none.
81  * @statematchtype: An #AtspiCollectionMatchType specifying how to interpret
82  *          @states.
83  * @attributes: (element-type gchar* gchar*): A #GHashTable specifying
84  *          attributes to match. To specify multiple attribute values,
85  *          separate each value with a :: If an attribute value contains a :,
86  *          then it can be escaped by preceding it with a \. A backslash can
87  *          likewise be escaped by inserting a double backslash.
88  * @attributematchtype: An #AtspiCollectionMatchType specifying how to
89  *          interpret @attributes.
90  * @interfaces: (element-type gchar*): An array of interfaces to match, or
91  *          NULL if not applicable.  Interface names should be specified
92  *          by their DBus names (org.a11y.Atspi.Accessible,
93  *          org.a11y.Atspi.Component, etc).
94  * @interfacematchtype: An #AtspiCollectionMatchType specifying how to
95  *          interpret @interfaces.
96  * @roles: (element-type AtspiRole): A #GArray of roles to match, or NULL if
97  *          not applicable.
98  * @rolematchtype: An #AtspiCollectionMatchType specifying how to
99  *          interpret @roles.
100  * @invert: if #TRUE, the match rule should be denied (inverted); if #FALSE,
101  *          it should not. For example, if the match rule defines that a match is
102  *          an object of ROLE_HEADING which has STATE_FOCUSABLE and a click action,
103  *          inverting it would match all objects that are not of ROLE_HEADING,
104  *          focusable and clickable at the same time.
105  *
106  * Creates a new #AtspiMatchRule with specified @states, @attributes, 
107  * @interfaces, and @roles.
108  *
109  * Returns: (transfer full): A new #AtspiMatchRule.
110  **/
111 AtspiMatchRule *
112 atspi_match_rule_new (AtspiStateSet *states,
113                       AtspiCollectionMatchType statematchtype,
114                       GHashTable *attributes,
115                       AtspiCollectionMatchType attributematchtype,
116                       GArray *roles,
117                       AtspiCollectionMatchType rolematchtype,
118                       GArray *interfaces,
119                       AtspiCollectionMatchType interfacematchtype,
120                       gboolean invert)
121 {
122   AtspiMatchRule *rule = g_object_new (ATSPI_TYPE_MATCH_RULE, NULL);
123   int i;
124
125   if (states)
126     rule->states = g_object_ref (states);
127   rule->statematchtype = statematchtype;
128
129   if (attributes)
130   {
131     GHashTableIter hash_table_iter;
132     gchar *key, *value;
133     rule->attributes = g_hash_table_new_full (g_str_hash, g_str_equal,
134                                               (GDestroyNotify) g_free,
135                                               (GDestroyNotify) g_free);
136     g_hash_table_iter_init (&hash_table_iter, attributes);
137             while (g_hash_table_iter_next (&hash_table_iter, (gpointer *)&key,
138                    (gpointer *)&value))
139       g_hash_table_insert (rule->attributes, g_strdup (key), g_strdup (value));
140   } else
141     rule->attributes = NULL;
142   rule->attributematchtype = attributematchtype;
143
144   if (interfaces)
145     rule->interfaces = g_array_ref (interfaces);
146   rule->interfacematchtype = interfacematchtype;
147
148   if (roles)
149   {
150     for (i = 0; i < roles->len; i++)
151     {
152       AtspiRole role = g_array_index (roles, AtspiRole, i);
153       if (role < 128)
154         rule->roles [role / 32] |= (1 << (role % 32));
155       else
156         g_warning ("Atspi: unexpected role %d\n", role);
157     }
158   }
159   else
160     rule->roles [0] = rule->roles [1] = 0;
161   rule->rolematchtype = rolematchtype;
162
163   rule->invert = invert;
164
165   return rule;
166 }
167
168 static void
169 append_entry (gpointer key, gpointer val, gpointer data)
170 {
171   DBusMessageIter *iter = data;
172   DBusMessageIter iter_entry;
173
174   if (!dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL,
175                                         &iter_entry))
176     return;
177   dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &key);
178   dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &val);
179   dbus_message_iter_close_container (iter, &iter_entry);
180 }
181
182 gboolean
183 _atspi_match_rule_marshal (AtspiMatchRule *rule, DBusMessageIter *iter)
184 {
185   DBusMessageIter iter_struct, iter_array, iter_dict;
186   dbus_int32_t states [2];
187   dbus_int32_t d_statematchtype = rule->statematchtype;
188   dbus_int32_t d_attributematchtype = rule->attributematchtype;
189   dbus_int32_t d_interfacematchtype = rule->interfacematchtype;
190   dbus_uint32_t d_rolematchtype = rule->rolematchtype;
191   dbus_bool_t d_invert = rule->invert;
192   gint i;
193   dbus_int32_t d_role;
194
195   if (!dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, NULL,
196                                          &iter_struct))
197     return FALSE;
198
199   /* states */
200   if (rule->states)
201   {
202     states [0] = rule->states->states & 0xffffffff;
203     states [1] = rule->states->states >> 32;
204   }
205   else
206   {
207     states [0] = states [1] = 0;
208   }
209   dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "i", &iter_array);
210   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [0]);
211   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [1]);
212   dbus_message_iter_close_container (&iter_struct, &iter_array);
213   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_statematchtype);
214
215   /* attributes */
216   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "{ss}",
217                                          &iter_dict))
218     return FALSE;
219   if (rule->attributes)
220     g_hash_table_foreach (rule->attributes, append_entry, &iter_dict);
221   dbus_message_iter_close_container (&iter_struct, &iter_dict);
222   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_attributematchtype);
223
224   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "i",
225       &iter_array))
226     return FALSE;
227   d_role = rule->roles [0];
228   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
229   d_role = rule->roles [1];
230   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
231   d_role = rule->roles [2];
232   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
233   d_role = rule->roles [3];
234   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
235   dbus_message_iter_close_container (&iter_struct, &iter_array);
236   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32,
237                                   &d_rolematchtype);
238
239   /* interfaces */
240   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
241       &iter_array))
242     return FALSE;
243   if (rule->interfaces)
244   {
245     for (i = 0; i < rule->interfaces->len; i++)
246     {
247       char *val = g_array_index (rule->interfaces, gchar *, i);
248       dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING, &val);
249     }
250   }
251   dbus_message_iter_close_container (&iter_struct, &iter_array);
252   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_interfacematchtype);
253
254   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_BOOLEAN, &d_invert);
255
256   dbus_message_iter_close_container (iter, &iter_struct);
257   return TRUE;
258 }