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"
44 #include "ephy-spinner.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
47 #include <libempathy/empathy-debug.h>
50 EmpathyContactListStore *list_store;
56 ChamplainView *map_view;
57 ChamplainLayer *layer;
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)
70 ephy_spinner_start (EPHY_SPINNER (window->throbber));
72 ephy_spinner_stop (EPHY_SPINNER (window->throbber));
76 map_view_marker_update_position (ChamplainMarker *marker,
77 EmpathyContact *contact)
83 location = empathy_contact_get_location (contact);
85 if (location == NULL ||
86 g_hash_table_size (location) == 0)
88 champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
92 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
95 clutter_actor_hide (CLUTTER_ACTOR (marker));
98 lat = g_value_get_double (value);
100 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
103 clutter_actor_hide (CLUTTER_ACTOR (marker));
106 lon = g_value_get_double (value);
108 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
109 champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
113 map_view_contact_location_notify (EmpathyContact *contact,
115 ChamplainMarker *marker)
117 map_view_marker_update_position (marker, contact);
121 map_view_zoom_in_cb (GtkWidget *widget,
122 EmpathyMapView *window)
124 champlain_view_zoom_in (window->map_view);
128 map_view_zoom_out_cb (GtkWidget *widget,
129 EmpathyMapView *window)
131 champlain_view_zoom_out (window->map_view);
135 map_view_zoom_fit_cb (GtkWidget *widget,
136 EmpathyMapView *window)
138 GList *item, *children;
141 children = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
142 markers = g_ptr_array_sized_new (g_list_length (children) + 1);
144 for (item = children; item != NULL; item = g_list_next (item))
145 g_ptr_array_add (markers, (gpointer) item->data);
147 g_ptr_array_add (markers, (gpointer) NULL);
148 champlain_view_ensure_markers_visible (window->map_view,
149 (ChamplainBaseMarker **) markers->pdata,
152 g_ptr_array_free (markers, TRUE);
153 g_list_free (children);
157 marker_clicked_cb (ChamplainMarker *marker,
158 ClutterButtonEvent *event,
159 EmpathyContact *contact)
163 if (event->button != 3)
166 menu = empathy_contact_menu_new (contact,
167 EMPATHY_CONTACT_FEATURE_CHAT |
168 EMPATHY_CONTACT_FEATURE_CALL |
169 EMPATHY_CONTACT_FEATURE_LOG |
170 EMPATHY_CONTACT_FEATURE_INFO);
175 gtk_widget_show (menu);
176 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
177 event->button, event->time);
178 g_object_ref_sink (menu);
179 g_object_unref (menu);
185 map_view_contacts_update_label (ChamplainMarker *marker)
192 GHashTable *location;
193 EmpathyContact *contact;
195 contact = g_object_get_data (G_OBJECT (marker), "contact");
196 location = empathy_contact_get_location (contact);
197 name = empathy_contact_get_name (contact);
198 gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
204 loctime = g_value_get_int64 (gtime);
205 date = empathy_time_to_string_relative (loctime);
206 label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
211 /* if location is older than a week */
212 if (now - loctime > (60 * 60 * 24 * 7))
213 clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
217 label = g_strconcat ("<b>", name, "</b>\n", NULL);
220 champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
221 champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
227 map_view_contacts_foreach (GtkTreeModel *model,
232 EmpathyMapView *window = (EmpathyMapView *) user_data;
233 EmpathyContact *contact;
234 ClutterActor *marker;
235 ClutterActor *texture;
236 GHashTable *location;
239 gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
245 location = empathy_contact_get_location (contact);
247 if (location == NULL || g_hash_table_size (location) == 0)
250 marker = champlain_marker_new ();
252 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
255 texture = clutter_texture_new ();
256 gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
258 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
259 g_object_unref (avatar);
262 champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
264 g_object_set_data_full (G_OBJECT (marker), "contact",
265 g_object_ref (contact), g_object_unref);
267 map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
269 clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
270 g_signal_connect (marker, "button-release-event",
271 G_CALLBACK (marker_clicked_cb), contact);
273 clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
275 g_signal_connect (contact, "notify::location",
276 G_CALLBACK (map_view_contact_location_notify), marker);
278 map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);
280 g_object_unref (contact);
285 map_view_destroy_cb (GtkWidget *widget,
286 EmpathyMapView *window)
290 g_source_remove (window->timeout_id);
292 item = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
295 EmpathyContact *contact;
296 ChamplainMarker *marker;
298 marker = CHAMPLAIN_MARKER (item->data);
299 contact = g_object_get_data (G_OBJECT (marker), "contact");
300 g_signal_handlers_disconnect_by_func (contact,
301 map_view_contact_location_notify, marker);
303 item = g_list_next (item);
306 g_object_unref (window->list_store);
307 g_object_unref (window->layer);
308 g_slice_free (EmpathyMapView, window);
312 map_view_key_press_cb (GtkWidget *widget,
316 if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
317 || event->keyval == GDK_Escape)
319 gtk_widget_destroy (widget);
327 map_view_tick (EmpathyMapView *window)
331 marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
333 for (; marker; marker = marker->next)
334 map_view_contacts_update_label (marker->data);
340 empathy_map_view_show (void)
342 static EmpathyMapView *window = NULL;
346 GtkWidget *throbber_holder;
349 EmpathyContactList *list_iface;
350 EmpathyContactListStore *list_store;
354 empathy_window_present (GTK_WINDOW (window->window));
355 return window->window;
358 window = g_slice_new0 (EmpathyMapView);
360 /* Set up interface */
361 filename = empathy_file_lookup ("empathy-map-view.ui", "src");
362 gui = empathy_builder_get_file (filename,
363 "map_view", &window->window,
364 "zoom_in", &window->zoom_in,
365 "zoom_out", &window->zoom_out,
366 "map_scrolledwindow", &sw,
367 "throbber", &throbber_holder,
371 empathy_builder_connect (gui, window,
372 "map_view", "destroy", map_view_destroy_cb,
373 "map_view", "key-press-event", map_view_key_press_cb,
374 "zoom_in", "clicked", map_view_zoom_in_cb,
375 "zoom_out", "clicked", map_view_zoom_out_cb,
376 "zoom_fit", "clicked", map_view_zoom_fit_cb,
379 g_object_unref (gui);
381 /* Clear the static pointer to window if the dialog is destroyed */
382 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
384 list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
385 list_store = empathy_contact_list_store_new (list_iface);
386 empathy_contact_list_store_set_show_groups (list_store, FALSE);
387 empathy_contact_list_store_set_show_avatars (list_store, TRUE);
388 g_object_unref (list_iface);
390 window->throbber = ephy_spinner_new ();
391 ephy_spinner_set_size (EPHY_SPINNER (window->throbber),
392 GTK_ICON_SIZE_LARGE_TOOLBAR);
393 gtk_widget_show (window->throbber);
394 gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
396 window->list_store = list_store;
398 /* Set up map view */
399 embed = gtk_champlain_embed_new ();
400 window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
401 g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
402 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
403 champlain_view_center_on (window->map_view, 36, 0);
405 gtk_container_add (GTK_CONTAINER (sw), embed);
406 gtk_widget_show_all (embed);
408 window->layer = g_object_ref (champlain_layer_new ());
409 champlain_view_add_layer (window->map_view, window->layer);
411 g_signal_connect (window->map_view, "notify::state",
412 G_CALLBACK (map_view_state_changed), window);
414 /* Set up contact list. */
415 model = GTK_TREE_MODEL (window->list_store);
416 gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
418 empathy_window_present (GTK_WINDOW (window->window));
420 /* Set up time updating loop */
421 window->timeout_id = g_timeout_add_seconds (5,
422 (GSourceFunc) map_view_tick, window);
424 return window->window;