Files
evolution/composer/e-msg-composer-attachment.c
Not Zed 234540a383 Fix bonobo object setup.
2002-11-08  Not Zed  <NotZed@Ximian.com>

        * listener.c: Fix bonobo object setup.

        * e-msg-composer-hdrs.c: Remove oaf stuff, fix destroy to be
        multi-call safe.

        * e-msg-composer-attachment.c (e_msg_composer_attachment_new):
        Remove utf8 from locale conversions, & glib api changes.
        (ok_cb): "

        * e-msg-composer-attachment-bar.c (add_from_file): gtk message
        dialog.
        (pixbuf_for_mime_type): glib,gnome-vfs api changes.
        (init): Estimate the icon_height based on the pango font
        description size, this is probably not correct.
        (properties_cb):
        (remove_cb): Changed signature for gnomeui callbacks.
        (popup_icon_context_menu):
        (popup_context_menu): popup_menu api change
        (destroy): Protect from multiple calls.
        (e_msg_composer_attachment_bar_new): Remove push/pop
        visual/colormap stuff.

        * composer-marshal.list: The list of marshallers used by the
        composer.

        * Makefile.am (composer-marshal.h): Added composer marshal builder.

        * evolution-composer.c (class_init): bonobo object epv setup change.
        (factory_fn): Fix changes to factory callback.

        * e-icon-list.c: (icon_get_height): Use bounding box to calc
        height.
        (icon_event): drop gtk_selection_extended.
        (e_icon_list_remove): "
        (select_icon):
        (unselect_icon): "

        * e-msg-composer.c (autosave_save_draft): Dup fd rather than poke
        camel_stream_fd's data.  Also, use camel_stream_close() rather
        than flush.
        (autosave_manager_query_load_orphans): Port to gtk dialog.
        (autosave_query_cb): Removed, redundant.
        (save): Port to gtkdialog.
        (prepare_engine): Fix bonobo-object-client code.
        (get_file_content): gtk dialog
        (do_exit): gtk dialog.
        (setup_signatures_menu): dump gtkutf8 stuff.
        (marshal_NONE__NONE_INT): What WAS jeff thinking?  Removed :)
        (class_init): g object setup.
        (e_msg_composer_get_type): "
        (create_composer): remove bonobo_window_construct, use create
        property instead.  g_signal stuff.  bonobo stuff.
        (is_special_header): Use ascii_strncasecmp
        (e_msg_composer_set_pending_body): gtk->g_object_get/set_data.
        (e_msg_composer_set_body): use ascii_strncasecmp
        (e_msg_composer_add_inline_image_from_mime_part): make cid const.
        (autosave_manager_register):
        (autosave_manager_unregister): Use g_path_get_basename() & account
        for differences.
        (composer_shutdown): rename to finalise/etc.
        (class_init): Use object:finalize instead of shutdown.
        (e_msg_composer_set_body): Use _() rather than U_().
        (build_message): gtk dialog.

2002-11-06  Not Zed  <NotZed@Ximian.com>

        * e-msg-composer-attachment-bar.c (pixbuf_for_mime_type): Add
        error return to gdk_pixbuf_new_from_file().

svn path=/trunk/; revision=18659
2002-11-08 06:49:14 +00:00

493 lines
13 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* -*- 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 <gtk/gtknotebook.h>
#include <gtk/gtktogglebutton.h>
#include <camel/camel.h>
#include <gal/widgets/e-unicode.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include "e-msg-composer.h"
#include "e-msg-composer-attachment.h"
enum {
CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static GtkObjectClass *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 (msg_composer_attachment != NULL);
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. */
struct _DialogData {
GtkWidget *dialog;
GtkEntry *file_name_entry;
GtkEntry *description_entry;
GtkEntry *mime_type_entry;
GtkToggleButton *disposition_checkbox;
EMsgComposerAttachment *attachment;
};
typedef struct _DialogData 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)
{
gchar *mime_type;
const gchar *file_name;
if (!data->attachment->guessed_type)
return;
file_name = gtk_entry_get_text(data->file_name_entry);
#warning "do we need to create file uri for gnome-vfs-get-mime-type"
mime_type = gnome_vfs_get_mime_type (file_name);
if (mime_type) {
gtk_entry_set_text (data->mime_type_entry, mime_type);
g_free(mime_type);
}
}
static void
set_entry (GladeXML *xml,
const gchar *widget_name,
const gchar *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 gchar *name,
const gchar *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;
/* need to unref dialog? */
gtk_widget_destroy (dialog_data->dialog);
g_object_unref(attachment->editor_gui);
attachment->editor_gui = NULL;
destroy_dialog_data (dialog_data);
}
static void
ok_cb (GtkWidget *widget, gpointer data)
{
DialogData *dialog_data;
EMsgComposerAttachment *attachment;
const gchar *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)
{
DialogData *dialog_data;
GladeXML *editor_gui;
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);
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"));
if (attachment != NULL) {
CamelContentType *content_type;
const char *disposition;
char *type;
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));
}