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