Improving atspi-matchrule documentation.
[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  *
81  * @states: An #AtspiStateSet specifying the states to match or NULL if none.
82  *
83  * @statematchtype: An #AtspiCollectionMatchType specifying how to interpret
84  *                  @states.
85  *
86  * @attributes: (element-type gchar* gchar*): A #GHashTable specifying
87  *              attributes to match.
88  *
89  * @attributematchtype: An #AtspiCollectionMatchType specifying how to
90  *                      interpret @attributes.
91  *
92  * @interfaces: (element-type gchar*): An array of interfaces to match, or
93  *              NULL if not applicable.  Interface names should be specified
94  *              by their DBus names (org.a11y.Atspi.Accessible,
95  *              org.a11y.Atspi.Component, etc).
96  *
97  * @interfacematchtype: An #AtspiCollectionMatchType specifying how to
98  *                      interpret @interfaces.
99  *
100  * @roles: (element-type AtspiRole): A #GArray of roles to match, or NULL if
101  *         not applicable.
102  *
103  * @rolematchtype: An #AtspiCollectionMatchType specifying how to
104  *                      interpret @roles.
105  *
106  * @invert: if #TRUE, the match rule should be denied (inverted); if #FALSE,
107  * it should not. For example, if the match rule defines that a match is
108  * an object of ROLE_HEADING which has STATE_FOCUSABLE and a click action, 
109  * inverting it would match all objects that are not of ROLE_HEADING, 
110  * focusable and clickable at the same time.  
111  *
112  * Creates a new #AtspiMatchRule with specified @states, @attributes, 
113  * @interfaces, and @roles.
114  *
115  * Returns: (transfer full): A new #AtspiMatchRule.
116  **/
117 AtspiMatchRule *
118 atspi_match_rule_new (AtspiStateSet *states,
119                       AtspiCollectionMatchType statematchtype,
120                       GHashTable *attributes,
121                       AtspiCollectionMatchType attributematchtype,
122                       GArray *roles,
123                       AtspiCollectionMatchType rolematchtype,
124                       GArray *interfaces,
125                       AtspiCollectionMatchType interfacematchtype,
126                       gboolean invert)
127 {
128   AtspiMatchRule *rule = g_object_new (ATSPI_TYPE_MATCH_RULE, NULL);
129   int i;
130
131   if (!rule)
132     return NULL;
133
134   if (states)
135     rule->states = g_object_ref (states);
136   rule->statematchtype = statematchtype;
137
138   if (attributes)
139   {
140     GHashTableIter hash_table_iter;
141     gchar *key, *value;
142     rule->attributes = g_hash_table_new_full (g_str_hash, g_str_equal,
143                                               (GDestroyNotify) g_free,
144                                               (GDestroyNotify) g_free);
145     g_hash_table_iter_init (&hash_table_iter, attributes);
146             while (g_hash_table_iter_next (&hash_table_iter, (gpointer *)&key,
147                    (gpointer *)&value))
148       g_hash_table_insert (rule->attributes, g_strdup (key), g_strdup (value));
149   } else
150     rule->attributes = NULL;
151   rule->attributematchtype = attributematchtype;
152
153   if (interfaces)
154     rule->interfaces = g_array_ref (interfaces);
155   rule->interfacematchtype = interfacematchtype;
156
157   if (roles)
158   {
159     for (i = 0; i < roles->len; i++)
160     {
161       AtspiRole role = g_array_index (roles, AtspiRole, i);
162       if (role < 128)
163         rule->roles [role / 32] |= (1 << (role % 32));
164       else
165         g_warning ("Atspi: unexpected role %d\n", role);
166     }
167   }
168   else
169     rule->roles [0] = rule->roles [1] = 0;
170   rule->rolematchtype = rolematchtype;
171
172   rule->invert = invert;
173
174   return rule;
175 }
176
177 static void
178 append_entry (gpointer key, gpointer val, gpointer data)
179 {
180   DBusMessageIter *iter = data;
181   DBusMessageIter iter_entry;
182
183   if (!dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL,
184                                         &iter_entry))
185     return;
186   dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &key);
187   dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &val);
188   dbus_message_iter_close_container (iter, &iter_entry);
189 }
190
191 gboolean
192 _atspi_match_rule_marshal (AtspiMatchRule *rule, DBusMessageIter *iter)
193 {
194   DBusMessageIter iter_struct, iter_array, iter_dict;
195   dbus_int32_t states [2];
196   dbus_int32_t d_statematchtype = rule->statematchtype;
197   dbus_int32_t d_attributematchtype = rule->attributematchtype;
198   dbus_int32_t d_interfacematchtype = rule->interfacematchtype;
199   dbus_uint32_t d_rolematchtype = rule->rolematchtype;
200   dbus_bool_t d_invert = rule->invert;
201   gint i;
202   dbus_int32_t d_role;
203
204   if (!dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, NULL,
205                                          &iter_struct))
206     return FALSE;
207
208   /* states */
209   if (rule->states)
210   {
211     states [0] = rule->states->states & 0xffffffff;
212     states [1] = rule->states->states >> 32;
213   }
214   else
215   {
216     states [0] = states [1] = 0;
217   }
218   dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "i", &iter_array);
219   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [0]);
220   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [1]);
221   dbus_message_iter_close_container (&iter_struct, &iter_array);
222   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_statematchtype);
223
224   /* attributes */
225   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "{ss}",
226                                          &iter_dict))
227     return FALSE;
228   g_hash_table_foreach (rule->attributes, append_entry, &iter_dict);
229   dbus_message_iter_close_container (&iter_struct, &iter_dict);
230   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_attributematchtype);
231
232   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "i",
233       &iter_array))
234     return FALSE;
235   d_role = rule->roles [0];
236   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
237   d_role = rule->roles [1];
238   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
239   d_role = rule->roles [2];
240   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
241   d_role = rule->roles [3];
242   dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
243   dbus_message_iter_close_container (&iter_struct, &iter_array);
244   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32,
245                                   &d_rolematchtype);
246
247   /* interfaces */
248   if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
249       &iter_array))
250     return FALSE;
251   if (rule->interfaces)
252   {
253     for (i = 0; i < rule->interfaces->len; i++)
254     {
255       char *val = g_array_index (rule->interfaces, gchar *, i);
256       dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_STRING, &val);
257     }
258   }
259   dbus_message_iter_close_container (&iter_struct, &iter_array);
260   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_interfacematchtype);
261
262   dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_BOOLEAN, &d_invert);
263
264   dbus_message_iter_close_container (iter, &iter_struct);
265   return TRUE;
266 }