Exemple #1
0
static gboolean
geoclue_localnet_set_address_fields (GeoclueLocalnet *localnet,
                                     char *country_code,
                                     char *country,
                                     char *region,
                                     char *locality,
                                     char *area,
                                     char *postalcode,
                                     char *street,
                                     GError **error)
{
	GHashTable *address;
	gboolean ret;
	
	address = geoclue_address_details_new ();
	if (country_code && (strlen (country_code) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRYCODE), 
		                     g_strdup (country_code));
		if (!country) {
			geoclue_address_details_set_country_from_code (address);
		}
	}
	if (country && (strlen (country) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_COUNTRY), 
		                     g_strdup (country));
	}
	if (region && (strlen (region) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_REGION), 
		                     g_strdup (region));
	}
	if (locality && (strlen (locality) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_LOCALITY), 
		                     g_strdup (locality));
	}
	if (area && (strlen (area) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_AREA), 
		                     g_strdup (area));
	}
	if (postalcode && (strlen (postalcode) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_POSTALCODE), 
		                     g_strdup (postalcode));
	}
	if (street && (strlen (street) > 0)) {
		g_hash_table_insert (address,
		                     g_strdup (GEOCLUE_ADDRESS_KEY_STREET), 
		                     g_strdup (street));
	}
	
	ret = geoclue_localnet_set_address (localnet,
	                                    address,
	                                    error);
	g_hash_table_destroy (address);
	return ret;
}
static gboolean
get_address (GcIfaceAddress   *gc,
             int              *timestamp,
             GHashTable      **address,
             GeoclueAccuracy **accuracy,
             GError          **error)
{
	GeoclueLocalnet *localnet;
	int i, ret_val;
	char *mac = NULL;
	Gateway *gw;

	localnet = GEOCLUE_LOCALNET (gc);

	/* we may be trying to read /proc/net/arp right after network connection.
	 * It's sometimes not up yet, try a couple of times */
	for (i = 0; i < 5; i++) {
		ret_val = get_mac_address (&mac);
		if (ret_val < 0)
			return FALSE;
		else if (ret_val == 1)
			break;
		usleep (200);
	}

	if (!mac) {
		g_warning ("Couldn't get current gateway mac address");
		if (error) {
			g_set_error (error, GEOCLUE_ERROR,
			             GEOCLUE_ERROR_NOT_AVAILABLE, "Could not get current gateway mac address");
		}
		return FALSE;
	}

	gw = geoclue_localnet_find_gateway (localnet, mac);
	g_free (mac);

	if (timestamp) {
		*timestamp = time(NULL);
	}
	if (address) {
		if (gw) {
			*address = geoclue_address_details_copy (gw->address);
		} else {
			*address = geoclue_address_details_new ();
		}
	}
	if (accuracy) {
		if (gw) {
			*accuracy = geoclue_accuracy_copy (gw->accuracy);
		} else {
			*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_NONE, 0, 0);
		}
	}
	return TRUE;
}
/**
 * geoclue_address_details_copy:
 * @source: #GHashTable to copy
 *
 * Deep-copies a #GHashTable.
 *
 * Return value: New, deep copied #GHashTable
 */
GHashTable *
geoclue_address_details_copy (GHashTable *source)
{
	GHashTable *target;

	g_assert (source != NULL);

	target = geoclue_address_details_new ();
	g_hash_table_foreach (source,
	                      (GHFunc)copy_address_key_and_value,
	                      target);
	return target;
}
Exemple #4
0
static void
geoclue_localnet_load_gateways_from_keyfile (GeoclueLocalnet  *localnet, 
                                             GKeyFile         *keyfile)
{
	char **groups;
	char **g;
	GError *error = NULL;
	
	groups = g_key_file_get_groups (keyfile, NULL);
	g = groups;
	while (*g) {
		GeoclueAccuracyLevel level;
		char **keys;
		char **k;
		Gateway *gateway = g_new0 (Gateway, 1);
		
		gateway->mac = g_ascii_strdown (*g, -1);
		gateway->address = geoclue_address_details_new ();
		
		/* read all keys in the group as address fields */
		keys = g_key_file_get_keys (keyfile, *g,
		                            NULL, &error);
		if (error) {
			g_warning ("Could not load keys for group [%s] from %s: %s", 
			           *g, localnet->keyfile_name, error->message);
			g_error_free (error);
			error = NULL;
		}
		
		k = keys;
		while (*k) {
			char *value;
			
			value = g_key_file_get_string (keyfile, *g, *k, NULL);
			g_hash_table_insert (gateway->address, 
			                     *k, value);
			k++;
		}
		g_free (keys);
		
		level = geoclue_address_details_get_accuracy_level (gateway->address);
		gateway->accuracy = geoclue_accuracy_new (level, 0, 0);
		
		localnet->gateways = g_slist_prepend (localnet->gateways, gateway);
		
		g++;
	}
	g_strfreev (groups);
}
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
}
static gboolean
geoclue_hostip_get_address (GcIfaceAddress   *iface,
                            int              *timestamp,
                            GHashTable      **address,
                            GeoclueAccuracy **accuracy,
                            GError          **error)
{
	GeoclueHostip *obj = GEOCLUE_HOSTIP (iface);
	gchar *locality = NULL;
	gchar *country = NULL;
	gchar *country_code = NULL;

	if (!gc_web_service_query (obj->web_service, error, (char *)0)) {
		return FALSE;
	}

	if (address) {
		*address = geoclue_address_details_new ();
		if (gc_web_service_get_string (obj->web_service,
					       &locality, HOSTIP_LOCALITY_XPATH)) {
			/* hostip "sctructured data" for the win... */
			if (g_ascii_strcasecmp (locality, "(Unknown city)") == 0 ||
			    g_ascii_strcasecmp (locality, "(Unknown City?)") == 0) {

				g_free (locality);
				locality = NULL;
			} else {
				geoclue_address_details_insert (*address,
				                                GEOCLUE_ADDRESS_KEY_LOCALITY,
				                                locality);
			}
		}

		if (gc_web_service_get_string (obj->web_service,
					       &country_code, HOSTIP_COUNTRYCODE_XPATH)) {
			if (g_ascii_strcasecmp (country_code, "XX") == 0) {
				g_free (country_code);
				country_code = NULL;
			} else {
				geoclue_address_details_insert (*address,
				                                GEOCLUE_ADDRESS_KEY_COUNTRYCODE,
				                                country_code);
				geoclue_address_details_set_country_from_code (*address);
			}
		}

		if (!g_hash_table_lookup (*address, GEOCLUE_ADDRESS_KEY_COUNTRY) &&
		    gc_web_service_get_string (obj->web_service,
		                               &country, HOSTIP_COUNTRY_XPATH)) {
			if (g_ascii_strcasecmp (country, "(Unknown Country?)") == 0) {
				g_free (country);
				country = NULL;
			} else {
				geoclue_address_details_insert (*address,
				                                GEOCLUE_ADDRESS_KEY_COUNTRY,
				                                country);
			}
		}
	}

	if (timestamp) {
		*timestamp = time (NULL);
	}

	if (accuracy) {
		if (locality && country) {
			*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_LOCALITY,
							  0, 0);
		} else if (country) {
			*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_COUNTRY,
							  0, 0);
		} else {
			*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_NONE,
							  0, 0);
		}
	}
	g_free (locality);
	g_free (country);
	g_free (country_code);

	return TRUE;
}