summaryrefslogtreecommitdiff
path: root/tui/nmt-route-entry.c
blob: db8c254d74ceee3d81811f41c7a99e07b0c0e773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2013 Red Hat, Inc.
 */

/**
 * SECTION:nmt-route-entry
 * @short_description: A set of widgets describing a single route.
 *
 * #NmtRouteEntry provides a set of three entry widgets, for entering
 * a network/prefix, a next hop, and a metric.
 *
 * This is used as a building block by #NmtRouteTable.
 */

#include "config.h"

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>

#include <glib/gi18n-lib.h>
#include <nm-setting-ip4-config.h>
#include <nm-setting-ip6-config.h>

#include "nmt-route-entry.h"
#include "nmt-ip-entry.h"

#include "nm-editor-bindings.h"

G_DEFINE_TYPE (NmtRouteEntry, nmt_route_entry, NMT_TYPE_NEWT_GRID)

#define NMT_ROUTE_ENTRY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMT_TYPE_ROUTE_ENTRY, NmtRouteEntryPrivate))

typedef struct {
	NmtNewtWidget *dest, *next_hop, *metric;

	int family;
	int ip_entry_width, metric_entry_width;
	NMIP4Route *ip4_route;
	NMIP6Route *ip6_route;
} NmtRouteEntryPrivate;

enum {
	PROP_0,
	PROP_FAMILY,
	PROP_IP_ENTRY_WIDTH,
	PROP_METRIC_ENTRY_WIDTH,
	PROP_IP4_ROUTE,
	PROP_IP6_ROUTE,

	LAST_PROP
};

/**
 * nmt_route_entry_new:
 * @family: the address family, eg %AF_INET
 * @ip_entry_width: the width in characters for the IP address entries
 * @metric_entry_width: the width in characters for the metric entry
 *
 * Creates a new #NmtRouteEntry
 *
 * Returns: a new #NmtRouteEntry
 */
NmtNewtWidget *
nmt_route_entry_new (int family,
                     int ip_entry_width,
                     int metric_entry_width)
{
	return g_object_new (NMT_TYPE_ROUTE_ENTRY,
	                     "family", family,
	                     "ip-entry-width", ip_entry_width,
	                     "metric-entry-width", metric_entry_width,
	                     NULL);
}

static void
nmt_route_entry_init (NmtRouteEntry *entry)
{
}

static gboolean
entry_validity_transform_to_warning_label (GBinding     *binding,
                                           const GValue *source_value,
                                           GValue       *target_value,
                                           gpointer      user_data)
{
	if (g_value_get_boolean (source_value))
		g_value_set_string (target_value, " ");
	else
		g_value_set_string (target_value, "!");
	return TRUE;
}

static NmtNewtWidget *
create_warning_label (NmtNewtWidget *entry)
{
	NmtNewtWidget *label;

	label = g_object_new (NMT_TYPE_NEWT_LABEL,
	                      "highlight", TRUE,
	                      NULL);
	g_object_bind_property_full (entry, "valid",
	                             label, "text",
	                             G_BINDING_SYNC_CREATE,
	                             entry_validity_transform_to_warning_label,
	                             NULL, NULL, NULL);
	return label;
}

static void
nmt_route_entry_constructed (GObject *object)
{
	NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object);
	NmtNewtGrid *grid = NMT_NEWT_GRID (object);
	NmtNewtWidget *warning_label;

	priv->dest = nmt_ip_entry_new (priv->ip_entry_width, priv->family, TRUE, FALSE);
	priv->next_hop = nmt_ip_entry_new (priv->ip_entry_width, priv->family, FALSE, TRUE);
	priv->metric = nmt_newt_entry_numeric_new (priv->metric_entry_width, 0, 65535);

	nmt_newt_grid_add (grid, priv->dest, 0, 0);
	warning_label = create_warning_label (priv->dest);
	nmt_newt_grid_add (grid, warning_label, 1, 0);

	nmt_newt_grid_add (grid, priv->next_hop, 2, 0);
	nmt_newt_widget_set_padding (priv->next_hop, 1, 0, 0, 0);
	warning_label = create_warning_label (priv->next_hop);
	nmt_newt_grid_add (grid, warning_label, 3, 0);

	nmt_newt_grid_add (grid, priv->metric, 4, 0);
	nmt_newt_widget_set_padding (priv->metric, 1, 0, 0, 0);

	if (priv->family == AF_INET) {
		nm_editor_bind_ip4_route_to_strings (object, "ip4-route",
		                                     priv->dest, "text",
		                                     priv->next_hop, "text",
		                                     priv->metric, "text",
		                                     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
	} else if (priv->family == AF_INET6) {
		nm_editor_bind_ip6_route_to_strings (object, "ip6-route",
		                                     priv->dest, "text",
		                                     priv->next_hop, "text",
		                                     priv->metric, "text",
		                                     G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
	} else
		g_assert_not_reached ();

	G_OBJECT_CLASS (nmt_route_entry_parent_class)->constructed (object);
}

static newtComponent
nmt_route_entry_get_focus_component (NmtNewtWidget *widget)
{
	NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (widget);
	
	return nmt_newt_widget_get_focus_component (priv->dest);
}

static void
nmt_route_entry_finalize (GObject *object)
{
	NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object);

	g_clear_pointer (&priv->ip4_route, nm_ip4_route_unref);
	g_clear_pointer (&priv->ip6_route, nm_ip6_route_unref);

	G_OBJECT_CLASS (nmt_route_entry_parent_class)->finalize (object);
}

static void
nmt_route_entry_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
	NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_FAMILY:
		priv->family = g_value_get_int (value);
		break;
	case PROP_IP_ENTRY_WIDTH:
		priv->ip_entry_width = g_value_get_int (value);
		break;
	case PROP_METRIC_ENTRY_WIDTH:
		priv->metric_entry_width = g_value_get_int (value);
		break;
	case PROP_IP4_ROUTE:
		g_return_if_fail (priv->family == AF_INET);
		if (priv->ip4_route)
			nm_ip4_route_unref (priv->ip4_route);
		priv->ip4_route = g_value_dup_boxed (value);
		break;
	case PROP_IP6_ROUTE:
		g_return_if_fail (priv->family == AF_INET);
		if (priv->ip6_route)
			nm_ip6_route_unref (priv->ip6_route);
		priv->ip6_route = g_value_dup_boxed (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_route_entry_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
	NmtRouteEntryPrivate *priv = NMT_ROUTE_ENTRY_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_FAMILY:
		g_value_set_int (value, priv->family);
		break;
	case PROP_IP_ENTRY_WIDTH:
		g_value_set_int (value, priv->ip_entry_width);
		break;
	case PROP_METRIC_ENTRY_WIDTH:
		g_value_set_int (value, priv->metric_entry_width);
		break;
	case PROP_IP4_ROUTE:
		g_return_if_fail (priv->family == AF_INET);
		g_value_set_boxed (value, priv->ip4_route);
		break;
	case PROP_IP6_ROUTE:
		g_return_if_fail (priv->family == AF_INET6);
		g_value_set_boxed (value, priv->ip6_route);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_route_entry_class_init (NmtRouteEntryClass *entry_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (entry_class);
	NmtNewtWidgetClass *widget_class = NMT_NEWT_WIDGET_CLASS (entry_class);

	g_type_class_add_private (entry_class, sizeof (NmtRouteEntryPrivate));

	/* virtual methods */
	object_class->constructed  = nmt_route_entry_constructed;
	object_class->set_property = nmt_route_entry_set_property;
	object_class->get_property = nmt_route_entry_get_property;
	object_class->finalize     = nmt_route_entry_finalize;

	widget_class->get_focus_component = nmt_route_entry_get_focus_component;

	/**
	 * NmtRouteEntry:family:
	 *
	 * The address family of the route, eg, %AF_INET
	 */
	g_object_class_install_property (object_class, PROP_FAMILY,
	                                 g_param_spec_int ("family", "", "",
	                                                   0, G_MAXINT, 0,
	                                                   G_PARAM_READWRITE |
	                                                   G_PARAM_CONSTRUCT_ONLY |
	                                                   G_PARAM_STATIC_STRINGS));
	/**
	 * NmtRouteEntry:ip-entry-width:
	 *
	 * The width in characters of the IP address entries
	 */
	g_object_class_install_property (object_class, PROP_IP_ENTRY_WIDTH,
	                                 g_param_spec_int ("ip-entry-width", "", "",
	                                                   0, G_MAXINT, 0,
	                                                   G_PARAM_READWRITE |
	                                                   G_PARAM_CONSTRUCT_ONLY |
	                                                   G_PARAM_STATIC_STRINGS));
	/**
	 * NmtRouteEntry:metric-entry-width:
	 *
	 * The width in characters of the metric entry
	 */
	g_object_class_install_property (object_class, PROP_METRIC_ENTRY_WIDTH,
	                                 g_param_spec_int ("metric-entry-width", "", "",
	                                                   0, G_MAXINT, 0,
	                                                   G_PARAM_READWRITE |
	                                                   G_PARAM_CONSTRUCT_ONLY |
	                                                   G_PARAM_STATIC_STRINGS));
	/**
	 * NmtRouteEntry:ip4-route:
	 *
	 * The contents of the entries, as an #NMIP4Route. Only valid
	 * if #NmtRouteEntry:family is %AF_INET.
	 */
	g_object_class_install_property (object_class, PROP_IP4_ROUTE,
	                                 g_param_spec_boxed ("ip4-route", "", "",
	                                                     nm_ip4_route_get_type (),
	                                                     G_PARAM_READWRITE |
	                                                     G_PARAM_STATIC_STRINGS));
	/**
	 * NmtRouteEntry:ip6-route:
	 *
	 * The contents of the entries, as an #NMIP6Route. Only valid
	 * if #NmtRouteEntry:family is %AF_INET6.
	 */
	g_object_class_install_property (object_class, PROP_IP6_ROUTE,
	                                 g_param_spec_boxed ("ip6-route", "", "",
	                                                     nm_ip6_route_get_type (),
	                                                     G_PARAM_READWRITE |
	                                                     G_PARAM_STATIC_STRINGS));
}