]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
Move empathy-log to tests/interactive and don't install it anymore
[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 #include "ephy-spinner.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
47 #include <libempathy/empathy-debug.h>
48
49 typedef struct {
50   EmpathyContactListStore *list_store;
51
52   GtkWidget *window;
53   GtkWidget *zoom_in;
54   GtkWidget *zoom_out;
55   GtkWidget *throbber;
56   ChamplainView *map_view;
57   ChamplainLayer *layer;
58   guint timeout_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     ephy_spinner_start (EPHY_SPINNER (window->throbber));
71   else
72     ephy_spinner_stop (EPHY_SPINNER (window->throbber));
73 }
74
75 static void
76 map_view_marker_update_position (ChamplainMarker *marker,
77     EmpathyContact *contact)
78 {
79   gdouble lon, lat;
80   GValue *value;
81   GHashTable *location;
82
83   location = empathy_contact_get_location (contact);
84
85   if (location == NULL ||
86       g_hash_table_size (location) == 0)
87   {
88     champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
89     return;
90   }
91
92   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
93   if (value == NULL)
94     {
95       clutter_actor_hide (CLUTTER_ACTOR (marker));
96       return;
97     }
98   lat = g_value_get_double (value);
99
100   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
101   if (value == NULL)
102     {
103       clutter_actor_hide (CLUTTER_ACTOR (marker));
104       return;
105     }
106   lon = g_value_get_double (value);
107
108   champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
109   champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
110 }
111
112 static void
113 map_view_contact_location_notify (EmpathyContact *contact,
114     GParamSpec *arg1,
115     ChamplainMarker *marker)
116 {
117   map_view_marker_update_position (marker, contact);
118 }
119
120 static void
121 map_view_zoom_in_cb (GtkWidget *widget,
122     EmpathyMapView *window)
123 {
124   champlain_view_zoom_in (window->map_view);
125 }
126
127 static void
128 map_view_zoom_out_cb (GtkWidget *widget,
129     EmpathyMapView *window)
130 {
131   champlain_view_zoom_out (window->map_view);
132 }
133
134 static void
135 map_view_zoom_fit_cb (GtkWidget *widget,
136     EmpathyMapView *window)
137 {
138   GList *item, *children;
139   GPtrArray *markers;
140
141   children = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
142   markers =  g_ptr_array_sized_new (g_list_length (children) + 1);
143
144   for (item = children; item != NULL; item = g_list_next (item))
145     g_ptr_array_add (markers, (gpointer) item->data);
146
147   g_ptr_array_add (markers, (gpointer) NULL);
148   champlain_view_ensure_markers_visible (window->map_view,
149     (ChamplainBaseMarker **) markers->pdata,
150     TRUE);
151
152   g_ptr_array_free (markers, TRUE);
153   g_list_free (children);
154 }
155
156 static gboolean
157 marker_clicked_cb (ChamplainMarker *marker,
158     ClutterButtonEvent *event,
159     EmpathyContact *contact)
160 {
161   GtkWidget *menu;
162
163   if (event->button != 3)
164     return FALSE;
165
166   menu = empathy_contact_menu_new (contact,
167       EMPATHY_CONTACT_FEATURE_CHAT |
168       EMPATHY_CONTACT_FEATURE_CALL |
169       EMPATHY_CONTACT_FEATURE_LOG |
170       EMPATHY_CONTACT_FEATURE_INFO);
171
172   if (menu == NULL)
173     return FALSE;
174
175   gtk_widget_show (menu);
176   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
177       event->button, event->time);
178
179   return FALSE;
180 }
181
182 static void
183 map_view_contacts_update_label (ChamplainMarker *marker)
184 {
185   const gchar *name;
186   gchar *date;
187   gchar *label;
188   GValue *gtime;
189   time_t loctime;
190   GHashTable *location;
191   EmpathyContact *contact;
192
193   contact = g_object_get_data (G_OBJECT (marker), "contact");
194   location = empathy_contact_get_location (contact);
195   name = empathy_contact_get_name (contact);
196   gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
197
198   if (gtime != NULL)
199     {
200       time_t now;
201
202       loctime = g_value_get_int64 (gtime);
203       date = empathy_time_to_string_relative (loctime);
204       label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
205       g_free (date);
206
207       now = time (NULL);
208
209       /* if location is older than a week */
210       if (now - loctime > (60 * 60 * 24 * 7))
211         clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
212     }
213   else
214     {
215       label = g_strconcat ("<b>", name, "</b>\n", NULL);
216     }
217
218   champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
219   champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
220
221   g_free (label);
222 }
223
224 static gboolean
225 map_view_contacts_foreach (GtkTreeModel *model,
226     GtkTreePath *path,
227     GtkTreeIter *iter,
228     gpointer user_data)
229 {
230   EmpathyMapView *window = (EmpathyMapView *) user_data;
231   EmpathyContact *contact;
232   ClutterActor *marker;
233   ClutterActor *texture;
234   GHashTable *location;
235   GdkPixbuf *avatar;
236
237   gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
238      &contact, -1);
239
240   if (contact == NULL)
241     return FALSE;
242
243   location = empathy_contact_get_location (contact);
244
245   if (location == NULL || g_hash_table_size (location) == 0)
246     return FALSE;
247
248   marker = champlain_marker_new ();
249
250   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
251   if (avatar != NULL)
252     {
253       texture = clutter_texture_new ();
254       gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
255           NULL);
256       champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
257       g_object_unref (avatar);
258     }
259   else
260     champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
261
262   g_object_set_data_full (G_OBJECT (marker), "contact",
263       g_object_ref (contact), g_object_unref);
264
265   map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
266
267   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
268   g_signal_connect (marker, "button-release-event",
269       G_CALLBACK (marker_clicked_cb), contact);
270
271   clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
272
273   g_signal_connect (contact, "notify::location",
274       G_CALLBACK (map_view_contact_location_notify), marker);
275
276   map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);
277
278   g_object_unref (contact);
279   return FALSE;
280 }
281
282 static void
283 map_view_destroy_cb (GtkWidget *widget,
284     EmpathyMapView *window)
285 {
286   GList *item;
287
288   g_source_remove (window->timeout_id);
289
290   item = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
291   while (item != NULL)
292   {
293     EmpathyContact *contact;
294     ChamplainMarker *marker;
295
296     marker = CHAMPLAIN_MARKER (item->data);
297     contact = g_object_get_data (G_OBJECT (marker), "contact");
298     g_signal_handlers_disconnect_by_func (contact,
299         map_view_contact_location_notify, marker);
300
301     item = g_list_next (item);
302   }
303
304   g_object_unref (window->list_store);
305   g_object_unref (window->layer);
306   g_slice_free (EmpathyMapView, window);
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_w)
315       || event->keyval == GDK_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 *window)
326 {
327   GList *marker;
328
329   marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
330
331   for (; marker; marker = marker->next)
332     map_view_contacts_update_label (marker->data);
333
334   return TRUE;
335 }
336
337 GtkWidget *
338 empathy_map_view_show (void)
339 {
340   static EmpathyMapView *window = NULL;
341   GtkBuilder *gui;
342   GtkWidget *sw;
343   GtkWidget *embed;
344   GtkWidget *throbber_holder;
345   gchar *filename;
346   GtkTreeModel *model;
347   EmpathyContactList *list_iface;
348   EmpathyContactListStore *list_store;
349
350   if (window)
351     {
352       empathy_window_present (GTK_WINDOW (window->window), TRUE);
353       return window->window;
354     }
355
356   window = g_slice_new0 (EmpathyMapView);
357
358   /* Set up interface */
359   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
360   gui = empathy_builder_get_file (filename,
361      "map_view", &window->window,
362      "zoom_in", &window->zoom_in,
363      "zoom_out", &window->zoom_out,
364      "map_scrolledwindow", &sw,
365      "throbber", &throbber_holder,
366      NULL);
367   g_free (filename);
368
369   empathy_builder_connect (gui, window,
370       "map_view", "destroy", map_view_destroy_cb,
371       "map_view", "key-press-event", map_view_key_press_cb,
372       "zoom_in", "clicked", map_view_zoom_in_cb,
373       "zoom_out", "clicked", map_view_zoom_out_cb,
374       "zoom_fit", "clicked", map_view_zoom_fit_cb,
375       NULL);
376
377   g_object_unref (gui);
378
379   /* Clear the static pointer to window if the dialog is destroyed */
380   g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
381
382   list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
383   list_store = empathy_contact_list_store_new (list_iface);
384   empathy_contact_list_store_set_show_groups (list_store, FALSE);
385   empathy_contact_list_store_set_show_avatars (list_store, TRUE);
386   g_object_unref (list_iface);
387
388   window->throbber = ephy_spinner_new ();
389   ephy_spinner_set_size (EPHY_SPINNER (window->throbber),
390       GTK_ICON_SIZE_LARGE_TOOLBAR);
391   gtk_widget_show (window->throbber);
392   gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
393
394   window->list_store = list_store;
395
396   /* Set up map view */
397   embed = gtk_champlain_embed_new ();
398   window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
399   g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
400      "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
401   champlain_view_center_on (window->map_view, 36, 0);
402
403   gtk_container_add (GTK_CONTAINER (sw), embed);
404   gtk_widget_show_all (embed);
405
406   window->layer = g_object_ref (champlain_layer_new ());
407   champlain_view_add_layer (window->map_view, window->layer);
408
409   g_signal_connect (window->map_view, "notify::state",
410       G_CALLBACK (map_view_state_changed), window);
411
412   /* Set up contact list. */
413   model = GTK_TREE_MODEL (window->list_store);
414   gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
415
416   empathy_window_present (GTK_WINDOW (window->window), TRUE);
417
418   /* Set up time updating loop */
419   window->timeout_id = g_timeout_add_seconds (5,
420       (GSourceFunc) map_view_tick, window);
421
422   return window->window;
423 }
424