2 * e-source-authentication.c
4 * This program 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) version 3.
9 * This program 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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
20 * SECTION: e-source-authentication
21 * @include: libedataserver/e-source-authentication.h
22 * @short_description: #ESource extension for authentication settings
24 * The #ESourceAuthentication extension tracks authentication settings
25 * for a user account on a remote server.
27 * Access the extension as follows:
30 * #include <libedataserver/e-source-authentication.h>
32 * ESourceAuthentication *extension;
34 * extension = e_source_get_extension (source, E_SOURCE_EXTENSION_AUTHENTICATION);
38 #include "e-source-authentication.h"
40 #include <libedataserver/e-data-server-util.h>
42 #define E_SOURCE_AUTHENTICATION_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE \
44 ((obj), E_TYPE_SOURCE_AUTHENTICATION, ESourceAuthenticationPrivate))
46 struct _ESourceAuthenticationPrivate {
47 GMutex *property_lock;
63 ESourceAuthentication,
64 e_source_authentication,
65 E_TYPE_SOURCE_EXTENSION)
68 source_authentication_set_property (GObject *object,
73 switch (property_id) {
75 e_source_authentication_set_host (
76 E_SOURCE_AUTHENTICATION (object),
77 g_value_get_string (value));
81 e_source_authentication_set_method (
82 E_SOURCE_AUTHENTICATION (object),
83 g_value_get_string (value));
87 e_source_authentication_set_port (
88 E_SOURCE_AUTHENTICATION (object),
89 g_value_get_uint (value));
93 e_source_authentication_set_user (
94 E_SOURCE_AUTHENTICATION (object),
95 g_value_get_string (value));
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
103 source_authentication_get_property (GObject *object,
108 switch (property_id) {
110 g_value_take_string (
112 e_source_authentication_dup_host (
113 E_SOURCE_AUTHENTICATION (object)));
117 g_value_take_string (
119 e_source_authentication_dup_method (
120 E_SOURCE_AUTHENTICATION (object)));
126 e_source_authentication_get_port (
127 E_SOURCE_AUTHENTICATION (object)));
131 g_value_take_string (
133 e_source_authentication_dup_user (
134 E_SOURCE_AUTHENTICATION (object)));
138 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
142 source_authentication_finalize (GObject *object)
144 ESourceAuthenticationPrivate *priv;
146 priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (object);
148 g_mutex_free (priv->property_lock);
151 g_free (priv->method);
154 /* Chain up to parent's finalize() method. */
155 G_OBJECT_CLASS (e_source_authentication_parent_class)->finalize (object);
159 e_source_authentication_class_init (ESourceAuthenticationClass *class)
161 GObjectClass *object_class;
162 ESourceExtensionClass *extension_class;
164 g_type_class_add_private (class, sizeof (ESourceAuthenticationPrivate));
166 object_class = G_OBJECT_CLASS (class);
167 object_class->set_property = source_authentication_set_property;
168 object_class->get_property = source_authentication_get_property;
169 object_class->finalize = source_authentication_finalize;
171 extension_class = E_SOURCE_EXTENSION_CLASS (class);
172 extension_class->name = E_SOURCE_EXTENSION_AUTHENTICATION;
174 g_object_class_install_property (
177 g_param_spec_string (
180 "Host name for the remote account",
184 G_PARAM_STATIC_STRINGS |
185 E_SOURCE_PARAM_SETTING));
187 g_object_class_install_property (
190 g_param_spec_string (
193 "Authentication method",
197 G_PARAM_STATIC_STRINGS |
198 E_SOURCE_PARAM_SETTING));
200 g_object_class_install_property (
206 "Port number for the remote account",
210 G_PARAM_STATIC_STRINGS |
211 E_SOURCE_PARAM_SETTING));
213 g_object_class_install_property (
216 g_param_spec_string (
219 "User name for the remote account",
223 G_PARAM_STATIC_STRINGS |
224 E_SOURCE_PARAM_SETTING));
228 e_source_authentication_init (ESourceAuthentication *extension)
230 extension->priv = E_SOURCE_AUTHENTICATION_GET_PRIVATE (extension);
231 extension->priv->property_lock = g_mutex_new ();
235 * e_source_authentication_required:
236 * @extension: an #ESourceAuthentication
238 * This is a convenience function which returns whether authentication
239 * is required at all, regardless of the method used. This relies on
240 * the convention of setting #ESourceAuthentication:method to "none"
241 * when authentication is <emphasis>not</emphasis> required.
243 * Returns: whether authentication is required at all
248 e_source_authentication_required (ESourceAuthentication *extension)
252 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), FALSE);
254 method = e_source_authentication_get_method (extension);
255 g_return_val_if_fail (method != NULL && *method != '\0', FALSE);
257 return (g_strcmp0 (method, "none") != 0);
261 * e_source_authentication_get_host:
262 * @extension: an #ESourceAuthentication
264 * Returns the host name used to authenticate to a remote account.
266 * Returns: the host name of a remote account
271 e_source_authentication_get_host (ESourceAuthentication *extension)
273 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
275 return extension->priv->host;
279 * e_source_authentication_dup_host:
280 * @extension: an #ESourceAuthentication
282 * Thread-safe variation of e_source_authentication_get_host().
283 * Use this function when accessing @extension from multiple threads.
285 * The returned string should be freed with g_free() when no longer needed.
287 * Returns: a newly-allocated copy of #ESourceAuthentication:host
292 e_source_authentication_dup_host (ESourceAuthentication *extension)
294 const gchar *protected;
297 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
299 g_mutex_lock (extension->priv->property_lock);
301 protected = e_source_authentication_get_host (extension);
302 duplicate = g_strdup (protected);
304 g_mutex_unlock (extension->priv->property_lock);
310 * e_source_authentication_set_host:
311 * @extension: an #ESourceAuthentication
312 * @host: (allow-none): a host name, or %NULL
314 * Sets the host name used to authenticate to a remote account.
316 * The internal copy of @host is automatically stripped of leading and
317 * trailing whitespace. If the resulting string is empty, %NULL is set
323 e_source_authentication_set_host (ESourceAuthentication *extension,
326 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
328 g_mutex_lock (extension->priv->property_lock);
330 g_free (extension->priv->host);
331 extension->priv->host = e_util_strdup_strip (host);
333 g_mutex_unlock (extension->priv->property_lock);
335 g_object_notify (G_OBJECT (extension), "host");
339 * e_source_authentication_get_method:
340 * @extension: an #ESourceAuthentication
342 * Returns the authentication method for a remote account. There are
343 * no pre-defined method names; backends are free to set this however
344 * they wish. If authentication is not required for a remote account,
345 * the convention is to set #ESourceAuthentication:method to "none".
347 * Returns: the authentication method for a remote account
352 e_source_authentication_get_method (ESourceAuthentication *extension)
354 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
356 return extension->priv->method;
360 * e_source_authentication_dup_method:
361 * @extension: an #ESourceAuthentication
363 * Thread-safe variation of e_source_authentication_get_method().
364 * Use this function when accessing @extension from multiple threads.
366 * The returned string should be freed with g_free() when no longer needed.
368 * Returns: a newly-allocated copy of #ESourceAuthentication:method
373 e_source_authentication_dup_method (ESourceAuthentication *extension)
375 const gchar *protected;
378 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
380 g_mutex_lock (extension->priv->property_lock);
382 protected = e_source_authentication_get_method (extension);
383 duplicate = g_strdup (protected);
385 g_mutex_unlock (extension->priv->property_lock);
391 * e_source_authentication_set_method:
392 * @extension: an #ESourceAuthentication
393 * @method: (allow-none): authentication method, or %NULL
395 * Sets the authentication method for a remote account. There are no
396 * pre-defined method names; backends are free to set this however they
397 * wish. If authentication is not required for a remote account, the
398 * convention is to set the method to "none". In keeping with that
399 * convention, #ESourceAuthentication:method will be set to "none" if
400 * @method is %NULL or an empty string.
405 e_source_authentication_set_method (ESourceAuthentication *extension,
408 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
410 g_mutex_lock (extension->priv->property_lock);
412 g_free (extension->priv->method);
413 extension->priv->method = e_util_strdup_strip (method);
415 if (extension->priv->method == NULL)
416 extension->priv->method = g_strdup ("none");
418 g_mutex_unlock (extension->priv->property_lock);
420 g_object_notify (G_OBJECT (extension), "method");
424 * e_source_authentication_get_port:
425 * @extension: an #ESourceAuthentication
427 * Returns the port number used to authenticate to a remote account.
429 * Returns: the port number of a remote account
434 e_source_authentication_get_port (ESourceAuthentication *extension)
436 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), 0);
438 return extension->priv->port;
442 * e_source_authentication_set_port:
443 * @extension: an #ESourceAuthentication
444 * @port: a port number
446 * Sets the port number used to authenticate to a remote account.
451 e_source_authentication_set_port (ESourceAuthentication *extension,
454 g_return_if_fail (E_SOURCE_AUTHENTICATION (extension));
456 extension->priv->port = port;
458 g_object_notify (G_OBJECT (extension), "port");
462 * e_source_authentication_get_user:
463 * @extension: an #ESourceAuthentication
465 * Returns the user name used to authenticate to a remote account.
467 * Returns: the user name of a remote account
472 e_source_authentication_get_user (ESourceAuthentication *extension)
474 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
476 return extension->priv->user;
480 * e_source_authentication_dup_user:
481 * @extension: an #ESourceAuthentication
483 * Thread-safe variation of e_source_authentication_get_user().
484 * Use this function when accessing @extension from multiple threads.
486 * The returned string should be freed with g_free() when no longer needed.
488 * Returns: a newly-allocated copy of #ESourceAuthentication:user
493 e_source_authentication_dup_user (ESourceAuthentication *extension)
495 const gchar *protected;
498 g_return_val_if_fail (E_IS_SOURCE_AUTHENTICATION (extension), NULL);
500 g_mutex_lock (extension->priv->property_lock);
502 protected = e_source_authentication_get_user (extension);
503 duplicate = g_strdup (protected);
505 g_mutex_unlock (extension->priv->property_lock);
511 * e_source_authentication_set_user:
512 * @extension: an #ESourceAuthentication
513 * @user: (allow-none): a user name, or %NULL
515 * Sets the user name used to authenticate to a remote account.
517 * The internal copy of @user is automatically stripped of leading and
518 * trailing whitespace. If the resulting string is empty, %NULL is set
524 e_source_authentication_set_user (ESourceAuthentication *extension,
527 g_return_if_fail (E_IS_SOURCE_AUTHENTICATION (extension));
529 g_mutex_lock (extension->priv->property_lock);
531 g_free (extension->priv->user);
532 extension->priv->user = e_util_strdup_strip (user);
534 g_mutex_unlock (extension->priv->property_lock);
536 g_object_notify (G_OBJECT (extension), "user");