aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-map-view.c
blob: da5837e43ce6c956fdba759450f7d1326bf54410 (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
/*
 * Copyright (C) 2008, 2009 Collabora Ltd.
 *
 * This library 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.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
 */

#include <config.h>

#include <sys/stat.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include <champlain/champlain.h>
#include <champlain-gtk/champlain-gtk.h>
#include <clutter-gtk/gtk-clutter-embed.h>
#if HAVE_GEOCLUE
#include <geoclue/geoclue-geocode.h>
#endif
#include <telepathy-glib/util.h>

#include <libempathy/empathy-contact.h>
#include <libempathy/empathy-contact-manager.h>
#include <libempathy/empathy-utils.h>
#include <libempathy/empathy-location.h>

#include <libempathy-gtk/empathy-contact-list-store.h>
#include <libempathy-gtk/empathy-contact-list-view.h>
#include <libempathy-gtk/empathy-presence-chooser.h>
#include <libempathy-gtk/empathy-ui-utils.h>

#include "empathy-map-view.h"

#define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
#include <libempathy/empathy-debug.h>

typedef struct {
  EmpathyContactListStore *list_store;

  GtkWidget *window;
  GtkWidget *zoom_in;
  GtkWidget *zoom_out;
  ChamplainView *map_view;
  ChamplainLayer *layer;
#if HAVE_GEOCLUE
  GeoclueGeocode *geocode;
#endif
} EmpathyMapView;

static void map_view_destroy_cb (GtkWidget *widget,
    EmpathyMapView *window);
static gboolean map_view_contacts_foreach (GtkTreeModel *model,
    GtkTreePath *path,
    GtkTreeIter *iter,
    gpointer user_data);
static void map_view_zoom_in_cb (GtkWidget *widget,
    EmpathyMapView *window);
static void map_view_zoom_out_cb (GtkWidget *widget,
    EmpathyMapView *window);
static void map_view_contact_location_notify (GObject *gobject,
    GParamSpec *arg1,
    gpointer user_data);
static gchar * get_dup_string (GHashTable *location,
    gchar *key);

GtkWidget *
empathy_map_view_show ()
{
  static EmpathyMapView *window = NULL;
  GtkBuilder *gui;
  GtkWidget *sw;
  GtkWidget *embed;
  gchar *filename;
  GtkTreeModel *model;
  EmpathyContactList *list_iface;
  EmpathyContactListStore *list_store;

  if (window)
    {
      empathy_window_present (GTK_WINDOW (window->window), TRUE);
      return window->window;
    }

  window = g_slice_new0 (EmpathyMapView);

  /* Set up interface */
  filename = empathy_file_lookup ("empathy-map-view.ui", "src");
  gui = empathy_builder_get_file (filename,
     "map_view", &window->window,
     "zoom_in", &window->zoom_in,
     "zoom_out", &window->zoom_out,
     "map_scrolledwindow", &sw,
     NULL);
  g_free (filename);

  empathy_builder_connect (gui, window,
      "map_view", "destroy", map_view_destroy_cb,
      "zoom_in", "clicked", map_view_zoom_in_cb,
      "zoom_out", "clicked", map_view_zoom_out_cb,
      NULL);

  g_object_unref (gui);

  /* Clear the static pointer to window if the dialog is destroyed */
  g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);

  list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
  list_store = empathy_contact_list_store_new (list_iface);
  empathy_contact_list_store_set_show_groups (list_store, FALSE);
  empathy_contact_list_store_set_show_avatars (list_store, TRUE);
  g_object_unref (list_iface);

  window->list_store = list_store;

  /* Set up map view */
  window->map_view = CHAMPLAIN_VIEW (champlain_view_new ());
  g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
     "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
  champlain_view_center_on (window->map_view, 36, 0);

  embed = champlain_view_embed_new (window->map_view);
  gtk_container_add (GTK_CONTAINER (sw),
     GTK_WIDGET (embed));
  gtk_widget_show_all (embed);

  window->layer = g_object_ref (champlain_layer_new ());
  champlain_view_add_layer (window->map_view, window->layer);

  /* Set up contact list. */
  model = GTK_TREE_MODEL (window->list_store);
  gtk_tree_model_foreach (model, map_view_contacts_foreach, window);

  empathy_window_present (GTK_WINDOW (window->window), TRUE);
  return window->window;
}

static void
map_view_destroy_cb (GtkWidget *widget,
    EmpathyMapView *window)
{
  GtkTreeModel *model;

  g_object_unref (window->list_store);
  g_object_unref (window->layer);
#if HAVE_GEOCLUE
  if (window->geocode != NULL)
    g_object_unref (window->geocode);
#endif
  g_slice_free (EmpathyMapView, window);
}

#if HAVE_GEOCLUE
#define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
#define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"

/* This callback is called by geoclue when it found a position
 * for the given address.  A position is necessary for a contact
 * to show up on the map
 */
static void
map_view_geocode_cb (GeoclueGeocode *geocode,
    GeocluePositionFields fields,
    double latitude,
    double longitude,
    double altitude,
    GeoclueAccuracy *accuracy,
    GError *error,
    gpointer userdata)
{
  GValue *new_value;
  GHashTable *location;

  location = empathy_contact_get_location (EMPATHY_CONTACT (userdata));

  GHashTable *address = g_object_get_data (userdata, "geoclue-address");
  g_hash_table_destroy (address);
  g_object_set_data (userdata, "geoclue-address", NULL);

  if (error != NULL)
    {
      DEBUG ("Error geocoding location : %s", error->message);
      return;
    }

  if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
    {
      new_value = tp_g_value_slice_new_double (longitude);
      g_hash_table_replace (location, EMPATHY_LOCATION_LON, new_value);
      DEBUG ("\t - Longitude: %f", longitude);
    }
  if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
    {
      new_value = tp_g_value_slice_new_double (latitude);
      g_hash_table_replace (location, EMPATHY_LOCATION_LAT, new_value);
      DEBUG ("\t - Latitude: %f", latitude);
    }
  if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
    {
      new_value = tp_g_value_slice_new_double (altitude);
      g_hash_table_replace (location, EMPATHY_LOCATION_ALT, new_value);
      DEBUG ("\t - Altitude: %f", altitude);
    }

  /* Don't change the accuracy as we used an address to get this position */

  /* Ref the location hash table as it will be unref'd in set_location, 
   * and we are only updating it */
  g_hash_table_ref (location);
  empathy_contact_set_location (EMPATHY_CONTACT (userdata), location);
  g_hash_table_unref (location);
}
#endif

static gchar *
get_dup_string (GHashTable *location,
    gchar *key)
{
  GValue *value;

  value = g_hash_table_lookup (location, key);
  if (value != NULL)
    return g_value_dup_string (value);

  return NULL;
}

static void
map_view_marker_update_position (ChamplainMarker *marker,
    EmpathyContact *contact)
{
  gdouble lon, lat;
  GValue *value;
  GHashTable *location;

  location = empathy_contact_get_location (contact);
  if (location == NULL ||
      g_hash_table_size (location) == 0)
  {
    clutter_actor_hide (CLUTTER_ACTOR (marker));
    return;
  }

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
  if (value == NULL)
    {
      clutter_actor_hide (CLUTTER_ACTOR (marker));
      return;
    }
  lat = g_value_get_double (value);

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
  if (value == NULL)
    {
      clutter_actor_hide (CLUTTER_ACTOR (marker));
      return;
    }
  lon = g_value_get_double (value);

  clutter_actor_show (CLUTTER_ACTOR (marker));
  champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
}

static void
map_view_contact_location_notify (GObject *gobject,
    GParamSpec *arg1,
    gpointer user_data)
{
  ChamplainMarker *marker = CHAMPLAIN_MARKER (user_data);
  EmpathyContact *contact = EMPATHY_CONTACT (gobject);
  map_view_marker_update_position (marker, contact);
}

static gboolean
map_view_contacts_foreach (GtkTreeModel *model,
    GtkTreePath *path,
    GtkTreeIter *iter,
    gpointer user_data)
{
  EmpathyMapView *window = (EmpathyMapView*) user_data;
  EmpathyContact *contact;
  ClutterActor *marker;
  ClutterActor *texture;
  GHashTable *location;
  GdkPixbuf *avatar;
  const gchar *name;

  gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
     &contact, -1);

  if (contact == NULL)
    return FALSE;

  location = empathy_contact_get_location (contact);

  if (location == NULL)
    return FALSE;

  marker = champlain_marker_new ();

  avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
  if (avatar != NULL)
    {
      texture = clutter_texture_new ();
      gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar);
      champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
    }
  else
    champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);

  name = empathy_contact_get_name (contact);
  champlain_marker_set_text (CHAMPLAIN_MARKER (marker), name);

  clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);

  g_signal_connect (contact, "notify::location",
      G_CALLBACK (map_view_contact_location_notify), marker);


#if HAVE_GEOCLUE
  gchar *str;
  GHashTable *address;
  GValue *value;

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
  if (value == NULL)
      {
        if (window->geocode == NULL)
          window->geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);

        address = geoclue_address_details_new();

        str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
        if (str != NULL)
          g_hash_table_insert (address, g_strdup ("country"), str);

        str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
        if (str != NULL)
          g_hash_table_insert (address, g_strdup ("postalcode"), str);

        str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
        if (str != NULL)
          g_hash_table_insert (address, g_strdup ("locality"), str);

        str = get_dup_string (location, EMPATHY_LOCATION_STREET);
        if (str != NULL)
          g_hash_table_insert (address, g_strdup ("street"), str);

        g_object_set_data (G_OBJECT (contact), "geoclue-address", address);

        geoclue_geocode_address_to_position_async (window->geocode, address,
            map_view_geocode_cb, contact);
      }
#endif

  map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);

  g_object_unref (contact);
  return FALSE;
}

static void
map_view_zoom_in_cb (GtkWidget *widget,
    EmpathyMapView *window)
{
  champlain_view_zoom_in (window->map_view);
}

static void
map_view_zoom_out_cb (GtkWidget *widget,
    EmpathyMapView *window)
{
  champlain_view_zoom_out (window->map_view);
}