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-contact-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;
196 if (event->button != 3)
199 contact = g_object_get_data (G_OBJECT (marker), "contact");
201 menu = empathy_contact_menu_new (contact,
202 EMPATHY_CONTACT_FEATURE_CHAT |
203 EMPATHY_CONTACT_FEATURE_CALL |
204 EMPATHY_CONTACT_FEATURE_LOG |
205 EMPATHY_CONTACT_FEATURE_FT |
206 EMPATHY_CONTACT_FEATURE_INFO);
211 gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), NULL);
213 gtk_widget_show (menu);
214 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
215 event->button, event->time);
221 map_view_contacts_update_label (ClutterActor *marker)
228 GHashTable *location;
229 EmpathyContact *contact;
231 contact = g_object_get_data (G_OBJECT (marker), "contact");
232 location = empathy_contact_get_location (contact);
233 name = empathy_contact_get_alias (contact);
234 gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
241 loctime = g_value_get_int64 (gtime);
242 date = empathy_time_to_string_relative (loctime);
243 label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
246 now = g_date_time_new_now_utc ();
247 d = g_date_time_new_from_unix_utc (loctime);
248 delta = g_date_time_difference (now, d);
250 /* if location is older than a week */
251 if (delta > G_TIME_SPAN_DAY * 7)
252 clutter_actor_set_opacity (marker, 0.75 * 255);
254 g_date_time_unref (now);
255 g_date_time_unref (d);
259 label = g_strconcat ("<b>", name, "</b>\n", NULL);
262 champlain_label_set_use_markup (CHAMPLAIN_LABEL (marker), TRUE);
263 champlain_label_set_text (CHAMPLAIN_LABEL (marker), label);
268 static ClutterActor *
269 create_marker (EmpathyMapView *self,
270 EmpathyContact *contact)
272 EmpathyMapViewPriv *priv = GET_PRIV (self);
273 ClutterActor *marker;
275 ClutterActor *texture = NULL;
277 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
280 texture = gtk_clutter_texture_new ();
282 gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture),
285 g_object_unref (avatar);
288 marker = champlain_label_new_with_image (texture);
290 g_object_set_data_full (G_OBJECT (marker), "contact",
291 g_object_ref (contact), g_object_unref);
293 g_hash_table_insert (priv->markers, g_object_ref (contact), marker);
295 map_view_contacts_update_label (marker);
297 clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
298 g_signal_connect (marker, "button-release-event",
299 G_CALLBACK (marker_clicked_cb), self);
301 champlain_marker_layer_add_marker (priv->layer, CHAMPLAIN_MARKER (marker));
303 DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
305 tp_clear_object (&texture);
310 map_view_key_press_cb (GtkWidget *widget,
314 if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
315 || event->keyval == GDK_KEY_Escape)
317 gtk_widget_destroy (widget);
325 map_view_tick (EmpathyMapView *self)
327 EmpathyMapViewPriv *priv = GET_PRIV (self);
330 marker = champlain_marker_layer_get_markers (priv->layer);
332 for (l = marker; l != NULL; l = g_list_next (l))
333 map_view_contacts_update_label (l->data);
335 g_list_free (marker);
340 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
343 EmpathyMapView *self)
345 EmpathyMapViewPriv *priv = GET_PRIV (self);
348 for (i = 0; i < added->len; i++)
350 TpContact *tp_contact = g_ptr_array_index (added, i);
351 EmpathyContact *contact;
353 if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
356 contact = empathy_contact_dup_from_tp_contact (tp_contact);
358 tp_g_signal_connect_object (contact, "notify::location",
359 G_CALLBACK (map_view_contact_location_notify), self, 0);
361 map_view_update_contact_position (self, contact);
363 /* Pass ownership to the hash table */
364 g_hash_table_insert (priv->contacts, g_object_ref (tp_contact),
368 for (i = 0; i < removed->len; i++)
370 TpContact *tp_contact = g_ptr_array_index (removed, i);
371 EmpathyContact *contact;
372 ClutterActor *marker;
374 contact = g_hash_table_lookup (priv->contacts, tp_contact);
378 marker = g_hash_table_lookup (priv->markers, contact);
381 clutter_actor_destroy (marker);
382 g_hash_table_remove (priv->markers, contact);
385 g_signal_handlers_disconnect_by_func (contact,
386 map_view_contact_location_notify, self);
388 g_hash_table_remove (priv->contacts, tp_contact);
393 empathy_map_view_constructor (GType type,
394 guint n_construct_params,
395 GObjectConstructParam *construct_params)
397 static GObject *window = NULL;
402 window = G_OBJECT_CLASS (empathy_map_view_parent_class)->constructor (
403 type, n_construct_params, construct_params);
405 g_object_add_weak_pointer (window, (gpointer) &window);
411 empathy_map_view_finalize (GObject *object)
413 EmpathyMapViewPriv *priv = GET_PRIV (object);
417 g_source_remove (priv->timeout_id);
419 g_hash_table_iter_init (&iter, priv->markers);
420 while (g_hash_table_iter_next (&iter, &contact, NULL))
421 g_signal_handlers_disconnect_by_func (contact,
422 map_view_contact_location_notify, object);
424 g_hash_table_unref (priv->markers);
425 g_object_unref (priv->aggregator);
426 g_object_unref (priv->layer);
427 g_hash_table_unref (priv->contacts);
429 G_OBJECT_CLASS (empathy_map_view_parent_class)->finalize (object);
433 empathy_map_view_class_init (EmpathyMapViewClass *klass)
435 GObjectClass *object_class = G_OBJECT_CLASS (klass);
437 object_class->constructor = empathy_map_view_constructor;
438 object_class->finalize = empathy_map_view_finalize;
440 g_type_class_add_private (object_class, sizeof (EmpathyMapViewPriv));
444 empathy_map_view_init (EmpathyMapView *self)
446 EmpathyMapViewPriv *priv;
450 GtkWidget *throbber_holder;
452 GPtrArray *contacts, *empty;
453 GtkWidget *main_vbox;
455 priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
456 EMPATHY_TYPE_MAP_VIEW, EmpathyMapViewPriv);
458 gtk_window_set_title (GTK_WINDOW (self), _("Contact Map View"));
459 gtk_window_set_role (GTK_WINDOW (self), "map_view");
460 gtk_window_set_default_size (GTK_WINDOW (self), 512, 384);
461 gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);
463 /* Set up interface */
464 filename = empathy_file_lookup ("empathy-map-view.ui", "src");
465 gui = empathy_builder_get_file (filename,
466 "main_vbox", &main_vbox,
467 "zoom_in", &priv->zoom_in,
468 "zoom_out", &priv->zoom_out,
469 "map_scrolledwindow", &sw,
470 "throbber", &throbber_holder,
474 gtk_container_add (GTK_CONTAINER (self), main_vbox);
476 empathy_builder_connect (gui, self,
477 "zoom_in", "clicked", map_view_zoom_in_cb,
478 "zoom_out", "clicked", map_view_zoom_out_cb,
479 "zoom_fit", "clicked", map_view_zoom_fit_cb,
482 g_signal_connect (self, "key-press-event",
483 G_CALLBACK (map_view_key_press_cb), self);
485 g_object_unref (gui);
487 priv->throbber = gtk_spinner_new ();
488 gtk_widget_set_size_request (priv->throbber, 16, 16);
489 gtk_container_add (GTK_CONTAINER (throbber_holder), priv->throbber);
491 /* Set up map view */
492 embed = gtk_champlain_embed_new ();
493 priv->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
494 g_object_set (G_OBJECT (priv->map_view),
496 "kinetic-mode", TRUE,
498 champlain_view_center_on (priv->map_view, 36, 0);
500 gtk_container_add (GTK_CONTAINER (sw), embed);
501 gtk_widget_show_all (embed);
503 priv->layer = g_object_ref (champlain_marker_layer_new ());
504 champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (priv->layer));
506 g_signal_connect (priv->map_view, "notify::state",
507 G_CALLBACK (map_view_state_changed), self);
509 /* Set up contact list. */
510 priv->markers = g_hash_table_new_full (NULL, NULL,
511 (GDestroyNotify) g_object_unref, NULL);
513 priv->aggregator = empathy_connection_aggregator_dup_singleton ();
514 priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
517 tp_g_signal_connect_object (priv->aggregator, "contact-list-changed",
518 G_CALLBACK (contact_list_changed_cb), self, 0);
520 contacts = empathy_connection_aggregator_dup_all_contacts (priv->aggregator);
521 empty = g_ptr_array_new ();
523 contact_list_changed_cb (priv->aggregator, contacts, empty, self);
525 g_ptr_array_unref (contacts);
526 g_ptr_array_unref (empty);
528 /* Set up time updating loop */
529 priv->timeout_id = g_timeout_add_seconds (5,
530 (GSourceFunc) map_view_tick, self);
534 empathy_map_view_show (void)
538 window = g_object_new (EMPATHY_TYPE_MAP_VIEW, NULL);
539 gtk_widget_show_all (window);
540 empathy_window_present (GTK_WINDOW (window));