static void
connection_secrets_response_cb (NMAWirelessDialog *dialog,
                                gint response,
                                gpointer user_data)
{
    SecretsRequestInfo *info = user_data;
    NMConnection *connection;
    GHashTable *settings = NULL;
    NMSetting *s_wireless_sec;
    const char *key_mgmt;
    GError *error = NULL;

    gtk_widget_hide (GTK_WIDGET (dialog));

    connection = nma_wireless_dialog_get_connection (dialog);

    if (response != GTK_RESPONSE_OK) {
        error = g_error_new (NM_SETTINGS_INTERFACE_ERROR,
                             NM_SETTINGS_INTERFACE_ERROR_SECRETS_REQUEST_CANCELED,
                             "%s.%d (%s): canceled",
                             __FILE__, __LINE__, __func__);

        goto done;
    }

    /* Returned secrets are a{sa{sv}}; this is the outer a{s...} hash that
     * will contain all the individual settings hashes.
     */
    settings = g_hash_table_new_full (g_str_hash, g_str_equal,
                                      g_free, (GDestroyNotify) g_hash_table_destroy);

    /* If the user chose an 802.1x-based auth method, return 802.1x secrets,
     * not wireless secrets.  Can happen with Dynamic WEP, because NM doesn't
     * know the capabilities of the AP (since Dynamic WEP APs don't broadcast
     * beacons), and therefore defaults to requesting WEP secrets from the
     * wireless-security setting, not the 802.1x setting.
     */

    s_wireless_sec = nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS_SECURITY);
    key_mgmt = nm_setting_wireless_security_get_key_mgmt (NM_SETTING_WIRELESS_SECURITY (s_wireless_sec));
    if (!strcmp (key_mgmt, "ieee8021x") || !strcmp (key_mgmt, "wpa-eap")) {
        const char *auth_alg;

        /* LEAP secrets aren't in the 802.1x setting */
        auth_alg = nm_setting_wireless_security_get_auth_alg (NM_SETTING_WIRELESS_SECURITY (s_wireless_sec));
        if (!auth_alg || strcmp (auth_alg, "leap")) {
            NMSetting *s_8021x;

            s_8021x = nm_connection_get_setting (connection, NM_TYPE_SETTING_802_1X);
            if (!s_8021x) {
                error = g_error_new (NM_SETTINGS_INTERFACE_ERROR,
                                     NM_SETTINGS_INTERFACE_ERROR_INVALID_CONNECTION,
                                     "%s.%d (%s): requested setting '802-1x' didn't"
                                     " exist in the connection.",
                                     __FILE__, __LINE__, __func__);
                goto done;
            }

            /* Add the 802.1x setting */
            g_hash_table_insert (settings,
                                 g_strdup (nm_setting_get_name (s_8021x)),
                                 nm_setting_to_hash (s_8021x));
        }
    }

    /* Add the 802-11-wireless-security setting no matter what */
    g_hash_table_insert (settings,
                         g_strdup (nm_setting_get_name (s_wireless_sec)),
                         nm_setting_to_hash (s_wireless_sec));

    info->callback ((NMSettingsConnectionInterface *) connection, settings, NULL, info->callback_data);

    /* Save the connection back to GConf _after_ hashing it, because
     * saving to GConf might trigger the GConf change notifiers, resulting
     * in the connection being read back in from GConf which clears secrets.
     */
    if (NM_IS_GCONF_CONNECTION (connection)) {
        nm_settings_connection_interface_update (NM_SETTINGS_CONNECTION_INTERFACE (connection),
                update_cb,
                NULL);
    }

done:
    if (settings)
        g_hash_table_destroy (settings);

    if (error) {
        g_warning ("%s", error->message);
        info->callback (NM_SETTINGS_CONNECTION_INTERFACE (connection), NULL, error, info->callback_data);
        g_error_free (error);
    }

    g_free (info);

    if (connection)
        nm_connection_clear_secrets (connection);

    gtk_widget_destroy (GTK_WIDGET (dialog));
}
static void
wireless_dialog_response_cb (GtkDialog *foo,
                             gint response,
                             gpointer user_data)
{
    NMAWirelessDialog *dialog = NMA_WIRELESS_DIALOG (foo);
    WirelessDialogClosure *closure = user_data;
    NMConnection *connection, *fuzzy_match = NULL;
    NMDevice *device;
    NMAccessPoint *ap;
    GSList *all, *iter;

    if (response != GTK_RESPONSE_OK)
        goto done;

    if (!nma_wireless_dialog_get_nag_ignored (dialog)) {
        GtkWidget *nag_dialog;

        /* Nag the user about certificates or whatever.  Only destroy the dialog
         * if no nagging was done.
         */
        nag_dialog = nma_wireless_dialog_nag_user (dialog);
        if (nag_dialog) {
            gtk_window_set_transient_for (GTK_WINDOW (nag_dialog), GTK_WINDOW (dialog));
            g_signal_connect (nag_dialog, "response",
                              G_CALLBACK (nag_dialog_response_cb),
                              dialog);
            return;
        }
    }

    /* nma_wireless_dialog_get_connection() returns a connection with the
     * refcount incremented, so the caller must remember to unref it.
     */
    connection = nma_wireless_dialog_get_connection (dialog, &device, &ap);
    g_assert (connection);
    g_assert (device);

    /* Find a similar connection and use that instead */
    all = nm_remote_settings_list_connections (closure->settings);
    for (iter = all; iter; iter = g_slist_next (iter)) {
        if (nm_connection_compare (connection,
                                   NM_CONNECTION (iter->data),
                                   (NM_SETTING_COMPARE_FLAG_FUZZY | NM_SETTING_COMPARE_FLAG_IGNORE_ID))) {
            fuzzy_match = NM_CONNECTION (iter->data);
            break;
        }
    }
    g_slist_free (all);

    if (fuzzy_match) {
        nm_client_activate_connection (closure->client,
                                       fuzzy_match,
                                       device,
                                       ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
                                       activate_existing_cb,
                                       NULL);
    } else {
        NMSetting *s_con;
        NMSettingWireless *s_wifi;
        const char *mode = NULL;

        /* Entirely new connection */

        /* Don't autoconnect adhoc networks by default for now */
        s_wifi = (NMSettingWireless *) nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS);
        if (s_wifi)
            mode = nm_setting_wireless_get_mode (s_wifi);
        if (g_strcmp0 (mode, "adhoc") == 0) {
            s_con = nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION);
            if (!s_con) {
                s_con = nm_setting_connection_new ();
                nm_connection_add_setting (connection, s_con);
            }
            g_object_set (G_OBJECT (s_con), NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NULL);
        }

        nm_client_add_and_activate_connection (closure->client,
                                               connection,
                                               device,
                                               ap ? nm_object_get_path (NM_OBJECT (ap)) : NULL,
                                               activate_new_cb,
                                               NULL);
    }

    /* Balance nma_wireless_dialog_get_connection() */
    g_object_unref (connection);

done:
    gtk_widget_hide (GTK_WIDGET (dialog));
    gtk_widget_destroy (GTK_WIDGET (dialog));
}