Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / addressbook / libebook / e-destination.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4  * e-destination.c
5  *
6  * Copyright (C) 2001-2004 Ximian, Inc.
7  *
8  * Authors: Jon Trowbridge <trow@ximian.com>
9  *          Chris Toshok <toshok@ximian.com>
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of version 2 of the GNU Lesser General Public
15  * License as published by the Free Software Foundation.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25  * USA.
26  */
27
28 /*
29   We should probably make most of the functions in this file a little
30   stupider..  all the work extracting useful info from the
31   EContact/raw text/etc should happen in e_destination_set_contact
32   (and the other setters), not in a bunch of if's in the respective
33   _get_*() functions.
34 */
35
36 #include <config.h>
37 #include "e-destination.h"
38
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <string.h>
42 #include <libebook/e-book.h>
43
44 #include <glib.h>
45 #include <libxml/xmlmemory.h>
46 #include <glib/gi18n-lib.h>
47 #include <camel/camel-internet-address.h>
48
49 #define d(x)
50
51 struct _EDestinationPrivate {
52         gchar *raw;
53
54         char *source_uid;
55
56         EContact *contact;
57         char *contact_uid;
58
59         int email_num;
60
61         char *name;
62         char *email;
63         char *addr;
64         char *textrep;
65         gboolean ignored;
66
67         GList *list_dests;
68
69         guint html_mail_override : 1;
70         guint wants_html_mail : 1;
71
72         guint show_addresses : 1;
73
74         guint auto_recipient : 1;
75 };
76
77 static gboolean       e_destination_from_contact       (const EDestination *);
78 static xmlNodePtr     e_destination_xml_encode         (const EDestination *dest);
79 static gboolean       e_destination_xml_decode         (EDestination *dest, xmlNodePtr node);
80 static void           e_destination_clear              (EDestination *dest);
81
82 /* Signals */
83
84 enum {
85         CHANGED,
86         LAST_SIGNAL
87 };
88
89 static guint signals [LAST_SIGNAL] = { 0 };
90
91 static GObjectClass *parent_class;
92
93 /* Copied from eab-book-util.c. The name selector also keeps its own copy... */
94 static gint
95 utf8_casefold_collate_len (const gchar *str1, const gchar *str2, gint len)
96 {
97         gchar *s1 = g_utf8_casefold(str1, len);
98         gchar *s2 = g_utf8_casefold(str2, len);
99         gint rv;
100
101         rv = g_utf8_collate (s1, s2);
102
103         g_free (s1);
104         g_free (s2);
105
106         return rv;
107 }
108
109 /* Copied from eab-book-util.c. The name selector also keeps its own copy... */
110 static gint
111 utf8_casefold_collate (const gchar *str1, const gchar *str2)
112 {
113         return utf8_casefold_collate_len (str1, str2, -1);
114 }
115
116 static void
117 e_destination_dispose (GObject *obj)
118 {
119         EDestination *dest = E_DESTINATION (obj);
120
121         if (dest->priv) {
122                 e_destination_clear (dest);
123           
124                 g_free (dest->priv);
125                 dest->priv = NULL;
126         }
127
128         if (G_OBJECT_CLASS (parent_class)->dispose)
129                 (* G_OBJECT_CLASS (parent_class)->dispose) (obj);
130 }
131
132 static void
133 e_destination_class_init (EDestinationClass *klass)
134 {
135         GObjectClass *object_class = G_OBJECT_CLASS (klass);
136
137         parent_class = g_type_class_ref (G_TYPE_OBJECT);
138
139         signals [CHANGED] =
140                 g_signal_new ("changed",
141                               G_OBJECT_CLASS_TYPE (object_class),
142                               G_SIGNAL_RUN_LAST,
143                               G_STRUCT_OFFSET (EDestinationClass, changed),
144                               NULL, NULL,
145                               g_cclosure_marshal_VOID__VOID,
146                               G_TYPE_NONE, 0);
147
148         object_class->dispose = e_destination_dispose;
149 }
150
151 static void
152 e_destination_init (EDestination *dest)
153 {
154         dest->priv = g_new0 (struct _EDestinationPrivate, 1);
155
156         dest->priv->auto_recipient = FALSE;
157         dest->priv->ignored = FALSE;
158 }
159
160 GType
161 e_destination_get_type (void)
162 {
163         static GType dest_type = 0;
164
165         if (!dest_type) {
166                 GTypeInfo dest_info = {
167                         sizeof (EDestinationClass),
168                         NULL, /* base_class_init */
169                         NULL, /* base_class_finalize */
170                         (GClassInitFunc)  e_destination_class_init,
171                         NULL, /* class_finalize */
172                         NULL, /* class_data */
173                         sizeof (EDestination),
174                         0,    /* n_preallocs */
175                         (GInstanceInitFunc) e_destination_init
176                 };
177
178                 dest_type = g_type_register_static (G_TYPE_OBJECT, "EDestination", &dest_info, 0);
179         }
180
181         return dest_type;
182 }
183
184 /**
185  * e_destination_new:
186  *
187  * Creates a new #EDestination with blank values.
188  *
189  * Return value: A newly created #EDestination.
190  **/
191 EDestination *
192 e_destination_new (void)
193 {
194         return g_object_new (E_TYPE_DESTINATION, NULL);
195 }
196
197 /**
198  * e_destination_copy:
199  * @dest: an #EDestination
200  *
201  * Creates a new #EDestination identical to @dest.
202  *
203  * Return value: A newly created #EDestination, identical to @dest.
204  */
205 EDestination*
206 e_destination_copy (const EDestination *dest)
207 {
208         EDestination *new_dest;
209         GList *iter;
210
211         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
212
213         new_dest = e_destination_new ();
214
215         new_dest->priv->source_uid         = g_strdup (dest->priv->source_uid);
216         new_dest->priv->contact_uid        = g_strdup (dest->priv->contact_uid);
217         new_dest->priv->name               = g_strdup (dest->priv->name);
218         new_dest->priv->email              = g_strdup (dest->priv->email);
219         new_dest->priv->addr               = g_strdup (dest->priv->addr);
220         new_dest->priv->email_num          = dest->priv->email_num;
221         new_dest->priv->ignored            = dest->priv->ignored;
222
223         if (dest->priv->contact)
224                 new_dest->priv->contact = g_object_ref (dest->priv->contact);
225
226         new_dest->priv->html_mail_override = dest->priv->html_mail_override;
227         new_dest->priv->wants_html_mail    = dest->priv->wants_html_mail;
228
229         /* deep copy, recursively copy our children */
230         for (iter = dest->priv->list_dests; iter != NULL; iter = g_list_next (iter)) {
231                 new_dest->priv->list_dests = g_list_append (new_dest->priv->list_dests,
232                                                             e_destination_copy (E_DESTINATION (iter->data)));
233         }
234
235         /* XXX other settings? */
236         new_dest->priv->raw = g_strdup(dest->priv->raw);
237
238         return new_dest;
239 }
240
241 static void
242 e_destination_clear (EDestination *dest)
243 {
244         g_free (dest->priv->source_uid);
245         dest->priv->source_uid = NULL;
246
247         g_free (dest->priv->contact_uid);
248         dest->priv->contact_uid = NULL;
249
250         g_free (dest->priv->raw);
251         dest->priv->raw = NULL;
252         
253         g_free (dest->priv->name);
254         dest->priv->name = NULL;
255         
256         g_free (dest->priv->email);
257         dest->priv->email = NULL;
258         
259         g_free (dest->priv->addr);
260         dest->priv->addr = NULL;
261         
262         g_free (dest->priv->textrep);
263         dest->priv->textrep = NULL;
264
265         if (dest->priv->contact) {
266                 g_object_unref (dest->priv->contact);
267                 dest->priv->contact = NULL;
268         }
269         dest->priv->email_num = -1;
270         
271         g_list_foreach (dest->priv->list_dests, (GFunc) g_object_unref, NULL);
272         g_list_free (dest->priv->list_dests);
273         dest->priv->list_dests = NULL;
274 }
275
276 static gboolean
277 nonempty (const char *s)
278 {
279         gunichar c;
280         if (s == NULL)
281                 return FALSE;
282         while (*s) {
283                 c = g_utf8_get_char (s);
284                 if (!g_unichar_isspace (c))
285                         return TRUE;
286                 s = g_utf8_next_char (s);
287         }
288         return FALSE;
289 }
290
291 /**
292  * e_destination_empty:
293  * @dest: an #EDestination
294  *
295  * Checks if @dest is blank.
296  *
297  * Return value: %TRUE if @dest is empty, %FALSE otherwise.
298  */
299 gboolean
300 e_destination_empty (const EDestination *dest)
301
302 {
303         struct _EDestinationPrivate *p;
304
305         g_return_val_if_fail (E_IS_DESTINATION (dest), TRUE);
306         
307         p = dest->priv;
308         
309         return !(p->contact != NULL
310                  || (p->source_uid && *p->source_uid)
311                  || (p->contact_uid && *p->contact_uid)
312                  || (nonempty (p->raw))
313                  || (nonempty (p->name))
314                  || (nonempty (p->email))
315                  || (nonempty (p->addr))
316                  || (p->list_dests != NULL));
317 }
318
319 /**
320  * e_destination_equal:
321  * @a: an #EDestination
322  * @b: an #EDestination
323  *
324  * Checks if @a and @b are equal.
325  *
326  * Return value: %TRUE if the destinations are equal, %FALSE otherwise.
327  **/
328 gboolean
329 e_destination_equal (const EDestination *a, const EDestination *b)
330 {
331         const struct _EDestinationPrivate *pa, *pb;
332         const char *na, *nb;
333         
334         g_return_val_if_fail (E_IS_DESTINATION (a), FALSE);
335         g_return_val_if_fail (E_IS_DESTINATION (b), FALSE);
336
337         if (a == b)
338                 return TRUE;
339         
340         pa = a->priv;
341         pb = b->priv;
342         
343         /* Check equality of contacts. */
344         if (pa->contact || pb->contact) {
345                 if (! (pa->contact && pb->contact))
346                         return FALSE;
347                 
348                 if (pa->contact == pb->contact || !strcmp (e_contact_get_const (pa->contact, E_CONTACT_UID),
349                                                            e_contact_get_const (pb->contact, E_CONTACT_UID)))
350                         return TRUE;
351                 
352                 return FALSE;
353         }
354         
355         /* Just in case name returns NULL */
356         na = e_destination_get_name (a);
357         nb = e_destination_get_name (b);
358         if ((na || nb) && !(na && nb && ! utf8_casefold_collate (na, nb)))
359                 return FALSE;
360         
361         if (!g_ascii_strcasecmp (e_destination_get_email (a), e_destination_get_email (b)))
362                 return TRUE;
363         else
364                 return FALSE;
365 }
366
367 /**
368  * e_destination_set_contact:
369  * @dest: an #EDestination
370  * @contact: an #EContact
371  * @email_num: an email index
372  *
373  * Sets @dest to point to one of @contact's e-mail addresses
374  * indicated by @email_num.
375  **/
376 void
377 e_destination_set_contact (EDestination *dest, EContact *contact, gint email_num)
378 {
379         g_return_if_fail (dest && E_IS_DESTINATION (dest));
380         g_return_if_fail (contact && E_IS_CONTACT (contact));
381
382         if (dest->priv->contact != contact ) {
383                 g_object_ref (contact);
384
385                 e_destination_clear (dest);
386
387                 dest->priv->contact = contact;
388
389                 dest->priv->contact_uid = e_contact_get (dest->priv->contact, E_CONTACT_UID);
390
391                 dest->priv->email_num = email_num;
392
393                 dest->priv->ignored = FALSE;
394
395                 /* handle the mailing list case */
396                 if (e_contact_get (dest->priv->contact, E_CONTACT_IS_LIST)) {
397                         GList *email = e_contact_get_attributes (dest->priv->contact, E_CONTACT_EMAIL);
398
399                         if (email) {
400                                 GList *iter;
401
402                                 for (iter = email; iter; iter = iter->next) {
403                                         EVCardAttribute *attr = iter->data;
404                                         GList *p;
405                                         EDestination *list_dest = e_destination_new ();
406                                         char *contact_uid = NULL;
407                                         char *email_addr = NULL;
408                                         char *name = NULL;
409                                         int email_num = -1;
410                                         gboolean html_pref = FALSE;
411
412                                         for (p = e_vcard_attribute_get_params (attr); p; p = p->next) {
413                                                 EVCardAttributeParam *param = p->data;
414                                                 const char *param_name = e_vcard_attribute_param_get_name (param);
415                                                 if (!g_ascii_strcasecmp (param_name,
416                                                                          EVC_X_DEST_CONTACT_UID)) {
417                                                         GList *v = e_vcard_attribute_param_get_values (param);
418                                                         contact_uid = v ? g_strdup (v->data) : NULL;
419                                                 }
420                                                 else if (!g_ascii_strcasecmp (param_name,
421                                                                               EVC_X_DEST_EMAIL_NUM)) {
422                                                         GList *v = e_vcard_attribute_param_get_values (param);
423                                                         email_num = v ? atoi (v->data) : -1;
424                                                 }
425                                                 else if (!g_ascii_strcasecmp (param_name,
426                                                                               EVC_X_DEST_NAME)) {
427                                                         GList *v = e_vcard_attribute_param_get_values (param);
428                                                         name = v ? v->data : NULL;
429                                                 }
430                                                 else if (!g_ascii_strcasecmp (param_name,
431                                                                               EVC_X_DEST_EMAIL)) {
432                                                         GList *v = e_vcard_attribute_param_get_values (param);
433                                                         email_addr = v ? v->data : NULL;
434                                                 }
435                                                 else if (!g_ascii_strcasecmp (param_name,
436                                                                               EVC_X_DEST_HTML_MAIL)) {
437                                                         GList *v = e_vcard_attribute_param_get_values (param);
438                                                         html_pref = v ? !g_ascii_strcasecmp (v->data, "true") : FALSE;
439                                                 }
440                                         }
441
442                                         if (contact_uid) e_destination_set_contact_uid (list_dest, contact_uid, email_num);
443                                         if (name) e_destination_set_name (list_dest, name);
444                                         if (email_addr) e_destination_set_email (list_dest, email_addr);
445                                         e_destination_set_html_mail_pref (list_dest, html_pref);
446                                         list_dest->priv->ignored = FALSE;
447
448                                         dest->priv->list_dests = g_list_append (dest->priv->list_dests, list_dest);
449                                 }
450
451                                 g_list_foreach (email, (GFunc) e_vcard_attribute_free, NULL);
452                                 g_list_free (email);
453                         }
454                 }
455                 else {
456                         /* handle the normal contact case */
457                         /* is there anything to do here? */
458                 }
459
460                 g_signal_emit (dest, signals [CHANGED], 0);
461         } else if (dest->priv->email_num != email_num){
462                 /* Splitting here would help the contact lists not rebuiding, so that it remembers ignored values */
463                 g_object_ref (contact);
464
465                 e_destination_clear (dest);
466
467                 dest->priv->contact = contact;
468
469                 dest->priv->contact_uid = e_contact_get (dest->priv->contact, E_CONTACT_UID);
470
471                 dest->priv->email_num = email_num;
472         
473                 g_signal_emit (dest, signals [CHANGED], 0);             
474         }
475         
476 }
477
478 /**
479  * e_destination_set_book:
480  * @dest: an #EDestination
481  * @book: an #EBook
482  *
483  * Specify the source @dest's contact comes from. This is useful
484  * if you need to update the contact later.
485  **/
486 void
487 e_destination_set_book (EDestination *dest, EBook *book)
488 {
489         ESource *source;
490
491         g_return_if_fail (dest && E_IS_DESTINATION (dest));
492         g_return_if_fail (book && E_IS_BOOK (book));
493
494         source = e_book_get_source (book);
495
496         if (!dest->priv->source_uid || strcmp (e_source_peek_uid (source), dest->priv->source_uid)) {
497                 e_destination_clear (dest);
498                 dest->priv->source_uid = g_strdup (e_source_peek_uid (source));
499
500                 g_signal_emit (dest, signals [CHANGED], 0);
501         }
502 }
503
504 /**
505  * e_destination_set_contact_uid:
506  * @dest: an #EDestination
507  * @uid: a unique contact ID
508  * @email_num: an email index
509  *
510  * Sets @dest to point to one of the contact specified by @uid's e-mail
511  * addresses indicated by @email_num.
512  **/
513 void
514 e_destination_set_contact_uid (EDestination *dest, const char *uid, gint email_num)
515 {
516         g_return_if_fail (dest && E_IS_DESTINATION (dest));
517         g_return_if_fail (uid != NULL);
518         
519         if (dest->priv->contact_uid == NULL
520             || strcmp (dest->priv->contact_uid, uid)
521             || dest->priv->email_num != email_num) {
522                 
523                 g_free (dest->priv->contact_uid);
524                 dest->priv->contact_uid = g_strdup (uid);
525                 dest->priv->email_num = email_num;
526                 
527                 /* If we already have a contact, remove it unless it's uid matches the one
528                    we just set. */
529                 if (dest->priv->contact && strcmp (uid,
530                                                    e_contact_get_const (dest->priv->contact, E_CONTACT_UID))) {
531                         g_object_unref (dest->priv->contact);
532                         dest->priv->contact = NULL;
533                 }
534
535                 g_signal_emit (dest, signals [CHANGED], 0);
536         }
537 }
538
539 static void
540 e_destination_set_source_uid (EDestination *dest, const char *uid)
541 {
542         g_return_if_fail (dest && E_IS_DESTINATION (dest));
543         g_return_if_fail (uid != NULL);
544         
545         if (dest->priv->source_uid == NULL
546             || strcmp (dest->priv->source_uid, uid)) {
547
548                 g_free (dest->priv->source_uid);
549                 dest->priv->source_uid = g_strdup (uid);
550
551                 g_signal_emit (dest, signals [CHANGED], 0);
552         }
553 }
554
555 /**
556  * e_destination_set_name:
557  * @dest: an #EDestination
558  * @name: the destination's full name
559  *
560  * Sets the full name of @dest's addressee.
561  **/
562 void
563 e_destination_set_name (EDestination *dest, const char *name)
564 {
565         gboolean changed = FALSE;
566
567         g_return_if_fail (E_IS_DESTINATION (dest));
568         
569         if (name == NULL) {
570                 if (dest->priv->name != NULL) {
571                         g_free (dest->priv->name);
572                         dest->priv->name = NULL;
573                         changed = TRUE;
574                 }
575         } else if (dest->priv->name == NULL || strcmp (dest->priv->name, name)) {
576                 g_free (dest->priv->name);
577                 dest->priv->name = g_strdup (name);
578                 changed = TRUE;
579         }
580         
581         if (changed) {
582                 g_free (dest->priv->addr);
583                 dest->priv->addr = NULL;
584                 g_free (dest->priv->textrep);
585                 dest->priv->textrep = NULL;
586
587                 g_signal_emit (dest, signals [CHANGED], 0);
588         }
589 }
590
591 /**
592  * e_destination_set_email:
593  * @dest: an #EDestination
594  * @email: the destination's e-mail address
595  *
596  * Sets the e-mail address of @dest's addressee.
597  **/
598 void
599 e_destination_set_email (EDestination *dest, const char *email)
600 {
601         gboolean changed = FALSE;
602
603         g_return_if_fail (E_IS_DESTINATION (dest));
604         
605         if (email == NULL) {
606                 if (dest->priv->email != NULL) {
607                         g_free (dest->priv->addr);
608                         dest->priv->addr = NULL;
609                         changed = TRUE;
610                 }
611         } else if (dest->priv->email == NULL || strcmp (dest->priv->email, email)) {
612                 g_free (dest->priv->email);
613                 dest->priv->email = g_strdup (email);
614                 changed = TRUE;
615         }
616         
617         if (changed) {
618                 g_free (dest->priv->addr);
619                 dest->priv->addr = NULL;
620                 g_free (dest->priv->textrep);
621                 dest->priv->textrep = NULL;
622
623                 g_signal_emit (dest, signals [CHANGED], 0);
624         }
625 }
626
627 static gboolean
628 e_destination_from_contact (const EDestination *dest)
629 {
630         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
631         return dest->priv->contact != NULL || dest->priv->source_uid != NULL || dest->priv->contact_uid != NULL;
632 }
633
634 /**
635  * e_destination_is_auto_recipient:
636  * @dest: an #EDestination
637  *
638  * Checks if @dest is flagged as an automatic recipient, meaning
639  * it was not explicitly specified by the user. This can be used
640  * to hide it from some UI elements.
641  *
642  * Return value: %TRUE if destination is an auto recipient, %FALSE otherwise.
643  **/
644 gboolean
645 e_destination_is_auto_recipient (const EDestination *dest)
646 {
647         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
648         
649         return dest->priv->auto_recipient;
650 }
651
652 /**
653  * e_destination_set_auto_recipient:
654  * @dest: an #EDestination
655  * @value: the auto recipient flag
656  * 
657  * Sets the flag indicating if @dest is an automatic recipient, meaning
658  * it was not explicitly specified by the user. This can be used
659  * to hide it from some UI elements.
660  **/
661 void
662 e_destination_set_auto_recipient (EDestination *dest, gboolean value)
663 {
664         g_return_if_fail (dest && E_IS_DESTINATION (dest));
665         
666         dest->priv->auto_recipient = value;
667
668         g_signal_emit (dest, signals [CHANGED], 0);
669 }
670
671 /**
672  * e_destination_get_contact:
673  * @dest: an #EDestination
674  *
675  * Gets the contact @dest is pointing to, if any.
676  *
677  * Return value: An #EContact, or %NULL if none was set.
678  **/
679 EContact *
680 e_destination_get_contact (const EDestination *dest)
681 {
682         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
683         
684         return dest->priv->contact;
685 }
686
687 /**
688  * e_destination_get_contact_uid:
689  * @dest: an #EDestination
690  *
691  * Gets the unique contact ID @dest is pointing to, if any.
692  *
693  * Return value: A unique contact ID, or %NULL if none was set.
694  */
695 const char *
696 e_destination_get_contact_uid (const EDestination *dest)
697 {
698         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
699         
700         return dest->priv->contact_uid;
701 }
702
703 /**
704  * e_destination_get_source_uid:
705  * @dest: an #EDestination
706  *
707  * Gets the unique source ID @dest is pointing to, if any. The source
708  * ID specifies which address book @dest's contact came from.
709  *
710  * Return value: A unique source ID, or %NULL if none was set.
711  */
712 const char *
713 e_destination_get_source_uid (const EDestination *dest)
714 {
715         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
716
717         return dest->priv->source_uid;
718 }
719
720 /**
721  * e_destination_get_email_num:
722  * @dest: an #EDestination
723  *
724  * Gets the index of the e-mail address of the contact that
725  * @dest is pointing to, if any.
726  *
727  * Return value: The e-mail index, or -1 if none was set.
728  **/
729 gint
730 e_destination_get_email_num (const EDestination *dest)
731 {
732         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), -1);
733         
734         if (dest->priv->contact == NULL && (dest->priv->source_uid == NULL || dest->priv->contact_uid == NULL))
735                 return -1;
736         
737         return dest->priv->email_num;
738 }
739
740 /**
741  * e_destination_get_name:
742  * @dest: an #EDestination
743  * 
744  * Gets the full name of @dest's addressee, or if the addressee is
745  * a contact list, the name the list was filed under.
746  *
747  * Return value: The full name of the addressee, or %NULL if none was set.
748  **/
749 const char *
750 e_destination_get_name (const EDestination *dest)
751 {
752         struct _EDestinationPrivate *priv;
753
754         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
755         
756         priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */
757         
758         if (priv->name == NULL) {
759                 if (priv->contact != NULL) {
760                         priv->name = e_contact_get (priv->contact, E_CONTACT_FULL_NAME);
761                         
762                         if (priv->name == NULL || *priv->name == '\0') {
763                                 g_free (priv->name);
764                                 priv->name = e_contact_get (priv->contact, E_CONTACT_FILE_AS);
765                         }
766                         
767                         if (priv->name == NULL || *priv->name == '\0') {
768                                 g_free (priv->name);
769                                 if (e_contact_get (priv->contact, E_CONTACT_IS_LIST))
770                                         priv->name = g_strdup (_("Unnamed List"));
771                                 else
772                                         priv->name = g_strdup (e_destination_get_email (dest));
773                         }
774                 }
775                 else if (priv->raw != NULL) {
776                         CamelInternetAddress *addr = camel_internet_address_new ();
777
778                         if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
779                                 const char *camel_name = NULL;
780                                 
781                                 camel_internet_address_get (addr, 0, &camel_name, NULL);
782                                 priv->name = g_strdup (camel_name);
783                         }
784                         
785                         camel_object_unref (CAMEL_OBJECT (addr));
786                 }
787         }
788         
789         return priv->name;
790 }
791
792 /**
793  * e_destination_is_ignored:
794  * @dest: an #EDestination
795  *
796  * Check if @dest is to be ignored.
797  *
798  * Return value: #TRUE if this destination should be ignored, else #FALSE.
799  */
800 gboolean
801 e_destination_is_ignored (const EDestination *dest)
802 {
803         return dest->priv->ignored;
804 }
805
806 /**
807  * e_destination_set_ignored:
808  * @dest: an #EDestination
809  * @ignored: #TRUE if this #EDestination should be ignored.
810  *
811  * Set the ignore flag on a #EDestination.
812  */
813 void
814 e_destination_set_ignored (EDestination *dest, gboolean ignored)
815 {
816         dest->priv->ignored = ignored;
817 }
818
819 /**
820  * e_destination_get_email:
821  * @dest: an #EDestination
822  *
823  * Gets the e-mail address of @dest's addressee.
824  *
825  * Return value: An e-mail address, or an empty string if none was set.
826  **/
827 const char *
828 e_destination_get_email (const EDestination *dest)
829 {
830         struct _EDestinationPrivate *priv;
831         
832         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
833         
834         priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */
835         
836         if (priv->email == NULL) {
837                 if (priv->contact != NULL) {
838                         /* Pull the address out of the card. */
839                         GList *email = e_contact_get (priv->contact, E_CONTACT_EMAIL);
840                         if (email) {
841                                 char *e = g_list_nth_data (email, priv->email_num);
842
843                                 if (e)
844                                         priv->email = g_strdup (e);
845                         } 
846                         if (email) {
847                                 g_list_foreach (email, (GFunc)g_free, NULL);
848                                 g_list_free (email);
849                         }
850                         
851                 } else if (priv->raw != NULL) {
852                         CamelInternetAddress *addr = camel_internet_address_new ();
853                         
854                         if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
855                                 const char *camel_email = NULL;
856                                 camel_internet_address_get (addr, 0, NULL, &camel_email);
857                                 priv->email = g_strdup (camel_email);
858                         }
859                         
860                         camel_object_unref (CAMEL_OBJECT (addr));
861                 } 
862                 
863                 /* Force e-mail to be non-null... */
864                 if (priv->email == NULL) {
865                         priv->email = g_strdup ("");
866                 }
867         }
868         
869         return priv->email;
870 }
871
872 /**
873  * e_destination_get_address:
874  * @dest: an #EDestination
875  *
876  * Gets the formatted name and e-mail address, or in the case of
877  * lists, the formatted list of e-mail addresses, from @dest.
878  *
879  * Return value: A formatted destination string, or %NULL if the destination was empty.
880  **/
881 const char *
882 e_destination_get_address (const EDestination *dest)
883 {
884         struct _EDestinationPrivate *priv;
885
886         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
887         
888         priv = (struct _EDestinationPrivate *)dest->priv; /* cast out const */
889         
890         if (priv->addr == NULL) {
891                 CamelInternetAddress *addr = camel_internet_address_new ();
892                 
893                 if (e_destination_is_evolution_list (dest)) {
894                         GList *iter = dest->priv->list_dests;
895                         
896                         while (iter) {
897                                 EDestination *list_dest = E_DESTINATION (iter->data);
898                                 
899                                 if (!e_destination_empty (list_dest) && !list_dest->priv->ignored) {
900                                         const char *name, *email;
901                                         name = e_destination_get_name (list_dest);
902                                         email = e_destination_get_email (list_dest);
903
904                                         if (nonempty (name) && nonempty (email))
905                                                 camel_internet_address_add (addr, name, email);
906                                         else if (nonempty (email))
907                                                 camel_address_decode (CAMEL_ADDRESS (addr), email);
908                                         else /* this case loses i suppose, but there's
909                                                 nothing we can do here */
910                                                 camel_address_decode (CAMEL_ADDRESS (addr), name);
911                                 }
912                                 iter = g_list_next (iter);
913                         }
914                         
915                         priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));
916                 } else if (priv->raw) {
917
918                         if (camel_address_unformat (CAMEL_ADDRESS (addr), priv->raw)) {
919                                 priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));
920                         }
921                 } else {
922                         const char *name, *email;
923                         name = e_destination_get_name (dest);
924                         email = e_destination_get_email (dest);
925
926                         if (nonempty (name) && nonempty (email))
927                                 camel_internet_address_add (addr, name, email);
928                         else if (nonempty (email))
929                                 camel_address_decode (CAMEL_ADDRESS (addr), email);
930                         else /* this case loses i suppose, but there's
931                                 nothing we can do here */
932                                 camel_address_decode (CAMEL_ADDRESS (addr), name);
933                         
934                         priv->addr = camel_address_encode (CAMEL_ADDRESS (addr));
935                 }
936                 
937                 camel_object_unref (CAMEL_OBJECT (addr));
938         }
939         
940         return priv->addr;
941 }
942
943 /**
944  * e_destination_set_raw:
945  * @dest: an #EDestination
946  * @raw: an unparsed string
947  *
948  * Sets @dest to point to the name and e-mail address resulting from
949  * parsing the supplied string. Useful for user input.
950  **/
951 void
952 e_destination_set_raw (EDestination *dest, const char *raw)
953 {
954         g_return_if_fail (E_IS_DESTINATION (dest));
955         g_return_if_fail (raw != NULL);
956         
957         if (dest->priv->raw == NULL || strcmp (dest->priv->raw, raw)) {
958                         
959                 e_destination_clear (dest);
960                 dest->priv->raw = g_strdup (raw);
961
962                 g_signal_emit (dest, signals [CHANGED], 0);
963         }
964 }
965
966 /**
967  * e_destination_get_textrep:
968  * @dest: an #EDestination
969  * @include_email: whether to include the e-mail address
970  *
971  * Generates a textual representation of @dest, suitable for referring
972  * to the destination during user interaction.
973  *
974  * Return value: A textual representation of the destination.
975  **/
976 const char *
977 e_destination_get_textrep (const EDestination *dest, gboolean include_email)
978 {
979         const char *name, *email;
980         
981         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
982         
983         if (dest->priv->raw)
984                 return dest->priv->raw;
985         
986         name  = e_destination_get_name (dest);
987         email = e_destination_get_email (dest);
988         
989         if (e_destination_from_contact (dest) && name != NULL && (!include_email || !email || !*email))
990                 return name;
991         
992         /* Make sure that our address gets quoted properly */
993         if (name && email && dest->priv->textrep == NULL) {
994                 CamelInternetAddress *addr = camel_internet_address_new ();
995                 
996                 camel_internet_address_add (addr, name, email);
997                 g_free (dest->priv->textrep);
998                 dest->priv->textrep = camel_address_format (CAMEL_ADDRESS (addr));
999                 camel_object_unref (CAMEL_OBJECT (addr));
1000         }
1001         
1002         if (dest->priv->textrep != NULL)
1003                 return dest->priv->textrep;
1004
1005         if (email)
1006                 return email;
1007         
1008         return "";
1009 }
1010
1011 /**
1012  * e_destination_is_evolution_list:
1013  * @dest: an #EDestination
1014  *
1015  * Checks if @dest is a list of addresses.
1016  *
1017  * Return value: %TRUE if destination is a list, %FALSE if it is an individual.
1018  **/
1019 gboolean
1020 e_destination_is_evolution_list (const EDestination *dest)
1021 {
1022         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
1023
1024         return dest->priv->list_dests != NULL;
1025 }
1026
1027 /**
1028  * e_destination_list_show_addresses:
1029  * @dest: an #EDestination
1030  *
1031  * If @dest is a list, checks if the addresses in the list
1032  * should be presented to the user during interaction.
1033  *
1034  * Return value: %TRUE if addresses should be shown, %FALSE otherwise.
1035  **/
1036 gboolean
1037 e_destination_list_show_addresses (const EDestination *dest)
1038 {
1039         g_return_val_if_fail (E_IS_DESTINATION (dest), FALSE);
1040         
1041         if (dest->priv->contact != NULL)
1042                 return GPOINTER_TO_UINT (e_contact_get (dest->priv->contact, E_CONTACT_LIST_SHOW_ADDRESSES));
1043         
1044         return dest->priv->show_addresses;
1045 }
1046
1047 /**
1048  * e_destination_list_get_dests:
1049  * @dest: an #EDestination
1050  *
1051  * If @dest is a list, gets the list of destinations. The list
1052  * and its elements belong to @dest, and should not be freed.
1053  *
1054  * Return value: A list of elements of type #EDestination, or %NULL.
1055  **/
1056 const GList *
1057 e_destination_list_get_dests (const EDestination *dest)
1058 {
1059         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
1060         
1061         if (!e_destination_is_evolution_list (dest))
1062                 return NULL;
1063
1064         return dest->priv->list_dests;
1065 }
1066
1067 /**
1068  * e_destination_get_html_mail_pref:
1069  * @dest: an #EDestination
1070  *
1071  * Check if @dest wants to get mail formatted as HTML.
1072  *
1073  * Return value: %TRUE if destination wants HTML, %FALSE if not.
1074  **/
1075 gboolean
1076 e_destination_get_html_mail_pref (const EDestination *dest)
1077 {
1078         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
1079         
1080         if (dest->priv->html_mail_override || dest->priv->contact == NULL)
1081                 return dest->priv->wants_html_mail;
1082         
1083         return e_contact_get (dest->priv->contact, E_CONTACT_WANTS_HTML) ? TRUE : FALSE;
1084 }
1085
1086 /**
1087  * e_destination_set_html_mail_pref:
1088  * @dest: an #EDestination
1089  * @flag: whether the destination wants HTML mail
1090  *
1091  * Specifies whether @dest wants to get mail formatted as HTML.
1092  **/
1093 void
1094 e_destination_set_html_mail_pref (EDestination *dest, gboolean flag)
1095 {
1096         g_return_if_fail (dest && E_IS_DESTINATION (dest));
1097         
1098         dest->priv->html_mail_override = TRUE;
1099         if (dest->priv->wants_html_mail != flag) {
1100                 dest->priv->wants_html_mail = flag;
1101
1102                 g_signal_emit (dest, signals [CHANGED], 0);
1103         }
1104 }
1105
1106 /*
1107  * Destination import/export
1108  */
1109
1110 /**
1111  * e_destination_get_textrepv:
1112  * @destv: %NULL-terminated array of pointers to #EDestination
1113  *
1114  * Generates a joint text representation of all the #EDestination
1115  * elements in @destv.
1116  *
1117  * Return value: The text representation of @destv.
1118  **/
1119 char *
1120 e_destination_get_textrepv (EDestination **destv)
1121 {
1122         int i, j, len = 0;
1123         char **strv;
1124         char *str;
1125
1126         g_return_val_if_fail (destv, NULL);
1127         
1128         /* Q: Please tell me this is only for assertion
1129            reasons. If this is considered to be ok behavior then you
1130            shouldn't use g_return's. Just a reminder ;-) 
1131            
1132            A: Yes, this is just an assertion.  (Though it does find the
1133            length of the vector in the process...)
1134         */
1135         while (destv[len]) {
1136                 g_return_val_if_fail (E_IS_DESTINATION (destv[len]), NULL);
1137                 len++;
1138         }
1139         
1140         strv = g_new0 (char *, len + 1);
1141         for (i = 0, j = 0; destv[i]; i++) {
1142                 if (!e_destination_empty (destv[i])) {
1143                         const char *addr = e_destination_get_address (destv[i]);
1144                         strv[j++] = addr ? (char *) addr : "";
1145                 }
1146         }
1147         
1148         str = g_strjoinv (", ", strv);
1149         
1150         g_free (strv);
1151         
1152         return str;
1153 }
1154
1155 /**
1156  * e_destination_xml_encode:
1157  * @dest: an #EDestination
1158  *
1159  * Generates an XML tree from @dest.
1160  *
1161  * Return value: Pointer to the root node of the XML tree.
1162  **/
1163 xmlNodePtr
1164 e_destination_xml_encode (const EDestination *dest)
1165 {
1166         xmlNodePtr dest_node;
1167         const char *str;
1168         
1169         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
1170         
1171         dest_node = xmlNewNode (NULL, (xmlChar*)"destination");
1172         
1173         str = e_destination_get_name (dest);
1174         if (str)
1175                 xmlNewTextChild (dest_node, NULL, (xmlChar*)"name", (xmlChar*)str);
1176         
1177         if (!e_destination_is_evolution_list (dest)) {
1178                 str = e_destination_get_email (dest);
1179                 if (str)
1180                         xmlNewTextChild (dest_node, NULL, (xmlChar*)"email", (xmlChar*)str);
1181         } else {
1182                 GList *iter = dest->priv->list_dests;
1183                 
1184                 while (iter) {
1185                         EDestination *list_dest = E_DESTINATION (iter->data);
1186                         xmlNodePtr list_node = xmlNewNode (NULL, (xmlChar*)"list_entry");
1187
1188                         str = e_destination_get_name (list_dest);
1189                         if (str) {
1190                                 xmlChar *escaped = xmlEncodeEntitiesReentrant (NULL, (xmlChar*)str);
1191                                 xmlNewTextChild (list_node, NULL, (xmlChar*)"name", escaped);
1192                                 xmlFree (escaped);
1193                         }
1194                         
1195                         str = e_destination_get_email (list_dest);
1196                         if (str) {
1197                                 xmlChar *escaped = xmlEncodeEntitiesReentrant (NULL, (xmlChar*)str);
1198                                 xmlNewTextChild (list_node, NULL, (xmlChar*)"email", escaped);
1199                                 xmlFree (escaped);
1200                         }
1201                         
1202                         xmlAddChild (dest_node, list_node);
1203                         
1204                         iter = g_list_next (iter);
1205                 }
1206                 
1207                 xmlNewProp (dest_node, (xmlChar*)"is_list", (xmlChar*)"yes");
1208                 xmlNewProp (dest_node, (xmlChar*)"show_addresses", 
1209                             e_destination_list_show_addresses (dest) ? (xmlChar*)"yes" : (xmlChar*)"no");
1210         }
1211         
1212         str = e_destination_get_source_uid (dest);
1213         if (str) {
1214                 xmlChar *escaped = xmlEncodeEntitiesReentrant (NULL, (xmlChar*)str);
1215                 xmlNewTextChild (dest_node, NULL, (xmlChar*)"source_uid", escaped);
1216                 xmlFree (escaped);
1217         }
1218         
1219         str = e_destination_get_contact_uid (dest);
1220         if (str) {
1221                 char buf[16];
1222                 
1223                 xmlNodePtr uri_node = xmlNewTextChild (dest_node, NULL, (xmlChar*)"card_uid", (xmlChar*)str);
1224                 g_snprintf (buf, 16, "%d", e_destination_get_email_num (dest));
1225                 xmlNewProp (uri_node, (xmlChar*)"email_num", (xmlChar*)buf);
1226         }
1227         
1228         xmlNewProp (dest_node, (xmlChar*)"html_mail", e_destination_get_html_mail_pref (dest) ? (xmlChar*)"yes" : (xmlChar*)"no");
1229         
1230         xmlNewProp (dest_node, (xmlChar*)"auto_recipient",
1231                     e_destination_is_auto_recipient (dest) ? (xmlChar*)"yes" : (xmlChar*)"no");
1232         
1233         return dest_node;
1234 }
1235
1236 /**
1237  * e_destination_xml_decode:
1238  * @dest: an #EDestination
1239  * @node: the root node of an XML tree
1240  *
1241  * Initializes @dest based on the information encoded in the
1242  * XML tree under @node.
1243  *
1244  * Return value: %TRUE if the XML tree was well-formed, %FALSE otherwise.
1245  **/
1246 gboolean
1247 e_destination_xml_decode (EDestination *dest, xmlNodePtr node)
1248 {
1249         char *name = NULL, *email = NULL, *source_uid = NULL, *card_uid = NULL;
1250         gboolean is_list = FALSE, show_addr = FALSE, auto_recip = FALSE;
1251         gboolean html_mail = FALSE;
1252         GList *list_dests = NULL;
1253         int email_num = -1;
1254         char *tmp;
1255
1256         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), FALSE);
1257         g_return_val_if_fail (node != NULL, FALSE);
1258         
1259         if (strcmp ((char*)node->name, "destination"))
1260                 return FALSE;
1261         
1262         tmp = (char*)xmlGetProp (node, (xmlChar*)"html_mail");
1263         if (tmp) {
1264                 html_mail = !strcmp (tmp, "yes");
1265                 xmlFree (tmp);
1266         }
1267         
1268         tmp = (char*)xmlGetProp (node, (xmlChar*)"is_list");
1269         if (tmp) {
1270                 is_list = !strcmp (tmp, "yes");
1271                 xmlFree (tmp);
1272         }
1273         
1274         tmp = (char*)xmlGetProp (node, (xmlChar*)"show_addresses");
1275         if (tmp) {
1276                 show_addr = !strcmp (tmp, "yes");
1277                 xmlFree (tmp);
1278         }
1279         
1280         tmp = (char*)xmlGetProp (node, (xmlChar*)"auto_recipient");
1281         if (tmp) {
1282                 auto_recip = !strcmp (tmp, "yes");
1283                 xmlFree (tmp);
1284         }
1285         
1286         node = node->xmlChildrenNode;
1287         while (node) {
1288                 if (!strcmp ((char*)node->name, "name")) {
1289                         tmp = (char*)xmlNodeGetContent (node);
1290                         g_free (name);
1291                         name = g_strdup (tmp);
1292                         xmlFree (tmp);
1293                 } else if (!is_list && !strcmp ((char*)node->name, "email")) {
1294                         tmp = (char*)xmlNodeGetContent (node);
1295                         g_free (email);
1296                         email = g_strdup (tmp);
1297                         xmlFree (tmp);
1298                 } else if (is_list && !strcmp ((char*)node->name, "list_entry")) {
1299                         xmlNodePtr subnode = node->xmlChildrenNode;
1300                         char *list_name = NULL, *list_email = NULL;
1301                         
1302                         while (subnode) {
1303                                 if (!strcmp ((char*)subnode->name, "name")) {
1304                                         tmp = (char*)xmlNodeGetContent (subnode);
1305                                         g_free (list_name);
1306                                         list_name = g_strdup (tmp);
1307                                         xmlFree (tmp);
1308                                 } else if (!strcmp ((char*)subnode->name, "email")) {
1309                                         tmp = (char*)xmlNodeGetContent (subnode);
1310                                         g_free (list_email);
1311                                         list_email = g_strdup (tmp);
1312                                         xmlFree (tmp);
1313                                 }
1314                                 
1315                                 subnode = subnode->next;
1316                         }
1317                         
1318                         if (list_name || list_email) {
1319                                 EDestination *list_dest = e_destination_new ();
1320                                 
1321                                 if (list_name)
1322                                         e_destination_set_name (list_dest, list_name);
1323                                 if (list_email)
1324                                         e_destination_set_email (list_dest, list_email);
1325                                 
1326                                 g_free (list_name);
1327                                 g_free (list_email);
1328                                 
1329                                 list_dests = g_list_append (list_dests, list_dest);
1330                         }
1331                 } else if (!strcmp ((char*)node->name, "source_uid")) {
1332                         tmp = (char*)xmlNodeGetContent (node);
1333                         g_free (source_uid);
1334                         source_uid = g_strdup (tmp);
1335                         xmlFree (tmp);
1336                 } else if (!strcmp ((char*)node->name, "card_uid")) {
1337                         tmp = (char*)xmlNodeGetContent (node);
1338                         g_free (card_uid);
1339                         card_uid = g_strdup (tmp);
1340                         xmlFree (tmp);
1341                         
1342                         tmp = (char*)xmlGetProp (node, (xmlChar*)"email_num");
1343                         email_num = atoi (tmp);
1344                         xmlFree (tmp);
1345                 }
1346                 
1347                 node = node->next;
1348         }
1349         
1350         e_destination_clear (dest);
1351         
1352         if (name) {
1353                 e_destination_set_name (dest, name);
1354                 g_free (name);
1355         }
1356         if (email) {
1357                 e_destination_set_email (dest, email);
1358                 g_free (email);
1359         }
1360         if (source_uid) {
1361                 e_destination_set_source_uid (dest, source_uid);
1362                 g_free (source_uid);
1363         }
1364         if (card_uid) {
1365                 e_destination_set_contact_uid (dest, card_uid, email_num);
1366                 g_free (card_uid);
1367         }
1368         if (list_dests)
1369                 dest->priv->list_dests = list_dests;
1370         
1371         dest->priv->html_mail_override = TRUE;
1372         dest->priv->wants_html_mail = html_mail;
1373         
1374         dest->priv->show_addresses = show_addr;
1375         
1376         dest->priv->auto_recipient = auto_recip;
1377         
1378         return TRUE;
1379 }
1380
1381 static char *
1382 null_terminate_and_remove_extra_whitespace (xmlChar *xml_in, gint size)
1383 {
1384         gboolean skip_white = FALSE;
1385         char *xml, *r, *w;
1386
1387         if (xml_in == NULL || size <= 0) 
1388                 return NULL;
1389         
1390         xml = g_strndup ((char*)xml_in, size);
1391         r = w = xml;
1392         
1393         while (*r) {
1394                 if (*r == '\n' || *r == '\r') {
1395                         skip_white = TRUE;
1396                 } else {
1397                         gunichar c = g_utf8_get_char (r);
1398                         gboolean is_space = g_unichar_isspace (c);
1399                         
1400                         *w = *r;
1401                         
1402                         if (!(skip_white && is_space))
1403                                 w++;
1404                         if (!is_space)
1405                                 skip_white = FALSE;
1406                 }
1407                 r = g_utf8_next_char (r);
1408         }
1409         
1410         *w = '\0';
1411         
1412         return xml;
1413 }
1414
1415 /**
1416  * e_destination_export:
1417  * @dest: an #EDestination
1418  *
1419  * Exports a destination to an XML document.
1420  *
1421  * Return value: An XML string, allocated with g_malloc.
1422  **/
1423 char *
1424 e_destination_export (const EDestination *dest)
1425 {
1426         xmlNodePtr dest_node;
1427         xmlDocPtr dest_doc;
1428         xmlChar *buffer = NULL;
1429         int size = -1;
1430         char *str;
1431
1432         g_return_val_if_fail (dest && E_IS_DESTINATION (dest), NULL);
1433         
1434         dest_node = e_destination_xml_encode (dest);
1435         if (dest_node == NULL)
1436                 return NULL;
1437         
1438         dest_doc = xmlNewDoc ((xmlChar*)XML_DEFAULT_VERSION);
1439         xmlDocSetRootElement (dest_doc, dest_node);
1440         
1441         xmlDocDumpMemory (dest_doc, &buffer, &size);
1442         xmlFreeDoc (dest_doc);
1443         
1444         str = null_terminate_and_remove_extra_whitespace (buffer, size);
1445         xmlFree (buffer);
1446         
1447         return str;
1448 }
1449
1450 /**
1451  * e_destination_import
1452  * @str: an XML string
1453  *
1454  * Creates an #EDestination from an XML document.
1455  *
1456  * Return value: An #EDestination, or %NULL if the document was not well-formed.
1457  **/
1458 EDestination *
1459 e_destination_import (const char *str)
1460 {
1461         EDestination *dest = NULL;
1462         xmlDocPtr dest_doc;
1463
1464         if (!(str && *str))
1465                 return NULL;
1466         
1467         dest_doc = xmlParseMemory ((char *) str, strlen (str));
1468         if (dest_doc && dest_doc->xmlRootNode) {
1469                 dest = e_destination_new ();
1470                 if (! e_destination_xml_decode (dest, dest_doc->xmlRootNode)) {
1471                         g_object_unref (dest);
1472                         dest = NULL;
1473                 }
1474         }
1475         xmlFreeDoc (dest_doc);
1476         
1477         return dest;
1478 }
1479
1480 /**
1481  * e_destination_exportv:
1482  * @destv: a %NULL-terminated array of pointers to #EDestination
1483  *
1484  * Exports multiple #EDestination elements to a single XML document.
1485  *
1486  * Return value: An XML string, allocated with g_malloc.
1487  **/
1488 char *
1489 e_destination_exportv (EDestination **destv)
1490 {
1491         xmlDocPtr destv_doc;
1492         xmlNodePtr destv_node;
1493         xmlChar *buffer = NULL;
1494         int i, size = -1;
1495         char *str;
1496
1497         if (destv == NULL || *destv == NULL)
1498                 return NULL;
1499         
1500         destv_doc  = xmlNewDoc ((xmlChar*)XML_DEFAULT_VERSION);
1501         destv_node = xmlNewNode (NULL, (xmlChar*)"destinations");
1502         xmlDocSetRootElement (destv_doc, destv_node);
1503         
1504         for (i = 0; destv[i]; i++) {
1505                 if (! e_destination_empty (destv[i])) {
1506                         xmlNodePtr dest_node = e_destination_xml_encode (destv[i]);
1507                         if (dest_node)
1508                                 xmlAddChild (destv_node, dest_node);
1509                 }
1510         }
1511         
1512         xmlDocDumpMemory (destv_doc, &buffer, &size);
1513         xmlFreeDoc (destv_doc);
1514         
1515         str = null_terminate_and_remove_extra_whitespace (buffer, size);
1516         xmlFree (buffer);
1517         
1518         return str;
1519 }
1520
1521 /**
1522  * e_destination_importv:
1523  * @str: an XML string
1524  *
1525  * Creates an array of pointers to #EDestination elements
1526  * from an XML document.
1527  *
1528  * Return value: A %NULL-terminated array of pointers to #EDestination elements.
1529  **/
1530 EDestination **
1531 e_destination_importv (const char *str)
1532 {
1533         GPtrArray *dest_array = NULL;
1534         xmlDocPtr destv_doc;
1535         xmlNodePtr node;
1536         EDestination **destv = NULL;
1537         
1538         if (!(str && *str))
1539                 return NULL;
1540         
1541         destv_doc = xmlParseMemory ((char *)str, strlen (str));
1542         if (destv_doc == NULL)
1543                 return NULL;
1544         
1545         node = destv_doc->xmlRootNode;
1546         
1547         if (strcmp ((char*)node->name, "destinations"))
1548                 goto finished;
1549         
1550         node = node->xmlChildrenNode;
1551         
1552         dest_array = g_ptr_array_new ();
1553         
1554         while (node) {
1555                 EDestination *dest;
1556                 
1557                 dest = e_destination_new ();
1558                 if (e_destination_xml_decode (dest, node) && !e_destination_empty (dest)) {
1559                         g_ptr_array_add (dest_array, dest);
1560                 } else {
1561                         g_object_unref (dest);
1562                 }
1563                 
1564                 node = node->next;
1565         }
1566         
1567         /* we need destv to be NULL terminated */
1568         g_ptr_array_add (dest_array, NULL);
1569         
1570         destv = (EDestination **) dest_array->pdata;
1571         g_ptr_array_free (dest_array, FALSE);
1572         
1573  finished:
1574         xmlFreeDoc (destv_doc);
1575         
1576         return destv;
1577 }
1578
1579 /**
1580  * e_destination_freev:
1581  * @destv: a %NULL-terminated array of pointers to #EDestination
1582  *
1583  * Unrefs the elements of @destv and frees @destv itself.
1584  **/
1585 void
1586 e_destination_freev (EDestination **destv)
1587 {
1588         int i;
1589         
1590         if (destv) {
1591                 for (i = 0; destv[i] != NULL; ++i) {
1592                         g_object_unref (destv[i]);
1593                 }
1594                 g_free (destv);
1595         }
1596
1597 }
1598
1599 /**
1600  * e_destination_export_to_vcard_attribute:
1601  * @dest: an #EDestination
1602  * @attr: an #EVCardAttribute
1603  *
1604  * Exports the contact information from @dest to parameters
1605  * and values in @attr, suitable for an address book.
1606  **/
1607 void
1608 e_destination_export_to_vcard_attribute (EDestination *dest, EVCardAttribute *attr)
1609 {
1610         e_vcard_attribute_remove_values (attr);
1611         e_vcard_attribute_remove_params (attr);
1612
1613         if (e_destination_get_contact_uid (dest))
1614                 e_vcard_attribute_add_param_with_value (attr,
1615                                                         e_vcard_attribute_param_new (EVC_X_DEST_CONTACT_UID),
1616                                                         e_destination_get_contact_uid (dest));
1617         if (e_destination_get_source_uid (dest))
1618                 e_vcard_attribute_add_param_with_value (attr,
1619                                                         e_vcard_attribute_param_new (EVC_X_DEST_SOURCE_UID),
1620                                                         e_destination_get_source_uid (dest));
1621         if (-1 != e_destination_get_email_num (dest)) {
1622                 char buf[10];
1623                 g_snprintf (buf, sizeof (buf), "%d", e_destination_get_email_num (dest));
1624                 e_vcard_attribute_add_param_with_value (attr,
1625                                                         e_vcard_attribute_param_new (EVC_X_DEST_EMAIL_NUM),
1626                                                         buf);
1627         }
1628         if (e_destination_get_name (dest))
1629                 e_vcard_attribute_add_param_with_value (attr,
1630                                                         e_vcard_attribute_param_new (EVC_X_DEST_NAME),
1631                                                         e_destination_get_name (dest));
1632         if (e_destination_get_email (dest))
1633                 e_vcard_attribute_add_param_with_value (attr,
1634                                                         e_vcard_attribute_param_new (EVC_X_DEST_EMAIL),
1635                                                         e_destination_get_email (dest));
1636         e_vcard_attribute_add_param_with_value (attr,
1637                                                 e_vcard_attribute_param_new (EVC_X_DEST_HTML_MAIL),
1638                                                 e_destination_get_html_mail_pref (dest) ? "TRUE" : "FALSE");
1639
1640         if (e_destination_get_address (dest))
1641                 e_vcard_attribute_add_value (attr, e_destination_get_address (dest));
1642 }