summaryrefslogtreecommitdiffstats
path: root/plugins/datetime/weather-tz.c
blob: f1de2b94a980f69b621b4c3c497f56803ff5f0f8 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2013 Kalev Lember <kalevlember@gmail.com>
 *
 * 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/>.
 *
 */

#include "config.h"

#include "weather-tz.h"
#include "tz.h"

#define GWEATHER_I_KNOW_THIS_IS_UNSTABLE
#include <libgweather/gweather.h>

static GList *
location_get_cities (GWeatherLocation *parent_location)
{
        GList *cities = NULL;
        GWeatherLocation **children;
        gint i;

        children = gweather_location_get_children (parent_location);
        for (i = 0; children[i]; i++) {
                if (gweather_location_get_level (children[i]) == GWEATHER_LOCATION_CITY) {
                        cities = g_list_prepend (cities,
                                                 children[i]);
                } else {
                        cities = g_list_concat (cities,
                                                location_get_cities (children[i]));
                }
        }

        return cities;
}

static gboolean
weather_location_has_timezone (GWeatherLocation *loc)
{
        return gweather_location_get_timezone (loc) != NULL;
}

/**
 * load_timezones:
 * @cities: a list of #GWeatherLocation
 *
 * Returns: a list of #TzLocation
 */
static GList *
load_timezones (GList *cities)
{
        GList *l;
        GList *tz_locations = NULL;

        for (l = cities; l; l = l->next) {
                TzLocation *loc;
                const gchar *country;
                const gchar *timezone_id;
                gdouble latitude;
                gdouble longitude;

                if (!gweather_location_has_coords (l->data) ||
                    !weather_location_has_timezone (l->data)) {
                        g_debug ("Incomplete GWeather location entry: (%s) %s",
                                 gweather_location_get_country (l->data),
                                 gweather_location_get_city_name (l->data));
                        continue;
                }

                country = gweather_location_get_country (l->data);
                timezone_id = gweather_timezone_get_tzid (gweather_location_get_timezone (l->data));
                gweather_location_get_coords (l->data,
                                              &latitude,
                                              &longitude);

                loc = g_new0 (TzLocation, 1);
                loc->country = g_strdup (country);
                loc->latitude = latitude;
                loc->longitude = longitude;
                loc->zone = g_strdup (timezone_id);
                loc->comment = NULL;

                tz_locations = g_list_prepend (tz_locations, loc);
        }

        return tz_locations;
}

GList *
weather_tz_db_get_locations (const gchar *country_code)
{
        GList *cities;
        GList *tz_locations;
        GWeatherLocation *world;
        GWeatherLocation *country;

        world = gweather_location_get_world ();

        country = gweather_location_find_by_country_code (world, country_code);

        if (!country)
                return NULL;

        cities = location_get_cities (country);
        tz_locations = load_timezones (cities);

        g_list_free (cities);

        return tz_locations;
}