Blame clients/tui/nmt-route-entry.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-route-entry
Packit 5756e2
 * @short_description: A set of widgets describing a single route.
Packit 5756e2
 *
Packit 5756e2
 * #NmtRouteEntry provides a set of three entry widgets, for entering
Packit 5756e2
 * a network/prefix, a next hop, and a metric.
Packit 5756e2
 *
Packit 5756e2
 * This is used as a building block by #NmtRouteTable.
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-client.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-route-entry.h"
Packit 5756e2
#include "nmt-ip-entry.h"
Packit 5756e2
Packit 5756e2
#include "nm-editor-bindings.h"
Packit 5756e2
Packit Service a1bd4f
G_DEFINE_TYPE(NmtRouteEntry, nmt_route_entry, NMT_TYPE_NEWT_GRID)
Packit 5756e2
Packit Service a1bd4f
#define NMT_ROUTE_ENTRY_GET_PRIVATE(o) \
Packit Service a1bd4f
    (G_TYPE_INSTANCE_GET_PRIVATE((o), NMT_TYPE_ROUTE_ENTRY, NmtRouteEntryPrivate))
Packit 5756e2
Packit 5756e2
typedef struct {
Packit Service a1bd4f
    NmtNewtWidget *dest, *next_hop, *metric;
Packit 5756e2
Packit Service a1bd4f
    int        family;
Packit Service a1bd4f
    int        ip_entry_width, metric_entry_width;
Packit Service a1bd4f
    NMIPRoute *route;
Packit 5756e2
} NmtRouteEntryPrivate;
Packit 5756e2
Packit 5756e2
enum {
Packit Service a1bd4f
    PROP_0,
Packit Service a1bd4f
    PROP_FAMILY,
Packit Service a1bd4f
    PROP_IP_ENTRY_WIDTH,
Packit Service a1bd4f
    PROP_METRIC_ENTRY_WIDTH,
Packit Service a1bd4f
    PROP_ROUTE,
Packit 5756e2
Packit Service a1bd4f
    LAST_PROP
Packit 5756e2
};
Packit 5756e2
Packit 5756e2
/**
Packit 5756e2
 * nmt_route_entry_new:
Packit 5756e2
 * @family: the address family, eg %AF_INET
Packit 5756e2
 * @ip_entry_width: the width in characters for the IP address entries
Packit 5756e2
 * @metric_entry_width: the width in characters for the metric entry
Packit 5756e2
 *
Packit 5756e2
 * Creates a new #NmtRouteEntry
Packit 5756e2
 *
Packit 5756e2
 * Returns: a new #NmtRouteEntry
Packit 5756e2
 */
Packit 5756e2
NmtNewtWidget *
Packit Service a1bd4f
nmt_route_entry_new(int family, int ip_entry_width, int metric_entry_width)
Packit 5756e2
{
Packit Service a1bd4f
    return g_object_new(NMT_TYPE_ROUTE_ENTRY,
Packit Service a1bd4f
                        "family",
Packit Service a1bd4f
                        family,
Packit Service a1bd4f
                        "ip-entry-width",
Packit Service a1bd4f
                        ip_entry_width,
Packit Service a1bd4f
                        "metric-entry-width",
Packit Service a1bd4f
                        metric_entry_width,
Packit Service a1bd4f
                        NULL);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_route_entry_init(NmtRouteEntry *entry)
Packit Service a1bd4f
{}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
entry_validity_transform_to_warning_label(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
    if (g_value_get_boolean(source_value))
Packit Service a1bd4f
        g_value_set_string(target_value, " ");
Packit Service a1bd4f
    else
Packit Service a1bd4f
        g_value_set_string(target_value, "!");
Packit Service a1bd4f
    return TRUE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static NmtNewtWidget *
Packit Service a1bd4f
create_warning_label(NmtNewtWidget *entry)
Packit 5756e2
{
Packit Service a1bd4f
    NmtNewtWidget *label;
Packit Service a1bd4f
Packit Service a1bd4f
    label = g_object_new(NMT_TYPE_NEWT_LABEL, "highlight", TRUE, NULL);
Packit Service a1bd4f
    g_object_bind_property_full(entry,
Packit Service a1bd4f
                                "valid",
Packit Service a1bd4f
                                label,
Packit Service a1bd4f
                                "text",
Packit Service a1bd4f
                                G_BINDING_SYNC_CREATE,
Packit Service a1bd4f
                                entry_validity_transform_to_warning_label,
Packit Service a1bd4f
                                NULL,
Packit Service a1bd4f
                                NULL,
Packit Service a1bd4f
                                NULL);
Packit Service a1bd4f
    return label;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_route_entry_constructed(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE(object);
Packit Service a1bd4f
    NmtNewtGrid *         grid = NMT_NEWT_GRID(object);
Packit Service a1bd4f
    NmtNewtWidget *       warning_label;
Packit Service a1bd4f
Packit Service a1bd4f
    priv->dest     = nmt_ip_entry_new(priv->ip_entry_width, priv->family, TRUE, FALSE);
Packit Service a1bd4f
    priv->next_hop = nmt_ip_entry_new(priv->ip_entry_width, priv->family, FALSE, TRUE);
Packit Service a1bd4f
    priv->metric = nmt_newt_entry_numeric_new_full(priv->metric_entry_width, 0, G_MAXUINT32, TRUE);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_grid_add(grid, priv->dest, 0, 0);
Packit Service a1bd4f
    warning_label = create_warning_label(priv->dest);
Packit Service a1bd4f
    nmt_newt_grid_add(grid, warning_label, 1, 0);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_grid_add(grid, priv->next_hop, 2, 0);
Packit Service a1bd4f
    nmt_newt_widget_set_padding(priv->next_hop, 1, 0, 0, 0);
Packit Service a1bd4f
    warning_label = create_warning_label(priv->next_hop);
Packit Service a1bd4f
    nmt_newt_grid_add(grid, warning_label, 3, 0);
Packit Service a1bd4f
Packit Service a1bd4f
    nmt_newt_grid_add(grid, priv->metric, 4, 0);
Packit Service a1bd4f
    nmt_newt_widget_set_padding(priv->metric, 1, 0, 0, 0);
Packit Service a1bd4f
Packit Service a1bd4f
    nm_editor_bind_ip_route_to_strings(priv->family,
Packit Service a1bd4f
                                       object,
Packit Service a1bd4f
                                       "route",
Packit Service a1bd4f
                                       priv->dest,
Packit Service a1bd4f
                                       "text",
Packit Service a1bd4f
                                       priv->next_hop,
Packit Service a1bd4f
                                       "text",
Packit Service a1bd4f
                                       priv->metric,
Packit Service a1bd4f
                                       "text",
Packit Service a1bd4f
                                       G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
Packit Service a1bd4f
Packit Service a1bd4f
    G_OBJECT_CLASS(nmt_route_entry_parent_class)->constructed(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static newtComponent
Packit Service a1bd4f
nmt_route_entry_get_focus_component(NmtNewtWidget *widget)
Packit 5756e2
{
Packit Service a1bd4f
    NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE(widget);
Packit 5756e2
Packit Service a1bd4f
    return nmt_newt_widget_get_focus_component(priv->dest);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_route_entry_finalize(GObject *object)
Packit 5756e2
{
Packit Service a1bd4f
    NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE(object);
Packit 5756e2
Packit Service a1bd4f
    nm_clear_pointer(&priv->route, nm_ip_route_unref);
Packit 5756e2
Packit Service a1bd4f
    G_OBJECT_CLASS(nmt_route_entry_parent_class)->finalize(object);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
nmt_route_entry_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_FAMILY:
Packit Service a1bd4f
        priv->family = g_value_get_int(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_IP_ENTRY_WIDTH:
Packit Service a1bd4f
        priv->ip_entry_width = g_value_get_int(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_METRIC_ENTRY_WIDTH:
Packit Service a1bd4f
        priv->metric_entry_width = g_value_get_int(value);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_ROUTE:
Packit Service a1bd4f
        if (priv->route)
Packit Service a1bd4f
            nm_ip_route_unref(priv->route);
Packit Service a1bd4f
        priv->route = g_value_dup_boxed(value);
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_route_entry_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Packit 5756e2
{
Packit Service a1bd4f
    NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE(object);
Packit Service a1bd4f
Packit Service a1bd4f
    switch (prop_id) {
Packit Service a1bd4f
    case PROP_FAMILY:
Packit Service a1bd4f
        g_value_set_int(value, priv->family);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_IP_ENTRY_WIDTH:
Packit Service a1bd4f
        g_value_set_int(value, priv->ip_entry_width);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_METRIC_ENTRY_WIDTH:
Packit Service a1bd4f
        g_value_set_int(value, priv->metric_entry_width);
Packit Service a1bd4f
        break;
Packit Service a1bd4f
    case PROP_ROUTE:
Packit Service a1bd4f
        g_value_set_boxed(value, priv->route);
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_route_entry_class_init(NmtRouteEntryClass *entry_class)
Packit 5756e2
{
Packit Service a1bd4f
    GObjectClass *      object_class = G_OBJECT_CLASS(entry_class);
Packit Service a1bd4f
    NmtNewtWidgetClass *widget_class = NMT_NEWT_WIDGET_CLASS(entry_class);
Packit Service a1bd4f
Packit Service a1bd4f
    g_type_class_add_private(entry_class, sizeof(NmtRouteEntryPrivate));
Packit Service a1bd4f
Packit Service a1bd4f
    /* virtual methods */
Packit Service a1bd4f
    object_class->constructed  = nmt_route_entry_constructed;
Packit Service a1bd4f
    object_class->set_property = nmt_route_entry_set_property;
Packit Service a1bd4f
    object_class->get_property = nmt_route_entry_get_property;
Packit Service a1bd4f
    object_class->finalize     = nmt_route_entry_finalize;
Packit Service a1bd4f
Packit Service a1bd4f
    widget_class->get_focus_component = nmt_route_entry_get_focus_component;
Packit Service a1bd4f
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtRouteEntry:family:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The address family of the route, eg, %AF_INET
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_FAMILY,
Packit Service a1bd4f
        g_param_spec_int("family",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_MAXINT,
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtRouteEntry:ip-entry-width:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The width in characters of the IP address entries
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_IP_ENTRY_WIDTH,
Packit Service a1bd4f
        g_param_spec_int("ip-entry-width",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_MAXINT,
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtRouteEntry:metric-entry-width:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The width in characters of the metric entry
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(
Packit Service a1bd4f
        object_class,
Packit Service a1bd4f
        PROP_METRIC_ENTRY_WIDTH,
Packit Service a1bd4f
        g_param_spec_int("metric-entry-width",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         "",
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_MAXINT,
Packit Service a1bd4f
                         0,
Packit Service a1bd4f
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
Packit Service a1bd4f
    /**
Packit Service a1bd4f
     * NmtRouteEntry:route:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * The contents of the entries, as an #NMIPRoute.
Packit Service a1bd4f
     */
Packit Service a1bd4f
    g_object_class_install_property(object_class,
Packit Service a1bd4f
                                    PROP_ROUTE,
Packit Service a1bd4f
                                    g_param_spec_boxed("route",
Packit Service a1bd4f
                                                       "",
Packit Service a1bd4f
                                                       "",
Packit Service a1bd4f
                                                       nm_ip_route_get_type(),
Packit Service a1bd4f
                                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 5756e2
}