2 * Copyright (C) 2008, 2009 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
28 #include <champlain/champlain.h>
29 #include <champlain-gtk/champlain-gtk.h>
30 #include <clutter-gtk/clutter-gtk.h>
31 #include <telepathy-glib/util.h>
33 #include <libempathy/empathy-contact.h>
34 #include <libempathy/empathy-connection-aggregator.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-location.h>
38 #include <libempathy-gtk/empathy-individual-menu.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include "empathy-map-view.h"
43 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
44 #include <libempathy/empathy-debug.h>
46 G_DEFINE_TYPE (EmpathyMapView, empathy_map_view, GTK_TYPE_WINDOW);
48 #define GET_PRIV(self) ((EmpathyMapViewPriv *)((EmpathyMapView *) self)->priv)
50 struct _EmpathyMapViewPriv {
51 EmpathyConnectionAggregator *aggregator;
56 ChamplainView *map_view;
57 ChamplainMarkerLayer *layer;
59 /* reffed (EmpathyContact *) => borrowed (ChamplainMarker *) */
61 gulong members_changed_id;
63 /* TpContact -> EmpathyContact */
68 map_view_state_changed (ChamplainView *view,
73 EmpathyMapViewPriv *priv = GET_PRIV (self);
75 g_object_get (G_OBJECT (view), "state", &state, NULL);
76 if (state == CHAMPLAIN_STATE_LOADING)
78 gtk_spinner_start (GTK_SPINNER (priv->throbber));
79 gtk_widget_show (priv->throbber);
83 gtk_spinner_stop (GTK_SPINNER (priv->throbber));
84 gtk_widget_hide (priv->throbber);
89 contact_has_location (EmpathyContact *contact)
93 location = empathy_contact_get_location (contact);
95 if (location == NULL || g_hash_table_size (location) == 0)
101 static ClutterActor * create_marker (EmpathyMapView *window,
102 EmpathyContact *contact);
105 map_view_update_contact_position (EmpathyMapView *self,
106 EmpathyContact *contact)
108 EmpathyMapViewPriv *priv = GET_PRIV (self);
111 GHashTable *location;
112 ClutterActor *marker;
113 gboolean has_location;
115 has_location = contact_has_location (contact);
117 marker = g_hash_table_lookup (priv->markers, contact);
123 marker = create_marker (self, contact);
125 else if (!has_location)
127 champlain_marker_animate_out (CHAMPLAIN_MARKER (marker));
131 location = empathy_contact_get_location (contact);
133 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
136 clutter_actor_hide (marker);
139 lat = g_value_get_double (value);
141 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
144 clutter_actor_hide (marker);
147 lon = g_value_get_double (value);
149 champlain_location_set_location (CHAMPLAIN_LOCATION (marker), lat, lon);
150 champlain_marker_animate_in (CHAMPLAIN_MARKER (marker));
154 map_view_contact_location_notify (EmpathyContact *contact,
156 EmpathyMapView *self)
158 map_view_update_contact_position (self, contact);
162 map_view_zoom_in_cb (GtkWidget *widget,
163 EmpathyMapView *self)
165 EmpathyMapViewPriv *priv = GET_PRIV (self);
167 champlain_view_zoom_in (priv->map_view);
171 map_view_zoom_out_cb (GtkWidget *widget,
172 EmpathyMapView *self)
174 EmpathyMapViewPriv *priv = GET_PRIV (self);
176 champlain_view_zoom_out (priv->map_view);
180 map_view_zoom_fit_cb (GtkWidget *widget,
181 EmpathyMapView *self)
183 EmpathyMapViewPriv *priv = GET_PRIV (self);
185 champlain_view_ensure_layers_visible (priv->map_view, TRUE);
189 marker_clicked_cb (ChamplainMarker *marker,
190 ClutterButtonEvent *event,
191 EmpathyMapView *self)
194 EmpathyContact *contact;
195 TpContact *tp_contact;
196 FolksIndividual *individual;
198 if (event->button != 3)
201 contact = g_object_get_data (G_OBJECT (marker), "contact");
205 tp_contact = empathy_contact_get_tp_contact (contact);
206 if (tp_contact == NULL)
209 individual = empathy_create_individual_from_tp_contact (tp_contact);
210 if (individual == NULL)
213 menu = empathy_individual_menu_new (individual,
214 EMPATHY_INDIVIDUAL_FEATURE_CHAT |
215 EMPATHY_INDIVIDUAL_FEATURE_CALL |
216 EMPATHY_INDIVIDUAL_FEATURE_LOG |
217 EMPATHY_INDIVIDUAL_FEATURE_INFO, NULL);
222 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), NULL);
224 gtk_widget_show (menu);
225 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
226 event->button, event->time);
229 g_object_unref (individual);
234 map_view_contacts_update_label (ClutterActor *marker)
241 GHashTable *location;
242 EmpathyContact *contact;
244 contact = g_object_get_data (G_OBJECT (marker), "contact");
245 location = empathy_contact_get_location (contact);
246 name = empathy_contact_get_alias (contact);
247 gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
254 loctime = g_value_get_int64 (gtime);
255 date = empathy_time_to_string_relative (loctime);
256 label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
259 now = g_date_time_new_now_utc ();
260 d = g_date_time_new_from_unix_utc (loctime);
261 delta = g_date_time_difference (now, d);
263 /* if location is older than a week */
264 if (delta > G_TIME_SPAN_DAY * 7)
265 clutter_actor_set_opacity (marker, 0.75 * 255);
267 g_date_time_unref (now);
268 g_date_time_unref (d);
272 label = g_strconcat ("<b>", name, "</b>\n", NULL);
275 champlain_label_set_use_markup (CHAMPLAIN_LABEL (marker), TRUE);
276 champlain_label_set_text (CHAMPLAIN_LABEL (marker), label);
281 static ClutterActor *
282 create_marker (EmpathyMapView *self,
283 EmpathyContact *contact)
285 EmpathyMapViewPriv *priv = GET_PRIV (self);
286 ClutterActor *marker;
288 ClutterActor *texture = NULL;
290 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
293 texture = gtk_clutter_texture_new ();
295 gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture),
298 g_object_unref (avatar);
301 marker = champlain_label_new_with_image (texture);
303 g_object_set_data_full (G_OBJECT (marker), "contact",
304 g_object_ref (contact), g_object_unref);
306 g_hash_table_insert (priv->markers, g_object_ref (contact), marker);
308 map_view_contacts_update_label (marker);
310 clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
311 g_signal_connect (marker, "button-release-event",
312 G_CALLBACK (marker_clicked_cb), self);
314 champlain_marker_layer_add_marker (priv->layer, CHAMPLAIN_MARKER (marker));
316 DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
318 tp_clear_object (&texture);
323 map_view_key_press_cb (GtkWidget *widget,
327 if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
328 || event->keyval == GDK_KEY_Escape)
330 gtk_widget_destroy (widget);
338 map_view_tick (EmpathyMapView *self)
340 EmpathyMapViewPriv *priv = GET_PRIV (self);
343 marker = champlain_marker_layer_get_markers (priv->layer);
345 for (l = marker; l != NULL; l = g_list_next (l))
346 map_view_contacts_update_label (l->data);
348 g_list_free (marker);
353 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
356 EmpathyMapView *self)
358 EmpathyMapViewPriv *priv = GET_PRIV (self);
361 for (i = 0; i < added->len; i++)
363 TpContact *tp_contact = g_ptr_array_index (added, i);
364 EmpathyContact *contact;
366 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
369 contact = empathy_contact_dup_from_tp_contact (tp_contact);
371 tp_g_signal_connect_object (contact, "notify::location",
372 G_CALLBACK (map_view_contact_location_notify), self, 0);
374 map_view_update_contact_position (self, contact);
376 /* Pass ownership to the hash table */
377 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact),
381 for (i = 0; i < removed->len; i++)
383 TpContact *tp_contact = g_ptr_array_index (removed, i);
384 EmpathyContact *contact;
385 ClutterActor *marker;
387 contact = g_hash_table_lookup (priv->contacts, tp_contact);
391 marker = g_hash_table_lookup (priv->markers, contact);
394 clutter_actor_destroy (marker);
395 g_hash_table_remove (priv->markers, contact);
398 g_signal_handlers_disconnect_by_func (contact,
399 map_view_contact_location_notify, self);
401 g_hash_table_remove (priv->contacts, tp_contact);
406 empathy_map_view_constructor (GType type,
407 guint n_construct_params,
408 GObjectConstructParam *construct_params)
410 static GObject *window = NULL;
415 window = G_OBJECT_CLASS (empathy_map_view_parent_class)->constructor (
416 type, n_construct_params, construct_params);
418 g_object_add_weak_pointer (window, (gpointer) &window);
424 empathy_map_view_finalize (GObject *object)
426 EmpathyMapViewPriv *priv = GET_PRIV (object);
430 g_source_remove (priv->timeout_id);
432 g_hash_table_iter_init (&iter, priv->markers);
433 while (g_hash_table_iter_next (&iter, &contact, NULL))
434 g_signal_handlers_disconnect_by_func (contact,
435 map_view_contact_location_notify, object);
437 g_hash_table_unref (priv->markers);
438 g_object_unref (priv->aggregator);
439 g_object_unref (priv->layer);
440 g_hash_table_unref (priv->contacts);
442 G_OBJECT_CLASS (empathy_map_view_parent_class)->finalize (object);
446 empathy_map_view_class_init (EmpathyMapViewClass *klass)
448 GObjectClass *object_class = G_OBJECT_CLASS (klass);
450 object_class->constructor = empathy_map_view_constructor;
451 object_class->finalize = empathy_map_view_finalize;
453 g_type_class_add_private (object_class, sizeof (EmpathyMapViewPriv));
457 empathy_map_view_init (EmpathyMapView *self)
459 EmpathyMapViewPriv *priv;
463 GtkWidget *throbber_holder;
465 GPtrArray *contacts, *empty;
466 GtkWidget *main_vbox;
468 priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
469 EMPATHY_TYPE_MAP_VIEW, EmpathyMapViewPriv);
471 gtk_window_set_title (GTK_WINDOW (self), _("Contact Map View"));
472 gtk_window_set_role (GTK_WINDOW (self), "map_view");
473 gtk_window_set_default_size (GTK_WINDOW (self), 512, 384);
474 gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);
476 /* Set up interface */
477 filename = empathy_file_lookup ("empathy-map-view.ui", "src");
478 gui = empathy_builder_get_file (filename,
479 "main_vbox", &main_vbox,
480 "zoom_in", &priv->zoom_in,
481 "zoom_out", &priv->zoom_out,
482 "map_scrolledwindow", &sw,
483 "throbber", &throbber_holder,
487 gtk_container_add (GTK_CONTAINER (self), main_vbox);
489 empathy_builder_connect (gui, self,
490 "zoom_in", "clicked", map_view_zoom_in_cb,
491 "zoom_out", "clicked", map_view_zoom_out_cb,
492 "zoom_fit", "clicked", map_view_zoom_fit_cb,
495 g_signal_connect (self, "key-press-event",
496 G_CALLBACK (map_view_key_press_cb), self);
498 g_object_unref (gui);
500 priv->throbber = gtk_spinner_new ();
501 gtk_widget_set_size_request (priv->throbber, 16, 16);
502 gtk_container_add (GTK_CONTAINER (throbber_holder), priv->throbber);
504 /* Set up map view */
505 embed = gtk_champlain_embed_new ();
506 priv->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
507 g_object_set (G_OBJECT (priv->map_view),
509 "kinetic-mode", TRUE,
511 champlain_view_center_on (priv->map_view, 36, 0);
513 gtk_container_add (GTK_CONTAINER (sw), embed);
514 gtk_widget_show_all (embed);
516 priv->layer = g_object_ref (champlain_marker_layer_new ());
517 champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (priv->layer));
519 g_signal_connect (priv->map_view, "notify::state",
520 G_CALLBACK (map_view_state_changed), self);
522 /* Set up contact list. */
523 priv->markers = g_hash_table_new_full (NULL, NULL,
524 (GDestroyNotify) g_object_unref, NULL);
526 priv->aggregator = empathy_connection_aggregator_dup_singleton ();
527 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
530 tp_g_signal_connect_object (priv->aggregator, "contact-list-changed",
531 G_CALLBACK (contact_list_changed_cb), self, 0);
533 contacts = empathy_connection_aggregator_dup_all_contacts (priv->aggregator);
534 empty = g_ptr_array_new ();
536 contact_list_changed_cb (priv->aggregator, contacts, empty, self);
538 g_ptr_array_unref (contacts);
539 g_ptr_array_unref (empty);
541 /* Set up time updating loop */
542 priv->timeout_id = g_timeout_add_seconds (5,
543 (GSourceFunc) map_view_tick, self);
547 empathy_map_view_show (void)
551 window = g_object_new (EMPATHY_TYPE_MAP_VIEW, NULL);
552 gtk_widget_show_all (window);
553 empathy_window_present (GTK_WINDOW (window));