1036c8ecce8b42c28d7c15002b77765763030521
[NetworkManager.git] / libnm-core / nm-simple-connection.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  * Boston, MA 02110-1301 USA.
17  *
18  * Copyright 2007 - 2008 Novell, Inc.
19  * Copyright 2007 - 2014 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include "nm-simple-connection.h"
25 #include "nm-setting-private.h"
26
27 static void nm_simple_connection_interface_init (NMConnectionInterface *iface);
28
29 G_DEFINE_TYPE_WITH_CODE (NMSimpleConnection, nm_simple_connection, G_TYPE_OBJECT,
30                          G_IMPLEMENT_INTERFACE (NM_TYPE_CONNECTION, nm_simple_connection_interface_init);
31                          )
32
33 static void
34 nm_simple_connection_init (NMSimpleConnection *self)
35 {
36 }
37
38 /**
39  * nm_simple_connection_new:
40  *
41  * Creates a new #NMSimpleConnection object with no #NMSetting objects. An
42  * #NMSimpleConnection does not directly represent a D-Bus-exported connection,
43  * but might be used in the process of creating a new one.
44  *
45  * Returns: (transfer full): the new empty #NMConnection object
46  **/
47 NMConnection *
48 nm_simple_connection_new (void)
49 {
50         return (NMConnection *) g_object_new (NM_TYPE_SIMPLE_CONNECTION, NULL);
51 }
52
53 /**
54  * nm_simple_connection_new_from_dbus:
55  * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
56  * @error: on unsuccessful return, an error
57  *
58  * Creates a new #NMSimpleConnection from a hash table describing the
59  * connection.  See nm_connection_to_dbus() for a description of the expected
60  * hash table.
61  *
62  * Returns: (transfer full): the new #NMSimpleConnection object, populated with
63  * settings created from the values in the hash table, or %NULL if the
64  * connection failed to validate
65  **/
66 NMConnection *
67 nm_simple_connection_new_from_dbus (GVariant *dict, GError **error)
68 {
69         NMConnection *connection;
70
71         g_return_val_if_fail (dict != NULL, NULL);
72         g_return_val_if_fail (g_variant_is_of_type (dict, NM_VARIANT_TYPE_CONNECTION), NULL);
73
74         connection = nm_simple_connection_new ();
75         if (   !nm_connection_replace_settings (connection, dict, error)
76             || !nm_connection_normalize (connection, NULL, NULL, error))
77                 g_clear_object (&connection);
78         return connection;
79 }
80
81 /**
82  * nm_simple_connection_new_clone:
83  * @connection: the #NMConnection to clone
84  *
85  * Clones an #NMConnection as an #NMSimpleConnection.
86  *
87  * Returns: (transfer full): a new #NMConnection containing the same settings
88  * and properties as the source #NMConnection
89  **/
90 NMConnection *
91 nm_simple_connection_new_clone (NMConnection *connection)
92 {
93         NMConnection *clone;
94
95         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
96
97         clone = nm_simple_connection_new ();
98         nm_connection_set_path (clone, nm_connection_get_path (connection));
99         nm_connection_replace_settings_from_connection (clone, connection);
100
101         return clone;
102 }
103
104 static void
105 dispose (GObject *object)
106 {
107         nm_connection_clear_secrets (NM_CONNECTION (object));
108
109         G_OBJECT_CLASS (nm_simple_connection_parent_class)->dispose (object);
110 }
111
112 static void
113 nm_simple_connection_class_init (NMSimpleConnectionClass *simple_class)
114 {
115         GObjectClass *object_class = G_OBJECT_CLASS (simple_class);
116
117         object_class->dispose = dispose;
118 }
119
120 static void
121 nm_simple_connection_interface_init (NMConnectionInterface *iface)
122 {
123 }