Пример #1
0
static void
mnp_wc_get_position_cb (GeocluePosition       *position,
                        GeocluePositionFields  fields,
                        int                    timestamp,
                        double                 latitude,
                        double                 longitude,
                        double                 altitude,
                        GeoclueAccuracy       *accuracy,
                        GError                *error,
                        gpointer               userdata)
{
    MnpWorldClockPrivate *priv = GET_PRIVATE (userdata);
    GeoclueAccuracy *n_accuracy;

    if (error) {
        g_warning ("Unable to get position: %s\n", error->message);
        return;
    }

    printf("POSI: %lf %lf\n", latitude, longitude);
    n_accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_DETAILED, 0, 0);
    geoclue_reverse_geocode_position_to_address_async (priv->geo_reverse_geocode,
            latitude,
            longitude,
            n_accuracy,
            mnp_wc_reverse_geocode_cb,
            userdata);

    geoclue_accuracy_free (n_accuracy);

}
/**
 * geoclue_accuracy_copy:
 * @accuracy: A #GeoclueAccuracy
 *
 * Creates a copy of @accuracy.
 *
 * Return value: A newly allocated #GeoclueAccuracy
 */
GeoclueAccuracy *
geoclue_accuracy_copy (GeoclueAccuracy *accuracy)
{
	GeoclueAccuracyLevel level;
	double hor, ver;

	geoclue_accuracy_get_details (accuracy, &level, &hor, &ver);
	return geoclue_accuracy_new (level, hor, ver);
}
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;
}
static gboolean
geoclue_hostip_get_position (GcIfacePosition        *iface,
                                 GeocluePositionFields  *fields,
                                 int                    *timestamp,
                                 double                 *latitude,
                                 double                 *longitude,
                                 double                 *altitude,
                                 GeoclueAccuracy       **accuracy,
                                 GError                **error)
{
	GeoclueHostip *obj = (GEOCLUE_HOSTIP (iface));
	gchar *coord_str = NULL;

	*fields = GEOCLUE_POSITION_FIELDS_NONE;

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

	if (gc_web_service_get_string (obj->web_service,
	                                &coord_str, HOSTIP_LATLON_XPATH)) {
		if (sscanf (coord_str, "%lf,%lf", longitude , latitude) == 2) {
			*fields |= GEOCLUE_POSITION_FIELDS_LONGITUDE;
			*fields |= GEOCLUE_POSITION_FIELDS_LATITUDE;
		}
		g_free (coord_str);
	}

	time ((time_t *)timestamp);

	if (*fields == GEOCLUE_POSITION_FIELDS_NONE) {
		*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_NONE,
		                                  0, 0);
	} else {
		*accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_LOCALITY,
		                                  0, 0);
	}
	return TRUE;
}
Пример #5
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
geoclue_gypsy_init (GeoclueGypsy *gypsy)
{
	gypsy->status = GEOCLUE_STATUS_ERROR;
	gypsy->control = gypsy_control_get_default ();

	gc_provider_set_details (GC_PROVIDER (gypsy),
				 "org.freedesktop.Geoclue.Providers.Gypsy",
				 "/org/freedesktop/Geoclue/Providers/Gypsy",
				 "Gypsy", "Gypsy provider");

	gypsy->position_fields = GYPSY_POSITION_FIELDS_NONE;

	gypsy->accuracy = geoclue_accuracy_new (GEOCLUE_ACCURACY_LEVEL_NONE,
						0.0, 0.0);
}
static gboolean
get_position (GcIfacePosition       *gc,
	      GeocluePositionFields *fields,
	      int                   *timestamp,
	      double                *latitude,
	      double                *longitude,
	      double                *altitude,
	      GeoclueAccuracy      **accuracy,
	      GError               **error)
{
	GeoclueGypsy *gypsy = GEOCLUE_GYPSY (gc);
	GeoclueAccuracyLevel level;
	double horizontal, vertical;

	*timestamp = gypsy->timestamp;

	*fields = GEOCLUE_POSITION_FIELDS_NONE;
	if (gypsy->position_fields & GYPSY_POSITION_FIELDS_LATITUDE) {
		*fields |= GEOCLUE_POSITION_FIELDS_LATITUDE;
		*latitude = gypsy->latitude;
	}
	if (gypsy->position_fields & GYPSY_POSITION_FIELDS_LONGITUDE) {
		*fields |= GEOCLUE_POSITION_FIELDS_LONGITUDE;
		*longitude = gypsy->longitude;
	}
	if (gypsy->position_fields & GYPSY_POSITION_FIELDS_ALTITUDE) {
		*fields |= GEOCLUE_POSITION_FIELDS_ALTITUDE;
		*altitude = gypsy->altitude;
	}

	geoclue_accuracy_get_details (gypsy->accuracy, &level,
				      &horizontal, &vertical);
	*accuracy = geoclue_accuracy_new (level, horizontal, vertical);

	return TRUE;
}
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;
}