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-list-store.h>
39 #include <libempathy-gtk/empathy-contact-list-view.h>
40 #include <libempathy-gtk/empathy-presence-chooser.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include "empathy-map-view.h"
45 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
46 #include <libempathy/empathy-debug.h>
49 EmpathyContactList *contact_list;
55 ChamplainView *map_view;
56 ChamplainLayer *layer;
58 /* reffed (EmpathyContact *) => borrowed (ChamplainMarker *) */
60 gulong members_changed_id;
64 map_view_state_changed (ChamplainView *view,
66 EmpathyMapView *window)
70 g_object_get (G_OBJECT (view), "state", &state, NULL);
71 if (state == CHAMPLAIN_STATE_LOADING)
73 gtk_spinner_start (GTK_SPINNER (window->throbber));
74 gtk_widget_show (window->throbber);
78 gtk_spinner_stop (GTK_SPINNER (window->throbber));
79 gtk_widget_hide (window->throbber);
84 contact_has_location (EmpathyContact *contact)
88 location = empathy_contact_get_location (contact);
90 if (location == NULL || g_hash_table_size (location) == 0)
96 static ChamplainMarker * create_marker (EmpathyMapView *window,
97 EmpathyContact *contact);
100 map_view_update_contact_position (EmpathyMapView *self,
101 EmpathyContact *contact)
105 GHashTable *location;
106 ChamplainMarker *marker;
107 gboolean has_location;
109 has_location = contact_has_location (contact);
111 marker = g_hash_table_lookup (self->markers, contact);
117 marker = create_marker (self, contact);
119 else if (!has_location)
121 champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
125 location = empathy_contact_get_location (contact);
127 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
130 clutter_actor_hide (CLUTTER_ACTOR (marker));
133 lat = g_value_get_double (value);
135 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
138 clutter_actor_hide (CLUTTER_ACTOR (marker));
141 lon = g_value_get_double (value);
143 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
144 champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
148 map_view_contact_location_notify (EmpathyContact *contact,
150 EmpathyMapView *self)
152 map_view_update_contact_position (self, contact);
156 map_view_zoom_in_cb (GtkWidget *widget,
157 EmpathyMapView *window)
159 champlain_view_zoom_in (window->map_view);
163 map_view_zoom_out_cb (GtkWidget *widget,
164 EmpathyMapView *window)
166 champlain_view_zoom_out (window->map_view);
170 map_view_zoom_fit_cb (GtkWidget *widget,
171 EmpathyMapView *window)
173 GList *item, *children;
176 children = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
177 markers = g_ptr_array_sized_new (g_list_length (children) + 1);
179 for (item = children; item != NULL; item = g_list_next (item))
180 g_ptr_array_add (markers, (gpointer) item->data);
182 g_ptr_array_add (markers, (gpointer) NULL);
183 champlain_view_ensure_markers_visible (window->map_view,
184 (ChamplainBaseMarker **) markers->pdata,
187 g_ptr_array_free (markers, TRUE);
188 g_list_free (children);
192 marker_clicked_cb (ChamplainMarker *marker,
193 ClutterButtonEvent *event,
194 EmpathyContact *contact)
198 if (event->button != 3)
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_INFO);
210 gtk_widget_show (menu);
211 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
212 event->button, event->time);
213 g_object_ref_sink (menu);
214 g_object_unref (menu);
220 map_view_contacts_update_label (ChamplainMarker *marker)
227 GHashTable *location;
228 EmpathyContact *contact;
230 contact = g_object_get_data (G_OBJECT (marker), "contact");
231 location = empathy_contact_get_location (contact);
232 name = empathy_contact_get_name (contact);
233 gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
239 loctime = g_value_get_int64 (gtime);
240 date = empathy_time_to_string_relative (loctime);
241 label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
246 /* if location is older than a week */
247 if (now - loctime > (60 * 60 * 24 * 7))
248 clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
252 label = g_strconcat ("<b>", name, "</b>\n", NULL);
255 champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
256 champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
261 static ChamplainMarker *
262 create_marker (EmpathyMapView *window,
263 EmpathyContact *contact)
265 ClutterActor *marker;
266 ClutterActor *texture;
269 marker = champlain_marker_new ();
271 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
274 texture = clutter_texture_new ();
275 gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
277 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
278 g_object_unref (avatar);
281 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
283 g_object_set_data_full (G_OBJECT (marker), "contact",
284 g_object_ref (contact), g_object_unref);
286 g_hash_table_insert (window->markers, g_object_ref (contact), marker);
288 map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
290 clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
291 g_signal_connect (marker, "button-release-event",
292 G_CALLBACK (marker_clicked_cb), contact);
294 clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
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;