Blame clients/tui/nmt-page-ip4.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2013 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * SECTION:nmt-page-ip4
Packit 5756e2
 * @short_description: The editor page for IP4 configuration
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-client.h"
Packit 5756e2
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
Packit 5756e2
#include "nmt-page-ip4.h"
Packit 5756e2
#include "nmt-ip-entry.h"
Packit 5756e2
#include "nmt-address-list.h"
Packit 5756e2
#include "nmt-route-editor.h"
Packit 5756e2
Packit 5756e2
#include "nm-editor-bindings.h"
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NmtPageIP4, nmt_page_ip4, NMT_TYPE_EDITOR_PAGE)
Packit 5756e2
Packit 5756e2
static NmtNewtPopupEntry ip4methods[] = {
Packit Service a1bd4f
    {N_("Disabled"), NM_SETTING_IP4_CONFIG_METHOD_DISABLED},
Packit Service a1bd4f
    {N_("Automatic"), NM_SETTING_IP4_CONFIG_METHOD_AUTO},
Packit Service a1bd4f
    {N_("Link-Local"), NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL},
Packit Service a1bd4f
    {N_("Manual"), NM_SETTING_IP4_CONFIG_METHOD_MANUAL},
Packit Service a1bd4f
    {N_("Shared"), NM_SETTING_IP4_CONFIG_METHOD_SHARED},
Packit Service a1bd4f
    {NULL, NULL}};
Packit 5756e2
Packit 5756e2
NmtEditorPage *
Packit Service a1bd4f
nmt_page_ip4_new(NMConnection *conn)
Packit 5756e2
{
Packit Service a1bd4f
    return g_object_new(NMT_TYPE_PAGE_IP4, "connection", conn, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_page_ip4_init(NmtPageIP4 *ip4)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
edit_routes(NmtNewtButton *button, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    NMSetting *  s_ip4 = user_data;
Packit Service a1bd4f
    NmtNewtForm *form;
Packit 5756e2
Packit Service a1bd4f
    form = nmt_route_editor_new(s_ip4);
Packit Service a1bd4f
    nmt_newt_form_run_sync(form);
Packit Service a1bd4f
    g_object_unref(form);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
ip4_routes_transform_to_description(GBinding *    binding,
Packit Service a1bd4f
                                    const GValue *source_value,
Packit Service a1bd4f
                                    GValue *      target_value,
Packit Service a1bd4f
                                    gpointer      user_data)
Packit 5756e2
{
Packit Service a1bd4f
    GPtrArray *routes;
Packit Service a1bd4f
    char *     text;
Packit Service a1bd4f
Packit Service a1bd4f
    routes = g_value_get_boxed(source_value);
Packit Service a1bd4f
    if (!routes || !routes->len)
Packit Service a1bd4f
        text = g_strdup(_("(No custom routes)"));
Packit Service a1bd4f
    else {
Packit Service a1bd4f
        text = g_strdup_printf(
Packit Service a1bd4f
            g_dngettext(GETTEXT_PACKAGE, "One custom route", "%d custom routes", routes->len),
Packit Service a1bd4f
            routes->len);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    g_value_take_string(target_value, text);
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_page_ip4_constructed(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtPageIP4 *       ip4 = NMT_PAGE_IP4(object);
Packit Service a1bd4f
    gboolean           show_by_default;
Packit Service a1bd4f
    NmtEditorSection * section;
Packit Service a1bd4f
    NmtEditorGrid *    grid;
Packit Service a1bd4f
    NMSettingIPConfig *s_ip4;
Packit Service a1bd4f
    NmtNewtWidget *    widget, *button;
Packit Service a1bd4f
    NMConnection *     conn;
Packit Service a1bd4f
Packit Service a1bd4f
    conn  = nmt_editor_page_get_connection(NMT_EDITOR_PAGE(ip4));
Packit Service a1bd4f
    s_ip4 = nm_connection_get_setting_ip4_config(conn);
Packit Service a1bd4f
    if (!s_ip4) {
Packit Service a1bd4f
        s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new();
Packit Service a1bd4f
        g_object_set(G_OBJECT(s_ip4),
Packit Service a1bd4f
                     NM_SETTING_IP_CONFIG_METHOD,
Packit Service a1bd4f
                     NM_SETTING_IP4_CONFIG_METHOD_AUTO,
Packit Service a1bd4f
                     NULL);
Packit Service a1bd4f
        nm_connection_add_setting(conn, (NMSetting *) s_ip4);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_popup_new(ip4methods);
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_METHOD,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "active-id",
Packit Service a1bd4f
                           G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
Packit Service a1bd4f
    if (!g_strcmp0(nm_setting_ip_config_get_method(s_ip4), NM_SETTING_IP4_CONFIG_METHOD_MANUAL))
Packit Service a1bd4f
        show_by_default = TRUE;
Packit Service a1bd4f
    else if (nm_setting_ip_config_get_num_addresses(s_ip4))
Packit Service a1bd4f
        show_by_default = TRUE;
Packit Service a1bd4f
    else
Packit Service a1bd4f
        show_by_default = FALSE;
Packit Service a1bd4f
Packit Service a1bd4f
    section = nmt_editor_section_new(_("IPv4 CONFIGURATION"), widget, show_by_default);
Packit Service a1bd4f
    grid    = nmt_editor_section_get_body(section);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_address_list_new(NMT_ADDRESS_LIST_IP4_WITH_PREFIX);
Packit Service a1bd4f
    nm_editor_bind_ip_addresses_with_prefix_to_strv(AF_INET,
Packit Service a1bd4f
                                                    s_ip4,
Packit Service a1bd4f
                                                    NM_SETTING_IP_CONFIG_ADDRESSES,
Packit Service a1bd4f
                                                    widget,
Packit Service a1bd4f
                                                    "strings",
Packit Service a1bd4f
                                                    G_BINDING_BIDIRECTIONAL
Packit Service a1bd4f
                                                        | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, _("Addresses"), widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_ip_entry_new(25, AF_INET, FALSE, TRUE);
Packit Service a1bd4f
    nm_editor_bind_ip_gateway_to_string(AF_INET,
Packit Service a1bd4f
                                        s_ip4,
Packit Service a1bd4f
                                        widget,
Packit Service a1bd4f
                                        "text",
Packit Service a1bd4f
                                        "sensitive",
Packit Service a1bd4f
                                        G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, _("Gateway"), widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_address_list_new(NMT_ADDRESS_LIST_IP4);
Packit Service a1bd4f
    nm_editor_bind_ip_addresses_to_strv(AF_INET,
Packit Service a1bd4f
                                        s_ip4,
Packit Service a1bd4f
                                        NM_SETTING_IP_CONFIG_DNS,
Packit Service a1bd4f
                                        widget,
Packit Service a1bd4f
                                        "strings",
Packit Service a1bd4f
                                        G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, _("DNS servers"), widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_address_list_new(NMT_ADDRESS_LIST_HOSTNAME);
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_DNS_SEARCH,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "strings",
Packit Service a1bd4f
                           G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, _("Search domains"), widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, nmt_newt_separator_new(), NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = g_object_new(NMT_TYPE_NEWT_LABEL, "text", "", "style", NMT_NEWT_LABEL_PLAIN, NULL);
Packit Service a1bd4f
    g_object_bind_property_full(s_ip4,
Packit Service a1bd4f
                                NM_SETTING_IP_CONFIG_ROUTES,
Packit Service a1bd4f
                                widget,
Packit Service a1bd4f
                                "text",
Packit Service a1bd4f
                                G_BINDING_SYNC_CREATE,
Packit Service a1bd4f
                                ip4_routes_transform_to_description,
Packit Service a1bd4f
                                NULL,
Packit Service a1bd4f
                                NULL,
Packit Service a1bd4f
                                NULL);
Packit Service a1bd4f
    button = nmt_newt_button_new(_("Edit..."));
Packit Service a1bd4f
    g_signal_connect(button, "clicked", G_CALLBACK(edit_routes), s_ip4);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, _("Routing"), widget, button);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_checkbox_new(_("Never use this network for default route"));
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_NEVER_DEFAULT,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "active",
Packit Service a1bd4f
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_checkbox_new(_("Ignore automatically obtained routes"));
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_IGNORE_AUTO_ROUTES,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "active",
Packit Service a1bd4f
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_checkbox_new(_("Ignore automatically obtained DNS parameters"));
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_IGNORE_AUTO_DNS,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "active",
Packit Service a1bd4f
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, nmt_newt_separator_new(), NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    widget = nmt_newt_checkbox_new(_("Require IPv4 addressing for this connection"));
Packit Service a1bd4f
    g_object_bind_property(s_ip4,
Packit Service a1bd4f
                           NM_SETTING_IP_CONFIG_MAY_FAIL,
Packit Service a1bd4f
                           widget,
Packit Service a1bd4f
                           "active",
Packit Service a1bd4f
                           G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL
Packit Service a1bd4f
                               | G_BINDING_INVERT_BOOLEAN);
Packit Service a1bd4f
    nmt_editor_grid_append(grid, NULL, widget, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_editor_page_add_section(NMT_EDITOR_PAGE(ip4), section);
Packit Service a1bd4f
Packit Service a1bd4f
    G_OBJECT_CLASS(nmt_page_ip4_parent_class)->constructed(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_page_ip4_class_init(NmtPageIP4Class *ip4_class)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *object_class = G_OBJECT_CLASS(ip4_class);
Packit 5756e2
Packit Service a1bd4f
    object_class->constructed = nmt_page_ip4_constructed;
Packit 5756e2
}