aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-msg-composer.c
blob: a4866711e377cb1695ab36e5fb4d1db30137be62 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-msg-composer.c
 *
 * Copyright (C) 1999  Helix Code, Inc.
 *
 * 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 Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

/*

   TODO

   - Somehow users should be able to see if any file(s) are attached even when
     the attachment bar is not shown.

*/

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

#include <gnome.h>
#include <libgnorba/gnorba.h>
#include <bonobo.h>
#include <bonobo/bonobo-stream-memory.h>

#include <glade/glade.h>

#include <camel/camel.h>

#include "e-msg-composer.h"
#include "e-msg-composer-address-dialog.h"
#include "e-msg-composer-attachment-bar.h"
#include "e-msg-composer-hdrs.h"


#define DEFAULT_WIDTH 600
#define DEFAULT_HEIGHT 500


enum {
    SEND,
    POSTPONE,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static GnomeAppClass *parent_class = NULL;


static GtkWidget *
create_editor (EMsgComposer *composer)
{
    GtkWidget *control;

    /* FIXME: Hardcoded value sucks!  */
    control = bonobo_widget_new_control ("control:html-editor",
                         bonobo_object_corba_objref (BONOBO_OBJECT (composer->uih)));
    if (control == NULL) {
        g_warning ("Cannot get the `control:html-editor' component.");
        return NULL;
    }

    return control;
}


static void
free_string_list (GList *list)
{
    GList *p;

    if (list == NULL)
        return;

    for (p = list; p != NULL; p = p->next)
        g_free (p->data);

    g_list_free (list);
}

static char *
get_editor_text (BonoboWidget *editor)
{
    Bonobo_PersistStream persist;
    BonoboStream *stream;
    CORBA_Environment ev;
    char *text;

    CORBA_exception_init (&ev);
    persist = (Bonobo_PersistStream)
        bonobo_object_client_query_interface (
            bonobo_widget_get_server (editor),
            "IDL:Bonobo/PersistStream:1.0",
            &ev);
    g_assert (persist != CORBA_OBJECT_NIL);

    stream = bonobo_stream_mem_create (NULL, 0, FALSE, TRUE);
    Bonobo_PersistStream_save (persist, (Bonobo_Stream)bonobo_object_corba_objref (BONOBO_OBJECT (stream)), &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        /* FIXME. Some error message. */
        return NULL;
    }
    if (ev._major != CORBA_SYSTEM_EXCEPTION)
        CORBA_Object_release (persist, &ev);

    Bonobo_Unknown_unref (persist, &ev);
    CORBA_exception_free (&ev);

    text = g_strdup (bonobo_stream_mem_get_buffer (BONOBO_STREAM_MEM (stream)));
    bonobo_object_unref (BONOBO_OBJECT(stream));
    return text;
}


/* This functions builds a CamelMimeMessage for the message that the user has
   composed in `composer'.  */
static CamelMimeMessage *
build_message (EMsgComposer *composer)
{
    CamelMimeMessage *new;
    char *text;
    int i;
    
    new = camel_mime_message_new ();

    e_msg_composer_hdrs_to_message (E_MSG_COMPOSER_HDRS (composer->hdrs),
                    new);
    for (i = 0; i < composer->extra_hdr_names->len; i++) {
        camel_medium_add_header (CAMEL_MEDIUM (new),
                     composer->extra_hdr_names->pdata[i],
                     composer->extra_hdr_values->pdata[i]);
    }

    text = get_editor_text (BONOBO_WIDGET (composer->editor));

    if (e_msg_composer_attachment_bar_get_num_attachments (E_MSG_COMPOSER_ATTACHMENT_BAR (composer->attachment_bar))) {
        CamelMultipart *multipart = camel_multipart_new ();
        CamelMimePart *part;

        part = camel_mime_part_new ();
        camel_mime_part_set_content (part, text,
                         strlen (text), "text/html");
        camel_multipart_add_part (multipart, part);

        e_msg_composer_attachment_bar_to_multipart (E_MSG_COMPOSER_ATTACHMENT_BAR (composer->attachment_bar), multipart);

        camel_medium_set_content_object (CAMEL_MEDIUM (new),
                         CAMEL_DATA_WRAPPER (multipart));
    } else {
        CamelDataWrapper *cdw;
        CamelStream *stream;

        stream = camel_stream_mem_new_with_buffer (text,
                               strlen (text));
        cdw = camel_data_wrapper_new ();
        camel_data_wrapper_construct_from_stream (cdw, stream);
        camel_data_wrapper_set_mime_type (cdw, "text/html");

        camel_medium_set_content_object (CAMEL_MEDIUM (new),
                         CAMEL_DATA_WRAPPER (cdw));
    }
    g_free (text);

    /* FIXME refcounting is most certainly wrong.  We want all the stuff to
           be destroyed when we unref() the message.  */

    return new;
}

static void
set_editor_text (BonoboWidget *editor, const char *text)
{
    Bonobo_PersistStream persist;
    BonoboStream *stream;
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    persist = (Bonobo_PersistStream)
        bonobo_object_client_query_interface (
            bonobo_widget_get_server (editor),
            "IDL:Bonobo/PersistStream:1.0",
            &ev);
    g_assert (persist != CORBA_OBJECT_NIL);

    stream = bonobo_stream_mem_create ((char *)text, strlen (text),
                       TRUE, FALSE);
    Bonobo_PersistStream_load (persist, (Bonobo_Stream)bonobo_object_corba_objref (BONOBO_OBJECT (stream)), &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        /* FIXME. Some error message. */
        return;
    }
    if (ev._major != CORBA_SYSTEM_EXCEPTION)
        CORBA_Object_release (persist, &ev);

    Bonobo_Unknown_unref (persist, &ev);
    CORBA_exception_free (&ev);
    bonobo_object_unref (BONOBO_OBJECT(stream));
}


static void
show_attachments (EMsgComposer *composer,
          gboolean show)
{
    if (show) {
        gtk_widget_show (composer->attachment_scrolled_window);
        gtk_widget_show (composer->attachment_bar);
    } else {
        gtk_widget_hide (composer->attachment_scrolled_window);
        gtk_widget_hide (composer->attachment_bar);
    }

    composer->attachment_bar_visible = show;

    /* Update the GUI.  */

#if 0
    gtk_check_menu_item_set_active
        (GTK_CHECK_MENU_ITEM
         (glade_xml_get_widget (composer->menubar_gui,
                    "menu_view_attachments")),
         show);
#endif

    /* XXX we should update the toggle toolbar item as well.  At
       this point, it is not a toggle because Glade is broken.  */
}


/* Address dialog callbacks.  */

static void
address_dialog_destroy_cb (GtkWidget *widget,
               gpointer data)
{
    EMsgComposer *composer;

    composer = E_MSG_COMPOSER (data);
    composer->address_dialog = NULL;
}

static void
address_dialog_apply_cb (EMsgComposerAddressDialog *dialog,
             gpointer data)
{
    EMsgComposerHdrs *hdrs;
    GList *list;

    hdrs = E_MSG_COMPOSER_HDRS (E_MSG_COMPOSER (data)->hdrs);

    list = e_msg_composer_address_dialog_get_to_list (dialog);
    e_msg_composer_hdrs_set_to (hdrs, list);

    list = e_msg_composer_address_dialog_get_cc_list (dialog);
    e_msg_composer_hdrs_set_cc (hdrs, list);

    list = e_msg_composer_address_dialog_get_bcc_list (dialog);
    e_msg_composer_hdrs_set_bcc (hdrs, list);
}


/* Message composer window callbacks.  */

static void
send_cb (GtkWidget *widget,
     gpointer data)
{
    gtk_signal_emit (GTK_OBJECT (data), signals[SEND]);
}

static void
exit_dialog_cb (int reply, gpointer data)
{
    if (reply == 0)
        gtk_widget_destroy (GTK_WIDGET (data));
}

static void
exit_cb (GtkWidget *widget, gpointer data)
{
    EMsgComposer *composer = E_MSG_COMPOSER (data);
    GtkWindow *parent =
        GTK_WINDOW (gtk_widget_get_ancestor (GTK_WIDGET (data),
                             GTK_TYPE_WINDOW));

    gnome_ok_cancel_dialog_parented ("Discard this message?",
                     exit_dialog_cb, composer, parent);
}
    

static void
menu_view_attachments_activate_cb (GtkWidget *widget,
                   gpointer data)
{
    e_msg_composer_show_attachments (E_MSG_COMPOSER (data),
                     GTK_CHECK_MENU_ITEM (widget)->active);
}

static void
toolbar_view_attachments_clicked_cb (GtkWidget *widget,
                      gpointer data)
{
    EMsgComposer *composer;

    composer = E_MSG_COMPOSER (data);

    e_msg_composer_show_attachments (composer,
                     ! composer->attachment_bar_visible);
}

static void
add_attachment_cb (GtkWidget *widget,
           gpointer data)
{
    EMsgComposer *composer;

    composer = E_MSG_COMPOSER (data);

    e_msg_composer_attachment_bar_attach
        (E_MSG_COMPOSER_ATTACHMENT_BAR (composer->attachment_bar),
         NULL);
}

/* Create the address dialog if not created already.  */
static void
setup_address_dialog (EMsgComposer *composer)
{
    EMsgComposerAddressDialog *dialog;
    EMsgComposerHdrs *hdrs;
    GList *list;

    if (composer->address_dialog != NULL)
        return;

    composer->address_dialog = e_msg_composer_address_dialog_new ();
    dialog = E_MSG_COMPOSER_ADDRESS_DIALOG (composer->address_dialog);
    hdrs = E_MSG_COMPOSER_HDRS (composer->hdrs);

    gtk_signal_connect (GTK_OBJECT (dialog),
                "destroy", address_dialog_destroy_cb, composer);
    gtk_signal_connect (GTK_OBJECT (dialog),
                "apply", address_dialog_apply_cb, composer);

    list = e_msg_composer_hdrs_get_to (hdrs);
    e_msg_composer_address_dialog_set_to_list (dialog, list);

    list = e_msg_composer_hdrs_get_cc (hdrs);
    e_msg_composer_address_dialog_set_cc_list (dialog, list);

    list = e_msg_composer_hdrs_get_bcc (hdrs);
    e_msg_composer_address_dialog_set_bcc_list (dialog, list);
}

static void
address_dialog_cb (GtkWidget *widget,
           gpointer data)
{
    EMsgComposer *composer;

    /* FIXME maybe we should hide the dialog on Cancel/OK instead of
           destroying it.  */

    composer = E_MSG_COMPOSER (data);

    setup_address_dialog (composer);

    gtk_widget_show (composer->address_dialog);
    gdk_window_show (composer->address_dialog->window);
}

static void
attachment_bar_changed_cb (EMsgComposerAttachmentBar *bar,
               gpointer data)
{
    EMsgComposer *composer;
    
    composer = E_MSG_COMPOSER (data);

    if (e_msg_composer_attachment_bar_get_num_attachments (bar) > 0)
        e_msg_composer_show_attachments (composer, TRUE);
    else
        e_msg_composer_show_attachments (composer, FALSE);
}


/* Menu bar implementation.  */

static GnomeUIInfo file_tree[] = {
    GNOMEUIINFO_MENU_OPEN_ITEM (NULL, NULL),
    GNOMEUIINFO_MENU_SAVE_ITEM (NULL, NULL),
    GNOMEUIINFO_MENU_SAVE_AS_ITEM (NULL, NULL),
    GNOMEUIINFO_ITEM_NONE (N_("Save in _folder..."), N_("Save the message in a specified folder"),
                   NULL),
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_ITEM_STOCK (N_("Send"), N_("Send the message"),
                send_cb, GNOME_STOCK_MENU_MAIL_SND),
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_MENU_EXIT_ITEM (exit_cb, NULL),
    GNOMEUIINFO_END
};

static GnomeUIInfo view_tree[] = {
    GNOMEUIINFO_ITEM_STOCK (N_("View _attachments"), N_("View/hide attachments"),
                menu_view_attachments_activate_cb, GNOME_STOCK_MENU_ATTACH),
    GNOMEUIINFO_END
};

static GnomeUIInfo menubar_info[] = {
    GNOMEUIINFO_MENU_FILE_TREE (file_tree),
    GNOMEUIINFO_MENU_VIEW_TREE (view_tree),
    GNOMEUIINFO_END
};

static void
create_menubar (EMsgComposer *composer)
{
    BonoboUIHandler *uih;
    BonoboUIHandlerMenuItem *list;

    uih = composer->uih;

    bonobo_ui_handler_create_menubar (uih);

    list = bonobo_ui_handler_menu_parse_uiinfo_list_with_data (menubar_info, composer);
    bonobo_ui_handler_menu_add_list (uih, "/", list);
}


/* Toolbar implementation.  */

static GnomeUIInfo toolbar_info[] = {
    GNOMEUIINFO_ITEM_STOCK (N_("Send"), N_("Send this message"), send_cb, GNOME_STOCK_PIXMAP_MAIL_SND),
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_ITEM_STOCK (N_("Cut"), N_("Cut selected region into the clipboard"), NULL, GNOME_STOCK_PIXMAP_CUT),
    GNOMEUIINFO_ITEM_STOCK (N_("Copy"), N_("Copy selected region into the clipboard"), NULL, GNOME_STOCK_PIXMAP_COPY),
    GNOMEUIINFO_ITEM_STOCK (N_("Paste"), N_("Paste selected region into the clipboard"), NULL, GNOME_STOCK_PIXMAP_PASTE),
    GNOMEUIINFO_ITEM_STOCK (N_("Undo"), N_("Undo last operation"), NULL, GNOME_STOCK_PIXMAP_UNDO),
    GNOMEUIINFO_SEPARATOR,
    GNOMEUIINFO_ITEM_STOCK (N_("Attach"), N_("Attach a file"), add_attachment_cb, GNOME_STOCK_PIXMAP_ATTACH),
    GNOMEUIINFO_END
};

static void
create_toolbar (EMsgComposer *composer)
{
    BonoboUIHandler *uih;
    BonoboUIHandlerToolbarItem *list;

    uih = composer->uih;

    bonobo_ui_handler_create_toolbar (uih, "Toolbar");

    list = bonobo_ui_handler_toolbar_parse_uiinfo_list_with_data (toolbar_info, composer);
    bonobo_ui_handler_toolbar_add_list (uih, "/Toolbar", list);
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
    EMsgComposer *composer;

    composer = E_MSG_COMPOSER (object);

    bonobo_object_unref (BONOBO_OBJECT (composer->uih));

    /* FIXME?  I assume the Bonobo widget will get destroyed
           normally?  */

    if (composer->address_dialog != NULL)
        gtk_widget_destroy (composer->address_dialog);
    if (composer->hdrs != NULL)
        gtk_widget_destroy (composer->hdrs);

    if (composer->extra_hdr_names) {
        int i;

        for (i = 0; i < composer->extra_hdr_names->len; i++) {
            g_free (composer->extra_hdr_names->pdata[i]);
            g_free (composer->extra_hdr_values->pdata[i]);
        }
        g_ptr_array_free (composer->extra_hdr_names, TRUE);
        g_ptr_array_free (composer->extra_hdr_values, TRUE);
    }

    if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


static void
class_init (EMsgComposerClass *klass)
{
    GtkObjectClass *object_class;

    object_class = (GtkObjectClass *) klass;

    object_class->destroy = destroy;

    parent_class = gtk_type_class (gnome_app_get_type ());

    signals[SEND] =
        gtk_signal_new ("send",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EMsgComposerClass, send),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    signals[POSTPONE] =
        gtk_signal_new ("postpone",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (EMsgComposerClass, postpone),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}

static void
init (EMsgComposer *composer)
{
    composer->uih = NULL;

    composer->hdrs = NULL;
    composer->extra_hdr_names = g_ptr_array_new ();
    composer->extra_hdr_values = g_ptr_array_new ();

    composer->editor = NULL;

    composer->address_dialog = NULL;

    composer->attachment_bar = NULL;
    composer->attachment_scrolled_window = NULL;
}


GtkType
e_msg_composer_get_type (void)
{
    static GtkType type = 0;

    if (type == 0) {
        static const GtkTypeInfo info = {
            "EMsgComposer",
            sizeof (EMsgComposer),
            sizeof (EMsgComposerClass),
            (GtkClassInitFunc) class_init,
            (GtkObjectInitFunc) init,
            /* reserved_1 */ NULL,
            /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };

        type = gtk_type_unique (gnome_app_get_type (), &info);
    }

    return type;
}


/**
 * e_msg_composer_construct:
 * @composer: A message composer widget
 * 
 * Construct @composer.
 **/
void
e_msg_composer_construct (EMsgComposer *composer)
{
    GtkWidget *vbox;

    g_return_if_fail (gtk_main_level () > 0);

    gtk_window_set_default_size (GTK_WINDOW (composer),
                     DEFAULT_WIDTH, DEFAULT_HEIGHT);

    gnome_app_construct (GNOME_APP (composer), "e-msg-composer",
                 "Compose a message");

    composer->uih = bonobo_ui_handler_new ();
    bonobo_ui_handler_set_app (composer->uih, GNOME_APP (composer));

    vbox = gtk_vbox_new (FALSE, 0);

    composer->hdrs = e_msg_composer_hdrs_new ();
    gtk_box_pack_start (GTK_BOX (vbox), composer->hdrs, FALSE, TRUE, 0);
    gtk_widget_show (composer->hdrs);

    /* Editor component.  */

    create_menubar (composer);
    create_toolbar (composer);
    composer->editor = create_editor (composer);

    gtk_widget_show (composer->editor);
    gtk_box_pack_start (GTK_BOX (vbox), composer->editor, TRUE, TRUE, 0);
    gtk_widget_show (composer->editor);

    /* Attachment editor, wrapped into a GtkScrolledWindow.  We don't
           show it for now.  */

    composer->attachment_scrolled_window = gtk_scrolled_window_new (NULL,
                                    NULL);
    gtk_scrolled_window_set_policy
        (GTK_SCROLLED_WINDOW (composer->attachment_scrolled_window),
         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    composer->attachment_bar = e_msg_composer_attachment_bar_new (NULL);
    GTK_WIDGET_SET_FLAGS (composer->attachment_bar, GTK_CAN_FOCUS);
    gtk_container_add (GTK_CONTAINER (composer->attachment_scrolled_window),
               composer->attachment_bar);
    gtk_box_pack_start (GTK_BOX (vbox),
                composer->attachment_scrolled_window,
                FALSE, TRUE, GNOME_PAD_SMALL);

    gtk_signal_connect (GTK_OBJECT (composer->attachment_bar), "changed",
                GTK_SIGNAL_FUNC (attachment_bar_changed_cb), composer);

    gnome_app_set_contents (GNOME_APP (composer), vbox);
    gtk_widget_show (vbox);

    e_msg_composer_show_attachments (composer, FALSE);
}

/**
 * e_msg_composer_new:
 *
 * Create a new message composer widget.  This function must be called
 * within the GTK+ main loop, or it will fail.
 * 
 * Return value: A pointer to the newly created widget
 **/
GtkWidget *
e_msg_composer_new (void)
{
    GtkWidget *new;

    g_return_val_if_fail (gtk_main_level () > 0, NULL);
 
    new = gtk_type_new (e_msg_composer_get_type ());
    e_msg_composer_construct (E_MSG_COMPOSER (new));

    return new;
}


/**
 * e_msg_composer_show_attachments:
 * @composer: A message composer widget
 * @show: A boolean specifying whether the attachment bar should be shown or
 * not
 * 
 * If @show is %FALSE, hide the attachment bar.  Otherwise, show it.
 **/
void
e_msg_composer_show_attachments (EMsgComposer *composer,
                 gboolean show)
{
    g_return_if_fail (composer != NULL);
    g_return_if_fail (E_IS_MSG_COMPOSER (composer));

    show_attachments (composer, show);
}


/**
 * e_msg_composer_set_headers:
 * @composer: a composer object
 * @to: the values for the "To" header
 * @cc: the values for the "Cc" header
 * @bcc: the values for the "Bcc" header
 * @subject: the value for the "Subject" header
 *
 * Sets the headers in the composer to the given values.
 **/
void 
e_msg_composer_set_headers (EMsgComposer *composer, const GList *to,
                const GList *cc, const GList *bcc,
                const char *subject)
{
    EMsgComposerHdrs *hdrs;

    g_return_if_fail (E_IS_MSG_COMPOSER (composer));
    hdrs = E_MSG_COMPOSER_HDRS (composer->hdrs);

    e_msg_composer_hdrs_set_to (hdrs, to);
    e_msg_composer_hdrs_set_cc (hdrs, cc);
    e_msg_composer_hdrs_set_bcc (hdrs, bcc);
    e_msg_composer_hdrs_set_subject (hdrs, subject);
}


/**
 * e_msg_composer_set_body_text:
 * @composer: a composer object
 * @text: the HTML text to initialize the editor with
 *
 * Loads the given HTML text into the editor.
 **/
void
e_msg_composer_set_body_text (EMsgComposer *composer, const char *text)
{
    g_return_if_fail (E_IS_MSG_COMPOSER (composer));

    set_editor_text (BONOBO_WIDGET (composer->editor), text);
}


/**
 * e_msg_composer_add_header:
 * @composer: a composer object
 * @name: the header name
 * @value: the header value
 *
 * Adds a header with @name and @value to the message. This header
 * may not be displayed by the composer, but will be included in
 * the message it outputs.
 **/
void
e_msg_composer_add_header (EMsgComposer *composer, const char *name,
               const char *value)
{
    g_return_if_fail (E_IS_MSG_COMPOSER (composer));
    g_return_if_fail (name != NULL);
    g_return_if_fail (value != NULL);

    g_ptr_array_add (composer->extra_hdr_names, g_strdup (name));
    g_ptr_array_add (composer->extra_hdr_values, g_strdup (value));
}


/**
 * e_msg_composer_get_message:
 * @composer: A message composer widget
 * 
 * Retrieve the message edited by the user as a CamelMimeMessage.  The
 * CamelMimeMessage object is created on the fly; subsequent calls to this
 * function will always create new objects from scratch.
 * 
 * Return value: A pointer to the new CamelMimeMessage object
 **/
CamelMimeMessage *
e_msg_composer_get_message (EMsgComposer *composer)
{
    g_return_val_if_fail (composer != NULL, NULL);
    g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), NULL);

    return build_message (composer);
}