summaryrefslogtreecommitdiff
path: root/libmateweather/mateweather-prefs.c
blob: b533ca2de768cf5184778be4217956e35da5d55a (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* mateweather-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 <mateconf/mateconf-client.h>

#define MATEWEATHER_I_KNOW_THIS_IS_UNSTABLE
#include "mateweather-prefs.h"
#include "weather-priv.h"

static MateConfEnumStringPair 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 MateConfEnumStringPair 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 MateConfEnumStringPair 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 MateConfEnumStringPair 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 *mateconf_str, MateWeatherPrefs *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 ( mateconf_str && mateconf_string_to_enum (temp_unit_enum_map, mateconf_str, &value) ) {
        prefs->temperature_unit = value;

	if ((prefs->temperature_unit == TEMP_UNIT_DEFAULT) &&
	    (mateconf_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 (mateconf_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 *mateconf_str, MateWeatherPrefs *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 (mateconf_str && mateconf_string_to_enum (speed_unit_enum_map, mateconf_str, &value) ) {
        prefs->speed_unit = value;
	if ((prefs->speed_unit == SPEED_UNIT_DEFAULT) &&
	    (mateconf_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 (mateconf_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 *mateconf_str, MateWeatherPrefs *prefs)
{
    gint value = 0;
#ifdef _NL_MEASUREMENT_MEASUREMENT
    char *imperial = NULL;
#endif

    prefs->pressure_unit = PRESSURE_UNIT_INVALID;
    prefs->use_pressure_default = TRUE;

    if ( mateconf_str && mateconf_string_to_enum (pressure_unit_enum_map, mateconf_str, &value) ) {
        prefs->pressure_unit = value;

        if ((prefs->pressure_unit == PRESSURE_UNIT_DEFAULT) &&
	    (mateconf_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 (mateconf_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 *mateconf_str, MateWeatherPrefs *prefs)
{
    gint value = 0;
#ifdef _NL_MEASUREMENT_MEASUREMENT
    char *imperial = NULL;
#endif

    prefs->distance_unit = DISTANCE_UNIT_INVALID;
    prefs->use_distance_default = TRUE;
    if (mateconf_str && mateconf_string_to_enum (distance_unit_enum_map, mateconf_str, &value)) {
        prefs->distance_unit = value;

	if ((prefs->distance_unit == DISTANCE_UNIT_DEFAULT) &&
	    (mateconf_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 (mateconf_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 *
mateweather_prefs_temp_enum_to_string (TempUnit temp)
{
    return mateconf_enum_to_string (temp_unit_enum_map, temp);
}

const char *
mateweather_prefs_speed_enum_to_string (SpeedUnit speed)
{
    return mateconf_enum_to_string (speed_unit_enum_map, speed);
}

const char *
mateweather_prefs_pressure_enum_to_string (PressureUnit pressure)
{
    return mateconf_enum_to_string (pressure_unit_enum_map, pressure);
}

const char *
mateweather_prefs_distance_enum_to_string (DistanceUnit distance)
{
    return mateconf_enum_to_string (distance_unit_enum_map, distance);
}


void
mateweather_prefs_load (MateWeatherPrefs *prefs, MateWeatherMateConf *ctx)
{
    GError *error = NULL;
    gchar  *mateconf_str = NULL;

    g_return_if_fail (prefs != NULL);
    g_return_if_fail (ctx != NULL);

    if (prefs->location) {
	weather_location_free (prefs->location);
    }
    prefs->location = mateweather_mateconf_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 =
    	mateweather_mateconf_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 =
    	mateweather_mateconf_get_bool (ctx, "auto_update", NULL);
    prefs->detailed =
    	mateweather_mateconf_get_bool (ctx, "enable_detailed_forecast", NULL);
    prefs->radar_enabled =
    	mateweather_mateconf_get_bool (ctx, "enable_radar_map", NULL);
    prefs->use_custom_radar_url =
    	mateweather_mateconf_get_bool (ctx, "use_custom_radar_url", NULL);

    if (prefs->radar) {
        g_free (prefs->radar);
        prefs->radar = NULL;
    }
    prefs->radar = mateweather_mateconf_get_string (ctx, "radar", NULL);

    mateconf_str = mateweather_mateconf_get_string (ctx, MATECONF_TEMP_UNIT, NULL);
    parse_temp_string (mateconf_str, prefs);
    g_free (mateconf_str);

    mateconf_str = mateweather_mateconf_get_string (ctx, MATECONF_SPEED_UNIT, NULL);
    parse_speed_string (mateconf_str, prefs);
    g_free (mateconf_str);

    mateconf_str = mateweather_mateconf_get_string (ctx, MATECONF_PRESSURE_UNIT, NULL);
    parse_pressure_string (mateconf_str, prefs);
    g_free (mateconf_str);

    mateconf_str = mateweather_mateconf_get_string (ctx, MATECONF_DISTANCE_UNIT, NULL);
    parse_distance_string (mateconf_str, prefs);
    g_free (mateconf_str);

    return;
}

TempUnit
mateweather_prefs_parse_temperature (const char *str, gboolean *is_default)
{
    MateWeatherPrefs 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;
}

SpeedUnit
mateweather_prefs_parse_speed (const char *str, gboolean *is_default)
{
    MateWeatherPrefs 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, MateConfEnumStringPair *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 *
mateweather_prefs_get_temp_display_name (TempUnit temp)
{
    return get_translated_unit (temp, temp_unit_enum_map, TEMP_UNIT_DEFAULT, TEMP_UNIT_FAHRENHEIT);
}

const char *
mateweather_prefs_get_speed_display_name (SpeedUnit speed)
{
    return get_translated_unit (speed, speed_unit_enum_map, SPEED_UNIT_DEFAULT, SPEED_UNIT_BFT);
}

const char *
mateweather_prefs_get_pressure_display_name (PressureUnit pressure)
{
    return get_translated_unit (pressure, pressure_unit_enum_map, PRESSURE_UNIT_DEFAULT, PRESSURE_UNIT_ATM);
}

const char *
mateweather_prefs_get_distance_display_name (DistanceUnit distance)
{
    return get_translated_unit (distance, distance_unit_enum_map, DISTANCE_UNIT_DEFAULT, DISTANCE_UNIT_MILES);
}