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