/* This callback is called by geoclue when it found a position
 * for the given address.  A position is necessary for a contact
 * to show up on the map
 */
static void
geocode_cb (GeoclueGeocode *geocode,
	    GeocluePositionFields fields,
	    double latitude,
	    double longitude,
	    double altitude,
	    GeoclueAccuracy *accuracy,
	    GError *error,
	    gpointer contact)
{
	GValue *new_value;
	GHashTable *location;

	location = empathy_contact_get_location (EMPATHY_CONTACT (contact));

	if (error != NULL) {
		DEBUG ("Error geocoding location : %s", error->message);
		g_object_unref (geocode);
		g_object_unref (contact);
		return;
	}

	if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE) {
		new_value = tp_g_value_slice_new_double (latitude);
		g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LAT),
			new_value);
		DEBUG ("\t - Latitude: %f", latitude);
	}
	if (fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
		new_value = tp_g_value_slice_new_double (longitude);
		g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_LON),
			new_value);
		DEBUG ("\t - Longitude: %f", longitude);
	}
	if (fields & GEOCLUE_POSITION_FIELDS_ALTITUDE) {
		new_value = tp_g_value_slice_new_double (altitude);
		g_hash_table_replace (location, g_strdup (EMPATHY_LOCATION_ALT),
			new_value);
		DEBUG ("\t - Altitude: %f", altitude);
	}

	/* Don't change the accuracy as we used an address to get this position */
	g_object_notify (contact, "location");
	g_object_unref (geocode);
	g_object_unref (contact);
}
Пример #2
0
static void
contact_widget_location_update (EmpathyContactWidget *information)
{
  GHashTable *location;
  GValue *value;
  gdouble lat = 0.0, lon = 0.0;
  gboolean has_position = TRUE;
  GtkWidget *label;
  guint row = 0;
  GHashTableIter iter;
  gpointer key, pvalue;

  if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
    {
      gtk_widget_hide (information->vbox_location);
      return;
    }

  location = empathy_contact_get_location (information->contact);
  if (location == NULL || g_hash_table_size (location) == 0)
    {
      gtk_widget_hide (information->vbox_location);
      return;
    }

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
  if (value == NULL)
      has_position = FALSE;
  else
      lat = g_value_get_double (value);

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
  if (value == NULL)
      has_position = FALSE;
  else
      lon = g_value_get_double (value);

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
  if (value == NULL)
    gtk_label_set_markup (GTK_LABEL (information->label_location), _("<b>Location</b>"));
  else
    {
      gchar *user_date;
      gchar *text;
      gint64 stamp;
      time_t time;

      stamp = g_value_get_int64 (value);
      time = stamp;

      user_date = empathy_time_to_string_relative (time);

      text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
      gtk_label_set_markup (GTK_LABEL (information->label_location), text);
      g_free (text);
    }


  /* Prepare the location information table */
  if (information->table_location != NULL)
    {
      gtk_widget_destroy (information->table_location);
    }

  information->table_location = gtk_table_new (1, 2, FALSE);
  gtk_box_pack_start (GTK_BOX (information->subvbox_location),
      information->table_location, FALSE, FALSE, 5);

  g_hash_table_iter_init (&iter, location);
  while (g_hash_table_iter_next (&iter, &key, &pvalue))
    {
      const gchar *skey;
      const gchar* user_label;
      GValue *gvalue;
      char *svalue = NULL;

      skey = (const gchar *) key;

      user_label = location_key_to_label (skey);
      gvalue = (GValue *) pvalue;

      label = gtk_label_new (user_label);
      gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
      gtk_table_attach (GTK_TABLE (information->table_location),
          label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
      gtk_widget_show (label);

      if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
        {
          gdouble dvalue;
          dvalue = g_value_get_double (gvalue);
          svalue = g_strdup_printf ("%f", dvalue);
        }
      else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
        {
          svalue = g_value_dup_string (gvalue);
        }
      else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
        {
          time_t time;

          time = g_value_get_int64 (value);
          svalue = empathy_time_to_string_utc (time, _("%B %e, %Y at %R UTC"));
        }

      if (svalue != NULL)
        {
          label = gtk_label_new (svalue);
          gtk_table_attach_defaults (GTK_TABLE (information->table_location),
              label, 1, 2, row, row + 1);
          gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
          gtk_widget_show (label);
        }

      g_free (svalue);
      row++;
    }

  gtk_widget_show (information->table_location);

#if HAVE_LIBCHAMPLAIN
  /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
   * windows
   */
  if (has_position &&
      !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
    {
      ClutterActor *marker;
      ChamplainLayer *layer;

      information->map_view_embed = gtk_champlain_embed_new ();
      information->map_view = gtk_champlain_embed_get_view (
          GTK_CHAMPLAIN_EMBED (information->map_view_embed));

      gtk_container_add (GTK_CONTAINER (information->viewport_map),
          information->map_view_embed);
      g_object_set (G_OBJECT (information->map_view), "show-license", FALSE,
          "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
          NULL);

      layer = champlain_layer_new ();
      champlain_view_add_layer (information->map_view, layer);

      marker = champlain_marker_new_with_text (
          empathy_contact_get_name (information->contact), NULL, NULL, NULL);
      champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
      clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);

      champlain_view_center_on (information->map_view, lat, lon);
      gtk_widget_show_all (information->viewport_map);
    }
#endif

    gtk_widget_show (information->vbox_location);
}
static void
tp_contact_factory_geocode (EmpathyContact *contact)
{
#if HAVE_GEOCLUE
	static GeoclueGeocode *geocode;
	gchar *str;
	GHashTable *address;
	GValue* value;
	GHashTable *location;

	location = empathy_contact_get_location (contact);
	if (location == NULL)
		return;

	value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
	if (value != NULL)
		return;

	if (geocode == NULL) {
		geocode = geoclue_geocode_new (GEOCODE_SERVICE, GEOCODE_PATH);
		g_object_add_weak_pointer (G_OBJECT (geocode), (gpointer *) &geocode);
	}
	else
		g_object_ref (geocode);

	address = geoclue_address_details_new ();

	str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY_CODE);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), str);
		DEBUG ("\t - countrycode: %s", str);
	}

	str = get_dup_string (location, EMPATHY_LOCATION_COUNTRY);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), str);
		DEBUG ("\t - country: %s", str);
	}

	str = get_dup_string (location, EMPATHY_LOCATION_POSTAL_CODE);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), str);
		DEBUG ("\t - postalcode: %s", str);
	}

	str = get_dup_string (location, EMPATHY_LOCATION_REGION);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_REGION), str);
		DEBUG ("\t - region: %s", str);
	}

	str = get_dup_string (location, EMPATHY_LOCATION_LOCALITY);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), str);
		DEBUG ("\t - locality: %s", str);
	}

	str = get_dup_string (location, EMPATHY_LOCATION_STREET);
	if (str != NULL) {
		g_hash_table_insert (address,
			g_strdup (GEOCLUE_ADDRESS_KEY_STREET), str);
		DEBUG ("\t - street: %s", str);
	}

	g_object_ref (contact);
	geoclue_geocode_address_to_position_async (geocode, address,
		geocode_cb, contact);

	g_hash_table_unref (address);
#endif
}
Пример #4
0
static void
contact_widget_location_update (EmpathyContactWidget *information)
{
  GHashTable *location;
  GValue *value;
  gdouble lat = 0.0, lon = 0.0;
  gboolean has_position = TRUE;
  GtkWidget *label;
  guint row = 0;
  static const gchar* ordered_geolocation_keys[] = {
    EMPATHY_LOCATION_TEXT,
    EMPATHY_LOCATION_URI,
    EMPATHY_LOCATION_DESCRIPTION,
    EMPATHY_LOCATION_BUILDING,
    EMPATHY_LOCATION_FLOOR,
    EMPATHY_LOCATION_ROOM,
    EMPATHY_LOCATION_STREET,
    EMPATHY_LOCATION_AREA,
    EMPATHY_LOCATION_LOCALITY,
    EMPATHY_LOCATION_REGION,
    EMPATHY_LOCATION_COUNTRY,
    NULL
  };
  int i;
  const gchar *skey;
  gboolean display_map = FALSE;

  if (!(information->flags & EMPATHY_CONTACT_WIDGET_SHOW_LOCATION))
    {
      gtk_widget_hide (information->vbox_location);
      return;
    }

  location = empathy_contact_get_location (information->contact);
  if (location == NULL || g_hash_table_size (location) == 0)
    {
      gtk_widget_hide (information->vbox_location);
      return;
    }

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LAT);
  if (value == NULL)
      has_position = FALSE;
  else
      lat = g_value_get_double (value);

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_LON);
  if (value == NULL)
      has_position = FALSE;
  else
      lon = g_value_get_double (value);

  value = g_hash_table_lookup (location, EMPATHY_LOCATION_TIMESTAMP);
  if (value == NULL)
    {
      gchar *loc = g_strdup_printf ("<b>%s</b>", _("Location"));
      gtk_label_set_markup (GTK_LABEL (information->label_location), loc);
      g_free (loc);
    }
  else
    {
      gchar *user_date;
      gchar *text;
      gint64 stamp;
      time_t time_;

      stamp = g_value_get_int64 (value);
      time_ = stamp;

      user_date = empathy_time_to_string_relative (time_);

      text = g_strconcat ( _("<b>Location</b>, "), user_date, NULL);
      gtk_label_set_markup (GTK_LABEL (information->label_location), text);
      g_free (text);
    }


  /* Prepare the location information table */
  if (information->table_location != NULL)
    {
      gtk_widget_destroy (information->table_location);
    }

  information->table_location = gtk_table_new (1, 2, FALSE);
  gtk_box_pack_start (GTK_BOX (information->subvbox_location),
      information->table_location, FALSE, FALSE, 5);


  for (i = 0; (skey = ordered_geolocation_keys[i]); i++)
    {
      const gchar* user_label;
      GValue *gvalue;
      char *svalue = NULL;

      gvalue = g_hash_table_lookup (location, (gpointer) skey);
      if (gvalue == NULL)
        continue;

      user_label = location_key_to_label (skey);

      label = gtk_label_new (user_label);
      gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
      gtk_table_attach (GTK_TABLE (information->table_location),
          label, 0, 1, row, row + 1, GTK_FILL, GTK_FILL, 10, 0);
      gtk_widget_show (label);

      if (G_VALUE_TYPE (gvalue) == G_TYPE_DOUBLE)
        {
          gdouble dvalue;
          dvalue = g_value_get_double (gvalue);
          svalue = g_strdup_printf ("%f", dvalue);
        }
      else if (G_VALUE_TYPE (gvalue) == G_TYPE_STRING)
        {
          svalue = g_value_dup_string (gvalue);
        }
      else if (G_VALUE_TYPE (gvalue) == G_TYPE_INT64)
        {
          time_t time_;

          time_ = g_value_get_int64 (value);
          svalue = empathy_time_to_string_utc (time_, _("%B %e, %Y at %R UTC"));
        }

      if (svalue != NULL)
        {
          label = gtk_label_new (svalue);
          gtk_table_attach_defaults (GTK_TABLE (information->table_location),
              label, 1, 2, row, row + 1);
          gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
          gtk_widget_show (label);

          if (!(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
            gtk_label_set_selectable (GTK_LABEL (label), TRUE);
        }

      g_free (svalue);
      row++;
    }

#if HAVE_LIBCHAMPLAIN
  if (has_position &&
      !(information->flags & EMPATHY_CONTACT_WIDGET_FOR_TOOLTIP))
    {
      /* Cannot be displayed in tooltips until Clutter-Gtk can deal with such
       * windows */
      display_map = TRUE;
    }
#endif

  if (row > 0)
    {
      /* We can display some fields */
      gtk_widget_show (information->table_location);
    }
  else if (!display_map)
    {
      /* Can't display either fields or map */
      gtk_widget_hide (information->vbox_location);
      return;
    }

#if HAVE_LIBCHAMPLAIN
  if (display_map)
    {
      ClutterActor *marker;
      ChamplainLayer *layer;

      information->map_view_embed = gtk_champlain_embed_new ();
      information->map_view = gtk_champlain_embed_get_view (
          GTK_CHAMPLAIN_EMBED (information->map_view_embed));

      gtk_container_add (GTK_CONTAINER (information->viewport_map),
          information->map_view_embed);
      g_object_set (G_OBJECT (information->map_view),
          "show-license", TRUE,
          "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC,
          "zoom-level", 10,
          NULL);

      layer = champlain_layer_new ();
      champlain_view_add_layer (information->map_view, layer);

      marker = champlain_marker_new_with_text (
          empathy_contact_get_name (information->contact), NULL, NULL, NULL);
      champlain_base_marker_set_position (CHAMPLAIN_BASE_MARKER (marker), lat, lon);
      clutter_container_add (CLUTTER_CONTAINER (layer), marker, NULL);

      champlain_view_center_on (information->map_view, lat, lon);
      gtk_widget_show_all (information->viewport_map);
    }
#endif

    gtk_widget_show (information->vbox_location);
}