]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
Updated Galician translations for docs
[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   DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
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 }