]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
566e1f6c22ca95a4ea3b79a6640cab3ce78ca01e
[empathy.git] / src / empathy-map-view.c
1 /*
2  * Copyright (C) 2008, 2009 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
19  */
20
21 #include <config.h>
22
23 #include <sys/stat.h>
24 #include <gtk/gtk.h>
25 #include <glib/gi18n.h>
26 #include <gdk/gdkkeysyms.h>
27
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>
32
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>
37
38 #include <libempathy-gtk/empathy-contact-menu.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
40
41 #include "empathy-map-view.h"
42
43 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
44 #include <libempathy/empathy-debug.h>
45
46 typedef struct {
47   EmpathyContactList *contact_list;
48
49   GtkWidget *window;
50   GtkWidget *zoom_in;
51   GtkWidget *zoom_out;
52   GtkWidget *throbber;
53   ChamplainView *map_view;
54   ChamplainLayer *layer;
55   guint timeout_id;
56   /* reffed (EmpathyContact *) => borrowed (ChamplainMarker *) */
57   GHashTable *markers;
58   gulong members_changed_id;
59 } EmpathyMapView;
60
61 static void
62 map_view_state_changed (ChamplainView *view,
63     GParamSpec *gobject,
64     EmpathyMapView *window)
65 {
66   ChamplainState state;
67
68   g_object_get (G_OBJECT (view), "state", &state, NULL);
69   if (state == CHAMPLAIN_STATE_LOADING)
70     {
71       gtk_spinner_start (GTK_SPINNER (window->throbber));
72       gtk_widget_show (window->throbber);
73     }
74   else
75     {
76       gtk_spinner_stop (GTK_SPINNER (window->throbber));
77       gtk_widget_hide (window->throbber);
78     }
79 }
80
81 static gboolean
82 contact_has_location (EmpathyContact *contact)
83 {
84   GHashTable *location;
85
86   location = empathy_contact_get_location (contact);
87
88   if (location == NULL || g_hash_table_size (location) == 0)
89     return FALSE;
90
91   return TRUE;
92 }
93
94 static ChamplainMarker * create_marker (EmpathyMapView *window,
95     EmpathyContact *contact);
96
97 static void
98 map_view_update_contact_position (EmpathyMapView *self,
99     EmpathyContact *contact)
100 {
101   gdouble lon, lat;
102   GValue *value;
103   GHashTable *location;
104   ChamplainMarker *marker;
105   gboolean has_location;
106
107   has_location = contact_has_location (contact);
108
109   marker = g_hash_table_lookup (self->markers, contact);
110   if (marker == NULL)
111     {
112       if (!has_location)
113         return;
114
115       marker = create_marker (self, contact);
116     }
117   else if (!has_location)
118     {
119       champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
120       return;
121     }
122
123   location = empathy_contact_get_location (contact);
124
125   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
126   if (value == NULL)
127     {
128       clutter_actor_hide (CLUTTER_ACTOR (marker));
129       return;
130     }
131   lat = g_value_get_double (value);
132
133   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
134   if (value == NULL)
135     {
136       clutter_actor_hide (CLUTTER_ACTOR (marker));
137       return;
138     }
139   lon = g_value_get_double (value);
140
141   champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
142   champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
143 }
144
145 static void
146 map_view_contact_location_notify (EmpathyContact *contact,
147     GParamSpec *arg1,
148     EmpathyMapView *self)
149 {
150   map_view_update_contact_position (self, contact);
151 }
152
153 static void
154 map_view_zoom_in_cb (GtkWidget *widget,
155     EmpathyMapView *window)
156 {
157   champlain_view_zoom_in (window->map_view);
158 }
159
160 static void
161 map_view_zoom_out_cb (GtkWidget *widget,
162     EmpathyMapView *window)
163 {
164   champlain_view_zoom_out (window->map_view);
165 }
166
167 static void
168 map_view_zoom_fit_cb (GtkWidget *widget,
169     EmpathyMapView *window)
170 {
171   GList *item, *children;
172   GPtrArray *markers;
173
174   children = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
175   markers =  g_ptr_array_sized_new (g_list_length (children) + 1);
176
177   for (item = children; item != NULL; item = g_list_next (item))
178     g_ptr_array_add (markers, (gpointer) item->data);
179
180   g_ptr_array_add (markers, (gpointer) NULL);
181   champlain_view_ensure_markers_visible (window->map_view,
182     (ChamplainBaseMarker **) markers->pdata,
183     TRUE);
184
185   g_ptr_array_free (markers, TRUE);
186   g_list_free (children);
187 }
188
189 static gboolean
190 marker_clicked_cb (ChamplainMarker *marker,
191     ClutterButtonEvent *event,
192     EmpathyContact *contact)
193 {
194   GtkWidget *menu;
195
196   if (event->button != 3)
197     return FALSE;
198
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);
204
205   if (menu == NULL)
206     return FALSE;
207
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);
213
214   return FALSE;
215 }
216
217 static void
218 map_view_contacts_update_label (ChamplainMarker *marker)
219 {
220   const gchar *name;
221   gchar *date;
222   gchar *label;
223   GValue *gtime;
224   time_t loctime;
225   GHashTable *location;
226   EmpathyContact *contact;
227
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);
232
233   if (gtime != NULL)
234     {
235       time_t now;
236
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);
240       g_free (date);
241
242       now = time (NULL);
243
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);
247     }
248   else
249     {
250       label = g_strconcat ("<b>", name, "</b>\n", NULL);
251     }
252
253   champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
254   champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
255
256   g_free (label);
257 }
258
259 static ChamplainMarker *
260 create_marker (EmpathyMapView *window,
261     EmpathyContact *contact)
262 {
263   ClutterActor *marker;
264   ClutterActor *texture;
265   GdkPixbuf *avatar;
266
267   marker = champlain_marker_new ();
268
269   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
270   if (avatar != NULL)
271     {
272       texture = clutter_texture_new ();
273       gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
274           NULL);
275       champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
276       g_object_unref (avatar);
277     }
278   else
279     champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
280
281   g_object_set_data_full (G_OBJECT (marker), "contact",
282       g_object_ref (contact), g_object_unref);
283
284   g_hash_table_insert (window->markers, g_object_ref (contact), marker);
285
286   map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
287
288   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
289   g_signal_connect (marker, "button-release-event",
290       G_CALLBACK (marker_clicked_cb), contact);
291
292   clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
293
294   return CHAMPLAIN_MARKER (marker);
295 }
296
297 static void
298 contact_added (EmpathyMapView *window,
299     EmpathyContact *contact)
300 {
301   g_signal_connect (contact, "notify::location",
302       G_CALLBACK (map_view_contact_location_notify), window);
303
304   map_view_update_contact_position (window, contact);
305 }
306
307 static void
308 map_view_destroy_cb (GtkWidget *widget,
309     EmpathyMapView *window)
310 {
311   GHashTableIter iter;
312   gpointer contact;
313
314   g_source_remove (window->timeout_id);
315
316   g_hash_table_iter_init (&iter, window->markers);
317   while (g_hash_table_iter_next (&iter, &contact, NULL))
318     g_signal_handlers_disconnect_by_func (contact,
319         map_view_contact_location_notify, window);
320
321   g_signal_handler_disconnect (window->contact_list,
322       window->members_changed_id);
323
324   g_hash_table_destroy (window->markers);
325   g_object_unref (window->contact_list);
326   g_object_unref (window->layer);
327   g_slice_free (EmpathyMapView, window);
328 }
329
330 static gboolean
331 map_view_key_press_cb (GtkWidget *widget,
332     GdkEventKey *event,
333     gpointer user_data)
334 {
335   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
336       || event->keyval == GDK_Escape)
337     {
338       gtk_widget_destroy (widget);
339       return TRUE;
340     }
341
342   return FALSE;
343 }
344
345 static gboolean
346 map_view_tick (EmpathyMapView *window)
347 {
348   GList *marker;
349
350   marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
351
352   for (; marker; marker = marker->next)
353     map_view_contacts_update_label (marker->data);
354
355   return TRUE;
356 }
357
358 static void
359 contact_removed (EmpathyMapView *self,
360     EmpathyContact *contact)
361 {
362   ClutterActor *marker;
363
364   marker = g_hash_table_lookup (self->markers, contact);
365   if (marker == NULL)
366     return;
367
368   clutter_actor_destroy (marker);
369   g_hash_table_remove (self->markers, contact);
370 }
371
372 static void
373 members_changed_cb (EmpathyContactList *list,
374     EmpathyContact *contact,
375     EmpathyContact *actor,
376     guint reason,
377     gchar *message,
378     gboolean is_member,
379     EmpathyMapView *self)
380 {
381   if (is_member)
382     {
383       contact_added (self, contact);
384     }
385   else
386     {
387       contact_removed (self, contact);
388     }
389 }
390
391 GtkWidget *
392 empathy_map_view_show (void)
393 {
394   static EmpathyMapView *window = NULL;
395   GtkBuilder *gui;
396   GtkWidget *sw;
397   GtkWidget *embed;
398   GtkWidget *throbber_holder;
399   gchar *filename;
400   GList *members, *l;
401
402   if (window)
403     {
404       empathy_window_present (GTK_WINDOW (window->window));
405       return window->window;
406     }
407
408   window = g_slice_new0 (EmpathyMapView);
409
410   /* Set up interface */
411   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
412   gui = empathy_builder_get_file (filename,
413      "map_view", &window->window,
414      "zoom_in", &window->zoom_in,
415      "zoom_out", &window->zoom_out,
416      "map_scrolledwindow", &sw,
417      "throbber", &throbber_holder,
418      NULL);
419   g_free (filename);
420
421   empathy_builder_connect (gui, window,
422       "map_view", "destroy", map_view_destroy_cb,
423       "map_view", "key-press-event", map_view_key_press_cb,
424       "zoom_in", "clicked", map_view_zoom_in_cb,
425       "zoom_out", "clicked", map_view_zoom_out_cb,
426       "zoom_fit", "clicked", map_view_zoom_fit_cb,
427       NULL);
428
429   g_object_unref (gui);
430
431   /* Clear the static pointer to window if the dialog is destroyed */
432   g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
433
434   window->contact_list = EMPATHY_CONTACT_LIST (
435       empathy_contact_manager_dup_singleton ());
436
437   window->members_changed_id = g_signal_connect (window->contact_list,
438       "members-changed", G_CALLBACK (members_changed_cb), window);
439
440   window->throbber = gtk_spinner_new ();
441   gtk_widget_set_size_request (window->throbber, 16, 16);
442   gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
443
444   /* Set up map view */
445   embed = gtk_champlain_embed_new ();
446   window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
447   g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
448      "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
449   champlain_view_center_on (window->map_view, 36, 0);
450
451   gtk_container_add (GTK_CONTAINER (sw), embed);
452   gtk_widget_show_all (embed);
453
454   window->layer = g_object_ref (champlain_layer_new ());
455   champlain_view_add_layer (window->map_view, window->layer);
456
457   g_signal_connect (window->map_view, "notify::state",
458       G_CALLBACK (map_view_state_changed), window);
459
460   /* Set up contact list. */
461   window->markers = g_hash_table_new_full (NULL, NULL,
462       (GDestroyNotify) g_object_unref, NULL);
463
464   members = empathy_contact_list_get_members (
465       window->contact_list);
466   for (l = members; l != NULL; l = g_list_next (l))
467     {
468       contact_added (window, l->data);
469       g_object_unref (l->data);
470     }
471   g_list_free (members);
472
473   empathy_window_present (GTK_WINDOW (window->window));
474
475   /* Set up time updating loop */
476   window->timeout_id = g_timeout_add_seconds (5,
477       (GSourceFunc) map_view_tick, window);
478
479   return window->window;
480 }