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