goa: Add missing linker flag (for real).
[platform/upstream/evolution-data-server.git] / libedataserver / e-source-mail-identity.c
1 /*
2  * e-source-mail-identity.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-identity
21  * @include: libedataserver/libedataserver.h
22  * @short_description: #ESource extension for an email identity
23  *
24  * The #ESourceMailIdentity extension describes an "identity" for a mail
25  * account, which is the information that other people see when they read
26  * your messages.
27  *
28  * Access the extension as follows:
29  *
30  * |[
31  *   #include <libedataserver/libedataserver.h>
32  *
33  *   ESourceMailIdentity *extension;
34  *
35  *   extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_IDENTITY);
36  * ]|
37  **/
38
39 #include "e-source-mail-identity.h"
40
41 #include <libedataserver/e-data-server-util.h>
42
43 #define E_SOURCE_MAIL_IDENTITY_GET_PRIVATE(obj) \
44         (G_TYPE_INSTANCE_GET_PRIVATE \
45         ((obj), E_TYPE_SOURCE_MAIL_IDENTITY, ESourceMailIdentityPrivate))
46
47 struct _ESourceMailIdentityPrivate {
48         GMutex property_lock;
49         gchar *address;
50         gchar *name;
51         gchar *organization;
52         gchar *reply_to;
53         gchar *signature_uid;
54 };
55
56 enum {
57         PROP_0,
58         PROP_ADDRESS,
59         PROP_NAME,
60         PROP_ORGANIZATION,
61         PROP_REPLY_TO,
62         PROP_SIGNATURE_UID
63 };
64
65 G_DEFINE_TYPE (
66         ESourceMailIdentity,
67         e_source_mail_identity,
68         E_TYPE_SOURCE_EXTENSION)
69
70 static void
71 source_mail_identity_set_property (GObject *object,
72                                    guint property_id,
73                                    const GValue *value,
74                                    GParamSpec *pspec)
75 {
76         switch (property_id) {
77                 case PROP_ADDRESS:
78                         e_source_mail_identity_set_address (
79                                 E_SOURCE_MAIL_IDENTITY (object),
80                                 g_value_get_string (value));
81                         return;
82
83                 case PROP_NAME:
84                         e_source_mail_identity_set_name (
85                                 E_SOURCE_MAIL_IDENTITY (object),
86                                 g_value_get_string (value));
87                         return;
88
89                 case PROP_ORGANIZATION:
90                         e_source_mail_identity_set_organization (
91                                 E_SOURCE_MAIL_IDENTITY (object),
92                                 g_value_get_string (value));
93                         return;
94
95                 case PROP_REPLY_TO:
96                         e_source_mail_identity_set_reply_to (
97                                 E_SOURCE_MAIL_IDENTITY (object),
98                                 g_value_get_string (value));
99                         return;
100
101                 case PROP_SIGNATURE_UID:
102                         e_source_mail_identity_set_signature_uid (
103                                 E_SOURCE_MAIL_IDENTITY (object),
104                                 g_value_get_string (value));
105                         return;
106         }
107
108         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
109 }
110
111 static void
112 source_mail_identity_get_property (GObject *object,
113                                    guint property_id,
114                                    GValue *value,
115                                    GParamSpec *pspec)
116 {
117         switch (property_id) {
118                 case PROP_ADDRESS:
119                         g_value_take_string (
120                                 value,
121                                 e_source_mail_identity_dup_address (
122                                 E_SOURCE_MAIL_IDENTITY (object)));
123                         return;
124
125                 case PROP_NAME:
126                         g_value_take_string (
127                                 value,
128                                 e_source_mail_identity_dup_name (
129                                 E_SOURCE_MAIL_IDENTITY (object)));
130                         return;
131
132                 case PROP_ORGANIZATION:
133                         g_value_take_string (
134                                 value,
135                                 e_source_mail_identity_dup_organization (
136                                 E_SOURCE_MAIL_IDENTITY (object)));
137                         return;
138
139                 case PROP_REPLY_TO:
140                         g_value_take_string (
141                                 value,
142                                 e_source_mail_identity_dup_reply_to (
143                                 E_SOURCE_MAIL_IDENTITY (object)));
144                         return;
145
146                 case PROP_SIGNATURE_UID:
147                         g_value_take_string (
148                                 value,
149                                 e_source_mail_identity_dup_signature_uid (
150                                 E_SOURCE_MAIL_IDENTITY (object)));
151                         return;
152         }
153
154         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
155 }
156
157 static void
158 source_mail_identity_finalize (GObject *object)
159 {
160         ESourceMailIdentityPrivate *priv;
161
162         priv = E_SOURCE_MAIL_IDENTITY_GET_PRIVATE (object);
163
164         g_mutex_clear (&priv->property_lock);
165
166         g_free (priv->address);
167         g_free (priv->name);
168         g_free (priv->organization);
169         g_free (priv->reply_to);
170         g_free (priv->signature_uid);
171
172         /* Chain up to parent's finalize() method. */
173         G_OBJECT_CLASS (e_source_mail_identity_parent_class)->finalize (object);
174 }
175
176 static void
177 e_source_mail_identity_class_init (ESourceMailIdentityClass *class)
178 {
179         GObjectClass *object_class;
180         ESourceExtensionClass *extension_class;
181
182         g_type_class_add_private (class, sizeof (ESourceMailIdentityPrivate));
183
184         object_class = G_OBJECT_CLASS (class);
185         object_class->set_property = source_mail_identity_set_property;
186         object_class->get_property = source_mail_identity_get_property;
187         object_class->finalize = source_mail_identity_finalize;
188
189         extension_class = E_SOURCE_EXTENSION_CLASS (class);
190         extension_class->name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
191
192         g_object_class_install_property (
193                 object_class,
194                 PROP_ADDRESS,
195                 g_param_spec_string (
196                         "address",
197                         "Address",
198                         "Sender's email address",
199                         NULL,
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_NAME,
208                 g_param_spec_string (
209                         "name",
210                         "Name",
211                         "Sender's name",
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_ORGANIZATION,
221                 g_param_spec_string (
222                         "organization",
223                         "Organization",
224                         "Sender's organization",
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_REPLY_TO,
234                 g_param_spec_string (
235                         "reply-to",
236                         "Reply-To",
237                         "Sender's reply-to address",
238                         NULL,
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_SIGNATURE_UID,
247                 g_param_spec_string (
248                         "signature-uid",
249                         "Signature UID",
250                         "ESource UID of the sender's signature",
251                         "none",
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_identity_init (ESourceMailIdentity *extension)
260 {
261         extension->priv = E_SOURCE_MAIL_IDENTITY_GET_PRIVATE (extension);
262         g_mutex_init (&extension->priv->property_lock);
263 }
264
265 /**
266  * e_source_mail_identity_get_address:
267  * @extension: an #ESourceMailIdentity
268  *
269  * Returns the email address for this identity from which to send messages.
270  * This may be an empty string but will never be %NULL.
271  *
272  * Returns: the sender's email address
273  *
274  * Since: 3.6
275  **/
276 const gchar *
277 e_source_mail_identity_get_address (ESourceMailIdentity *extension)
278 {
279         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
280
281         return extension->priv->address;
282 }
283
284 /**
285  * e_source_mail_identity_dup_address:
286  * @extension: an #ESourceMailIdentity
287  *
288  * Thread-safe variation of e_source_mail_identity_get_address().
289  * Use this function when accessing @extension from multiple threads.
290  *
291  * The returned string should be freed with g_free() when no longer needed.
292  *
293  * Returns: a newly-allocated copy of #ESourceMailIdentity:address
294  *
295  * Since: 3.6
296  **/
297 gchar *
298 e_source_mail_identity_dup_address (ESourceMailIdentity *extension)
299 {
300         const gchar *protected;
301         gchar *duplicate;
302
303         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
304
305         g_mutex_lock (&extension->priv->property_lock);
306
307         protected = e_source_mail_identity_get_address (extension);
308         duplicate = g_strdup (protected);
309
310         g_mutex_unlock (&extension->priv->property_lock);
311
312         return duplicate;
313 }
314
315 /**
316  * e_source_mail_identity_set_address:
317  * @extension: an #ESourceMailIdentity
318  * @address: (allow-none): the sender's email address, or %NULL
319  *
320  * Sets the email address for this identity from which to send messages.
321  *
322  * The internal copy of @address is automatically stripped of leading and
323  * trailing whitespace.  If the resulting string is empty, %NULL is set
324  * instead.
325  *
326  * Since: 3.6
327  **/
328 void
329 e_source_mail_identity_set_address (ESourceMailIdentity *extension,
330                                     const gchar *address)
331 {
332         g_return_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension));
333
334         g_mutex_lock (&extension->priv->property_lock);
335
336         if (g_strcmp0 (extension->priv->address, address) == 0) {
337                 g_mutex_unlock (&extension->priv->property_lock);
338                 return;
339         }
340
341         g_free (extension->priv->address);
342         extension->priv->address = e_util_strdup_strip (address);
343
344         g_mutex_unlock (&extension->priv->property_lock);
345
346         g_object_notify (G_OBJECT (extension), "address");
347 }
348
349 /**
350  * e_source_mail_identity_get_name:
351  * @extension: an #ESourceMailIdentity
352  *
353  * Returns the sender's name for this identity.
354  *
355  * Returns: the sender's name
356  *
357  * Since: 3.6
358  **/
359 const gchar *
360 e_source_mail_identity_get_name (ESourceMailIdentity *extension)
361 {
362         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
363
364         return extension->priv->name;
365 }
366
367 /**
368  * e_source_mail_identity_dup_name:
369  * @extension: an #ESourceMailIdentity
370  *
371  * Thread-safe variation of e_source_mail_identity_get_name().
372  * Use this function when accessing @extension from multiple threads.
373  *
374  * The returned string should be freed with g_free() when no longer needed.
375  *
376  * Returns: a newly-allocated copy of #ESourceMailIdentity:name
377  *
378  * Since: 3.6
379  **/
380 gchar *
381 e_source_mail_identity_dup_name (ESourceMailIdentity *extension)
382 {
383         const gchar *protected;
384         gchar *duplicate;
385
386         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
387
388         g_mutex_lock (&extension->priv->property_lock);
389
390         protected = e_source_mail_identity_get_name (extension);
391         duplicate = g_strdup (protected);
392
393         g_mutex_unlock (&extension->priv->property_lock);
394
395         return duplicate;
396 }
397
398 /**
399  * e_source_mail_identity_set_name:
400  * @extension: an #ESourceMailIdentity
401  * @name: (allow-none): the sender's name, or %NULL
402  *
403  * Sets the sender's name for this identity.
404  *
405  * The internal copy of @name is automatically stripped of leading and
406  * trailing whitespace.  If @name is %NULL or the resulting string is
407  * empty, the result of g_get_real_name() is set instead.
408  *
409  * Since: 3.6
410  **/
411 void
412 e_source_mail_identity_set_name (ESourceMailIdentity *extension,
413                                  const gchar *name)
414 {
415         g_return_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension));
416
417         g_mutex_lock (&extension->priv->property_lock);
418
419         if (extension->priv->name != NULL &&
420             g_strcmp0 (extension->priv->name, name) == 0) {
421                 g_mutex_unlock (&extension->priv->property_lock);
422                 return;
423         }
424
425         g_free (extension->priv->name);
426         extension->priv->name = e_util_strdup_strip (name);
427
428         if (extension->priv->name == NULL)
429                 extension->priv->name = g_strdup (g_get_real_name ());
430
431         g_mutex_unlock (&extension->priv->property_lock);
432
433         g_object_notify (G_OBJECT (extension), "name");
434 }
435
436 /**
437  * e_source_mail_identity_get_organization:
438  * @extension: an #ESourceMailIdentity
439  *
440  * Returns the sender's organization for this identity.
441  *
442  * Returns: the sender's organization
443  *
444  * Since: 3.6
445  **/
446 const gchar *
447 e_source_mail_identity_get_organization (ESourceMailIdentity *extension)
448 {
449         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
450
451         return extension->priv->organization;
452 }
453
454 /**
455  * e_source_mail_identity_dup_organization:
456  * @extension: an #ESourceMailIdentity
457  *
458  * Thread-safe variation of e_source_mail_identity_dup_organization().
459  * Use this function when accessing @extension from multiple threads.
460  *
461  * The returned string should be freed with g_free() when no longer needed.
462  *
463  * Returns: a newly-allocated copy of #ESourceMailIdentity:organization
464  *
465  * Since: 3.6
466  **/
467 gchar *
468 e_source_mail_identity_dup_organization (ESourceMailIdentity *extension)
469 {
470         const gchar *protected;
471         gchar *duplicate;
472
473         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
474
475         g_mutex_lock (&extension->priv->property_lock);
476
477         protected = e_source_mail_identity_get_organization (extension);
478         duplicate = g_strdup (protected);
479
480         g_mutex_unlock (&extension->priv->property_lock);
481
482         return duplicate;
483 }
484
485 /**
486  * e_source_mail_identity_set_organization:
487  * @extension: an #ESourceMailIdentity
488  * @organization: (allow-none): the sender's organization, or %NULL
489  *
490  * Sets the sender's organization for this identity.
491  *
492  * The internal copy of @organization is automatically stripped of leading
493  * and trailing whitespace.  If the resulting string is empty, %NULL is set
494  * instead.
495  *
496  * Since: 3.6
497  **/
498 void
499 e_source_mail_identity_set_organization (ESourceMailIdentity *extension,
500                                          const gchar *organization)
501 {
502         g_return_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension));
503
504         g_mutex_lock (&extension->priv->property_lock);
505
506         if (g_strcmp0 (extension->priv->organization, organization) == 0) {
507                 g_mutex_unlock (&extension->priv->property_lock);
508                 return;
509         }
510
511         g_free (extension->priv->organization);
512         extension->priv->organization = e_util_strdup_strip (organization);
513
514         g_mutex_unlock (&extension->priv->property_lock);
515
516         g_object_notify (G_OBJECT (extension), "organization");
517 }
518
519 /**
520  * e_source_mail_identity_get_reply_to:
521  * @extension: an #ESourceMailIdentity
522  *
523  * Returns the email address for this identity to which recipients should
524  * send replies.
525  *
526  * Returns: the sender's reply-to address
527  *
528  * Since: 3.6
529  **/
530 const gchar *
531 e_source_mail_identity_get_reply_to (ESourceMailIdentity *extension)
532 {
533         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
534
535         return extension->priv->reply_to;
536 }
537
538 /**
539  * e_source_mail_identity_dup_reply_to:
540  * @extension: an #ESourceMailIdentity
541  *
542  * Thread-safe variation of e_source_mail_identity_get_reply_to().
543  * Use this function when accessing @extension from multiple threads.
544  *
545  * The returned string should be freed with g_free() when no longer needed.
546  *
547  * Returns: a newly-allocated copy of #ESourceMailIdentity:reply-to
548  *
549  * Since: 3.6
550  **/
551 gchar *
552 e_source_mail_identity_dup_reply_to (ESourceMailIdentity *extension)
553 {
554         const gchar *protected;
555         gchar *duplicate;
556
557         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
558
559         g_mutex_lock (&extension->priv->property_lock);
560
561         protected = e_source_mail_identity_get_reply_to (extension);
562         duplicate = g_strdup (protected);
563
564         g_mutex_unlock (&extension->priv->property_lock);
565
566         return duplicate;
567 }
568
569 /**
570  * e_source_mail_identity_set_reply_to:
571  * @extension: an #ESourceMailIdentity
572  * @reply_to: (allow-none): the sender's reply-to address, or %NULL
573  *
574  * Sets the email address for this identity to which recipients should
575  * send replies.
576  *
577  * The internal copy of @reply_to is automatically stripped of leading
578  * and trailing whitespace.  If the resulting string is empty, %NULL is
579  * set instead.
580  *
581  * Since: 3.6
582  **/
583 void
584 e_source_mail_identity_set_reply_to (ESourceMailIdentity *extension,
585                                      const gchar *reply_to)
586 {
587         g_return_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension));
588
589         g_mutex_lock (&extension->priv->property_lock);
590
591         if (g_strcmp0 (extension->priv->reply_to, reply_to) == 0) {
592                 g_mutex_unlock (&extension->priv->property_lock);
593                 return;
594         }
595
596         g_free (extension->priv->reply_to);
597         extension->priv->reply_to = e_util_strdup_strip (reply_to);
598
599         g_mutex_unlock (&extension->priv->property_lock);
600
601         g_object_notify (G_OBJECT (extension), "reply-to");
602 }
603
604 /**
605  * e_source_mail_identity_get_signature_uid:
606  * @extension: an #ESourceMailIdentity
607  *
608  * Returns the #ESource:uid of an #ESource describing a mail signature.
609  *
610  * If the user does not want to use a signature for this identity, the
611  * convention is to set the #ESourceMailIdentity:signature-uid property
612  * to "none".
613  *
614  * Returns: the sender's signature ID, or "none"
615  *
616  * Since: 3.6
617  **/
618 const gchar *
619 e_source_mail_identity_get_signature_uid (ESourceMailIdentity *extension)
620 {
621         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
622
623         return extension->priv->signature_uid;
624 }
625
626 /**
627  * e_source_mail_identity_dup_signature_uid:
628  * @extension: an #ESourceMailIdentity
629  *
630  * Thread-safe variation of e_source_mail_identity_get_signature_uid().
631  * Use this function when accessing @extension from multiple threads.
632  *
633  * The returned string should be freed with g_free() when no longer needed.
634  *
635  * Returns: a newly-allocated copy of #ESourceMailIdentity:signature-uid
636  *
637  * Since: 3.6
638  **/
639 gchar *
640 e_source_mail_identity_dup_signature_uid (ESourceMailIdentity *extension)
641 {
642         const gchar *protected;
643         gchar *duplicate;
644
645         g_return_val_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension), NULL);
646
647         g_mutex_lock (&extension->priv->property_lock);
648
649         protected = e_source_mail_identity_get_signature_uid (extension);
650         duplicate = g_strdup (protected);
651
652         g_mutex_unlock (&extension->priv->property_lock);
653
654         return duplicate;
655 }
656
657 /**
658  * e_source_mail_identity_set_signature_uid:
659  * @extension: an #ESourceMailIdentity
660  * @signature_uid: (allow-none): the sender's signature ID, or %NULL
661  *
662  * Sets the #ESource:uid of an #ESource describing a mail signature.
663  *
664  * If the user does not want to use a signature for this identity, the
665  * convention is to set the #ESourceMailIdentity:signature-uid property
666  * to "none".  In keeping with that convention, the property will be set
667  * to "none" if @signature is %NULL or an empty string.
668  *
669  * Since: 3.6
670  **/
671 void
672 e_source_mail_identity_set_signature_uid (ESourceMailIdentity *extension,
673                                           const gchar *signature_uid)
674 {
675         g_return_if_fail (E_IS_SOURCE_MAIL_IDENTITY (extension));
676
677         /* Convert empty strings to "none". */
678         if (signature_uid == NULL || *signature_uid == '\0')
679                 signature_uid = "none";
680
681         g_mutex_lock (&extension->priv->property_lock);
682
683         if (g_strcmp0 (extension->priv->signature_uid, signature_uid) == 0) {
684                 g_mutex_unlock (&extension->priv->property_lock);
685                 return;
686         }
687
688         g_free (extension->priv->signature_uid);
689         extension->priv->signature_uid = g_strdup (signature_uid);
690
691         g_mutex_unlock (&extension->priv->property_lock);
692
693         g_object_notify (G_OBJECT (extension), "signature-uid");
694 }
695