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>
27 #include <champlain/champlain.h>
28 #include <champlain-gtk/champlain-gtk.h>
29 #include <clutter-gtk/gtk-clutter-embed.h>
30 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-location.h>
37 #include <libempathy-gtk/empathy-contact-list-store.h>
38 #include <libempathy-gtk/empathy-contact-list-view.h>
39 #include <libempathy-gtk/empathy-presence-chooser.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include "empathy-map-view.h"
44 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
45 #include <libempathy/empathy-debug.h>
48 EmpathyContactListStore
*list_store
;
53 ChamplainView
*map_view
;
54 ChamplainLayer
*layer
;
57 static void map_view_destroy_cb (GtkWidget
*widget
,
58 EmpathyMapView
*window
);
59 static gboolean
map_view_contacts_foreach (GtkTreeModel
*model
,
63 static void map_view_zoom_in_cb (GtkWidget
*widget
,
64 EmpathyMapView
*window
);
65 static void map_view_zoom_out_cb (GtkWidget
*widget
,
66 EmpathyMapView
*window
);
67 static void map_view_contact_location_notify (GObject
*gobject
,
72 empathy_map_view_show (void)
74 static EmpathyMapView
*window
= NULL
;
80 EmpathyContactList
*list_iface
;
81 EmpathyContactListStore
*list_store
;
85 empathy_window_present (GTK_WINDOW (window
->window
), TRUE
);
86 return window
->window
;
89 window
= g_slice_new0 (EmpathyMapView
);
91 /* Set up interface */
92 filename
= empathy_file_lookup ("empathy-map-view.ui", "src");
93 gui
= empathy_builder_get_file (filename
,
94 "map_view", &window
->window
,
95 "zoom_in", &window
->zoom_in
,
96 "zoom_out", &window
->zoom_out
,
97 "map_scrolledwindow", &sw
,
101 empathy_builder_connect (gui
, window
,
102 "map_view", "destroy", map_view_destroy_cb
,
103 "zoom_in", "clicked", map_view_zoom_in_cb
,
104 "zoom_out", "clicked", map_view_zoom_out_cb
,
107 g_object_unref (gui
);
109 /* Clear the static pointer to window if the dialog is destroyed */
110 g_object_add_weak_pointer (G_OBJECT (window
->window
), (gpointer
*) &window
);
112 list_iface
= EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
113 list_store
= empathy_contact_list_store_new (list_iface
);
114 empathy_contact_list_store_set_show_groups (list_store
, FALSE
);
115 empathy_contact_list_store_set_show_avatars (list_store
, TRUE
);
116 g_object_unref (list_iface
);
118 window
->list_store
= list_store
;
120 /* Set up map view */
121 window
->map_view
= CHAMPLAIN_VIEW (champlain_view_new ());
122 g_object_set (G_OBJECT (window
->map_view
), "zoom-level", 1,
123 "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC
, NULL
);
124 champlain_view_center_on (window
->map_view
, 36, 0);
126 embed
= champlain_view_embed_new (window
->map_view
);
127 gtk_container_add (GTK_CONTAINER (sw
),
129 gtk_widget_show_all (embed
);
131 window
->layer
= g_object_ref (champlain_layer_new ());
132 champlain_view_add_layer (window
->map_view
, window
->layer
);
134 /* Set up contact list. */
135 model
= GTK_TREE_MODEL (window
->list_store
);
136 gtk_tree_model_foreach (model
, map_view_contacts_foreach
, window
);
138 empathy_window_present (GTK_WINDOW (window
->window
), TRUE
);
139 return window
->window
;
143 map_view_destroy_cb (GtkWidget
*widget
,
144 EmpathyMapView
*window
)
148 item
= clutter_container_get_children (window
->layer
);
151 EmpathyContact
*contact
;
152 ChamplainMarker
*marker
;
154 marker
= CHAMPLAIN_MARKER (item
->data
);
155 contact
= g_object_get_data (G_OBJECT (marker
), "contact");
156 g_signal_handlers_disconnect_by_func (contact
, map_view_contact_location_notify
, marker
);
158 item
= g_list_next (item
);
161 g_object_unref (window
->list_store
);
162 g_object_unref (window
->layer
);
163 g_slice_free (EmpathyMapView
, window
);
167 map_view_marker_update_position (ChamplainMarker
*marker
,
168 EmpathyContact
*contact
)
172 GHashTable
*location
;
174 location
= empathy_contact_get_location (contact
);
176 if (location
== NULL
||
177 g_hash_table_size (location
) == 0)
179 clutter_actor_hide (CLUTTER_ACTOR (marker
));
183 value
= g_hash_table_lookup (location
, EMPATHY_LOCATION_LAT
);
186 clutter_actor_hide (CLUTTER_ACTOR (marker
));
189 lat
= g_value_get_double (value
);
191 value
= g_hash_table_lookup (location
, EMPATHY_LOCATION_LON
);
194 clutter_actor_hide (CLUTTER_ACTOR (marker
));
197 lon
= g_value_get_double (value
);
199 clutter_actor_show (CLUTTER_ACTOR (marker
));
200 champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker
), lat
, lon
);
204 map_view_contact_location_notify (GObject
*gobject
,
208 ChamplainMarker
*marker
= CHAMPLAIN_MARKER (user_data
);
209 EmpathyContact
*contact
= EMPATHY_CONTACT (gobject
);
210 map_view_marker_update_position (marker
, contact
);
214 map_view_contacts_foreach (GtkTreeModel
*model
,
219 EmpathyMapView
*window
= (EmpathyMapView
*) user_data
;
220 EmpathyContact
*contact
;
221 ClutterActor
*marker
;
222 ClutterActor
*texture
;
223 GHashTable
*location
;
231 gtk_tree_model_get (model
, iter
, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT
,
237 location
= empathy_contact_get_location (contact
);
239 if (location
== NULL
)
242 marker
= champlain_marker_new ();
244 avatar
= empathy_pixbuf_avatar_from_contact_scaled (contact
, 32, 32);
247 texture
= clutter_texture_new ();
248 gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture
), avatar
);
249 champlain_marker_set_image (CHAMPLAIN_MARKER (marker
), texture
);
250 g_object_unref (avatar
);
253 champlain_marker_set_image (CHAMPLAIN_MARKER (marker
), NULL
);
255 name
= empathy_contact_get_name (contact
);
256 gtime
= g_hash_table_lookup (location
, EMPATHY_LOCATION_TIMESTAMP
);
259 time
= g_value_get_int64 (gtime
);
260 date
= empathy_time_to_string_relative (time
);
261 label
= g_strconcat ("<b>", name
, "</b>\n<small>", date
, "</small>", NULL
);
266 label
= g_strconcat ("<b>", name
, "</b>\n", NULL
);
268 champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker
), TRUE
);
269 champlain_marker_set_text (CHAMPLAIN_MARKER (marker
), label
);
272 clutter_container_add (CLUTTER_CONTAINER (window
->layer
), marker
, NULL
);
274 g_signal_connect (contact
, "notify::location",
275 G_CALLBACK (map_view_contact_location_notify
), marker
);
276 g_object_set_data_full (G_OBJECT (marker
), "contact", g_object_ref (contact
), g_object_unref
);
278 map_view_marker_update_position (CHAMPLAIN_MARKER (marker
), contact
);
280 g_object_unref (contact
);
285 map_view_zoom_in_cb (GtkWidget
*widget
,
286 EmpathyMapView
*window
)
288 champlain_view_zoom_in (window
->map_view
);
292 map_view_zoom_out_cb (GtkWidget
*widget
,
293 EmpathyMapView
*window
)
295 champlain_view_zoom_out (window
->map_view
);