224f476ab397a7450e39c413a2e2abcea1ea09fc
[profile/ivi/navit.git] / navit / 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 <signal.h>
23 #include "debug.h"
24 #include "item.h"
25 #include "callback.h"
26 #include "navit.h"
27 #include "config_.h"
28 #ifdef HAVE_API_WIN32_CE
29 #include "libc.h"
30 #endif
31
32 struct config {
33         struct attr **attrs;
34         struct callback_list *cbl;
35 } *config;
36
37 struct config *
38 config_get(void)
39 {
40         return config;
41 }
42
43 int config_empty_ok;
44
45 static int configured;
46
47 struct attr_iter {
48         void *iter;
49 };
50
51 void
52 config_destroy(struct config *this_)
53 {
54         attr_list_free(this_->attrs);
55         callback_list_destroy(this_->cbl);
56         g_free(config);
57         exit(0);
58 }
59
60 static void
61 config_terminate(int sig)
62 {
63         dbg(0,"terminating\n");
64         config_destroy(config);
65 }
66
67 static void
68 config_new_int(void)
69 {
70         config=g_new0(struct config, 1);
71         config->cbl=callback_list_new();
72 #ifndef HAVE_API_WIN32_CE
73         signal(SIGTERM, config_terminate);
74 #ifndef HAVE_API_WIN32
75 #ifndef __MINGW32__
76         signal(SIGPIPE, SIG_IGN);
77 #endif /* __MINGW32__ */
78 #endif
79 #endif
80 }
81
82 int
83 config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
84 {
85         return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
86 }
87
88 static int
89 config_set_attr_int(struct config *this_, struct attr *attr)
90 {
91         switch (attr->type) {
92         case attr_language:
93                 setenv("LANG",attr->u.str,1);
94                 return 1;
95         default:
96                 return 0;
97         }
98 }
99
100 int
101 config_set_attr(struct config *this_, struct attr *attr)
102 {
103         return config_set_attr_int(this_, attr);
104 }
105
106 int
107 config_add_attr(struct config *this_, struct attr *attr)
108 {
109         if (!config) {
110                 config_new_int();
111                 this_=config;
112         }
113         switch (attr->type) {
114         case attr_callback:
115                 callback_list_add(this_->cbl, attr->u.callback);
116         default:        
117                 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
118         }
119         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
120         return 1;
121 }
122
123 int
124 config_remove_attr(struct config *this_, struct attr *attr)
125 {
126         switch (attr->type) {
127         case attr_callback:
128                 callback_list_remove(this_->cbl, attr->u.callback);
129         default:        
130                 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
131         }
132         callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
133         return 1;
134 }
135
136 struct attr_iter *
137 config_attr_iter_new()
138 {
139         return g_new0(struct attr_iter, 1);
140 }
141
142 void
143 config_attr_iter_destroy(struct attr_iter *iter)
144 {
145         g_free(iter);
146 }
147
148
149 struct config *
150 config_new(struct attr *parent, struct attr **attrs)
151 {
152         if (configured) {
153                 dbg(0,"only one config allowed\n");
154                 return config;
155         }
156         if (parent) {
157                 dbg(0,"no parent in config allowed\n");
158                 return NULL;
159         }
160         if (!config)
161                 config_new_int();
162         config->attrs=attr_list_dup(attrs);
163         while (*attrs) {
164                 if (!config_set_attr_int(config,*attrs)) {
165                         dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
166                         config_destroy(config);
167                         config=NULL;
168                         break;
169                 }
170                 attrs++;
171         }
172         configured=1;
173         return config;
174 }