aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/e-msg-composer.c
blob: 70602c001865f38c1ba94f54d35e1d06471c546d (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
/* -*- 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 <glade/glade.h>
#include <camel/camel.h>

#include "e-msg-composer.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;


/* This functions builds a CamelMimeMessage for the message that the user has
   composed in `composer'.  */
static CamelMimeMessage *
build_message (EMsgComposer *composer)
{
    CamelMimeMessage *new;
    CamelMimeBodyPart *body_part;
    CamelMultipart *multipart;
    gchar *text;

    new = camel_mime_message_new_with_session (NULL);

    e_msg_composer_hdrs_to_message (E_MSG_COMPOSER_HDRS (composer->hdrs),
                    new);

    multipart = camel_multipart_new ();
    body_part = camel_mime_body_part_new ();

    text = gtk_editable_get_chars (GTK_EDITABLE (composer->text), 0, -1);
    camel_mime_part_set_text (CAMEL_MIME_PART (body_part), text);
    camel_multipart_add_part (multipart, body_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));

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

    return new;
}


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.  */

    gtk_check_menu_item_set_active
        (GTK_CHECK_MENU_ITEM
         (glade_xml_get_widget (composer->menubar_gui,
                    "menu_view_attachments")),
         show);

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


/* Callbacks.  */

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

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);
}

static void
glade_connect (GladeXML *gui,
           const gchar *widget_name,
           const gchar *signal_name,
           GtkSignalFunc callback,
           gpointer callback_data)
{
    GtkWidget *widget;

    widget = glade_xml_get_widget (gui, widget_name);
    if (widget == NULL)
        g_warning ("Widget `%s' was not found.", widget_name);
    else
        gtk_signal_connect (GTK_OBJECT (widget), signal_name,
                    GTK_SIGNAL_FUNC (callback), callback_data);
}

static void
attachment_bar_changed (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);
}

static void
setup_signals (EMsgComposer *composer)
{
    glade_connect (composer->menubar_gui, "menu_send",
               "activate", GTK_SIGNAL_FUNC (send_cb), composer);
    glade_connect (composer->toolbar_gui, "toolbar_send",
               "clicked", GTK_SIGNAL_FUNC (send_cb), composer);

    glade_connect (composer->menubar_gui, "menu_view_attachments",
               "activate",
               GTK_SIGNAL_FUNC (menu_view_attachments_activate_cb),
               composer);
    glade_connect (composer->toolbar_gui, "toolbar_view_attachments",
               "clicked",
               GTK_SIGNAL_FUNC (toolbar_view_attachments_clicked_cb),
               composer);

    glade_connect (composer->menubar_gui, "menu_add_attachment",
               "activate",
               GTK_SIGNAL_FUNC (add_attachment_cb), composer);
    glade_connect (composer->toolbar_gui, "toolbar_add_attachment",
               "clicked",
               GTK_SIGNAL_FUNC (add_attachment_cb), composer);

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


/* GtkObject methods.  */

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

    composer = E_MSG_COMPOSER (object);

    gtk_object_unref (GTK_OBJECT (composer->menubar_gui));
    gtk_object_unref (GTK_OBJECT (composer->toolbar_gui));
    gtk_object_unref (GTK_OBJECT (composer->appbar_gui));

    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->menubar_gui = NULL;
    composer->toolbar_gui = NULL;
    composer->appbar_gui = NULL;

    composer->hdrs = NULL;

    composer->text = NULL;
    composer->text_scrolled_window = 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;
}


void
e_msg_composer_construct (EMsgComposer *composer)
{
    GtkWidget *vbox;

    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->menubar_gui = glade_xml_new (E_GUIDIR "/e-msg-composer.glade",
                           "menubar");
    gnome_app_set_menus (GNOME_APP (composer),
                 GTK_MENU_BAR (glade_xml_get_widget (composer->menubar_gui,
                                 "menubar")));

    composer->toolbar_gui = glade_xml_new (E_GUIDIR "/e-msg-composer.glade",
                           "toolbar");
    gnome_app_set_toolbar (GNOME_APP (composer),
                   GTK_TOOLBAR (glade_xml_get_widget (composer->toolbar_gui,
                                  "toolbar")));

    composer->appbar_gui = glade_xml_new (E_GUIDIR "/e-msg-composer.glade",
                          "appbar");
    gnome_app_set_statusbar (GNOME_APP (composer),
                 glade_xml_get_widget (composer->appbar_gui,
                               "appbar"));

    vbox = gtk_vbox_new (FALSE, GNOME_PAD);

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

    /* GtkText for message body editing, wrapped into a
       GtkScrolledWindow.  */

    composer->text_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy
        (GTK_SCROLLED_WINDOW (composer->text_scrolled_window),
         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    composer->text = gtk_text_new (NULL, NULL);
    gtk_text_set_word_wrap (GTK_TEXT (composer->text), FALSE);
    gtk_text_set_editable (GTK_TEXT (composer->text), TRUE);
    gtk_container_add (GTK_CONTAINER (composer->text_scrolled_window),
               composer->text);
    gtk_widget_show (composer->text);
    gtk_box_pack_start (GTK_BOX (vbox), composer->text_scrolled_window,
                TRUE, TRUE, 0);
    gtk_widget_show (composer->text_scrolled_window);

    /* 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, 0);

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

    e_msg_composer_show_attachments (composer, FALSE);

    setup_signals (composer);
}

GtkWidget *
e_msg_composer_new (void)
{
    GtkWidget *new;
 
    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_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);
}