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