Add new ESource classes.
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-mail-composition.c
1 /*
2  * e-source-mail-composition.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-mail-composition
21  * @include: libedataserver/e-source-mail-composition.h
22  * @short_description: #ESource extension for mail composition settings
23  *
24  * The #ESourceMailComposition extension tracks settings to be applied
25  * when composing a new mail message.
26  *
27  * Access the extension as follows:
28  *
29  * |[
30  *   #include <libedataserver/e-source-mail-composition.h>
31  *
32  *   ESourceMailComposition *extension;
33  *
34  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_COMPOSITION);
35  * ]|
36  **/
37
38 #include "e-source-mail-composition.h"
39
40 #include <libedataserver/e-data-server-util.h>
41
42 #define E_SOURCE_MAIL_COMPOSITION_GET_PRIVATE(obj) \
43         (G_TYPE_INSTANCE_GET_PRIVATE \
44         ((obj), E_TYPE_SOURCE_MAIL_COMPOSITION, ESourceMailCompositionPrivate))
45
46 struct _ESourceMailCompositionPrivate {
47         GMutex *property_lock;
48         gchar **bcc;
49         gchar **cc;
50         gchar *drafts_folder;
51         gchar *templates_folder;
52         gboolean sign_imip;
53 };
54
55 enum {
56         PROP_0,
57         PROP_BCC,
58         PROP_CC,
59         PROP_DRAFTS_FOLDER,
60         PROP_SIGN_IMIP,
61         PROP_TEMPLATES_FOLDER
62 };
63
64 G_DEFINE_TYPE (
65         ESourceMailComposition,
66         e_source_mail_composition,
67         E_TYPE_SOURCE_EXTENSION)
68
69 static void
70 source_mail_composition_set_property (GObject *object,
71                                       guint property_id,
72                                       const GValue *value,
73                                       GParamSpec *pspec)
74 {
75         switch (property_id) {
76                 case PROP_BCC:
77                         e_source_mail_composition_set_bcc (
78                                 E_SOURCE_MAIL_COMPOSITION (object),
79                                 g_value_get_boxed (value));
80                         return;
81
82                 case PROP_CC:
83                         e_source_mail_composition_set_cc (
84                                 E_SOURCE_MAIL_COMPOSITION (object),
85                                 g_value_get_boxed (value));
86                         return;
87
88                 case PROP_DRAFTS_FOLDER:
89                         e_source_mail_composition_set_drafts_folder (
90                                 E_SOURCE_MAIL_COMPOSITION (object),
91                                 g_value_get_string (value));
92                         return;
93
94                 case PROP_SIGN_IMIP:
95                         e_source_mail_composition_set_sign_imip (
96                                 E_SOURCE_MAIL_COMPOSITION (object),
97                                 g_value_get_boolean (value));
98                         return;
99
100                 case PROP_TEMPLATES_FOLDER:
101                         e_source_mail_composition_set_templates_folder (
102                                 E_SOURCE_MAIL_COMPOSITION (object),
103                                 g_value_get_string (value));
104                         return;
105         }
106
107         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
108 }
109
110 static void
111 source_mail_composition_get_property (GObject *object,
112                                       guint property_id,
113                                       GValue *value,
114                                       GParamSpec *pspec)
115 {
116         switch (property_id) {
117                 case PROP_BCC:
118                         g_value_take_boxed (
119                                 value,
120                                 e_source_mail_composition_dup_bcc (
121                                 E_SOURCE_MAIL_COMPOSITION (object)));
122                         return;
123
124                 case PROP_CC:
125                         g_value_take_boxed (
126                                 value,
127                                 e_source_mail_composition_dup_cc (
128                                 E_SOURCE_MAIL_COMPOSITION (object)));
129                         return;
130
131                 case PROP_DRAFTS_FOLDER:
132                         g_value_take_string (
133                                 value,
134                                 e_source_mail_composition_dup_drafts_folder (
135                                 E_SOURCE_MAIL_COMPOSITION (object)));
136                         return;
137
138                 case PROP_SIGN_IMIP:
139                         g_value_set_boolean (
140                                 value,
141                                 e_source_mail_composition_get_sign_imip (
142                                 E_SOURCE_MAIL_COMPOSITION (object)));
143                         return;
144
145                 case PROP_TEMPLATES_FOLDER:
146                         g_value_take_string (
147                                 value,
148                                 e_source_mail_composition_dup_templates_folder (
149                                 E_SOURCE_MAIL_COMPOSITION (object)));
150                         return;
151         }
152
153         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
154 }
155
156 static void
157 source_mail_composition_finalize (GObject *object)
158 {
159         ESourceMailCompositionPrivate *priv;
160
161         priv = E_SOURCE_MAIL_COMPOSITION_GET_PRIVATE (object);
162
163         g_mutex_free (priv->property_lock);
164
165         g_strfreev (priv->bcc);
166         g_strfreev (priv->cc);
167         g_free (priv->drafts_folder);
168         g_free (priv->templates_folder);
169
170         /* Chain up to parent's finalize() method. */
171         G_OBJECT_CLASS (e_source_mail_composition_parent_class)->
172                 finalize (object);
173 }
174
175 static void
176 e_source_mail_composition_class_init (ESourceMailCompositionClass *class)
177 {
178         GObjectClass *object_class;
179         ESourceExtensionClass *extension_class;
180
181         g_type_class_add_private (
182                 class, sizeof (ESourceMailCompositionPrivate));
183
184         object_class = G_OBJECT_CLASS (class);
185         object_class->set_property = source_mail_composition_set_property;
186         object_class->get_property = source_mail_composition_get_property;
187         object_class->finalize = source_mail_composition_finalize;
188
189         extension_class = E_SOURCE_EXTENSION_CLASS (class);
190         extension_class->name = E_SOURCE_EXTENSION_MAIL_COMPOSITION;
191
192         g_object_class_install_property (
193                 object_class,
194                 PROP_BCC,
195                 g_param_spec_boxed (
196                         "bcc",
197                         "Bcc",
198                         "Recipients to blind carbon-copy",
199                         G_TYPE_STRV,
200                         G_PARAM_READWRITE |
201                         G_PARAM_CONSTRUCT |
202                         G_PARAM_STATIC_STRINGS |
203                         E_SOURCE_PARAM_SETTING));
204
205         g_object_class_install_property (
206                 object_class,
207                 PROP_CC,
208                 g_param_spec_boxed (
209                         "cc",
210                         "Cc",
211                         "Recipients to carbon-copy",
212                         G_TYPE_STRV,
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_DRAFTS_FOLDER,
221                 g_param_spec_string (
222                         "drafts-folder",
223                         "Drafts Folder",
224                         "Preferred folder for draft messages",
225                         NULL,
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_SIGN_IMIP,
234                 g_param_spec_boolean (
235                         "sign-imip",
236                         "Sign iMIP",
237                         "Include iMIP messages when signing",
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_TEMPLATES_FOLDER,
247                 g_param_spec_string (
248                         "templates-folder",
249                         "Templates Folder",
250                         "Preferred folder for message templates",
251                         NULL,
252                         G_PARAM_READWRITE |
253                         G_PARAM_CONSTRUCT |
254                         G_PARAM_STATIC_STRINGS |
255                         E_SOURCE_PARAM_SETTING));
256 }
257
258 static void
259 e_source_mail_composition_init (ESourceMailComposition *extension)
260 {
261         extension->priv = E_SOURCE_MAIL_COMPOSITION_GET_PRIVATE (extension);
262         extension->priv->property_lock = g_mutex_new ();
263 }
264
265 /**
266  * e_source_mail_composition_get_bcc:
267  * @extension: an #ESourceMailComposition
268  *
269  * Returns a %NULL-terminated string array of recipients which should
270  * automatically be added to the blind carbon-copy (Bcc) list when
271  * composing a new mail message.  The recipient strings should be of
272  * the form "Full Name <email-address>".  The returned array is owned
273  * by @extension and should not be modified or freed.
274  *
275  * Returns: a %NULL-terminated string array of Bcc recipients
276  *
277  * Since: 3.6
278  **/
279 const gchar * const *
280 e_source_mail_composition_get_bcc (ESourceMailComposition *extension)
281 {
282         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
283
284         return (const gchar * const *) extension->priv->bcc;
285 }
286
287 /**
288  * e_source_mail_composition_dup_bcc:
289  * @extension: an #ESourceMailComposition
290  *
291  * Thread-safe variation of e_source_mail_composition_get_bcc().
292  * Use this function when accessing @extension from multiple threads.
293  *
294  * The returned string array should be freed with g_strfreev() when no
295  * longer needed.
296  *
297  * Returns: a newly-allocated copy of #ESourceMailComposition:bcc
298  *
299  * Since: 3.6
300  **/
301 gchar **
302 e_source_mail_composition_dup_bcc (ESourceMailComposition *extension)
303 {
304         const gchar * const *protected;
305         gchar **duplicate;
306
307         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
308
309         g_mutex_lock (extension->priv->property_lock);
310
311         protected = e_source_mail_composition_get_bcc (extension);
312         duplicate = g_strdupv ((gchar **) protected);
313
314         g_mutex_unlock (extension->priv->property_lock);
315
316         return duplicate;
317 }
318
319 /**
320  * e_source_mail_composition_set_bcc:
321  * @extension: an #ESource
322  * @bcc: (allow-none): a %NULL-terminated string array of Bcc recipients
323  *
324  * Sets the recipients which should automatically be added to the blind
325  * carbon-copy (Bcc) list when composing a new mail message.  The recipient
326  * strings should be of the form "Full Name <email-address>".
327  *
328  * Since: 3.6
329  **/
330 void
331 e_source_mail_composition_set_bcc (ESourceMailComposition *extension,
332                                    const gchar * const *bcc)
333 {
334         g_return_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension));
335
336         g_mutex_lock (extension->priv->property_lock);
337
338         g_strfreev (extension->priv->bcc);
339         extension->priv->bcc = g_strdupv ((gchar **) bcc);
340
341         g_mutex_unlock (extension->priv->property_lock);
342
343         g_object_notify (G_OBJECT (extension), "bcc");
344 }
345
346 /**
347  * e_source_mail_composition_get_cc:
348  * @extension: an #ESourceMailComposition
349  *
350  * Returns a %NULL-terminated string array of recipients which should
351  * automatically be added to the carbon-copy (Cc) list when composing a
352  * new mail message.  The recipient strings should be of the form "Full
353  * Name <email-address>".  The returned array is owned by @extension and
354  * should not be modified or freed.
355  *
356  * Returns: a %NULL-terminated string array of Cc recipients
357  *
358  * Since: 3.6
359  **/
360 const gchar * const *
361 e_source_mail_composition_get_cc (ESourceMailComposition *extension)
362 {
363         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
364
365         return (const gchar * const *) extension->priv->cc;
366 }
367
368 /**
369  * e_source_mail_composition_dup_cc:
370  * @extension: an #ESourceMailComposition
371  *
372  * Thread-safe variation of e_source_mail_composition_get_cc().
373  * Use this function when accessing @extension from multiple threads.
374  *
375  * The returned string array should be freed with g_strfreev() when no
376  * longer needed.
377  *
378  * Returns: a newly-allocated copy of #ESourceMailComposition:cc
379  *
380  * Since: 3.6
381  **/
382 gchar **
383 e_source_mail_composition_dup_cc (ESourceMailComposition *extension)
384 {
385         const gchar * const *protected;
386         gchar **duplicate;
387
388         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
389
390         g_mutex_lock (extension->priv->property_lock);
391
392         protected = e_source_mail_composition_get_cc (extension);
393         duplicate = g_strdupv ((gchar **) protected);
394
395         g_mutex_unlock (extension->priv->property_lock);
396
397         return duplicate;
398 }
399
400 /**
401  * e_source_mail_composition_set_cc:
402  * @extension: an #ESourceMailComposition
403  * @cc: (allow-none): a %NULL-terminated string array of Cc recipients
404  *
405  * Sets the recipients which should automatically be added to the carbon
406  * copy (Cc) list when composing a new mail message.  The recipient strings
407  * should be of the form "Full Name <email-address>".
408  *
409  * Since: 3.6
410  **/
411 void
412 e_source_mail_composition_set_cc (ESourceMailComposition *extension,
413                                   const gchar * const *cc)
414 {
415         g_return_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension));
416
417         g_mutex_lock (extension->priv->property_lock);
418
419         g_strfreev (extension->priv->cc);
420         extension->priv->cc = g_strdupv ((gchar **) cc);
421
422         g_mutex_unlock (extension->priv->property_lock);
423
424         g_object_notify (G_OBJECT (extension), "cc");
425 }
426
427 /**
428  * e_source_mail_composition_get_drafts_folder:
429  * @extension: an #ESourceMailComposition
430  * 
431  * Returns a string identifying the preferred folder for draft messages.
432  * The format of the identifier string is defined by the client application.
433  *
434  * Returns: an identifier for the preferred drafts folder
435  *
436  * Since: 3.6
437  **/
438 const gchar *
439 e_source_mail_composition_get_drafts_folder (ESourceMailComposition *extension)
440 {
441         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
442
443         return extension->priv->drafts_folder;
444 }
445
446 /**
447  * e_source_mail_composition_dup_drafts_folder:
448  * @extension: an #ESourceMailComposition
449  *
450  * Thread-safe variation of e_source_mail_composition_get_drafts_folder().
451  * Use this function when accessing @extension from multiple threads.
452  *
453  * The returned string should be freed with g_free() when no longer needed.
454  *
455  * Returns: a newly-allocated copy of #ESourceMailComposition:drafts-folder
456  *
457  * Since: 3.6
458  **/
459 gchar *
460 e_source_mail_composition_dup_drafts_folder (ESourceMailComposition *extension)
461 {
462         const gchar *protected;
463         gchar *duplicate;
464
465         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
466
467         g_mutex_lock (extension->priv->property_lock);
468
469         protected = e_source_mail_composition_get_drafts_folder (extension);
470         duplicate = g_strdup (protected);
471
472         g_mutex_unlock (extension->priv->property_lock);
473
474         return duplicate;
475 }
476
477 /**
478  * e_source_mail_composition_set_drafts_folder:
479  * @extension: an #ESourceMailComposition
480  * @drafts_folder: (allow-none): an identifier for the preferred drafts
481  *                 folder, or %NULL
482  *
483  * Sets the preferred folder for draft messages by an identifier string.
484  * The format of the identifier string is defined by the client application.
485  *
486  * The internal copy of @drafts_folder is automatically stripped of
487  * leading and trailing whitespace.  If the resulting string is empty,
488  * %NULL is set instead.
489  *
490  * Since: 3.6
491  **/
492 void
493 e_source_mail_composition_set_drafts_folder (ESourceMailComposition *extension,
494                                              const gchar *drafts_folder)
495 {
496         g_return_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension));
497
498         g_mutex_lock (extension->priv->property_lock);
499
500         g_free (extension->priv->drafts_folder);
501         extension->priv->drafts_folder = e_util_strdup_strip (drafts_folder);
502
503         g_mutex_unlock (extension->priv->property_lock);
504
505         g_object_notify (G_OBJECT (extension), "drafts-folder");
506 }
507
508 /**
509  * e_source_mail_composition_get_sign_imip:
510  * @extension: an #ESourceMailComposition
511  *
512  * Returns whether outgoing iMIP messages such as meeting requests should
513  * also be signed.  This is primarily intended as a workaround for certain
514  * versions of Microsoft Outlook which can't handle signed iMIP messages.
515  *
516  * Returns: whether outgoing iMIP messages should be signed
517  *
518  * Since: 3.6
519  **/
520 gboolean
521 e_source_mail_composition_get_sign_imip (ESourceMailComposition *extension)
522 {
523         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), FALSE);
524
525         return extension->priv->sign_imip;
526 }
527
528 /**
529  * e_source_mail_composition_set_sign_imip:
530  * @extension: an #ESourceMailComposition
531  * @sign_imip: whether outgoing iMIP messages should be signed
532  *
533  * Sets whether outgoing iMIP messages such as meeting requests should
534  * also be signed.  This is primarily intended as a workaround for certain
535  * versions of Microsoft Outlook which can't handle signed iMIP messages.
536  *
537  * Since: 3.6
538  **/
539 void
540 e_source_mail_composition_set_sign_imip (ESourceMailComposition *extension,
541                                          gboolean sign_imip)
542 {
543         g_return_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension));
544
545         extension->priv->sign_imip = sign_imip;
546
547         g_object_notify (G_OBJECT (extension), "sign-imip");
548 }
549
550 /**
551  * e_source_mail_composition_get_templates_folder:
552  * @extension: an #ESourceMailComposition
553  *
554  * Returns a string identifying the preferred folder for message templates.
555  * The format of the identifier string is defined by the client application.
556  *
557  * Returns: an identifier for the preferred templates folder
558  *
559  * Since: 3.6
560  **/
561 const gchar *
562 e_source_mail_composition_get_templates_folder (ESourceMailComposition *extension)
563 {
564         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
565
566         return extension->priv->templates_folder;
567 }
568
569 /**
570  * e_source_mail_composition_dup_templates_folder:
571  * @extension: an #ESourceMailComposition
572  *
573  * Thread-safe variation of e_source_mail_composition_get_templates_folder().
574  * Use this function when accessing @extension from multiple threads.
575  *
576  * The returned string should be freed with g_free() when no longer needed.
577  *
578  * Returns: a newly-allocated copy of #ESourceMailComposition:templates-folder
579  *
580  * Since: 3.6
581  **/
582 gchar *
583 e_source_mail_composition_dup_templates_folder (ESourceMailComposition *extension)
584 {
585         const gchar *protected;
586         gchar *duplicate;
587
588         g_return_val_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension), NULL);
589
590         g_mutex_lock (extension->priv->property_lock);
591
592         protected = e_source_mail_composition_get_templates_folder (extension);
593         duplicate = g_strdup (protected);
594
595         g_mutex_unlock (extension->priv->property_lock);
596
597         return duplicate;
598 }
599
600 /**
601  * e_source_mail_composition_set_templates_folder:
602  * @extension: an #ESourceMailComposition
603  * @templates_folder: (allow-none): an identifier for the preferred templates
604  *                    folder, or %NULL
605  *
606  * Sets the preferred folder for message templates by an identifier string.
607  * The format of the identifier string is defined by the client application.
608  *
609  * The internal copy of @templates_folder is automatically stripped of
610  * leading and trailing whitespace.  If the resulting string is empty,
611  * %NULL is set instead.
612  *
613  * Since: 3.6
614  **/
615 void
616 e_source_mail_composition_set_templates_folder (ESourceMailComposition *extension,
617                                                 const gchar *templates_folder)
618 {
619         g_return_if_fail (E_IS_SOURCE_MAIL_COMPOSITION (extension));
620
621         g_mutex_lock (extension->priv->property_lock);
622
623         g_free (extension->priv->templates_folder);
624         extension->priv->templates_folder = e_util_strdup_strip (templates_folder);
625
626         g_mutex_unlock (extension->priv->property_lock);
627
628         g_object_notify (G_OBJECT (extension), "templates-folder");
629 }
630