device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-cdma.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2013 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-setting-cdma.h"
27 #include "nm-utils.h"
28 #include "nm-setting-private.h"
29
30 /**
31  * SECTION:nm-setting-cdma
32  * @short_description: Describes CDMA-based mobile broadband properties
33  * @include: nm-setting-cdma.h
34  *
35  * The #NMSettingCdma object is a #NMSetting subclass that describes
36  * properties that allow connections to IS-95-based mobile broadband
37  * networks, including those using CDMA2000/EVDO technology.
38  */
39
40 /**
41  * nm_setting_cdma_error_quark:
42  *
43  * Registers an error quark for #NMSettingCdma if necessary.
44  *
45  * Returns: the error quark used for #NMSettingCdma errors.
46  **/
47 GQuark
48 nm_setting_cdma_error_quark (void)
49 {
50         static GQuark quark;
51
52         if (G_UNLIKELY (!quark))
53                 quark = g_quark_from_static_string ("nm-setting-cdma-error-quark");
54         return quark;
55 }
56
57
58 G_DEFINE_TYPE_WITH_CODE (NMSettingCdma, nm_setting_cdma, NM_TYPE_SETTING,
59                          _nm_register_setting (NM_SETTING_CDMA_SETTING_NAME,
60                                                g_define_type_id,
61                                                1,
62                                                NM_SETTING_CDMA_ERROR))
63 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_CDMA)
64
65 #define NM_SETTING_CDMA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_CDMA, NMSettingCdmaPrivate))
66
67 typedef struct {
68         char *number; /* For dialing, duh */
69         char *username;
70         char *password;
71         NMSettingSecretFlags password_flags;
72 } NMSettingCdmaPrivate;
73
74 enum {
75         PROP_0,
76         PROP_NUMBER,
77         PROP_USERNAME,
78         PROP_PASSWORD,
79         PROP_PASSWORD_FLAGS,
80
81         LAST_PROP
82 };
83
84 /**
85  * nm_setting_cdma_new:
86  *
87  * Creates a new #NMSettingCdma object with default values.
88  *
89  * Returns: the new empty #NMSettingCdma object
90  **/
91 NMSetting *
92 nm_setting_cdma_new (void)
93 {
94         return (NMSetting *) g_object_new (NM_TYPE_SETTING_CDMA, NULL);
95 }
96
97 /**
98  * nm_setting_cdma_get_number:
99  * @setting: the #NMSettingCdma
100  *
101  * Returns: the #NMSettingCdma:number property of the setting
102  **/
103 const char *
104 nm_setting_cdma_get_number (NMSettingCdma *setting)
105 {
106         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
107
108         return NM_SETTING_CDMA_GET_PRIVATE (setting)->number;
109 }
110
111 /**
112  * nm_setting_cdma_get_username:
113  * @setting: the #NMSettingCdma
114  *
115  * Returns: the #NMSettingCdma:username property of the setting
116  **/
117 const char *
118 nm_setting_cdma_get_username (NMSettingCdma *setting)
119 {
120         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
121
122         return NM_SETTING_CDMA_GET_PRIVATE (setting)->username;
123 }
124
125 /**
126  * nm_setting_cdma_get_password:
127  * @setting: the #NMSettingCdma
128  *
129  * Returns: the #NMSettingCdma:password property of the setting
130  **/
131 const char *
132 nm_setting_cdma_get_password (NMSettingCdma *setting)
133 {
134         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
135
136         return NM_SETTING_CDMA_GET_PRIVATE (setting)->password;
137 }
138
139 /**
140  * nm_setting_cdma_get_password_flags:
141  * @setting: the #NMSettingCdma
142  *
143  * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingCdma:password
144  **/
145 NMSettingSecretFlags
146 nm_setting_cdma_get_password_flags (NMSettingCdma *setting)
147 {
148         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NM_SETTING_SECRET_FLAG_NONE);
149
150         return NM_SETTING_CDMA_GET_PRIVATE (setting)->password_flags;
151 }
152
153 static gboolean
154 verify (NMSetting *setting, GSList *all_settings, GError **error)
155 {
156         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (setting);
157
158         if (!priv->number) {
159                 g_set_error_literal (error,
160                                      NM_SETTING_CDMA_ERROR,
161                                      NM_SETTING_CDMA_ERROR_MISSING_PROPERTY,
162                                      _("property is missing"));
163                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_NUMBER);
164                 return FALSE;
165         } else if (!strlen (priv->number)) {
166                 g_set_error_literal (error,
167                                      NM_SETTING_CDMA_ERROR,
168                                      NM_SETTING_CDMA_ERROR_INVALID_PROPERTY,
169                                      _("property is empty"));
170                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_NUMBER);
171                 return FALSE;
172         }
173
174         if (priv->username && !strlen (priv->username)) {
175                 g_set_error_literal (error,
176                                      NM_SETTING_CDMA_ERROR,
177                                      NM_SETTING_CDMA_ERROR_INVALID_PROPERTY,
178                                      _("property is empty"));
179                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_USERNAME);
180                 return FALSE;
181         }
182
183         return TRUE;
184 }
185
186 static GPtrArray *
187 need_secrets (NMSetting *setting)
188 {
189         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (setting);
190         GPtrArray *secrets = NULL;
191
192         if (priv->password && *priv->password)
193                 return NULL;
194
195         if (priv->username) {
196                 if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
197                         secrets = g_ptr_array_sized_new (1);
198                         g_ptr_array_add (secrets, NM_SETTING_CDMA_PASSWORD);
199                 }
200         }
201
202         return secrets;
203 }
204
205 static void
206 nm_setting_cdma_init (NMSettingCdma *setting)
207 {
208 }
209
210 static void
211 finalize (GObject *object)
212 {
213         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (object);
214
215         g_free (priv->number);
216         g_free (priv->username);
217         g_free (priv->password);
218
219         G_OBJECT_CLASS (nm_setting_cdma_parent_class)->finalize (object);
220 }
221
222 static void
223 set_property (GObject *object, guint prop_id,
224               const GValue *value, GParamSpec *pspec)
225 {
226         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (object);
227
228         switch (prop_id) {
229         case PROP_NUMBER:
230                 g_free (priv->number);
231                 priv->number = g_value_dup_string (value);
232                 break;
233         case PROP_USERNAME:
234                 g_free (priv->username);
235                 priv->username = g_value_dup_string (value);
236                 break;
237         case PROP_PASSWORD:
238                 g_free (priv->password);
239                 priv->password = g_value_dup_string (value);
240                 break;
241         case PROP_PASSWORD_FLAGS:
242                 priv->password_flags = g_value_get_uint (value);
243                 break;
244         default:
245                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246                 break;
247         }
248 }
249
250 static void
251 get_property (GObject *object, guint prop_id,
252               GValue *value, GParamSpec *pspec)
253 {
254         NMSettingCdma *setting = NM_SETTING_CDMA (object);
255
256         switch (prop_id) {
257         case PROP_NUMBER:
258                 g_value_set_string (value, nm_setting_cdma_get_number (setting));
259                 break;
260         case PROP_USERNAME:
261                 g_value_set_string (value, nm_setting_cdma_get_username (setting));
262                 break;
263         case PROP_PASSWORD:
264                 g_value_set_string (value, nm_setting_cdma_get_password (setting));
265                 break;
266         case PROP_PASSWORD_FLAGS:
267                 g_value_set_uint (value, nm_setting_cdma_get_password_flags (setting));
268                 break;
269         default:
270                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271                 break;
272         }
273 }
274
275 static void
276 nm_setting_cdma_class_init (NMSettingCdmaClass *setting_class)
277 {
278         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
279         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
280
281         g_type_class_add_private (setting_class, sizeof (NMSettingCdmaPrivate));
282
283         /* virtual methods */
284         object_class->set_property = set_property;
285         object_class->get_property = get_property;
286         object_class->finalize     = finalize;
287         parent_class->verify       = verify;
288         parent_class->need_secrets = need_secrets;
289
290         /* Properties */
291
292         /**
293          * NMSettingCdma:number:
294          *
295          * The number to dial to establish the connection to the CDMA-based mobile
296          * broadband network, if any.  If not specified, the default number (#777)
297          * is used when required.
298          **/
299         g_object_class_install_property
300                 (object_class, PROP_NUMBER,
301                  g_param_spec_string (NM_SETTING_CDMA_NUMBER, "", "",
302                                       NULL,
303                                       G_PARAM_READWRITE |
304                                       G_PARAM_STATIC_STRINGS));
305
306         /**
307          * NMSettingCdma:username:
308          *
309          * The username used to authenticate with the network, if required.  Many
310          * providers do not require a username, or accept any username.  But if a
311          * username is required, it is specified here.
312          **/
313         g_object_class_install_property
314                 (object_class, PROP_USERNAME,
315                  g_param_spec_string (NM_SETTING_CDMA_USERNAME, "", "",
316                                       NULL,
317                                       G_PARAM_READWRITE |
318                                       G_PARAM_STATIC_STRINGS));
319
320         /**
321          * NMSettingCdma:password:
322          *
323          * The password used to authenticate with the network, if required.  Many
324          * providers do not require a password, or accept any password.  But if a
325          * password is required, it is specified here.
326          **/
327         g_object_class_install_property
328                 (object_class, PROP_PASSWORD,
329                  g_param_spec_string (NM_SETTING_CDMA_PASSWORD, "", "",
330                                       NULL,
331                                       G_PARAM_READWRITE |
332                                       NM_SETTING_PARAM_SECRET |
333                                       G_PARAM_STATIC_STRINGS));
334
335         /**
336          * NMSettingCdma:password-flags:
337          *
338          * Flags indicating how to handle the #NMSettingCdma:password property.
339          **/
340         g_object_class_install_property
341                 (object_class, PROP_PASSWORD_FLAGS,
342                  g_param_spec_uint (NM_SETTING_CDMA_PASSWORD_FLAGS, "", "",
343                                     NM_SETTING_SECRET_FLAG_NONE,
344                                     NM_SETTING_SECRET_FLAGS_ALL,
345                                     NM_SETTING_SECRET_FLAG_NONE,
346                                     G_PARAM_READWRITE |
347                                     G_PARAM_STATIC_STRINGS));
348 }