aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/vcard-inline/vcard-inline.c
blob: 24e551afe97330b19b298af8769516d07bec9a5c (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * 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)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <libebook/e-book-client.h>
#include <libebook/e-contact.h>
#include <gtkhtml/gtkhtml-embedded.h>
#include <libedataserverui/e-client-utils.h>
#include <libedataserverui/e-source-selector-dialog.h>

#include "addressbook/gui/merging/eab-contact-merging.h"
#include "addressbook/gui/widgets/eab-contact-display.h"
#include "addressbook/util/eab-book-util.h"
#include "mail/em-format-hook.h"
#include "mail/em-format-html.h"

#define d(x)

typedef struct _VCardInlinePObject VCardInlinePObject;

struct _VCardInlinePObject {
    EMFormatHTMLPObject object;

    GSList *contact_list;
    ESourceList *source_list;
    GtkWidget *contact_display;
    GtkWidget *message_label;
};

static gint org_gnome_vcard_inline_classid;

/* Forward Declarations */
void org_gnome_vcard_inline_format (gpointer ep, EMFormatHookTarget *target);
gint e_plugin_lib_enable (EPlugin *ep, gint enable);

gint
e_plugin_lib_enable (EPlugin *ep, gint enable)
{
    return 0;
}

static void
org_gnome_vcard_inline_pobject_free (EMFormatHTMLPObject *object)
{
    VCardInlinePObject *vcard_object;

    vcard_object = (VCardInlinePObject *) object;

    e_client_util_free_object_slist (vcard_object->contact_list);
    vcard_object->contact_list = NULL;

    if (vcard_object->source_list != NULL) {
        g_object_unref (vcard_object->source_list);
        vcard_object->source_list = NULL;
    }

    if (vcard_object->contact_display != NULL) {
        g_object_unref (vcard_object->contact_display);
        vcard_object->contact_display = NULL;
    }

    if (vcard_object->message_label != NULL) {
        g_object_unref (vcard_object->message_label);
        vcard_object->message_label = NULL;
    }
}

static void
org_gnome_vcard_inline_decode (VCardInlinePObject *vcard_object,
                               CamelMimePart *mime_part)
{
    CamelDataWrapper *data_wrapper;
    CamelMedium *medium;
    CamelStream *stream;
    GSList *contact_list;
    GByteArray *array;
    const gchar *string;
    const guint8 padding[2] = {0};

    array = g_byte_array_new ();
    medium = CAMEL_MEDIUM (mime_part);

    /* Stream takes ownership of the byte array. */
    stream = camel_stream_mem_new_with_byte_array (array);
    data_wrapper = camel_medium_get_content (medium);
    camel_data_wrapper_decode_to_stream_sync (
        data_wrapper, stream, NULL, NULL);

    /* because the result is not NULL-terminated */
    g_byte_array_append (array, padding, 2);

    string = (gchar *) array->data;
    contact_list = eab_contact_list_from_string (string);
    vcard_object->contact_list = contact_list;

    g_object_unref (mime_part);
    g_object_unref (stream);
}

static void
org_gnome_vcard_inline_client_loaded_cb (GObject *source_object,
                                         GAsyncResult *result,
                                         gpointer user_data)
{
    ESource *source = E_SOURCE (source_object);
    EClient *client = NULL;
    EBookClient *book_client;
    GSList *contact_list = user_data, *iter;
    GError *error = NULL;

    e_client_utils_open_new_finish (source, result, &client, &error);

    if (error != NULL) {
        g_warn_if_fail (client == NULL);
        g_warning (
            "%s: Failed to open book client: %s",
            G_STRFUNC, error->message);
        g_error_free (error);
        goto exit;
    }

    g_return_if_fail (E_IS_BOOK_CLIENT (client));

    book_client = E_BOOK_CLIENT (client);

    for (iter = contact_list; iter != NULL; iter = iter->next) {
        EContact *contact;

        contact = E_CONTACT (iter->data);
        eab_merging_book_add_contact (book_client, contact, NULL, NULL);
    }

    g_object_unref (client);

 exit:
    e_client_util_free_object_slist (contact_list);
}

static void
org_gnome_vcard_inline_save_cb (VCardInlinePObject *vcard_object)
{
    ESource *source;
    GSList *contact_list;
    GtkWidget*dialog;

    g_return_if_fail (vcard_object->source_list != NULL);

    dialog = e_source_selector_dialog_new (NULL, vcard_object->source_list);

    e_source_selector_dialog_select_default_source (
        E_SOURCE_SELECTOR_DIALOG (dialog));

    if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_OK) {
        gtk_widget_destroy (dialog);
        return;
    }

    source = e_source_selector_dialog_peek_primary_selection (
        E_SOURCE_SELECTOR_DIALOG (dialog));

    gtk_widget_destroy (dialog);

    g_return_if_fail (source != NULL);

    contact_list = e_client_util_copy_object_slist (NULL, vcard_object->contact_list);

    e_client_utils_open_new (source, E_CLIENT_SOURCE_TYPE_CONTACTS, FALSE, NULL,
        e_client_utils_authenticate_handler, NULL,
        org_gnome_vcard_inline_client_loaded_cb, contact_list);
}

static void
org_gnome_vcard_inline_toggle_cb (VCardInlinePObject *vcard_object,
                                  GtkButton *button)
{
    EABContactDisplay *contact_display;
    EABContactDisplayMode mode;
    const gchar *label;

    contact_display = EAB_CONTACT_DISPLAY (vcard_object->contact_display);
    mode = eab_contact_display_get_mode (contact_display);

    /* Toggle between "full" and "compact" modes. */
    if (mode == EAB_CONTACT_DISPLAY_RENDER_NORMAL) {
        mode = EAB_CONTACT_DISPLAY_RENDER_COMPACT;
        label = _("Show Full vCard");
    } else {
        mode = EAB_CONTACT_DISPLAY_RENDER_NORMAL;
        label = _("Show Compact vCard");
    }

    eab_contact_display_set_mode (contact_display, mode);
    gtk_button_set_label (button, label);
}

static gboolean
org_gnome_vcard_inline_embed (EMFormatHTML *format,
                              GtkHTMLEmbedded *embedded,
                              EMFormatHTMLPObject *object)
{
    VCardInlinePObject *vcard_object;
    GtkWidget *button_box;
    GtkWidget *container;
    GtkWidget *widget;
    EContact *contact;
    guint length;

    vcard_object = (VCardInlinePObject *) object;
    length = g_slist_length (vcard_object->contact_list);

    if (vcard_object->contact_list != NULL)
        contact = E_CONTACT (vcard_object->contact_list->data);
    else
        contact = NULL;

    container = GTK_WIDGET (embedded);

    widget = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (container), widget);
    gtk_widget_show (widget);

    container = widget;

    widget = gtk_hbutton_box_new ();
    gtk_button_box_set_layout (
        GTK_BUTTON_BOX (widget), GTK_BUTTONBOX_START);
    gtk_box_set_spacing (GTK_BOX (widget), 12);
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, TRUE, 0);
    gtk_widget_show (widget);

    button_box = widget;

    widget = eab_contact_display_new ();
    eab_contact_display_set_contact (
        EAB_CONTACT_DISPLAY (widget), contact);
    eab_contact_display_set_mode (
        EAB_CONTACT_DISPLAY (widget),
        EAB_CONTACT_DISPLAY_RENDER_COMPACT);
    gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
    vcard_object->contact_display = g_object_ref (widget);
    gtk_widget_show (widget);

    widget = gtk_label_new (NULL);
    gtk_box_pack_start (GTK_BOX (container), widget, TRUE, TRUE, 0);
    vcard_object->message_label = g_object_ref (widget);

    if (length == 2) {
        const gchar *text;

        text = _("There is one other contact.");
        gtk_label_set_text (GTK_LABEL (widget), text);
        gtk_widget_show (widget);

    } else if (length > 2) {
        gchar *text;

        /* Translators: This will always be two or more. */
        text = g_strdup_printf (ngettext (
            "There is %d other contact.",
            "There are %d other contacts.",
            length - 1), length - 1);
        gtk_label_set_text (GTK_LABEL (widget), text);
        gtk_widget_show (widget);
        g_free (text);

    } else
        gtk_widget_hide (widget);

    container = button_box;

    widget = gtk_button_new_with_label (_("Show Full vCard"));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);
    gtk_widget_show (widget);

    g_signal_connect_swapped (
        widget, "clicked",
        G_CALLBACK (org_gnome_vcard_inline_toggle_cb),
        vcard_object);

    widget = gtk_button_new_with_label (_("Save in Address Book"));
    gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0);

    /* This depends on having a source list. */
    if (vcard_object->source_list != NULL)
        gtk_widget_show (widget);
    else
        gtk_widget_hide (widget);

    g_signal_connect_swapped (
        widget, "clicked",
        G_CALLBACK (org_gnome_vcard_inline_save_cb),
        vcard_object);

    return TRUE;
}

void
org_gnome_vcard_inline_format (gpointer ep, EMFormatHookTarget *target)
{
    VCardInlinePObject *vcard_object;
    gchar *classid;
    gchar *content;

    classid = g_strdup_printf (
        "org-gnome-vcard-inline-display-%d",
        org_gnome_vcard_inline_classid++);

    vcard_object = (VCardInlinePObject *)
        em_format_html_add_pobject (
            EM_FORMAT_HTML (target->format),
            sizeof (VCardInlinePObject),
            classid, target->part,
            org_gnome_vcard_inline_embed);

    g_object_ref (target->part);

    vcard_object->object.free = org_gnome_vcard_inline_pobject_free;
    org_gnome_vcard_inline_decode (vcard_object, target->part);

    e_book_client_get_sources (&vcard_object->source_list, NULL);

    content = g_strdup_printf ("<object classid=%s></object>", classid);
    camel_stream_write_string (target->stream, content, NULL, NULL);
    g_free (content);

    g_free (classid);
}