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