Merge branch 'upstream' into maemo
[navit-package] / navit / config_.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19
20 #include <stdlib.h>
21 #include <glib.h>
22 #include "debug.h"
23 #include "item.h"
24 #include "callback.h"
25 #include "config_.h"
26
27 struct config {
28         struct attr **attrs;
29         struct callback_list *cbl;
30 } *config;
31
32 int config_empty_ok;
33
34 struct attr_iter {
35         void *iter;
36 };
37
38 void
39 config_destroy(struct config *this_)
40 {
41         attr_list_free(this_->attrs);
42         callback_list_destroy(this_->cbl);
43         g_free(config);
44         exit(0);
45 }
46
47 int
48 config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
49 {
50         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
51 }
52
53 static int
54 config_set_attr_int(struct config *this_, struct attr *attr)
55 {
56         switch (attr->type) {
57         case attr_language:
58                 setenv("LANG",attr->u.str,1);
59                 return 1;
60         default:
61                 return 0;
62         }
63 }
64
65 int
66 config_set_attr(struct config *this_, struct attr *attr)
67 {
68         return config_set_attr_int(this_, attr);
69 }
70
71 int
72 config_add_attr(struct config *this_, struct attr *attr)
73 {
74         switch (attr->type) {
75         case attr_callback:
76                 callback_list_add(this_->cbl, attr->u.callback);
77         default:        
78                 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
79         }
80         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
81         return 1;
82 }
83
84 int
85 config_remove_attr(struct config *this_, struct attr *attr)
86 {
87         switch (attr->type) {
88         case attr_callback:
89                 callback_list_remove(this_->cbl, attr->u.callback);
90         default:        
91                 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
92         }
93         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
94         return 1;
95 }
96
97 struct attr_iter *
98 config_attr_iter_new()
99 {
100         return g_new0(struct attr_iter, 1);
101 }
102
103 void
104 config_attr_iter_destroy(struct attr_iter *iter)
105 {
106         g_free(iter);
107 }
108
109
110 struct config *
111 config_new(struct attr *parent, struct attr **attrs)
112 {
113         if (config) {
114                 dbg(0,"only one config allowed\n");
115                 return NULL;
116         }
117         if (parent) {
118                 dbg(0,"no parent in config allowed\n");
119                 return NULL;
120         }
121         config=g_new0(struct config, 1);
122         config->attrs=attr_list_dup(attrs);
123         config->cbl=callback_list_new();
124         while (*attrs) {
125                 if (!config_set_attr_int(config,*attrs)) {
126                         dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
127                         config_destroy(config);
128                         config=NULL;
129                         break;
130                 }
131                 attrs++;
132         }
133         return config;
134 }