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-contact-manager.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>
47 EmpathyContactList *contact_list;
53 ChamplainView *map_view;
54 ChamplainLayer *layer;
56 /* reffed (EmpathyContact *) => borrowed (ChamplainMarker *) */
58 gulong members_changed_id;
62 map_view_state_changed (ChamplainView *view,
64 EmpathyMapView *window)
68 g_object_get (G_OBJECT (view), "state", &state, NULL);
69 if (state == CHAMPLAIN_STATE_LOADING)
71 gtk_spinner_start (GTK_SPINNER (window->throbber));
72 gtk_widget_show (window->throbber);
76 gtk_spinner_stop (GTK_SPINNER (window->throbber));
77 gtk_widget_hide (window->throbber);
82 contact_has_location (EmpathyContact *contact)
86 location = empathy_contact_get_location (contact);
88 if (location == NULL || g_hash_table_size (location) == 0)
94 static ChamplainMarker * create_marker (EmpathyMapView *window,
95 EmpathyContact *contact);
98 map_view_update_contact_position (EmpathyMapView *self,
99 EmpathyContact *contact)
103 GHashTable *location;
104 ChamplainMarker *marker;
105 gboolean has_location;
107 has_location = contact_has_location (contact);
109 marker = g_hash_table_lookup (self->markers, contact);
115 marker = create_marker (self, contact);
117 else if (!has_location)
119 champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
123 location = empathy_contact_get_location (contact);
125 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
128 clutter_actor_hide (CLUTTER_ACTOR (marker));
131 lat = g_value_get_double (value);
133 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
136 clutter_actor_hide (CLUTTER_ACTOR (marker));
139 lon = g_value_get_double (value);
141 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
142 champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
146 map_view_contact_location_notify (EmpathyContact *contact,
148 EmpathyMapView *self)
150 map_view_update_contact_position (self, contact);
154 map_view_zoom_in_cb (GtkWidget *widget,
155 EmpathyMapView *window)
157 champlain_view_zoom_in (window->map_view);
161 map_view_zoom_out_cb (GtkWidget *widget,
162 EmpathyMapView *window)
164 champlain_view_zoom_out (window->map_view);
168 map_view_zoom_fit_cb (GtkWidget *widget,
169 EmpathyMapView *window)
171 GList *item, *children;
174 children = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
175 markers = g_ptr_array_sized_new (g_list_length (children) + 1);
177 for (item = children; item != NULL; item = g_list_next (item))
178 g_ptr_array_add (markers, (gpointer) item->data);
180 g_ptr_array_add (markers, (gpointer) NULL);
181 champlain_view_ensure_markers_visible (window->map_view,
182 (ChamplainBaseMarker **) markers->pdata,
185 g_ptr_array_free (markers, TRUE);
186 g_list_free (children);
190 marker_clicked_cb (ChamplainMarker *marker,
191 ClutterButtonEvent *event,
192 EmpathyContact *contact)
196 if (event->button != 3)
199 menu = empathy_contact_menu_new (contact,
200 EMPATHY_CONTACT_FEATURE_CHAT |
201 EMPATHY_CONTACT_FEATURE_CALL |
202 EMPATHY_CONTACT_FEATURE_LOG |
203 EMPATHY_CONTACT_FEATURE_INFO);
208 gtk_widget_show (menu);
209 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
210 event->button, event->time);
211 g_object_ref_sink (menu);
212 g_object_unref (menu);
218 map_view_contacts_update_label (ChamplainMarker *marker)
225 GHashTable *location;
226 EmpathyContact *contact;
228 contact = g_object_get_data (G_OBJECT (marker), "contact");
229 location = empathy_contact_get_location (contact);
230 name = empathy_contact_get_name (contact);
231 gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
237 loctime = g_value_get_int64 (gtime);
238 date = empathy_time_to_string_relative (loctime);
239 label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
244 /* if location is older than a week */
245 if (now - loctime > (60 * 60 * 24 * 7))
246 clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
250 label = g_strconcat ("<b>", name, "</b>\n", NULL);
253 champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
254 champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
259 static ChamplainMarker *
260 create_marker (EmpathyMapView *window,
261 EmpathyContact *contact)
263 ClutterActor *marker;
264 ClutterActor *texture;
267 marker = champlain_marker_new ();
269 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
272 texture = clutter_texture_new ();
273 gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
275 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
276 g_object_unref (avatar);
279 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
281 g_object_set_data_full (G_OBJECT (marker), "contact",
282 g_object_ref (contact), g_object_unref);
284 g_hash_table_insert (window->markers, g_object_ref (contact), marker);
286 map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
288 clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
289 g_signal_connect (marker, "button-release-event",
290 G_CALLBACK (marker_clicked_cb), contact);
292 clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
294 DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
296 return CHAMPLAIN_MARKER (marker);
300 contact_added (EmpathyMapView *window,
301 EmpathyContact *contact)
303 g_signal_connect (contact, "notify::location",
304 G_CALLBACK (map_view_contact_location_notify), window);
306 map_view_update_contact_position (window, contact);
310 map_view_destroy_cb (GtkWidget *widget,
311 EmpathyMapView *window)
316 g_source_remove (window->timeout_id);
318 g_hash_table_iter_init (&iter, window->markers);
319 while (g_hash_table_iter_next (&iter, &contact, NULL))
320 g_signal_handlers_disconnect_by_func (contact,
321 map_view_contact_location_notify, window);
323 g_signal_handler_disconnect (window->contact_list,
324 window->members_changed_id);
326 g_hash_table_destroy (window->markers);
327 g_object_unref (window->contact_list);
328 g_object_unref (window->layer);
329 g_slice_free (EmpathyMapView, window);
333 map_view_key_press_cb (GtkWidget *widget,
337 if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
338 || event->keyval == GDK_Escape)
340 gtk_widget_destroy (widget);
348 map_view_tick (EmpathyMapView *window)
352 marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
354 for (; marker; marker = marker->next)
355 map_view_contacts_update_label (marker->data);
361 contact_removed (EmpathyMapView *self,
362 EmpathyContact *contact)
364 ClutterActor *marker;
366 marker = g_hash_table_lookup (self->markers, contact);
370 clutter_actor_destroy (marker);
371 g_hash_table_remove (self->markers, contact);
375 members_changed_cb (EmpathyContactList *list,
376 EmpathyContact *contact,
377 EmpathyContact *actor,
381 EmpathyMapView *self)
385 contact_added (self, contact);
389 contact_removed (self, contact);
394 empathy_map_view_show (void)
396 static EmpathyMapView *window = NULL;
400 GtkWidget *throbber_holder;
406 empathy_window_present (GTK_WINDOW (window->window));
407 return window->window;
410 window = g_slice_new0 (EmpathyMapView);
412 /* Set up interface */
413 filename = empathy_file_lookup ("empathy-map-view.ui", "src");
414 gui = empathy_builder_get_file (filename,
415 "map_view", &window->window,
416 "zoom_in", &window->zoom_in,
417 "zoom_out", &window->zoom_out,
418 "map_scrolledwindow", &sw,
419 "throbber", &throbber_holder,
423 empathy_builder_connect (gui, window,
424 "map_view", "destroy", map_view_destroy_cb,
425 "map_view", "key-press-event", map_view_key_press_cb,
426 "zoom_in", "clicked", map_view_zoom_in_cb,
427 "zoom_out", "clicked", map_view_zoom_out_cb,
428 "zoom_fit", "clicked", map_view_zoom_fit_cb,
431 g_object_unref (gui);
433 /* Clear the static pointer to window if the dialog is destroyed */
434 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
436 window->contact_list = EMPATHY_CONTACT_LIST (
437 empathy_contact_manager_dup_singleton ());
439 window->members_changed_id = g_signal_connect (window->contact_list,
440 "members-changed", G_CALLBACK (members_changed_cb), window);
442 window->throbber = gtk_spinner_new ();
443 gtk_widget_set_size_request (window->throbber, 16, 16);
444 gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
446 /* Set up map view */
447 embed = gtk_champlain_embed_new ();
448 window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
449 g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
450 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
451 champlain_view_center_on (window->map_view, 36, 0);
453 gtk_container_add (GTK_CONTAINER (sw), embed);
454 gtk_widget_show_all (embed);
456 window->layer = g_object_ref (champlain_layer_new ());
457 champlain_view_add_layer (window->map_view, window->layer);
459 g_signal_connect (window->map_view, "notify::state",
460 G_CALLBACK (map_view_state_changed), window);
462 /* Set up contact list. */
463 window->markers = g_hash_table_new_full (NULL, NULL,
464 (GDestroyNotify) g_object_unref, NULL);
466 members = empathy_contact_list_get_members (
467 window->contact_list);
468 for (l = members; l != NULL; l = g_list_next (l))
470 contact_added (window, l->data);
471 g_object_unref (l->data);
473 g_list_free (members);
475 empathy_window_present (GTK_WINDOW (window->window));
477 /* Set up time updating loop */
478 window->timeout_id = g_timeout_add_seconds (5,
479 (GSourceFunc) map_view_tick, window);
481 return window->window;