2 * Copyright (C) 2006 Jonathan Matthew <jonathan@kaolin.hn.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Common junk for the out-of-process metadata reader.
27 #include "rb-metadata.h"
28 #include "rb-metadata-dbus.h"
32 _get_basic_checked (DBusMessageIter
*iter
, gpointer value
, int type
)
34 if (dbus_message_iter_get_arg_type (iter
) != type
) {
35 rb_debug ("Expected D-BUS type '%c', got '%c'",
36 type
, dbus_message_iter_get_arg_type (iter
));
39 dbus_message_iter_get_basic (iter
, value
);
40 dbus_message_iter_next (iter
);
45 rb_metadata_dbus_get_boolean (DBusMessageIter
*iter
, gboolean
*value
)
47 return _get_basic_checked (iter
, value
, DBUS_TYPE_BOOLEAN
);
51 rb_metadata_dbus_get_uint32 (DBusMessageIter
*iter
, guint32
*value
)
53 return _get_basic_checked (iter
, value
, DBUS_TYPE_UINT32
);
57 rb_metadata_dbus_get_string (DBusMessageIter
*iter
, gchar
**value
)
60 if (!_get_basic_checked (iter
, &msg_value
, DBUS_TYPE_STRING
))
62 *value
= g_strdup (msg_value
);
67 rb_metadata_dbus_add_to_message (RBMetaData
*md
, DBusMessageIter
*iter
)
69 DBusMessageIter a_iter
;
70 RBMetaDataField field
;
72 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
73 DBUS_TYPE_UINT32_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
74 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
;
75 rb_debug ("opening container type %s", etype
);
76 if (!dbus_message_iter_open_container (iter
, DBUS_TYPE_ARRAY
, etype
, &a_iter
)) {
80 for (field
= RB_METADATA_FIELD_TITLE
; field
< RB_METADATA_FIELD_LAST
; field
++) {
81 GType vtype
= rb_metadata_get_field_type (field
);
83 DBusMessageIter d_iter
;
84 DBusMessageIter v_iter
;
85 const char *v_sig
= NULL
;
87 if (!rb_metadata_get (md
, field
, &v
))
90 if (!dbus_message_iter_open_container (&a_iter
, DBUS_TYPE_DICT_ENTRY
, NULL
, &d_iter
)) {
94 if (!dbus_message_iter_append_basic (&d_iter
, DBUS_TYPE_UINT32
, &field
)) {
100 v_sig
= DBUS_TYPE_UINT32_AS_STRING
;
103 v_sig
= DBUS_TYPE_DOUBLE_AS_STRING
;
106 v_sig
= DBUS_TYPE_STRING_AS_STRING
;
109 if (!dbus_message_iter_open_container (&d_iter
, DBUS_TYPE_VARIANT
, v_sig
, &v_iter
)) {
113 /* not exactly stolen from dbus-gvalue.c */
117 dbus_uint32_t n
= g_value_get_ulong (&v
);
118 if (!dbus_message_iter_append_basic (&v_iter
, DBUS_TYPE_UINT32
, &n
)) {
126 double n
= g_value_get_double (&v
);
127 if (!dbus_message_iter_append_basic (&v_iter
, DBUS_TYPE_DOUBLE
, &n
)) {
135 const char *s
= g_value_get_string (&v
);
138 if (!dbus_message_iter_append_basic (&v_iter
, DBUS_TYPE_STRING
, &s
)) {
145 g_assert_not_reached ();
150 if (!dbus_message_iter_close_container (&d_iter
, &v_iter
)) {
154 if (!dbus_message_iter_close_container (&a_iter
, &d_iter
)) {
159 if (!dbus_message_iter_close_container (iter
, &a_iter
)) {
167 rb_metadata_dbus_read_from_message (RBMetaData
*md
, GHashTable
*metadata
, DBusMessageIter
*iter
)
169 DBusMessageIter a_iter
;
172 if (dbus_message_iter_get_arg_type (iter
) != DBUS_TYPE_ARRAY
) {
173 rb_debug ("Expected D-BUS array, got type '%c'",
174 dbus_message_iter_get_arg_type (iter
));
178 dbus_message_iter_recurse (iter
, &a_iter
);
180 current_type
= dbus_message_iter_get_arg_type (&a_iter
);
181 if (current_type
!= DBUS_TYPE_INVALID
&& current_type
!= DBUS_TYPE_DICT_ENTRY
) {
182 rb_debug ("Expected D-BUS dict entry, got type '%c'", (guchar
) current_type
);
186 while (current_type
!= DBUS_TYPE_INVALID
) {
187 DBusMessageIter e_iter
;
188 DBusMessageIter v_iter
;
189 RBMetaDataField field
;
192 dbus_message_iter_recurse (&a_iter
, &e_iter
);
194 if (!rb_metadata_dbus_get_uint32 (&e_iter
, &field
)) {
198 if (dbus_message_iter_get_arg_type (&e_iter
) != DBUS_TYPE_VARIANT
) {
199 rb_debug ("Expected D-BUS variant type for value; got type '%c'",
200 dbus_message_iter_get_arg_type (&e_iter
));
204 dbus_message_iter_recurse (&e_iter
, &v_iter
);
205 val
= g_new0 (GValue
, 1);
206 switch (dbus_message_iter_get_arg_type (&v_iter
)) {
207 case DBUS_TYPE_UINT32
:
210 dbus_message_iter_get_basic (&v_iter
, &n
);
211 g_value_init (val
, G_TYPE_ULONG
);
212 g_value_set_ulong (val
, n
);
216 case DBUS_TYPE_DOUBLE
:
219 dbus_message_iter_get_basic (&v_iter
, &n
);
220 g_value_init (val
, G_TYPE_DOUBLE
);
221 g_value_set_double (val
, n
);
225 case DBUS_TYPE_STRING
:
228 dbus_message_iter_get_basic (&v_iter
, &n
);
229 g_value_init (val
, G_TYPE_STRING
);
230 g_value_set_string (val
, n
);
235 g_assert_not_reached ();
239 g_hash_table_insert (metadata
, GINT_TO_POINTER (field
), val
);
241 dbus_message_iter_next (&a_iter
);
242 current_type
= dbus_message_iter_get_arg_type (&a_iter
);