]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
auth-client: add some more debugging
[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-connection-aggregator.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 G_DEFINE_TYPE (EmpathyMapView, empathy_map_view, GTK_TYPE_WINDOW);
47
48 #define GET_PRIV(self) ((EmpathyMapViewPriv *)((EmpathyMapView *) self)->priv)
49
50 struct _EmpathyMapViewPriv {
51   EmpathyConnectionAggregator *aggregator;
52
53   GtkWidget *zoom_in;
54   GtkWidget *zoom_out;
55   GtkWidget *throbber;
56   ChamplainView *map_view;
57   ChamplainMarkerLayer *layer;
58   guint timeout_id;
59   /* reffed (EmpathyContact *) => borrowed (ChamplainMarker *) */
60   GHashTable *markers;
61   gulong members_changed_id;
62
63   /* TpContact -> EmpathyContact */
64   GHashTable *contacts;
65 };
66
67 static void
68 map_view_state_changed (ChamplainView *view,
69     GParamSpec *gobject,
70     EmpathyMapView *self)
71 {
72   ChamplainState state;
73   EmpathyMapViewPriv *priv = GET_PRIV (self);
74
75   g_object_get (G_OBJECT (view), "state", &state, NULL);
76   if (state == CHAMPLAIN_STATE_LOADING)
77     {
78       gtk_spinner_start (GTK_SPINNER (priv->throbber));
79       gtk_widget_show (priv->throbber);
80     }
81   else
82     {
83       gtk_spinner_stop (GTK_SPINNER (priv->throbber));
84       gtk_widget_hide (priv->throbber);
85     }
86 }
87
88 static gboolean
89 contact_has_location (EmpathyContact *contact)
90 {
91   GHashTable *location;
92
93   location = empathy_contact_get_location (contact);
94
95   if (location == NULL || g_hash_table_size (location) == 0)
96     return FALSE;
97
98   return TRUE;
99 }
100
101 static ClutterActor * create_marker (EmpathyMapView *window,
102     EmpathyContact *contact);
103
104 static void
105 map_view_update_contact_position (EmpathyMapView *self,
106     EmpathyContact *contact)
107 {
108   EmpathyMapViewPriv *priv = GET_PRIV (self);
109   gdouble lon, lat;
110   GValue *value;
111   GHashTable *location;
112   ClutterActor *marker;
113   gboolean has_location;
114
115   has_location = contact_has_location (contact);
116
117   marker = g_hash_table_lookup (priv->markers, contact);
118   if (marker == NULL)
119     {
120       if (!has_location)
121         return;
122
123       marker = create_marker (self, contact);
124     }
125   else if (!has_location)
126     {
127       champlain_marker_animate_out (CHAMPLAIN_MARKER (marker));
128       return;
129     }
130
131   location = empathy_contact_get_location (contact);
132
133   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
134   if (value == NULL)
135     {
136       clutter_actor_hide (marker);
137       return;
138     }
139   lat = g_value_get_double (value);
140
141   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
142   if (value == NULL)
143     {
144       clutter_actor_hide (marker);
145       return;
146     }
147   lon = g_value_get_double (value);
148
149   champlain_location_set_location (CHAMPLAIN_LOCATION (marker), lat, lon);
150   champlain_marker_animate_in (CHAMPLAIN_MARKER (marker));
151 }
152
153 static void
154 map_view_contact_location_notify (EmpathyContact *contact,
155     GParamSpec *arg1,
156     EmpathyMapView *self)
157 {
158   map_view_update_contact_position (self, contact);
159 }
160
161 static void
162 map_view_zoom_in_cb (GtkWidget *widget,
163     EmpathyMapView *self)
164 {
165   EmpathyMapViewPriv *priv = GET_PRIV (self);
166
167   champlain_view_zoom_in (priv->map_view);
168 }
169
170 static void
171 map_view_zoom_out_cb (GtkWidget *widget,
172     EmpathyMapView *self)
173 {
174   EmpathyMapViewPriv *priv = GET_PRIV (self);
175
176   champlain_view_zoom_out (priv->map_view);
177 }
178
179 static void
180 map_view_zoom_fit_cb (GtkWidget *widget,
181     EmpathyMapView *self)
182 {
183   EmpathyMapViewPriv *priv = GET_PRIV (self);
184
185   champlain_view_ensure_layers_visible (priv->map_view, TRUE);
186 }
187
188 static gboolean
189 marker_clicked_cb (ChamplainMarker *marker,
190     ClutterButtonEvent *event,
191     EmpathyMapView *self)
192 {
193   GtkWidget *menu;
194   EmpathyContact *contact;
195
196   if (event->button != 3)
197     return FALSE;
198
199   contact = g_object_get_data (G_OBJECT (marker), "contact");
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_FT |
206       EMPATHY_CONTACT_FEATURE_INFO);
207
208   if (menu == NULL)
209     return FALSE;
210
211   gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), NULL);
212
213   gtk_widget_show (menu);
214   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
215       event->button, event->time);
216
217   return FALSE;
218 }
219
220 static void
221 map_view_contacts_update_label (ClutterActor *marker)
222 {
223   const gchar *name;
224   gchar *date;
225   gchar *label;
226   GValue *gtime;
227   gint64 loctime;
228   GHashTable *location;
229   EmpathyContact *contact;
230
231   contact = g_object_get_data (G_OBJECT (marker), "contact");
232   location = empathy_contact_get_location (contact);
233   name = empathy_contact_get_alias (contact);
234   gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
235
236   if (gtime != NULL)
237     {
238       GDateTime *now, *d;
239       GTimeSpan delta;
240
241       loctime = g_value_get_int64 (gtime);
242       date = empathy_time_to_string_relative (loctime);
243       label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
244       g_free (date);
245
246       now = g_date_time_new_now_utc ();
247       d = g_date_time_new_from_unix_utc (loctime);
248       delta = g_date_time_difference (now, d);
249
250       /* if location is older than a week */
251       if (delta > G_TIME_SPAN_DAY * 7)
252         clutter_actor_set_opacity (marker, 0.75 * 255);
253
254       g_date_time_unref (now);
255       g_date_time_unref (d);
256     }
257   else
258     {
259       label = g_strconcat ("<b>", name, "</b>\n", NULL);
260     }
261
262   champlain_label_set_use_markup (CHAMPLAIN_LABEL (marker), TRUE);
263   champlain_label_set_text (CHAMPLAIN_LABEL (marker), label);
264
265   g_free (label);
266 }
267
268 static ClutterActor *
269 create_marker (EmpathyMapView *self,
270     EmpathyContact *contact)
271 {
272   EmpathyMapViewPriv *priv = GET_PRIV (self);
273   ClutterActor *marker;
274   GdkPixbuf *avatar;
275   ClutterActor *texture = NULL;
276
277   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
278   if (avatar != NULL)
279     {
280       texture = gtk_clutter_texture_new ();
281
282       gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture),
283           avatar, NULL);
284
285       g_object_unref (avatar);
286     }
287
288   marker = champlain_label_new_with_image (texture);
289
290   g_object_set_data_full (G_OBJECT (marker), "contact",
291       g_object_ref (contact), g_object_unref);
292
293   g_hash_table_insert (priv->markers, g_object_ref (contact), marker);
294
295   map_view_contacts_update_label (marker);
296
297   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
298   g_signal_connect (marker, "button-release-event",
299       G_CALLBACK (marker_clicked_cb), self);
300
301   champlain_marker_layer_add_marker (priv->layer, CHAMPLAIN_MARKER (marker));
302
303   DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
304
305   tp_clear_object (&texture);
306   return marker;
307 }
308
309 static gboolean
310 map_view_key_press_cb (GtkWidget *widget,
311     GdkEventKey *event,
312     gpointer user_data)
313 {
314   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
315       || event->keyval == GDK_KEY_Escape)
316     {
317       gtk_widget_destroy (widget);
318       return TRUE;
319     }
320
321   return FALSE;
322 }
323
324 static gboolean
325 map_view_tick (EmpathyMapView *self)
326 {
327   EmpathyMapViewPriv *priv = GET_PRIV (self);
328   GList *marker, *l;
329
330   marker = champlain_marker_layer_get_markers (priv->layer);
331
332   for (l = marker; l != NULL; l = g_list_next (l))
333     map_view_contacts_update_label (l->data);
334
335   g_list_free (marker);
336   return TRUE;
337 }
338
339 static void
340 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
341     GPtrArray *added,
342     GPtrArray *removed,
343     EmpathyMapView *self)
344 {
345   EmpathyMapViewPriv *priv = GET_PRIV (self);
346   guint i;
347
348   for (i = 0; i < added->len; i++)
349     {
350       TpContact *tp_contact = g_ptr_array_index (added, i);
351       EmpathyContact *contact;
352
353       if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
354         continue;
355
356       contact = empathy_contact_dup_from_tp_contact (tp_contact);
357
358       tp_g_signal_connect_object (contact, "notify::location",
359           G_CALLBACK (map_view_contact_location_notify), self, 0);
360
361       map_view_update_contact_position (self, contact);
362
363       /* Pass ownership to the hash table */
364       g_hash_table_insert (priv->contacts, g_object_ref (tp_contact),
365           contact);
366     }
367
368   for (i = 0; i < removed->len; i++)
369     {
370       TpContact *tp_contact = g_ptr_array_index (removed, i);
371       EmpathyContact *contact;
372       ClutterActor *marker;
373
374       contact = g_hash_table_lookup (priv->contacts, tp_contact);
375       if (contact == NULL)
376         continue;
377
378       marker = g_hash_table_lookup (priv->markers, contact);
379       if (marker != NULL)
380         {
381           clutter_actor_destroy (marker);
382           g_hash_table_remove (priv->markers, contact);
383         }
384
385       g_signal_handlers_disconnect_by_func (contact,
386           map_view_contact_location_notify, self);
387
388       g_hash_table_remove (priv->contacts, tp_contact);
389     }
390 }
391
392 static GObject *
393 empathy_map_view_constructor (GType type,
394     guint n_construct_params,
395     GObjectConstructParam *construct_params)
396 {
397   static GObject *window = NULL;
398
399   if (window != NULL)
400     return window;
401
402   window = G_OBJECT_CLASS (empathy_map_view_parent_class)->constructor (
403       type, n_construct_params, construct_params);
404
405   g_object_add_weak_pointer (window, (gpointer) &window);
406
407   return window;
408 }
409
410 static void
411 empathy_map_view_finalize (GObject *object)
412 {
413   EmpathyMapViewPriv *priv = GET_PRIV (object);
414   GHashTableIter iter;
415   gpointer contact;
416
417   g_source_remove (priv->timeout_id);
418
419   g_hash_table_iter_init (&iter, priv->markers);
420   while (g_hash_table_iter_next (&iter, &contact, NULL))
421     g_signal_handlers_disconnect_by_func (contact,
422         map_view_contact_location_notify, object);
423
424   g_hash_table_unref (priv->markers);
425   g_object_unref (priv->aggregator);
426   g_object_unref (priv->layer);
427   g_hash_table_unref (priv->contacts);
428
429   G_OBJECT_CLASS (empathy_map_view_parent_class)->finalize (object);
430 }
431
432 static void
433 empathy_map_view_class_init (EmpathyMapViewClass *klass)
434 {
435   GObjectClass *object_class = G_OBJECT_CLASS (klass);
436
437   object_class->constructor = empathy_map_view_constructor;
438   object_class->finalize = empathy_map_view_finalize;
439
440   g_type_class_add_private (object_class, sizeof (EmpathyMapViewPriv));
441 }
442
443 static void
444 empathy_map_view_init (EmpathyMapView *self)
445 {
446   EmpathyMapViewPriv *priv;
447   GtkBuilder *gui;
448   GtkWidget *sw;
449   GtkWidget *embed;
450   GtkWidget *throbber_holder;
451   gchar *filename;
452   GPtrArray *contacts, *empty;
453   GtkWidget *main_vbox;
454
455   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
456       EMPATHY_TYPE_MAP_VIEW, EmpathyMapViewPriv);
457
458   gtk_window_set_title (GTK_WINDOW (self), _("Contact Map View"));
459   gtk_window_set_role (GTK_WINDOW (self), "map_view");
460   gtk_window_set_default_size (GTK_WINDOW (self), 512, 384);
461   gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);
462
463   /* Set up interface */
464   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
465   gui = empathy_builder_get_file (filename,
466      "main_vbox", &main_vbox,
467      "zoom_in", &priv->zoom_in,
468      "zoom_out", &priv->zoom_out,
469      "map_scrolledwindow", &sw,
470      "throbber", &throbber_holder,
471      NULL);
472   g_free (filename);
473
474   gtk_container_add (GTK_CONTAINER (self), main_vbox);
475
476   empathy_builder_connect (gui, self,
477       "zoom_in", "clicked", map_view_zoom_in_cb,
478       "zoom_out", "clicked", map_view_zoom_out_cb,
479       "zoom_fit", "clicked", map_view_zoom_fit_cb,
480       NULL);
481
482   g_signal_connect (self, "key-press-event",
483       G_CALLBACK (map_view_key_press_cb), self);
484
485   g_object_unref (gui);
486
487   priv->throbber = gtk_spinner_new ();
488   gtk_widget_set_size_request (priv->throbber, 16, 16);
489   gtk_container_add (GTK_CONTAINER (throbber_holder), priv->throbber);
490
491   /* Set up map view */
492   embed = gtk_champlain_embed_new ();
493   priv->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
494   g_object_set (G_OBJECT (priv->map_view),
495      "zoom-level", 1,
496      "kinetic-mode", TRUE,
497      NULL);
498   champlain_view_center_on (priv->map_view, 36, 0);
499
500   gtk_container_add (GTK_CONTAINER (sw), embed);
501   gtk_widget_show_all (embed);
502
503   priv->layer = g_object_ref (champlain_marker_layer_new ());
504   champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (priv->layer));
505
506   g_signal_connect (priv->map_view, "notify::state",
507       G_CALLBACK (map_view_state_changed), self);
508
509   /* Set up contact list. */
510   priv->markers = g_hash_table_new_full (NULL, NULL,
511       (GDestroyNotify) g_object_unref, NULL);
512
513   priv->aggregator = empathy_connection_aggregator_dup_singleton ();
514   priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
515       g_object_unref);
516
517   tp_g_signal_connect_object (priv->aggregator, "contact-list-changed",
518       G_CALLBACK (contact_list_changed_cb), self, 0);
519
520   contacts = empathy_connection_aggregator_dup_all_contacts (priv->aggregator);
521   empty = g_ptr_array_new ();
522
523   contact_list_changed_cb (priv->aggregator, contacts, empty, self);
524
525   g_ptr_array_unref (contacts);
526   g_ptr_array_unref (empty);
527
528   /* Set up time updating loop */
529   priv->timeout_id = g_timeout_add_seconds (5,
530       (GSourceFunc) map_view_tick, self);
531 }
532
533 GtkWidget *
534 empathy_map_view_show (void)
535 {
536   GtkWidget *window;
537
538   window = g_object_new (EMPATHY_TYPE_MAP_VIEW, NULL);
539   gtk_widget_show_all (window);
540   empathy_window_present (GTK_WINDOW (window));
541
542   return window;
543 }