Blame clients/tui/nmt-address-list.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:nmt-address-list
Packit 5756e2
 * @short_description: An editable list of IP addresses or hostnames
Packit 5756e2
 *
Packit 5756e2
 * #NmtAddressList is a subclass of #NmtWidgetList that contains
Packit 5756e2
 * entries displaying IP addresses, address/prefix strings, or
Packit 5756e2
 * hostnames. This is designed for binding its #NmtAddressList:strings
Packit 5756e2
 * property to an appropriate #NMSettingIP4Config or
Packit 5756e2
 * #NMSettingIP6Config property via one of the nm-editor-bindings
Packit 5756e2
 * functions.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#include "nm-default.h"
Packit 5756e2
Packit 5756e2
#include "nmt-address-list.h"
Packit 5756e2
Packit 5756e2
#include <arpa/inet.h>
Packit 5756e2
#include <netinet/in.h>
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
Packit 5756e2
#include "nmt-ip-entry.h"
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NmtAddressList, nmt_address_list, NMT_TYPE_WIDGET_LIST)
Packit 5756e2
Packit Service a1bd4f
#define NMT_ADDRESS_LIST_GET_PRIVATE(o) \
Packit Service a1bd4f
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_ADDRESS_LIST, NmtAddressListPrivate))
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    NmtAddressListType list_type;
Packit Service a1bd4f
    char **            strings;
Packit 5756e2
} NmtAddressListPrivate;
Packit 5756e2
Packit 5756e2
enum {
Packit Service a1bd4f
    PROP_0,
Packit Service a1bd4f
    PROP_LIST_TYPE,
Packit Service a1bd4f
    PROP_STRINGS,
Packit 5756e2
Packit Service a1bd4f
    LAST_PROP
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * NmtAddressListType:
Packit 5756e2
 * @NMT_ADDRESS_LIST_IP4_WITH_PREFIX: IPv4 address/prefix strings
Packit 5756e2
 * @NMT_ADDRESS_LIST_IP4: IPv4 addresses
Packit 5756e2
 * @NMT_ADDRESS_LIST_IP6_WITH_PREFIX: IPv6 address/prefix strings
Packit 5756e2
 * @NMT_ADDRESS_LIST_IP6: IPv6 addresses
Packit 5756e2
 * @NMT_ADDRESS_LIST_HOSTNAME: hostnames
Packit 5756e2
 *
Packit 5756e2
 * The type of address in an #NmtAddressList
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_address_list_new:
Packit 5756e2
 * @list_type: the type of address the list will contain
Packit 5756e2
 *
Packit 5756e2
 * Creates a new #NmtAddressList
Packit 5756e2
 *
Packit 5756e2
 * Returns: a new #NmtAddressList
Packit 5756e2
 */
Packit 5756e2
NmtNewtWidget *
Packit Service a1bd4f
nmt_address_list_new(NmtAddressListType list_type)
Packit 5756e2
{
Packit Service a1bd4f
    return g_object_new(NMT_TYPE_ADDRESS_LIST, "list-type", list_type, NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_init(NmtAddressList *list)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
strings_transform_to_entry(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
    int    n = GPOINTER_TO_INT(user_data);
Packit Service a1bd4f
    char **strings;
Packit 5756e2
Packit Service a1bd4f
    strings = g_value_get_boxed(source_value);
Packit Service a1bd4f
    if (n >= g_strv_length(strings))
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    g_value_set_string(target_value, strings[n]);
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
strings_transform_from_entry(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
    NmtAddressList *       list = NMT_ADDRESS_LIST(g_binding_get_source(binding));
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list);
Packit Service a1bd4f
    int                    n    = GPOINTER_TO_INT(user_data);
Packit 5756e2
Packit Service a1bd4f
    if (n >= g_strv_length(priv->strings))
Packit Service a1bd4f
        return FALSE;
Packit 5756e2
Packit Service a1bd4f
    g_free(priv->strings[n]);
Packit Service a1bd4f
    priv->strings[n] = g_value_dup_string(source_value);
Packit 5756e2
Packit Service a1bd4f
    g_value_set_boxed(target_value, priv->strings);
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
hostname_filter(NmtNewtEntry *entry, const char *text, int ch, int position, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    return g_ascii_isalnum(ch) || ch == '.' || ch == '-';
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static NmtNewtWidget *
Packit Service a1bd4f
nmt_address_list_create_widget(NmtWidgetList *list, int num)
Packit 5756e2
{
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list);
Packit Service a1bd4f
    NmtNewtWidget *        entry;
Packit Service a1bd4f
Packit Service a1bd4f
    if (priv->list_type == NMT_ADDRESS_LIST_IP4_WITH_PREFIX)
Packit Service a1bd4f
        entry = nmt_ip_entry_new(25, AF_INET, TRUE, FALSE);
Packit Service a1bd4f
    else if (priv->list_type == NMT_ADDRESS_LIST_IP4)
Packit Service a1bd4f
        entry = nmt_ip_entry_new(25, AF_INET, FALSE, FALSE);
Packit Service a1bd4f
    else if (priv->list_type == NMT_ADDRESS_LIST_IP6_WITH_PREFIX)
Packit Service a1bd4f
        entry = nmt_ip_entry_new(25, AF_INET6, TRUE, FALSE);
Packit Service a1bd4f
    else if (priv->list_type == NMT_ADDRESS_LIST_IP6)
Packit Service a1bd4f
        entry = nmt_ip_entry_new(25, AF_INET6, FALSE, FALSE);
Packit Service a1bd4f
    else if (priv->list_type == NMT_ADDRESS_LIST_HOSTNAME) {
Packit Service a1bd4f
        entry = nmt_newt_entry_new(25, NMT_NEWT_ENTRY_NONEMPTY);
Packit Service a1bd4f
        nmt_newt_entry_set_filter(NMT_NEWT_ENTRY(entry), hostname_filter, list);
Packit Service a1bd4f
    } else
Packit Service a1bd4f
        g_assert_not_reached();
Packit Service a1bd4f
Packit Service a1bd4f
    g_object_bind_property_full(list,
Packit Service a1bd4f
                                "strings",
Packit Service a1bd4f
                                entry,
Packit Service a1bd4f
                                "text",
Packit Service a1bd4f
                                G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE,
Packit Service a1bd4f
                                strings_transform_to_entry,
Packit Service a1bd4f
                                strings_transform_from_entry,
Packit Service a1bd4f
                                GINT_TO_POINTER(num),
Packit Service a1bd4f
                                NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    return entry;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_add_clicked(NmtWidgetList *list)
Packit 5756e2
{
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list);
Packit Service a1bd4f
    int                    len;
Packit 5756e2
Packit Service a1bd4f
    len                    = priv->strings ? g_strv_length(priv->strings) : 0;
Packit Service a1bd4f
    priv->strings          = g_renew(char *, priv->strings, len + 2);
Packit Service a1bd4f
    priv->strings[len]     = g_strdup("");
Packit Service a1bd4f
    priv->strings[len + 1] = NULL;
Packit 5756e2
Packit Service a1bd4f
    nmt_widget_list_set_length(list, len + 1);
Packit Service a1bd4f
    g_object_notify(G_OBJECT(list), "strings");
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_remove_clicked(NmtWidgetList *list, int num)
Packit 5756e2
{
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list);
Packit Service a1bd4f
    int                    len;
Packit 5756e2
Packit Service a1bd4f
    len = g_strv_length(priv->strings);
Packit Service a1bd4f
    g_free(priv->strings[num]);
Packit Service a1bd4f
    memmove(priv->strings + num, priv->strings + num + 1, (len - num) * sizeof(char *));
Packit 5756e2
Packit Service a1bd4f
    nmt_widget_list_set_length(list, len - 1);
Packit Service a1bd4f
    g_object_notify(G_OBJECT(list), "strings");
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_set_property(GObject *     object,
Packit Service a1bd4f
                              guint         prop_id,
Packit Service a1bd4f
                              const GValue *value,
Packit Service a1bd4f
                              GParamSpec *  pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_LIST_TYPE:
Packit Service a1bd4f
        priv->list_type = g_value_get_uint(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_STRINGS:
Packit Service a1bd4f
        g_strfreev(priv->strings);
Packit Service a1bd4f
        priv->strings = g_value_dup_boxed(value);
Packit Service a1bd4f
        if (!priv->strings)
Packit Service a1bd4f
            priv->strings = g_new0(char *, 1);
Packit Service a1bd4f
        nmt_widget_list_set_length(NMT_WIDGET_LIST(object), g_strv_length(priv->strings));
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_LIST_TYPE:
Packit Service a1bd4f
        g_value_set_uint(value, priv->list_type);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_STRINGS:
Packit Service a1bd4f
        g_value_set_boxed(value, priv->strings);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_address_list_class_init(NmtAddressListClass *list_class)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *      object_class      = G_OBJECT_CLASS(list_class);
Packit Service a1bd4f
    NmtWidgetListClass *widget_list_class = NMT_WIDGET_LIST_CLASS(list_class);
Packit Service a1bd4f
Packit Service a1bd4f
    g_type_class_add_private(list_class, sizeof(NmtAddressListPrivate));
Packit Service a1bd4f
Packit Service a1bd4f
    /* virtual methods */
Packit Service a1bd4f
    object_class->set_property = nmt_address_list_set_property;
Packit Service a1bd4f
    object_class->get_property = nmt_address_list_get_property;
Packit Service a1bd4f
Packit Service a1bd4f
    widget_list_class->create_widget  = nmt_address_list_create_widget;
Packit Service a1bd4f
    widget_list_class->add_clicked    = nmt_address_list_add_clicked;
Packit Service a1bd4f
    widget_list_class->remove_clicked = nmt_address_list_remove_clicked;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtAddressList:list-type:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The type of address the list holds.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_LIST_TYPE,
Packit Service a1bd4f
        g_param_spec_uint("list-type",
Packit Service a1bd4f
                          "",
Packit Service a1bd4f
                          "",
Packit Service a1bd4f
                          0,
Packit Service a1bd4f
                          G_MAXUINT,
Packit Service a1bd4f
                          0,
Packit Service a1bd4f
                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtAddressList:strings:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The strings in the list's entries.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(object_class,
Packit Service a1bd4f
                                    PROP_STRINGS,
Packit Service a1bd4f
                                    g_param_spec_boxed("strings",
Packit Service a1bd4f
                                                       "",
Packit Service a1bd4f
                                                       "",
Packit Service a1bd4f
                                                       G_TYPE_STRV,
Packit Service a1bd4f
                                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 5756e2
}