aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-msg-composer-attachment.c
blob: 45b8a86c8eb9489de2ac4bc7b4a4ba9bc5adc79a (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
/* -*- 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.
 *
 */


/* This is the object representing an email attachment.  It is implemented as a
   GtkObject to make it easier for the application to handle it.  For example,
   the "changed" signal is emitted whenever something changes in the
   attachment.  Also, this contains the code to let users edit the
   attachment manually. */

#include <sys/stat.h>
#include <errno.h>

#include <camel/camel.h>
#include <gtk/gtknotebook.h>
#include <gtk/gtktogglebutton.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>

#include "e-util/e-mktemp.h"

#include "e-msg-composer.h"
#include "e-msg-composer-attachment.h"


enum {
    CHANGED,
    LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };

static GObjectClass *parent_class = NULL;


static void
changed (EMsgComposerAttachment *attachment)
{
    g_signal_emit (attachment, signals[CHANGED], 0);
}


/* GtkObject methods.  */

static void
finalise(GObject *object)
{
    EMsgComposerAttachment *attachment;
    
    attachment = E_MSG_COMPOSER_ATTACHMENT (object);
    
    camel_object_unref (attachment->body);
    if (attachment->pixbuf_cache != NULL)
        g_object_unref (attachment->pixbuf_cache);
    
        G_OBJECT_CLASS (parent_class)->finalize (object);
}


/* Signals.  */

static void
real_changed (EMsgComposerAttachment *msg_composer_attachment)
{
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT (msg_composer_attachment));
}


static void
class_init (EMsgComposerAttachmentClass *klass)
{
    GObjectClass *object_class;
    
    object_class = (GObjectClass*) klass;
    parent_class = g_type_class_ref (G_TYPE_OBJECT);
    
    object_class->finalize = finalise;
    klass->changed = real_changed;
    
    signals[CHANGED] = g_signal_new ("changed",
                     E_TYPE_MSG_COMPOSER_ATTACHMENT,
                     G_SIGNAL_RUN_FIRST,
                     G_STRUCT_OFFSET (EMsgComposerAttachmentClass, changed),
                     NULL,
                     NULL,
                     g_cclosure_marshal_VOID__VOID,
                     G_TYPE_NONE, 0);
}

static void
init (EMsgComposerAttachment *msg_composer_attachment)
{
    msg_composer_attachment->editor_gui = NULL;
    msg_composer_attachment->body = NULL;
    msg_composer_attachment->size = 0;
    msg_composer_attachment->pixbuf_cache = NULL;
}

GType
e_msg_composer_attachment_get_type (void)
{
    static GType type = 0;
    
    if (type == 0) {
        static const GTypeInfo info = {
            sizeof (EMsgComposerAttachmentClass),
            NULL,
            NULL,
            (GClassInitFunc) class_init,
            NULL,
            NULL,
            sizeof (EMsgComposerAttachment),
            0,
            (GInstanceInitFunc) init,
        };
        
        type = g_type_register_static (G_TYPE_OBJECT, "EMsgComposerAttachment", &info, 0);
    }
    
    return type;
}


/**
 * e_msg_composer_attachment_new:
 * @file_name: filename to attach
 * @disposition: Content-Disposition of the attachment
 * @ex: exception
 *
 * Return value: the new attachment, or %NULL on error
 **/
EMsgComposerAttachment *
e_msg_composer_attachment_new (const char *file_name,
                   const char *disposition,
                   CamelException *ex)
{
    EMsgComposerAttachment *new;
    CamelMimePart *part;
    CamelDataWrapper *wrapper;
    CamelStream *stream;
    struct stat statbuf;
    char *mime_type;
    char *filename;
    
    g_return_val_if_fail (file_name != NULL, NULL);
    
    if (stat (file_name, &statbuf) < 0) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot attach file %s: %s"),
                      file_name, g_strerror (errno));
        return NULL;
    }
    
    /* return if it's not a regular file */
    if (!S_ISREG (statbuf.st_mode)) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot attach file %s: not a regular file"),
                      file_name);
        return NULL;
    }
    
    stream = camel_stream_fs_new_with_name (file_name, O_RDONLY, 0);
    if (!stream) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot attach file %s: %s"),
                      file_name, g_strerror (errno));
        return NULL;
    }
    
    wrapper = camel_data_wrapper_new ();
    camel_data_wrapper_construct_from_stream (wrapper, stream);
    
    mime_type = e_msg_composer_guess_mime_type (file_name);
    if (mime_type) {
        if (!strcasecmp (mime_type, "message/rfc822")) {
            camel_object_unref (wrapper);
            wrapper = (CamelDataWrapper *) camel_mime_message_new ();
            
            camel_stream_reset (stream);
            camel_data_wrapper_construct_from_stream (wrapper, stream);
        }
        
        camel_data_wrapper_set_mime_type (wrapper, mime_type);
        g_free (mime_type);
    } else
        camel_data_wrapper_set_mime_type (wrapper, "application/octet-stream");
    
    camel_object_unref (stream);
    
    part = camel_mime_part_new ();
    camel_medium_set_content_object (CAMEL_MEDIUM (part), wrapper);
    camel_object_unref (wrapper);
    
    camel_mime_part_set_disposition (part, disposition);
    filename = g_path_get_basename(file_name);
    camel_mime_part_set_filename (part, filename);
    g_free (filename);
    
#if 0
    /* Note: Outlook 2002 is broken with respect to Content-Ids on
           non-multipart/related parts, so as an interoperability
           workaround, don't set a Content-Id on these parts. Fixes
           bug #10032 */
    /* set the Content-Id */
    content_id = header_msgid_generate ();
    camel_mime_part_set_content_id (part, content_id);
    g_free (content_id);
#endif
    
    new = g_object_new (E_TYPE_MSG_COMPOSER_ATTACHMENT, NULL);
    new->editor_gui = NULL;
    new->body = part;
    new->size = statbuf.st_size;
    new->guessed_type = TRUE;
    
    return new;
}


/**
 * e_msg_composer_attachment_new_from_mime_part:
 * @part: a CamelMimePart
 * 
 * Return value: a new EMsgComposerAttachment based on the mime part
 **/
EMsgComposerAttachment *
e_msg_composer_attachment_new_from_mime_part (CamelMimePart *part)
{
    EMsgComposerAttachment *new;
    CamelMimePart *mime_part;
    CamelStream *stream;
    
    g_return_val_if_fail (CAMEL_IS_MIME_PART (part), NULL);
    
    stream = camel_stream_mem_new ();
    if (camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (part), stream) == -1) {
        camel_object_unref (stream);
        return NULL;
    }
    
    camel_stream_reset (stream);
    mime_part = camel_mime_part_new ();
    
    if (camel_data_wrapper_construct_from_stream (CAMEL_DATA_WRAPPER (mime_part), stream) == -1) {
        camel_object_unref (mime_part);
        camel_object_unref (stream);
        return NULL;
    }
    
    camel_object_unref (stream);
    
    new = g_object_new (E_TYPE_MSG_COMPOSER_ATTACHMENT, NULL);
    new->editor_gui = NULL;
    new->body = mime_part;
    new->guessed_type = FALSE;
    new->size = 0;
    
    return new;
}


/* The attachment property dialog.  */

typedef struct {
    GtkWidget *dialog;
    GtkEntry *file_name_entry;
    GtkEntry *description_entry;
    GtkEntry *mime_type_entry;
    GtkToggleButton *disposition_checkbox;
    EMsgComposerAttachment *attachment;
} DialogData;

static void
destroy_dialog_data (DialogData *data)
{
    g_free (data);
}

/*
 * fixme: I am converting EVERYTHING to/from UTF-8, although mime types
 * are in ASCII. This is not strictly necessary, but we want to be
 * consistent and possibly check for errors somewhere.
 */

static void
update_mime_type (DialogData *data)
{
    const char *filename, *tmpdir;
    char *mime_type, *path, *uri;
    
    if (!data->attachment->guessed_type)
        return;
    
    filename = gtk_entry_get_text (data->file_name_entry);
    if (filename) {
        /* gnome_vfs_get_mime_type() might try to access the
         * file and read it to 'sniff' the mime-type if it is
         * there (and gnome-vfs can't determine the mime-type
         * based on filename extension?), so we need to make
         * sure the file doesn't exist yet we want to keep the
         * filename intact so that gnome-vfs can try and
         * determine the mime-type by the filename. This is
         * kinda nasty.
         */
        
        tmpdir = e_mkdtemp ("mime-identify-XXXXXX");
        path = g_strdup_printf ("%s/%s", tmpdir, filename);
        uri = gnome_vfs_get_uri_from_local_path (path);
        mime_type = gnome_vfs_get_mime_type (uri);
        g_free (path);
        g_free (uri);
        
        if (mime_type) {
            gtk_entry_set_text (data->mime_type_entry, mime_type);
            g_free (mime_type);
        }
    }
}

static void
set_entry (GladeXML *xml, const char *widget_name, const char *value)
{
    GtkEntry *entry;
    
    entry = GTK_ENTRY (glade_xml_get_widget (xml, widget_name));
    if (entry == NULL)
        g_warning ("Entry for `%s' not found.", widget_name);
    else
        gtk_entry_set_text (entry, value ? value : "");
}

static void
connect_widget (GladeXML *gui, const char *name, const char *signal_name,
        GCallback func, gpointer data)
{
    GtkWidget *widget;
    
    widget = glade_xml_get_widget (gui, name);
    g_signal_connect (widget, signal_name, func, data);
}

static void
close_cb (GtkWidget *widget, gpointer data)
{
    EMsgComposerAttachment *attachment;
    DialogData *dialog_data;
    
    dialog_data = (DialogData *) data;
    attachment = dialog_data->attachment;
    
    gtk_widget_destroy (dialog_data->dialog);
    g_object_unref (attachment->editor_gui);
    attachment->editor_gui = NULL;
    
    g_object_unref (attachment);
    
    destroy_dialog_data (dialog_data);
}

static void
ok_cb (GtkWidget *widget, gpointer data)
{
    DialogData *dialog_data;
    EMsgComposerAttachment *attachment;
    const char *str;
    
    dialog_data = (DialogData *) data;
    attachment = dialog_data->attachment;
    
    str = gtk_entry_get_text (dialog_data->file_name_entry);
    camel_mime_part_set_filename (attachment->body, str);
    
    str = gtk_entry_get_text (dialog_data->description_entry);
    camel_mime_part_set_description (attachment->body, str);
    
    str = gtk_entry_get_text (dialog_data->mime_type_entry);
    camel_mime_part_set_content_type (attachment->body, str);
    
    camel_data_wrapper_set_mime_type(camel_medium_get_content_object(CAMEL_MEDIUM (attachment->body)), str);
    
    switch (gtk_toggle_button_get_active (dialog_data->disposition_checkbox)) {
    case 0:
        camel_mime_part_set_disposition (attachment->body, "attachment");
        break;
    case 1:
        camel_mime_part_set_disposition (attachment->body, "inline");
        break;
    default:
        /* Hmmmm? */
        break;
    }
    
    changed (attachment);
    close_cb (widget, data);
}

static void
file_name_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, gpointer data)
{
    DialogData *dialog_data;
    
    dialog_data = (DialogData *) data;
    update_mime_type (dialog_data);
}


void
e_msg_composer_attachment_edit (EMsgComposerAttachment *attachment, GtkWidget *parent)
{
    CamelContentType *content_type;
    const char *disposition;
    DialogData *dialog_data;
    GladeXML *editor_gui;
    char *type;
    
    g_return_if_fail (attachment != NULL);
    g_return_if_fail (E_IS_MSG_COMPOSER_ATTACHMENT (attachment));
    
    if (attachment->editor_gui != NULL) {
        GtkWidget *window;
        
        window = glade_xml_get_widget (attachment->editor_gui,
                           "dialog");
        gdk_window_show (window->window);
        return;
    }
    
    editor_gui = glade_xml_new (E_GLADEDIR "/e-msg-composer-attachment.glade",
                    NULL, NULL);
    if (editor_gui == NULL) {
        g_warning ("Cannot load `e-msg-composer-attachment.glade'");
        return;
    }
    
    attachment->editor_gui = editor_gui;
    
    gtk_window_set_transient_for
        (GTK_WINDOW (glade_xml_get_widget (editor_gui, "dialog")),
         GTK_WINDOW (gtk_widget_get_toplevel (parent)));
    
    dialog_data = g_new (DialogData, 1);
    g_object_ref (attachment);
    dialog_data->attachment = attachment;
    dialog_data->dialog = glade_xml_get_widget (editor_gui, "dialog");
    dialog_data->file_name_entry = GTK_ENTRY (
        glade_xml_get_widget (editor_gui, "file_name_entry"));
    dialog_data->description_entry = GTK_ENTRY (
        glade_xml_get_widget (editor_gui, "description_entry"));
    dialog_data->mime_type_entry = GTK_ENTRY (
        glade_xml_get_widget (editor_gui, "mime_type_entry"));
    dialog_data->disposition_checkbox = GTK_TOGGLE_BUTTON (
        glade_xml_get_widget (editor_gui, "disposition_checkbox"));
    
    set_entry (editor_gui, "file_name_entry",
           camel_mime_part_get_filename (attachment->body));
    set_entry (editor_gui, "description_entry",
           camel_mime_part_get_description (attachment->body));
    content_type = camel_mime_part_get_content_type (attachment->body);
    type = header_content_type_simple (content_type);
    set_entry (editor_gui, "mime_type_entry", type);
    g_free (type);
    
    disposition = camel_mime_part_get_disposition (attachment->body);
    gtk_toggle_button_set_active (dialog_data->disposition_checkbox,
                      disposition && !g_ascii_strcasecmp (disposition, "inline"));
    
    connect_widget (editor_gui, "ok_button", "clicked", (GCallback)ok_cb, dialog_data);
    connect_widget (editor_gui, "close_button", "clicked", (GCallback)close_cb, dialog_data);
    
    connect_widget (editor_gui, "file_name_entry", "focus_out_event",
            (GCallback)file_name_focus_out_cb, dialog_data);
    
#warning "signal connect while alive"   
    /* make sure that when the composer gets hidden/closed that our windows also close */
    parent = gtk_widget_get_toplevel (parent);
    gtk_signal_connect_while_alive (GTK_OBJECT (parent), "destroy", (GCallback)close_cb, dialog_data,
                    GTK_OBJECT (dialog_data->dialog));
    gtk_signal_connect_while_alive (GTK_OBJECT (parent), "hide", (GCallback)close_cb, dialog_data,
                    GTK_OBJECT (dialog_data->dialog));
}