]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
Merge remote-tracking branch 'glassrose/add-All-service-selection-in-debug-window'
[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-individual-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   TpContact *tp_contact;
196   FolksIndividual *individual;
197
198   if (event->button != 3)
199     return FALSE;
200
201   contact = g_object_get_data (G_OBJECT (marker), "contact");
202   if (contact == NULL)
203     return FALSE;
204
205   tp_contact = empathy_contact_get_tp_contact (contact);
206   if (tp_contact == NULL)
207     return FALSE;
208
209   individual = empathy_create_individual_from_tp_contact (tp_contact);
210   if (individual == NULL)
211     return FALSE;
212
213   menu = empathy_individual_menu_new (individual,
214       EMPATHY_INDIVIDUAL_FEATURE_CHAT |
215       EMPATHY_INDIVIDUAL_FEATURE_CALL |
216       EMPATHY_INDIVIDUAL_FEATURE_LOG |
217       EMPATHY_INDIVIDUAL_FEATURE_INFO, NULL);
218
219   if (menu == NULL)
220     goto out;
221
222   gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (self), NULL);
223
224   gtk_widget_show (menu);
225   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
226       event->button, event->time);
227
228 out:
229   g_object_unref (individual);
230   return FALSE;
231 }
232
233 static void
234 map_view_contacts_update_label (ClutterActor *marker)
235 {
236   const gchar *name;
237   gchar *date;
238   gchar *label;
239   GValue *gtime;
240   gint64 loctime;
241   GHashTable *location;
242   EmpathyContact *contact;
243
244   contact = g_object_get_data (G_OBJECT (marker), "contact");
245   location = empathy_contact_get_location (contact);
246   name = empathy_contact_get_alias (contact);
247   gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
248
249   if (gtime != NULL)
250     {
251       GDateTime *now, *d;
252       GTimeSpan delta;
253
254       loctime = g_value_get_int64 (gtime);
255       date = empathy_time_to_string_relative (loctime);
256       label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
257       g_free (date);
258
259       now = g_date_time_new_now_utc ();
260       d = g_date_time_new_from_unix_utc (loctime);
261       delta = g_date_time_difference (now, d);
262
263       /* if location is older than a week */
264       if (delta > G_TIME_SPAN_DAY * 7)
265         clutter_actor_set_opacity (marker, 0.75 * 255);
266
267       g_date_time_unref (now);
268       g_date_time_unref (d);
269     }
270   else
271     {
272       label = g_strconcat ("<b>", name, "</b>\n", NULL);
273     }
274
275   champlain_label_set_use_markup (CHAMPLAIN_LABEL (marker), TRUE);
276   champlain_label_set_text (CHAMPLAIN_LABEL (marker), label);
277
278   g_free (label);
279 }
280
281 static ClutterActor *
282 create_marker (EmpathyMapView *self,
283     EmpathyContact *contact)
284 {
285   EmpathyMapViewPriv *priv = GET_PRIV (self);
286   ClutterActor *marker;
287   GdkPixbuf *avatar;
288   ClutterActor *texture = NULL;
289
290   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
291   if (avatar != NULL)
292     {
293       texture = gtk_clutter_texture_new ();
294
295       gtk_clutter_texture_set_from_pixbuf (GTK_CLUTTER_TEXTURE (texture),
296           avatar, NULL);
297
298       g_object_unref (avatar);
299     }
300
301   marker = champlain_label_new_with_image (texture);
302
303   g_object_set_data_full (G_OBJECT (marker), "contact",
304       g_object_ref (contact), g_object_unref);
305
306   g_hash_table_insert (priv->markers, g_object_ref (contact), marker);
307
308   map_view_contacts_update_label (marker);
309
310   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
311   g_signal_connect (marker, "button-release-event",
312       G_CALLBACK (marker_clicked_cb), self);
313
314   champlain_marker_layer_add_marker (priv->layer, CHAMPLAIN_MARKER (marker));
315
316   DEBUG ("Create marker for %s", empathy_contact_get_id (contact));
317
318   tp_clear_object (&texture);
319   return marker;
320 }
321
322 static gboolean
323 map_view_key_press_cb (GtkWidget *widget,
324     GdkEventKey *event,
325     gpointer user_data)
326 {
327   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_KEY_w)
328       || event->keyval == GDK_KEY_Escape)
329     {
330       gtk_widget_destroy (widget);
331       return TRUE;
332     }
333
334   return FALSE;
335 }
336
337 static gboolean
338 map_view_tick (EmpathyMapView *self)
339 {
340   EmpathyMapViewPriv *priv = GET_PRIV (self);
341   GList *marker, *l;
342
343   marker = champlain_marker_layer_get_markers (priv->layer);
344
345   for (l = marker; l != NULL; l = g_list_next (l))
346     map_view_contacts_update_label (l->data);
347
348   g_list_free (marker);
349   return TRUE;
350 }
351
352 static void
353 contact_list_changed_cb (EmpathyConnectionAggregator *aggregator,
354     GPtrArray *added,
355     GPtrArray *removed,
356     EmpathyMapView *self)
357 {
358   EmpathyMapViewPriv *priv = GET_PRIV (self);
359   guint i;
360
361   for (i = 0; i < added->len; i++)
362     {
363       TpContact *tp_contact = g_ptr_array_index (added, i);
364       EmpathyContact *contact;
365
366       if (g_hash_table_lookup (priv->contacts, tp_contact) != NULL)
367         continue;
368
369       contact = empathy_contact_dup_from_tp_contact (tp_contact);
370
371       tp_g_signal_connect_object (contact, "notify::location",
372           G_CALLBACK (map_view_contact_location_notify), self, 0);
373
374       map_view_update_contact_position (self, contact);
375
376       /* Pass ownership to the hash table */
377       g_hash_table_insert (priv->contacts, g_object_ref (tp_contact),
378           contact);
379     }
380
381   for (i = 0; i < removed->len; i++)
382     {
383       TpContact *tp_contact = g_ptr_array_index (removed, i);
384       EmpathyContact *contact;
385       ClutterActor *marker;
386
387       contact = g_hash_table_lookup (priv->contacts, tp_contact);
388       if (contact == NULL)
389         continue;
390
391       marker = g_hash_table_lookup (priv->markers, contact);
392       if (marker != NULL)
393         {
394           clutter_actor_destroy (marker);
395           g_hash_table_remove (priv->markers, contact);
396         }
397
398       g_signal_handlers_disconnect_by_func (contact,
399           map_view_contact_location_notify, self);
400
401       g_hash_table_remove (priv->contacts, tp_contact);
402     }
403 }
404
405 static GObject *
406 empathy_map_view_constructor (GType type,
407     guint n_construct_params,
408     GObjectConstructParam *construct_params)
409 {
410   static GObject *window = NULL;
411
412   if (window != NULL)
413     return window;
414
415   window = G_OBJECT_CLASS (empathy_map_view_parent_class)->constructor (
416       type, n_construct_params, construct_params);
417
418   g_object_add_weak_pointer (window, (gpointer) &window);
419
420   return window;
421 }
422
423 static void
424 empathy_map_view_finalize (GObject *object)
425 {
426   EmpathyMapViewPriv *priv = GET_PRIV (object);
427   GHashTableIter iter;
428   gpointer contact;
429
430   g_source_remove (priv->timeout_id);
431
432   g_hash_table_iter_init (&iter, priv->markers);
433   while (g_hash_table_iter_next (&iter, &contact, NULL))
434     g_signal_handlers_disconnect_by_func (contact,
435         map_view_contact_location_notify, object);
436
437   g_hash_table_unref (priv->markers);
438   g_object_unref (priv->aggregator);
439   g_object_unref (priv->layer);
440   g_hash_table_unref (priv->contacts);
441
442   G_OBJECT_CLASS (empathy_map_view_parent_class)->finalize (object);
443 }
444
445 static void
446 empathy_map_view_class_init (EmpathyMapViewClass *klass)
447 {
448   GObjectClass *object_class = G_OBJECT_CLASS (klass);
449
450   object_class->constructor = empathy_map_view_constructor;
451   object_class->finalize = empathy_map_view_finalize;
452
453   g_type_class_add_private (object_class, sizeof (EmpathyMapViewPriv));
454 }
455
456 static void
457 empathy_map_view_init (EmpathyMapView *self)
458 {
459   EmpathyMapViewPriv *priv;
460   GtkBuilder *gui;
461   GtkWidget *sw;
462   GtkWidget *embed;
463   GtkWidget *throbber_holder;
464   gchar *filename;
465   GPtrArray *contacts, *empty;
466   GtkWidget *main_vbox;
467
468   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
469       EMPATHY_TYPE_MAP_VIEW, EmpathyMapViewPriv);
470
471   gtk_window_set_title (GTK_WINDOW (self), _("Contact Map View"));
472   gtk_window_set_role (GTK_WINDOW (self), "map_view");
473   gtk_window_set_default_size (GTK_WINDOW (self), 512, 384);
474   gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);
475
476   /* Set up interface */
477   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
478   gui = empathy_builder_get_file (filename,
479      "main_vbox", &main_vbox,
480      "zoom_in", &priv->zoom_in,
481      "zoom_out", &priv->zoom_out,
482      "map_scrolledwindow", &sw,
483      "throbber", &throbber_holder,
484      NULL);
485   g_free (filename);
486
487   gtk_container_add (GTK_CONTAINER (self), main_vbox);
488
489   empathy_builder_connect (gui, self,
490       "zoom_in", "clicked", map_view_zoom_in_cb,
491       "zoom_out", "clicked", map_view_zoom_out_cb,
492       "zoom_fit", "clicked", map_view_zoom_fit_cb,
493       NULL);
494
495   g_signal_connect (self, "key-press-event",
496       G_CALLBACK (map_view_key_press_cb), self);
497
498   g_object_unref (gui);
499
500   priv->throbber = gtk_spinner_new ();
501   gtk_widget_set_size_request (priv->throbber, 16, 16);
502   gtk_container_add (GTK_CONTAINER (throbber_holder), priv->throbber);
503
504   /* Set up map view */
505   embed = gtk_champlain_embed_new ();
506   priv->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
507   g_object_set (G_OBJECT (priv->map_view),
508      "zoom-level", 1,
509      "kinetic-mode", TRUE,
510      NULL);
511   champlain_view_center_on (priv->map_view, 36, 0);
512
513   gtk_container_add (GTK_CONTAINER (sw), embed);
514   gtk_widget_show_all (embed);
515
516   priv->layer = g_object_ref (champlain_marker_layer_new ());
517   champlain_view_add_layer (priv->map_view, CHAMPLAIN_LAYER (priv->layer));
518
519   g_signal_connect (priv->map_view, "notify::state",
520       G_CALLBACK (map_view_state_changed), self);
521
522   /* Set up contact list. */
523   priv->markers = g_hash_table_new_full (NULL, NULL,
524       (GDestroyNotify) g_object_unref, NULL);
525
526   priv->aggregator = empathy_connection_aggregator_dup_singleton ();
527   priv->contacts = g_hash_table_new_full (NULL, NULL, g_object_unref,
528       g_object_unref);
529
530   tp_g_signal_connect_object (priv->aggregator, "contact-list-changed",
531       G_CALLBACK (contact_list_changed_cb), self, 0);
532
533   contacts = empathy_connection_aggregator_dup_all_contacts (priv->aggregator);
534   empty = g_ptr_array_new ();
535
536   contact_list_changed_cb (priv->aggregator, contacts, empty, self);
537
538   g_ptr_array_unref (contacts);
539   g_ptr_array_unref (empty);
540
541   /* Set up time updating loop */
542   priv->timeout_id = g_timeout_add_seconds (5,
543       (GSourceFunc) map_view_tick, self);
544 }
545
546 GtkWidget *
547 empathy_map_view_show (void)
548 {
549   GtkWidget *window;
550
551   window = g_object_new (EMPATHY_TYPE_MAP_VIEW, NULL);
552   gtk_widget_show_all (window);
553   empathy_window_present (GTK_WINDOW (window));
554
555   return window;
556 }