1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * gsf-docprop-vector.c: A type implementing OLE Document Property vectors using a GValueArray
4 *
5 * Copyright (C) 2004-2006 Frank Chiulli (fc-linux@cox.net)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 #include <gsf-config.h>
23 #include <gsf/gsf-docprop-vector.h>
24 #include <gsf/gsf.h>
25
26 /* TODO: Drop GValueArray when breaking API */
27
28 struct _GsfDocPropVector {
29 GObject parent;
30
31 GArray *ga;
32 GValueArray *gva;
33 };
34 typedef GObjectClass GsfDocPropVectorClass;
35
36 #define GSF_DOCPROP_VECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), GSF_DOCPROP_VECTOR_TYPE, GsfDocPropVectorClass))
37 #define GSF_DOCPROP_VECTOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GSF_DOCPROP_VECTOR_TYPE, GsfDocPropVectorClass))
38 #define IS_GSF_DOCPROP_VECTOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), GSF_DOCPROP_VECTOR_TYPE))
39
40 static GObjectClass *parent_class;
41
42 GValueArray *
gsf_value_get_docprop_varray(GValue const * value)43 gsf_value_get_docprop_varray (GValue const *value)
44 {
45 GsfDocPropVector *v = gsf_value_get_docprop_vector (value);
46 return v ? v->gva : NULL;
47 }
48
49 /**
50 * gsf_value_get_docprop_array:
51 * @value: A GValue of type #GsfDocPropVector.
52 *
53 * This function returns the array of values inside #GsfDocPropVector.
54 * No additional references are created.
55 *
56 * Returns: (transfer none) (element-type GValue) (nullable): A #GArray
57 * of #GValue
58 **/
59 GArray *
gsf_value_get_docprop_array(GValue const * value)60 gsf_value_get_docprop_array (GValue const *value)
61 {
62 GsfDocPropVector *v = gsf_value_get_docprop_vector (value);
63 return v ? v->ga : NULL;
64 }
65
66 /**
67 * gsf_value_get_docprop_vector:
68 * @value: A GValue of type #GsfDocPropVector.
69 *
70 * This function returns a pointer to the GsfDocPropVector structure in @value.
71 * No additional references are created.
72 *
73 * Returns: (transfer none): A pointer to the #GsfDocPropVector structure in @value
74 **/
75 GsfDocPropVector *
gsf_value_get_docprop_vector(GValue const * value)76 gsf_value_get_docprop_vector (GValue const *value)
77 {
78 g_return_val_if_fail (VAL_IS_GSF_DOCPROP_VECTOR (value), NULL);
79
80 return (GsfDocPropVector *) g_value_get_object (value);
81 }
82
83 /**
84 * gsf_docprop_vector_append:
85 * @vector: The vector to which the GValue will be added
86 * @value: The GValue to add to @vector
87 *
88 * Insert a copy of @value as the last element of @vector.
89 **/
90 void
gsf_docprop_vector_append(GsfDocPropVector * vector,GValue * value)91 gsf_docprop_vector_append (GsfDocPropVector *vector, GValue *value)
92 {
93 g_return_if_fail (vector != NULL);
94 g_return_if_fail (value != NULL);
95
96 if (G_IS_VALUE (value)) {
97 GValue val = G_VALUE_INIT;
98
99 g_value_init (&val, G_VALUE_TYPE (value));
100 g_value_copy (value, &val);
101 g_array_append_vals (vector->ga, &val, 1);
102
103 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
104 vector->gva = g_value_array_append (vector->gva, value);
105 G_GNUC_END_IGNORE_DEPRECATIONS
106 }
107 }
108
109 /**
110 * gsf_docprop_vector_as_string:
111 * @vector: The #GsfDocPropVector from which GValues will be extracted.
112 *
113 * This function returns a string which represents all the GValues in @vector.
114 * The caller is responsible for freeing the result.
115 *
116 * Returns (transfer full): a string of comma-separated values
117 **/
118 gchar*
gsf_docprop_vector_as_string(GsfDocPropVector const * vector)119 gsf_docprop_vector_as_string (GsfDocPropVector const *vector)
120 {
121 gchar *rstring;
122
123 guint i;
124 guint num_values;
125
126 g_return_val_if_fail (vector != NULL, NULL);
127 g_return_val_if_fail (vector->ga != NULL, NULL);
128
129 rstring = g_new0 (gchar, 1);
130 num_values = vector->ga->len;
131
132 for (i = 0; i < num_values; i++) {
133 char *str;
134 GValue *v;
135
136 v = &g_array_index (vector->ga, GValue, i);
137 str = g_strdup_value_contents (v);
138 rstring = g_strconcat (rstring, str, ",", NULL);
139 g_free (str);
140 }
141
142 return rstring;
143 }
144
145 static void
gsf_docprop_vector_finalize(GObject * obj)146 gsf_docprop_vector_finalize (GObject *obj)
147 {
148 GsfDocPropVector *vector = (GsfDocPropVector *) obj;
149 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
150 if (vector->gva != NULL) {
151 g_value_array_free (vector->gva);
152 vector->gva = NULL;
153 }
154 G_GNUC_END_IGNORE_DEPRECATIONS
155 g_clear_pointer (&vector->ga, g_array_unref);
156 parent_class->finalize (obj);
157 }
158
159 static void
gsf_docprop_vector_class_init(GObjectClass * gobject_class)160 gsf_docprop_vector_class_init (GObjectClass *gobject_class)
161 {
162 parent_class = g_type_class_peek (G_TYPE_OBJECT);
163 gobject_class->finalize = gsf_docprop_vector_finalize;
164 }
165
166 static void
gsf_docprop_vector_init(GsfDocPropVector * vector)167 gsf_docprop_vector_init (GsfDocPropVector *vector)
168 {
169 vector->ga = g_array_sized_new (FALSE, TRUE, sizeof (GValue), 0);
170 g_array_set_clear_func (vector->ga, (GDestroyNotify) g_value_unset);
171 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
172 vector->gva = g_value_array_new (0);
173 G_GNUC_END_IGNORE_DEPRECATIONS
174 }
175
GSF_CLASS(GsfDocPropVector,gsf_docprop_vector,gsf_docprop_vector_class_init,gsf_docprop_vector_init,G_TYPE_OBJECT)176 GSF_CLASS (GsfDocPropVector, gsf_docprop_vector,
177 gsf_docprop_vector_class_init, gsf_docprop_vector_init,
178 G_TYPE_OBJECT)
179
180 /**
181 * gsf_docprop_vector_new:
182 *
183 * This function creates a new gsf_docprop_vector object.
184 *
185 * Returns: GsfDocPropVector*
186 **/
187 GsfDocPropVector*
188 gsf_docprop_vector_new (void)
189 {
190 return g_object_new (GSF_DOCPROP_VECTOR_TYPE, NULL);
191 }
192