aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/widgets/eab-contact-display.c
blob: c6a8490b168585583995694c9578878ecacb103e (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Chris Toshok <toshok@ximian.com>
 *
 *  Copyright (C) 2003 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

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

#include "eab-contact-display.h"
#include "eab-popup.h"

#include "eab-gui-util.h"
#include "e-util/e-html-utils.h"
#include "e-util/e-icon-factory.h"

#include <string.h>
#include <glib/gi18n.h>
#include <libgnome/gnome-url.h>
#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-stream.h>

#define HANDLE_MAILTO_INTERNALLY 1

#define PARENT_TYPE (GTK_TYPE_HTML)

struct _EABContactDisplayPrivate {
    EContact *contact;

        GtkWidget *invisible;
    char *selection_uri;
};

static struct {
    gchar *name;
    gchar *pretty_name;
}
common_location [] =
{
    { "WORK",  N_ ("Work")  },
    { "HOME",  N_ ("Home")  },
    { "OTHER", N_ ("Other") }
};

#define HTML_HEADER "<!doctype html public \"-//W3C//DTD HTML 4.0 TRANSITIONAL//EN\">\n<html>\n"  \
                    "<head>\n<meta name=\"generator\" content=\"Evolution Addressbook Component\">\n</head>\n"

#define HEADER_COLOR      "#7f7f7f"
#define IMAGE_COL_WIDTH   "20"
#define CONTACT_LIST_ICON "stock_contact-list"
#define AIM_ICON          "im-aim"
#define GROUPWISE_ICON    "im-nov"
#define ICQ_ICON          "im-icq"
#define JABBER_ICON       "im-jabber"
#define MSN_ICON          "im-msn"
#define YAHOO_ICON        "im-yahoo"
#define GADUGADU_ICON       "im-gadugadu"
#define VIDEOCONF_ICON    "stock_video-conferencing"

#define MAX_COMPACT_IMAGE_DIMENSION 48

static void
eab_uri_popup_link_open(EPopup *ep, EPopupItem *item, void *data)
{
    EABPopupTargetURI *t = (EABPopupTargetURI *)ep->target;
    GError *err = NULL;

    gnome_url_show(t->uri, &err);
    if (err) {
        g_warning("gnome_url_show: %s", err->message);
        g_error_free(err);
    }
}

static void
eab_uri_popup_email_address_copy(EPopup *ep, EPopupItem *item, void *data)
{
    EABContactDisplay *display = data;
    struct _EABContactDisplayPrivate *p = display->priv;
        EABPopupTargetURI *t = (EABPopupTargetURI *)ep->target;
        const char *url = t->uri;
        char *html=NULL;
        int i=0;
        GList *email_list, *l;
        int email_num = atoi (url + strlen ("internal-mailto:"));

    email_list = e_contact_get (p->contact, E_CONTACT_EMAIL);
    for (l = email_list; l; l=l->next) {
        if(i==email_num)
            html = e_text_to_html (l->data, 0);
    i++;
        }

        g_free(p->selection_uri);
    p->selection_uri = g_strdup(html);
    g_free (html);

    gtk_selection_owner_set(p->invisible, GDK_SELECTION_PRIMARY, gtk_get_current_event_time());
    gtk_selection_owner_set(p->invisible, GDK_SELECTION_CLIPBOARD, gtk_get_current_event_time());
}

static void
eab_uri_popup_link_copy(EPopup *ep, EPopupItem *pitem, void *data)
{
    EABContactDisplay *display = data;
    struct _EABContactDisplayPrivate *p = display->priv;

    g_free(p->selection_uri);
    p->selection_uri = g_strdup(pitem->user_data);

    gtk_selection_owner_set(p->invisible, GDK_SELECTION_PRIMARY, gtk_get_current_event_time());
    gtk_selection_owner_set(p->invisible, GDK_SELECTION_CLIPBOARD, gtk_get_current_event_time());
}

static void
eab_uri_popup_address_send(EPopup *ep, EPopupItem *item, void *data)
{
         EABPopupTargetURI *t = (EABPopupTargetURI *)ep->target;
         const char *url = t->uri;
         EABContactDisplay *display = data;
     struct _EABContactDisplayPrivate *p = display->priv;

     int mail_num = atoi (url + strlen ("internal-mailto:"));

         if (mail_num == -1)
         return;

         eab_send_contact (p->contact, mail_num, EAB_DISPOSITION_AS_TO);

}

static void
eab_selection_get(GtkWidget *widget, GtkSelectionData *data, guint info, guint time_stamp, EABContactDisplay *display)
{
    struct _EABContactDisplayPrivate *p = display->priv;

    if (p->selection_uri == NULL)
        return;

    gtk_selection_data_set(data, data->target, 8, (guchar *)p->selection_uri, strlen(p->selection_uri));
}

static void
eab_selection_clear_event(GtkWidget *widget, GdkEventSelection *event, EABContactDisplay *display)
{
#if 0
    struct _EABContactDisplayPrivate *p = display->priv;

    g_free(p->selection_uri);
    p->selection_uri = NULL;
#endif
}

static EPopupItem eab_uri_popups[] = {
    { E_POPUP_ITEM, "05.open", N_("_Open Link in Browser"), eab_uri_popup_link_open, NULL, NULL, EAB_POPUP_URI_NOT_MAILTO },
        { E_POPUP_ITEM, "10.copy", N_("_Copy Link Location"), eab_uri_popup_link_copy, NULL, "edit-copy", EAB_POPUP_URI_NOT_MAILTO },
        { E_POPUP_ITEM, "15.send", N_("_Send New Message To..."), eab_uri_popup_address_send, NULL, "mail-message-new", EAB_POPUP_URI_MAILTO},
    { E_POPUP_ITEM, "20.copy", N_("Copy _Email Address"), eab_uri_popup_email_address_copy, NULL, "edit-copy", EAB_POPUP_URI_MAILTO},
        };


static void
eab_uri_popup_free(EPopup *ep, GSList *list, void *data)
{
    while (list){
        GSList *n = list->next;
        struct _EPopupItem *item = list->data;

        g_free(item->user_data);
        item->user_data = NULL;
        g_slist_free_1(list);

        list = n;
            }
}

static int
eab_uri_popup_event(EABContactDisplay *display, GdkEvent *event, const char *uri)
{
    EABPopup *emp;
    EABPopupTargetURI *t ;
    GtkMenu *menu;
    GSList *menus = NULL;
    int i;

    emp = eab_popup_new("org.gnome.evolution.addressbook.contactdisplay.popup");

    t = eab_popup_target_new_uri(emp, uri);
    t->target.widget = (GtkWidget *)display;

    for (i=0;i<sizeof(eab_uri_popups)/sizeof(eab_uri_popups[0]);i++) {
        eab_uri_popups[i].user_data = g_strdup(t->uri);
        menus = g_slist_prepend(menus, &eab_uri_popups[i]);
    }
    e_popup_add_items((EPopup *)emp, menus, NULL, eab_uri_popup_free, display);

        menu = e_popup_create_menu_once((EPopup *)emp,(EPopupTarget*)t, 0);

        if (event == NULL) {
        gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 0, gtk_get_current_event_time());
    } else {
        gtk_menu_popup(menu, NULL, NULL, NULL, NULL, event->button.button, event->button.time);
    }

    return TRUE;
}

static void
on_url_requested (GtkHTML *html, const char *url, GtkHTMLStream *handle,
          EABContactDisplay *display)
{
    if (!strcmp (url, "internal-contact-photo:")) {
        EContactPhoto *photo;

        photo = e_contact_get (display->priv->contact, E_CONTACT_PHOTO);
        if (!photo)
            photo = e_contact_get (display->priv->contact, E_CONTACT_LOGO);

        gtk_html_stream_write (handle, (char *)photo->data.inlined.data, photo->data.inlined.length);

        gtk_html_end (html, handle, GTK_HTML_STREAM_OK);

        e_contact_photo_free (photo);
    }
    else if (!strncmp (url, "evo-icon:", strlen ("evo-icon:"))) {
        gchar *data;
        gsize data_length;
        gchar *filename;

        filename = e_icon_factory_get_icon_filename (url + strlen ("evo-icon:"), E_ICON_SIZE_MENU);
        if (g_file_get_contents (filename, &data, &data_length, NULL)) {
            gtk_html_stream_write (handle, data, data_length);
            g_free (data);
        }

        gtk_html_stream_close (handle, GTK_HTML_STREAM_OK);

        g_free (filename);
    }
}

static void
on_link_clicked (GtkHTML *html, const char *url, EABContactDisplay *display)
{
    GError *err = NULL;

#ifdef HANDLE_MAILTO_INTERNALLY
    if (!strncmp (url, "internal-mailto:", strlen ("internal-mailto:"))) {
        int mail_num = atoi (url + strlen ("internal-mailto:"));

        if (mail_num == -1)
            return;

        eab_send_contact (display->priv->contact, mail_num, EAB_DISPOSITION_AS_TO);

        return;
    }
#endif

    gnome_url_show (url, &err);

    if (err) {
        g_warning ("gnome_url_show: %s", err->message);
        g_error_free (err);
    }
}

#if 0
static void
render_address (GtkHTMLStream *html_stream, EContact *contact, const char *html_label, EContactField adr_field, EContactField label_field)
{
    EContactAddress *adr;
    const char *label;

    label = e_contact_get_const (contact, label_field);
    if (label) {
        char *html = e_text_to_html (label, E_TEXT_TO_HTML_CONVERT_NL);

        gtk_html_stream_printf (html_stream, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td><td valign=\"top\" width=\"100\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\">%s</td></tr>", html_label, _("(map)"), html);

This shoul      g_free (html);
        return;
    }

    adr = e_contact_get (contact, adr_field);
    if (adr &&
        (adr->po || adr->ext || adr->street || adr->locality || adr->region || adr->code || adr->country)) {

        gtk_html_stream_printf (html_stream, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td><td valign=\"top\" width=\"100\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\">", html_label, _("map"));

        if (adr->po && *adr->po) gtk_html_stream_printf (html_stream, "%s<br>", adr->po);
        if (adr->ext && *adr->ext) gtk_html_stream_printf (html_stream, "%s<br>", adr->ext);
        if (adr->street && *adr->street) gtk_html_stream_printf (html_stream, "%s<br>", adr->street);
        if (adr->locality && *adr->locality) gtk_html_stream_printf (html_stream, "%s<br>", adr->locality);
        if (adr->region && *adr->region) gtk_html_stream_printf (html_stream, "%s<br>", adr->region);
        if (adr->code && *adr->code) gtk_html_stream_printf (html_stream, "%s<br>", adr->code);
        if (adr->country && *adr->country) gtk_html_stream_printf (html_stream, "%s<br>", adr->country);

        gtk_html_stream_printf (html_stream, "</td></tr>");
    }
    if (adr)
        e_contact_address_free (adr);
}
#endif

static void
render_name_value (GtkHTMLStream *html_stream, const char *label, const char *str, const char *icon, unsigned int html_flags)
{
    char *value = e_text_to_html (str, html_flags);

    if (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL) {
        gtk_html_stream_printf (html_stream, "<tr><td align=\"right\" valign=\"top\">%s</td> <td align=\"right\" valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td>", value, label);
        gtk_html_stream_printf (html_stream, "<td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
        if (icon)
            gtk_html_stream_printf (html_stream, "<img width=\"16\" height=\"16\" src=\"evo-icon:%s\"></td></tr>", icon);
        else
            gtk_html_stream_printf (html_stream, "</td></tr>");
    } else {
        gtk_html_stream_printf (html_stream, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
        if (icon)
            gtk_html_stream_printf (html_stream, "<img width=\"16\" height=\"16\" src=\"evo-icon:%s\">", icon);
        gtk_html_stream_printf (html_stream, "</td><td valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td> <td valign=\"top\">%s</td></tr>", label, value);
    }

    g_free (value);
}

static void
render_attribute (GtkHTMLStream *html_stream, EContact *contact, const char *html_label, EContactField field, const char *icon, unsigned int html_flags)
{
    const char *str;

    str = e_contact_get_const (contact, field);

    if (str && *str) {
        render_name_value (html_stream, html_label, str, icon, html_flags);
    }
}

static void
accum_address (GString *gstr, EContact *contact, const char *html_label, EContactField adr_field, EContactField label_field)
{
    EContactAddress *adr;
    const char *label;
    gboolean is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);

    label = e_contact_get_const (contact, label_field);
    if (label) {
        char *html = e_text_to_html (label, E_TEXT_TO_HTML_CONVERT_NL);

#ifdef mapping_works
        if (is_rtl)
            g_string_append_printf (gstr, "<tr><td align=\"right\" valign=\"top\">%s</td><td valign=\"top\" width=\"100\" align=\"right\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td></tr>", html, html_label, _("(map)"));
        else
            g_string_append_printf (gstr, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td><td valign=\"top\" width=\"100\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\">%s</td></tr>", html_label, _("(map)"), html);
#else
        if (is_rtl)
            g_string_append_printf (gstr, "<tr><td align=\"right\" valign=\"top\">%s</td><td valign=\"top\" width=\"100\" align=\"right\"><font color=" HEADER_COLOR ">%s:</font></td><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td></tr>", html, html_label);
        else
            g_string_append_printf (gstr, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td><td valign=\"top\" width=\"100\"><font color=" HEADER_COLOR ">%s:</font></td><td valign=\"top\">%s</td></tr>", html_label, html);
#endif

        g_free (html);
        return;
    }

    adr = e_contact_get (contact, adr_field);
    if (adr &&
        (adr->po || adr->ext || adr->street || adr->locality || adr->region || adr->code || adr->country)) {
        if (is_rtl)
            g_string_append_printf (gstr, "<tr><td align=\"right\" valign=\"top\">");
        else
            g_string_append_printf (gstr, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td><td valign=\"top\" width=\"100\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\">", html_label, _("map"));


        if (adr->po && *adr->po) g_string_append_printf (gstr, "%s<br>", adr->po);
        if (adr->ext && *adr->ext) g_string_append_printf (gstr, "%s<br>", adr->ext);
        if (adr->street && *adr->street) g_string_append_printf (gstr, "%s<br>", adr->street);
        if (adr->locality && *adr->locality) g_string_append_printf (gstr, "%s<br>", adr->locality);
        if (adr->region && *adr->region) g_string_append_printf (gstr, "%s<br>", adr->region);
        if (adr->code && *adr->code) g_string_append_printf (gstr, "%s<br>", adr->code);
        if (adr->country && *adr->country) g_string_append_printf (gstr, "%s<br>", adr->country);

        if (is_rtl)
            g_string_append_printf (gstr, "</td><td valign=\"top\" width=\"100\" align=\"right\"><font color=" HEADER_COLOR ">%s:</font><br><a href=\"http://www.mapquest.com/\">%s</a></td><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td></tr>", html_label, _("map"));
        else
            g_string_append_printf (gstr, "</td></tr>");
    }
    if (adr)
        e_contact_address_free (adr);
}

static void
accum_name_value (GString *gstr, const char *label, const char *str, const char *icon, unsigned int html_flags)
{
    char *value = e_text_to_html (str, html_flags);

    if (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL) {
        g_string_append_printf (gstr, "<tr><td valign=\"top\" align=\"right\">%s</td> <td align=\"right\" valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td>", value, label);
        g_string_append_printf (gstr, "<td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
        if (icon)
            g_string_append_printf (gstr, "<img width=\"16\" height=\"16\" src=\"evo-icon:%s\"></td></tr>", icon);
        else 
            g_string_append_printf (gstr, "</td></tr>");
    } else {
        g_string_append_printf (gstr, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
        if (icon)
            g_string_append_printf (gstr, "<img width=\"16\" height=\"16\" src=\"evo-icon:%s\">", icon);
        g_string_append_printf (gstr, "</td><td valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td> <td valign=\"top\">%s</td></tr>", label, value);
    }

    g_free (value);
}


static void
accum_attribute (GString *gstr, EContact *contact, const char *html_label, EContactField field, const char *icon, unsigned int html_flags)
{
    const char *str;

    str = e_contact_get_const (contact, field);

    if (str && *str) {
        accum_name_value (gstr, html_label, str, icon, html_flags);
    }
}

static void
accum_time_attribute (GString *gstr, EContact *contact, const char *html_label, EContactField field, const char *icon, unsigned int html_flags)
{
    EContactDate *date;
    GDate *gdate = NULL;
    char sdate[100];

    date = e_contact_get (contact, field);
    if (date) {
        gdate = g_date_new_dmy ( date->day,
                     date->month,
                     date->year );
        g_date_strftime (sdate, 100, "%x", gdate);
        g_date_free (gdate);
        accum_name_value (gstr, html_label, sdate, icon, html_flags);
        e_contact_date_free (date);
    }
}

static void
accum_multival_attribute (GString *gstr, EContact *contact, const char *html_label, EContactField field, const char *icon, unsigned int html_flags)
{
    GList *val_list, *l;

    val_list = e_contact_get (contact, field);
    for (l = val_list; l; l = l->next) {
        const char *str = (const char *) l->data;
        accum_name_value (gstr, html_label, str, icon, html_flags);
    }
    g_list_foreach (val_list, (GFunc) g_free, NULL);
    g_list_free (val_list);
}

static void
render_contact_list (GtkHTMLStream *html_stream, EContact *contact)
{
    GList *email_list;
    GList *l;

    gtk_html_stream_printf (html_stream, "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
    gtk_html_stream_printf (html_stream, "<td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
    gtk_html_stream_printf (html_stream, "<img width=\"16\" height=\"16\" src=\"evo-icon:" CONTACT_LIST_ICON "\">");
    gtk_html_stream_printf (html_stream, "</td><td valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td> <td valign=\"top\">", _("List Members"));

    email_list = e_contact_get (contact, E_CONTACT_EMAIL);
    for (l = email_list; l; l = l->next) {
        gchar *value;

        value = eab_parse_qp_email_to_html (l->data);

        if (!value)
            value = e_text_to_html (l->data, E_TEXT_TO_HTML_CONVERT_ADDRESSES);

        gtk_html_stream_printf (html_stream, "%s<br>", value);

        g_free (value);
    }

    gtk_html_stream_printf (html_stream, "</td></tr></table>");
}

static void
start_block (GtkHTMLStream *html_stream, const char *label)
{
    gtk_html_stream_printf (html_stream, "<tr><td height=\"20\" colspan=\"3\"><font color=" HEADER_COLOR "><b>%s</b></font></td></tr>", label);
}

static void
end_block (GtkHTMLStream *html_stream)
{
    gtk_html_stream_printf (html_stream, "<tr><td height=\"20\">&nbsp;</td></tr>");
}

static const char *
get_email_location (EVCardAttribute *attr)
{
    gint i;

    for (i = 0; i < G_N_ELEMENTS (common_location); i++) {
        if (e_vcard_attribute_has_type (attr, common_location [i].name))
            return _(common_location [i].pretty_name);
    }

    return _("Other");
}

static void
render_contact (GtkHTMLStream *html_stream, EContact *contact)
{
    GString *accum;
    GList *email_list, *l, *email_attr_list, *al;
    gboolean is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
#ifdef HANDLE_MAILTO_INTERNALLY
    int email_num = 0;
#endif
    char *nl, *nick=NULL;

    gtk_html_stream_printf (html_stream, "<table border=\"0\">");

    accum = g_string_new ("");
    nl = "";

    start_block (html_stream, "");

    email_list = e_contact_get (contact, E_CONTACT_EMAIL);
    email_attr_list = e_contact_get_attributes (contact, E_CONTACT_EMAIL);

    for (l = email_list, al=email_attr_list; l && al; l = l->next, al = al->next) {
        char *html = NULL, *name = NULL, *mail = NULL;
        char *attr_str = (char *)get_email_location ((EVCardAttribute *) al->data);

#ifdef HANDLE_MAILTO_INTERNALLY
        if (!eab_parse_qp_email (l->data, &name, &mail))
            mail = e_text_to_html (l->data, 0);

        g_string_append_printf (accum, "%s%s%s<a href=\"internal-mailto:%d\">%s</a>%s <font color=" HEADER_COLOR ">(%s)</font>", 
                        nl,
                        name ? name : "",
                        name ? " &lt;" : "",
                        email_num,
                        mail,
                        name ? "&gt;" : "",
                        attr_str ? attr_str : "");
        email_num ++;
#else
        html = eab_parse_qp_email_to_html (l->data);

        if (!html)
            html = e_text_to_html (l->data, E_TEXT_TO_HTML_CONVERT_ADDRESSES);

        g_string_append_printf (accum, "%s%s <font color=" HEADER_COLOR ">(%s)</font>", nl, html, attr_str ? attr_str : "");
#endif
        nl = "<br>";

        g_free (html);
        g_free (name);
        g_free (mail);
    }
    g_list_foreach (email_list, (GFunc)g_free, NULL);
    g_list_free (email_list);

    if (accum->len) {

#ifdef HANDLE_MAILTO_INTERNALLY
        if (is_rtl) {
            gtk_html_stream_printf (html_stream,
                    "<tr><td valign=\"top\" align=\"right\">%s</td> <td valign=\"top\" align=\"right\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\"></td></tr>",
                    accum->str, _("Email"));
        } else {
            gtk_html_stream_printf (html_stream, "<tr><td valign=\"top\" width=\"" IMAGE_COL_WIDTH "\">");
            gtk_html_stream_printf (html_stream,
                    "</td><td valign=\"top\" width=\"100\" nowrap><font color=" HEADER_COLOR ">%s:</font></td> <td valign=\"top\">%s</td></tr>",
                    _("Email"), accum->str);
        }
#else
        render_name_value (html_stream, _("Email"), accum->str, NULL,
                   E_TEXT_TO_HTML_CONVERT_ADDRESSES | E_TEXT_TO_HTML_CONVERT_NL);
#endif
    }

    g_string_assign (accum, "");
    nick = e_contact_get (contact, E_CONTACT_NICKNAME);
    if (nick && *nick) {
        accum_name_value (accum, _("Nickname"), nick, NULL, 0);
        if (accum->len > 0)
            gtk_html_stream_printf (html_stream, "%s", accum->str);
    }

    g_string_assign (accum, "");
    accum_multival_attribute (accum, contact, _("AIM"), E_CONTACT_IM_AIM, AIM_ICON, 0);
    accum_multival_attribute (accum, contact, _("GroupWise"), E_CONTACT_IM_GROUPWISE, GROUPWISE_ICON, 0);
    accum_multival_attribute (accum, contact, _("ICQ"), E_CONTACT_IM_ICQ, ICQ_ICON, 0);
    accum_multival_attribute (accum, contact, _("Jabber"), E_CONTACT_IM_JABBER, JABBER_ICON, 0);
    accum_multival_attribute (accum, contact, _("MSN"), E_CONTACT_IM_MSN, MSN_ICON, 0);
    accum_multival_attribute (accum, contact, _("Yahoo"), E_CONTACT_IM_YAHOO, YAHOO_ICON, 0);
    accum_multival_attribute (accum, contact, _("Gadu-Gadu"), E_CONTACT_IM_GADUGADU, GADUGADU_ICON, 0);

    if (accum->len > 0)
        gtk_html_stream_printf (html_stream, "%s", accum->str);

    end_block (html_stream);

    g_string_assign (accum, "");

    accum_attribute (accum, contact, _("Company"), E_CONTACT_ORG, NULL, 0);
    accum_attribute (accum, contact, _("Department"), E_CONTACT_ORG_UNIT, NULL, 0);
    accum_attribute (accum, contact, _("Profession"), E_CONTACT_ROLE, NULL, 0);
    accum_attribute (accum, contact, _("Position"), E_CONTACT_TITLE, NULL, 0);
    accum_attribute (accum, contact, _("Manager"), E_CONTACT_MANAGER, NULL, 0);
    accum_attribute (accum, contact, _("Assistant"), E_CONTACT_ASSISTANT, NULL, 0);
    accum_attribute (accum, contact, _("Video Chat"), E_CONTACT_VIDEO_URL, VIDEOCONF_ICON, E_TEXT_TO_HTML_CONVERT_URLS);
    accum_attribute (accum, contact, _("Calendar"), E_CONTACT_CALENDAR_URI, NULL, E_TEXT_TO_HTML_CONVERT_URLS);
    accum_attribute (accum, contact, _("Free/Busy"), E_CONTACT_FREEBUSY_URL, NULL, E_TEXT_TO_HTML_CONVERT_URLS);
    accum_attribute (accum, contact, _("Phone"), E_CONTACT_PHONE_BUSINESS, NULL, 0);
    accum_attribute (accum, contact, _("Fax"), E_CONTACT_PHONE_BUSINESS_FAX, NULL, 0);
    accum_address   (accum, contact, _("Address"), E_CONTACT_ADDRESS_WORK, E_CONTACT_ADDRESS_LABEL_WORK);

    if (accum->len > 0) {
        start_block (html_stream, _("Work"));
        gtk_html_stream_printf (html_stream, "%s", accum->str);
        end_block (html_stream);
    }

    g_string_assign (accum, "");

    accum_attribute (accum, contact, _("Home Page"), E_CONTACT_HOMEPAGE_URL, NULL, E_TEXT_TO_HTML_CONVERT_URLS);
    accum_attribute (accum, contact, _("Web Log"), E_CONTACT_BLOG_URL, NULL, E_TEXT_TO_HTML_CONVERT_URLS);

    accum_attribute (accum, contact, _("Phone"), E_CONTACT_PHONE_HOME, NULL, 0);
    accum_attribute (accum, contact, _("Mobile Phone"), E_CONTACT_PHONE_MOBILE, NULL, 0);
    accum_address   (accum, contact, _("Address"), E_CONTACT_ADDRESS_HOME, E_CONTACT_ADDRESS_LABEL_HOME);
    accum_time_attribute (accum, contact, _("Birthday"), E_CONTACT_BIRTH_DATE, NULL, 0);
    accum_time_attribute (accum, contact, _("Anniversary"), E_CONTACT_ANNIVERSARY, NULL, 0);
    accum_attribute (accum, contact, _("Spouse"), E_CONTACT_SPOUSE, NULL, 0);
    if (accum->len > 0) {
        start_block (html_stream, _("Personal"));
        gtk_html_stream_printf (html_stream, "%s", accum->str);
        end_block (html_stream);
    }

    start_block (html_stream, "");

    render_attribute (html_stream, contact, _("Note"), E_CONTACT_NOTE, NULL,
              E_TEXT_TO_HTML_CONVERT_ADDRESSES | E_TEXT_TO_HTML_CONVERT_URLS | E_TEXT_TO_HTML_CONVERT_NL);
    end_block (html_stream);

    gtk_html_stream_printf (html_stream, "</table>");
}

static void
eab_contact_display_render_normal (EABContactDisplay *display, EContact *contact)
{
    GtkHTMLStream *html_stream;
    gboolean is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);

    if (display->priv->contact)
        g_object_unref (display->priv->contact);
    display->priv->contact = contact;
    if (display->priv->contact)
        g_object_ref (display->priv->contact);

    html_stream = gtk_html_begin (GTK_HTML (display));
    gtk_html_stream_write (html_stream, HTML_HEADER, sizeof (HTML_HEADER) - 1);
    gtk_html_stream_printf (html_stream, "<body><table width=\"100%%\"><tr><td %s>\n", is_rtl ? " align=\"right\" " : "");

    if (contact) {
        const char *str;
        char *html;
        EContactPhoto *photo;

        gtk_html_stream_printf (html_stream, "<table cellspacing=\"20\" border=\"0\"><td %s valign=\"top\">", is_rtl ? " align=\"right\" " : "");
        photo = e_contact_get (contact, E_CONTACT_PHOTO);
        if (!photo)
            photo = e_contact_get (contact, E_CONTACT_LOGO);
        /* Only handle inlined photos for now */
        if (photo && photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
            gtk_html_stream_printf (html_stream, "<img border=\"1\" src=\"internal-contact-photo:\">");
            e_contact_photo_free (photo);
        }

        gtk_html_stream_printf (html_stream, "</td><td %s valign=\"top\">\n", is_rtl ? " align=\"right\" " : "");

        str = e_contact_get_const (contact, E_CONTACT_FILE_AS);
        if (!str)
            str = e_contact_get_const (contact, E_CONTACT_FULL_NAME);

        if (str) {
            html = e_text_to_html (str, 0);
#ifdef HANDLE_MAILTO_INTERNALLY
            if (e_contact_get (contact, E_CONTACT_IS_LIST))
                gtk_html_stream_printf (html_stream, "<h2><a href=\"internal-mailto:0\">%s</a></h2>", html);
            else
#endif
            gtk_html_stream_printf (html_stream, "<h2>%s</h2>", html);
            g_free (html);
        }


        if (e_contact_get (contact, E_CONTACT_IS_LIST))
            render_contact_list (html_stream, contact);
        else
            render_contact (html_stream, contact);

        gtk_html_stream_printf (html_stream, "</td></tr></table>\n");
    }

    gtk_html_stream_printf (html_stream, "</td></tr></table></body></html>\n");
    gtk_html_end (GTK_HTML (display), html_stream, GTK_HTML_STREAM_OK);
}

static void
eab_contact_display_render_compact (EABContactDisplay *display, EContact *contact)
{
    GtkHTMLStream *html_stream;

    if (display->priv->contact)
        g_object_unref (display->priv->contact);
    display->priv->contact = contact;
    if (display->priv->contact)
        g_object_ref (display->priv->contact);

    html_stream = gtk_html_begin (GTK_HTML (display));
    gtk_html_stream_write (html_stream, HTML_HEADER, sizeof (HTML_HEADER) - 1);
    gtk_html_stream_write (html_stream, "<body>\n", 7);

    if (contact) {
        const char *str;
        char *html;
        EContactPhoto *photo;
        guint bg_frame = 0x000000, bg_body = 0xEEEEEE;
        GtkStyle *style;

        style = gtk_widget_get_style (GTK_WIDGET (display));
        if (style) {
            gushort r, g, b;

            r = style->black.red >> 8;
            g = style->black.green >> 8;
            b = style->black.blue >> 8;
            bg_frame = ((r << 16) | (g << 8) | b) & 0xffffff;

            #define DARKER(a) (((a) >= 0x22) ? ((a) - 0x22) : 0)
            r = DARKER (style->bg[GTK_STATE_NORMAL].red >> 8);
            g = DARKER (style->bg[GTK_STATE_NORMAL].green >> 8);
            b = DARKER (style->bg[GTK_STATE_NORMAL].blue >> 8);
            bg_body = ((r << 16) | (g << 8) | b) & 0xffffff;
            #undef DARKER
        }

        gtk_html_stream_printf (html_stream,
                    "<table width=\"100%%\" cellpadding=1 cellspacing=0 bgcolor=\"#%06X\">"
                    "<tr><td valign=\"top\">"
                    "<table width=\"100%%\" cellpadding=0 cellspacing=0 bgcolor=\"#%06X\">"
                    "<tr><td valign=\"top\">"
                    "<table>"
                    "<tr><td valign=\"top\">", bg_frame, bg_body);

        photo = e_contact_get (contact, E_CONTACT_PHOTO);
        if (!photo)
            photo = e_contact_get (contact, E_CONTACT_LOGO);
        if (photo) {
            int calced_width = MAX_COMPACT_IMAGE_DIMENSION, calced_height = MAX_COMPACT_IMAGE_DIMENSION;
            GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
            GdkPixbuf *pixbuf;

            /* figure out if we need to downscale the
               image here.  we don't scale the pixbuf
               itself, just insert width/height tags in
               the html */
            gdk_pixbuf_loader_write (loader, photo->data.inlined.data, photo->data.inlined.length, NULL);
            gdk_pixbuf_loader_close (loader, NULL);
            pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
            if (pixbuf)
                g_object_ref (pixbuf);
            g_object_unref (loader);
            if (pixbuf) {
                int max_dimension;

                calced_width = gdk_pixbuf_get_width (pixbuf);
                calced_height = gdk_pixbuf_get_height (pixbuf);

                max_dimension = calced_width;
                if (max_dimension < calced_height)
                    max_dimension = calced_height;

                if (max_dimension > MAX_COMPACT_IMAGE_DIMENSION) {
                    calced_width *= ((float)MAX_COMPACT_IMAGE_DIMENSION / max_dimension);
                    calced_height *= ((float)MAX_COMPACT_IMAGE_DIMENSION / max_dimension);
                }
            }

            g_object_unref (pixbuf);
            gtk_html_stream_printf (html_stream, "<img width=\"%d\" height=\"%d\" src=\"internal-contact-photo:\">",
                        calced_width, calced_height);
            e_contact_photo_free (photo);
        }

        gtk_html_stream_printf (html_stream, "</td><td valign=\"top\">\n");

        str = e_contact_get_const (contact, E_CONTACT_FILE_AS);
        if (str) {
            html = e_text_to_html (str, 0);
            gtk_html_stream_printf (html_stream, "<b>%s</b>", html);
            g_free (html);
        }
        else {
            str = e_contact_get_const (contact, E_CONTACT_FULL_NAME);
            if (str) {
                html = e_text_to_html (str, 0);
                gtk_html_stream_printf (html_stream, "<b>%s</b>", html);
                g_free (html);
            }
        }

        gtk_html_stream_write (html_stream, "<hr>", 4);

        if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
            GList *email_list;
            GList *l;

            gtk_html_stream_printf (html_stream, "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign=\"top\">");
            gtk_html_stream_printf (html_stream, "<b>%s:</b>&nbsp;<td>", _("List Members"));

            email_list = e_contact_get (contact, E_CONTACT_EMAIL);

            for (l = email_list; l; l = l->next) {
                if (l->data) {
                    html = e_text_to_html (l->data, 0);
                    gtk_html_stream_printf (html_stream, "%s, ", html);
                    g_free (html);
                }
            }
            gtk_html_stream_printf (html_stream, "</td></tr></table>");
        }
        else {
            gboolean comma = FALSE;
            str = e_contact_get_const (contact, E_CONTACT_TITLE);
            if (str) {
                html = e_text_to_html (str, 0);
                gtk_html_stream_printf (html_stream, "<b>%s:</b> %s<br>", _("Job Title"), str);
                g_free (html);
            }

            #define print_email() {                             \
                html = eab_parse_qp_email_to_html (str);                \
                                                    \
                if (!html)                              \
                    html = e_text_to_html (str, 0);                 \
                                                    \
                gtk_html_stream_printf (html_stream, "%s%s", comma ? ", " : "", html);  \
                g_free (html);                              \
                comma = TRUE;                               \
            }

            gtk_html_stream_printf (html_stream, "<b>%s:</b> ", _("Email"));
            str = e_contact_get_const (contact, E_CONTACT_EMAIL_1);
            if (str)
                print_email ();

            str = e_contact_get_const (contact, E_CONTACT_EMAIL_2);
            if (str)
                print_email ();

            str = e_contact_get_const (contact, E_CONTACT_EMAIL_3);
            if (str)
                print_email ();

            gtk_html_stream_write (html_stream, "<br>", 4);

            #undef print_email

            str = e_contact_get_const (contact, E_CONTACT_HOMEPAGE_URL);
            if (str) {
                html = e_text_to_html (str, E_TEXT_TO_HTML_CONVERT_URLS);
                gtk_html_stream_printf (html_stream, "<b>%s:</b> %s<br>",
                            _("Home page"), html);
                g_free (html);
            }

            str = e_contact_get_const (contact, E_CONTACT_BLOG_URL);
            if (str) {
                html = e_text_to_html (str, E_TEXT_TO_HTML_CONVERT_URLS);
                gtk_html_stream_printf (html_stream, "<b>%s:</b> %s<br>",
                            _("Blog"), html);
            }
        }

        gtk_html_stream_printf (html_stream, "</td></tr></table></td></tr></table></td></tr></table>\n");
    }

    gtk_html_stream_write (html_stream, "</body></html>\n", 15);
    gtk_html_end (GTK_HTML (display), html_stream, GTK_HTML_STREAM_OK);
}

void
eab_contact_display_render (EABContactDisplay *display, EContact *contact,
                EABContactDisplayRenderMode mode)
{
    switch (mode) {
    case EAB_CONTACT_DISPLAY_RENDER_NORMAL:
        eab_contact_display_render_normal (display, contact);
        break;
    case EAB_CONTACT_DISPLAY_RENDER_COMPACT:
        eab_contact_display_render_compact (display, contact);
        break;
    }
}

static int
eab_html_press_event (GtkWidget *widget, GdkEvent *event,EABContactDisplay *display)
{
    char *uri;
    gboolean res = FALSE;


    if (event->button.button!= 3 )
        return FALSE;

        uri = gtk_html_get_url_at (GTK_HTML (widget), event->button.x, event->button.y);
    if (uri){
        eab_uri_popup_event(display,event,uri);
                }

         g_free(uri);

    return res;
}

GtkWidget*
eab_contact_display_new (void)
{
    EABContactDisplay *display;

    struct _EABContactDisplayPrivate *p;

    display = g_object_new (EAB_TYPE_CONTACT_DISPLAY, NULL);
    p=display->priv = g_new0 (EABContactDisplayPrivate, 1);

    gtk_html_set_default_content_type (GTK_HTML (display), "text/html; charset=utf-8");

    gtk_html_set_editable (GTK_HTML (display), FALSE);

    g_signal_connect (display, "url_requested",
              G_CALLBACK (on_url_requested),
              display);
    g_signal_connect (display, "link_clicked",
              G_CALLBACK (on_link_clicked),
              display);
        g_signal_connect(display, "button_press_event",
                          G_CALLBACK(eab_html_press_event),
                           display);
        p->invisible = gtk_invisible_new();
    g_signal_connect(p->invisible, "selection_get", G_CALLBACK(eab_selection_get), display);
    g_signal_connect(p->invisible, "selection_clear_event", G_CALLBACK(eab_selection_clear_event), display);
    gtk_selection_add_target(p->invisible, GDK_SELECTION_PRIMARY, GDK_SELECTION_TYPE_STRING, 0);
    gtk_selection_add_target(p->invisible, GDK_SELECTION_CLIPBOARD, GDK_SELECTION_TYPE_STRING, 1);

#if 0
    g_signal_connect (display, "object_requested",
              G_CALLBACK (on_object_requested),
              mail_display);
    g_signal_connect (display, "button_press_event",
              G_CALLBACK (html_button_press_event), mail_display);
    g_signal_connect (display, "motion_notify_event",
              G_CALLBACK (html_motion_notify_event), mail_display);
    g_signal_connect (display, "enter_notify_event",
              G_CALLBACK (html_enter_notify_event), mail_display);
    g_signal_connect (display, "iframe_created",
              G_CALLBACK (html_iframe_created), mail_display);
    g_signal_connect (display, "on_url",
              G_CALLBACK (html_on_url), mail_display);
#endif

    return GTK_WIDGET (display);
}

static void
eab_contact_display_init (GObject *object)
{
    gtk_html_construct ((GtkHTML *)object);
}

static void
eab_contact_display_class_init (GtkObjectClass *object_class)
{
    /*  object_class->destroy = mail_display_destroy;*/
}

GType
eab_contact_display_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info =  {
            sizeof (EABContactDisplayClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) eab_contact_display_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (EABContactDisplay),
            0,             /* n_preallocs */
            (GInstanceInitFunc) eab_contact_display_init,
        };

        type = g_type_register_static (PARENT_TYPE, "EABContactDisplay", &info, 0);
    }

    return type;
}