Store element properties in key files
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  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 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  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "connman.h"
27
28 int __connman_storage_init(void)
29 {
30         DBG("");
31
32         return 0;
33 }
34
35 void __connman_storage_cleanup(void)
36 {
37         DBG("");
38 }
39
40 int __connman_element_load(struct connman_element *element)
41 {
42         DBG("element %p name %s", element, element->name);
43
44         return 0;
45 }
46
47 static void do_update(GKeyFile *keyfile, struct connman_element *element)
48 {
49         GSList *list;
50
51         DBG("element %p name %s", element, element->name);
52
53         g_key_file_set_string(keyfile, element->path, "Name", element->name);
54
55         g_key_file_set_boolean(keyfile, element->path, "Enabled",
56                                                         element->enabled);
57
58         connman_element_lock(element);
59
60         for (list = element->properties; list; list = list->next) {
61                 struct connman_property *property = list->data;
62
63                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
64                         continue;
65
66                 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
67                         continue;
68
69                 if (property->type == DBUS_TYPE_STRING)
70                         g_key_file_set_string(keyfile, element->path,
71                                         property->name, property->value);
72         }
73
74         connman_element_unlock(element);
75 }
76
77 int __connman_element_store(struct connman_element *element)
78 {
79         GKeyFile *keyfile;
80         gchar *pathname, *data = NULL;
81         gsize length;
82
83         DBG("element %p name %s", element, element->name);
84
85         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
86         if (pathname == NULL)
87                 return -ENOMEM;
88
89         keyfile = g_key_file_new();
90
91         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
92                 goto update;
93
94         if (length > 0) {
95                 if (g_key_file_load_from_data(keyfile, data, length,
96                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
97                         goto done;
98         }
99
100         g_free(data);
101
102 update:
103         do_update(keyfile, element);
104
105         data = g_key_file_to_data(keyfile, &length, NULL);
106
107         g_file_set_contents(pathname, data, length, NULL);
108
109 done:
110         g_free(data);
111
112         g_key_file_free(keyfile);
113
114         g_free(pathname);
115
116         return 0;
117 }