Add framework for configuration files
[platform/upstream/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 <glib.h>
27
28 #include "connman.h"
29
30 struct connman_config {
31         char *ident;
32         char *name;
33         char *description;
34 };
35
36 static GHashTable *config_hash = NULL;
37
38 static int load_config(struct connman_config *config)
39 {
40         GKeyFile *keyfile;
41         char *str;
42
43         DBG("config %p", config);
44
45         keyfile = __connman_storage_open_config(config->ident);
46         if (keyfile == NULL)
47                 return -EIO;
48
49         str = g_key_file_get_string(keyfile, "global", "Name", NULL);
50         if (str != NULL) {
51                 g_free(config->name);
52                 config->name = str;
53         }
54
55         str = g_key_file_get_string(keyfile, "global", "Description", NULL);
56         if (str != NULL) {
57                 g_free(config->description);
58                 config->description = str;
59         }
60
61         __connman_storage_close_config(config->ident, keyfile, FALSE);
62
63         return 0;
64 }
65
66 static void free_config(struct connman_config *config)
67 {
68         g_free(config->description);
69         g_free(config->name);
70         g_free(config->ident);
71         g_free(config);
72 }
73
74 static void unregister_config(gpointer data)
75 {
76         struct connman_config *config = data;
77
78         connman_info("Removing configuration %s", config->ident);
79
80         free_config(config);
81 }
82
83 static int create_config(const char *ident)
84 {
85         struct connman_config *config;
86
87         DBG("ident %s", ident);
88
89         config = g_try_new0(struct connman_config, 1);
90         if (config == NULL)
91                 return -ENOMEM;
92
93         config->ident = g_strdup(ident);
94
95         if (config->ident == NULL) {
96                 free_config(config);
97                 return -ENOMEM;
98         }
99
100         if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
101                 free_config(config);
102                 return -EEXIST;
103         }
104
105         g_hash_table_insert(config_hash, g_strdup(config->ident), config);
106
107         connman_info("Adding configuration %s", config->ident);
108
109         load_config(config);
110
111         return 0;
112 }
113
114 static int config_init(void)
115 {
116         GDir *dir;
117         const gchar *file;
118
119         DBG("");
120
121         dir = g_dir_open(STORAGEDIR, 0, NULL);
122         if (dir != NULL) {
123                 while ((file = g_dir_read_name(dir)) != NULL) {
124                         GString *str;
125                         gchar *ident;
126
127                         if (g_str_has_suffix(file, ".config") == FALSE)
128                                 continue;
129
130                         ident = g_strrstr(file, ".config");
131                         if (ident == NULL)
132                                 continue;
133
134                         str = g_string_new_len(file, ident - file);
135                         if (str == NULL)
136                                 continue;
137
138                         ident = g_string_free(str, FALSE);
139
140                         if (connman_dbus_validate_ident(ident) == TRUE)
141                                 create_config(ident);
142
143                         g_free(ident);
144                 }
145
146                 g_dir_close(dir);
147         }
148
149         return 0;
150 }
151
152 int __connman_config_init(void)
153 {
154         DBG("");
155
156         config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
157                                                 g_free, unregister_config);
158
159         return config_init();
160 }
161
162 void __connman_config_cleanup(void)
163 {
164         DBG("");
165
166         g_hash_table_destroy(config_hash);
167         config_hash = NULL;
168 }