Update Italian translation
[glib.git] / gio / gtlspassword.c
bloba64faa4641fbc88d508068d391d7a16d0f46289a
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2011 Collabora, Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Stef Walter <stefw@collabora.co.uk>
21 #include "config.h"
22 #include "glib.h"
23 #include "glibintl.h"
25 #include "gioenumtypes.h"
26 #include "gtlspassword.h"
28 #include <string.h>
30 /**
31 * SECTION:gtlspassword
32 * @title: GTlsPassword
33 * @short_description: TLS Passwords for prompting
34 * @include: gio/gio.h
36 * Holds a password used in TLS.
39 /**
40 * GTlsPassword:
42 * An abstract interface representing a password used in TLS. Often used in
43 * user interaction such as unlocking a key storage token.
45 * Since: 2.30
48 enum
50 PROP_0,
51 PROP_FLAGS,
52 PROP_DESCRIPTION,
53 PROP_WARNING
56 struct _GTlsPasswordPrivate
58 guchar *value;
59 gsize length;
60 GDestroyNotify destroy;
61 GTlsPasswordFlags flags;
62 gchar *description;
63 gchar *warning;
66 G_DEFINE_TYPE_WITH_PRIVATE (GTlsPassword, g_tls_password, G_TYPE_OBJECT)
68 static void
69 g_tls_password_init (GTlsPassword *password)
71 password->priv = g_tls_password_get_instance_private (password);
74 static const guchar *
75 g_tls_password_real_get_value (GTlsPassword *password,
76 gsize *length)
78 if (length)
79 *length = password->priv->length;
80 return password->priv->value;
83 static void
84 g_tls_password_real_set_value (GTlsPassword *password,
85 guchar *value,
86 gssize length,
87 GDestroyNotify destroy)
89 if (password->priv->destroy)
90 (password->priv->destroy) (password->priv->value);
91 password->priv->destroy = NULL;
92 password->priv->value = NULL;
93 password->priv->length = 0;
95 if (length < 0)
96 length = strlen ((gchar*) value);
98 password->priv->value = value;
99 password->priv->length = length;
100 password->priv->destroy = destroy;
103 static const gchar*
104 g_tls_password_real_get_default_warning (GTlsPassword *password)
106 GTlsPasswordFlags flags;
108 flags = g_tls_password_get_flags (password);
110 if (flags & G_TLS_PASSWORD_FINAL_TRY)
111 return _("This is the last chance to enter the password correctly before your access is locked out.");
112 if (flags & G_TLS_PASSWORD_MANY_TRIES)
113 return _("Several password entered have been incorrect, and your access will be locked out after further failures.");
114 if (flags & G_TLS_PASSWORD_RETRY)
115 return _("The password entered is incorrect.");
117 return NULL;
120 static void
121 g_tls_password_get_property (GObject *object,
122 guint prop_id,
123 GValue *value,
124 GParamSpec *pspec)
126 GTlsPassword *password = G_TLS_PASSWORD (object);
128 switch (prop_id)
130 case PROP_FLAGS:
131 g_value_set_flags (value, g_tls_password_get_flags (password));
132 break;
133 case PROP_WARNING:
134 g_value_set_string (value, g_tls_password_get_warning (password));
135 break;
136 case PROP_DESCRIPTION:
137 g_value_set_string (value, g_tls_password_get_description (password));
138 break;
139 default:
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 break;
145 static void
146 g_tls_password_set_property (GObject *object,
147 guint prop_id,
148 const GValue *value,
149 GParamSpec *pspec)
151 GTlsPassword *password = G_TLS_PASSWORD (object);
153 switch (prop_id)
155 case PROP_FLAGS:
156 g_tls_password_set_flags (password, g_value_get_flags (value));
157 break;
158 case PROP_WARNING:
159 g_tls_password_set_warning (password, g_value_get_string (value));
160 break;
161 case PROP_DESCRIPTION:
162 g_tls_password_set_description (password, g_value_get_string (value));
163 break;
164 default:
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 break;
170 static void
171 g_tls_password_finalize (GObject *object)
173 GTlsPassword *password = G_TLS_PASSWORD (object);
175 g_tls_password_real_set_value (password, NULL, 0, NULL);
176 g_free (password->priv->warning);
177 g_free (password->priv->description);
179 G_OBJECT_CLASS (g_tls_password_parent_class)->finalize (object);
182 static void
183 g_tls_password_class_init (GTlsPasswordClass *klass)
185 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187 klass->get_value = g_tls_password_real_get_value;
188 klass->set_value = g_tls_password_real_set_value;
189 klass->get_default_warning = g_tls_password_real_get_default_warning;
191 gobject_class->get_property = g_tls_password_get_property;
192 gobject_class->set_property = g_tls_password_set_property;
193 gobject_class->finalize = g_tls_password_finalize;
195 g_object_class_install_property (gobject_class, PROP_FLAGS,
196 g_param_spec_flags ("flags",
197 P_("Flags"),
198 P_("Flags about the password"),
199 G_TYPE_TLS_PASSWORD_FLAGS,
200 G_TLS_PASSWORD_NONE,
201 G_PARAM_READWRITE |
202 G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (gobject_class, PROP_DESCRIPTION,
205 g_param_spec_string ("description",
206 P_("Description"),
207 P_("Description of what the password is for"),
208 NULL,
209 G_PARAM_READWRITE |
210 G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_WARNING,
213 g_param_spec_string ("warning",
214 P_("Warning"),
215 P_("Warning about the password"),
216 NULL,
217 G_PARAM_READWRITE |
218 G_PARAM_STATIC_STRINGS));
223 * g_tls_password_new:
224 * @flags: the password flags
225 * @description: description of what the password is for
227 * Create a new #GTlsPassword object.
229 * Returns: (transfer full): The newly allocated password object
231 GTlsPassword *
232 g_tls_password_new (GTlsPasswordFlags flags,
233 const gchar *description)
235 return g_object_new (G_TYPE_TLS_PASSWORD,
236 "flags", flags,
237 "description", description,
238 NULL);
242 * g_tls_password_get_value:
243 * @password: a #GTlsPassword object
244 * @length: (nullable): location to place the length of the password.
246 * Get the password value. If @length is not %NULL then it will be
247 * filled in with the length of the password value. (Note that the
248 * password value is not nul-terminated, so you can only pass %NULL
249 * for @length in contexts where you know the password will have a
250 * certain fixed length.)
252 * Returns: The password value (owned by the password object).
254 * Since: 2.30
256 const guchar *
257 g_tls_password_get_value (GTlsPassword *password,
258 gsize *length)
260 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
261 return G_TLS_PASSWORD_GET_CLASS (password)->get_value (password, length);
265 * g_tls_password_set_value:
266 * @password: a #GTlsPassword object
267 * @value: the new password value
268 * @length: the length of the password, or -1
270 * Set the value for this password. The @value will be copied by the password
271 * object.
273 * Specify the @length, for a non-nul-terminated password. Pass -1 as
274 * @length if using a nul-terminated password, and @length will be
275 * calculated automatically. (Note that the terminating nul is not
276 * considered part of the password in this case.)
278 * Since: 2.30
280 void
281 g_tls_password_set_value (GTlsPassword *password,
282 const guchar *value,
283 gssize length)
285 g_return_if_fail (G_IS_TLS_PASSWORD (password));
287 if (length < 0)
288 length = strlen ((gchar *)value);
290 g_tls_password_set_value_full (password, g_memdup (value, length), length, g_free);
294 * g_tls_password_set_value_full:
295 * @password: a #GTlsPassword object
296 * @value: the value for the password
297 * @length: the length of the password, or -1
298 * @destroy: (nullable): a function to use to free the password.
300 * Provide the value for this password.
302 * The @value will be owned by the password object, and later freed using
303 * the @destroy function callback.
305 * Specify the @length, for a non-nul-terminated password. Pass -1 as
306 * @length if using a nul-terminated password, and @length will be
307 * calculated automatically. (Note that the terminating nul is not
308 * considered part of the password in this case.)
310 * Virtual: set_value
311 * Since: 2.30
313 void
314 g_tls_password_set_value_full (GTlsPassword *password,
315 guchar *value,
316 gssize length,
317 GDestroyNotify destroy)
319 g_return_if_fail (G_IS_TLS_PASSWORD (password));
320 G_TLS_PASSWORD_GET_CLASS (password)->set_value (password, value,
321 length, destroy);
325 * g_tls_password_get_flags:
326 * @password: a #GTlsPassword object
328 * Get flags about the password.
330 * Returns: The flags about the password.
332 * Since: 2.30
334 GTlsPasswordFlags
335 g_tls_password_get_flags (GTlsPassword *password)
337 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_PASSWORD_NONE);
338 return password->priv->flags;
342 * g_tls_password_set_flags:
343 * @password: a #GTlsPassword object
344 * @flags: The flags about the password
346 * Set flags about the password.
348 * Since: 2.30
350 void
351 g_tls_password_set_flags (GTlsPassword *password,
352 GTlsPasswordFlags flags)
354 g_return_if_fail (G_IS_TLS_PASSWORD (password));
356 password->priv->flags = flags;
358 g_object_notify (G_OBJECT (password), "flags");
362 * g_tls_password_get_description:
363 * @password: a #GTlsPassword object
365 * Get a description string about what the password will be used for.
367 * Returns: The description of the password.
369 * Since: 2.30
371 const gchar*
372 g_tls_password_get_description (GTlsPassword *password)
374 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
375 return password->priv->description;
379 * g_tls_password_set_description:
380 * @password: a #GTlsPassword object
381 * @description: The description of the password
383 * Set a description string about what the password will be used for.
385 * Since: 2.30
387 void
388 g_tls_password_set_description (GTlsPassword *password,
389 const gchar *description)
391 gchar *copy;
393 g_return_if_fail (G_IS_TLS_PASSWORD (password));
395 copy = g_strdup (description);
396 g_free (password->priv->description);
397 password->priv->description = copy;
399 g_object_notify (G_OBJECT (password), "description");
403 * g_tls_password_get_warning:
404 * @password: a #GTlsPassword object
406 * Get a user readable translated warning. Usually this warning is a
407 * representation of the password flags returned from
408 * g_tls_password_get_flags().
410 * Returns: The warning.
412 * Since: 2.30
414 const gchar *
415 g_tls_password_get_warning (GTlsPassword *password)
417 g_return_val_if_fail (G_IS_TLS_PASSWORD (password), NULL);
419 if (password->priv->warning == NULL)
420 return G_TLS_PASSWORD_GET_CLASS (password)->get_default_warning (password);
422 return password->priv->warning;
426 * g_tls_password_set_warning:
427 * @password: a #GTlsPassword object
428 * @warning: The user readable warning
430 * Set a user readable translated warning. Usually this warning is a
431 * representation of the password flags returned from
432 * g_tls_password_get_flags().
434 * Since: 2.30
436 void
437 g_tls_password_set_warning (GTlsPassword *password,
438 const gchar *warning)
440 gchar *copy;
442 g_return_if_fail (G_IS_TLS_PASSWORD (password));
444 copy = g_strdup (warning);
445 g_free (password->priv->warning);
446 password->priv->warning = copy;
448 g_object_notify (G_OBJECT (password), "warning");