]> git.0d.be Git - empathy.git/blob - src/empathy-map-view.c
Updated Polish 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-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   g_object_ref_sink (menu);
179   g_object_unref (menu);
180
181   return FALSE;
182 }
183
184 static void
185 map_view_contacts_update_label (ChamplainMarker *marker)
186 {
187   const gchar *name;
188   gchar *date;
189   gchar *label;
190   GValue *gtime;
191   time_t loctime;
192   GHashTable *location;
193   EmpathyContact *contact;
194
195   contact = g_object_get_data (G_OBJECT (marker), "contact");
196   location = empathy_contact_get_location (contact);
197   name = empathy_contact_get_name (contact);
198   gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
199
200   if (gtime != NULL)
201     {
202       time_t now;
203
204       loctime = g_value_get_int64 (gtime);
205       date = empathy_time_to_string_relative (loctime);
206       label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
207       g_free (date);
208
209       now = time (NULL);
210
211       /* if location is older than a week */
212       if (now - loctime > (60 * 60 * 24 * 7))
213         clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
214     }
215   else
216     {
217       label = g_strconcat ("<b>", name, "</b>\n", NULL);
218     }
219
220   champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
221   champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
222
223   g_free (label);
224 }
225
226 static gboolean
227 map_view_contacts_foreach (GtkTreeModel *model,
228     GtkTreePath *path,
229     GtkTreeIter *iter,
230     gpointer user_data)
231 {
232   EmpathyMapView *window = (EmpathyMapView *) user_data;
233   EmpathyContact *contact;
234   ClutterActor *marker;
235   ClutterActor *texture;
236   GHashTable *location;
237   GdkPixbuf *avatar;
238
239   gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
240      &contact, -1);
241
242   if (contact == NULL)
243     return FALSE;
244
245   location = empathy_contact_get_location (contact);
246
247   if (location == NULL || g_hash_table_size (location) == 0)
248     return FALSE;
249
250   marker = champlain_marker_new ();
251
252   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
253   if (avatar != NULL)
254     {
255       texture = clutter_texture_new ();
256       gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
257           NULL);
258       champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
259       g_object_unref (avatar);
260     }
261   else
262     champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
263
264   g_object_set_data_full (G_OBJECT (marker), "contact",
265       g_object_ref (contact), g_object_unref);
266
267   map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
268
269   clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
270   g_signal_connect (marker, "button-release-event",
271       G_CALLBACK (marker_clicked_cb), contact);
272
273   clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
274
275   g_signal_connect (contact, "notify::location",
276       G_CALLBACK (map_view_contact_location_notify), marker);
277
278   map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);
279
280   g_object_unref (contact);
281   return FALSE;
282 }
283
284 static void
285 map_view_destroy_cb (GtkWidget *widget,
286     EmpathyMapView *window)
287 {
288   GList *item;
289
290   g_source_remove (window->timeout_id);
291
292   item = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
293   while (item != NULL)
294   {
295     EmpathyContact *contact;
296     ChamplainMarker *marker;
297
298     marker = CHAMPLAIN_MARKER (item->data);
299     contact = g_object_get_data (G_OBJECT (marker), "contact");
300     g_signal_handlers_disconnect_by_func (contact,
301         map_view_contact_location_notify, marker);
302
303     item = g_list_next (item);
304   }
305
306   g_object_unref (window->list_store);
307   g_object_unref (window->layer);
308   g_slice_free (EmpathyMapView, window);
309 }
310
311 static gboolean
312 map_view_key_press_cb (GtkWidget *widget,
313     GdkEventKey *event,
314     gpointer user_data)
315 {
316   if ((event->state & GDK_CONTROL_MASK && event->keyval == GDK_w)
317       || event->keyval == GDK_Escape)
318     {
319       gtk_widget_destroy (widget);
320       return TRUE;
321     }
322
323   return FALSE;
324 }
325
326 static gboolean
327 map_view_tick (EmpathyMapView *window)
328 {
329   GList *marker;
330
331   marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
332
333   for (; marker; marker = marker->next)
334     map_view_contacts_update_label (marker->data);
335
336   return TRUE;
337 }
338
339 GtkWidget *
340 empathy_map_view_show (void)
341 {
342   static EmpathyMapView *window = NULL;
343   GtkBuilder *gui;
344   GtkWidget *sw;
345   GtkWidget *embed;
346   GtkWidget *throbber_holder;
347   gchar *filename;
348   GtkTreeModel *model;
349   EmpathyContactList *list_iface;
350   EmpathyContactListStore *list_store;
351
352   if (window)
353     {
354       empathy_window_present (GTK_WINDOW (window->window), TRUE);
355       return window->window;
356     }
357
358   window = g_slice_new0 (EmpathyMapView);
359
360   /* Set up interface */
361   filename = empathy_file_lookup ("empathy-map-view.ui", "src");
362   gui = empathy_builder_get_file (filename,
363      "map_view", &window->window,
364      "zoom_in", &window->zoom_in,
365      "zoom_out", &window->zoom_out,
366      "map_scrolledwindow", &sw,
367      "throbber", &throbber_holder,
368      NULL);
369   g_free (filename);
370
371   empathy_builder_connect (gui, window,
372       "map_view", "destroy", map_view_destroy_cb,
373       "map_view", "key-press-event", map_view_key_press_cb,
374       "zoom_in", "clicked", map_view_zoom_in_cb,
375       "zoom_out", "clicked", map_view_zoom_out_cb,
376       "zoom_fit", "clicked", map_view_zoom_fit_cb,
377       NULL);
378
379   g_object_unref (gui);
380
381   /* Clear the static pointer to window if the dialog is destroyed */
382   g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
383
384   list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
385   list_store = empathy_contact_list_store_new (list_iface);
386   empathy_contact_list_store_set_show_groups (list_store, FALSE);
387   empathy_contact_list_store_set_show_avatars (list_store, TRUE);
388   g_object_unref (list_iface);
389
390   window->throbber = ephy_spinner_new ();
391   ephy_spinner_set_size (EPHY_SPINNER (window->throbber),
392       GTK_ICON_SIZE_LARGE_TOOLBAR);
393   gtk_widget_show (window->throbber);
394   gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
395
396   window->list_store = list_store;
397
398   /* Set up map view */
399   embed = gtk_champlain_embed_new ();
400   window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
401   g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
402      "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
403   champlain_view_center_on (window->map_view, 36, 0);
404
405   gtk_container_add (GTK_CONTAINER (sw), embed);
406   gtk_widget_show_all (embed);
407
408   window->layer = g_object_ref (champlain_layer_new ());
409   champlain_view_add_layer (window->map_view, window->layer);
410
411   g_signal_connect (window->map_view, "notify::state",
412       G_CALLBACK (map_view_state_changed), window);
413
414   /* Set up contact list. */
415   model = GTK_TREE_MODEL (window->list_store);
416   gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
417
418   empathy_window_present (GTK_WINDOW (window->window), TRUE);
419
420   /* Set up time updating loop */
421   window->timeout_id = g_timeout_add_seconds (5,
422       (GSourceFunc) map_view_tick, window);
423
424   return window->window;
425 }
426