aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/eab-composer-util.c
blob: ce3a96cc13a9e79f2b4339c71ed28c189cb32efd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "eab-composer-util.h"

#include <glib/gi18n.h>
#include <libebook/e-contact.h>
#include <libebook/e-destination.h>

#include "mail/em-composer-utils.h"
#include "composer/e-msg-composer.h"
#include "addressbook/util/eab-book-util.h"
#include "addressbook/gui/widgets/eab-gui-util.h"

static void
eab_send_as_to (GList *destinations)
{
    EMsgComposer *composer;
    EComposerHeaderTable *table;
    GPtrArray *to_array;
    GPtrArray *bcc_array;

    union {
        gpointer *pdata;
        EDestination **destinations;
    } convert;

    if (destinations == NULL)
        return;

    composer = e_msg_composer_new ();
    table = e_msg_composer_get_header_table (composer);
    em_composer_utils_setup_default_callbacks (composer);

    to_array = g_ptr_array_new ();
    bcc_array = g_ptr_array_new ();

    /* Sort contacts into "To" and "Bcc" destinations. */
    while (destinations != NULL) {
        EDestination *destination = destinations->data;

        if (e_destination_is_evolution_list (destination)) {
            if (e_destination_list_show_addresses (destination))
                g_ptr_array_add (to_array, destination);
            else
                g_ptr_array_add (bcc_array, destination);
        } else
            g_ptr_array_add (to_array, destination);

        destinations = g_list_next (destinations);
    }

    /* Add sentinels to each array. */
    g_ptr_array_add (to_array, NULL);
    g_ptr_array_add (bcc_array, NULL);

    /* XXX Acrobatics like this make me question whether NULL-terminated
     *     arrays are really the best argument type for passing a list of
     *     destinations to the header table. */

    /* Add "To" destinations. */
    convert.pdata = to_array->pdata;
    e_composer_header_table_set_destinations_to (
        table, convert.destinations);
    g_ptr_array_free (to_array, FALSE);
    e_destination_freev (convert.destinations);

    /* Add "Bcc" destinations. */
    convert.pdata = bcc_array->pdata;
    e_composer_header_table_set_destinations_bcc (
        table, convert.destinations);
    g_ptr_array_free (bcc_array, FALSE);
    e_destination_freev (convert.destinations);

    gtk_widget_show (GTK_WIDGET (composer));
}

static const char *
get_email (EContact *contact, EContactField field_id, gchar **to_free)
{
    char *name = NULL, *mail = NULL;
    const char *value = e_contact_get_const (contact, field_id);

    *to_free = NULL;

    if (eab_parse_qp_email (value, &name, &mail)) {
        *to_free = g_strdup_printf ("%s <%s>", name, mail);
        value = *to_free;
    }

    g_free (name);
    g_free (mail);

    return value;
}

static void
eab_send_as_attachment (GList *destinations)
{
    EMsgComposer *composer;
    EComposerHeaderTable *table;
    CamelMimePart *attachment;
    GList *contacts, *iter;
    gchar *data;

    if (contacts == NULL)
        return;

    composer = e_msg_composer_new ();
    table = e_msg_composer_get_header_table (composer);
    em_composer_utils_setup_default_callbacks (composer);

    attachment = camel_mime_part_new ();

    contacts = g_list_copy (destinations);
    for (iter = contacts; iter != NULL; iter = iter->next)
        iter->data = e_destination_get_contact (iter->data);
    data = eab_contact_list_to_string (contacts);
    g_list_free (contacts);

    camel_mime_part_set_content (
        attachment, data, strlen (data), "text/x-vcard");

    if (destinations->next != NULL)
        camel_mime_part_set_description (
            attachment, _("Multiple vCards"));
    else {
        EContact *contact;
        const gchar *file_as;
        gchar *description;

        contact = e_destination_get_contact (destinations->data);
        file_as = e_contact_get_const (contact, E_CONTACT_FILE_AS);
        description = g_strdup_printf (_("vCard for %s"), file_as);
        camel_mime_part_set_description (attachment, description);
        g_free (description);
    }

    camel_mime_part_set_disposition (attachment, "attachment");

    e_msg_composer_attach (composer, attachment);
    camel_object_unref (attachment);

    if (destinations->next != NULL)
        e_composer_header_table_set_subject (
            table, _("Contact information"));
    else {
        EContact *contact;
        gchar *tempstr;
        const gchar *tempstr2;
        gchar *tempfree = NULL;

        contact = e_destination_get_contact (destinations->data);
        tempstr2 = e_contact_get_const (contact, E_CONTACT_FILE_AS);
        if (!tempstr2 || !*tempstr2)
            tempstr2 = e_contact_get_const (contact, E_CONTACT_FULL_NAME);
        if (!tempstr2 || !*tempstr2)
            tempstr2 = e_contact_get_const (contact, E_CONTACT_ORG);
        if (!tempstr2 || !*tempstr2) {
            g_free (tempfree);
            tempstr2 = get_email (contact, E_CONTACT_EMAIL_1, &tempfree);
        }
        if (!tempstr2 || !*tempstr2) {
            g_free (tempfree);
            tempstr2 = get_email (contact, E_CONTACT_EMAIL_2, &tempfree);
        }
        if (!tempstr2 || !*tempstr2) {
            g_free (tempfree);
            tempstr2 = get_email (contact, E_CONTACT_EMAIL_3, &tempfree);
        }

        if (!tempstr2 || !*tempstr2)
            tempstr = g_strdup_printf (_("Contact information"));
        else
            tempstr = g_strdup_printf (_("Contact information for %s"), tempstr2);

        e_composer_header_table_set_subject (table, tempstr);

        g_free (tempstr);
        g_free (tempfree);
    }

    gtk_widget_show (GTK_WIDGET (composer));
}

void
eab_send_message (GList *destinations,
                  EABDisposition disposition)
{
    switch (disposition) {
        case EAB_DISPOSITION_AS_TO:
            eab_send_as_to (destinations);
            break;

        case EAB_DISPOSITION_AS_ATTACHMENT:
            eab_send_as_attachment (destinations);
            break;
    }
}