lib: made API multi thread safe(untested)
[platform/core/appfw/message-port-dbus.git] / lib / msgport-utils.c
1 #include "msgport-utils.h"
2 #include "common/dbus-error.h" /* MsgPortError */
3 #include "common/log.h"
4
5 static void
6 _bundle_iter_cb (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
7 {
8     GVariantBuilder *builder = (GVariantBuilder *)user_data;
9     void *val;
10     size_t size;
11
12     /* NOTE: we support only string values as 
13      * MessagePort API support key,value strings 
14      */
15     if (bundle_keyval_get_type ((bundle_keyval_t*)kv) != BUNDLE_TYPE_STR) return;
16
17     bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, &val, &size);
18
19     g_variant_builder_add (builder, "{sv}", key, g_variant_new_string ((const gchar *)val));
20 }
21
22 GVariant * bundle_to_variant_map (bundle *b)
23 {
24     GVariantBuilder builder;
25
26     g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
27
28     bundle_foreach (b, _bundle_iter_cb, &builder);
29
30     return g_variant_builder_end (&builder);
31 }
32
33 bundle * bundle_from_variant_map (GVariant *v_data)
34 {
35     bundle *b = NULL;
36     GVariantIter iter;
37     gchar *key = NULL;
38     GVariant *value  = NULL;
39
40     if (!v_data) return b;
41
42     g_variant_iter_init (&iter, v_data);
43
44     b = bundle_create ();
45
46     while (g_variant_iter_next (&iter, "{sv}", &key, &value)) {
47         bundle_add (b, key, g_variant_get_string (value, NULL));
48         g_free (key);
49         g_variant_unref (value);
50     }
51
52     return b;
53 }
54
55 messageport_error_e
56 msgport_daemon_error_to_error (const GError *error)
57 {
58     if (!error) return MESSAGEPORT_ERROR_NONE;
59
60     switch (error->code) {
61         case MSGPORT_ERROR_OUT_OF_MEMORY:
62             return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
63         case MSGPORT_ERROR_NOT_FOUND:
64             return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
65         case MSGPORT_ERROR_INVALID_PARAMS:
66             return MESSAGEPORT_ERROR_INVALID_PARAMETER;
67         case MSGPORT_ERROR_CERTIFICATE_MISMATCH:
68             return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
69         case MSGPORT_ERROR_UNKNOWN:
70         case MSGPORT_ERROR_IO_ERROR:
71             return MESSAGEPORT_ERROR_IO_ERROR;
72     }
73
74     return MESSAGEPORT_ERROR_IO_ERROR;
75 }