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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* gweather-prefs.c - Preference handling functions
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see
* <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
#include <langinfo.h>
#endif
#include <gconf/gconf-client.h>
#define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
#include "gweather-prefs.h"
#include "weather-priv.h"
/**
* SECTION:gweather-prefs
* @Title: gweather-prefs
*/
WeatherLocation * _gweather_gconf_get_location (GWeatherGConf *ctx);
static GConfEnumStringPair temp_unit_enum_map [] = {
{ TEMP_UNIT_DEFAULT, N_("Default") },
/* translators: Kelvin */
{ TEMP_UNIT_KELVIN, N_("K") },
/* translators: Celsius */
{ TEMP_UNIT_CENTIGRADE, N_("C") },
/* translators: Fahrenheit */
{ TEMP_UNIT_FAHRENHEIT, N_("F") },
{ 0, NULL }
};
static GConfEnumStringPair speed_unit_enum_map [] = {
{ SPEED_UNIT_DEFAULT, N_("Default") },
/* translators: meters per second */
{ SPEED_UNIT_MS, N_("m/s") },
/* translators: kilometers per hour */
{ SPEED_UNIT_KPH, N_("km/h") },
/* translators: miles per hour */
{ SPEED_UNIT_MPH, N_("mph") },
/* translators: knots (speed unit) */
{ SPEED_UNIT_KNOTS, N_("knots") },
/* translators: wind speed */
{ SPEED_UNIT_BFT, N_("Beaufort scale") },
{ 0, NULL }
};
static GConfEnumStringPair pressure_unit_enum_map [] = {
{ PRESSURE_UNIT_DEFAULT, N_("Default") },
/* translators: kilopascals */
{ PRESSURE_UNIT_KPA, N_("kPa") },
/* translators: hectopascals */
{ PRESSURE_UNIT_HPA, N_("hPa") },
/* translators: millibars */
{ PRESSURE_UNIT_MB, N_("mb") },
/* translators: millimeters of mercury */
{ PRESSURE_UNIT_MM_HG, N_("mmHg") },
/* translators: inches of mercury */
{ PRESSURE_UNIT_INCH_HG, N_("inHg") },
/* translators: atmosphere */
{ PRESSURE_UNIT_ATM, N_("atm") },
{ 0, NULL }
};
static GConfEnumStringPair distance_unit_enum_map [] = {
{ DISTANCE_UNIT_DEFAULT, N_("Default") },
/* translators: meters */
{ DISTANCE_UNIT_METERS, N_("m") },
/* translators: kilometers */
{ DISTANCE_UNIT_KM, N_("km") },
/* translators: miles */
{ DISTANCE_UNIT_MILES, N_("mi") },
{ 0, NULL }
};
static void
parse_temp_string (const gchar *gconf_str, GWeatherPrefs *prefs)
{
gint value = 0;
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
char *imperial = NULL;
#endif
prefs->temperature_unit = TEMP_UNIT_INVALID;
prefs->use_temperature_default = TRUE;
if ( gconf_str && gconf_string_to_enum (temp_unit_enum_map, gconf_str, &value) ) {
prefs->temperature_unit = value;
if ((prefs->temperature_unit == TEMP_UNIT_DEFAULT) &&
(gconf_string_to_enum (temp_unit_enum_map, _("DEFAULT_TEMP_UNIT"), &value)) ) {
prefs->temperature_unit = value;
} else {
prefs->use_temperature_default = FALSE;
}
} else {
/* TRANSLATOR: This is the default unit to use for temperature measurements. */
/* Valid values are: "K" (Kelvin), "C" (Celsius) and "F" (Fahrenheit) */
if (gconf_string_to_enum (temp_unit_enum_map, _("DEFAULT_TEMP_UNIT"), &value) ) {
prefs->temperature_unit = value;
}
}
if (!prefs->temperature_unit || prefs->temperature_unit == TEMP_UNIT_DEFAULT ) {
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if ( imperial && imperial[0] == 2 ) {
/* imperial */
prefs->temperature_unit = TEMP_UNIT_FAHRENHEIT;
} else
#endif
prefs->temperature_unit = TEMP_UNIT_CENTIGRADE;
}
}
static void
parse_speed_string (const gchar *gconf_str, GWeatherPrefs *prefs)
{
gint value = 0;
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
char *imperial = NULL;
#endif
prefs->speed_unit = SPEED_UNIT_INVALID;
prefs->use_speed_default = TRUE;
if (gconf_str && gconf_string_to_enum (speed_unit_enum_map, gconf_str, &value) ) {
prefs->speed_unit = value;
if ((prefs->speed_unit == SPEED_UNIT_DEFAULT) &&
(gconf_string_to_enum (speed_unit_enum_map, _("DEFAULT_SPEED_UNIT"), &value)) ) {
prefs->speed_unit = value;
} else {
prefs->use_speed_default = FALSE;
}
}
else {
/* TRANSLATOR: This is the default unit to use for wind speed. */
/* Valid values are: "m/s" (meters per second), "km/h" (kilometers per hour), */
/* "mph" (miles per hour) and "knots" */
if (gconf_string_to_enum (speed_unit_enum_map, _("DEFAULT_SPEED_UNIT"), &value) ) {
prefs->speed_unit = value;
}
}
if ((!prefs->speed_unit) || prefs->speed_unit == SPEED_UNIT_DEFAULT) {
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if (imperial && imperial[0] == 2) {
/* imperial */
prefs->speed_unit = SPEED_UNIT_KNOTS;
} else
#endif
prefs->speed_unit = SPEED_UNIT_MS;
}
}
static void
parse_pressure_string (const gchar *gconf_str, GWeatherPrefs *prefs)
{
gint value = 0;
#ifdef _NL_MEASUREMENT_MEASUREMENT
char *imperial = NULL;
#endif
prefs->pressure_unit = PRESSURE_UNIT_INVALID;
prefs->use_pressure_default = TRUE;
if ( gconf_str && gconf_string_to_enum (pressure_unit_enum_map, gconf_str, &value) ) {
prefs->pressure_unit = value;
if ((prefs->pressure_unit == PRESSURE_UNIT_DEFAULT) &&
(gconf_string_to_enum (pressure_unit_enum_map, _("DEFAULT_PRESSURE_UNIT"), &value)) ) {
prefs->pressure_unit = value;
} else {
prefs->use_pressure_default = FALSE;
}
}
else {
/* TRANSLATOR: This is the default unit to use for atmospheric pressure. */
/* Valid values are: "kPa" (kiloPascals), "hPa" (hectoPascals),
"mb" (millibars), "mmHg" (millimeters of mercury),
"inHg" (inches of mercury) and "atm" (atmosphere) */
if (gconf_string_to_enum (pressure_unit_enum_map, _("DEFAULT_PRESSURE_UNIT"), &value) ) {
prefs->pressure_unit = value;
}
}
if ( (!prefs->pressure_unit) || prefs->pressure_unit == PRESSURE_UNIT_DEFAULT ) {
#ifdef _NL_MEASUREMENT_MEASUREMENT
imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if (imperial && imperial[0] == 2) {
/* imperial */
prefs->pressure_unit = PRESSURE_UNIT_INCH_HG;
} else
#endif
prefs->pressure_unit = PRESSURE_UNIT_HPA;
}
}
static void
parse_distance_string (const gchar *gconf_str, GWeatherPrefs *prefs)
{
gint value = 0;
#ifdef _NL_MEASUREMENT_MEASUREMENT
char *imperial = NULL;
#endif
prefs->distance_unit = DISTANCE_UNIT_INVALID;
prefs->use_distance_default = TRUE;
if (gconf_str && gconf_string_to_enum (distance_unit_enum_map, gconf_str, &value)) {
prefs->distance_unit = value;
if ((prefs->distance_unit == DISTANCE_UNIT_DEFAULT) &&
(gconf_string_to_enum (distance_unit_enum_map, _("DEFAULT_DISTANCE_UNIT"), &value)) ) {
prefs->distance_unit = value;
} else {
prefs->use_distance_default = FALSE;
}
}
else {
/* TRANSLATOR: This is the default unit to use for visibility distance. */
/* Valid values are: "m" (meters), "km" (kilometers) and "mi" (miles) */
if (gconf_string_to_enum (distance_unit_enum_map, _("DEFAULT_DISTANCE_UNIT"), &value) ) {
prefs->distance_unit = value;
}
}
if ((!prefs->distance_unit) || prefs->distance_unit == DISTANCE_UNIT_DEFAULT) {
#ifdef _NL_MEASUREMENT_MEASUREMENT
imperial = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
if (imperial && imperial[0] == 2) {
/* imperial */
prefs->distance_unit = DISTANCE_UNIT_MILES;
} else
#endif
prefs->distance_unit = DISTANCE_UNIT_METERS;
}
return;
}
const char *
gweather_prefs_temp_enum_to_string (GWeatherTemperatureUnit temp)
{
return gconf_enum_to_string (temp_unit_enum_map, temp);
}
const char *
gweather_prefs_speed_enum_to_string (GWeatherSpeedUnit speed)
{
return gconf_enum_to_string (speed_unit_enum_map, speed);
}
const char *
gweather_prefs_pressure_enum_to_string (GWeatherPressureUnit pressure)
{
return gconf_enum_to_string (pressure_unit_enum_map, pressure);
}
const char *
gweather_prefs_distance_enum_to_string (GWeatherDistanceUnit distance)
{
return gconf_enum_to_string (distance_unit_enum_map, distance);
}
void
gweather_prefs_load (GWeatherPrefs *prefs, GWeatherGConf *ctx)
{
GError *error = NULL;
gchar *gconf_str = NULL;
g_return_if_fail (prefs != NULL);
g_return_if_fail (ctx != NULL);
if (prefs->location) {
_weather_location_free (prefs->location);
}
prefs->location = _gweather_gconf_get_location (ctx);
/* Assume we use unit defaults */
prefs->use_temperature_default = TRUE;
prefs->use_speed_default = TRUE;
prefs->use_pressure_default = TRUE;
prefs->use_distance_default = TRUE;
prefs->update_interval =
gweather_gconf_get_int (ctx, "auto_update_interval", &error);
if (error) {
g_print ("%s \n", error->message);
g_error_free (error);
error = NULL;
}
prefs->update_interval = MAX (prefs->update_interval, 60);
prefs->update_enabled =
gweather_gconf_get_bool (ctx, "auto_update", NULL);
prefs->detailed =
gweather_gconf_get_bool (ctx, "enable_detailed_forecast", NULL);
prefs->radar_enabled =
gweather_gconf_get_bool (ctx, "enable_radar_map", NULL);
prefs->use_custom_radar_url =
gweather_gconf_get_bool (ctx, "use_custom_radar_url", NULL);
if (prefs->radar) {
g_free (prefs->radar);
prefs->radar = NULL;
}
prefs->radar = gweather_gconf_get_string (ctx, "radar", NULL);
gconf_str = gweather_gconf_get_string (ctx, GCONF_TEMP_UNIT, NULL);
parse_temp_string (gconf_str, prefs);
g_free (gconf_str);
gconf_str = gweather_gconf_get_string (ctx, GCONF_SPEED_UNIT, NULL);
parse_speed_string (gconf_str, prefs);
g_free (gconf_str);
gconf_str = gweather_gconf_get_string (ctx, GCONF_PRESSURE_UNIT, NULL);
parse_pressure_string (gconf_str, prefs);
g_free (gconf_str);
gconf_str = gweather_gconf_get_string (ctx, GCONF_DISTANCE_UNIT, NULL);
parse_distance_string (gconf_str, prefs);
g_free (gconf_str);
return;
}
GWeatherTemperatureUnit
gweather_prefs_parse_temperature (const char *str, gboolean *is_default)
{
GWeatherPrefs prefs;
g_return_val_if_fail (str != NULL, TEMP_UNIT_INVALID);
g_return_val_if_fail (is_default != NULL, TEMP_UNIT_INVALID);
parse_temp_string (str, &prefs);
*is_default = prefs.use_temperature_default;
return prefs.temperature_unit;
}
GWeatherSpeedUnit
gweather_prefs_parse_speed (const char *str, gboolean *is_default)
{
GWeatherPrefs prefs;
g_return_val_if_fail (str != NULL, SPEED_UNIT_INVALID);
g_return_val_if_fail (is_default != NULL, SPEED_UNIT_INVALID);
parse_speed_string (str, &prefs);
*is_default = prefs.use_speed_default;
return prefs.speed_unit;
}
static const char *
get_translated_unit (int unit, GConfEnumStringPair *pairs, int min_value, int max_value)
{
g_return_val_if_fail (unit >= min_value && unit <= max_value, NULL);
return _(pairs[unit - 1].str); /* minus 1 because enum value 0 is for "invalid" (look at weather.h) */
}
const char *
gweather_prefs_get_temp_display_name (GWeatherTemperatureUnit temp)
{
return get_translated_unit (temp, temp_unit_enum_map, TEMP_UNIT_DEFAULT, TEMP_UNIT_FAHRENHEIT);
}
const char *
gweather_prefs_get_speed_display_name (GWeatherSpeedUnit speed)
{
return get_translated_unit (speed, speed_unit_enum_map, SPEED_UNIT_DEFAULT, SPEED_UNIT_BFT);
}
const char *
gweather_prefs_get_pressure_display_name (GWeatherPressureUnit pressure)
{
return get_translated_unit (pressure, pressure_unit_enum_map, PRESSURE_UNIT_DEFAULT, PRESSURE_UNIT_ATM);
}
const char *
gweather_prefs_get_distance_display_name (GWeatherDistanceUnit distance)
{
return get_translated_unit (distance, distance_unit_enum_map, DISTANCE_UNIT_DEFAULT, DISTANCE_UNIT_MILES);
}
|