Blame clients/tui/nmtui-connect.c

Packit Service a1bd4f
/* SPDX-License-Identifier: GPL-2.0+ */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2013 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * SECTION:nmtui-connect
Packit 5756e2
 * @short_description: nm-applet-like functionality
Packit 5756e2
 *
Packit 5756e2
 * nmtui-connect implements activating and deactivating #NMConnections,
Packit 5756e2
 * including presenting a password dialog if necessary.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
Packit 5756e2
#include "nmt-newt.h"
Packit 5756e2
Packit 5756e2
#include "nmtui.h"
Packit 5756e2
#include "nmtui-connect.h"
Packit 5756e2
#include "nmt-connect-connection-list.h"
Packit 5756e2
#include "nmt-password-dialog.h"
Packit 5756e2
#include "nm-secret-agent-simple.h"
Packit 5756e2
#include "nm-vpn-helpers.h"
Packit 5756e2
#include "nm-client-utils.h"
Packit 5756e2
#include "nmt-utils.h"
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * Runs openconnect to authenticate. The current screen state is saved
Packit 5756e2
 * before starting the command and restored after it returns.
Packit 5756e2
 */
Packit 5756e2
static gboolean
Packit Service a1bd4f
openconnect_authenticate(NMConnection *connection, char **cookie, char **gateway, char **gwcert)
Packit 5756e2
{
Packit Service a1bd4f
    GError *      error = NULL;
Packit Service a1bd4f
    NMSettingVpn *s_vpn;
Packit Service a1bd4f
    gboolean      ret;
Packit Service a1bd4f
    int           status = 0;
Packit Service a1bd4f
    const char *  gw, *port;
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_message_dialog(
Packit Service a1bd4f
        _("openconnect will be run to authenticate.\nIt will return to nmtui when completed."));
Packit Service a1bd4f
Packit Service a1bd4f
    /* Get port */
Packit Service a1bd4f
    s_vpn = nm_connection_get_setting_vpn(connection);
Packit Service a1bd4f
    gw    = nm_setting_vpn_get_data_item(s_vpn, "gateway");
Packit Service a1bd4f
    port  = gw ? strrchr(gw, ':') : NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    newtSuspend();
Packit Service a1bd4f
Packit Service a1bd4f
    ret = nm_vpn_openconnect_authenticate_helper(gw, cookie, gateway, gwcert, &status, &error);
Packit Service a1bd4f
Packit Service a1bd4f
    newtResume();
Packit Service a1bd4f
Packit Service a1bd4f
    if (!ret) {
Packit Service a1bd4f
        nmt_newt_message_dialog(_("Error: openconnect failed: %s"), error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (WIFEXITED(status)) {
Packit Service a1bd4f
        if (WEXITSTATUS(status) != 0) {
Packit Service a1bd4f
            nmt_newt_message_dialog(_("openconnect failed with status %d"), WEXITSTATUS(status));
Packit Service a1bd4f
            return FALSE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else if (WIFSIGNALED(status)) {
Packit Service a1bd4f
        nmt_newt_message_dialog(_("openconnect failed with signal %d"), WTERMSIG(status));
Packit Service a1bd4f
        return FALSE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (gateway && *gateway && port) {
Packit Service a1bd4f
        char *tmp = *gateway;
Packit Service a1bd4f
        *gateway  = g_strdup_printf("%s%s", *gateway, port);
Packit Service a1bd4f
        g_free(tmp);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
secrets_requested(NMSecretAgentSimple *agent,
Packit Service a1bd4f
                  const char *         request_id,
Packit Service a1bd4f
                  const char *         title,
Packit Service a1bd4f
                  const char *         msg,
Packit Service a1bd4f
                  GPtrArray *          secrets,
Packit Service a1bd4f
                  gpointer             user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NmtNewtForm * form;
Packit Service a1bd4f
    NMConnection *connection = NM_CONNECTION(user_data);
Packit Service a1bd4f
    int           i;
Packit Service a1bd4f
Packit Service a1bd4f
    /* Get secrets for OpenConnect VPN */
Packit Service a1bd4f
    if (connection && nm_connection_is_type(connection, NM_SETTING_VPN_SETTING_NAME)) {
Packit Service a1bd4f
        NMSettingVpn *s_vpn = nm_connection_get_setting_vpn(connection);
Packit Service a1bd4f
Packit Service a1bd4f
        if (nm_streq0(nm_setting_vpn_get_service_type(s_vpn),
Packit Service a1bd4f
                      NM_SECRET_AGENT_VPN_TYPE_OPENCONNECT)) {
Packit Service a1bd4f
            gs_free char *cookie  = NULL;
Packit Service a1bd4f
            gs_free char *gateway = NULL;
Packit Service a1bd4f
            gs_free char *gwcert  = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
            openconnect_authenticate(connection, &cookie, &gateway, &gwcert);
Packit Service a1bd4f
Packit Service a1bd4f
            for (i = 0; i < secrets->len; i++) {
Packit Service a1bd4f
                NMSecretAgentSimpleSecret *secret = secrets->pdata[i];
Packit Service a1bd4f
Packit Service a1bd4f
                if (secret->secret_type != NM_SECRET_AGENT_SECRET_TYPE_VPN_SECRET)
Packit Service a1bd4f
                    continue;
Packit Service a1bd4f
                if (!nm_streq0(secret->vpn_type, NM_SECRET_AGENT_VPN_TYPE_OPENCONNECT))
Packit Service a1bd4f
                    continue;
Packit Service a1bd4f
                if (nm_streq0(secret->entry_id,
Packit Service a1bd4f
                              NM_SECRET_AGENT_ENTRY_ID_PREFX_VPN_SECRETS "cookie")) {
Packit Service a1bd4f
                    g_free(secret->value);
Packit Service a1bd4f
                    secret->value = g_steal_pointer(&cookie);
Packit Service a1bd4f
                } else if (nm_streq0(secret->entry_id,
Packit Service a1bd4f
                                     NM_SECRET_AGENT_ENTRY_ID_PREFX_VPN_SECRETS "gateway")) {
Packit Service a1bd4f
                    g_free(secret->value);
Packit Service a1bd4f
                    secret->value = g_steal_pointer(&gateway);
Packit Service a1bd4f
                } else if (nm_streq0(secret->entry_id,
Packit Service a1bd4f
                                     NM_SECRET_AGENT_ENTRY_ID_PREFX_VPN_SECRETS "gwcert")) {
Packit Service a1bd4f
                    g_free(secret->value);
Packit Service a1bd4f
                    secret->value = g_steal_pointer(&gwcert);
Packit Service a1bd4f
                }
Packit Service a1bd4f
            }
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    form = nmt_password_dialog_new(request_id, title, msg, secrets);
Packit Service a1bd4f
    nmt_newt_form_run_sync(form);
Packit Service a1bd4f
Packit Service a1bd4f
    if (nmt_password_dialog_succeeded(NMT_PASSWORD_DIALOG(form)))
Packit Service a1bd4f
        nm_secret_agent_simple_response(agent, request_id, secrets);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        nm_secret_agent_simple_response(agent, request_id, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    g_object_unref(form);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    NMDevice *          device;
Packit Service a1bd4f
    NMActiveConnection *active;
Packit Service a1bd4f
    NmtSyncOp *         op;
Packit 5756e2
} ActivateConnectionInfo;
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
connect_cancelled(NmtNewtForm *form, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    ActivateConnectionInfo *info  = user_data;
Packit Service a1bd4f
    GError *                error = NULL;
Packit 5756e2
Packit Service a1bd4f
    error = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
Packit Service a1bd4f
    nmt_sync_op_complete_boolean(info->op, FALSE, error);
Packit Service a1bd4f
    g_clear_error(&error);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
check_activated(ActivateConnectionInfo *info)
Packit 5756e2
{
Packit Service a1bd4f
    NMActiveConnectionState ac_state;
Packit Service a1bd4f
    const char *            reason = NULL;
Packit Service a1bd4f
    gs_free_error GError *error    = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    ac_state = nmc_activation_get_effective_state(info->active, info->device, &reason);
Packit Service a1bd4f
    if (!NM_IN_SET(ac_state,
Packit Service a1bd4f
                   NM_ACTIVE_CONNECTION_STATE_ACTIVATED,
Packit Service a1bd4f
                   NM_ACTIVE_CONNECTION_STATE_DEACTIVATED))
Packit Service a1bd4f
        return;
Packit Service a1bd4f
Packit Service a1bd4f
    if (ac_state == NM_ACTIVE_CONNECTION_STATE_DEACTIVATED) {
Packit Service a1bd4f
        nm_assert(reason);
Packit Service a1bd4f
        error = g_error_new(NM_CLIENT_ERROR,
Packit Service a1bd4f
                            NM_CLIENT_ERROR_FAILED,
Packit Service a1bd4f
                            _("Activation failed: %s"),
Packit Service a1bd4f
                            reason);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_sync_op_complete_boolean(info->op, error == NULL, error);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
activate_ac_state_changed(GObject *object, GParamSpec *pspec, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    check_activated(user_data);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
activate_device_state_changed(GObject *object, GParamSpec *pspec, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    check_activated(user_data);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
activate_callback(GObject *client, GAsyncResult *result, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NmtSyncOp *         op = user_data;
Packit Service a1bd4f
    NMActiveConnection *ac;
Packit Service a1bd4f
    GError *            error = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    ac = nm_client_activate_connection_finish(NM_CLIENT(client), result, &error);
Packit Service a1bd4f
    if (error)
Packit Service a1bd4f
        nmt_sync_op_complete_pointer(op, NULL, error);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        nmt_sync_op_complete_pointer(op, ac, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
add_and_activate_callback(GObject *client, GAsyncResult *result, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NmtSyncOp *         op = user_data;
Packit Service a1bd4f
    NMActiveConnection *ac;
Packit Service a1bd4f
    GError *            error = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    ac = nm_client_add_and_activate_connection_finish(NM_CLIENT(client), result, &error);
Packit Service a1bd4f
    if (error)
Packit Service a1bd4f
        nmt_sync_op_complete_pointer(op, NULL, error);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        nmt_sync_op_complete_pointer(op, ac, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
deactivate_connection(NMActiveConnection *ac)
Packit 5756e2
{
Packit Service a1bd4f
    GError *error = NULL;
Packit 5756e2
Packit Service a1bd4f
    if (!nm_client_deactivate_connection(nm_client, ac, NULL, &error)) {
Packit Service a1bd4f
        nmt_newt_message_dialog(_("Could not deactivate connection: %s"), error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
activate_connection(NMConnection *connection, NMDevice *device, NMObject *specific_object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtNewtForm *   form;
Packit Service a1bd4f
    gs_unref_object NMSecretAgentSimple *agent = NULL;
Packit Service a1bd4f
    NmtNewtWidget *                      label;
Packit Service a1bd4f
    NmtSyncOp                            op;
Packit Service a1bd4f
    const char *                         specific_object_path;
Packit Service a1bd4f
    NMActiveConnection *                 ac;
Packit Service a1bd4f
    GError *                             error = NULL;
Packit Service a1bd4f
    ActivateConnectionInfo               info  = {};
Packit Service a1bd4f
Packit Service a1bd4f
    form  = g_object_new(NMT_TYPE_NEWT_FORM, "escape-exits", TRUE, NULL);
Packit Service a1bd4f
    label = nmt_newt_label_new(_("Connecting..."));
Packit Service a1bd4f
    nmt_newt_form_set_content(form, label);
Packit Service a1bd4f
Packit Service a1bd4f
    agent = nm_secret_agent_simple_new("nmtui");
Packit Service a1bd4f
    if (agent) {
Packit Service a1bd4f
        if (connection) {
Packit Service a1bd4f
            nm_secret_agent_simple_enable(agent, nm_object_get_path(NM_OBJECT(connection)));
Packit Service a1bd4f
        }
Packit Service a1bd4f
        g_signal_connect(agent,
Packit Service a1bd4f
                         NM_SECRET_AGENT_SIMPLE_REQUEST_SECRETS,
Packit Service a1bd4f
                         G_CALLBACK(secrets_requested),
Packit Service a1bd4f
                         connection);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    specific_object_path = specific_object ? nm_object_get_path(specific_object) : NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    /* There's no way to cancel an nm_client_activate_connection() /
Packit Service a1bd4f
     * nm_client_add_and_activate_connection() call, so we always let them
Packit Service a1bd4f
     * complete, even if the user hits Esc; they shouldn't normally take long
Packit Service a1bd4f
     * to complete anyway.
Packit Service a1bd4f
     */
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_sync_op_init(&op);
Packit Service a1bd4f
    if (connection) {
Packit Service a1bd4f
        nm_client_activate_connection_async(nm_client,
Packit Service a1bd4f
                                            connection,
Packit Service a1bd4f
                                            device,
Packit Service a1bd4f
                                            specific_object_path,
Packit Service a1bd4f
                                            NULL,
Packit Service a1bd4f
                                            activate_callback,
Packit Service a1bd4f
                                            &op);
Packit Service a1bd4f
    } else {
Packit Service a1bd4f
        nm_client_add_and_activate_connection_async(nm_client,
Packit Service a1bd4f
                                                    NULL,
Packit Service a1bd4f
                                                    device,
Packit Service a1bd4f
                                                    specific_object_path,
Packit Service a1bd4f
                                                    NULL,
Packit Service a1bd4f
                                                    add_and_activate_callback,
Packit Service a1bd4f
                                                    &op);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_form_show(form);
Packit Service a1bd4f
Packit Service a1bd4f
    ac = nmt_sync_op_wait_pointer(&op, &error);
Packit Service a1bd4f
    if (!ac) {
Packit Service a1bd4f
        nmt_newt_message_dialog(_("Could not activate connection: %s"), error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
        goto done;
Packit Service a1bd4f
    } else if (nm_active_connection_get_state(ac) == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) {
Packit Service a1bd4f
        /* Already active */
Packit Service a1bd4f
        goto done;
Packit Service a1bd4f
    } else if (!nmt_newt_widget_get_realized(NMT_NEWT_WIDGET(form))) {
Packit Service a1bd4f
        /* User already hit Esc */
Packit Service a1bd4f
        goto done;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (agent && !connection) {
Packit Service a1bd4f
        connection = NM_CONNECTION(nm_active_connection_get_connection(ac));
Packit Service a1bd4f
        if (connection) {
Packit Service a1bd4f
            nm_secret_agent_simple_enable(agent, nm_object_get_path(NM_OBJECT(connection)));
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    /* Now wait for the connection to actually reach the ACTIVATED state,
Packit Service a1bd4f
     * allowing the user to cancel if it takes too long.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    nmt_sync_op_init(&op);
Packit Service a1bd4f
    info.active = ac;
Packit Service a1bd4f
    info.device = device;
Packit Service a1bd4f
    info.op     = &op;
Packit Service a1bd4f
Packit Service a1bd4f
    g_signal_connect(form, "quit", G_CALLBACK(connect_cancelled), &info;;
Packit Service a1bd4f
    g_signal_connect(ac,
Packit Service a1bd4f
                     "notify::" NM_ACTIVE_CONNECTION_STATE,
Packit Service a1bd4f
                     G_CALLBACK(activate_ac_state_changed),
Packit Service a1bd4f
                     &info;;
Packit Service a1bd4f
    if (device) {
Packit Service a1bd4f
        g_signal_connect(device,
Packit Service a1bd4f
                         "notify::" NM_DEVICE_STATE,
Packit Service a1bd4f
                         G_CALLBACK(activate_device_state_changed),
Packit Service a1bd4f
                         &info;;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (!nmt_sync_op_wait_boolean(&op, &error)) {
Packit Service a1bd4f
        if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
Packit Service a1bd4f
            nmt_newt_message_dialog(_("Could not activate connection: %s"), error->message);
Packit Service a1bd4f
        g_clear_error(&error);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_signal_handlers_disconnect_by_func(form, G_CALLBACK(connect_cancelled), &info;;
Packit Service a1bd4f
    g_signal_handlers_disconnect_by_func(ac, G_CALLBACK(activate_ac_state_changed), &info;;
Packit Service a1bd4f
    if (device)
Packit Service a1bd4f
        g_signal_handlers_disconnect_by_func(device,
Packit Service a1bd4f
                                             G_CALLBACK(activate_device_state_changed),
Packit Service a1bd4f
                                             &info;;
Packit Service a1bd4f
Packit Service a1bd4f
done:
Packit Service a1bd4f
    if (nmt_newt_widget_get_realized(NMT_NEWT_WIDGET(form)))
Packit Service a1bd4f
        nmt_newt_form_quit(form);
Packit Service a1bd4f
    g_object_unref(form);
Packit Service a1bd4f
Packit Service a1bd4f
    if (agent)
Packit Service a1bd4f
        nm_secret_agent_old_unregister(NM_SECRET_AGENT_OLD(agent), NULL, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
listbox_activated(NmtNewtListbox *listbox, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NmtConnectConnectionList *list = NMT_CONNECT_CONNECTION_LIST(listbox);
Packit Service a1bd4f
    NMConnection *            connection;
Packit Service a1bd4f
    NMDevice *                device;
Packit Service a1bd4f
    NMObject *                specific_object;
Packit Service a1bd4f
    NMActiveConnection *      ac;
Packit Service a1bd4f
Packit Service a1bd4f
    if (!nmt_connect_connection_list_get_selection(list,
Packit Service a1bd4f
                                                   &connection,
Packit Service a1bd4f
                                                   &device,
Packit Service a1bd4f
                                                   &specific_object,
Packit Service a1bd4f
                                                   &ac))
Packit Service a1bd4f
        return;
Packit Service a1bd4f
Packit Service a1bd4f
    if (ac)
Packit Service a1bd4f
        deactivate_connection(ac);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        activate_connection(connection, device, specific_object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
activate_clicked(NmtNewtButton *button, gpointer listbox)
Packit 5756e2
{
Packit Service a1bd4f
    listbox_activated(listbox, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
listbox_active_changed(GObject *object, GParamSpec *pspec, gpointer button)
Packit 5756e2
{
Packit Service a1bd4f
    NmtConnectConnectionList *list = NMT_CONNECT_CONNECTION_LIST(object);
Packit Service a1bd4f
    static const char *       activate, *deactivate;
Packit Service a1bd4f
    static int                deactivate_padding, activate_padding;
Packit Service a1bd4f
    NMActiveConnection *      ac;
Packit Service a1bd4f
    gboolean                  has_selection;
Packit Service a1bd4f
Packit Service a1bd4f
    if (G_UNLIKELY(activate == NULL)) {
Packit Service a1bd4f
        int activate_width, deactivate_width;
Packit Service a1bd4f
Packit Service a1bd4f
        activate         = _("Activate");
Packit Service a1bd4f
        activate_width   = nmt_newt_text_width(activate);
Packit Service a1bd4f
        deactivate       = _("Deactivate");
Packit Service a1bd4f
        deactivate_width = nmt_newt_text_width(deactivate);
Packit Service a1bd4f
Packit Service a1bd4f
        activate_padding   = MAX(0, deactivate_width - activate_width);
Packit Service a1bd4f
        deactivate_padding = MAX(0, activate_width - deactivate_width);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    has_selection = nmt_connect_connection_list_get_selection(list, NULL, NULL, NULL, &ac);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_component_set_sensitive(button, has_selection);
Packit Service a1bd4f
    if (has_selection && ac) {
Packit Service a1bd4f
        nmt_newt_button_set_label(button, deactivate);
Packit Service a1bd4f
        nmt_newt_widget_set_padding(button, 0, 0, deactivate_padding, 0);
Packit Service a1bd4f
    } else {
Packit Service a1bd4f
        nmt_newt_button_set_label(button, activate);
Packit Service a1bd4f
        nmt_newt_widget_set_padding(button, 0, 0, activate_padding, 0);
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static NmtNewtForm *
Packit Service a1bd4f
nmt_connect_connection_list(gboolean is_top)
Packit 5756e2
{
Packit Service a1bd4f
    int            screen_width, screen_height;
Packit Service a1bd4f
    NmtNewtForm *  form;
Packit Service a1bd4f
    NmtNewtWidget *list, *activate, *quit, *bbox, *grid;
Packit Service a1bd4f
Packit Service a1bd4f
    newtGetScreenSize(&screen_width, &screen_height);
Packit Service a1bd4f
Packit Service a1bd4f
    form = g_object_new(NMT_TYPE_NEWT_FORM,
Packit Service a1bd4f
                        "y",
Packit Service a1bd4f
                        2,
Packit Service a1bd4f
                        "height",
Packit Service a1bd4f
                        screen_height - 4,
Packit Service a1bd4f
                        "escape-exits",
Packit Service a1bd4f
                        TRUE,
Packit Service a1bd4f
                        NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    grid = nmt_newt_grid_new();
Packit Service a1bd4f
Packit Service a1bd4f
    list = nmt_connect_connection_list_new();
Packit Service a1bd4f
    nmt_newt_grid_add(NMT_NEWT_GRID(grid), list, 0, 0);
Packit Service a1bd4f
    nmt_newt_grid_set_flags(NMT_NEWT_GRID(grid),
Packit Service a1bd4f
                            list,
Packit Service a1bd4f
                            NMT_NEWT_GRID_FILL_X | NMT_NEWT_GRID_FILL_Y | NMT_NEWT_GRID_EXPAND_X
Packit Service a1bd4f
                                | NMT_NEWT_GRID_EXPAND_Y);
Packit Service a1bd4f
    g_signal_connect(list, "activated", G_CALLBACK(listbox_activated), NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    bbox = nmt_newt_button_box_new(NMT_NEWT_BUTTON_BOX_VERTICAL);
Packit Service a1bd4f
    nmt_newt_grid_add(NMT_NEWT_GRID(grid), bbox, 1, 0);
Packit Service a1bd4f
    nmt_newt_widget_set_padding(bbox, 1, 1, 0, 1);
Packit Service a1bd4f
Packit Service a1bd4f
    activate = nmt_newt_button_box_add_start(NMT_NEWT_BUTTON_BOX(bbox), _("Activate"));
Packit Service a1bd4f
    g_signal_connect(list, "notify::active", G_CALLBACK(listbox_active_changed), activate);
Packit Service a1bd4f
    listbox_active_changed(G_OBJECT(list), NULL, activate);
Packit Service a1bd4f
    g_signal_connect(activate, "clicked", G_CALLBACK(activate_clicked), list);
Packit Service a1bd4f
Packit Service a1bd4f
    quit = nmt_newt_button_box_add_end(NMT_NEWT_BUTTON_BOX(bbox), is_top ? _("Quit") : _("Back"));
Packit Service a1bd4f
    nmt_newt_widget_set_exit_on_activate(quit, TRUE);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_form_set_content(form, grid);
Packit Service a1bd4f
    return form;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static NmtNewtForm *
Packit Service a1bd4f
nmt_connect_connection(const char *identifier)
Packit 5756e2
{
Packit Service a1bd4f
    NmtNewtWidget *     list;
Packit Service a1bd4f
    NMConnection *      connection;
Packit Service a1bd4f
    NMDevice *          device;
Packit Service a1bd4f
    NMObject *          specific_object;
Packit Service a1bd4f
    NMActiveConnection *ac;
Packit Service a1bd4f
Packit Service a1bd4f
    list = nmt_connect_connection_list_new();
Packit Service a1bd4f
    if (!nmt_connect_connection_list_get_connection(NMT_CONNECT_CONNECTION_LIST(list),
Packit Service a1bd4f
                                                    identifier,
Packit Service a1bd4f
                                                    &connection,
Packit Service a1bd4f
                                                    &device,
Packit Service a1bd4f
                                                    &specific_object,
Packit Service a1bd4f
                                                    &ac))
Packit Service a1bd4f
        nmt_newt_message_dialog(_("No such connection '%s'"), identifier);
Packit Service a1bd4f
    else if (ac)
Packit Service a1bd4f
        nmt_newt_message_dialog(_("Connection is already active"));
Packit Service a1bd4f
    else
Packit Service a1bd4f
        activate_connection(connection, device, specific_object);
Packit Service a1bd4f
    g_object_unref(list);
Packit Service a1bd4f
Packit Service a1bd4f
    return NULL;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
NmtNewtForm *
Packit Service a1bd4f
nmtui_connect(gboolean is_top, int argc, char **argv)
Packit 5756e2
{
Packit Service a1bd4f
    if (argc == 2)
Packit Service a1bd4f
        return nmt_connect_connection(argv[1]);
Packit Service a1bd4f
    else
Packit Service a1bd4f
        return nmt_connect_connection_list(is_top);
Packit 5756e2
}