Don't repeat a tooltip format string
[empathy-mirror.git] / src / empathy-map-view.c
blobb8a32885cf498a2e91d14a6ef5f7b6cd73f7e023
1 /*
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>
21 #include <config.h>
23 #include <sys/stat.h>
24 #include <gtk/gtk.h>
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>
47 typedef struct {
48 EmpathyContactListStore *list_store;
50 GtkWidget *window;
51 GtkWidget *zoom_in;
52 GtkWidget *zoom_out;
53 ChamplainView *map_view;
54 ChamplainLayer *layer;
55 } EmpathyMapView;
57 static void map_view_destroy_cb (GtkWidget *widget,
58 EmpathyMapView *window);
59 static gboolean map_view_contacts_foreach (GtkTreeModel *model,
60 GtkTreePath *path,
61 GtkTreeIter *iter,
62 gpointer user_data);
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,
68 GParamSpec *arg1,
69 gpointer user_data);
71 GtkWidget *
72 empathy_map_view_show (void)
74 static EmpathyMapView *window = NULL;
75 GtkBuilder *gui;
76 GtkWidget *sw;
77 GtkWidget *embed;
78 gchar *filename;
79 GtkTreeModel *model;
80 EmpathyContactList *list_iface;
81 EmpathyContactListStore *list_store;
83 if (window)
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,
98 NULL);
99 g_free (filename);
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,
105 NULL);
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),
128 GTK_WIDGET (embed));
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;
142 static void
143 map_view_destroy_cb (GtkWidget *widget,
144 EmpathyMapView *window)
146 GList *item;
148 item = clutter_container_get_children (window->layer);
149 while (item != NULL)
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);
166 static void
167 map_view_marker_update_position (ChamplainMarker *marker,
168 EmpathyContact *contact)
170 gdouble lon, lat;
171 GValue *value;
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));
180 return;
183 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
184 if (value == NULL)
186 clutter_actor_hide (CLUTTER_ACTOR (marker));
187 return;
189 lat = g_value_get_double (value);
191 value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
192 if (value == NULL)
194 clutter_actor_hide (CLUTTER_ACTOR (marker));
195 return;
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);
203 static void
204 map_view_contact_location_notify (GObject *gobject,
205 GParamSpec *arg1,
206 gpointer user_data)
208 ChamplainMarker *marker = CHAMPLAIN_MARKER (user_data);
209 EmpathyContact *contact = EMPATHY_CONTACT (gobject);
210 map_view_marker_update_position (marker, contact);
213 static gboolean
214 map_view_contacts_foreach (GtkTreeModel *model,
215 GtkTreePath *path,
216 GtkTreeIter *iter,
217 gpointer user_data)
219 EmpathyMapView *window = (EmpathyMapView*) user_data;
220 EmpathyContact *contact;
221 ClutterActor *marker;
222 ClutterActor *texture;
223 GHashTable *location;
224 GdkPixbuf *avatar;
225 const gchar *name;
226 gchar *date;
227 gchar *label;
228 GValue *gtime;
229 time_t time;
231 gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
232 &contact, -1);
234 if (contact == NULL)
235 return FALSE;
237 location = empathy_contact_get_location (contact);
239 if (location == NULL)
240 return FALSE;
242 marker = champlain_marker_new ();
244 avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
245 if (avatar != NULL)
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);
252 else
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);
257 if (gtime != NULL)
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);
262 g_free (date);
264 else
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);
270 g_free (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);
281 return FALSE;
284 static void
285 map_view_zoom_in_cb (GtkWidget *widget,
286 EmpathyMapView *window)
288 champlain_view_zoom_in (window->map_view);
291 static void
292 map_view_zoom_out_cb (GtkWidget *widget,
293 EmpathyMapView *window)
295 champlain_view_zoom_out (window->map_view);