summaryrefslogtreecommitdiff
path: root/atspi/atspi-matchrule.c
blob: e867d0ea345b4e4b87e2571ebd3cba29cba1f27d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "atspi-private.h"

G_DEFINE_TYPE (AtspiMatchRule, atspi_match_rule, G_TYPE_OBJECT)

static void
atspi_match_rule_init (AtspiMatchRule *match_rule)
{
}

static void
atspi_match_rule_finalize (GObject *obj)
{
  AtspiMatchRule *rule = ATSPI_MATCH_RULE (obj);

  if (rule->states)
    g_object_unref (rule->states);
  if (rule->attributes)
    g_hash_table_unref (rule->attributes);
  if (rule->roles)
    g_array_free (rule->roles, TRUE);
  /* TODO: Check that interfaces don't leak */
  if (rule->interfaces)
    g_array_free (rule->interfaces, TRUE);
}

static void
atspi_match_rule_class_init (AtspiMatchRuleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = atspi_match_rule_finalize;
}

/**
 * atspi_match_rule_new:
 *
 * @states: An #AtspiStateSet specifying the states to match or NULL if none.
 * @statematchtype: An #AtspiCollectionMatchType specifying how to interpret
 *                  @states.
 * @attributes: (element-type gchar* gchar*): A #GHashTable specifying
 *              attributes to match.
 * @attributematchtype: An #AtspiCollectionMatchType specifying how to
 *                      interpret @attributes.
 * @roles: (element-type AtspiRole): A #GArray of roles to match, or NULL if
 *         not applicable.
 * @interfaces: (element-type gchar*): An array of interfaces to match, or
 *              NUL if not appliable.  Interface names should be specified
 *              by their DBus names (org.a11y.Atspi.Accessible,
 *              org.a11y.Atspi.Component, etc).
 * @interfacematchtype: An #AtspiCollectionMatchType specifying how to
 *                      interpret @interfaces.
 * @invert: Specifies whether results should be inverted.
 * TODO: Document this parameter better.
 *
 * Returns: (transfer full): A new #AtspiMatchRule.
 */
AtspiMatchRule *
atspi_match_rule_new (AtspiStateSet *states,
                      AtspiCollectionMatchType statematchtype,
                      GHashTable *attributes,
                      AtspiCollectionMatchType attributematchtype,
                      GArray *roles,
                      AtspiCollectionMatchType rolematchtype,
                      gboolean invert)
{
  AtspiMatchRule *rule = g_object_new (ATSPI_TYPE_MATCH_RULE, NULL);

  if (!rule)
    return NULL;

  if (states)
    rule->states = g_object_ref (states);
  rule->statematchtype = statematchtype;

  if (attributes)
    rule->attributes = g_object_ref (attributes);
    rule->attributematchtype = attributematchtype;

  if (roles)
    rule->roles = g_array_ref(roles);
  rule->rolematchtype = rolematchtype;

  rule->invert = invert;

  return rule;
}

static void
append_entry (gpointer *key, gpointer *val, gpointer data)
{
  DBusMessageIter *iter = data;
  DBusMessageIter iter_entry;

  if (!dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL,
                                        &iter_entry))
    return;
  dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &key);
  dbus_message_iter_append_basic (&iter_entry, DBUS_TYPE_STRING, &val);
  dbus_message_iter_close_container (iter, &iter_entry);
}

gboolean
_atspi_match_rule_marshal (AtspiMatchRule *rule, DBusMessageIter *iter)
{
  DBusMessageIter iter_struct, iter_array, iter_dict;
  dbus_int32_t states [2];
  dbus_int32_t d_statematchtype = rule->statematchtype;
  dbus_int32_t d_attributematchtype = rule->attributematchtype;
  dbus_int32_t d_interfacematchtype = rule->interfacematchtype;
  dbus_uint32_t d_rolematchtype = rule->rolematchtype;
  dbus_bool_t d_invert = rule->invert;
  gint i;

  if (!dbus_message_iter_open_container (iter, DBUS_TYPE_STRUCT, "(aiiasiaiisib)",
                                         &iter_struct))
    return FALSE;

  /* states */
  if (rule->states)
  {
    states [0] = rule->states->states & 0xffffffff;
    states [1] = rule->states->states >> 32;
  }
  else
  {
    states [0] = states [1] = 0;
  }
  dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "u", &iter_array);
  dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [0]);
  dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &states [1]);
  dbus_message_iter_close_container (&iter_struct, &iter_array);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_statematchtype);

  /* attributes */
  if (!dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{ss}",
                                         &iter_dict))
    return FALSE;
  g_hash_table_foreach (rule->attributes, append_entry, &iter_dict);
  dbus_message_iter_close_container (iter, &iter_dict);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32, &d_attributematchtype);

  /* roles */
  if (!dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "i",
      &iter_array))
    return FALSE;
  if (rule->roles)
  {
    for (i = 0; i < rule->roles->len; i++)
    {
      dbus_int32_t d_role = g_array_index (rule->roles, AtspiRole, i);
      dbus_message_iter_append_basic (&iter_array, DBUS_TYPE_INT32, &d_role);
    }
  }
  dbus_message_iter_close_container (&iter_struct, &iter_array);
  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_INT32,
                                  &d_rolematchtype);

  dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_BOOLEAN, &d_invert);

  dbus_message_iter_close_container (iter, &iter_struct);
  return TRUE;
}