Add new ESource classes.
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-openpgp.c
1 /*
2  * e-source-openpgp.c
3  *
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.
8  *
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.
13  *
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/>
16  *
17  */
18
19 /**
20  * SECTION: e-source-openpgp
21  * @include: libedataserver/e-source-openpgp.h
22  * @short_description: #ESource extension for OpenPGP settings
23  *
24  * The #ESourceOpenPGP extension tracks OpenPGP (RFC 4880) settings to be
25  * applied to outgoing mail messages.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/e-source-openpgp.h>
31  *
32  *   ESourceOpenPGP *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_OPENPGP);
35  * ]|
36  **/
37
38 #include "e-source-openpgp.h"
39
40 #include <libedataserver/e-data-server-util.h>
41
42 #define E_SOURCE_OPENPGP_GET_PRIVATE(obj) \
43         (G_TYPE_INSTANCE_GET_PRIVATE \
44         ((obj), E_TYPE_SOURCE_OPENPGP, ESourceOpenPGPPrivate))
45
46 struct _ESourceOpenPGPPrivate {
47         GMutex *property_lock;
48         gchar *key_id;
49         gchar *signing_algorithm;
50
51         gboolean always_trust;
52         gboolean encrypt_to_self;
53         gboolean sign_by_default;
54 };
55
56 enum {
57         PROP_0,
58         PROP_ALWAYS_TRUST,
59         PROP_ENCRYPT_TO_SELF,
60         PROP_KEY_ID,
61         PROP_SIGNING_ALGORITHM,
62         PROP_SIGN_BY_DEFAULT
63 };
64
65 G_DEFINE_TYPE (
66         ESourceOpenPGP,
67         e_source_openpgp,
68         E_TYPE_SOURCE_EXTENSION)
69
70 static void
71 source_openpgp_set_property (GObject *object,
72                              guint property_id,
73                              const GValue *value,
74                              GParamSpec *pspec)
75 {
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));
81                         return;
82
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));
87                         return;
88
89                 case PROP_KEY_ID:
90                         e_source_openpgp_set_key_id (
91                                 E_SOURCE_OPENPGP (object),
92                                 g_value_get_string (value));
93                         return;
94
95                 case PROP_SIGNING_ALGORITHM:
96                         e_source_openpgp_set_signing_algorithm (
97                                 E_SOURCE_OPENPGP (object),
98                                 g_value_get_string (value));
99                         return;
100
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));
105                         return;
106         }
107
108         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 }
110
111 static void
112 source_openpgp_get_property (GObject *object,
113                              guint property_id,
114                              GValue *value,
115                              GParamSpec *pspec)
116 {
117         switch (property_id) {
118                 case PROP_ALWAYS_TRUST:
119                         g_value_set_boolean (
120                                 value,
121                                 e_source_openpgp_get_always_trust (
122                                 E_SOURCE_OPENPGP (object)));
123                         return;
124
125                 case PROP_ENCRYPT_TO_SELF:
126                         g_value_set_boolean (
127                                 value,
128                                 e_source_openpgp_get_encrypt_to_self (
129                                 E_SOURCE_OPENPGP (object)));
130                         return;
131
132                 case PROP_KEY_ID:
133                         g_value_take_string (
134                                 value,
135                                 e_source_openpgp_dup_key_id (
136                                 E_SOURCE_OPENPGP (object)));
137                         return;
138
139                 case PROP_SIGNING_ALGORITHM:
140                         g_value_take_string (
141                                 value,
142                                 e_source_openpgp_dup_signing_algorithm (
143                                 E_SOURCE_OPENPGP (object)));
144                         return;
145
146                 case PROP_SIGN_BY_DEFAULT:
147                         g_value_set_boolean (
148                                 value,
149                                 e_source_openpgp_get_sign_by_default (
150                                 E_SOURCE_OPENPGP (object)));
151                         return;
152         }
153
154         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
155 }
156
157 static void
158 source_openpgp_finalize (GObject *object)
159 {
160         ESourceOpenPGPPrivate *priv;
161
162         priv = E_SOURCE_OPENPGP_GET_PRIVATE (object);
163
164         g_mutex_free (priv->property_lock);
165
166         g_free (priv->key_id);
167         g_free (priv->signing_algorithm);
168
169         /* Chain up to parent's finalize() method. */
170         G_OBJECT_CLASS (e_source_openpgp_parent_class)->finalize (object);
171 }
172
173 static void
174 e_source_openpgp_class_init (ESourceOpenPGPClass *class)
175 {
176         GObjectClass *object_class;
177         ESourceExtensionClass *extension_class;
178
179         g_type_class_add_private (class, sizeof (ESourceOpenPGPPrivate));
180
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;
185
186         extension_class = E_SOURCE_EXTENSION_CLASS (class);
187         extension_class->name = E_SOURCE_EXTENSION_OPENPGP;
188
189         g_object_class_install_property (
190                 object_class,
191                 PROP_ALWAYS_TRUST,
192                 g_param_spec_boolean (
193                         "always-trust",
194                         "Always Trust",
195                         "Always trust keys in my keyring",
196                         FALSE,
197                         G_PARAM_READWRITE |
198                         G_PARAM_CONSTRUCT |
199                         G_PARAM_STATIC_STRINGS |
200                         E_SOURCE_PARAM_SETTING));
201
202         g_object_class_install_property (
203                 object_class,
204                 PROP_ENCRYPT_TO_SELF,
205                 g_param_spec_boolean (
206                         "encrypt-to-self",
207                         "Encrypt To Self",
208                         "Always encrypt to myself",
209                         TRUE,
210                         G_PARAM_READWRITE |
211                         G_PARAM_CONSTRUCT |
212                         G_PARAM_STATIC_STRINGS |
213                         E_SOURCE_PARAM_SETTING));
214
215         g_object_class_install_property (
216                 object_class,
217                 PROP_KEY_ID,
218                 g_param_spec_string (
219                         "key-id",
220                         "Key ID",
221                         "PGP/GPG Key ID",
222                         NULL,
223                         G_PARAM_READWRITE |
224                         G_PARAM_CONSTRUCT |
225                         G_PARAM_STATIC_STRINGS |
226                         E_SOURCE_PARAM_SETTING));
227
228         g_object_class_install_property (
229                 object_class,
230                 PROP_SIGNING_ALGORITHM,
231                 g_param_spec_string (
232                         "signing-algorithm",
233                         "Signing Algorithm",
234                         "Hash algorithm used to sign messages",
235                         NULL,
236                         G_PARAM_READWRITE |
237                         G_PARAM_CONSTRUCT |
238                         G_PARAM_STATIC_STRINGS |
239                         E_SOURCE_PARAM_SETTING));
240
241         g_object_class_install_property (
242                 object_class,
243                 PROP_SIGN_BY_DEFAULT,
244                 g_param_spec_boolean (
245                         "sign-by-default",
246                         "Sign By Default",
247                         "Sign outgoing messages by default",
248                         FALSE,
249                         G_PARAM_READWRITE |
250                         G_PARAM_CONSTRUCT |
251                         G_PARAM_STATIC_STRINGS |
252                         E_SOURCE_PARAM_SETTING));
253 }
254
255 static void
256 e_source_openpgp_init (ESourceOpenPGP *extension)
257 {
258         extension->priv = E_SOURCE_OPENPGP_GET_PRIVATE (extension);
259         extension->priv->property_lock = g_mutex_new ();
260 }
261
262 /**
263  * e_source_openpgp_get_always_trust:
264  * @extension: an #ESourceOpenPGP
265  *
266  * Returns whether to skip key validation and assume that used keys are
267  * always fully trusted.
268  *
269  * Returns: whether used keys are always fully trusted
270  *
271  * Since: 3.6
272  **/
273 gboolean
274 e_source_openpgp_get_always_trust (ESourceOpenPGP *extension)
275 {
276         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
277
278         return extension->priv->always_trust;
279 }
280
281 /**
282  * e_source_openpgp_set_always_trust:
283  * @extension: an #ESourceOpenPGP
284  * @always_trust: whether used keys are always fully trusted
285  *
286  * Sets whether to skip key validation and assume that used keys are
287  * always fully trusted.
288  *
289  * Since: 3.6
290  **/
291 void
292 e_source_openpgp_set_always_trust (ESourceOpenPGP *extension,
293                                    gboolean always_trust)
294 {
295         g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
296
297         extension->priv->always_trust = always_trust;
298
299         g_object_notify (G_OBJECT (extension), "always-trust");
300 }
301
302 /**
303  * e_source_openpgp_get_encrypt_to_self:
304  * @extension: an #ESourceOpenPGP
305  *
306  * Returns whether to "encrypt-to-self" when sending encrypted messages.
307  *
308  * Returns: whether to "encrypt-to-self"
309  *
310  * Since: 3.6
311  **/
312 gboolean
313 e_source_openpgp_get_encrypt_to_self (ESourceOpenPGP *extension)
314 {
315         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
316
317         return extension->priv->encrypt_to_self;
318 }
319
320 /**
321  * e_source_openpgp_set_encrypt_to_self:
322  * @extension: an #ESourceOpenPGP
323  * @encrypt_to_self: whether to "encrypt-to-self"
324  *
325  * Sets whether to "encrypt-to-self" when sending encrypted messages.
326  *
327  * Since: 3.6
328  **/
329 void
330 e_source_openpgp_set_encrypt_to_self (ESourceOpenPGP *extension,
331                                       gboolean encrypt_to_self)
332 {
333         g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
334
335         extension->priv->encrypt_to_self = encrypt_to_self;
336
337         g_object_notify (G_OBJECT (extension), "encrypt-to-self");
338 }
339
340 /**
341  * e_source_openpgp_get_key_id:
342  * @extension: an #ESourceOpenPGP
343  *
344  * Returns the OpenPGP key ID used to sign and encrypt messages.
345  *
346  * Returns: the key ID used to sign and encrypt messages
347  *
348  * Since: 3.6
349  **/
350 const gchar *
351 e_source_openpgp_get_key_id (ESourceOpenPGP *extension)
352 {
353         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
354
355         return extension->priv->key_id;
356 }
357
358 /**
359  * e_source_openpgp_dup_key_id:
360  * @extension: an #ESourceOpenPGP
361  *
362  * Thread-safe variation of e_source_openpgp_get_key_id().
363  * Use this function when accessing @extension from multiple threads.
364  *
365  * The returned string should be freed with g_free() when no longer needed.
366  *
367  * Returns: a newly-allocated copy of #ESourceOpenPGP:key-id
368  *
369  * Since: 3.6
370  **/
371 gchar *
372 e_source_openpgp_dup_key_id (ESourceOpenPGP *extension)
373 {
374         const gchar *protected;
375         gchar *duplicate;
376
377         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
378
379         g_mutex_lock (extension->priv->property_lock);
380
381         protected = e_source_openpgp_get_key_id (extension);
382         duplicate = g_strdup (protected);
383
384         g_mutex_unlock (extension->priv->property_lock);
385
386         return duplicate;
387 }
388
389 /**
390  * e_source_openpgp_set_key_id:
391  * @extension: an #ESourceOpenPGP
392  * @key_id: the key ID used to sign and encrypt messages
393  *
394  * Sets the OpenPGP key ID used to sign and encrypt messages.
395  *
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
398  * instead.
399  *
400  * Since: 3.6
401  **/
402 void
403 e_source_openpgp_set_key_id (ESourceOpenPGP *extension,
404                              const gchar *key_id)
405 {
406         g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
407
408         g_mutex_lock (extension->priv->property_lock);
409
410         g_free (extension->priv->key_id);
411         extension->priv->key_id = e_util_strdup_strip (key_id);
412
413         g_mutex_unlock (extension->priv->property_lock);
414
415         g_object_notify (G_OBJECT (extension), "key-id");
416 }
417
418 /**
419  * e_source_openpgp_get_signing_algorithm:
420  * @extension: an #ESourceOpenPGP
421  *
422  * Returns the name of the hash algorithm used to digitally sign outgoing
423  * messages.
424  *
425  * Returns: the signing algorithm for outgoing messages
426  *
427  * Since: 3.6
428  **/
429 const gchar *
430 e_source_openpgp_get_signing_algorithm (ESourceOpenPGP *extension)
431 {
432         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
433
434         return extension->priv->signing_algorithm;
435 }
436
437 /**
438  * e_source_openpgp_dup_signing_algorithm:
439  * @extension: an #ESourceOpenPGP
440  *
441  * Thread-safe variation of e_source_openpgp_get_signing_algorithm().
442  * Use this function when accessing @extension from multiple threads.
443  *
444  * The returned string should be freed with g_free() when no longer needed.
445  *
446  * Returns: a newly-allocated copy of #ESourceOpenPGP:signing-algorithm
447  *
448  * Since: 3.6
449  **/
450 gchar *
451 e_source_openpgp_dup_signing_algorithm (ESourceOpenPGP *extension)
452 {
453         const gchar *protected;
454         gchar *duplicate;
455
456         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), NULL);
457
458         g_mutex_lock (extension->priv->property_lock);
459
460         protected = e_source_openpgp_get_signing_algorithm (extension);
461         duplicate = g_strdup (protected);
462
463         g_mutex_unlock (extension->priv->property_lock);
464
465         return duplicate;
466 }
467
468 /**
469  * e_source_openpgp_set_signing_algorithm:
470  * @extension: an #ESourceOpenPGP
471  * @signing_algorithm: the signing algorithm for outgoing messages
472  *
473  * Sets the name of the hash algorithm used to digitally sign outgoing
474  * messages.
475  *
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.
479  *
480  * Since: 3.6
481  **/
482 void
483 e_source_openpgp_set_signing_algorithm (ESourceOpenPGP *extension,
484                                         const gchar *signing_algorithm)
485 {
486         g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
487
488         g_mutex_lock (extension->priv->property_lock);
489
490         g_free (extension->priv->signing_algorithm);
491         extension->priv->signing_algorithm =
492                 e_util_strdup_strip (signing_algorithm);
493
494         g_mutex_unlock (extension->priv->property_lock);
495
496         g_object_notify (G_OBJECT (extension), "signing-algorithm");
497 }
498
499 /**
500  * e_source_openpgp_get_sign_by_default:
501  * @extension: an #ESourceOpenPGP
502  *
503  * Returns whether to digitally sign outgoing messages by default using
504  * OpenPGP-compliant software such as GNU Privacy Guard (GnuPG).
505  *
506  * Returns: whether to sign outgoing messages by default
507  *
508  * Since: 3.6
509  **/
510 gboolean
511 e_source_openpgp_get_sign_by_default (ESourceOpenPGP *extension)
512 {
513         g_return_val_if_fail (E_IS_SOURCE_OPENPGP (extension), FALSE);
514
515         return extension->priv->sign_by_default;
516 }
517
518 /**
519  * e_source_openpgp_set_sign_by_default:
520  * @extension: an #ESourceOpenPGP
521  * @sign_by_default: whether to sign outgoing messages by default
522  *
523  * Sets whether to digitally sign outgoing messages by default using
524  * OpenPGP-compliant software such as GNU Privacy Guard (GnuPG).
525  *
526  * Since: 3.6
527  **/
528 void
529 e_source_openpgp_set_sign_by_default (ESourceOpenPGP *extension,
530                                       gboolean sign_by_default)
531 {
532         g_return_if_fail (E_IS_SOURCE_OPENPGP (extension));
533
534         extension->priv->sign_by_default = sign_by_default;
535
536         g_object_notify (G_OBJECT (extension), "sign-by-default");
537 }
538