evolution-3.6.4/modules/addressbook/eab-composer-util.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 16  *
 17  */
 18 
 19 #ifdef HAVE_CONFIG_H
 20 #include <config.h>
 21 #endif
 22 
 23 #include "eab-composer-util.h"
 24 
 25 #include <string.h>
 26 #include <glib/gi18n.h>
 27 #include <libebook/libebook.h>
 28 
 29 #include "composer/e-msg-composer.h"
 30 #include "addressbook/util/eab-book-util.h"
 31 #include "addressbook/gui/widgets/eab-gui-util.h"
 32 
 33 void
 34 eab_send_as_to (EShell *shell,
 35                 GSList *destinations)
 36 {
 37 	EMsgComposer *composer;
 38 	EComposerHeaderTable *table;
 39 	GPtrArray *to_array;
 40 	GPtrArray *bcc_array;
 41 
 42 	union {
 43 		gpointer *pdata;
 44 		EDestination **destinations;
 45 	} convert;
 46 
 47 	g_return_if_fail (E_IS_SHELL (shell));
 48 
 49 	if (destinations == NULL)
 50 		return;
 51 
 52 	composer = e_msg_composer_new (shell);
 53 	table = e_msg_composer_get_header_table (composer);
 54 
 55 	to_array = g_ptr_array_new ();
 56 	bcc_array = g_ptr_array_new ();
 57 
 58 	/* Sort contacts into "To" and "Bcc" destinations. */
 59 	while (destinations != NULL) {
 60 		EDestination *destination = destinations->data;
 61 
 62 		if (e_destination_is_evolution_list (destination)) {
 63 			if (e_destination_list_show_addresses (destination))
 64 				g_ptr_array_add (to_array, destination);
 65 			else
 66 				g_ptr_array_add (bcc_array, destination);
 67 		} else
 68 			g_ptr_array_add (to_array, destination);
 69 
 70 		destinations = g_slist_next (destinations);
 71 	}
 72 
 73 	/* Add sentinels to each array. */
 74 	g_ptr_array_add (to_array, NULL);
 75 	g_ptr_array_add (bcc_array, NULL);
 76 
 77 	/* XXX Acrobatics like this make me question whether NULL-terminated
 78 	 *     arrays are really the best argument type for passing a list of
 79 	 *     destinations to the header table. */
 80 
 81 	/* Set "To" destinations. */
 82 	convert.pdata = to_array->pdata;
 83 	e_composer_header_table_set_destinations_to (
 84 		table, convert.destinations);
 85 	g_ptr_array_free (to_array, FALSE);
 86 
 87 	/* Add "Bcc" destinations. */
 88 	convert.pdata = bcc_array->pdata;
 89 	e_composer_header_table_add_destinations_bcc (
 90 		table, convert.destinations);
 91 	g_ptr_array_free (bcc_array, FALSE);
 92 
 93 	gtk_widget_show (GTK_WIDGET (composer));
 94 }
 95 
 96 static const gchar *
 97 get_email (EContact *contact,
 98            EContactField field_id,
 99            gchar **to_free)
100 {
101 	gchar *name = NULL, *mail = NULL;
102 	const gchar *value = e_contact_get_const (contact, field_id);
103 
104 	*to_free = NULL;
105 
106 	if (eab_parse_qp_email (value, &name, &mail)) {
107 		*to_free = g_strdup_printf ("%s <%s>", name, mail);
108 		value = *to_free;
109 	}
110 
111 	g_free (name);
112 	g_free (mail);
113 
114 	return value;
115 }
116 
117 void
118 eab_send_as_attachment (EShell *shell,
119                         GSList *destinations)
120 {
121 	EMsgComposer *composer;
122 	EComposerHeaderTable *table;
123 	CamelMimePart *attachment;
124 	GSList *contacts, *iter;
125 	gchar *data;
126 
127 	g_return_if_fail (E_IS_SHELL (shell));
128 
129 	if (destinations == NULL)
130 		return;
131 
132 	composer = e_msg_composer_new (shell);
133 	table = e_msg_composer_get_header_table (composer);
134 
135 	attachment = camel_mime_part_new ();
136 
137 	contacts = g_slist_copy (destinations);
138 	for (iter = contacts; iter != NULL; iter = iter->next)
139 		iter->data = e_destination_get_contact (iter->data);
140 	data = eab_contact_list_to_string (contacts);
141 	g_slist_free (contacts);
142 
143 	camel_mime_part_set_content (
144 		attachment, data, strlen (data), "text/x-vcard");
145 
146 	if (destinations->next != NULL)
147 		camel_mime_part_set_description (
148 			attachment, _("Multiple vCards"));
149 	else {
150 		EContact *contact;
151 		const gchar *file_as;
152 		gchar *description;
153 
154 		contact = e_destination_get_contact (destinations->data);
155 		file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
156 		description = g_strdup_printf (_("vCard for %s"), file_as);
157 		camel_mime_part_set_description (attachment, description);
158 		g_free (description);
159 	}
160 
161 	camel_mime_part_set_disposition (attachment, "attachment");
162 
163 	e_msg_composer_attach (composer, attachment);
164 	g_object_unref (attachment);
165 
166 	if (destinations->next != NULL)
167 		e_composer_header_table_set_subject (
168 			table, _("Contact information"));
169 	else {
170 		EContact *contact;
171 		gchar *tempstr;
172 		const gchar *tempstr2;
173 		gchar *tempfree = NULL;
174 
175 		contact = e_destination_get_contact (destinations->data);
176 		tempstr2 = e_contact_get_const (contact, E_CONTACT_FILE_AS);
177 		if (!tempstr2 || !*tempstr2)
178 			tempstr2 = e_contact_get_const (contact, E_CONTACT_FULL_NAME);
179 		if (!tempstr2 || !*tempstr2)
180 			tempstr2 = e_contact_get_const (contact, E_CONTACT_ORG);
181 		if (!tempstr2 || !*tempstr2) {
182 			g_free (tempfree);
183 			tempstr2 = get_email (contact, E_CONTACT_EMAIL_1, &tempfree);
184 		}
185 		if (!tempstr2 || !*tempstr2) {
186 			g_free (tempfree);
187 			tempstr2 = get_email (contact, E_CONTACT_EMAIL_2, &tempfree);
188 		}
189 		if (!tempstr2 || !*tempstr2) {
190 			g_free (tempfree);
191 			tempstr2 = get_email (contact, E_CONTACT_EMAIL_3, &tempfree);
192 		}
193 
194 		if (!tempstr2 || !*tempstr2)
195 			tempstr = g_strdup_printf (_("Contact information"));
196 		else
197 			tempstr = g_strdup_printf (_("Contact information for %s"), tempstr2);
198 
199 		e_composer_header_table_set_subject (table, tempstr);
200 
201 		g_free (tempstr);
202 		g_free (tempfree);
203 	}
204 
205 	gtk_widget_show (GTK_WIDGET (composer));
206 }