Initial pass at adding i18n and proper object disposal
[platform/upstream/at-spi2-core.git] / atspi / atspi-relation.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  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "atspi-private.h"
25
26 /**
27  * atspi_relation_get_relation_type:
28  * @obj: a pointer to the #AtspiRelation object to query.
29  *
30  * Get the type of relationship represented by an #AtspiRelation.
31  *
32  * Returns: an #AtspiRelationType indicating the type of relation
33  *         encapsulated in this #AtspiRelation object.
34  *
35  **/
36 AtspiRelationType
37 atspi_relation_get_relation_type (AtspiRelation *obj)
38 {
39   return obj->relation_type;
40 }
41
42 /**
43  * atspi_relation_get_n_targets:
44  * @obj: a pointer to the #AtspiRelation object to query.
45  *
46  * Get the number of objects which this relationship has as its
47  *       target objects (the subject is the #Accessible from which this
48  *       #AtspiRelation originated).
49  *
50  * Returns: a short integer indicating how many target objects which the
51  *       originating #Accessible object has the #AtspiRelation
52  *       relationship with.
53  **/
54 gint
55 atspi_relation_get_n_targets (AtspiRelation *obj)
56 {
57   return obj->targets->len;
58 }
59
60 /**
61  * atspi_relation_get_target:
62  * @obj: a pointer to the #AtspiRelation object to query.
63  * @i: a (zero-index) integer indicating which (of possibly several) target is requested.
64  *
65  * Get the @i-th target of a specified #AtspiRelation relationship.
66  *
67  * Returns: (transfer full): an #AtspiAccessible which is the @i-th object
68  *          with which the originating #AtspiAccessible has relationship
69  *          specified in the #AtspiRelation object.
70  *
71  **/
72 AtspiAccessible *
73 atspi_relation_get_target (AtspiRelation *obj, gint i)
74 {
75   g_return_val_if_fail (obj, NULL);
76
77   g_return_val_if_fail (i >= 0 && i < obj->targets->len, NULL);
78   return g_object_ref (g_array_index (obj->targets, AtspiAccessible *, i));
79 }
80
81 AtspiRelation *
82 _atspi_relation_new_from_iter (DBusMessageIter *iter)
83 {
84   DBusMessageIter iter_struct, iter_array;
85   dbus_uint32_t d_type;
86   AtspiRelation *relation = g_object_new (ATSPI_TYPE_RELATION, NULL);
87
88   if (!relation)
89     return NULL;
90
91   dbus_message_iter_recurse (iter, &iter_struct);
92   dbus_message_iter_get_basic (&iter_struct, &d_type);
93   relation->relation_type = d_type;
94   dbus_message_iter_next (&iter_struct);
95
96   relation->targets = g_array_new (TRUE, TRUE, sizeof (AtspiAccessible *));
97   dbus_message_iter_recurse (&iter_struct, &iter_array);
98   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
99   {
100     AtspiAccessible *accessible;
101     GArray *new_array;
102     accessible = _atspi_dbus_return_accessible_from_iter (&iter_array);
103     new_array = g_array_append_val (relation->targets, accessible);
104     if (new_array)
105       relation->targets = new_array;
106     /* Iter was moved already, so no need to call dbus_message_iter_next */
107   }
108   return relation;
109 }
110
111 G_DEFINE_TYPE (AtspiRelation, atspi_relation, G_TYPE_OBJECT)
112
113 static void
114 atspi_relation_init (AtspiRelation *relation)
115 {
116 }
117
118 static void
119 atspi_relation_finalize (GObject *object)
120 {
121   AtspiRelation *relation = ATSPI_RELATION (object);
122   gint i;
123
124   for (i = 0; i < relation->targets->len; i++)
125   g_object_unref (g_array_index (relation->targets, AtspiAccessible *, i));
126   g_array_free (relation->targets, TRUE);
127
128   G_OBJECT_CLASS (atspi_relation_parent_class)->finalize (object);
129 }
130
131 static void
132 atspi_relation_class_init (AtspiRelationClass *klass)
133 {
134   GObjectClass *object_class = G_OBJECT_CLASS (klass);
135
136   object_class->finalize = atspi_relation_finalize;
137 }