Updated Finnish translation
[rhythmbox.git] / metadata / rb-metadata-dbus.c
blobcd64fdd7dfe30cb53c0f377851211733cb0feba9
1 /*
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.
24 #include <config.h>
25 #include <glib.h>
27 #include "rb-metadata.h"
28 #include "rb-metadata-dbus.h"
29 #include "rb-debug.h"
31 static gboolean
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));
37 return FALSE;
39 dbus_message_iter_get_basic (iter, value);
40 dbus_message_iter_next (iter);
41 return TRUE;
44 gboolean
45 rb_metadata_dbus_get_boolean (DBusMessageIter *iter, gboolean *value)
47 return _get_basic_checked (iter, value, DBUS_TYPE_BOOLEAN);
50 gboolean
51 rb_metadata_dbus_get_uint32 (DBusMessageIter *iter, guint32 *value)
53 return _get_basic_checked (iter, value, DBUS_TYPE_UINT32);
56 gboolean
57 rb_metadata_dbus_get_string (DBusMessageIter *iter, gchar **value)
59 gchar *msg_value;
60 if (!_get_basic_checked (iter, &msg_value, DBUS_TYPE_STRING))
61 return FALSE;
62 *value = g_strdup (msg_value);
63 return TRUE;
66 gboolean
67 rb_metadata_dbus_add_to_message (RBMetaData *md, DBusMessageIter *iter)
69 DBusMessageIter a_iter;
70 RBMetaDataField field;
71 const char *etype =
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)) {
77 return FALSE;
80 for (field = RB_METADATA_FIELD_TITLE; field < RB_METADATA_FIELD_LAST; field++) {
81 GType vtype = rb_metadata_get_field_type (field);
82 GValue v = {0,};
83 DBusMessageIter d_iter;
84 DBusMessageIter v_iter;
85 const char *v_sig = NULL;
87 if (!rb_metadata_get (md, field, &v))
88 continue;
90 if (!dbus_message_iter_open_container (&a_iter, DBUS_TYPE_DICT_ENTRY, NULL, &d_iter)) {
91 return FALSE;
94 if (!dbus_message_iter_append_basic (&d_iter, DBUS_TYPE_UINT32, &field)) {
95 return FALSE;
98 switch (vtype) {
99 case G_TYPE_ULONG:
100 v_sig = DBUS_TYPE_UINT32_AS_STRING;
101 break;
102 case G_TYPE_DOUBLE:
103 v_sig = DBUS_TYPE_DOUBLE_AS_STRING;
104 break;
105 case G_TYPE_STRING:
106 v_sig = DBUS_TYPE_STRING_AS_STRING;
107 break;
109 if (!dbus_message_iter_open_container (&d_iter, DBUS_TYPE_VARIANT, v_sig, &v_iter)) {
110 return FALSE;
113 /* not exactly stolen from dbus-gvalue.c */
114 switch (vtype) {
115 case G_TYPE_ULONG:
117 dbus_uint32_t n = g_value_get_ulong (&v);
118 if (!dbus_message_iter_append_basic (&v_iter, DBUS_TYPE_UINT32, &n)) {
119 return FALSE;
121 break;
124 case G_TYPE_DOUBLE:
126 double n = g_value_get_double (&v);
127 if (!dbus_message_iter_append_basic (&v_iter, DBUS_TYPE_DOUBLE, &n)) {
128 return FALSE;
130 break;
133 case G_TYPE_STRING:
135 const char *s = g_value_get_string (&v);
136 if (!s)
137 s = "";
138 if (!dbus_message_iter_append_basic (&v_iter, DBUS_TYPE_STRING, &s)) {
139 return FALSE;
141 break;
144 default:
145 g_assert_not_reached ();
146 break;
149 g_value_unset (&v);
150 if (!dbus_message_iter_close_container (&d_iter, &v_iter)) {
151 return FALSE;
154 if (!dbus_message_iter_close_container (&a_iter, &d_iter)) {
155 return FALSE;
159 if (!dbus_message_iter_close_container (iter, &a_iter)) {
160 return FALSE;
163 return TRUE;
166 gboolean
167 rb_metadata_dbus_read_from_message (RBMetaData *md, GHashTable *metadata, DBusMessageIter *iter)
169 DBusMessageIter a_iter;
170 int current_type;
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));
175 return FALSE;
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);
183 return FALSE;
186 while (current_type != DBUS_TYPE_INVALID) {
187 DBusMessageIter e_iter;
188 DBusMessageIter v_iter;
189 RBMetaDataField field;
190 GValue *val;
192 dbus_message_iter_recurse (&a_iter, &e_iter);
194 if (!rb_metadata_dbus_get_uint32 (&e_iter, &field)) {
195 return FALSE;
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));
201 return FALSE;
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:
209 dbus_uint32_t n;
210 dbus_message_iter_get_basic (&v_iter, &n);
211 g_value_init (val, G_TYPE_ULONG);
212 g_value_set_ulong (val, n);
213 break;
216 case DBUS_TYPE_DOUBLE:
218 double n;
219 dbus_message_iter_get_basic (&v_iter, &n);
220 g_value_init (val, G_TYPE_DOUBLE);
221 g_value_set_double (val, n);
222 break;
225 case DBUS_TYPE_STRING:
227 const gchar *n;
228 dbus_message_iter_get_basic (&v_iter, &n);
229 g_value_init (val, G_TYPE_STRING);
230 g_value_set_string (val, n);
231 break;
234 default:
235 g_assert_not_reached ();
236 break;
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);
245 return TRUE;