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