]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
8df13922ad3531a5074eb082fb1cd792239772ae
[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
27 #include <champlain/champlain.h>
28 #include <champlain-gtk/champlain-gtk.h>
29 #include <clutter-gtk/clutter-gtk.h>
30 #include <telepathy-glib/util.h>
31
32 #include <libempathy/empathy-contact.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-utils.h>
35 #include <libempathy/empathy-location.h>
36
37 #include <libempathy-gtk/empathy-contact-list-store.h>
38 #include <libempathy-gtk/empathy-contact-list-view.h>
39 #include <libempathy-gtk/empathy-presence-chooser.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
41
42 #include "empathy-map-view.h"
43 #include "ephy-spinner.h"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
46 #include <libempathy/empathy-debug.h>
47
48 typedef struct {
49   EmpathyContactListStore *list_store;
50
51   GtkWidget *window;
52   GtkWidget *zoom_in;
53   GtkWidget *zoom_out;
54   GtkWidget *throbber;
55   ChamplainView *map_view;
56   ChamplainLayer *layer;
57 } EmpathyMapView;
58
59 static void
60 map_view_state_changed (ChamplainView *view,
61     GParamSpec *gobject,
62     EmpathyMapView *window)
63 {
64   ChamplainState state;
65
66   g_object_get (G_OBJECT (view), "state", &state, NULL);
67   if (state == CHAMPLAIN_STATE_LOADING)
68     ephy_spinner_start (EPHY_SPINNER (window->throbber));
69   else
70     ephy_spinner_stop (EPHY_SPINNER (window->throbber));
71 }
72
73 static void
74 map_view_marker_update_position (ChamplainMarker *marker,
75     EmpathyContact *contact)
76 {
77   gdouble lon, lat;
78   GValue *value;
79   GHashTable *location;
80
81   location = empathy_contact_get_location (contact);
82
83   if (location == NULL ||
84       g_hash_table_size (location) == 0)
85   {
86     champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
87     return;
88   }
89
90   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
91   if (value == NULL)
92     {
93       clutter_actor_hide (CLUTTER_ACTOR (marker));
94       return;
95     }
96   lat = g_value_get_double (value);
97
98   value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
99   if (value == NULL)
100     {
101       clutter_actor_hide (CLUTTER_ACTOR (marker));
102       return;
103     }
104   lon = g_value_get_double (value);
105
106   champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
107   champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
108 }
109
110 static void
111 map_view_contact_location_notify (EmpathyContact *contact,
112     GParamSpec *arg1,
113     ChamplainMarker *marker)
114 {
115   map_view_marker_update_position (marker, contact);
116 }
117
118 static void
119 map_view_zoom_in_cb (GtkWidget *widget,
120     EmpathyMapView *window)
121 {
122   champlain_view_zoom_in (window->map_view);
123 }
124
125 static void
126 map_view_zoom_out_cb (GtkWidget *widget,
127     EmpathyMapView *window)
128 {
129   champlain_view_zoom_out (window->map_view);
130 }
131
132 static gboolean
133 marker_clicked_cb (ChamplainMarker *marker,
134     ClutterButtonEvent *event,
135     EmpathyContact *contact)
136 {
137   GtkWidget *menu;
138
139   if (event->button != 3)
140     return FALSE;
141
142   menu = empathy_contact_menu_new (contact,
143       EMPATHY_CONTACT_FEATURE_CHAT |
144       EMPATHY_CONTACT_FEATURE_CALL |
145       EMPATHY_CONTACT_FEATURE_LOG |
146       EMPATHY_CONTACT_FEATURE_INFO);
147
148   if (menu == NULL)
149     return FALSE;
150
151   gtk_widget_show (menu);
152   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
153       event->button, event->time);
154
155   return FALSE;
156 }
157
158 static gboolean
159 map_view_contacts_foreach (GtkTreeModel *model,
160     GtkTreePath *path,
161     GtkTreeIter *iter,
162     gpointer user_data)
163 {
164   EmpathyMapView *window = (EmpathyMapView *) user_data;
165   EmpathyContact *contact;
166   ClutterActor *marker;
167   ClutterActor *texture;
168   GHashTable *location;
169   GdkPixbuf *avatar;
170   const gchar *name;
171   gchar *date;
172   gchar *label;
173   GValue *gtime;
174   time_t loctime;
175
176   gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
177      &contact, -1);
178
179   if (contact == NULL)
180     return FALSE;
181
182   location = empathy_contact_get_location (contact);
183
184   if (location == NULL || g_hash_table_size (location) == 0)
185     return FALSE;
186
187   marker = champlain_marker_new ();
188
189   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
190   if (avatar != NULL)
191     {
192       texture = clutter_texture_new ();
193       gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
194           NULL);
195       champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
196       g_object_unref (avatar);
197     }
198   else
199     champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
200
201   name = empathy_contact_get_name (contact);
202   gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
203   if (gtime != NULL)
204     {
205       time_t now;
206
207       loctime = g_value_get_int64 (gtime);
208       date = empathy_time_to_string_relative (loctime);
209       label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
210       g_free (date);
211
212       now = time (NULL);
213
214       /* if location is older than a week */
215       if (now - loctime > (60 * 60 * 24 * 7))
216         clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
217     }
218   else
219     {
220       label = g_strconcat ("<b>", name, "</b>\n", NULL);
221     }
222   champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
223   champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
224   g_free (label);
225
226   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
227   g_signal_connect (marker, "button-release-event",
228       G_CALLBACK (marker_clicked_cb), contact);
229
230   clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
231
232   g_signal_connect (contact, "notify::location",
233       G_CALLBACK (map_view_contact_location_notify), marker);
234   g_object_set_data_full (G_OBJECT (marker), "contact",
235       g_object_ref (contact), g_object_unref);
236
237   map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);
238
239   g_object_unref (contact);
240   return FALSE;
241 }
242
243 static void
244 map_view_destroy_cb (GtkWidget *widget,
245     EmpathyMapView *window)
246 {
247   GList *item;
248
249   item = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
250   while (item != NULL)
251   {
252     EmpathyContact *contact;
253     ChamplainMarker *marker;
254
255     marker = CHAMPLAIN_MARKER (item->data);
256     contact = g_object_get_data (G_OBJECT (marker), "contact");
257     g_signal_handlers_disconnect_by_func (contact,
258         map_view_contact_location_notify, marker);
259
260     item = g_list_next (item);
261   }
262
263   g_object_unref (window->list_store);
264   g_object_unref (window->layer);
265   g_slice_free (EmpathyMapView, window);
266 }
267
268 GtkWidget *
269 empathy_map_view_show (void)
270 {
271   static EmpathyMapView *window = NULL;
272   GtkBuilder *gui;
273   GtkWidget *sw;
274   GtkWidget *embed;
275   GtkWidget *throbber_holder;
276   gchar *filename;
277   GtkTreeModel *model;
278   EmpathyContactList *list_iface;
279   EmpathyContactListStore *list_store;
280
281   if (window)
282     {
283       empathy_window_present (GTK_WINDOW (window->window), TRUE);
284       return window->window;
285     }
286
287   window = g_slice_new0 (EmpathyMapView);
288
289   /* Set up interface */
290   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
291   gui = empathy_builder_get_file (filename,
292      "map_view", &window->window,
293      "zoom_in", &window->zoom_in,
294      "zoom_out", &window->zoom_out,
295      "map_scrolledwindow", &sw,
296      "throbber", &throbber_holder,
297      NULL);
298   g_free (filename);
299
300   empathy_builder_connect (gui, window,
301       "map_view", "destroy", map_view_destroy_cb,
302       "zoom_in", "clicked", map_view_zoom_in_cb,
303       "zoom_out", "clicked", map_view_zoom_out_cb,
304       NULL);
305
306   g_object_unref (gui);
307
308   /* Clear the static pointer to window if the dialog is destroyed */
309   g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
310
311   list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
312   list_store = empathy_contact_list_store_new (list_iface);
313   empathy_contact_list_store_set_show_groups (list_store, FALSE);
314   empathy_contact_list_store_set_show_avatars (list_store, TRUE);
315   g_object_unref (list_iface);
316
317   window->throbber = ephy_spinner_new ();
318   ephy_spinner_set_size (EPHY_SPINNER (window->throbber),
319       GTK_ICON_SIZE_LARGE_TOOLBAR);
320   gtk_widget_show (window->throbber);
321   gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
322
323   window->list_store = list_store;
324
325   /* Set up map view */
326   embed = gtk_champlain_embed_new ();
327   window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
328   g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
329      "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
330   champlain_view_center_on (window->map_view, 36, 0);
331
332   gtk_container_add (GTK_CONTAINER (sw), embed);
333   gtk_widget_show_all (embed);
334
335   window->layer = g_object_ref (champlain_layer_new ());
336   champlain_view_add_layer (window->map_view, window->layer);
337
338   g_signal_connect (window->map_view, "notify::state",
339       G_CALLBACK (map_view_state_changed), window);
340
341   /* Set up contact list. */
342   model = GTK_TREE_MODEL (window->list_store);
343   gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
344
345   empathy_window_present (GTK_WINDOW (window->window), TRUE);
346   return window->window;
347 }
348