summaryrefslogtreecommitdiff
path: root/src/navigation/map-viewer/configuration-plugin/genivi_mapviewer_configuration.cxx
blob: 755d73ba3a01c740f8bdede8b9d67ea8788fb745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2013-2014, PCA Peugeot Citroen
*
* \file genivi_mapviewer_configuration.cxx
*
* \brief This file is part of the Navit POC.
*
* \author Martin Schaller <martin.schaller@it-schaller.de>
* \author Philippe Colliot <philippe.colliot@mpsa.com>
*
* \version 1.0
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
* 
* 2014-03-18, Philippe Colliot, Add JPN language to locale
*
* @licence end@
*/
#ifndef DBUS_HAS_RECURSIVE_MUTEX
#define DBUS_HAS_RECURSIVE_MUTEX
#endif
#include <dbus-c++/glib-integration.h>
#include <locale.h>
#include <stdlib.h>
#include "genivi-mapviewer-constants.h"
#include "genivi-mapviewer-configuration_adaptor.h"
#include <config.h>
#define USE_PLUGINS 1
#include <navit/debug.h>
#include <navit/plugin.h>
#include <navit/event.h>
#include "navigation-common-dbus.h"

#if (!DEBUG_ENABLED)
#undef dbg
#define dbg(level,...) ;
#endif

static DBus::Glib::BusDispatcher dispatcher;
static DBus::Connection *conn;

static struct map32 {
	const char *c3;
	const char *c2;
} language_map[] = {
	{"deu","de"},
	{"eng","en"},
	{"fra","fr"},
	{"jpn","jp"},
    {"kor","ko"},
};

static struct map32 country_map[] = {
	{"CHE","CH"},
	{"DEU","DE"},
	{"FRA","FR"},
	{"USA","US"},
	{"JPN","JP"},
    {"KOR","KO"},
};

static std::string scriptCode = "Latn";

static DBus::Variant
variant_array_uint16(std::vector< uint16_t > i)
{
	DBus::Variant variant;
	DBus::MessageIter iter=variant.writer();
	iter << i;
	return variant;
}

static const char *
map_3_to_2(struct map32 *map, int len, const char *in)
{
	int i;
	for (i = 0 ; i < len ; i++)
		if (!strcmp(map[i].c3,in))
			return map[i].c2;
	return "";
}

static const char *
map_2_to_3(struct map32 *map, int len, const char *in)
{
	int i;
	for (i = 0 ; i < len ; i++)
		if (!strcmp(map[i].c2,in))
			return map[i].c3;
	return "";
}

static const char *
language_3_to_2(const char *in)
{
	return map_3_to_2(language_map, sizeof(language_map)/sizeof(language_map[0]), in);
}

static const char *
language_2_to_3(const char *in)
{
	return map_2_to_3(language_map, sizeof(language_map)/sizeof(language_map[0]), in);
}

static const char *
country_3_to_2(const char *in)
{
	return map_3_to_2(country_map, sizeof(country_map)/sizeof(country_map[0]), in);
}

static const char *
country_2_to_3(const char *in)
{
	return map_2_to_3(country_map, sizeof(country_map)/sizeof(country_map[0]), in);
}

std::map< int32_t, int32_t > m_unitsOfMeasurement;

class  Configuration
: public org::genivi::navigation::mapviewer::Configuration_adaptor,
  public DBus::IntrospectableAdaptor,
  public DBus::ObjectAdaptor
{
	public:
	Configuration(DBus::Connection &connection)
	: DBus::ObjectAdaptor(connection, "/org/genivi/mapviewer")
	{
	}

	::DBus::Struct< uint16_t, uint16_t, uint16_t, std::string >
	GetVersion()
	{
		::DBus::Struct< uint16_t, uint16_t, uint16_t, std::string >ret;
		ret._1=2;
		ret._2=0;
		ret._3=0;
		ret._4="07-06-2013";
		return ret;
	}

    void
    SetUnitsOfMeasurement(const std::map< int32_t, int32_t >& unitsOfMeasurement)
    {
        m_unitsOfMeasurement=unitsOfMeasurement;
        std::vector< int32_t > changed;
        changed.push_back(GENIVI_MAPVIEWER_UNITS_OF_MEASUREMENT);
        ConfigurationChanged(changed);
    }

    std::map< int32_t, int32_t >
    GetUnitsOfMeasurement()
    {
        return m_unitsOfMeasurement;
    }

    std::map< int32_t, std::vector< int32_t > >
    GetSupportedUnitsOfMeasurement()
    {
        std::map< int32_t, std::vector< int32_t > > ret;
        std::vector< int32_t > length;
        length.push_back(GENIVI_MAPVIEWER_MILE);
        length.push_back(GENIVI_MAPVIEWER_METER);
        ret[GENIVI_MAPVIEWER_LENGTH]=length;
        return ret;
     }

	void
    SetTimeFormat(const DBusCommonAPIEnumeration& timeFormat)
	{
		throw DBus::ErrorNotSupported("Not yet supported");
	}

    DBusCommonAPIEnumeration
	GetTimeFormat()
	{
		throw DBus::ErrorNotSupported("Not yet supported");
	}

    std::vector< DBusCommonAPIEnumeration >
    GetSupportedTimeFormats()
    {
        throw DBus::ErrorNotSupported("Not yet supported");
    }

	void
    SetLocale(const std::string& language, const std::string& country, const std::string& script)
    {
		char *lang=g_strdup_printf("%s_%s.UTF-8",language_3_to_2(language.c_str()),country_3_to_2(country.c_str()));
		setenv("LANG",lang,1);
		setlocale(LC_ALL,"");
        scriptCode = script;
        std::vector< DBusCommonAPIEnumeration > changed;
		changed.push_back(GENIVI_MAPVIEWER_LOCALE);
		ConfigurationChanged(changed);
	}

	void
    GetLocale(std::string& language, std::string& country, std::string& script)
    {
		char *lang=g_strdup(getenv("LANG"));
		char *sep,*dot;
		if (!lang || !(sep=strchr(lang,'_'))) {
			g_free(lang);
			throw DBus::ErrorFailed("LANG doesn't exist or doesn't contain a _");
		}
		*sep++='\0';
		language=language_2_to_3(lang);
		dot=strchr(sep,'.');
		if (dot)
			*dot='\0';
		country=country_2_to_3(sep);
        script = scriptCode;
        g_free(lang);
	}

    std::vector< ::DBus::Struct< std::string, std::string, std::string > >
    GetSupportedLocales()
    {
        std::vector< ::DBus::Struct< std::string, std::string, std::string > > ret;
        ::DBus::Struct< std::string, std::string, std::string > en_US { "eng","USA", "Latn" };
        ::DBus::Struct< std::string, std::string, std::string > de_DE { "deu","DEU", "Latn" };
        ::DBus::Struct< std::string, std::string, std::string > fr_FR { "fra","FRA", "Latn" };
        ::DBus::Struct< std::string, std::string, std::string > jp_JP { "jpn","JPN", "Hrkt" };
        ::DBus::Struct< std::string, std::string, std::string > ko_KO { "kor","KOR", "Hang" };
        ret.push_back(en_US);
        ret.push_back(de_DE);
        ret.push_back(fr_FR);
        ret.push_back(jp_JP);
        ret.push_back(ko_KO);
        return ret;
    }

	void
    SetCoordinatesFormat(const DBusCommonAPIEnumeration& coordinatesFormat)
	{
		throw DBus::ErrorNotSupported("Not yet supported");
	}

    DBusCommonAPIEnumeration
	GetCoordinatesFormat()
	{
		throw DBus::ErrorNotSupported("Not yet supported");
    }

    std::vector< DBusCommonAPIEnumeration >
    GetSupportedCoordinatesFormats()
    {
        throw DBus::ErrorNotSupported("Not yet supported");
    }
};

static class Configuration *server;

static DBus::Variant
variant_enumeration(DBusCommonAPIEnumeration i)
{
	DBus::Variant variant;
	DBus::MessageIter iter=variant.writer();
	iter << i;
	return variant;
}


void
plugin_init(void)
{
	event_request_system("glib","genivi_mapviewerconfiguration");
	dispatcher.attach(NULL);
	DBus::default_dispatcher = &dispatcher;
	conn = new DBus::Connection(DBus::Connection::SessionBus());
	conn->setup(&dispatcher);
    conn->request_name("org.genivi.navigation.mapviewer.Configuration");
    m_unitsOfMeasurement[GENIVI_MAPVIEWER_LENGTH]=GENIVI_MAPVIEWER_KM;
	server=new Configuration(*conn);
}