aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-msg-composer-attachment-bar.c
blob: 05a1b549f3d6b9d02779ae40425edaf1789d09f3 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 *  Authors: Ettore Perazzoli <ettore@ximian.com>
 *           Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 1999-2002 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., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


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

#include <string.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <gconf/gconf.h>
#include <gconf/gconf-client.h>
#include <libgnome/gnome-util.h>
#include <libgnomeui/gnome-app.h>
#include <libgnomeui/gnome-app-helper.h>
#include <libgnomeui/gnome-popup-menu.h>
#include <libgnomevfs/gnome-vfs-mime-handlers.h>

#include "e-msg-composer.h"
#include "e-msg-composer-select-file.h"
#include "e-msg-composer-attachment.h"
#include "e-msg-composer-attachment-bar.h"

#include <gal/util/e-iconv.h>

#include <camel/camel-data-wrapper.h>
#include <camel/camel-stream-fs.h>
#include <camel/camel-stream-null.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-mime-filter-bestenc.h>
#include <camel/camel-mime-part.h>

#include "e-util/e-gui-utils.h"

#define ICON_WIDTH 64
#define ICON_SEPARATORS " /-_"
#define ICON_SPACING 2
#define ICON_ROW_SPACING ICON_SPACING
#define ICON_COL_SPACING ICON_SPACING
#define ICON_BORDER 2
#define ICON_TEXT_SPACING 2


static GnomeIconListClass *parent_class = NULL;

struct _EMsgComposerAttachmentBarPrivate {
    GList *attachments;
    guint num_attachments;

    GtkWidget *context_menu;
    GtkWidget *icon_context_menu;
};


enum {
    CHANGED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };


static void update (EMsgComposerAttachmentBar *bar);


static char *
size_to_string (gulong size)
{
    char *size_string;
    
    /* FIXME: The following should probably go into a separate module, as
           we might have to do the same thing in other places as well.  Also,
       I am not sure this will be OK for all the languages.  */
    
    if (size < 1e3L) {
        size_string = NULL;
    } else {
        gdouble displayed_size;
        
        if (size < 1e6L) {
            displayed_size = (gdouble) size / 1.0e3;
            size_string = g_strdup_printf (_("%.0fK"), displayed_size);
        } else if (size < 1e9L) {
            displayed_size = (gdouble) size / 1.0e6;
            size_string = g_strdup_printf (_("%.0fM"), displayed_size);
        } else {
            displayed_size = (gdouble) size / 1.0e9;
            size_string = g_strdup_printf (_("%.0fG"), displayed_size);
        }
    }
    
    return size_string;
}

/* Attachment handling functions.  */

static void
free_attachment_list (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GList *p;
    
    priv = bar->priv;
    
    for (p = priv->attachments; p != NULL; p = p->next)
        g_object_unref (p->data);
    
    priv->attachments = NULL;
}

static void
attachment_changed_cb (EMsgComposerAttachment *attachment,
               gpointer data)
{
    update (E_MSG_COMPOSER_ATTACHMENT_BAR (data));
}

static void
add_common (EMsgComposerAttachmentBar *bar,
        EMsgComposerAttachment *attachment)
{
    g_return_if_fail (attachment != NULL);
    
    g_signal_connect (attachment, "changed",
              G_CALLBACK (attachment_changed_cb),
              bar);
    
    bar->priv->attachments = g_list_append (bar->priv->attachments,
                        attachment);
    bar->priv->num_attachments++;
    
    update (bar);
    
    g_signal_emit (bar, signals[CHANGED], 0);
}

static void
add_from_mime_part (EMsgComposerAttachmentBar *bar,
            CamelMimePart *part)
{
    add_common (bar, e_msg_composer_attachment_new_from_mime_part (part));
}

static void
add_from_file (EMsgComposerAttachmentBar *bar,
           const char *file_name,
           const char *disposition)
{
    EMsgComposerAttachment *attachment;
    EMsgComposer *composer;
    CamelException ex;
    GtkWidget *dialog;
    
    camel_exception_init (&ex);
    attachment = e_msg_composer_attachment_new (file_name, disposition, &ex);
    if (attachment) {
        add_common (bar, attachment);
    } else {
        composer = E_MSG_COMPOSER (gtk_widget_get_toplevel (GTK_WIDGET (bar)));
        dialog = gtk_message_dialog_new(GTK_WINDOW(composer),
                        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
                        GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
                        "%s", camel_exception_get_description (&ex));
        gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy(dialog);
        camel_exception_clear (&ex);
    }
}

static void
remove_attachment (EMsgComposerAttachmentBar *bar,
           EMsgComposerAttachment *attachment)
{
    bar->priv->attachments = g_list_remove (bar->priv->attachments,
                        attachment);
    bar->priv->num_attachments--;
    
    g_object_unref(attachment);
    
    g_signal_emit (bar, signals[CHANGED], 0);
}


/* Icon list contents handling.  */

static void
update (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GnomeIconList *icon_list;
    GList *p;
    
    priv = bar->priv;
    icon_list = GNOME_ICON_LIST (bar);
    
    gnome_icon_list_freeze (icon_list);
    
    gnome_icon_list_clear (icon_list);
    
    /* FIXME could be faster, but we don't care.  */
    for (p = priv->attachments; p != NULL; p = p->next) {
        EMsgComposerAttachment *attachment;
        CamelContentType *content_type;
        char *size_string, *label;
        GdkPixbuf *pixbuf;
        gboolean image;
        const char *desc;
        
        attachment = p->data;
        content_type = camel_mime_part_get_content_type (attachment->body);
        /* Get the image out of the attachment 
           and create a thumbnail for it */
        image = header_content_type_is (content_type, "image", "*");
        
        if (image && attachment->pixbuf_cache == NULL) {
            CamelDataWrapper *wrapper;
            CamelStream *mstream;
            GdkPixbufLoader *loader;
            gboolean error = TRUE;
            char tmp[4096];
            int t;
            
            wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body));
            mstream = camel_stream_mem_new ();
            
            camel_data_wrapper_write_to_stream (wrapper, mstream);
            
            camel_stream_reset (mstream);
            
            /* Stream image into pixbuf loader */
            loader = gdk_pixbuf_loader_new ();
            do {
                t = camel_stream_read (mstream, tmp, 4096);
                if (t > 0) {
                    error = !gdk_pixbuf_loader_write (loader, tmp, t, NULL);
                    if (error) {
                        break;
                    }
                } else {
                    if (camel_stream_eos (mstream))
                        break;
                    error = TRUE;
                    break;
                }
                
            } while (!camel_stream_eos (mstream));
            
            if (!error) {
                int ratio, width, height;
                
                /* Shrink pixbuf */
                pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
                width = gdk_pixbuf_get_width (pixbuf);
                height = gdk_pixbuf_get_height (pixbuf);
                if (width >= height) {
                    if (width > 48) {
                        ratio = width / 48;
                        width = 48;
                        height = height / ratio;
                    }
                } else {
                    if (height > 48) {
                        ratio = height / 48;
                        height = 48;
                        width = width / ratio;
                    }
                }
                
                attachment->pixbuf_cache = gdk_pixbuf_scale_simple 
                    (pixbuf,
                     width,
                     height,
                     GDK_INTERP_BILINEAR);
            } else {
                g_warning ("GdkPixbufLoader Error");
                image = FALSE;
            }
            
            /* Destroy everything */
            gdk_pixbuf_loader_close (loader, NULL);
            g_object_unref (loader);
            camel_stream_close (mstream);
        }
        
        desc = camel_mime_part_get_description (attachment->body);
        if (!desc || *desc == '\0')
            desc = camel_mime_part_get_filename (attachment->body);
        
        if (!desc)
            desc = _("attachment");
        
        if (attachment->size
            && (size_string = size_to_string (attachment->size))) {
            label = g_strdup_printf ("%s (%s)", desc, size_string);
            g_free (size_string);
        } else
            label = g_strdup (desc);
        
        if (image) {
            gnome_icon_list_append_pixbuf (icon_list, attachment->pixbuf_cache, NULL, label);
        } else {
            char *mime_type;
            
            mime_type = header_content_type_simple (content_type);
            pixbuf = e_icon_for_mime_type (mime_type, 48);
            g_free (mime_type);
            gnome_icon_list_append_pixbuf (icon_list, pixbuf, NULL, label);
            if (pixbuf)
                g_object_unref (pixbuf);
        }
        
        g_free (label);
    }
    
    gnome_icon_list_thaw (icon_list);
}

static void
remove_selected (EMsgComposerAttachmentBar *bar)
{
    GnomeIconList *icon_list;
    EMsgComposerAttachment *attachment;
    GList *attachment_list, *p;
    int num;
    
    icon_list = GNOME_ICON_LIST (bar);
    
    /* Weee!  I am especially proud of this piece of cheesy code: it is
           truly awful.  But unless one attaches a huge number of files, it
           will not be as greedy as intended.  FIXME of course.  */
    
    attachment_list = NULL;
    p = gnome_icon_list_get_selection (icon_list);
    for ( ; p != NULL; p = p->next) {
        num = GPOINTER_TO_INT (p->data);
        attachment = E_MSG_COMPOSER_ATTACHMENT (g_list_nth (bar->priv->attachments, num)->data);
        attachment_list = g_list_prepend (attachment_list, attachment);
    }
    
    for (p = attachment_list; p != NULL; p = p->next)
        remove_attachment (bar, E_MSG_COMPOSER_ATTACHMENT (p->data));
    
    g_list_free (attachment_list);
    
    update (bar);
}

static void
edit_selected (EMsgComposerAttachmentBar *bar)
{
    GnomeIconList *icon_list;
    EMsgComposerAttachment *attachment;
    GList *selection;
    int num;
    
    icon_list = GNOME_ICON_LIST (bar);
    
    selection = gnome_icon_list_get_selection (icon_list);
    num = GPOINTER_TO_INT (selection->data);
    attachment = g_list_nth (bar->priv->attachments, num)->data;
    
    e_msg_composer_attachment_edit (attachment, GTK_WIDGET (bar));
}


/* "Attach" dialog.  */

static void
add_from_user (EMsgComposerAttachmentBar *bar)
{
    EMsgComposer *composer;
    GPtrArray *file_list;
    gboolean is_inline = FALSE;
    int i;
    
    composer = E_MSG_COMPOSER (gtk_widget_get_toplevel (GTK_WIDGET (bar)));
    
    file_list = e_msg_composer_select_file_attachments (composer, &is_inline);
    if (!file_list)
        return;
    
    for (i = 0; i < file_list->len; i++) {
        add_from_file (bar, file_list->pdata[i], is_inline ? "inline" : "attachment");
        g_free (file_list->pdata[i]);
    }
    
    g_ptr_array_free (file_list, TRUE);
}


/* Callbacks.  */

static void
add_cb (GtkWidget *widget, gpointer data, GtkWidget *for_widget)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));
    
    add_from_user (E_MSG_COMPOSER_ATTACHMENT_BAR (data));
}

static void
properties_cb (GtkWidget *widget, gpointer data, GtkWidget *for_widget)
{
    EMsgComposerAttachmentBar *bar;
    
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));
    
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (data);
    edit_selected (data);
}

static void
remove_cb (GtkWidget *widget, gpointer data, GtkWidget *for_widget)
{
    EMsgComposerAttachmentBar *bar;
    
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (data));
    
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (data);
    remove_selected (bar);
}


/* Popup menu handling.  */

static GnomeUIInfo icon_context_menu_info[] = {
    GNOMEUIINFO_ITEM (N_("Remove"),
              N_("Remove selected items from the attachment list"),
              remove_cb, NULL),
    GNOMEUIINFO_MENU_PROPERTIES_ITEM (properties_cb, NULL),
    GNOMEUIINFO_END
};

static GtkWidget *
get_icon_context_menu (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    
    priv = bar->priv;
    if (priv->icon_context_menu == NULL)
        priv->icon_context_menu = gnome_popup_menu_new (icon_context_menu_info);
    
    return priv->icon_context_menu;
}

static void
popup_icon_context_menu (EMsgComposerAttachmentBar *bar,
             gint num,
             GdkEventButton *event)
{
    GtkWidget *menu;
    
    menu = get_icon_context_menu (bar);
    gnome_popup_menu_do_popup (menu, NULL, NULL, event, bar, NULL);
}

static GnomeUIInfo context_menu_info[] = {
    GNOMEUIINFO_ITEM (N_("Add attachment..."),
              N_("Attach a file to the message"),
              add_cb, NULL),
    GNOMEUIINFO_END
};

static GtkWidget *
get_context_menu (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    
    priv = bar->priv;
    if (priv->context_menu == NULL)
        priv->context_menu = gnome_popup_menu_new (context_menu_info);
    
    return priv->context_menu;
}

static void
popup_context_menu (EMsgComposerAttachmentBar *bar,
            GdkEventButton *event)
{
    GtkWidget *menu;
    
    menu = get_context_menu (bar);
    gnome_popup_menu_do_popup (menu, NULL, NULL, event, bar, NULL);
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
    EMsgComposerAttachmentBar *bar;
    
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (object);
    
    if (bar->priv) {
        free_attachment_list (bar);
        g_free (bar->priv);
        bar->priv = NULL;
    }
    
    if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* GtkWidget methods.  */


static void
popup_menu_placement_callback (GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer user_data)
{
    EMsgComposerAttachmentBar *bar;
    GnomeIconList *icon_list;
    GList *selection;
    GnomeCanvasPixbuf *image;
    
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (user_data);
    icon_list = GNOME_ICON_LIST (user_data);
    
    gdk_window_get_origin (((GtkWidget*) bar)->window, x, y);
    
    selection = gnome_icon_list_get_selection (icon_list);
    if (selection == NULL)
        return;
    
    image = gnome_icon_list_get_icon_pixbuf_item (icon_list, (gint)selection->data);
    if (image == NULL)
        return;
    
    /* Put menu to the center of icon. */
    *x += (int)(image->item.x1 + image->item.x2) / 2;
    *y += (int)(image->item.y1 + image->item.y2) / 2;
}

static gboolean 
popup_menu_event (GtkWidget *widget)
{
    EMsgComposerAttachmentBar *bar = E_MSG_COMPOSER_ATTACHMENT_BAR (widget);
    GnomeIconList *icon_list = GNOME_ICON_LIST (widget);
    GList *selection = gnome_icon_list_get_selection (icon_list);
    GtkWidget *menu;
    
    if (selection == NULL)
        menu = get_context_menu (bar);
    else
        menu = get_icon_context_menu (bar);
    
    gnome_popup_menu_do_popup (menu, popup_menu_placement_callback, 
                   widget, NULL, widget, NULL);
    
    return TRUE;
}


static gint
button_press_event (GtkWidget *widget,
            GdkEventButton *event)
{
    EMsgComposerAttachmentBar *bar;
    GnomeIconList *icon_list;
    int icon_number;
    
    bar = E_MSG_COMPOSER_ATTACHMENT_BAR (widget);
    icon_list = GNOME_ICON_LIST (widget);
    
    if (event->button != 3)
        return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
    
    icon_number = gnome_icon_list_get_icon_at (icon_list, event->x, event->y);
    
    if (icon_number >= 0) {
        gnome_icon_list_select_icon (icon_list, icon_number);
        popup_icon_context_menu (bar, icon_number, event);
    } else {
        popup_context_menu (bar, event);
    }
    
    return TRUE;
}


/* Initialization.  */

static void
class_init (EMsgComposerAttachmentBarClass *klass)
{
    GtkObjectClass *object_class;
    GtkWidgetClass *widget_class;
    GnomeIconListClass *icon_list_class;
    
    object_class = GTK_OBJECT_CLASS (klass);
    widget_class = GTK_WIDGET_CLASS (klass);
    icon_list_class = GNOME_ICON_LIST_CLASS (klass);
    
    parent_class = g_type_class_ref (gnome_icon_list_get_type ());
    
    object_class->destroy = destroy;
    
    widget_class->button_press_event = button_press_event;
    widget_class->popup_menu = popup_menu_event;

    
    /* Setup signals.  */
    
    signals[CHANGED] =
        g_signal_new ("changed",
                  E_TYPE_MSG_COMPOSER_ATTACHMENT_BAR,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMsgComposerAttachmentBarClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
}

static void
init (EMsgComposerAttachmentBar *bar)
{
    EMsgComposerAttachmentBarPrivate *priv;
    
    priv = g_new (EMsgComposerAttachmentBarPrivate, 1);
    
    priv->attachments = NULL;
    priv->context_menu = NULL;
    priv->icon_context_menu = NULL;
    
    priv->num_attachments = 0;
    
    bar->priv = priv;
}


GType
e_msg_composer_attachment_bar_get_type (void)
{
    static GType type = 0;
    
    if (type == 0) {
        static const GTypeInfo info = {
            sizeof (EMsgComposerAttachmentBarClass),
            NULL, NULL,
            (GClassInitFunc) class_init,
            NULL, NULL,
            sizeof (EMsgComposerAttachmentBar),
            0,
            (GInstanceInitFunc) init,
        };
        
        type = g_type_register_static (GNOME_TYPE_ICON_LIST, "EMsgComposerAttachmentBar", &info, 0);
    }
    
    return type;
}

GtkWidget *
e_msg_composer_attachment_bar_new (GtkAdjustment *adj)
{
    EMsgComposerAttachmentBar *new;
    GnomeIconList *icon_list;
    int width, height, icon_width, window_height;
    PangoFontMetrics *metrics;
    PangoContext *context;
    
    new = g_object_new (e_msg_composer_attachment_bar_get_type (), NULL);
    
    icon_list = GNOME_ICON_LIST (new);
    
    context = gtk_widget_get_pango_context ((GtkWidget *) new);
    metrics = pango_context_get_metrics (context, ((GtkWidget *) new)->style->font_desc, pango_context_get_language (context));
    width = PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (metrics)) * 15;
    /* This should be *2, but the icon list creates too much space above ... */
    height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics)) * 3;
    pango_font_metrics_unref (metrics);
    
    icon_width = ICON_WIDTH + ICON_SPACING + ICON_BORDER + ICON_TEXT_SPACING;
    icon_width = MAX (icon_width, width);
    
    gnome_icon_list_construct (icon_list, icon_width, adj, 0);
    
    window_height = ICON_WIDTH + ICON_SPACING + ICON_BORDER + ICON_TEXT_SPACING + height;
    gtk_widget_set_size_request (GTK_WIDGET (new), icon_width * 4, window_height);
    
    gnome_icon_list_set_separators (icon_list, ICON_SEPARATORS);
    gnome_icon_list_set_row_spacing (icon_list, ICON_ROW_SPACING);
    gnome_icon_list_set_col_spacing (icon_list, ICON_COL_SPACING);
    gnome_icon_list_set_icon_border (icon_list, ICON_BORDER);
    gnome_icon_list_set_text_spacing (icon_list, ICON_TEXT_SPACING);
    gnome_icon_list_set_selection_mode (icon_list, GTK_SELECTION_MULTIPLE);
    
    return GTK_WIDGET (new);
}

static const char *
get_default_charset (void)
{
    GConfClient *gconf;
    const char *charset;
    char *buf;
    
    gconf = gconf_client_get_default ();
    buf = gconf_client_get_string (gconf, "/apps/evolution/mail/composer/charset", NULL);
    g_object_unref (gconf);
    
    if (buf != NULL) {
        charset = e_iconv_charset_name (buf);
        g_free (buf);
    } else
        charset = e_iconv_locale_charset ();
    
    return charset;
}

static void
attach_to_multipart (CamelMultipart *multipart,
             EMsgComposerAttachment *attachment,
             const char *default_charset)
{
    CamelContentType *content_type;
    CamelDataWrapper *content;
    
    content_type = camel_mime_part_get_content_type (attachment->body);
    content = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body));
    
    if (!CAMEL_IS_MULTIPART (content)) {
        if (header_content_type_is (content_type, "text", "*")) {
            CamelMimePartEncodingType encoding;
            CamelStreamFilter *filter_stream;
            CamelMimeFilterBestenc *bestenc;
            CamelStream *stream;
            const char *charset;
            char *type;
            
            charset = header_content_type_param (content_type, "charset");
            
            stream = camel_stream_null_new ();
            filter_stream = camel_stream_filter_new_with_stream (stream);
            bestenc = camel_mime_filter_bestenc_new (CAMEL_BESTENC_GET_ENCODING);
            camel_stream_filter_add (filter_stream, CAMEL_MIME_FILTER (bestenc));
            camel_object_unref (stream);
            
            camel_data_wrapper_write_to_stream (content, CAMEL_STREAM (filter_stream));
            camel_object_unref (filter_stream);
            
            encoding = camel_mime_filter_bestenc_get_best_encoding (bestenc, CAMEL_BESTENC_8BIT);
            camel_mime_part_set_encoding (attachment->body, encoding);
            
            if (encoding == CAMEL_MIME_PART_ENCODING_7BIT) {
                /* the text fits within us-ascii so this is safe */
                /* FIXME: check that this isn't iso-2022-jp? */
                default_charset = "us-ascii";
            } else if (!charset) {
                if (!default_charset)
                    default_charset = get_default_charset ();
                
                /* FIXME: We should really check that this fits within the
                                   default_charset and if not find one that does and/or
                   allow the user to specify? */
            }
            
            if (!charset) {
                /* looks kinda nasty, but this is how ya have to do it */
                header_content_type_set_param (content_type, "charset", default_charset);
                type = header_content_type_format (content_type);
                camel_mime_part_set_content_type (attachment->body, type);
                g_free (type);
            }
            
            camel_object_unref (bestenc);
        } else if (!CAMEL_IS_MIME_MESSAGE (content)) {
            camel_mime_part_set_encoding (attachment->body, CAMEL_MIME_PART_ENCODING_BASE64);
        }
    }
    
    camel_multipart_add_part (multipart, attachment->body);
}

void
e_msg_composer_attachment_bar_to_multipart (EMsgComposerAttachmentBar *bar,
                        CamelMultipart *multipart,
                        const char *default_charset)
{
    EMsgComposerAttachmentBarPrivate *priv;
    GList *p;
    
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));
    g_return_if_fail (CAMEL_IS_MULTIPART (multipart));
    
    priv = bar->priv;
    
    for (p = priv->attachments; p != NULL; p = p->next) {
        EMsgComposerAttachment *attachment;
        
        attachment = E_MSG_COMPOSER_ATTACHMENT (p->data);
        attach_to_multipart (multipart, attachment, default_charset);
    }
}


guint
e_msg_composer_attachment_bar_get_num_attachments (EMsgComposerAttachmentBar *bar)
{
    g_return_val_if_fail (bar != NULL, 0);
    g_return_val_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar), 0);
    
    return bar->priv->num_attachments;
}


void
e_msg_composer_attachment_bar_attach (EMsgComposerAttachmentBar *bar,
                      const gchar *file_name)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));
    
    if (file_name == NULL)
        add_from_user (bar);
    else
        add_from_file (bar, file_name, "attachment");
}

void
e_msg_composer_attachment_bar_attach_mime_part (EMsgComposerAttachmentBar *bar,
                        CamelMimePart *part)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT_BAR (bar));
    
    add_from_mime_part (bar, part);
}