static void
add_connections_for_aps (NmtConnectDevice *nmtdev,
                         const GPtrArray  *connections)
{
	NmtConnectConnection *nmtconn;
	NMConnection *conn;
	NMAccessPoint *ap;
	const GPtrArray *aps;
	GHashTable *seen_ssids;
	GBytes *ssid;
	char *ap_hash;
	int i, c;

	aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nmtdev->device));
	if (!aps->len)
		return;

	seen_ssids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	for (i = 0; i < aps->len; i++) {
		ap = aps->pdata[i];

		if (!nm_access_point_get_ssid (ap))
			continue;

		ap_hash = hash_ap (ap);
		if (g_hash_table_contains (seen_ssids, ap_hash)) {
			g_free (ap_hash);
			continue;
		}
		g_hash_table_add (seen_ssids, ap_hash);

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->device = nmtdev->device;
		nmtconn->ap = g_object_ref (ap);
		ssid = nm_access_point_get_ssid (ap);
		if (ssid)
			nmtconn->ssid = nm_utils_ssid_to_utf8 (g_bytes_get_data (ssid, NULL),
			                                       g_bytes_get_size (ssid));

		for (c = 0; c < connections->len; c++) {
			conn = connections->pdata[c];
			if (   nm_device_connection_valid (nmtdev->device, conn)
			    && nm_access_point_connection_valid (ap, conn)) {
				nmtconn->name = nm_connection_get_id (conn);
				nmtconn->conn = g_object_ref (conn);
				break;
			}
		}

		if (!nmtconn->name)
			nmtconn->name = nmtconn->ssid ? nmtconn->ssid : "<unknown>";

		nmtdev->conns = g_slist_prepend (nmtdev->conns, nmtconn);
	}

	g_hash_table_unref (seen_ssids);
}
static void
add_connections_for_aps (NmtConnectDevice *nmtdev,
                         GSList           *connections)
{
	NmtConnectConnection *nmtconn;
	NMConnection *conn;
	NMAccessPoint *ap;
	const GPtrArray *aps;
	GHashTable *seen_ssids;
	char *ap_hash;
	GSList *iter;
	int i;

	aps = nm_device_wifi_get_access_points (NM_DEVICE_WIFI (nmtdev->device));
	if (!aps)
		return;

	seen_ssids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	for (i = 0; i < aps->len; i++) {
		ap = aps->pdata[i];

		if (!nm_access_point_get_ssid (ap))
			continue;

		ap_hash = hash_ap (ap);
		if (g_hash_table_contains (seen_ssids, ap_hash)) {
			g_free (ap_hash);
			continue;
		}
		g_hash_table_add (seen_ssids, ap_hash);

		nmtconn = g_slice_new0 (NmtConnectConnection);
		nmtconn->device = nmtdev->device;
		nmtconn->ap = g_object_ref (ap);
		nmtconn->ssid = nm_utils_ssid_to_utf8 (nm_access_point_get_ssid (ap));

		for (iter = connections; iter; iter = iter->next) {
			conn = iter->data;
			if (   nm_device_connection_valid (nmtdev->device, conn)
			    && nm_access_point_connection_valid (ap, conn)) {
				nmtconn->name = nm_connection_get_id (conn);
				nmtconn->conn = g_object_ref (conn);
				break;
			}
		}

		if (!iter)
			nmtconn->name = nmtconn->ssid;

		nmtdev->conns = g_slist_prepend (nmtdev->conns, nmtconn);
	}

	g_hash_table_unref (seen_ssids);
}
/**
 * nm_access_point_filter_connections:
 * @ap: an #NMAccessPoint to filter connections for
 * @connections: (element-type NMConnection): a list of
 * #NMConnection objects to filter
 *
 * Filters a given list of connections for a given #NMAccessPoint object and
 * return connections which may be activated with the access point.  Any
 * returned connections will match the @ap's SSID and (if given) BSSID and
 * other attributes like security settings, channel, etc.
 *
 * To obtain the list of connections that are compatible with this access point,
 * use nm_remote_settings_list_connections() and then filter the returned list
 * for a given #NMDevice using nm_device_filter_connections() and finally
 * filter that list with this function.
 *
 * Returns: (transfer container) (element-type NMConnection): a
 * list of #NMConnection objects that could be activated with the given @ap.
 * The elements of the list are owned by their creator and should not be freed
 * by the caller, but the returned list itself is owned by the caller and should
 * be freed with g_slist_free() when it is no longer required.
 **/
GSList *
nm_access_point_filter_connections (NMAccessPoint *ap, const GSList *connections)
{
	GSList *filtered = NULL;
	const GSList *iter;

	for (iter = connections; iter; iter = g_slist_next (iter)) {
		NMConnection *candidate = NM_CONNECTION (iter->data);

		if (nm_access_point_connection_valid (ap, candidate))
			filtered = g_slist_prepend (filtered, candidate);
	}

	return g_slist_reverse (filtered);
}