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 |
struct attr_iter *iter=config_attr_iter_new();
|
64 |
struct attr navit;
|
65 |
dbg(0,"terminating\n");
|
66 |
while (config_get_attr(config, attr_navit, &navit, iter))
|
67 |
navit_destroy(navit.u.navit);
|
68 |
config_attr_iter_destroy(iter);
|
69 |
config_destroy(config);
|
70 |
}
|
71 |
|
72 |
static void
|
73 |
config_new_int(void)
|
74 |
{
|
75 |
config=g_new0(struct config, 1);
|
76 |
config->cbl=callback_list_new("config_new_int:config->cbl");
|
77 |
#ifndef HAVE_API_WIN32_CE
|
78 |
signal(SIGTERM, config_terminate);
|
79 |
#ifndef HAVE_API_WIN32
|
80 |
#ifndef __MINGW32__
|
81 |
signal(SIGPIPE, SIG_IGN);
|
82 |
#endif /* __MINGW32__ */
|
83 |
#endif
|
84 |
#endif
|
85 |
}
|
86 |
|
87 |
int
|
88 |
config_get_attr(struct config *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
|
89 |
{
|
90 |
return attr_generic_get_attr(this_->attrs, NULL, type, attr, iter);
|
91 |
}
|
92 |
|
93 |
static int
|
94 |
config_set_attr_int(struct config *this_, struct attr *attr)
|
95 |
{
|
96 |
switch (attr->type) {
|
97 |
case attr_language:
|
98 |
setenv("LANG",attr->u.str,1);
|
99 |
return 1;
|
100 |
default:
|
101 |
return 0;
|
102 |
}
|
103 |
}
|
104 |
|
105 |
int
|
106 |
config_set_attr(struct config *this_, struct attr *attr)
|
107 |
{
|
108 |
return config_set_attr_int(this_, attr);
|
109 |
}
|
110 |
|
111 |
int
|
112 |
config_add_attr(struct config *this_, struct attr *attr)
|
113 |
{
|
114 |
if (!config) {
|
115 |
config_new_int();
|
116 |
this_=config;
|
117 |
}
|
118 |
switch (attr->type) {
|
119 |
case attr_callback:
|
120 |
callback_list_add(this_->cbl, attr->u.callback);
|
121 |
default:
|
122 |
this_->attrs=attr_generic_add_attr(this_->attrs, attr);
|
123 |
}
|
124 |
callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, 1);
|
125 |
return 1;
|
126 |
}
|
127 |
|
128 |
int
|
129 |
config_remove_attr(struct config *this_, struct attr *attr)
|
130 |
{
|
131 |
switch (attr->type) {
|
132 |
case attr_callback:
|
133 |
callback_list_remove(this_->cbl, attr->u.callback);
|
134 |
default:
|
135 |
this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
|
136 |
}
|
137 |
callback_list_call_attr_2(this_->cbl, attr->type, attr->u.data, -1);
|
138 |
return 1;
|
139 |
}
|
140 |
|
141 |
struct attr_iter *
|
142 |
config_attr_iter_new()
|
143 |
{
|
144 |
return g_new0(struct attr_iter, 1);
|
145 |
}
|
146 |
|
147 |
void
|
148 |
config_attr_iter_destroy(struct attr_iter *iter)
|
149 |
{
|
150 |
g_free(iter);
|
151 |
}
|
152 |
|
153 |
|
154 |
struct config *
|
155 |
config_new(struct attr *parent, struct attr **attrs)
|
156 |
{
|
157 |
if (configured) {
|
158 |
dbg(0,"only one config allowed\n");
|
159 |
return config;
|
160 |
}
|
161 |
if (parent) {
|
162 |
dbg(0,"no parent in config allowed\n");
|
163 |
return NULL;
|
164 |
}
|
165 |
if (!config)
|
166 |
config_new_int();
|
167 |
config->attrs=attr_list_dup(attrs);
|
168 |
while (*attrs) {
|
169 |
if (!config_set_attr_int(config,*attrs)) {
|
170 |
dbg(0,"failed to set attribute '%s'\n",attr_to_name((*attrs)->type));
|
171 |
config_destroy(config);
|
172 |
config=NULL;
|
173 |
break;
|
174 |
}
|
175 |
attrs++;
|
176 |
}
|
177 |
configured=1;
|
178 |
return config;
|
179 |
}
|