aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/e-contact-map-window.c
blob: b2d13c34f0e17faf25c25ca4ec8098a8d51a8de2 (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
/*
 * e-contact-map-window.c
 *
 * 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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2011 Dan Vratil <dvratil@redhat.com>
 *
 */

/* This defines WITH_CONTACT_MAPS. */
#include <config.h>

#ifdef WITH_CONTACT_MAPS

#include "e-contact-map-window.h"

#include <string.h>
#include <glib/gi18n.h>

#include <champlain/champlain.h>

#include <e-util/e-util.h>

#include "e-contact-map.h"

#define E_CONTACT_MAP_WINDOW_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_CONTACT_MAP_WINDOW, EContactMapWindowPrivate))

struct _EContactMapWindowPrivate {
    EContactMap *map;

    GtkWidget *zoom_in_btn;
    GtkWidget *zoom_out_btn;

    GtkWidget *search_entry;
    GtkListStore *completion_model;

    /* contact name -> marker */
    GHashTable *hash_table;

    GtkWidget *spinner;
    gint tasks_cnt;
};

enum {
    SHOW_CONTACT_EDITOR,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (
    EContactMapWindow,
    e_contact_map_window,
    GTK_TYPE_WINDOW)

static gboolean
contact_map_marker_button_release_event_cb (ClutterActor *actor,
                                            ClutterEvent *event,
                                            EContactMapWindow *window)
{
    const gchar *contact_uid;

    if (clutter_event_get_click_count (event) != 2)
        return FALSE;

    contact_uid = g_object_get_data (G_OBJECT (actor), "contact-uid");
    g_return_val_if_fail (contact_uid != NULL, FALSE);

    g_signal_emit (
        window,
        signals[SHOW_CONTACT_EDITOR], 0,
        contact_uid);

    return TRUE;
}

static void
contact_map_window_get_contacts_cb (GObject *source_object,
                                    GAsyncResult *result,
                                    gpointer user_data)
{
    EContactMapWindow *window;
    GSList *list = NULL, *link;
    GError *local_error = NULL;

    window = E_CONTACT_MAP_WINDOW (user_data);

    e_book_client_get_contacts_finish (
        E_BOOK_CLIENT (source_object),
        result, &list, &local_error);

    if (local_error != NULL) {
        g_warning (
            "%s: Failed to get contacts: %s",
            G_STRFUNC, local_error->message);
        g_error_free (local_error);
    }

    for (link = list; link != NULL; link = g_slist_next (link)) {
        EContact *contact = E_CONTACT (link->data);
        e_contact_map_add_contact (window->priv->map, contact);
    }

    g_slist_free_full (list, (GDestroyNotify) g_object_unref);
}

static void
contact_map_window_zoom_in_cb (GtkButton *button,
                               EContactMapWindow *window)
{
    ChamplainView *view;

    view = e_contact_map_get_view (window->priv->map);

    champlain_view_zoom_in (view);
}

static void
contact_map_window_zoom_out_cb (GtkButton *button,
                                EContactMapWindow *window)
{
    ChamplainView *view;

    view = e_contact_map_get_view (window->priv->map);

    champlain_view_zoom_out (view);
}

static void
contact_map_window_zoom_level_changed_cb (ChamplainView *view,
                                          GParamSpec *pspec,
                                          EContactMapWindow *window)
{
    gint zoom_level;

    zoom_level = champlain_view_get_zoom_level (view);

    gtk_widget_set_sensitive (
        window->priv->zoom_in_btn,
        (zoom_level < champlain_view_get_max_zoom_level (view)));

    gtk_widget_set_sensitive (
        window->priv->zoom_out_btn,
        (zoom_level > champlain_view_get_min_zoom_level (view)));
}

/**
 * Add contact to hash_table only when EContactMap tells us
 * that the contact has really been added to map.
 */
static void
contact_map_window_contact_added_cb (EContactMap *map,
                                     ClutterActor *marker,
                                     EContactMapWindow *window)
{
    GtkListStore *list_store;
    GtkTreeIter iter;
    const gchar *name;

    name = champlain_label_get_text (CHAMPLAIN_LABEL (marker));

    g_hash_table_insert (
        window->priv->hash_table,
        g_strdup (name), marker);

    list_store = window->priv->completion_model;
    gtk_list_store_append (list_store, &iter);
    gtk_list_store_set (list_store, &iter, 0, name, -1);

    g_signal_connect (
        marker, "button-release-event",
        G_CALLBACK (contact_map_marker_button_release_event_cb),
        window);

    window->priv->tasks_cnt--;
    if (window->priv->tasks_cnt == 0) {
        gtk_spinner_stop (GTK_SPINNER (window->priv->spinner));
        gtk_widget_hide (window->priv->spinner);
    }
}

static void
contact_map_window_contact_removed_cb (EContactMap *map,
                                       const gchar *name,
                                       EContactMapWindow *window)
{
    GtkListStore *list_store;
    GtkTreeModel *tree_model;
    GtkTreeIter iter;
    gboolean iter_valid;

    list_store = window->priv->completion_model;
    tree_model = GTK_TREE_MODEL (list_store);

    g_hash_table_remove (window->priv->hash_table, name);

    iter_valid = gtk_tree_model_get_iter_first (tree_model, &iter);

    while (iter_valid) {
        gchar *name_str;
        gboolean match;

        gtk_tree_model_get (tree_model, &iter, 0, &name_str, -1);
        match = (g_ascii_strcasecmp (name_str, name) == 0);
        g_free (name_str);

        if (match) {
            gtk_list_store_remove (list_store, &iter);
            break;
        }

        iter_valid = gtk_tree_model_iter_next (tree_model, &iter);
    }
}

static void
contact_map_window_geocoding_started_cb (EContactMap *map,
                                         ClutterActor *marker,
                                         EContactMapWindow *window)
{
    gtk_spinner_start (GTK_SPINNER (window->priv->spinner));
    gtk_widget_show (window->priv->spinner);

    window->priv->tasks_cnt++;
}

static void
contact_map_window_geocoding_failed_cb (EContactMap *map,
                                        const gchar *name,
                                        EContactMapWindow *window)
{
    window->priv->tasks_cnt--;

    if (window->priv->tasks_cnt == 0) {
        gtk_spinner_stop (GTK_SPINNER (window->priv->spinner));
        gtk_widget_hide (window->priv->spinner);
    }
}

static void
contact_map_window_find_contact_cb (GtkButton *button,
                                    gpointer user_data)
{
    EContactMapWindowPrivate *priv = E_CONTACT_MAP_WINDOW (user_data)->priv;
    ClutterActor *marker;

    marker = g_hash_table_lookup (
        priv->hash_table,
        gtk_entry_get_text (GTK_ENTRY (priv->search_entry)));

    if (marker)
        e_contact_map_zoom_on_marker (priv->map, marker);
}

static gboolean
contact_map_window_entry_key_pressed_cb (GtkWidget *entry,
                                         GdkEventKey *event,
                                         gpointer user_data)
{
    if (event->keyval == GDK_KEY_Return)
        contact_map_window_find_contact_cb (NULL, user_data);

    return FALSE;
}

static gboolean
entry_completion_match_selected_cb (GtkEntryCompletion *widget,
                                    GtkTreeModel *model,
                                    GtkTreeIter *iter,
                                    gpointer user_data)
{
    GValue name_val = {0};
    EContactMapWindowPrivate *priv = E_CONTACT_MAP_WINDOW (user_data)->priv;
    const gchar *name;

    gtk_tree_model_get_value (model, iter, 0, &name_val);
    g_return_val_if_fail (G_VALUE_HOLDS_STRING (&name_val), FALSE);

    name = g_value_get_string (&name_val);
    gtk_entry_set_text (GTK_ENTRY (priv->search_entry), name);

    contact_map_window_find_contact_cb (NULL, user_data);

    return TRUE;
}

static void
contact_map_window_dispose (GObject *object)
{
    EContactMapWindowPrivate *priv;

    priv = E_CONTACT_MAP_WINDOW_GET_PRIVATE (object);

    if (priv->map != NULL) {
        gtk_widget_destroy (GTK_WIDGET (priv->map));
        priv->map = NULL;
    }

    g_clear_object (&priv->completion_model);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_contact_map_window_parent_class)->dispose (object);
}

static void
contact_map_window_finalize (GObject *object)
{
    EContactMapWindowPrivate *priv;

    priv = E_CONTACT_MAP_WINDOW_GET_PRIVATE (object);

    g_hash_table_destroy (priv->hash_table);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_contact_map_window_parent_class)->finalize (object);
}

static void
e_contact_map_window_class_init (EContactMapWindowClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EContactMapWindowPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = contact_map_window_dispose;
    object_class->finalize = contact_map_window_finalize;

    signals[SHOW_CONTACT_EDITOR] = g_signal_new (
        "show-contact-editor",
        G_TYPE_FROM_CLASS (class),
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (EContactMapWindowClass, show_contact_editor),
        NULL, NULL,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1, G_TYPE_STRING);
}

static void
e_contact_map_window_init (EContactMapWindow *window)
{
    EContactMapWindowPrivate *priv;
    GtkWidget *map;
    GtkWidget *button, *entry;
    GtkWidget *hbox, *vbox, *viewport;
    GtkEntryCompletion *entry_completion;
    GtkListStore *completion_model;
    ChamplainView *view;
    GHashTable *hash_table;

    priv = E_CONTACT_MAP_WINDOW_GET_PRIVATE (window);
    window->priv = priv;

    priv->tasks_cnt = 0;

    hash_table = g_hash_table_new_full (
        (GHashFunc) g_str_hash,
        (GEqualFunc) g_str_equal,
        (GDestroyNotify) g_free,
        (GDestroyNotify) NULL);
    priv->hash_table = hash_table;

    gtk_window_set_title (GTK_WINDOW (window), _("Contacts Map"));
    gtk_container_set_border_width (GTK_CONTAINER (window), 12);
    gtk_widget_set_size_request (GTK_WIDGET (window), 800, 600);

    /* The map view itself */
    map = e_contact_map_new ();
    view = e_contact_map_get_view (E_CONTACT_MAP (map));
    champlain_view_set_zoom_level (view, 2);
    priv->map = E_CONTACT_MAP (map);
    e_signal_connect_notify (
        view, "notify::zoom-level",
        G_CALLBACK (contact_map_window_zoom_level_changed_cb), window);
    g_signal_connect (
        map, "contact-added",
        G_CALLBACK (contact_map_window_contact_added_cb), window);
    g_signal_connect (
        map, "contact-removed",
        G_CALLBACK (contact_map_window_contact_removed_cb), window);
    g_signal_connect (
        map, "geocoding-started",
        G_CALLBACK (contact_map_window_geocoding_started_cb), window);
    g_signal_connect (
        map, "geocoding-failed",
        G_CALLBACK (contact_map_window_geocoding_failed_cb), window);

    /* HBox container */
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 7);

    /* Spinner */
    button = gtk_spinner_new ();
    gtk_container_add (GTK_CONTAINER (hbox), button);
    gtk_widget_hide (button);
    priv->spinner = button;

    /* Zoom-in button */
    button = e_dialog_button_new_with_icon ("zoom-in", _("Zoom _In"));
    g_signal_connect (
        button, "clicked",
        G_CALLBACK (contact_map_window_zoom_in_cb), window);
    priv->zoom_in_btn = button;
    gtk_container_add (GTK_CONTAINER (hbox), button);

    /* Zoom-out button */
    button = e_dialog_button_new_with_icon ("zoom-out", _("Zoom _Out"));
    g_signal_connect (
        button, "clicked",
        G_CALLBACK (contact_map_window_zoom_out_cb), window);
    priv->zoom_out_btn = button;
    gtk_container_add (GTK_CONTAINER (hbox), button);

    /* Completion model */
    completion_model = gtk_list_store_new (1, G_TYPE_STRING);
    priv->completion_model = completion_model;

    /* Entry completion */
    entry_completion = gtk_entry_completion_new ();
    gtk_entry_completion_set_model (
        entry_completion, GTK_TREE_MODEL (completion_model));
    gtk_entry_completion_set_text_column (entry_completion, 0);
    g_signal_connect (
        entry_completion, "match-selected",
        G_CALLBACK (entry_completion_match_selected_cb), window);

    /* Search entry */
    entry = gtk_entry_new ();
    gtk_entry_set_completion (GTK_ENTRY (entry), entry_completion);
    g_signal_connect (
        entry, "key-press-event",
        G_CALLBACK (contact_map_window_entry_key_pressed_cb), window);
    window->priv->search_entry = entry;
    gtk_container_add (GTK_CONTAINER (hbox), entry);

    /* Search button */
    button = e_dialog_button_new_with_icon ("edit-find", _("_Find"));
    g_signal_connect (
        button, "clicked",
        G_CALLBACK (contact_map_window_find_contact_cb), window);
    gtk_container_add (GTK_CONTAINER (hbox), button);

    viewport = gtk_frame_new (NULL);
    gtk_container_add (GTK_CONTAINER (viewport), map);

    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
    gtk_container_add (GTK_CONTAINER (vbox), viewport);
    gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

    gtk_container_add (GTK_CONTAINER (window), vbox);

    gtk_widget_show_all (vbox);
    gtk_widget_hide (priv->spinner);
}

EContactMapWindow *
e_contact_map_window_new (void)
{
    return g_object_new (E_TYPE_CONTACT_MAP_WINDOW, NULL);
}

EContactMap *
e_contact_map_window_get_map (EContactMapWindow *window)
{
    g_return_val_if_fail (E_IS_CONTACT_MAP_WINDOW (window), NULL);

    return window->priv->map;
}

/**
 * Gets all contacts from @book and puts them
 * on the map view
 */
void
e_contact_map_window_load_addressbook (EContactMapWindow *map,
                                       EBookClient *book_client)
{
    EBookQuery *book_query;
    gchar *query_string;

    g_return_if_fail (E_IS_CONTACT_MAP_WINDOW (map));
    g_return_if_fail (E_IS_BOOK_CLIENT (book_client));

    book_query = e_book_query_field_exists (E_CONTACT_ADDRESS);
    query_string = e_book_query_to_string (book_query);
    e_book_query_unref (book_query);

    e_book_client_get_contacts (
        book_client, query_string, NULL,
        contact_map_window_get_contacts_cb, map);

    g_free (query_string);
}

#endif /* WITH_CONTACT_MAPS */