nm-settings-plugin.c 8.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* NetworkManager system settings service
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
17
 * Copyright (C) 2007 - 2018 Red Hat, Inc.
18 19 20
 * Copyright (C) 2008 Novell, Inc.
 */

21
#include "nm-default.h"
22 23

#include "nm-settings-plugin.h"
24

25 26 27
#include "nm-utils.h"
#include "nm-core-internal.h"

28 29
#include "nm-settings-connection.h"

30
/*****************************************************************************/
Dan Winship's avatar
Dan Winship committed
31

32 33 34 35 36 37 38 39 40 41 42 43
enum {
	UNMANAGED_SPECS_CHANGED,
	UNRECOGNIZED_SPECS_CHANGED,

	LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (NMSettingsPlugin, nm_settings_plugin, G_TYPE_OBJECT)

/*****************************************************************************/
44 45

GSList *
46
nm_settings_plugin_get_unmanaged_specs (NMSettingsPlugin *self)
47
{
48 49
	NMSettingsPluginClass *klass;

50
	g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (self), NULL);
51

52 53 54 55
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (!klass->get_unmanaged_specs)
		return NULL;
	return klass->get_unmanaged_specs (self);
56 57
}

58 59
GSList *
nm_settings_plugin_get_unrecognized_specs (NMSettingsPlugin *self)
60
{
61 62 63
	NMSettingsPluginClass *klass;

	g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (self), NULL);
64

65 66 67 68
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (!klass->get_unrecognized_specs)
		return NULL;
	return klass->get_unrecognized_specs (self);
69 70 71
}

void
72 73 74
nm_settings_plugin_reload_connections (NMSettingsPlugin *self,
                                       NMSettingsPluginConnectionLoadCallback callback,
                                       gpointer user_data)
75
{
76 77
	NMSettingsPluginClass *klass;

78
	g_return_if_fail (NM_IS_SETTINGS_PLUGIN (self));
79
	g_return_if_fail (callback);
80

81 82 83
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (klass->reload_connections)
		klass->reload_connections (self, callback, user_data);
84 85
}

86 87 88
NMSettingsPluginConnectionLoadEntry *
nm_settings_plugin_create_connection_load_entries (const char *const*filenames,
                                                   gsize *out_len)
89
{
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	NMSettingsPluginConnectionLoadEntry *entries;
	gsize len;
	gsize i;

	len = NM_PTRARRAY_LEN (filenames);
	if (len == 0) {
		*out_len = 0;
		return NULL;
	}

	entries = g_new (NMSettingsPluginConnectionLoadEntry, len);
	for (i = 0; i < len; i++) {
		entries[i] = (NMSettingsPluginConnectionLoadEntry) {
			.filename = filenames[i],
			.error    = NULL,
			.handled  = FALSE,
		};
	}
108

109 110
	*out_len = len;
	return entries;
111 112
}

113 114 115 116 117 118
void
nm_settings_plugin_load_connections (NMSettingsPlugin *self,
                                     NMSettingsPluginConnectionLoadEntry *entries,
                                     gsize n_entries,
                                     NMSettingsPluginConnectionLoadCallback callback,
                                     gpointer user_data)
119
{
120 121 122
	NMSettingsPluginClass *klass;

	g_return_if_fail (NM_IS_SETTINGS_PLUGIN (self));
123

124 125 126
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (klass->load_connections)
		klass->load_connections (self, entries, n_entries, callback, user_data);
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141
void
nm_settings_plugin_load_connections_done (NMSettingsPlugin *self)
{
	NMSettingsPluginClass *klass;

	g_return_if_fail (NM_IS_SETTINGS_PLUGIN (self));

	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (klass->load_connections_done)
		klass->load_connections_done (self);
}

gboolean
142
nm_settings_plugin_add_connection (NMSettingsPlugin *self,
143
                                   NMConnection *connection,
144 145
                                   NMSettingsStorage **out_storage,
                                   NMConnection **out_connection,
146 147
                                   GError **error)
{
148
	NMSettingsPluginClass *klass;
149

150 151 152 153 154 155 156 157 158
	g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (self), FALSE);
	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);

#if NM_MORE_ASSERTS > 5
	nm_assert (nm_connection_verify (connection, NULL));
#endif

	NM_SET_OUT (out_storage, NULL);
	NM_SET_OUT (out_connection, NULL);
159

160 161
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);
	if (!klass->add_connection) {
162
		g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_NOT_SUPPORTED,
163 164
		                     "settings plugin does not support adding connections");
		return FALSE;
165
	}
166 167 168 169 170
	return klass->add_connection (self,
	                              connection,
	                              out_storage,
	                              out_connection,
	                              error);
171 172
}

173 174 175 176 177 178 179 180 181
gboolean
nm_settings_plugin_update_connection (NMSettingsPlugin *self,
                                      NMSettingsStorage *storage,
                                      NMConnection *connection,
                                      NMSettingsStorage **out_storage,
                                      NMConnection **out_connection,
                                      GError **error)
{
	NMSettingsPluginClass *klass = NULL;
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
	g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (self), FALSE);
	g_return_val_if_fail (NM_IS_SETTINGS_STORAGE (storage), FALSE);
	g_return_val_if_fail (nm_settings_storage_get_plugin (storage) == self, FALSE);
	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);

#if NM_MORE_ASSERTS > 5
	nm_assert (nm_connection_verify (connection, NULL));
	nm_assert (nm_streq (nm_connection_get_uuid (connection), nm_settings_storage_get_uuid (storage)));
#endif

	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);

	NM_SET_OUT (out_storage, NULL);
	NM_SET_OUT (out_connection, NULL);

	if (!klass->update_connection) {
		g_set_error (error,
		             NM_SETTINGS_ERROR,
		             NM_SETTINGS_ERROR_NOT_SUPPORTED,
		             "settings plugin does not support modifying connections");
		return FALSE;
	}
	return klass->update_connection (self,
	                                 storage,
	                                 connection,
	                                 out_storage,
	                                 out_connection,
	                                 error);
}

gboolean
nm_settings_plugin_delete_connection (NMSettingsPlugin *self,
                                      NMSettingsStorage *storage,
                                      GError **error)
217
{
218 219 220 221 222
	NMSettingsPluginClass *klass = NULL;

	g_return_val_if_fail (NM_IS_SETTINGS_PLUGIN (self), FALSE);
	g_return_val_if_fail (NM_IS_SETTINGS_STORAGE (storage), FALSE);
	g_return_val_if_fail (nm_settings_storage_get_plugin (storage) == self, FALSE);
223

224 225 226 227 228 229 230 231 232 233 234 235 236
	klass = NM_SETTINGS_PLUGIN_GET_CLASS (self);

	if (!klass->delete_connection) {
		g_set_error (error,
		             NM_SETTINGS_ERROR,
		             NM_SETTINGS_ERROR_NOT_SUPPORTED,
		             "settings plugin does not support deleting connections");
		return FALSE;
	}

	return klass->delete_connection (self,
	                                 storage,
	                                 error);
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
void
_nm_settings_plugin_emit_signal_unmanaged_specs_changed (NMSettingsPlugin *self)
{
	nm_assert (NM_IS_SETTINGS_PLUGIN (self));

	g_signal_emit (self, signals[UNMANAGED_SPECS_CHANGED], 0);
}

void
_nm_settings_plugin_emit_signal_unrecognized_specs_changed (NMSettingsPlugin *self)
{
	nm_assert (NM_IS_SETTINGS_PLUGIN (self));

	g_signal_emit (self, signals[UNRECOGNIZED_SPECS_CHANGED], 0);
}

/*****************************************************************************/

static void
nm_settings_plugin_init (NMSettingsPlugin *self)
{
}

static void
nm_settings_plugin_class_init (NMSettingsPluginClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	signals[UNMANAGED_SPECS_CHANGED] =
	    g_signal_new (NM_SETTINGS_PLUGIN_UNMANAGED_SPECS_CHANGED,
	                  G_OBJECT_CLASS_TYPE (object_class),
	                  G_SIGNAL_RUN_FIRST,
	                  0, NULL, NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE, 0);

	signals[UNRECOGNIZED_SPECS_CHANGED] =
	    g_signal_new (NM_SETTINGS_PLUGIN_UNRECOGNIZED_SPECS_CHANGED,
	                  G_OBJECT_CLASS_TYPE (object_class),
	                  G_SIGNAL_RUN_FIRST,
	                  0, NULL, NULL,
	                  g_cclosure_marshal_VOID__VOID,
	                  G_TYPE_NONE, 0);
284
}