aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/e-timezone-dialog/e-timezone-dialog.c
blob: 3dc5f35709d9ce6ae6686f3fe4fa018fe1503fa2 (plain) (blame)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Damon Chaplin <damon@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <time.h>
#include <string.h>
#include <glib/gi18n.h>
#include <misc/e-map.h>
#include <libecal/e-cal-time-util.h>
#include <libecal/e-cal-system-timezone.h>

#include "e-util/e-util.h"
#include "e-util/e-util-private.h"

#include "e-timezone-dialog.h"

#ifdef G_OS_WIN32
/* Undef the similar macros from pthread.h, they don't check if
 * gmtime() and localtime() return NULL.
 */
#undef gmtime_r
#undef localtime_r

/* The gmtime() and localtime() in Microsoft's C library are MT-safe */
#define gmtime_r(tp,tmp) (gmtime(tp)?(*(tmp)=*gmtime(tp),(tmp)):0)
#define localtime_r(tp,tmp) (localtime(tp)?(*(tmp)=*localtime(tp),(tmp)):0)
#endif

#define E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA 0xc070a0ff
#define E_TIMEZONE_DIALOG_MAP_POINT_HOVER_RGBA 0xffff60ff
#define E_TIMEZONE_DIALOG_MAP_POINT_SELECTED_1_RGBA 0xff60e0ff
#define E_TIMEZONE_DIALOG_MAP_POINT_SELECTED_2_RGBA 0x000000ff

struct _ETimezoneDialogPrivate {
    /* The selected timezone. May be NULL for a 'local time' (i.e. when
       the displayed name is ""). */
    icaltimezone *zone;

    GtkBuilder *builder;

    EMapPoint *point_selected;
    EMapPoint *point_hover;

    EMap *map;

    /* The timeout used to flash the nearest point. */
    guint timeout_id;

    /* Widgets from the UI file */
    GtkWidget *app;
    GtkWidget *table;
    GtkWidget *map_window;
    GtkWidget *timezone_combo;
    GtkWidget *preview_label;
};

static void e_timezone_dialog_dispose       (GObject    *object);
static void e_timezone_dialog_finalize      (GObject    *object);

static gboolean get_widgets         (ETimezoneDialog *etd);
static gboolean on_map_timeout          (gpointer    data);
static gboolean on_map_motion           (GtkWidget  *widget,
                         GdkEventMotion *event,
                         gpointer    data);
static gboolean on_map_leave            (GtkWidget  *widget,
                         GdkEventCrossing *event,
                         gpointer    data);
static gboolean on_map_visibility_changed   (GtkWidget  *w,
                         GdkEventVisibility *event,
                         gpointer    data);
static gboolean on_map_button_pressed       (GtkWidget  *w,
                         GdkEventButton *event,
                         gpointer    data);

static icaltimezone* get_zone_from_point    (ETimezoneDialog *etd,
                         EMapPoint  *point);
static void set_map_timezone        (ETimezoneDialog *etd,
                         icaltimezone    *zone);
static void on_combo_changed        (GtkComboBox    *combo,
                         ETimezoneDialog *etd);

static void timezone_combo_get_active_text  (GtkComboBox *combo,
                         const gchar **zone_name);
static gboolean timezone_combo_set_active_text  (GtkComboBox *combo,
                         const gchar *zone_name);

static void map_destroy_cb          (gpointer data,
                         GObject *where_object_was);

G_DEFINE_TYPE (ETimezoneDialog, e_timezone_dialog, G_TYPE_OBJECT)

/* Class initialization function for the event editor */
static void
e_timezone_dialog_class_init (ETimezoneDialogClass *class)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose  = e_timezone_dialog_dispose;
    object_class->finalize = e_timezone_dialog_finalize;
}

/* Object initialization function for the event editor */
static void
e_timezone_dialog_init (ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;

    priv = g_new0 (ETimezoneDialogPrivate, 1);
    etd->priv = priv;

    priv->point_selected = NULL;
    priv->point_hover = NULL;
    priv->timeout_id = 0;
}

/* Dispose handler for the event editor */
static void
e_timezone_dialog_dispose (GObject *object)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (E_IS_TIMEZONE_DIALOG (object));

    etd = E_TIMEZONE_DIALOG (object);
    priv = etd->priv;

    /* Destroy the actual dialog. */
    if (priv->app != NULL) {
        gtk_widget_destroy (priv->app);
        priv->app = NULL;
    }

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

    if (priv->builder) {
        g_object_unref (priv->builder);
        priv->builder = NULL;
    }

    (* G_OBJECT_CLASS (e_timezone_dialog_parent_class)->dispose) (object);
}

/* Finalize handler for the event editor */
static void
e_timezone_dialog_finalize (GObject *object)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (E_IS_TIMEZONE_DIALOG (object));

    etd = E_TIMEZONE_DIALOG (object);
    priv = etd->priv;

    g_free (priv);

    (* G_OBJECT_CLASS (e_timezone_dialog_parent_class)->finalize) (object);
}

static void
e_timezone_dialog_add_timezones (ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;
    icalarray *zones;
    GtkComboBox *combo;
    GList *l, *list_items = NULL;
    GtkListStore *list_store;
    GtkTreeIter iter;
    GtkCellRenderer *cell;
    GHashTable *index;
    gint i;

    priv = etd->priv;

    /* Get the array of builtin timezones. */
    zones = icaltimezone_get_builtin_timezones ();

    for (i = 0; i < zones->num_elements; i++) {
        icaltimezone *zone;
        gchar *location;

        zone = icalarray_element_at (zones, i);

        location = _(icaltimezone_get_location (zone));

        e_map_add_point (priv->map, location,
                 icaltimezone_get_longitude (zone),
                 icaltimezone_get_latitude (zone),
                 E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA);

        list_items = g_list_prepend (list_items, location);
    }

    list_items = g_list_sort (list_items, (GCompareFunc) g_utf8_collate);

    /* Put the "UTC" entry at the top of the combo's list. */
    list_items = g_list_prepend (list_items, _("UTC"));

    combo = GTK_COMBO_BOX (priv->timezone_combo);

    cell = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start ((GtkCellLayout *) combo, cell, TRUE);
    gtk_cell_layout_set_attributes ((GtkCellLayout *) combo, cell, "text", 0, NULL);

    list_store = gtk_list_store_new (1, G_TYPE_STRING);
    index = g_hash_table_new (g_str_hash, g_str_equal);
    for (l = list_items, i = 0; l != NULL; l = l->next, ++i) {
        gtk_list_store_append (list_store, &iter);
        gtk_list_store_set (list_store, &iter, 0, (gchar *)(l->data), -1);
        g_hash_table_insert (index, (gchar *)(l->data), GINT_TO_POINTER (i));
    }

    g_object_set_data_full (
        G_OBJECT (list_store), "index", index,
        (GDestroyNotify) g_hash_table_destroy);

    gtk_combo_box_set_model (combo, (GtkTreeModel *) list_store);

    gtk_rc_parse_string (
        "style \"e-timezone-combo-style\" {\n"
        "  GtkComboBox::appears-as-list = 1\n"
        "}\n"
        "\n"
        "widget \"*.e-timezone-dialog-combo\" style \"e-timezone-combo-style\"");

    gtk_widget_set_name (priv->timezone_combo, "e-timezone-dialog-combo");

    g_list_free (list_items);
}

ETimezoneDialog *
e_timezone_dialog_construct (ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;
    GtkWidget *map;

    g_return_val_if_fail (etd != NULL, NULL);
    g_return_val_if_fail (E_IS_TIMEZONE_DIALOG (etd), NULL);

    priv = etd->priv;

    /* Load the content widgets */

    priv->builder = gtk_builder_new ();
    e_load_ui_builder_definition (priv->builder, "e-timezone-dialog.ui");

    if (!get_widgets (etd)) {
        g_message (
            "%s(): Could not find all widgets in the XML file!",
            G_STRFUNC);
        goto error;
    }

    gtk_container_set_border_width (
        GTK_CONTAINER (GTK_DIALOG (priv->app)->vbox), 0);
    gtk_container_set_border_width (
        GTK_CONTAINER (GTK_DIALOG (priv->app)->action_area), 12);

    priv->map = e_map_new ();
    map = GTK_WIDGET (priv->map);

    g_object_weak_ref(G_OBJECT(map), map_destroy_cb, priv);

    gtk_widget_set_events (map, gtk_widget_get_events (map)
                   | GDK_LEAVE_NOTIFY_MASK
                   | GDK_VISIBILITY_NOTIFY_MASK);

    e_timezone_dialog_add_timezones (etd);

    gtk_container_add (GTK_CONTAINER (priv->map_window), map);
    gtk_widget_show (map);

    /* Ensure a reasonable minimum amount of map is visible */
    gtk_widget_set_size_request (priv->map_window, 200, 200);

    g_signal_connect (
        map, "motion-notify-event",
        G_CALLBACK (on_map_motion), etd);
    g_signal_connect (
        map, "leave-notify-event",
        G_CALLBACK (on_map_leave), etd);
    g_signal_connect (
        map, "visibility-notify-event",
        G_CALLBACK (on_map_visibility_changed), etd);
    g_signal_connect (
        map, "button-press-event",
        G_CALLBACK (on_map_button_pressed), etd);

    g_signal_connect (
        priv->timezone_combo, "changed",
        G_CALLBACK (on_combo_changed), etd);

    return etd;

 error:

    g_object_unref (etd);
    return NULL;
}

#if 0
static gint
get_local_offset (void)
{
    time_t now = time(NULL), t_gmt, t_local;
    struct tm gmt, local;
    gint diff;

    gmtime_r (&now, &gmt);
    localtime_r (&now, &local);
    t_gmt = mktime (&gmt);
    t_local = mktime (&local);
    diff = t_local - t_gmt;

    return diff;
}
#endif

static icaltimezone*
get_local_timezone(void)
{
    icaltimezone *zone;
    gchar *location;

    tzset();
    location = e_cal_system_timezone_get_location ();

    if (location)
        zone = icaltimezone_get_builtin_timezone (location);
    else
        zone = icaltimezone_get_utc_timezone ();

    g_free (location);

    return zone;
}

/* Gets the widgets from the XML file and returns if they are all available.
 * For the widgets whose values can be simply set with e-dialog-utils, it does
 * that as well.
 */
static gboolean
get_widgets (ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;
    GtkBuilder *builder;

    priv = etd->priv;
    builder = etd->priv->builder;

    priv->app = e_builder_get_widget (builder, "timezone-dialog");
    priv->map_window = e_builder_get_widget (builder, "map-window");
    priv->timezone_combo = e_builder_get_widget (builder, "timezone-combo");
    priv->table = e_builder_get_widget (builder, "timezone-table");
    priv->preview_label = e_builder_get_widget (builder, "preview-label");

    return (priv->app
        && priv->map_window
        && priv->timezone_combo
        && priv->table
        && priv->preview_label);
}

/**
 * e_timezone_dialog_new:
 *
 * Creates a new event editor dialog.
 *
 * Return value: A newly-created event editor dialog, or NULL if the event
 * editor could not be created.
 **/
ETimezoneDialog *
e_timezone_dialog_new (void)
{
    ETimezoneDialog *etd;

    etd = E_TIMEZONE_DIALOG (g_object_new (E_TYPE_TIMEZONE_DIALOG, NULL));
    return e_timezone_dialog_construct (E_TIMEZONE_DIALOG (etd));
}

static void
format_utc_offset           (gint        utc_offset,
                     gchar      *buffer)
{
  const gchar *sign = "+";
  gint hours, minutes, seconds;

  if (utc_offset < 0) {
    utc_offset = -utc_offset;
    sign = "-";
  }

  hours = utc_offset / 3600;
  minutes = (utc_offset % 3600) / 60;
  seconds = utc_offset % 60;

  /* Sanity check. Standard timezone offsets shouldn't be much more than 12
     hours, and daylight saving shouldn't change it by more than a few hours.
     (The maximum offset is 15 hours 56 minutes at present.) */
  if (hours < 0 || hours >= 24 || minutes < 0 || minutes >= 60
      || seconds < 0 || seconds >= 60) {
    fprintf (stderr, "Warning: Strange timezone offset: H:%i M:%i S:%i\n",
         hours, minutes, seconds);
  }

  if (hours == 0 && minutes == 0 && seconds == 0)
      strcpy (buffer, _("UTC"));
  else if (seconds == 0)
      sprintf (buffer, "%s %s%02i:%02i", _("UTC"), sign, hours, minutes);
  else
      sprintf (buffer, "%s %s%02i:%02i:%02i", _("UTC"), sign, hours, minutes, seconds);
}

static gchar *
zone_display_name_with_offset (icaltimezone *zone)
{
    const gchar *display_name;
    struct tm local;
    struct icaltimetype tt;
    gint offset;
    gchar buffer [100];
    time_t now = time(NULL);

    gmtime_r ((const time_t *) &now, &local);
    tt = tm_to_icaltimetype (&local, TRUE);
    offset = icaltimezone_get_utc_offset(zone, &tt, NULL);

    format_utc_offset (offset, buffer);

    display_name = icaltimezone_get_display_name (zone);
    if (icaltimezone_get_builtin_timezone (display_name))
        display_name = _(display_name);

    return g_strdup_printf("%s (%s)\n", display_name, buffer);;
}

static const gchar *
zone_display_name (icaltimezone *zone)
{
    const gchar *display_name;

    display_name = icaltimezone_get_display_name (zone);
    if (icaltimezone_get_builtin_timezone (display_name))
        display_name = _(display_name);

    return display_name;
}

/* This flashes the currently selected timezone in the map. */
static gboolean
on_map_timeout (gpointer data)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;

    etd = E_TIMEZONE_DIALOG (data);
    priv = etd->priv;

    if (!priv->point_selected)
        return TRUE;

        if (e_map_point_get_color_rgba (priv->point_selected)
        == E_TIMEZONE_DIALOG_MAP_POINT_SELECTED_1_RGBA)
        e_map_point_set_color_rgba (priv->map, priv->point_selected,
                        E_TIMEZONE_DIALOG_MAP_POINT_SELECTED_2_RGBA);
    else
        e_map_point_set_color_rgba (priv->map, priv->point_selected,
                        E_TIMEZONE_DIALOG_MAP_POINT_SELECTED_1_RGBA);

    return TRUE;
}

static gboolean
on_map_motion (GtkWidget *widget, GdkEventMotion *event, gpointer data)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;
    gdouble longitude, latitude;
    icaltimezone *new_zone;
    gchar *display=NULL;

    etd = E_TIMEZONE_DIALOG (data);
    priv = etd->priv;

    e_map_window_to_world (priv->map, (gdouble) event->x, (gdouble) event->y,
                   &longitude, &latitude);

    if (priv->point_hover && priv->point_hover != priv->point_selected)
        e_map_point_set_color_rgba (priv->map, priv->point_hover,
                        E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA);

    priv->point_hover = e_map_get_closest_point (priv->map, longitude,
                             latitude, TRUE);

    if (priv->point_hover != priv->point_selected)
        e_map_point_set_color_rgba (priv->map, priv->point_hover,
                        E_TIMEZONE_DIALOG_MAP_POINT_HOVER_RGBA);

    new_zone = get_zone_from_point (etd, priv->point_hover);

    display = zone_display_name_with_offset(new_zone);
    gtk_label_set_text (GTK_LABEL (priv->preview_label), display);

    g_free (display);

    return TRUE;
}

static gboolean
on_map_leave (GtkWidget *widget, GdkEventCrossing *event, gpointer data)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;

    etd = E_TIMEZONE_DIALOG (data);
    priv = etd->priv;

    /* We only want to reset the hover point if this is a normal leave
       event. For some reason we are getting leave events when the
       button is pressed in the map, which causes problems. */
    if (event->mode != GDK_CROSSING_NORMAL)
        return FALSE;

    if (priv->point_hover && priv->point_hover != priv->point_selected)
        e_map_point_set_color_rgba (priv->map, priv->point_hover,
                        E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA);

    timezone_combo_set_active_text (GTK_COMBO_BOX (priv->timezone_combo),
                    zone_display_name (priv->zone));
    gtk_label_set_text (GTK_LABEL (priv->preview_label), "");

    priv->point_hover = NULL;

    return FALSE;
}

static gboolean
on_map_visibility_changed (GtkWidget *w, GdkEventVisibility *event,
               gpointer data)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;

    etd = E_TIMEZONE_DIALOG (data);
    priv = etd->priv;

    if (event->state != GDK_VISIBILITY_FULLY_OBSCURED) {
        /* Map is visible, at least partly, so make sure we flash the
           selected point. */
        if (!priv->timeout_id)
            priv->timeout_id = g_timeout_add (100, on_map_timeout, etd);
    } else {
        /* Map is invisible, so don't waste resources on the timeout.*/
        if (priv->timeout_id) {
            g_source_remove (priv->timeout_id);
            priv->timeout_id = 0;
        }
    }

    return FALSE;
}

static gboolean
on_map_button_pressed (GtkWidget *w, GdkEventButton *event, gpointer data)
{
    ETimezoneDialog *etd;
    ETimezoneDialogPrivate *priv;
    gdouble longitude, latitude;

    etd = E_TIMEZONE_DIALOG (data);
    priv = etd->priv;

    e_map_window_to_world (priv->map, (gdouble) event->x, (gdouble) event->y,
                   &longitude, &latitude);

    if (event->button != 1) {
        e_map_zoom_out (priv->map);
    } else {
        if (e_map_get_magnification (priv->map) <= 1.0)
            e_map_zoom_to_location (priv->map, longitude,
                        latitude);

        if (priv->point_selected)
            e_map_point_set_color_rgba (priv->map,
                            priv->point_selected,
                            E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA);
        priv->point_selected = priv->point_hover;

        priv->zone = get_zone_from_point (etd, priv->point_selected);
        timezone_combo_set_active_text (GTK_COMBO_BOX (priv->timezone_combo),
                        zone_display_name (priv->zone));
    }

    return TRUE;
}

/* Returns the translated timezone location of the given EMapPoint,
   e.g. "Europe/London". */
static icaltimezone *
get_zone_from_point (ETimezoneDialog *etd,
             EMapPoint *point)
{
    icalarray *zones;
    gdouble longitude, latitude;
    gint i;

    if (point == NULL)
        return NULL;

    e_map_point_get_location (point, &longitude, &latitude);

    /* Get the array of builtin timezones. */
    zones = icaltimezone_get_builtin_timezones ();

    for (i = 0; i < zones->num_elements; i++) {
        icaltimezone *zone;
        gdouble zone_longitude, zone_latitude;

        zone = icalarray_element_at (zones, i);
        zone_longitude = icaltimezone_get_longitude (zone);
        zone_latitude = icaltimezone_get_latitude (zone);

        if (zone_longitude - 0.005 <= longitude &&
            zone_longitude + 0.005 >= longitude &&
            zone_latitude - 0.005 <= latitude &&
            zone_latitude + 0.005 >= latitude)
        {
            return zone;
        }
    }

    g_return_val_if_reached(NULL);
}

/**
 * e_timezone_dialog_get_timezone:
 * @etd: the timezone dialog
 *
 * Return value: the currently-selected timezone, or %NULL if no timezone
 * is selected.
 **/
icaltimezone *
e_timezone_dialog_get_timezone (ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;

    g_return_val_if_fail (E_IS_TIMEZONE_DIALOG (etd), NULL);

    priv = etd->priv;

    return priv->zone;
}

/**
 * e_timezone_dialog_set_timezone:
 * @etd: the timezone dialog
 * @zone: the timezone
 *
 * Sets the timezone of @etd to @zone. Updates the display name and
 * selected location. The caller must ensure that @zone is not freed
 * before @etd is destroyed.
 **/

void
e_timezone_dialog_set_timezone (ETimezoneDialog *etd,
                icaltimezone    *zone)
{
    ETimezoneDialogPrivate *priv;
    gchar *display = NULL;

    g_return_if_fail (E_IS_TIMEZONE_DIALOG (etd));

    if (!zone)
        zone = get_local_timezone();

    if (zone)
        display = zone_display_name_with_offset(zone);

    priv = etd->priv;

    priv->zone = zone;

    gtk_label_set_text (GTK_LABEL (priv->preview_label),
                zone ? display : "");
    timezone_combo_set_active_text (GTK_COMBO_BOX (priv->timezone_combo),
                    zone ? zone_display_name(zone) : "");

    set_map_timezone (etd, zone);
    g_free (display);
}

GtkWidget *
e_timezone_dialog_get_toplevel  (ETimezoneDialog  *etd)
{
    ETimezoneDialogPrivate *priv;

    g_return_val_if_fail (etd != NULL, NULL);
    g_return_val_if_fail (E_IS_TIMEZONE_DIALOG (etd), NULL);

    priv = etd->priv;

    return priv->app;
}

static void
set_map_timezone (ETimezoneDialog *etd, icaltimezone *zone)
{
    ETimezoneDialogPrivate *priv;
    EMapPoint *point;
    gdouble zone_longitude, zone_latitude;

    priv = etd->priv;

    if (zone) {
        zone_longitude = icaltimezone_get_longitude (zone);
        zone_latitude = icaltimezone_get_latitude (zone);
        point = e_map_get_closest_point (priv->map,
                         zone_longitude,
                         zone_latitude,
                         FALSE);
    } else
        point = NULL;

    if (priv->point_selected)
        e_map_point_set_color_rgba (priv->map, priv->point_selected,
                        E_TIMEZONE_DIALOG_MAP_POINT_NORMAL_RGBA);

    priv->point_selected = point;
}

static void
on_combo_changed (GtkComboBox *combo_box, ETimezoneDialog *etd)
{
    ETimezoneDialogPrivate *priv;
    const gchar *new_zone_name;
    icalarray *zones;
    icaltimezone *map_zone = NULL;
    gchar *location;
    gint i;

    priv = etd->priv;

    timezone_combo_get_active_text (GTK_COMBO_BOX (priv->timezone_combo), &new_zone_name);

    if (!*new_zone_name)
        priv->zone = NULL;
    else if (!g_utf8_collate (new_zone_name, _("UTC")))
        priv->zone = icaltimezone_get_utc_timezone ();
    else {
        priv->zone = NULL;

        zones = icaltimezone_get_builtin_timezones ();
        for (i = 0; i < zones->num_elements; i++) {
            map_zone = icalarray_element_at (zones, i);
            location = _(icaltimezone_get_location (map_zone));
            if (!g_utf8_collate (new_zone_name, location)) {
                priv->zone = map_zone;
                break;
            }
        }
    }

    set_map_timezone (etd, map_zone);
}

static void
timezone_combo_get_active_text (GtkComboBox *combo, const gchar **zone_name)
{
    GtkTreeModel *list_store;
    GtkTreeIter iter;

    list_store = gtk_combo_box_get_model (combo);

    /* Get the active iter in the list */
    if (gtk_combo_box_get_active_iter (combo, &iter))
        gtk_tree_model_get (list_store, &iter, 0, zone_name, -1);
    else
        *zone_name = "";
}

static gboolean
timezone_combo_set_active_text (GtkComboBox *combo, const gchar *zone_name)
{
    GtkTreeModel *list_store;
    GHashTable *index;
    gpointer id = NULL;

    list_store = gtk_combo_box_get_model (combo);
    index = (GHashTable *) g_object_get_data (G_OBJECT (list_store), "index");

    if (zone_name && *zone_name)
        id = g_hash_table_lookup (index, zone_name);

    gtk_combo_box_set_active (combo, GPOINTER_TO_INT (id));

    return (id != NULL);
}

static void
map_destroy_cb(gpointer data, GObject *where_object_was)
{

    ETimezoneDialogPrivate *priv = data;
    if (priv->timeout_id) {
        g_source_remove(priv->timeout_id);
        priv->timeout_id = 0;
    }
    return;
}