goa: Add missing linker flag (for real).
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-smime.c
1 /*
2  * e-source-smime.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-smime
21  * @include: libedataserver/libedataserver.h
22  * @short_description: #ESource extension for S/MIME settings
23  *
24  * The #ESourceSMIME extension tracks Secure/Multipurpose Internet Mail
25  * Extensions (S/MIME) settings to be applied to outgoing mail messages.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/libedataserver.h>
31  *
32  *   ESourceSMIME *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_SMIME);
35  * ]|
36  **/
37
38 #include "e-source-smime.h"
39
40 #include <libedataserver/e-data-server-util.h>
41
42 #define E_SOURCE_SMIME_GET_PRIVATE(obj) \
43         (G_TYPE_INSTANCE_GET_PRIVATE \
44         ((obj), E_TYPE_SOURCE_SMIME, ESourceSMIMEPrivate))
45
46 struct _ESourceSMIMEPrivate {
47         GMutex property_lock;
48         gchar *encryption_certificate;
49         gchar *signing_algorithm;
50         gchar *signing_certificate;
51
52         gboolean encrypt_by_default;
53         gboolean encrypt_to_self;
54         gboolean sign_by_default;
55 };
56
57 enum {
58         PROP_0,
59         PROP_ENCRYPTION_CERTIFICATE,
60         PROP_ENCRYPT_BY_DEFAULT,
61         PROP_ENCRYPT_TO_SELF,
62         PROP_SIGNING_ALGORITHM,
63         PROP_SIGNING_CERTIFICATE,
64         PROP_SIGN_BY_DEFAULT
65 };
66
67 G_DEFINE_TYPE (
68         ESourceSMIME,
69         e_source_smime,
70         E_TYPE_SOURCE_EXTENSION)
71
72 static void
73 source_smime_set_property (GObject *object,
74                            guint property_id,
75                            const GValue *value,
76                            GParamSpec *pspec)
77 {
78         switch (property_id) {
79                 case PROP_ENCRYPTION_CERTIFICATE:
80                         e_source_smime_set_encryption_certificate (
81                                 E_SOURCE_SMIME (object),
82                                 g_value_get_string (value));
83                         return;
84
85                 case PROP_ENCRYPT_BY_DEFAULT:
86                         e_source_smime_set_encrypt_by_default (
87                                 E_SOURCE_SMIME (object),
88                                 g_value_get_boolean (value));
89                         return;
90
91                 case PROP_ENCRYPT_TO_SELF:
92                         e_source_smime_set_encrypt_to_self (
93                                 E_SOURCE_SMIME (object),
94                                 g_value_get_boolean (value));
95                         return;
96
97                 case PROP_SIGNING_ALGORITHM:
98                         e_source_smime_set_signing_algorithm (
99                                 E_SOURCE_SMIME (object),
100                                 g_value_get_string (value));
101                         return;
102
103                 case PROP_SIGNING_CERTIFICATE:
104                         e_source_smime_set_signing_certificate (
105                                 E_SOURCE_SMIME (object),
106                                 g_value_get_string (value));
107                         return;
108
109                 case PROP_SIGN_BY_DEFAULT:
110                         e_source_smime_set_sign_by_default (
111                                 E_SOURCE_SMIME (object),
112                                 g_value_get_boolean (value));
113                         return;
114         }
115
116         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
117 }
118
119 static void
120 source_smime_get_property (GObject *object,
121                            guint property_id,
122                            GValue *value,
123                            GParamSpec *pspec)
124 {
125         switch (property_id) {
126                 case PROP_ENCRYPTION_CERTIFICATE:
127                         g_value_take_string (
128                                 value,
129                                 e_source_smime_dup_encryption_certificate (
130                                 E_SOURCE_SMIME (object)));
131                         return;
132
133                 case PROP_ENCRYPT_BY_DEFAULT:
134                         g_value_set_boolean (
135                                 value,
136                                 e_source_smime_get_encrypt_by_default (
137                                 E_SOURCE_SMIME (object)));
138                         return;
139
140                 case PROP_ENCRYPT_TO_SELF:
141                         g_value_set_boolean (
142                                 value,
143                                 e_source_smime_get_encrypt_to_self (
144                                 E_SOURCE_SMIME (object)));
145                         return;
146
147                 case PROP_SIGNING_ALGORITHM:
148                         g_value_take_string (
149                                 value,
150                                 e_source_smime_dup_signing_algorithm (
151                                 E_SOURCE_SMIME (object)));
152                         return;
153
154                 case PROP_SIGNING_CERTIFICATE:
155                         g_value_take_string (
156                                 value,
157                                 e_source_smime_dup_signing_certificate (
158                                 E_SOURCE_SMIME (object)));
159                         return;
160
161                 case PROP_SIGN_BY_DEFAULT:
162                         g_value_set_boolean (
163                                 value,
164                                 e_source_smime_get_sign_by_default (
165                                 E_SOURCE_SMIME (object)));
166                         return;
167         }
168
169         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
170 }
171
172 static void
173 source_smime_finalize (GObject *object)
174 {
175         ESourceSMIMEPrivate *priv;
176
177         priv = E_SOURCE_SMIME_GET_PRIVATE (object);
178
179         g_mutex_clear (&priv->property_lock);
180
181         g_free (priv->encryption_certificate);
182         g_free (priv->signing_algorithm);
183         g_free (priv->signing_certificate);
184
185         /* Chain up to parent's finalize() method. */
186         G_OBJECT_CLASS (e_source_smime_parent_class)->finalize (object);
187 }
188
189 static void
190 e_source_smime_class_init (ESourceSMIMEClass *class)
191 {
192         GObjectClass *object_class;
193         ESourceExtensionClass *extension_class;
194
195         g_type_class_add_private (class, sizeof (ESourceSMIMEPrivate));
196
197         object_class = G_OBJECT_CLASS (class);
198         object_class->set_property = source_smime_set_property;
199         object_class->get_property = source_smime_get_property;
200         object_class->finalize = source_smime_finalize;
201
202         extension_class = E_SOURCE_EXTENSION_CLASS (class);
203         extension_class->name = E_SOURCE_EXTENSION_SMIME;
204
205         g_object_class_install_property (
206                 object_class,
207                 PROP_ENCRYPTION_CERTIFICATE,
208                 g_param_spec_string (
209                         "encryption-certificate",
210                         "Encryption Certificate",
211                         "S/MIME certificate for encrypting messages",
212                         NULL,
213                         G_PARAM_READWRITE |
214                         G_PARAM_CONSTRUCT |
215                         G_PARAM_STATIC_STRINGS |
216                         E_SOURCE_PARAM_SETTING));
217
218         g_object_class_install_property (
219                 object_class,
220                 PROP_ENCRYPT_BY_DEFAULT,
221                 g_param_spec_boolean (
222                         "encrypt-by-default",
223                         "Encrypt By Default",
224                         "Encrypt outgoing messages by default",
225                         FALSE,
226                         G_PARAM_READWRITE |
227                         G_PARAM_CONSTRUCT |
228                         G_PARAM_STATIC_STRINGS |
229                         E_SOURCE_PARAM_SETTING));
230
231         g_object_class_install_property (
232                 object_class,
233                 PROP_ENCRYPT_TO_SELF,
234                 g_param_spec_boolean (
235                         "encrypt-to-self",
236                         "Encrypt To Self",
237                         "Always encrypt to myself",
238                         TRUE,
239                         G_PARAM_READWRITE |
240                         G_PARAM_CONSTRUCT |
241                         G_PARAM_STATIC_STRINGS |
242                         E_SOURCE_PARAM_SETTING));
243
244         g_object_class_install_property (
245                 object_class,
246                 PROP_SIGNING_ALGORITHM,
247                 g_param_spec_string (
248                         "signing-algorithm",
249                         "Signing Algorithm",
250                         "Hash algorithm used to sign messages",
251                         NULL,
252                         G_PARAM_READWRITE |
253                         G_PARAM_CONSTRUCT |
254                         G_PARAM_STATIC_STRINGS |
255                         E_SOURCE_PARAM_SETTING));
256
257         g_object_class_install_property (
258                 object_class,
259                 PROP_SIGNING_CERTIFICATE,
260                 g_param_spec_string (
261                         "signing-certificate",
262                         "Signing Certificate",
263                         "S/MIME certificate for signing messages",
264                         NULL,
265                         G_PARAM_READWRITE |
266                         G_PARAM_CONSTRUCT |
267                         G_PARAM_STATIC_STRINGS |
268                         E_SOURCE_PARAM_SETTING));
269
270         g_object_class_install_property (
271                 object_class,
272                 PROP_SIGN_BY_DEFAULT,
273                 g_param_spec_boolean (
274                         "sign-by-default",
275                         "Sign By Default",
276                         "Sign outgoing messages by default",
277                         FALSE,
278                         G_PARAM_READWRITE |
279                         G_PARAM_CONSTRUCT |
280                         G_PARAM_STATIC_STRINGS |
281                         E_SOURCE_PARAM_SETTING));
282 }
283
284 static void
285 e_source_smime_init (ESourceSMIME *extension)
286 {
287         extension->priv = E_SOURCE_SMIME_GET_PRIVATE (extension);
288         g_mutex_init (&extension->priv->property_lock);
289 }
290
291 /**
292  * e_source_smime_get_encryption_certificate:
293  * @extension: an #ESourceSMIME
294  *
295  * Returns the S/MIME certificate name used to encrypt messages.
296  *
297  * Returns: the certificate name used to encrypt messages
298  *
299  * Since: 3.6
300  **/
301 const gchar *
302 e_source_smime_get_encryption_certificate (ESourceSMIME *extension)
303 {
304         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
305
306         return extension->priv->encryption_certificate;
307 }
308
309 /**
310  * e_source_smime_dup_encryption_certificate:
311  * @extension: an #ESourceSMIME
312  *
313  * Thread-safe variation of e_source_smime_get_encryption_certificate().
314  * Use this function when accessing @extension from multiple threads.
315  *
316  * The returned string should be freed with g_free() when no longer needed.
317  *
318  * Returns: a newly-allocated copy of #ESourceSMIME:encryption-certificate
319  *
320  * Since: 3.6
321  **/
322 gchar *
323 e_source_smime_dup_encryption_certificate (ESourceSMIME *extension)
324 {
325         const gchar *protected;
326         gchar *duplicate;
327
328         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
329
330         g_mutex_lock (&extension->priv->property_lock);
331
332         protected = e_source_smime_get_encryption_certificate (extension);
333         duplicate = g_strdup (protected);
334
335         g_mutex_unlock (&extension->priv->property_lock);
336
337         return duplicate;
338 }
339
340 /**
341  * e_source_smime_set_encryption_certificate:
342  * @extension: an #ESourceSMIME
343  * @encryption_certificate: (allow-none): the certificate name used to encrypt
344  *                          messages, or %NULL
345  *
346  * Sets the certificate name used to encrypt messages.
347  *
348  * The internal copy of @encryption_certificate is automatically stripped
349  * of leading and trailing whitespace.  If the resulting string is empty,
350  * %NULL is set instead.
351  *
352  * Since: 3.6
353  **/
354 void
355 e_source_smime_set_encryption_certificate (ESourceSMIME *extension,
356                                            const gchar *encryption_certificate)
357 {
358         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
359
360         g_mutex_lock (&extension->priv->property_lock);
361
362         if (g_strcmp0 (
363                 extension->priv->encryption_certificate,
364                 encryption_certificate) == 0) {
365                 g_mutex_unlock (&extension->priv->property_lock);
366                 return;
367         }
368
369         g_free (extension->priv->encryption_certificate);
370         extension->priv->encryption_certificate =
371                 e_util_strdup_strip (encryption_certificate);
372
373         g_mutex_unlock (&extension->priv->property_lock);
374
375         g_object_notify (G_OBJECT (extension), "encryption-certificate");
376 }
377
378 /**
379  * e_source_smime_get_encrypt_by_default:
380  * @extension: an #ESourceSMIME
381  *
382  * Returns whether to encrypt outgoing messages by default using S/MIME
383  * software such as Mozilla Network Security Services (NSS).
384  *
385  * Returns: whether to encrypt outgoing messages by default
386  *
387  * Since: 3.6
388  **/
389 gboolean
390 e_source_smime_get_encrypt_by_default (ESourceSMIME *extension)
391 {
392         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), FALSE);
393
394         return extension->priv->encrypt_by_default;
395 }
396
397 /**
398  * e_source_smime_set_encrypt_by_default:
399  * @extension: an #ESourceSMIME
400  * @encrypt_by_default: whether to encrypt outgoing messages by default
401  *
402  * Sets whether to encrypt outgoing messages by default using S/MIME
403  * software such as Mozilla Network Security Services (NSS).
404  *
405  * Since: 3.6
406  **/
407 void
408 e_source_smime_set_encrypt_by_default (ESourceSMIME *extension,
409                                        gboolean encrypt_by_default)
410 {
411         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
412
413         if (extension->priv->encrypt_by_default == encrypt_by_default)
414                 return;
415
416         extension->priv->encrypt_by_default = encrypt_by_default;
417
418         g_object_notify (G_OBJECT (extension), "encrypt-by-default");
419 }
420
421 /**
422  * e_source_smime_get_encrypt_to_self:
423  * @extension: an #ESourceSMIME
424  *
425  * Returns whether to "encrypt-to-self" when sending encrypted messages.
426  *
427  * Returns: whether to "encrypt-to-self"
428  *
429  * Since: 3.6
430  **/
431 gboolean
432 e_source_smime_get_encrypt_to_self (ESourceSMIME *extension)
433 {
434         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), FALSE);
435
436         return extension->priv->encrypt_to_self;
437 }
438
439 /**
440  * e_source_smime_set_encrypt_to_self:
441  * @extension: an #ESourceSMIME
442  * @encrypt_to_self: whether to "encrypt-to-self"
443  *
444  * Sets whether to "encrypt-to-self" when sending encrypted messages.
445  *
446  * Since: 3.6
447  **/
448 void
449 e_source_smime_set_encrypt_to_self (ESourceSMIME *extension,
450                                     gboolean encrypt_to_self)
451 {
452         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
453
454         if (extension->priv->encrypt_to_self == encrypt_to_self)
455                 return;
456
457         extension->priv->encrypt_to_self = encrypt_to_self;
458
459         g_object_notify (G_OBJECT (extension), "encrypt-to-self");
460 }
461
462 /**
463  * e_source_smime_get_signing_algorithm:
464  * @extension: an #ESourceSMIME
465  *
466  * Returns the name of the hash algorithm used to digitally sign outgoing
467  * messages.
468  *
469  * Returns: the signing algorithm for outgoing messages
470  *
471  * Since: 3.6
472  **/
473 const gchar *
474 e_source_smime_get_signing_algorithm (ESourceSMIME *extension)
475 {
476         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
477
478         return extension->priv->signing_algorithm;
479 }
480
481 /**
482  * e_source_smime_dup_signing_algorithm:
483  * @extension: an #ESourceSMIME
484  *
485  * Thread-safe variation of e_source_smime_get_signing_algorithm().
486  * Use this function when accessing @extension from multiple threads.
487  *
488  * The returned string should be freed with g_free() when no longer needed.
489  *
490  * Returns: a newly-allocated copy of #ESourceSMIME:signing-algorithm
491  *
492  * Since: 3.6
493  **/
494 gchar *
495 e_source_smime_dup_signing_algorithm (ESourceSMIME *extension)
496 {
497         const gchar *protected;
498         gchar *duplicate;
499
500         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
501
502         g_mutex_lock (&extension->priv->property_lock);
503
504         protected = e_source_smime_get_signing_algorithm (extension);
505         duplicate = g_strdup (protected);
506
507         g_mutex_unlock (&extension->priv->property_lock);
508
509         return duplicate;
510 }
511
512 /**
513  * e_source_smime_set_signing_algorithm:
514  * @extension: an #ESourceSMIME
515  * @signing_algorithm: (allow-none): the signing algorithm for outgoing
516  *                     messages, or %NULL
517  *
518  * Sets the name of the hash algorithm used to digitally sign outgoing
519  * messages.
520  *
521  * The internal copy of @signing_algorithm is automatically stripped of
522  * leading and trailing whitespace.  If the resulting string is empty,
523  * %NULL is set instead.
524  *
525  * Since: 3.6
526  **/
527 void
528 e_source_smime_set_signing_algorithm (ESourceSMIME *extension,
529                                       const gchar *signing_algorithm)
530 {
531         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
532
533         g_mutex_lock (&extension->priv->property_lock);
534
535         if (g_strcmp0 (extension->priv->signing_algorithm, signing_algorithm) == 0) {
536                 g_mutex_unlock (&extension->priv->property_lock);
537                 return;
538         }
539
540         g_free (extension->priv->signing_algorithm);
541         extension->priv->signing_algorithm =
542                 e_util_strdup_strip (signing_algorithm);
543
544         g_mutex_unlock (&extension->priv->property_lock);
545
546         g_object_notify (G_OBJECT (extension), "signing-algorithm");
547 }
548
549 /**
550  * e_source_smime_get_signing_certificate:
551  * @extension: an #ESourceSMIME
552  *
553  * Returns the S/MIME certificate name used to sign messages.
554  *
555  * Returns: the certificate name used to sign messages
556  *
557  * Since: 3.6
558  **/
559 const gchar *
560 e_source_smime_get_signing_certificate (ESourceSMIME *extension)
561 {
562         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
563
564         return extension->priv->signing_certificate;
565 }
566
567 /**
568  * e_source_smime_dup_signing_certificate:
569  * @extension: an #ESourceSMIME
570  *
571  * Thread-safe variation of e_source_smime_get_signing_certificate().
572  * Use this function when accessing @extension from multiple threads.
573  *
574  * The returned string should be freed with g_free() when no longer needed.
575  *
576  * Returns: a newly-allocated copy of #ESourceSMIME:signing-certificate
577  *
578  * Since: 3.6
579  **/
580 gchar *
581 e_source_smime_dup_signing_certificate (ESourceSMIME *extension)
582 {
583         const gchar *protected;
584         gchar *duplicate;
585
586         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), NULL);
587
588         g_mutex_lock (&extension->priv->property_lock);
589
590         protected = e_source_smime_get_signing_certificate (extension);
591         duplicate = g_strdup (protected);
592
593         g_mutex_unlock (&extension->priv->property_lock);
594
595         return duplicate;
596 }
597
598 /**
599  * e_source_smime_set_signing_certificate:
600  * @extension: an #ESourceSMIME
601  * @signing_certificate: (allow-none): the certificate name used to sign
602  *                       messages, or %NULL
603  *
604  * Sets the S/MIME certificate name used to sign messages.
605  *
606  * The internal copy of @signing_certificate is automatically stripped
607  * of leading and trailing whitespace.  If the resulting string is empty,
608  * %NULL is set instead.
609  *
610  * Since: 3.6
611  **/
612 void
613 e_source_smime_set_signing_certificate (ESourceSMIME *extension,
614                                         const gchar *signing_certificate)
615 {
616         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
617
618         g_mutex_lock (&extension->priv->property_lock);
619
620         if (g_strcmp0 (extension->priv->signing_certificate, signing_certificate) == 0) {
621                 g_mutex_unlock (&extension->priv->property_lock);
622                 return;
623         }
624
625         g_free (extension->priv->signing_certificate);
626         extension->priv->signing_certificate =
627                 e_util_strdup_strip (signing_certificate);
628
629         g_mutex_unlock (&extension->priv->property_lock);
630
631         g_object_notify (G_OBJECT (extension), "signing-certificate");
632 }
633
634 /**
635  * e_source_smime_get_sign_by_default:
636  * @extension: an #ESourceSMIME
637  *
638  * Returns whether to digitally sign outgoing messages by default using
639  * S/MIME software such as Mozilla Network Security Services (NSS).
640  *
641  * Returns: whether to sign outgoing messages by default
642  *
643  * Since: 3.6
644  **/
645 gboolean
646 e_source_smime_get_sign_by_default (ESourceSMIME *extension)
647 {
648         g_return_val_if_fail (E_IS_SOURCE_SMIME (extension), FALSE);
649
650         return extension->priv->sign_by_default;
651 }
652
653 /**
654  * e_source_smime_set_sign_by_default:
655  * @extension: an #ESourceSMIME
656  * @sign_by_default: whether to sign outgoing messages by default
657  *
658  * Sets whether to digitally sign outgoing messages by default using
659  * S/MIME software such as Mozilla Network Security Services (NSS).
660  *
661  * Since: 3.6
662  **/
663 void
664 e_source_smime_set_sign_by_default (ESourceSMIME *extension,
665                                     gboolean sign_by_default)
666 {
667         g_return_if_fail (E_IS_SOURCE_SMIME (extension));
668
669         if (extension->priv->sign_by_default == sign_by_default)
670                 return;
671
672         extension->priv->sign_by_default = sign_by_default;
673
674         g_object_notify (G_OBJECT (extension), "sign-by-default");
675 }
676