libnm-core: add _nm_simple_connection_new_from_dbus() function
[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 there was
64  * an error.
65  **/
66 NMConnection *
67 _nm_simple_connection_new_from_dbus (GVariant *dict, NMSettingParseFlags parse_flags, 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         g_return_val_if_fail (!NM_FLAGS_ANY (parse_flags, ~NM_SETTING_PARSE_FLAGS_ALL), NULL);
74         g_return_val_if_fail (!NM_FLAGS_ALL (parse_flags, NM_SETTING_PARSE_FLAGS_STRICT | NM_SETTING_PARSE_FLAGS_BEST_EFFORT), NULL);
75
76         connection = nm_simple_connection_new ();
77         if (!_nm_connection_replace_settings (connection, dict, parse_flags, error))
78                 g_clear_object (&connection);
79         return connection;
80 }
81
82 /**
83  * nm_simple_connection_new_from_dbus:
84  * @dict: a #GVariant of type %NM_VARIANT_TYPE_CONNECTION describing the connection
85  * @error: on unsuccessful return, an error
86  *
87  * Creates a new #NMSimpleConnection from a hash table describing the
88  * connection and normalize the connection.  See nm_connection_to_dbus() for a
89  * description of the expected hash table.
90  *
91  * Returns: (transfer full): the new #NMSimpleConnection object, populated with
92  * settings created from the values in the hash table, or %NULL if the
93  * connection failed to normalize.
94  **/
95 NMConnection *
96 nm_simple_connection_new_from_dbus (GVariant *dict, GError **error)
97 {
98         return _nm_simple_connection_new_from_dbus (dict,
99                                                     NM_SETTING_PARSE_FLAGS_NORMALIZE,
100                                                     error);
101 }
102
103 /**
104  * nm_simple_connection_new_clone:
105  * @connection: the #NMConnection to clone
106  *
107  * Clones an #NMConnection as an #NMSimpleConnection.
108  *
109  * Returns: (transfer full): a new #NMConnection containing the same settings
110  * and properties as the source #NMConnection
111  **/
112 NMConnection *
113 nm_simple_connection_new_clone (NMConnection *connection)
114 {
115         NMConnection *clone;
116
117         g_return_val_if_fail (NM_IS_CONNECTION (connection), NULL);
118
119         clone = nm_simple_connection_new ();
120         nm_connection_set_path (clone, nm_connection_get_path (connection));
121         nm_connection_replace_settings_from_connection (clone, connection);
122
123         return clone;
124 }
125
126 static void
127 dispose (GObject *object)
128 {
129         nm_connection_clear_secrets (NM_CONNECTION (object));
130
131         G_OBJECT_CLASS (nm_simple_connection_parent_class)->dispose (object);
132 }
133
134 static void
135 nm_simple_connection_class_init (NMSimpleConnectionClass *simple_class)
136 {
137         GObjectClass *object_class = G_OBJECT_CLASS (simple_class);
138
139         object_class->dispose = dispose;
140 }
141
142 static void
143 nm_simple_connection_interface_init (NMConnectionInterface *iface)
144 {
145 }