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-security
21 * @include: libedataserver/e-source-security.h
22 * @short_description: #ESource extension for security settings
24 * The #ESourceSecurity extension tracks settings for establishing a
25 * secure connection with a remote server.
27 * Access the extension as follows:
30 * #include <libedataserver/e-source-security.h>
32 * ESourceSecurity *extension;
34 * extension = e_source_get_extension (source, E_SOURCE_EXTENSION_SECURITY);
38 #include "e-source-security.h"
40 #include <libedataserver/e-data-server-util.h>
42 #define E_SOURCE_SECURITY_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE \
44 ((obj), E_TYPE_SOURCE_SECURITY, ESourceSecurityPrivate))
46 #define SECURE_METHOD "tls"
48 struct _ESourceSecurityPrivate {
49 GMutex *property_lock;
62 E_TYPE_SOURCE_EXTENSION)
65 source_security_set_property (GObject *object,
70 switch (property_id) {
72 e_source_security_set_method (
73 E_SOURCE_SECURITY (object),
74 g_value_get_string (value));
78 e_source_security_set_secure (
79 E_SOURCE_SECURITY (object),
80 g_value_get_boolean (value));
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 source_security_get_property (GObject *object,
93 switch (property_id) {
97 e_source_security_dup_method (
98 E_SOURCE_SECURITY (object)));
102 g_value_set_boolean (
104 e_source_security_get_secure (
105 E_SOURCE_SECURITY (object)));
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
113 source_security_finalize (GObject *object)
115 ESourceSecurityPrivate *priv;
117 priv = E_SOURCE_SECURITY_GET_PRIVATE (object);
119 g_mutex_free (priv->property_lock);
121 g_free (priv->method);
123 /* Chain up to parent's finalize() method. */
124 G_OBJECT_CLASS (e_source_security_parent_class)->finalize (object);
128 e_source_security_class_init (ESourceSecurityClass *class)
130 GObjectClass *object_class;
131 ESourceExtensionClass *extension_class;
133 g_type_class_add_private (class, sizeof (ESourceSecurityPrivate));
135 object_class = G_OBJECT_CLASS (class);
136 object_class->set_property = source_security_set_property;
137 object_class->get_property = source_security_get_property;
138 object_class->finalize = source_security_finalize;
140 extension_class = E_SOURCE_EXTENSION_CLASS (class);
141 extension_class->name = E_SOURCE_EXTENSION_SECURITY;
143 g_object_class_install_property (
146 g_param_spec_string (
153 G_PARAM_STATIC_STRINGS |
154 E_SOURCE_PARAM_SETTING));
156 g_object_class_install_property (
159 g_param_spec_boolean (
162 "Secure the network connection",
165 G_PARAM_STATIC_STRINGS));
169 e_source_security_init (ESourceSecurity *extension)
171 extension->priv = E_SOURCE_SECURITY_GET_PRIVATE (extension);
172 extension->priv->property_lock = g_mutex_new ();
176 * e_source_security_get_method:
177 * @extension: an #ESourceSecurity
179 * Returns the method used to establish a secure network connection to a
180 * remote account. There are no pre-defined method names; backends are
181 * free to set this however they wish. If a secure connection is not
182 * desired, the convention is to set #ESourceSecurity:method to "none".
184 * Returns: the method used to establish a secure network connection
189 e_source_security_get_method (ESourceSecurity *extension)
191 g_return_val_if_fail (E_IS_SOURCE_SECURITY (extension), NULL);
193 return extension->priv->method;
197 * e_source_security_dup_method:
198 * @extension: an #ESourceSecurity
200 * Thread-safe variation of e_source_security_get_method().
201 * Use this function when accessing @extension from multiple threads.
203 * The returned string should be freed with g_free() when no longer needed.
205 * Returns: a newly-allocated copy of #ESourceSecurity:method
210 e_source_security_dup_method (ESourceSecurity *extension)
212 const gchar *protected;
215 g_return_val_if_fail (E_IS_SOURCE_SECURITY (extension), NULL);
217 g_mutex_lock (extension->priv->property_lock);
219 protected = e_source_security_get_method (extension);
220 duplicate = g_strdup (protected);
222 g_mutex_unlock (extension->priv->property_lock);
228 * e_source_security_set_method:
229 * @extension: an #ESourceSecurity
230 * @method: (allow-none): security method, or %NULL
232 * Sets the method used to establish a secure network connection to a
233 * remote account. There are no pre-defined method names; backends are
234 * free to set this however they wish. If a secure connection is not
235 * desired, the convention is to set #ESourceSecurity:method to "none".
236 * In keeping with that convention, #ESourceSecurity:method will be set
237 * to "none" if @method is %NULL or an empty string.
242 e_source_security_set_method (ESourceSecurity *extension,
247 g_return_if_fail (E_IS_SOURCE_SECURITY (extension));
249 g_mutex_lock (extension->priv->property_lock);
251 g_free (extension->priv->method);
252 extension->priv->method = e_util_strdup_strip (method);
254 if (extension->priv->method == NULL)
255 extension->priv->method = g_strdup ("none");
257 g_mutex_unlock (extension->priv->property_lock);
259 object = G_OBJECT (extension);
260 g_object_freeze_notify (object);
261 g_object_notify (object, "method");
262 g_object_notify (object, "secure");
263 g_object_thaw_notify (object);
267 * e_source_security_get_secure:
268 * @extension: an #ESourceSecurity
270 * This is a convenience function which returns whether a secure network
271 * connection is desired, regardless of the method used. This relies on
272 * the convention of setting #ESourceSecurity:method to "none" when a
273 * secure network connection is <emphasis>not</emphasis> desired.
275 * Returns: whether a secure network connection is desired
280 e_source_security_get_secure (ESourceSecurity *extension)
284 g_return_val_if_fail (E_IS_SOURCE_SECURITY (extension), FALSE);
286 method = e_source_security_get_method (extension);
287 g_return_val_if_fail (method != NULL, FALSE);
289 return (g_strcmp0 (method, "none") != 0);
293 * e_source_security_set_secure:
294 * @extension: an #ESourceSecurity
295 * @secure: whether a secure network connection is desired
297 * This function provides a simpler way to set #ESourceSecurity:method
298 * when using a secure network connection is a yes or no option and the
299 * exact method name is unimportant. If @secure is %FALSE, the
300 * #ESourceSecurity:method property is set to "none". If @secure is
301 * %TRUE, the function assumes the backend will use Transport Layer
302 * Security and sets the #ESourceSecurity:method property to "tls".
307 e_source_security_set_secure (ESourceSecurity *extension,
312 g_return_if_fail (E_IS_SOURCE_SECURITY (extension));
314 method = secure ? SECURE_METHOD : "none";
315 e_source_security_set_method (extension, method);