~mterry/geonames/search-countries-too

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
/*
 * Copyright 2015 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
 *
 * Authors:
 *     Lars Uebernickel <lars.uebernickel@canonical.com>
 */

#include "geonames.h"
#include "geonames-query.h"

/**
 * SECTION: geonames
 * @title: Geonames
 * @short_description: Access the geonames.org database
 * @include: geonames.h
 *
 * This library provides access to a local copy of a subset of the city
 * and country data of geonames.org.
 */

static GVariant *geonames_data;

static void
ensure_geonames_data (void)
{
  if (g_once_init_enter (&geonames_data))
    {
      g_autoptr(GBytes) data;
      GVariant *v;

      data = g_resources_lookup_data ("/com/ubuntu/geonames/cities.compiled", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
      g_assert (data);

      v = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(ssssu)"), data, TRUE);

      g_once_init_leave (&geonames_data, v);
    }
}

static void
task_func (GTask        *task,
           gpointer      source_object,
           gpointer      task_data,
           GCancellable *cancellable)
{
  gchar *query = task_data;
  GArray *indices;

  indices = geonames_query_cities_db (geonames_data, query);

  g_task_return_pointer (task, indices, (GDestroyNotify) g_array_unref);
}

/**
 * geonames_query_cities:
 * @query: the search string
 * @flags: #GeonamesQueryFlags
 * @cancellable: (nullable): a #GCancellable
 * @callback: (nullable): a #GAsyncReadyCallback
 * @user_data: user data passed into @callback
 *
 * Asynchronously queries the geonames city database with the search
 * terms in @query. When the operation is finished, @callback is called
 * from the thread-default main context you are calling this method
 * from. Call geonames_query_cities_finish() from @callback to retrieve
 * the list of results.
 *
 * Results are weighted by how well and how many tokens match a
 * particular city, as well as importance of a city.
 *
 * If @query is empty, no results are returned.
 */
void
geonames_query_cities (const gchar         *query,
                       GeonamesQueryFlags   flags,
                       GCancellable        *cancellable,
                       GAsyncReadyCallback  callback,
                       gpointer             user_data)
{
  GTask *task;

  ensure_geonames_data ();

  task = g_task_new (NULL, cancellable, callback, user_data);
  g_task_set_task_data (task, g_strdup (query), g_free);

  g_task_run_in_thread (task, task_func);
}

static gint *
free_index_array (GArray *array,
                  guint  *length)
{
  const gint sentinel = -1;

  if (length)
    *length = array->len;

  /* append sentinel after taking the length */
  g_array_append_val (array, sentinel);

  return (gint *) g_array_free (array, FALSE);
}

/**
 * geonames_query_cities_finish:
 * @result: the #GAsyncResult from the callback passed to
 *   geonames_query_cities()
 * @length: (out) (optional): optional location for storing the number
 *   of returned cities
 * @error: a #GError
 *
 * Finishes an operation started with geonames_query_cities() and
 * returns the resulting matches.
 *
 * Returns: (array length=@length): The list of cities matching the
 * search query, as indices that can be passed into cities with
 * geonames_get_city().
 */
gint *
geonames_query_cities_finish (GAsyncResult        *result,
                              guint               *length,
                              GError             **error)
{
  GArray *array;

  g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);

  array = g_task_propagate_pointer (G_TASK (result), error);
  if (array == NULL)
    return NULL;

  return free_index_array (array, length);
}

/**
 * geonames_query_cities_sync:
 * @query: the search string
 * @flags: #GeonamesQueryFlags
 * @cancellable: (nullable): a #GCancellable
 * @length: (out) (optional): optional location for storing the number
 *   of returned cities
 * @error: a #GError
 *
 * Synchronous version of geonames_query_cities().
 *
 * Returns: (array length=@length): The list of cities matching the
 * search query, as indices that can be passed into cities with
 * geonames_get_city().
 */
gint *
geonames_query_cities_sync (const gchar         *query,
                            GeonamesQueryFlags   flags,
                            guint               *length,
                            GCancellable        *cancellable,
                            GError             **error)
{
  GArray *indices;

  ensure_geonames_data ();

  indices = geonames_query_cities_db (geonames_data, query);

  return free_index_array (indices, length);
}

/**
 * geonames_get_n_cities:
 *
 * Returns the amount of cities in the geonames database.
 *
 * Returns: The amount of cities
 */
gint
geonames_get_n_cities (void)
{
  ensure_geonames_data ();

  return g_variant_n_children (geonames_data);
}

/**
 * geonames_get_city:
 * @index: The index of the city to retrieve
 *
 * Retrieves the city at @index in the geonames database.
 *
 * Returns: (transfer full): the #GeonamesCity at @index in the database
 */
GeonamesCity *
geonames_get_city (gint index)
{
  ensure_geonames_data ();

  g_return_val_if_fail (index < g_variant_n_children (geonames_data), NULL);

  return g_variant_get_child_value (geonames_data, index);
}

/**
 * geonames_city_free:
 * @city: a #GeonamesCity
 *
 * Frees @city.
 */
void
geonames_city_free (GeonamesCity *city)
{
  g_variant_unref (city);
}

/**
 * geonames_city_get_name:
 * @city: a #GeonamesCity
 *
 * Returns: the name of @city
 */
const gchar *
geonames_city_get_name (GeonamesCity *city)
{
  const gchar *name;

  g_variant_get_child (city, 0, "&s", &name);

  return name;
}

/**
 * geonames_city_get_state:
 * @city: a #GeonamesCity
 *
 * Returns: the state of @city
 */
const gchar *
geonames_city_get_state (GeonamesCity *city)
{
  const gchar *state;

  g_variant_get_child (city, 1, "&s", &state);

  return state;
}

/**
 * geonames_city_get_country:
 * @city: a #GeonamesCity
 *
 * Returns: the country of @city
 */
const gchar *
geonames_city_get_country (GeonamesCity *city)
{
  const gchar *country;

  g_variant_get_child (city, 2, "&s", &country);

  return country;
}

/**
 * geonames_city_get_timezone:
 * @city: a #GeonamesCity
 *
 * Returns: the timezone of @city
 */
const gchar *
geonames_city_get_timezone (GeonamesCity *city)
{
  const gchar *timezone;

  g_variant_get_child (city, 3, "&s", &timezone);

  return timezone;
}