aboutsummaryrefslogtreecommitdiff
path: root/src/timezone-completion.c
blob: fd6c97bda6cb6c94b517e99792eca9d1de038a53 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* -*- Mode: C; coding: utf-8; indent-tabs-mode: nil; tab-width: 2 -*-

Copyright 2011 Canonical Ltd.

Authors:
    Michael Terry <michael.terry@canonical.com>

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/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <json-glib/json-glib.h>
#include <gdk/gdk.h>
#include <glib/gi18n.h>
#include "timezone-completion.h"
#include "tz.h"

enum {
  LAST_SIGNAL
};

/* static guint signals[LAST_SIGNAL] = { }; */

typedef struct _TimezoneCompletionPrivate TimezoneCompletionPrivate;
struct _TimezoneCompletionPrivate
{
  GtkTreeModel * initial_model;
  GtkEntry *     entry;
  guint          queued_request;
  guint          changed_id;
  GCancellable * cancel;
  gchar *        request_text;
  GHashTable *   request_table;
};

#define TIMEZONE_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), TIMEZONE_COMPLETION_TYPE, TimezoneCompletionPrivate))

#define GEONAME_URL "http://geoname-lookup.ubuntu.com/?query=%s&release=%s"

/* Prototypes */
static void timezone_completion_class_init (TimezoneCompletionClass *klass);
static void timezone_completion_init       (TimezoneCompletion *self);
static void timezone_completion_dispose    (GObject *object);
static void timezone_completion_finalize   (GObject *object);

G_DEFINE_TYPE (TimezoneCompletion, timezone_completion, GTK_TYPE_ENTRY_COMPLETION);

static gboolean
match_func (GtkEntryCompletion *completion, const gchar *key,
            GtkTreeIter *iter, gpointer user_data)
{
  // geonames does the work for us
  return TRUE;
}

static void
save_and_use_model (TimezoneCompletion * completion, GtkTreeModel * model)
{
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE(completion);

  g_hash_table_insert (priv->request_table, g_strdup (priv->request_text), g_object_ref_sink (model));

  if (model == priv->initial_model)
    gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (completion), NULL, NULL, NULL);
  else
    gtk_entry_completion_set_match_func (GTK_ENTRY_COMPLETION (completion), match_func, NULL, NULL);

  gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), model);
  gtk_entry_completion_complete (GTK_ENTRY_COMPLETION (completion));
}

static void
json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data)
{
  TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE(completion);
  GError * error = NULL;
  const gchar * prev_name = NULL;
  const gchar * prev_admin1 = NULL;
  const gchar * prev_country = NULL;

  json_parser_load_from_stream_finish (JSON_PARSER (object), res, &error);

  if (priv->cancel && (error == NULL || error->code != G_IO_ERROR_CANCELLED)) {
    g_cancellable_reset (priv->cancel);
  }

  if (error != NULL) {
    g_warning ("Could not parse geoname JSON data: %s", error->message);
    g_error_free (error);
    save_and_use_model (completion, priv->initial_model);
    return;
  }

  GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING);

  JsonReader * reader = json_reader_new (json_parser_get_root (JSON_PARSER (object)));

  if (!json_reader_is_array (reader)) {
    g_object_unref (G_OBJECT (reader));
    return;
  }

  gint i, count = json_reader_count_elements (reader);
  for (i = 0; i < count; ++i) {
    if (!json_reader_read_element (reader, i))
      continue;

    if (json_reader_is_object (reader)) {
      const gchar * name = NULL;
      const gchar * admin1 = NULL;
      const gchar * country = NULL;
      const gchar * longitude = NULL;
      const gchar * latitude = NULL;
      gboolean skip = FALSE;
      if (json_reader_read_member (reader, "name")) {
        name = json_reader_get_string_value (reader);
        json_reader_end_member (reader);
      }
      if (json_reader_read_member (reader, "admin1")) {
        admin1 = json_reader_get_string_value (reader);
        json_reader_end_member (reader);
      }
      if (json_reader_read_member (reader, "country")) {
        country = json_reader_get_string_value (reader);
        json_reader_end_member (reader);
      }
      if (json_reader_read_member (reader, "longitude")) {
        longitude = json_reader_get_string_value (reader);
        json_reader_end_member (reader);
      }
      if (json_reader_read_member (reader, "latitude")) {
        latitude = json_reader_get_string_value (reader);
        json_reader_end_member (reader);
      }

      if (g_strcmp0(name, prev_name) == 0 &&
          g_strcmp0(admin1, prev_admin1) == 0 &&
          g_strcmp0(country, prev_country) == 0) {
        // Sometimes the data will have duplicate entries that only differ
        // in longitude and latitude.  e.g. "rio de janeiro", "wellington"
        skip = TRUE;
      }

      if (!skip) {
        GtkTreeIter iter;
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            TIMEZONE_COMPLETION_ZONE, NULL,
                            TIMEZONE_COMPLETION_NAME, name,
                            TIMEZONE_COMPLETION_ADMIN1, admin1,
                            TIMEZONE_COMPLETION_COUNTRY, country,
                            TIMEZONE_COMPLETION_LONGITUDE, longitude,
                            TIMEZONE_COMPLETION_LATITUDE, latitude,
                            -1);
      }

      prev_name = name;
      prev_admin1 = admin1;
      prev_country = country;
    }

    json_reader_end_element (reader);
  }

  save_and_use_model (completion, GTK_TREE_MODEL (store));
  g_object_unref (G_OBJECT (reader));
}

static void
geonames_data_ready (GObject *object, GAsyncResult *res, gpointer user_data)
{
  TimezoneCompletion * completion = TIMEZONE_COMPLETION (user_data);
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);
  GError * error = NULL;
  GFileInputStream * stream;

  stream = g_file_read_finish (G_FILE (object), res, &error);

  if (priv->cancel && (error == NULL || error->code != G_IO_ERROR_CANCELLED)) {
    g_cancellable_reset (priv->cancel);
  }

  if (error != NULL) {
    g_warning ("Could not connect to geoname lookup server: %s", error->message);
    g_error_free (error);
    save_and_use_model (completion, priv->initial_model);
    return;
  }

  JsonParser * parser = json_parser_new ();
  json_parser_load_from_stream_async (parser, G_INPUT_STREAM (stream), priv->cancel,
                                      json_parse_ready, user_data);
}

static gboolean
request_zones (TimezoneCompletion * completion)
{
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);

  priv->queued_request = 0;

  if (priv->entry == NULL) {
    return FALSE;
  }

  const gchar * text = gtk_entry_get_text (priv->entry);

  gpointer data;
  if (g_hash_table_lookup_extended (priv->request_table, text, NULL, &data)) {
    gtk_entry_completion_set_model (GTK_ENTRY_COMPLETION (completion), GTK_TREE_MODEL (data));
    gtk_entry_completion_complete (GTK_ENTRY_COMPLETION (completion));
    return FALSE;
  }

  /* Cancel any ongoing request */
  if (priv->cancel) {
    g_cancellable_cancel (priv->cancel);
    g_cancellable_reset (priv->cancel);
  }
  g_free (priv->request_text);

  priv->request_text = g_strdup (text);

  gchar * escaped = g_uri_escape_string (text, NULL, FALSE);
  gchar * url = g_strdup_printf (GEONAME_URL, escaped, "11.04"); // FIXME: don't hardcode

  GFile * file =  g_file_new_for_uri (url);
  g_file_read_async (file, G_PRIORITY_DEFAULT, priv->cancel,
                     geonames_data_ready, completion);

  return FALSE;
}

static void
entry_changed (GtkEntry * entry, TimezoneCompletion * completion)
{
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);

  if (priv->queued_request) {
    g_source_remove (priv->queued_request);
  }
  priv->queued_request = g_timeout_add (300, (GSourceFunc)request_zones, completion);
}

void
timezone_completion_watch_entry (TimezoneCompletion * completion, GtkEntry * entry)
{
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);

  if (priv->entry) {
    g_source_remove (priv->changed_id);
    g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
  }

  guint id = g_signal_connect (entry, "changed", G_CALLBACK (entry_changed), completion);
  priv->changed_id = id;

  priv->entry = entry;
  g_object_add_weak_pointer (G_OBJECT (entry), (gpointer *)&priv->entry);
}

static GtkListStore *
get_initial_model (void)
{
  TzDB * db = tz_load_db ();
  GPtrArray * locations = tz_get_locations (db);

  GtkListStore * store = gtk_list_store_new (TIMEZONE_COMPLETION_LAST,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING,
                                             G_TYPE_STRING);

  gint i;
  for (i = 0; i < locations->len; ++i) {
    TzLocation * loc = g_ptr_array_index (locations, i);
    GtkTreeIter iter;
    gtk_list_store_append (store, &iter);

    /* FIXME: need something better than below for non-English locales */
    const gchar * last_bit = ((const gchar *)strrchr (loc->zone, '/')) + 1;
    if (last_bit == NULL)
      last_bit = loc->zone;
    gchar * name = g_strdup (last_bit);
    gchar * underscore;
    while ((underscore = strchr (name, '_'))) {
       *underscore = ' ';
    }

    gtk_list_store_set (store, &iter,
                        TIMEZONE_COMPLETION_ZONE, loc->zone,
                        TIMEZONE_COMPLETION_NAME, name,
                        TIMEZONE_COMPLETION_COUNTRY, loc->country,
                        -1);

    g_free (name);
  }

  tz_db_free (db);
  return store;
}

static void
data_func (GtkCellLayout *cell_layout, GtkCellRenderer *cell,
           GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data)
{
  GValue name_val = {0}, admin1_val = {0}, country_val = {0};
  const gchar * name, * admin1, * country;

  gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_NAME, &name_val);
  gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_ADMIN1, &admin1_val);
  gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_COUNTRY, &country_val);

  name = g_value_get_string (&name_val);
  admin1 = g_value_get_string (&admin1_val);
  country = g_value_get_string (&country_val);

  gchar * user_name;
  if (admin1 == NULL || admin1[0] == 0) {
    user_name = g_strdup_printf ("%s <small>(%s)</small>", name, country);
  } else {
    user_name = g_strdup_printf ("%s <small>(%s, %s)</small>", name, admin1, country);
  }

  g_object_set (G_OBJECT (cell), "markup", user_name, NULL);

  g_value_unset (&name_val);
  g_value_unset (&admin1_val);
  g_value_unset (&country_val);
}

static void
timezone_completion_class_init (TimezoneCompletionClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (TimezoneCompletionPrivate));

  object_class->dispose = timezone_completion_dispose;
  object_class->finalize = timezone_completion_finalize;

  return;
}

static void
timezone_completion_init (TimezoneCompletion * self)
{
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (self);

  priv->initial_model = GTK_TREE_MODEL (get_initial_model ());

  g_object_set (G_OBJECT (self),
                "text-column", TIMEZONE_COMPLETION_NAME,
                "popup-set-width", FALSE,
                NULL);

  priv->cancel = g_cancellable_new ();

  priv->request_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);

  GtkCellRenderer * cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, TRUE);
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (self), cell, data_func, NULL, NULL);

  return;
}

static void
timezone_completion_dispose (GObject * object)
{
  G_OBJECT_CLASS (timezone_completion_parent_class)->dispose (object);

  TimezoneCompletion * completion = TIMEZONE_COMPLETION (object);
  TimezoneCompletionPrivate * priv = TIMEZONE_COMPLETION_GET_PRIVATE (completion);

  if (priv->changed_id) {
    g_source_remove (priv->changed_id);
    priv->changed_id = 0;
  }

  if (priv->entry != NULL) {
    g_object_remove_weak_pointer (G_OBJECT (priv->entry), (gpointer *)&priv->entry);
  }

  if (priv->initial_model != NULL) {
    g_object_unref (G_OBJECT (priv->initial_model));
    priv->initial_model = NULL;
  }

  if (priv->queued_request) {
    g_source_remove (priv->queued_request);
    priv->queued_request = 0;
  }

  if (priv->cancel != NULL) {
    g_cancellable_cancel (priv->cancel);
    g_object_unref (priv->cancel);
    priv->cancel = NULL;
  }

  if (priv->request_text != NULL) {
    g_free (priv->request_text);
    priv->request_text = NULL;
  }

  if (priv->request_table != NULL) {
    g_hash_table_destroy (priv->request_table);
    priv->request_table = NULL;
  }

  return;
}

static void
timezone_completion_finalize (GObject * object)
{
  G_OBJECT_CLASS (timezone_completion_parent_class)->finalize (object);
  return;
}

TimezoneCompletion *
timezone_completion_new ()
{
  TimezoneCompletion * self = g_object_new (TIMEZONE_COMPLETION_TYPE, NULL);
  return self;
}