Bump release : v1.0.1
[profile/ivi/message-port.git] / lib / msgport-utils.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "msgport-utils.h"
27 #include "common/dbus-error.h" /* MsgPortError */
28 #include "common/log.h"
29
30 static void
31 _bundle_iter_cb (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
32 {
33     GVariantBuilder *builder = (GVariantBuilder *)user_data;
34     void *val;
35     size_t size;
36
37     /* NOTE: we support only string values as 
38      * MessagePort API support key,value strings 
39      */
40     if (bundle_keyval_get_type ((bundle_keyval_t*)kv) != BUNDLE_TYPE_STR) return;
41
42     bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, &val, &size);
43
44     g_variant_builder_add (builder, "{sv}", key, g_variant_new_string ((const gchar *)val));
45 }
46
47 GVariant * bundle_to_variant_map (bundle *b)
48 {
49     GVariantBuilder builder;
50
51     g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
52
53     bundle_foreach (b, _bundle_iter_cb, &builder);
54
55     return g_variant_builder_end (&builder);
56 }
57
58 bundle * bundle_from_variant_map (GVariant *v_data)
59 {
60     bundle *b = NULL;
61     GVariantIter iter;
62     gchar *key = NULL;
63     GVariant *value  = NULL;
64
65     if (!v_data) return b;
66
67     g_variant_iter_init (&iter, v_data);
68
69     b = bundle_create ();
70
71     while (g_variant_iter_next (&iter, "{sv}", &key, &value)) {
72         bundle_add (b, key, g_variant_get_string (value, NULL));
73         g_free (key);
74         g_variant_unref (value);
75     }
76
77     return b;
78 }
79
80 messageport_error_e
81 msgport_daemon_error_to_error (const GError *error)
82 {
83     if (!error) return MESSAGEPORT_ERROR_NONE;
84
85     switch (error->code) {
86         case MSGPORT_ERROR_OUT_OF_MEMORY:
87             return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
88         case MSGPORT_ERROR_NOT_FOUND:
89             return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
90         case MSGPORT_ERROR_INVALID_PARAMS:
91             return MESSAGEPORT_ERROR_INVALID_PARAMETER;
92         case MSGPORT_ERROR_CERTIFICATE_MISMATCH:
93             return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
94         case MSGPORT_ERROR_UNKNOWN:
95         case MSGPORT_ERROR_IO_ERROR:
96             return MESSAGEPORT_ERROR_IO_ERROR;
97     }
98
99     return MESSAGEPORT_ERROR_IO_ERROR;
100 }