nm-simple-connection.c 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2007 - 2008 Novell, Inc.
 * Copyright 2007 - 2014 Red Hat, Inc.
 */

22
#include "nm-default.h"
23

24
#include "nm-simple-connection.h"
25

26
#include "nm-setting-private.h"
27

28 29 30 31 32 33 34 35
/**
 * SECTION:nm-simple-connection
 * @short_description: An unmanaged connection
 *
 * An #NMSimpleConnection does not directly represent a D-Bus-exported connection,
 * but might be used in the process of creating a new one.
 **/

36 37
/*****************************************************************************/

38 39 40 41 42 43
static void nm_simple_connection_interface_init (NMConnectionInterface *iface);

G_DEFINE_TYPE_WITH_CODE (NMSimpleConnection, nm_simple_connection, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (NM_TYPE_CONNECTION, nm_simple_connection_interface_init);
                         )

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

46 47 48 49 50 51 52 53
static void
nm_simple_connection_init (NMSimpleConnection *self)
{
}

/**
 * nm_simple_connection_new:
 *
54
 * Creates a new #NMSimpleConnection object with no #NMSetting objects.
55 56 57 58 59 60 61 62 63 64
 *
 * Returns: (transfer full): the new empty #NMConnection object
 **/
NMConnection *
nm_simple_connection_new (void)
{
	return (NMConnection *) g_object_new (NM_TYPE_SIMPLE_CONNECTION, NULL);
}

/**
65
 * _nm_simple_connection_new_from_dbus:
66
 * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
67 68 69
 * @error: on unsuccessful return, an error
 *
 * Creates a new #NMSimpleConnection from a hash table describing the
70
 * connection.  See nm_connection_to_dbus() for a description of the expected
71 72 73
 * hash table.
 *
 * Returns: (transfer full): the new #NMSimpleConnection object, populated with
74 75
 * settings created from the values in the hash table, or %NULL if there was
 * an error.
76 77
 **/
NMConnection *
78
_nm_simple_connection_new_from_dbus (GVariant *dict, NMSettingParseFlags parse_flags, GError **error)
79 80 81
{
	NMConnection *connection;

82 83
	g_return_val_if_fail (dict != NULL, NULL);
	g_return_val_if_fail (g_variant_is_of_type (dict, NM_VARIANT_TYPE_CONNECTION), NULL);
84 85
	g_return_val_if_fail (!NM_FLAGS_ANY (parse_flags, ~NM_SETTING_PARSE_FLAGS_ALL), NULL);
	g_return_val_if_fail (!NM_FLAGS_ALL (parse_flags, NM_SETTING_PARSE_FLAGS_STRICT | NM_SETTING_PARSE_FLAGS_BEST_EFFORT), NULL);
86 87

	connection = nm_simple_connection_new ();
88
	if (!_nm_connection_replace_settings (connection, dict, parse_flags, error))
89 90
		g_clear_object (&connection);
	return connection;
91 92
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/**
 * nm_simple_connection_new_from_dbus:
 * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
 * @error: on unsuccessful return, an error
 *
 * Creates a new #NMSimpleConnection from a hash table describing the
 * connection and normalize the connection.  See nm_connection_to_dbus() for a
 * description of the expected hash table.
 *
 * Returns: (transfer full): the new #NMSimpleConnection object, populated with
 * settings created from the values in the hash table, or %NULL if the
 * connection failed to normalize.
 **/
NMConnection *
nm_simple_connection_new_from_dbus (GVariant *dict, GError **error)
{
	return _nm_simple_connection_new_from_dbus (dict,
	                                            NM_SETTING_PARSE_FLAGS_NORMALIZE,
	                                            error);
}

114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * nm_simple_connection_new_clone:
 * @connection: the #NMConnection to clone
 *
 * Clones an #NMConnection as an #NMSimpleConnection.
 *
 * Returns: (transfer full): a new #NMConnection containing the same settings
 * and properties as the source #NMConnection
 **/
NMConnection *
nm_simple_connection_new_clone (NMConnection *connection)
{
	NMConnection *clone;
127
	const char *path;
128 129 130 131

	g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);

	clone = nm_simple_connection_new ();
132 133 134 135 136

	path = nm_connection_get_path (connection);
	if (path)
		nm_connection_set_path (clone, path);

137
	nm_connection_replace_settings_from_connection (clone, connection);
138 139 140 141

	return clone;
}

142 143 144 145 146 147 148 149
static void
dispose (GObject *object)
{
	nm_connection_clear_secrets (NM_CONNECTION (object));

	G_OBJECT_CLASS (nm_simple_connection_parent_class)->dispose (object);
}

150 151 152
static void
nm_simple_connection_class_init (NMSimpleConnectionClass *simple_class)
{
153 154 155
	GObjectClass *object_class = G_OBJECT_CLASS (simple_class);

	object_class->dispose = dispose;
156 157 158 159 160 161
}

static void
nm_simple_connection_interface_init (NMConnectionInterface *iface)
{
}