summaryrefslogtreecommitdiff
path: root/tui/nmt-slave-list.c
blob: 284614aa2f18061089b827931ae51e8761a69acb (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
/* -*- 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-slave-list:
 * @short_description: An editable list of a connection's slaves
 *
 * #NmtSlaveList implements an #NmtEditConnectionList for the
 * slaves of a connection.
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include <nm-remote-connection.h>

#include "nmt-slave-list.h"

G_DEFINE_TYPE (NmtSlaveList, nmt_slave_list, NMT_TYPE_EDIT_CONNECTION_LIST)

#define NMT_SLAVE_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NMT_TYPE_SLAVE_LIST, NmtSlaveListPrivate))

typedef struct {
	NMConnection *master;
	const char *master_type, *master_uuid;

	NmtAddConnectionTypeFilter type_filter;
	gpointer type_filter_data;
} NmtSlaveListPrivate;

enum {
	PROP_0,
	PROP_MASTER,
	PROP_TYPE_FILTER,
	PROP_TYPE_FILTER_DATA,

	LAST_PROP
};

static gboolean nmt_slave_list_connection_filter (NmtEditConnectionList *list,
                                                  NMConnection          *connection,
                                                  gpointer               user_data);

/**
 * nmt_slave_list_new:
 * @master: the master #NMConnection whose slaves are being listed
 * @type_filter: (allow-none): a function to limit the availble slave types
 * @type_filter_data: (allow-none): data for @type_filter.
 *
 * Creates a new #NmtSlaveList.
 *
 * If @type_filter is non-%NULL, it will be used to limit the connection
 * types that are available when the user clicks on the "Add" button to add
 * a new slave. If the @type_filter filters the list down to only a single
 * connection type, then the user will not be presented with a connection-type
 * dialog, and will instead be immediately taken to an editor window for the
 * new slave after clicking "Add".
 *
 * Returns: a new #NmtSlaveList.
 */
NmtNewtWidget *
nmt_slave_list_new (NMConnection               *master,
                    NmtAddConnectionTypeFilter  type_filter,
                    gpointer                    type_filter_data)
{
	return g_object_new (NMT_TYPE_SLAVE_LIST,
	                     "master", master,
	                     "type-filter", type_filter,
	                     "type-filter-data", type_filter_data,
	                     "grouped", FALSE,
	                     "connection-filter", nmt_slave_list_connection_filter,
	                     NULL);
}

static void
nmt_slave_list_init (NmtSlaveList *list)
{
}

static void
nmt_slave_list_finalize (GObject *object)
{
	NmtSlaveListPrivate *priv = NMT_SLAVE_LIST_GET_PRIVATE (object);

	g_object_unref (priv->master);

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

static gboolean
nmt_slave_list_connection_filter (NmtEditConnectionList *list,
                                  NMConnection          *connection,
                                  gpointer               user_data)
{
	NmtSlaveListPrivate *priv = NMT_SLAVE_LIST_GET_PRIVATE (list);
	NMSettingConnection *s_con;
	const char *master, *master_ifname, *slave_type;

	s_con = nm_connection_get_setting_connection (connection);
	g_return_val_if_fail (s_con != NULL, FALSE);

	slave_type = nm_setting_connection_get_slave_type (s_con);
	if (g_strcmp0 (slave_type, priv->master_type) != 0)
		return FALSE;

	master = nm_setting_connection_get_master (s_con);
	if (!master)
		return FALSE;

	master_ifname = nm_connection_get_virtual_iface_name (priv->master);
	if (g_strcmp0 (master, master_ifname) != 0 && g_strcmp0 (master, priv->master_uuid) != 0)
		return FALSE;

	return TRUE;
}

static void
nmt_slave_list_add_connection (NmtEditConnectionList *list)
{
	NmtSlaveListPrivate *priv = NMT_SLAVE_LIST_GET_PRIVATE (list);

	nmt_add_connection_full (_("Select the type of slave connection you wish to add."), NULL,
	                         priv->master, priv->type_filter, priv->type_filter_data);
}

static void
nmt_slave_list_edit_connection (NmtEditConnectionList *list,
                                NMConnection          *connection)
{
	nmt_edit_connection (connection);
}

static void
nmt_slave_list_remove_connection (NmtEditConnectionList  *list,
                                  NMRemoteConnection     *connection)
{
	nmt_remove_connection (connection);
}

static void
nmt_slave_list_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
	NmtSlaveListPrivate *priv = NMT_SLAVE_LIST_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_MASTER:
		priv->master = g_value_dup_object (value);
		if (priv->master) {
			NMSettingConnection *s_con = nm_connection_get_setting_connection (priv->master);

			priv->master_type = nm_setting_connection_get_connection_type (s_con);
			priv->master_uuid = nm_setting_connection_get_uuid (s_con);
		}
		break;
	case PROP_TYPE_FILTER:
		priv->type_filter = g_value_get_pointer (value);
		break;
	case PROP_TYPE_FILTER_DATA:
		priv->type_filter_data = g_value_get_pointer (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_slave_list_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
	NmtSlaveListPrivate *priv = NMT_SLAVE_LIST_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_MASTER:
		g_value_set_object (value, priv->master);
		break;
	case PROP_TYPE_FILTER:
		g_value_set_pointer (value, priv->type_filter);
		break;
	case PROP_TYPE_FILTER_DATA:
		g_value_set_pointer (value, priv->type_filter_data);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
nmt_slave_list_class_init (NmtSlaveListClass *list_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (list_class);
	NmtEditConnectionListClass *connection_list_class = NMT_EDIT_CONNECTION_LIST_CLASS (list_class);

	g_type_class_add_private (list_class, sizeof (NmtSlaveListPrivate));

	/* virtual methods */
	object_class->set_property = nmt_slave_list_set_property;
	object_class->get_property = nmt_slave_list_get_property;
	object_class->finalize     = nmt_slave_list_finalize;

	connection_list_class->add_connection    = nmt_slave_list_add_connection;
	connection_list_class->edit_connection   = nmt_slave_list_edit_connection;
	connection_list_class->remove_connection = nmt_slave_list_remove_connection;

	/**
	 * NmtSlaveList:master:
	 *
	 * The master #NMConnection whose slaves are being displayed.
	 */
	g_object_class_install_property (object_class, PROP_MASTER,
	                                 g_param_spec_object ("master", "", "",
	                                                      NM_TYPE_CONNECTION,
	                                                      G_PARAM_READWRITE |
	                                                      G_PARAM_CONSTRUCT_ONLY |
	                                                      G_PARAM_STATIC_STRINGS));
	/**
	 * NmtSlaveList:type-filter:
	 *
	 * If non-%NULL, this will be used to limit the connection types
	 * that are available when the user clicks on the "Add" button to
	 * add a new slave. If the filter filters the list down to only a
	 * single connection type, then the user will not be presented
	 * with a connection-type dialog, and will instead be immediately
	 * taken to an editor window for the new slave after clicking
	 * "Add".
	 */
	g_object_class_install_property (object_class, PROP_TYPE_FILTER,
	                                 g_param_spec_pointer ("type-filter", "", "",
	                                                       G_PARAM_READWRITE |
	                                                       G_PARAM_CONSTRUCT_ONLY |
	                                                       G_PARAM_STATIC_STRINGS));
	/**
	 * NmtSlaveList:type-filter-data:
	 *
	 * User data passed to #NmtSlaveList:type-filter
	 */
	g_object_class_install_property (object_class, PROP_TYPE_FILTER_DATA,
	                                 g_param_spec_pointer ("type-filter-data", "", "",
	                                                       G_PARAM_READWRITE |
	                                                       G_PARAM_CONSTRUCT_ONLY |
	                                                       G_PARAM_STATIC_STRINGS));
}