]> git.0d.be Git - empathy.git/blobdiff - src/empathy-map-view.c
Update marker's label every second
[empathy.git] / src / empathy-map-view.c
index c6affb409d15867884a1b88bef0a36b4f98c9026..9eadee756de555a197fff5f494db44d47232c033 100644 (file)
@@ -1,22 +1,21 @@
 /*
- * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2008, 2009 Collabora Ltd.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
- * Authors: Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
+ * Authors: Pierre-Luc Beaudoin <pierre-luc.beaudoin@collabora.co.uk>
  */
 
 #include <config.h>
 
 #include <champlain/champlain.h>
 #include <champlain-gtk/champlain-gtk.h>
-#include <clutter-gtk/gtk-clutter-embed.h>
-#include <geoclue/geoclue-geocode.h>
+#include <clutter-gtk/clutter-gtk.h>
 #include <telepathy-glib/util.h>
 
 #include <libempathy/empathy-contact.h>
-#include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-chatroom-manager.h>
-#include <libempathy/empathy-chatroom.h>
-#include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-contact-manager.h>
-#include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-location.h>
-#include <libempathy/empathy-status-presets.h>
 
-#include <libempathy-gtk/empathy-contact-dialogs.h>
 #include <libempathy-gtk/empathy-contact-list-store.h>
 #include <libempathy-gtk/empathy-contact-list-view.h>
 #include <libempathy-gtk/empathy-presence-chooser.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
-#include <libempathy-gtk/empathy-geometry.h>
-#include <libempathy-gtk/empathy-conf.h>
-#include <libempathy-gtk/empathy-log-window.h>
-#include <libempathy-gtk/empathy-new-message-dialog.h>
-#include <libempathy-gtk/empathy-gtk-enum-types.h>
 
-#include "empathy-accounts-dialog.h"
 #include "empathy-map-view.h"
 #include "ephy-spinner.h"
-#include "empathy-preferences.h"
-#include "empathy-about-dialog.h"
-#include "empathy-new-chatroom-dialog.h"
-#include "empathy-chatrooms-window.h"
-#include "empathy-event-manager.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_LOCATION
 #include <libempathy/empathy-debug.h>
@@ -70,290 +51,334 @@ typedef struct {
   GtkWidget *window;
   GtkWidget *zoom_in;
   GtkWidget *zoom_out;
+  GtkWidget *throbber;
   ChamplainView *map_view;
   ChamplainLayer *layer;
-
+  guint timeout_id;
 } EmpathyMapView;
 
-static void map_view_destroy_cb (GtkWidget *widget,
-    EmpathyMapView *window);
-static gboolean map_view_contacts_foreach (GtkTreeModel *model,
-    GtkTreePath *path, GtkTreeIter *iter, gpointer user_data);
-static void map_view_zoom_in_cb (GtkWidget *widget, EmpathyMapView *window);
-static void map_view_zoom_out_cb (GtkWidget *widget, EmpathyMapView *window);
-static void map_view_contact_location_notify (GObject *gobject,
-    GParamSpec *arg1, gpointer user_data);
-static gchar * get_dup_string (GHashTable *location, gchar *key);
-
-// FIXME: Make it so that only one window can be shown
-GtkWidget *
-empathy_map_view_show ()
-{
-  static EmpathyMapView *window = NULL;
-  GtkBuilder *gui;
-  GtkWidget *sw;
-  GtkWidget *embed;
-  gchar *filename;
-  GtkTreeModel *model;
-  EmpathyContactList *list_iface;
-  EmpathyContactListStore *list_store;
-
-  /*
-  if (window)
-    {
-      empathy_window_present (GTK_WINDOW (window->window), TRUE);
-      return window->window;
-    }
-  */
-
-  window = g_new0 (EmpathyMapView, 1);
-
-  /* Set up interface */
-  filename = empathy_file_lookup ("empathy-map-view.ui", "src");
-  gui = empathy_builder_get_file (filename, "map_view",
-      &window->window, "zoom_in", &window->zoom_in, "zoom_out",
-      &window->zoom_out, "map_scrolledwindow", &sw, NULL);
-  g_free (filename);
-
-  empathy_builder_connect (gui, window, "map_view", "destroy",
-      map_view_destroy_cb, "zoom_in", "clicked", map_view_zoom_in_cb,
-      "zoom_out", "clicked", map_view_zoom_out_cb, NULL);
-
-  g_object_unref (gui);
-
-  list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
-  list_store = empathy_contact_list_store_new (list_iface);
-  empathy_contact_list_store_set_show_groups (list_store, FALSE);
-  empathy_contact_list_store_set_show_avatars (list_store, TRUE);
-  g_object_unref (list_iface);
-
-  window->list_store = list_store;
-
-  /* Set up map view */
-  window->map_view = CHAMPLAIN_VIEW (champlain_view_new (
-      CHAMPLAIN_VIEW_MODE_KINETIC));
-  g_object_set (G_OBJECT (window->map_view), "zoom-level", 1, NULL);
-  champlain_view_center_on (window->map_view, 36, 0);
-
-  embed = champlain_view_embed_new (window->map_view);
-  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (sw),
-     GTK_WIDGET (embed));
-  gtk_widget_show_all (embed);
-
-  window->layer = champlain_layer_new ();
-  champlain_view_add_layer (CHAMPLAIN_VIEW (window->map_view), window->layer);
-
-  /* Set up contact list. */
-  model = GTK_TREE_MODEL (window->list_store);
-  gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
-
-  empathy_window_present (GTK_WINDOW (window->window), TRUE);
-  return window->window;
-}
-
 static void
-map_view_destroy_cb (GtkWidget *widget,
-                     EmpathyMapView *window)
+map_view_state_changed (ChamplainView *view,
+    GParamSpec *gobject,
+    EmpathyMapView *window)
 {
-  GtkTreeModel *model;
+  ChamplainState state;
 
-  g_object_unref (window->list_store);
-  g_free (window);
+  g_object_get (G_OBJECT (view), "state", &state, NULL);
+  if (state == CHAMPLAIN_STATE_LOADING)
+    ephy_spinner_start (EPHY_SPINNER (window->throbber));
+  else
+    ephy_spinner_stop (EPHY_SPINNER (window->throbber));
 }
 
-#define GEOCODE_SERVICE "org.freedesktop.Geoclue.Providers.Yahoo"
-#define GEOCODE_PATH "/org/freedesktop/Geoclue/Providers/Yahoo"
-
 static void
-map_view_geocode_cb (GeoclueGeocode *geocode,
-                     GeocluePositionFields fields,
-                     double latitude,
-                     double longitude,
-                     double altitude,
-                     GeoclueAccuracy *accuracy,
-                     GError *error,
-                     gpointer userdata)
+map_view_marker_update_position (ChamplainMarker *marker,
+    EmpathyContact *contact)
 {
-  GValue *new_value;
-  gboolean found = FALSE;
-  GHashTable *location = empathy_contact_get_location (EMPATHY_CONTACT (userdata));
-  g_hash_table_ref (location);
+  gdouble lon, lat;
+  GValue *value;
+  GHashTable *location;
 
-  if (error)
+  location = empathy_contact_get_location (contact);
+
+  if (location == NULL ||
+      g_hash_table_size (location) == 0)
   {
-      return;
+    champlain_base_marker_animate_out (CHAMPLAIN_BASE_MARKER (marker));
+    return;
   }
 
-  if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE)
-    {
-      new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
-      g_value_set_double (new_value, longitude);
-      g_hash_table_replace (location, EMPATHY_LOCATION_LON, new_value);
-      DEBUG ("\t - Longitude: %f", longitude);
-    }
-  if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE)
+  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
+  if (value == NULL)
     {
-      new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
-      g_value_set_double (new_value, latitude);
-      g_hash_table_replace (location, EMPATHY_LOCATION_LAT, new_value);
-      DEBUG ("\t - Latitude: %f", latitude);
-      found = TRUE;
+      clutter_actor_hide (CLUTTER_ACTOR (marker));
+      return;
     }
-  if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE)
+  lat = g_value_get_double (value);
+
+  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
+  if (value == NULL)
     {
-      new_value = tp_g_value_slice_new (G_TYPE_DOUBLE);
-      g_value_set_double (new_value, altitude);
-      g_hash_table_replace (location, EMPATHY_LOCATION_ALT, new_value);
-      DEBUG ("\t - Altitude: %f", altitude);
+      clutter_actor_hide (CLUTTER_ACTOR (marker));
+      return;
     }
+  lon = g_value_get_double (value);
 
-  //Don't change the accuracy as we used an address to get this position
-  if (found)
-    empathy_contact_set_location (EMPATHY_CONTACT (userdata), location);
-  g_hash_table_unref (location);
+  champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
+  champlain_base_marker_animate_in (CHAMPLAIN_BASE_MARKER (marker));
 }
 
-
-static gchar *
-get_dup_string (GHashTable *location, gchar *key)
+static void
+map_view_contact_location_notify (EmpathyContact *contact,
+    GParamSpec *arg1,
+    ChamplainMarker *marker)
 {
-  GValue *value;
-
-  value = g_hash_table_lookup (location, key);
-  if (value)
-  {
-      return g_value_dup_string (value);
-  }
-  return NULL;
+  map_view_marker_update_position (marker, contact);
+}
 
+static void
+map_view_zoom_in_cb (GtkWidget *widget,
+    EmpathyMapView *window)
+{
+  champlain_view_zoom_in (window->map_view);
 }
 
 static void
-map_view_marker_update (ChamplainMarker *marker,
-                        EmpathyContact *contact)
+map_view_zoom_out_cb (GtkWidget *widget,
+    EmpathyMapView *window)
 {
-  gdouble lon, lat;
-  GValue *value;
-  GHashTable *location = empathy_contact_get_location (contact);
+  champlain_view_zoom_out (window->map_view);
+}
 
+static gboolean
+marker_clicked_cb (ChamplainMarker *marker,
+    ClutterButtonEvent *event,
+    EmpathyContact *contact)
+{
+  GtkWidget *menu;
 
-  if (location == NULL ||
-      g_hash_table_size (location) == 0)
-  {
-    clutter_actor_hide (CLUTTER_ACTOR (marker));
-    return;
-  }
+  if (event->button != 3)
+    return FALSE;
 
-  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
-  if (value == NULL)
-    {
-      GeoclueGeocode * geocode = geoclue_geocode_new (GEOCODE_SERVICE,
-          GEOCODE_PATH);
-      gchar *str;
+  menu = empathy_contact_menu_new (contact,
+      EMPATHY_CONTACT_FEATURE_CHAT |
+      EMPATHY_CONTACT_FEATURE_CALL |
+      EMPATHY_CONTACT_FEATURE_LOG |
+      EMPATHY_CONTACT_FEATURE_INFO);
 
-      GHashTable *address = geoclue_address_details_new();
-      str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
-      if (str != NULL)
-        g_hash_table_insert (address, g_strdup ("country"), str);
+  if (menu == NULL)
+    return FALSE;
 
-      str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
-      if (str != NULL)
-        g_hash_table_insert (address, g_strdup ("postalcode"), str);
+  gtk_widget_show (menu);
+  gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
+      event->button, event->time);
 
-      str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
-      if (str != NULL)
-        g_hash_table_insert (address, g_strdup ("locality"), str);
+  return FALSE;
+}
 
-      str = get_dup_string (location, EMPATHY_LOCATION_STREET);
-      if (str != NULL)
-        g_hash_table_insert (address, g_strdup ("street"), str);
+static void
+map_view_contacts_update_label (ChamplainMarker *marker)
+{
+  const gchar *name;
+  gchar *date;
+  gchar *label;
+  GValue *gtime;
+  time_t loctime;
+  GHashTable *location;
+  EmpathyContact *contact;
 
-      geoclue_geocode_address_to_position_async (geocode, address,
-          map_view_geocode_cb, contact);
-      clutter_actor_hide (CLUTTER_ACTOR (marker));
-      return;
-    }
-  lat = g_value_get_double (value);
+  contact = g_object_get_data (G_OBJECT (marker), "contact");
+  location = empathy_contact_get_location (contact);
+  name = empathy_contact_get_name (contact);
+  gtime = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
 
-  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
-  if (value == NULL)
-    return;
-  lon = g_value_get_double (value);
+  if (gtime != NULL)
+    {
+      time_t now;
 
-  clutter_actor_show (CLUTTER_ACTOR (marker));
-  champlain_marker_set_position (marker, lat, lon);
-}
+      loctime = g_value_get_int64 (gtime);
+      date = empathy_time_to_string_relative (loctime);
+      label = g_strconcat ("<b>", name, "</b>\n<small>", date, "</small>", NULL);
+      g_free (date);
 
+      now = time (NULL);
 
-static void
-map_view_contact_location_notify (GObject *gobject,
-                                  GParamSpec *arg1,
-                                  gpointer user_data)
-{
-  ChamplainMarker *marker = CHAMPLAIN_MARKER (user_data);
-  EmpathyContact *contact = EMPATHY_CONTACT (gobject);
-  map_view_marker_update (marker, contact);
-}
+      /* if location is older than a week */
+      if (now - loctime > (60 * 60 * 24 * 7))
+        clutter_actor_set_opacity (CLUTTER_ACTOR (marker), 0.75 * 255);
+    }
+  else
+    {
+      label = g_strconcat ("<b>", name, "</b>\n", NULL);
+    }
 
+  champlain_marker_set_use_markup (CHAMPLAIN_MARKER (marker), TRUE);
+  champlain_marker_set_text (CHAMPLAIN_MARKER (marker), label);
+
+  g_free (label);
+}
 
 static gboolean
 map_view_contacts_foreach (GtkTreeModel *model,
-                           GtkTreePath *path,
-                           GtkTreeIter *iter,
-                           gpointer user_data)
+    GtkTreePath *path,
+    GtkTreeIter *iter,
+    gpointer user_data)
 {
-  EmpathyMapView *window = (EmpathyMapView*) user_data;
+  EmpathyMapView *window = (EmpathyMapView *) user_data;
   EmpathyContact *contact;
   ClutterActor *marker;
   ClutterActor *texture;
   GHashTable *location;
-  GValue *value;
   GdkPixbuf *avatar;
-  guint handle_id;
 
   gtk_tree_model_get (model, iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
      &contact, -1);
+
   if (contact == NULL)
     return FALSE;
 
-  marker = champlain_marker_new ();
+  location = empathy_contact_get_location (contact);
 
-  handle_id = g_signal_connect (contact, "notify::location",
-      G_CALLBACK (map_view_contact_location_notify), marker);
-  g_object_set_data (G_OBJECT (contact), "map-view-handle",
-      GINT_TO_POINTER (handle_id));
+  if (location == NULL || g_hash_table_size (location) == 0)
+    return FALSE;
 
-  clutter_actor_set_anchor_point (marker, 16, 16);
+  marker = champlain_marker_new ();
 
   avatar = empathy_pixbuf_avatar_from_contact_scaled (contact, 32, 32);
-  if (avatar)
+  if (avatar != NULL)
     {
       texture = clutter_texture_new ();
-      gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar);
-      clutter_actor_set_position (CLUTTER_ACTOR (texture), 5, 5);
-      clutter_container_add (CLUTTER_CONTAINER (marker), texture, NULL);
+      gtk_clutter_texture_set_from_pixbuf (CLUTTER_TEXTURE (texture), avatar,
+          NULL);
+      champlain_marker_set_image (CHAMPLAIN_MARKER (marker), texture);
+      g_object_unref (avatar);
     }
+  else
+    champlain_marker_set_image (CHAMPLAIN_MARKER (marker), NULL);
+
+  g_object_set_data_full (G_OBJECT (marker), "contact",
+      g_object_ref (contact), g_object_unref);
+
+  map_view_contacts_update_label (CHAMPLAIN_MARKER (marker));
 
-  map_view_marker_update (CHAMPLAIN_MARKER (marker), contact);
+  clutter_actor_set_reactive (CLUTTER_ACTOR (marker), TRUE);
+  g_signal_connect (marker, "button-release-event",
+      G_CALLBACK (marker_clicked_cb), contact);
 
   clutter_container_add (CLUTTER_CONTAINER (window->layer), marker, NULL);
 
+  g_signal_connect (contact, "notify::location",
+      G_CALLBACK (map_view_contact_location_notify), marker);
+
+  map_view_marker_update_position (CHAMPLAIN_MARKER (marker), contact);
+
   g_object_unref (contact);
   return FALSE;
 }
 
-
 static void
-map_view_zoom_in_cb (GtkWidget *widget,
-                     EmpathyMapView *window)
+map_view_destroy_cb (GtkWidget *widget,
+    EmpathyMapView *window)
 {
-  champlain_view_zoom_in (window->map_view);
+  GList *item;
+
+  g_source_remove (window->timeout_id);
+
+  item = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
+  while (item != NULL)
+  {
+    EmpathyContact *contact;
+    ChamplainMarker *marker;
+
+    marker = CHAMPLAIN_MARKER (item->data);
+    contact = g_object_get_data (G_OBJECT (marker), "contact");
+    g_signal_handlers_disconnect_by_func (contact,
+        map_view_contact_location_notify, marker);
+
+    item = g_list_next (item);
+  }
+
+  g_object_unref (window->list_store);
+  g_object_unref (window->layer);
+  g_slice_free (EmpathyMapView, window);
 }
 
+static gboolean
+map_view_tick (EmpathyMapView *window)
+{
+  GList *marker;
 
-static void
-map_view_zoom_out_cb (GtkWidget *widget,
-                      EmpathyMapView *window)
+  marker = clutter_container_get_children (CLUTTER_CONTAINER (window->layer));
+
+  for (; marker; marker = marker->next)
+    map_view_contacts_update_label (marker->data);
+
+  return TRUE;
+}
+
+GtkWidget *
+empathy_map_view_show (void)
 {
-  champlain_view_zoom_out (window->map_view);
+  static EmpathyMapView *window = NULL;
+  GtkBuilder *gui;
+  GtkWidget *sw;
+  GtkWidget *embed;
+  GtkWidget *throbber_holder;
+  gchar *filename;
+  GtkTreeModel *model;
+  EmpathyContactList *list_iface;
+  EmpathyContactListStore *list_store;
+
+  if (window)
+    {
+      empathy_window_present (GTK_WINDOW (window->window), TRUE);
+      return window->window;
+    }
+
+  window = g_slice_new0 (EmpathyMapView);
+
+  /* Set up interface */
+  filename = empathy_file_lookup ("empathy-map-view.ui", "src");
+  gui = empathy_builder_get_file (filename,
+     "map_view", &window->window,
+     "zoom_in", &window->zoom_in,
+     "zoom_out", &window->zoom_out,
+     "map_scrolledwindow", &sw,
+     "throbber", &throbber_holder,
+     NULL);
+  g_free (filename);
+
+  empathy_builder_connect (gui, window,
+      "map_view", "destroy", map_view_destroy_cb,
+      "zoom_in", "clicked", map_view_zoom_in_cb,
+      "zoom_out", "clicked", map_view_zoom_out_cb,
+      NULL);
+
+  g_object_unref (gui);
+
+  /* Clear the static pointer to window if the dialog is destroyed */
+  g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer *) &window);
+
+  list_iface = EMPATHY_CONTACT_LIST (empathy_contact_manager_dup_singleton ());
+  list_store = empathy_contact_list_store_new (list_iface);
+  empathy_contact_list_store_set_show_groups (list_store, FALSE);
+  empathy_contact_list_store_set_show_avatars (list_store, TRUE);
+  g_object_unref (list_iface);
+
+  window->throbber = ephy_spinner_new ();
+  ephy_spinner_set_size (EPHY_SPINNER (window->throbber),
+      GTK_ICON_SIZE_LARGE_TOOLBAR);
+  gtk_widget_show (window->throbber);
+  gtk_container_add (GTK_CONTAINER (throbber_holder), window->throbber);
+
+  window->list_store = list_store;
+
+  /* Set up map view */
+  embed = gtk_champlain_embed_new ();
+  window->map_view = gtk_champlain_embed_get_view (GTK_CHAMPLAIN_EMBED (embed));
+  g_object_set (G_OBJECT (window->map_view), "zoom-level", 1,
+     "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
+  champlain_view_center_on (window->map_view, 36, 0);
+
+  gtk_container_add (GTK_CONTAINER (sw), embed);
+  gtk_widget_show_all (embed);
+
+  window->layer = g_object_ref (champlain_layer_new ());
+  champlain_view_add_layer (window->map_view, window->layer);
+
+  g_signal_connect (window->map_view, "notify::state",
+      G_CALLBACK (map_view_state_changed), window);
+
+  /* Set up contact list. */
+  model = GTK_TREE_MODEL (window->list_store);
+  gtk_tree_model_foreach (model, map_view_contacts_foreach, window);
+
+  empathy_window_present (GTK_WINDOW (window->window), TRUE);
+
+  /* Set up time updating loop */
+  window->timeout_id = g_timeout_add_seconds (5,
+      (GSourceFunc) map_view_tick, window);
+
+  return window->window;
 }
+