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-openpgp
21 * @include: libedataserver/e-source-openpgp.h
22 * @short_description: #ESource extension for OpenPGP settings
24 * The #ESourceOpenPGP extension tracks OpenPGP (RFC 4880) settings to be
25 * applied to outgoing mail messages.
27 * Access the extension as follows:
30 * #include <libedataserver/e-source-openpgp.h>
32 * ESourceOpenPGP *extension;
34 * extension = e_source_get_extension (source, E_SOURCE_EXTENSION_OPENPGP);
38 #include "e-source-openpgp.h"
40 #include <libedataserver/e-data-server-util.h>
42 #define E_SOURCE_OPENPGP_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE \
44 ((obj), E_TYPE_SOURCE_OPENPGP, ESourceOpenPGPPrivate))
46 struct _ESourceOpenPGPPrivate {
47 GMutex *property_lock;
49 gchar *signing_algorithm;
51 gboolean always_trust;
52 gboolean encrypt_to_self;
53 gboolean sign_by_default;
61 PROP_SIGNING_ALGORITHM,
68 E_TYPE_SOURCE_EXTENSION)
71 source_openpgp_set_property (GObject *object,
76 switch (property_id) {
77 case PROP_ALWAYS_TRUST:
78 e_source_openpgp_set_always_trust (
79 E_SOURCE_OPENPGP (object),
80 g_value_get_boolean (value));
83 case PROP_ENCRYPT_TO_SELF:
84 e_source_openpgp_set_encrypt_to_self (
85 E_SOURCE_OPENPGP (object),
86 g_value_get_boolean (value));
90 e_source_openpgp_set_key_id (
91 E_SOURCE_OPENPGP (object),
92 g_value_get_string (value));
95 case PROP_SIGNING_ALGORITHM:
96 e_source_openpgp_set_signing_algorithm (
97 E_SOURCE_OPENPGP (object),
98 g_value_get_string (value));
101 case PROP_SIGN_BY_DEFAULT:
102 e_source_openpgp_set_sign_by_default (
103 E_SOURCE_OPENPGP (object),
104 g_value_get_boolean (value));
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 source_openpgp_get_property (GObject *object,
117 switch (property_id) {
118 case PROP_ALWAYS_TRUST:
119 g_value_set_boolean (
121 e_source_openpgp_get_always_trust (
122 E_SOURCE_OPENPGP (object)));
125 case PROP_ENCRYPT_TO_SELF:
126 g_value_set_boolean (
128 e_source_openpgp_get_encrypt_to_self (
129 E_SOURCE_OPENPGP (object)));
133 g_value_take_string (
135 e_source_openpgp_dup_key_id (
136 E_SOURCE_OPENPGP (object)));
139 case PROP_SIGNING_ALGORITHM:
140 g_value_take_string (
142 e_source_openpgp_dup_signing_algorithm (
143 E_SOURCE_OPENPGP (object)));
146 case PROP_SIGN_BY_DEFAULT:
147 g_value_set_boolean (
149 e_source_openpgp_get_sign_by_default (
150 E_SOURCE_OPENPGP (object)));
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 source_openpgp_finalize (GObject *object)
160 ESourceOpenPGPPrivate *priv;
162 priv = E_SOURCE_OPENPGP_GET_PRIVATE (object);
164 g_mutex_free (priv->property_lock);
166 g_free (priv->key_id);
167 g_free (priv->signing_algorithm);
169 /* Chain up to parent's finalize() method. */
170 G_OBJECT_CLASS (e_source_openpgp_parent_class)->finalize (object);
174 e_source_openpgp_class_init (ESourceOpenPGPClass *class)
176 GObjectClass *object_class;
177 ESourceExtensionClass *extension_class;
179 g_type_class_add_private (class, sizeof (ESourceOpenPGPPrivate));
181 object_class = G_OBJECT_CLASS (class);
182 object_class->set_property = source_openpgp_set_property;
183 object_class->get_property = source_openpgp_get_property;
184 object_class->finalize = source_openpgp_finalize;
186 extension_class = E_SOURCE_EXTENSION_CLASS (class);
187 extension_class->name = E_SOURCE_EXTENSION_OPENPGP;
189 g_object_class_install_property (
192 g_param_spec_boolean (
195 "Always trust keys in my keyring",
199 G_PARAM_STATIC_STRINGS |
200 E_SOURCE_PARAM_SETTING));
202 g_object_class_install_property (
204 PROP_ENCRYPT_TO_SELF,
205 g_param_spec_boolean (
208 "Always encrypt to myself",
212 G_PARAM_STATIC_STRINGS |
213 E_SOURCE_PARAM_SETTING));
215 g_object_class_install_property (
218 g_param_spec_string (
225 G_PARAM_STATIC_STRINGS |
226 E_SOURCE_PARAM_SETTING));
228 g_object_class_install_property (
230 PROP_SIGNING_ALGORITHM,
231 g_param_spec_string (
234 "Hash algorithm used to sign messages",
238 G_PARAM_STATIC_STRINGS |
239 E_SOURCE_PARAM_SETTING));
241 g_object_class_install_property (
243 PROP_SIGN_BY_DEFAULT,
244 g_param_spec_boolean (
247 "Sign outgoing messages by default",
251 G_PARAM_STATIC_STRINGS |
252 E_SOURCE_PARAM_SETTING));
256 e_source_openpgp_init (ESourceOpenPGP *extension)
258 extension->priv = E_SOURCE_OPENPGP_GET_PRIVATE (extension);
259 extension->priv->property_lock = g_mutex_new ();
263 * e_source_openpgp_get_always_trust:
264 * @extension: an #ESourceOpenPGP
266 * Returns whether to skip key validation and assume that used keys are
267 * always fully trusted.
269 * Returns: whether used keys are always fully trusted
274 e_source_openpgp_get_always_trust (ESourceOpenPGP *extension)
276 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
278 return extension->priv->always_trust;
282 * e_source_openpgp_set_always_trust:
283 * @extension: an #ESourceOpenPGP
284 * @always_trust: whether used keys are always fully trusted
286 * Sets whether to skip key validation and assume that used keys are
287 * always fully trusted.
292 e_source_openpgp_set_always_trust (ESourceOpenPGP *extension,
293 gboolean always_trust)
295 g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
297 extension->priv->always_trust = always_trust;
299 g_object_notify (G_OBJECT (extension), "always-trust");
303 * e_source_openpgp_get_encrypt_to_self:
304 * @extension: an #ESourceOpenPGP
306 * Returns whether to "encrypt-to-self" when sending encrypted messages.
308 * Returns: whether to "encrypt-to-self"
313 e_source_openpgp_get_encrypt_to_self (ESourceOpenPGP *extension)
315 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
317 return extension->priv->encrypt_to_self;
321 * e_source_openpgp_set_encrypt_to_self:
322 * @extension: an #ESourceOpenPGP
323 * @encrypt_to_self: whether to "encrypt-to-self"
325 * Sets whether to "encrypt-to-self" when sending encrypted messages.
330 e_source_openpgp_set_encrypt_to_self (ESourceOpenPGP *extension,
331 gboolean encrypt_to_self)
333 g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
335 extension->priv->encrypt_to_self = encrypt_to_self;
337 g_object_notify (G_OBJECT (extension), "encrypt-to-self");
341 * e_source_openpgp_get_key_id:
342 * @extension: an #ESourceOpenPGP
344 * Returns the OpenPGP key ID used to sign and encrypt messages.
346 * Returns: the key ID used to sign and encrypt messages
351 e_source_openpgp_get_key_id (ESourceOpenPGP *extension)
353 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
355 return extension->priv->key_id;
359 * e_source_openpgp_dup_key_id:
360 * @extension: an #ESourceOpenPGP
362 * Thread-safe variation of e_source_openpgp_get_key_id().
363 * Use this function when accessing @extension from multiple threads.
365 * The returned string should be freed with g_free() when no longer needed.
367 * Returns: a newly-allocated copy of #ESourceOpenPGP:key-id
372 e_source_openpgp_dup_key_id (ESourceOpenPGP *extension)
374 const gchar *protected;
377 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
379 g_mutex_lock (extension->priv->property_lock);
381 protected = e_source_openpgp_get_key_id (extension);
382 duplicate = g_strdup (protected);
384 g_mutex_unlock (extension->priv->property_lock);
390 * e_source_openpgp_set_key_id:
391 * @extension: an #ESourceOpenPGP
392 * @key_id: the key ID used to sign and encrypt messages
394 * Sets the OpenPGP key ID used to sign and encrypt messages.
396 * The internal copy of @key_id is automatically stripped of leading and
397 * trailing whitespace. If the resulting string is empty, %NULL is set
403 e_source_openpgp_set_key_id (ESourceOpenPGP *extension,
406 g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
408 g_mutex_lock (extension->priv->property_lock);
410 g_free (extension->priv->key_id);
411 extension->priv->key_id = e_util_strdup_strip (key_id);
413 g_mutex_unlock (extension->priv->property_lock);
415 g_object_notify (G_OBJECT (extension), "key-id");
419 * e_source_openpgp_get_signing_algorithm:
420 * @extension: an #ESourceOpenPGP
422 * Returns the name of the hash algorithm used to digitally sign outgoing
425 * Returns: the signing algorithm for outgoing messages
430 e_source_openpgp_get_signing_algorithm (ESourceOpenPGP *extension)
432 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
434 return extension->priv->signing_algorithm;
438 * e_source_openpgp_dup_signing_algorithm:
439 * @extension: an #ESourceOpenPGP
441 * Thread-safe variation of e_source_openpgp_get_signing_algorithm().
442 * Use this function when accessing @extension from multiple threads.
444 * The returned string should be freed with g_free() when no longer needed.
446 * Returns: a newly-allocated copy of #ESourceOpenPGP:signing-algorithm
451 e_source_openpgp_dup_signing_algorithm (ESourceOpenPGP *extension)
453 const gchar *protected;
456 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
458 g_mutex_lock (extension->priv->property_lock);
460 protected = e_source_openpgp_get_signing_algorithm (extension);
461 duplicate = g_strdup (protected);
463 g_mutex_unlock (extension->priv->property_lock);
469 * e_source_openpgp_set_signing_algorithm:
470 * @extension: an #ESourceOpenPGP
471 * @signing_algorithm: the signing algorithm for outgoing messages
473 * Sets the name of the hash algorithm used to digitally sign outgoing
476 * The internal copy of @signing_algorithm is automatically stripped of
477 * leading and trailing whitespace. If the resulting string is empty,
478 * %NULL is set instead.
483 e_source_openpgp_set_signing_algorithm (ESourceOpenPGP *extension,
484 const gchar *signing_algorithm)
486 g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
488 g_mutex_lock (extension->priv->property_lock);
490 g_free (extension->priv->signing_algorithm);
491 extension->priv->signing_algorithm =
492 e_util_strdup_strip (signing_algorithm);
494 g_mutex_unlock (extension->priv->property_lock);
496 g_object_notify (G_OBJECT (extension), "signing-algorithm");
500 * e_source_openpgp_get_sign_by_default:
501 * @extension: an #ESourceOpenPGP
503 * Returns whether to digitally sign outgoing messages by default using
504 * OpenPGP-compliant software such as GNU Privacy Guard (GnuPG).
506 * Returns: whether to sign outgoing messages by default
511 e_source_openpgp_get_sign_by_default (ESourceOpenPGP *extension)
513 g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
515 return extension->priv->sign_by_default;
519 * e_source_openpgp_set_sign_by_default:
520 * @extension: an #ESourceOpenPGP
521 * @sign_by_default: whether to sign outgoing messages by default
523 * Sets whether to digitally sign outgoing messages by default using
524 * OpenPGP-compliant software such as GNU Privacy Guard (GnuPG).
529 e_source_openpgp_set_sign_by_default (ESourceOpenPGP *extension,
530 gboolean sign_by_default)
532 g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
534 extension->priv->sign_by_default = sign_by_default;
536 g_object_notify (G_OBJECT (extension), "sign-by-default");