diff options
Diffstat (limited to 'widgets/misc/e-attachment-bar.c')
-rw-r--r-- | widgets/misc/e-attachment-bar.c | 1982 |
1 files changed, 1404 insertions, 578 deletions
diff --git a/widgets/misc/e-attachment-bar.c b/widgets/misc/e-attachment-bar.c index 316aa85a0e..4608d77b9d 100644 --- a/widgets/misc/e-attachment-bar.c +++ b/widgets/misc/e-attachment-bar.c @@ -43,6 +43,7 @@ #include "e-attachment.h" #include "e-attachment-bar.h" +#include "e-mime-part-utils.h" #include <libedataserver/e-data-server-util.h> @@ -56,11 +57,13 @@ #include <camel/camel-mime-filter-bestenc.h> #include <camel/camel-mime-part.h> -#include "e-util/e-util.h" +#include "e-util/e-binding.h" +#include "e-util/e-error.h" #include "e-util/e-gui-utils.h" #include "e-util/e-icon-factory.h" -#include "e-util/e-error.h" #include "e-util/e-mktemp.h" +#include "e-util/e-util.h" +#include "e-util/gconf-bridge.h" #define ICON_WIDTH 64 #define ICON_SEPARATORS " /-_" @@ -70,84 +73,418 @@ #define ICON_BORDER 2 #define ICON_TEXT_SPACING 2 - -static GnomeIconListClass *parent_class = NULL; +#define E_ATTACHMENT_BAR_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), E_TYPE_ATTACHMENT_BAR, EAttachmentBarPrivate)) struct _EAttachmentBarPrivate { - GtkWidget *attach; /* attachment file dialogue, if active */ - gboolean batch_unref; GPtrArray *attachments; + gchar *current_folder; char *path; + + GtkUIManager *ui_manager; + GtkActionGroup *standard_actions; + GtkActionGroup *editable_actions; + GtkActionGroup *open_actions; + guint merge_id; + + gchar *background_filename; + gchar *background_options; + + guint editable : 1; +}; + +enum { + PROP_0, + PROP_BACKGROUND_FILENAME, + PROP_BACKGROUND_OPTIONS, + PROP_CURRENT_FOLDER, + PROP_EDITABLE, + PROP_UI_MANAGER }; - enum { CHANGED, + UPDATE_ACTIONS, LAST_SIGNAL }; -static guint signals[LAST_SIGNAL] = { 0 }; +static gpointer parent_class; +static guint signals[LAST_SIGNAL]; + +static const gchar *ui = +"<ui>" +" <popup name='attachment-popup'>" +" <menuitem action='save-as'/>" +" <menuitem action='set-background'/>" +" <menuitem action='remove'/>" +" <menuitem action='properties'/>" +" <placeholder name='custom-actions'/>" +" <separator/>" +" <menuitem action='add'/>" +" <separator/>" +" <placeholder name='open-actions'/>" +" </popup>" +"</ui>"; - -static void update (EAttachmentBar *bar); +static void +action_add_cb (GtkAction *action, + EAttachmentBar *attachment_bar) +{ + GtkWidget *dialog; + GtkWidget *option; + GSList *uris, *iter; + const gchar *disposition; + gboolean active; + gpointer parent; + gint response; + + parent = gtk_widget_get_toplevel (GTK_WIDGET (attachment_bar)); + parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL; + + dialog = gtk_file_chooser_dialog_new ( + _("Insert Attachment"), parent, + GTK_FILE_CHOOSER_ACTION_OPEN, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + _("A_ttach"), GTK_RESPONSE_OK, + NULL); + + gtk_dialog_set_default_response ( + GTK_DIALOG (dialog), GTK_RESPONSE_OK); + gtk_file_chooser_set_local_only ( + GTK_FILE_CHOOSER (dialog), FALSE); + gtk_file_chooser_set_select_multiple ( + GTK_FILE_CHOOSER (dialog), TRUE); + gtk_window_set_icon_name ( + GTK_WINDOW (dialog), "mail-attachment"); + + option = gtk_check_button_new_with_mnemonic ( + _("_Suggest automatic display of attachment")); + gtk_file_chooser_set_extra_widget ( + GTK_FILE_CHOOSER (dialog), option); + gtk_widget_show (option); + + response = e_attachment_bar_file_chooser_dialog_run ( + attachment_bar, dialog); + + if (response != GTK_RESPONSE_OK) + goto exit; + + uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog)); + active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (option)); + disposition = active ? "inline" : "attachment"; + + for (iter = uris; iter != NULL; iter = iter->next) { + CamelURL *url; + + url = camel_url_new (iter->data, NULL); + if (url == NULL) + continue; -/* Attachment handling functions. */ + /* XXX Do we really need two different attach functions? */ + if (g_ascii_strcasecmp (url->protocol, "file") == 0) + e_attachment_bar_attach ( + attachment_bar, url->path, disposition); + else + e_attachment_bar_attach_remote_file ( + attachment_bar, iter->data, disposition); + + camel_url_free (url); + } + + g_slist_foreach (uris, (GFunc) g_free, NULL); + g_slist_free (uris); + +exit: + gtk_widget_destroy (dialog); +} static void -attachment_destroy (EAttachmentBar *bar, EAttachment *attachment) +action_properties_cb (GtkAction *action, + EAttachmentBar *attachment_bar) { - if (bar->priv->batch_unref) - return; + GnomeIconList *icon_list; + GPtrArray *array; + GList *selection; + gpointer parent; - if (g_ptr_array_remove (bar->priv->attachments, attachment)) { - update (bar); - g_signal_emit (bar, signals[CHANGED], 0); + array = attachment_bar->priv->attachments; + + icon_list = GNOME_ICON_LIST (attachment_bar); + selection = gnome_icon_list_get_selection (icon_list); + g_return_if_fail (selection != NULL); + + parent = gtk_widget_get_toplevel (GTK_WIDGET (icon_list)); + parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL; + + while (selection != NULL) { + gint index = GPOINTER_TO_INT (selection->data); + EAttachment *attachment; + + selection = g_list_next (selection); + + if (index >= array->len) + continue; + + attachment = array->pdata[index]; + e_attachment_edit (attachment, parent); } } static void -attachment_changed_cb (EAttachment *attachment, - gpointer data) +action_recent_cb (GtkAction *action, + EAttachmentBar *attachment_bar) { - update (E_ATTACHMENT_BAR (data)); + GtkRecentChooser *chooser; + GFile *file; + gchar *uri; + + chooser = GTK_RECENT_CHOOSER (action); + + /* Wish: gtk_recent_chooser_get_current_file() */ + uri = gtk_recent_chooser_get_current_uri (chooser); + file = g_file_new_for_uri (uri); + g_free (uri); + + if (g_file_is_native (file)) + e_attachment_bar_attach ( + E_ATTACHMENT_BAR (attachment_bar), + g_file_get_path (file), "attachment"); + else + e_attachment_bar_attach_remote_file ( + E_ATTACHMENT_BAR (attachment_bar), + g_file_get_uri (file), "attachment"); + + g_object_unref (file); } static void -add_common (EAttachmentBar *bar, EAttachment *attachment) +action_remove_cb (GtkAction *action, + EAttachmentBar *attachment_bar) { - g_return_if_fail (attachment != NULL); + GnomeIconList *icon_list; + GPtrArray *array; + GList *selection; + GList *trash = NULL; - g_ptr_array_add (bar->priv->attachments, attachment); - g_object_weak_ref ((GObject *) attachment, (GWeakNotify) attachment_destroy, bar); - g_signal_connect (attachment, "changed", G_CALLBACK (attachment_changed_cb), bar); + array = attachment_bar->priv->attachments; - update (bar); + icon_list = GNOME_ICON_LIST (attachment_bar); + selection = gnome_icon_list_get_selection (icon_list); + g_return_if_fail (selection != NULL); - g_signal_emit (bar, signals[CHANGED], 0); + while (selection != NULL) { + gint index = GPOINTER_TO_INT (selection->data); + + selection = g_list_next (selection); + + if (index >= array->len) + continue; + + /* We can't unref the attachment here because that may + * change the selection and invalidate the list we are + * iterating over. So move it to a trash list instead. */ + trash = g_list_prepend (trash, array->pdata[index]); + array->pdata[index] = NULL; + } + + /* Compress the attachment array. */ + while (g_ptr_array_remove (array, NULL)); + + /* Take out the trash. */ + g_list_foreach (trash, (GFunc) g_object_unref, NULL); + g_list_free (trash); + + e_attachment_bar_refresh (attachment_bar); + + g_signal_emit (attachment_bar, signals[CHANGED], 0); } static void -add_from_mime_part (EAttachmentBar *bar, CamelMimePart *part) +action_save_as_cb (GtkAction *action, + EAttachmentBar *attachment_bar) { - add_common (bar, e_attachment_new_from_mime_part (part)); } static void -add_from_file (EAttachmentBar *bar, const char *file_name, const char *disposition) +action_set_background_cb (GtkAction *action, + EAttachmentBar *attachment_bar) { + GnomeIconList *icon_list; + CamelContentType *content_type; + CamelMimePart *mime_part; EAttachment *attachment; - CamelException ex; + GPtrArray *array; + GList *selection; + gchar *basename; + gchar *filename; + gchar *dirname; + GFile *file; + gint index; + GError *error = NULL; + + icon_list = GNOME_ICON_LIST (attachment_bar); + selection = gnome_icon_list_get_selection (icon_list); + g_return_if_fail (selection != NULL); + + array = attachment_bar->priv->attachments; + index = GPOINTER_TO_INT (selection->data); + attachment = E_ATTACHMENT (array->pdata[index]); + mime_part = e_attachment_get_mime_part (attachment); + g_return_if_fail (CAMEL_IS_MIME_PART (mime_part)); + + content_type = camel_mime_part_get_content_type (mime_part); + basename = g_strdup (camel_mime_part_get_filename (mime_part)); + + if (basename == NULL || basename == '\0') { + g_free (basename); + basename = g_strdup_printf ( + _("untitled_image.%s"), + content_type->subtype); + } - camel_exception_init (&ex); + dirname = g_build_filename ( + g_get_home_dir (), ".gnome2", "wallpapers", NULL); - if ((attachment = e_attachment_new (file_name, disposition, &ex))) { - add_common (bar, attachment); - } else { - /* FIXME: Avoid using error from mailer */ - e_error_run ((GtkWindow *) gtk_widget_get_toplevel ((GtkWidget *) bar), "mail-composer:no-attach", - file_name, camel_exception_get_description (&ex), NULL); - camel_exception_clear (&ex); + index = 0; + filename = g_build_filename (dirname, basename, NULL); + + while (g_file_test (filename, G_FILE_TEST_EXISTS)) { + gchar *temp; + gchar *ext; + + ext = strrchr (filename, '.'); + if (ext != NULL) + *ext++ = '\0'; + + if (ext == NULL) + temp = g_strdup_printf ( + "%s (%d)", basename, index++); + else + temp = g_strdup_printf ( + "%s (%d).%s", basename, index++, ext); + + g_free (basename); + g_free (filename); + basename = temp; + + filename = g_build_filename (dirname, basename, NULL); + } + + g_free (basename); + g_free (dirname); + + file = g_file_new_for_path (filename); + + if (e_mime_part_utils_save_to_file (mime_part, file, &error)) { + const gchar *background_filename; + const gchar *background_options; + + background_filename = + e_attachment_bar_get_background_filename ( + attachment_bar); + background_options = + e_attachment_bar_get_background_options ( + attachment_bar); + + if (g_strcmp0 (background_filename, filename) == 0) + e_attachment_bar_set_background_filename ( + attachment_bar, NULL); + + e_attachment_bar_set_background_filename ( + attachment_bar, filename); + + if (g_strcmp0 (background_options, "none") == 0) + e_attachment_bar_set_background_options ( + attachment_bar, "wallpaper"); + } + + g_object_unref (file); + g_free (filename); + + if (error != NULL) { + g_warning ("%s", error->message); + g_error_free (error); + } +} + +static GtkActionEntry standard_entries[] = { + + { "save-as", + GTK_STOCK_SAVE_AS, + NULL, + NULL, + NULL, /* XXX Add a tooltip! */ + G_CALLBACK (action_save_as_cb) }, + + { "set-background", + NULL, + N_("Set as _Background"), + NULL, + NULL, /* XXX Add a tooltip! */ + G_CALLBACK (action_set_background_cb) } +}; + +static GtkActionEntry editable_entries[] = { + + { "add", + GTK_STOCK_ADD, + N_("A_dd Attachment..."), + NULL, + NULL, /* XXX Add a tooltip! */ + G_CALLBACK (action_add_cb) }, + + { "properties", + GTK_STOCK_PROPERTIES, + NULL, + NULL, + NULL, /* XXX Add a tooltip! */ + G_CALLBACK (action_properties_cb) }, + + { "remove", + GTK_STOCK_REMOVE, + NULL, + NULL, + NULL, /* XXX Add a tooltip! */ + G_CALLBACK (action_remove_cb) } +}; + +static void +attachment_bar_show_popup_menu (EAttachmentBar *attachment_bar, + GdkEventButton *event) +{ + GtkUIManager *ui_manager; + GtkWidget *menu; + + ui_manager = e_attachment_bar_get_ui_manager (attachment_bar); + menu = gtk_ui_manager_get_widget (ui_manager, "/attachment-popup"); + g_return_if_fail (GTK_IS_MENU (menu)); + + e_attachment_bar_update_actions (attachment_bar); + + if (event != NULL) + gtk_menu_popup ( + GTK_MENU (menu), NULL, NULL, NULL, NULL, + event->button, event->time); + else + gtk_menu_popup ( + GTK_MENU (menu), NULL, NULL, NULL, NULL, + 0, gtk_get_current_event_time ()); +} + +/* Attachment handling functions. */ + +static void +attachment_destroy (EAttachmentBar *bar, + EAttachment *attachment) +{ + if (bar->priv->batch_unref) + return; + + if (g_ptr_array_remove (bar->priv->attachments, attachment)) { + e_attachment_bar_refresh (bar); + g_signal_emit (bar, signals[CHANGED], 0); } } @@ -161,6 +498,7 @@ get_system_thumbnail (EAttachment *attachment, CamelContentType *content_type) { GdkPixbuf *pixbuf = NULL; #ifdef HAVE_LIBGNOMEUI_GNOME_THUMBNAIL_H + CamelMimePart *mime_part; struct stat file_stat; char *file_uri = NULL; gboolean is_tmp = FALSE; @@ -168,9 +506,11 @@ get_system_thumbnail (EAttachment *attachment, CamelContentType *content_type) if (!attachment || !attachment->is_available_local) return NULL; + mime_part = e_attachment_get_mime_part (attachment); + if (attachment->store_uri && g_str_has_prefix (attachment->store_uri, "file://")) file_uri = attachment->store_uri; - else if (attachment->body) { + else if (mime_part != NULL) { /* save part to the temp directory */ char *tmp_file; @@ -182,7 +522,7 @@ get_system_thumbnail (EAttachment *attachment, CamelContentType *content_type) char *mfilename = NULL; const char * filename; - filename = camel_mime_part_get_filename (attachment->body); + filename = camel_mime_part_get_filename (mime_part); if (filename == NULL) filename = "unknown"; else { @@ -202,7 +542,7 @@ get_system_thumbnail (EAttachment *attachment, CamelContentType *content_type) if (stream) { CamelDataWrapper *content; - content = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body)); + content = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part)); if (camel_data_wrapper_decode_to_stream (content, stream) == -1 || camel_stream_flush (stream) == -1) { @@ -298,17 +638,24 @@ scale_pixbuf (GdkPixbuf *pixbuf) /* Icon list contents handling. */ static void -calculate_height_width(EAttachmentBar *bar, int *new_width, int *new_height) +calculate_height_width (EAttachmentBar *bar, + gint *new_width, + gint *new_height) { int width, height, icon_width; PangoFontMetrics *metrics; PangoContext *context; - context = gtk_widget_get_pango_context ((GtkWidget *) bar); - metrics = pango_context_get_metrics (context, ((GtkWidget *) bar)->style->font_desc, pango_context_get_language (context)); - width = PANGO_PIXELS (pango_font_metrics_get_approximate_char_width (metrics)) * 15; + context = gtk_widget_get_pango_context (GTK_WIDGET (bar)); + metrics = pango_context_get_metrics ( + context, GTK_WIDGET (bar)->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; + 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; @@ -316,21 +663,23 @@ calculate_height_width(EAttachmentBar *bar, int *new_width, int *new_height) *new_width = MAX (icon_width, width); if (new_height) - *new_height = ICON_WIDTH + ICON_SPACING + ICON_BORDER + ICON_TEXT_SPACING + height; - - return; + *new_height = ICON_WIDTH + ICON_SPACING + + ICON_BORDER + ICON_TEXT_SPACING + height; } void e_attachment_bar_create_attachment_cache (EAttachment *attachment) { - CamelContentType *content_type; + CamelMimePart *mime_part; + + g_return_if_fail (E_IS_ATTACHMENT (attachment)); - if (!attachment->body) + mime_part = e_attachment_get_mime_part (attachment); + if (mime_part == NULL) return; - content_type = camel_mime_part_get_content_type (attachment->body); + content_type = camel_mime_part_get_content_type (mime_part); if (camel_content_type_is(content_type, "image", "*")) { CamelDataWrapper *wrapper; @@ -339,7 +688,7 @@ e_attachment_bar_create_attachment_cache (EAttachment *attachment) gboolean error = TRUE; GdkPixbuf *pixbuf; - wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body)); + wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part)); mstream = (CamelStreamMem *) camel_stream_mem_new (); camel_data_wrapper_decode_to_stream (wrapper, (CamelStream *) mstream); @@ -350,12 +699,15 @@ e_attachment_bar_create_attachment_cache (EAttachment *attachment) gdk_pixbuf_loader_close (loader, NULL); if (!error) { + /* The loader owns the reference. */ pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); - attachment->pixbuf_cache = scale_pixbuf (pixbuf); - pixbuf = attachment->pixbuf_cache; - g_object_ref(pixbuf); + + /* This returns a new GdkPixbuf. */ + pixbuf = scale_pixbuf (pixbuf); + e_attachment_set_thumbnail (attachment, pixbuf); + g_object_unref (pixbuf); } else { - attachment->pixbuf_cache = NULL; + e_attachment_set_thumbnail (attachment, NULL); g_warning ("GdkPixbufLoader Error"); } @@ -366,244 +718,40 @@ e_attachment_bar_create_attachment_cache (EAttachment *attachment) } static void -update (EAttachmentBar *bar) -{ - struct _EAttachmentBarPrivate *priv; - GnomeIconList *icon_list; - int bar_width, bar_height; - int i; - - 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 (i = 0; i < priv->attachments->len; i++) { - EAttachment *attachment; - CamelContentType *content_type; - char *size_string, *label; - GdkPixbuf *pixbuf = NULL; - const char *desc; - - attachment = priv->attachments->pdata[i]; - - if (!attachment->is_available_local || !attachment->body) { - if ((pixbuf = e_icon_factory_get_icon("mail-attachment", E_ICON_SIZE_DIALOG))) { - attachment->index = gnome_icon_list_append_pixbuf (icon_list, pixbuf, NULL, ""); - g_object_unref (pixbuf); - } - continue; - } - - content_type = camel_mime_part_get_content_type (attachment->body); - /* Get the image out of the attachment - and create a thumbnail for it */ - if ((pixbuf = attachment->pixbuf_cache)) { - g_object_ref(pixbuf); - } else if (camel_content_type_is(content_type, "image", "*")) { - CamelDataWrapper *wrapper; - CamelStreamMem *mstream; - GdkPixbufLoader *loader; - gboolean error = TRUE; - - wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body)); - mstream = (CamelStreamMem *) camel_stream_mem_new (); - - camel_data_wrapper_decode_to_stream (wrapper, (CamelStream *) mstream); - - /* Stream image into pixbuf loader */ - loader = gdk_pixbuf_loader_new (); - error = !gdk_pixbuf_loader_write (loader, mstream->buffer->data, mstream->buffer->len, NULL); - gdk_pixbuf_loader_close (loader, NULL); - - if (!error) { - pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); - attachment->pixbuf_cache = scale_pixbuf (pixbuf); - pixbuf = attachment->pixbuf_cache; - g_object_ref (pixbuf); - } else { - pixbuf = NULL; - g_warning ("GdkPixbufLoader Error"); - } - - /* Destroy everything */ - g_object_unref (loader); - camel_object_unref (mstream); - } else if (!bar->expand && (pixbuf = get_system_thumbnail (attachment, content_type))) { - attachment->pixbuf_cache = scale_pixbuf (pixbuf); - pixbuf = attachment->pixbuf_cache; - g_object_ref (pixbuf); - } - - desc = camel_mime_part_get_description (attachment->body); - if (!desc || *desc == '\0') { - if (attachment->file_name) - desc = attachment->file_name; - else - desc = camel_mime_part_get_filename (attachment->body); - } - - if (!desc) - desc = _("attachment"); - - if (attachment->size && (size_string = g_format_size_for_display (attachment->size))) { - label = g_strdup_printf ("%s (%s)", desc, size_string); - g_free (size_string); - } else - label = g_strdup (desc); - - if (pixbuf == NULL) { - char *mime_type; - - mime_type = camel_content_type_simple (content_type); - pixbuf = e_icon_for_mime_type (mime_type, 48); - if (pixbuf == NULL) { - g_warning("cannot find icon for mime type %s (installation problem?)", mime_type); - pixbuf = e_icon_factory_get_icon("mail-attachment", E_ICON_SIZE_DIALOG); - } - g_free (mime_type); - - /* remember this picture and use it later again */ - if (pixbuf) - attachment->pixbuf_cache = g_object_ref (pixbuf); - } - - if (pixbuf) { - GdkPixbuf *pixbuf_orig = pixbuf; - pixbuf = gdk_pixbuf_add_alpha (pixbuf_orig, TRUE, 255, 255, 255); - - /* gdk_pixbuf_add_alpha returns a newly allocated pixbuf, - free the original one. - */ - g_object_unref (pixbuf_orig); - - /* In case of a attachment bar, in a signed/encrypted part, display the status as a emblem*/ - if (attachment->sign) { - /* Show the signature status at the right-bottom.*/ - GdkPixbuf *sign = NULL; - int x, y; - - if (attachment->sign == CAMEL_CIPHER_VALIDITY_SIGN_BAD) - sign = e_icon_factory_get_icon ("stock_signature-bad", E_ICON_SIZE_MENU); - else if (attachment->sign == CAMEL_CIPHER_VALIDITY_SIGN_GOOD) - sign = e_icon_factory_get_icon ("stock_signature-ok", E_ICON_SIZE_MENU); - else - sign = e_icon_factory_get_icon ("stock_signature", E_ICON_SIZE_MENU); - - x = gdk_pixbuf_get_width (pixbuf) - 17; - y = gdk_pixbuf_get_height (pixbuf) - 17; - - gdk_pixbuf_copy_area (sign, 0, 0, 16, 16, pixbuf, x, y); - g_object_unref (sign); - } - - if (attachment->encrypt) { - /* Show the encryption status at the top left.*/ - GdkPixbuf *encrypt = e_icon_factory_get_icon ("stock_lock-ok", E_ICON_SIZE_MENU); - - gdk_pixbuf_copy_area (encrypt, 0, 0, 16, 16, pixbuf, 1, 1); - g_object_unref (encrypt); - } - - gnome_icon_list_append_pixbuf (icon_list, pixbuf, NULL, label); - g_object_unref (pixbuf); - } - - g_free (label); - } - - gnome_icon_list_thaw (icon_list); - - /* Resize */ - if (bar->expand) { - gtk_widget_get_size_request ((GtkWidget *) bar, &bar_width, &bar_height); - - if (bar->priv->attachments->len) { - int per_col, rows, height, width; - - calculate_height_width(bar, &width, &height); - per_col = bar_width / width; - per_col = (per_col ? per_col : 1); - rows = (bar->priv->attachments->len + per_col -1) / per_col; - gtk_widget_set_size_request ((GtkWidget *) bar, bar_width, rows * height); - } - } -} - -static void update_remote_file (EAttachment *attachment, EAttachmentBar *bar) { GnomeIconList *icon_list; GnomeIconTextItem *item; + const gchar *filename; char *msg, *base; if (attachment->percentage == -1) { - update (bar); + e_attachment_bar_refresh (bar); return; } - base = g_path_get_basename(attachment->file_name); - msg = g_strdup_printf("%s (%d%%)", base, attachment->percentage); - g_free(base); + filename = e_attachment_get_filename (attachment); + base = g_path_get_basename (filename); + msg = g_strdup_printf ("%s (%d%%)", base, attachment->percentage); + g_free (base); icon_list = GNOME_ICON_LIST (bar); gnome_icon_list_freeze (icon_list); - item = gnome_icon_list_get_icon_text_item (icon_list, attachment->index); + item = gnome_icon_list_get_icon_text_item ( + icon_list, attachment->index); if (!item->is_text_allocated) g_free (item->text); - gnome_icon_text_item_configure (item, item->x, item->y, item->width, item->fontname, msg, item->is_editable, TRUE); + gnome_icon_text_item_configure ( + item, item->x, item->y, item->width, + item->fontname, msg, item->is_editable, TRUE); gnome_icon_list_thaw (icon_list); } void -e_attachment_bar_remove_selected (EAttachmentBar *bar) -{ - struct _EAttachmentBarPrivate *priv; - EAttachment *attachment; - int id, left, nrem = 0; - GList *items; - GPtrArray *temp_arr; - - g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - - priv = bar->priv; - - if (!(items = gnome_icon_list_get_selection ((GnomeIconList *) bar))) - return; - - temp_arr = g_ptr_array_new (); - while (items != NULL) { - if ((id = GPOINTER_TO_INT (items->data) - nrem) < priv->attachments->len) { - attachment = E_ATTACHMENT(g_ptr_array_index (priv->attachments, id)); - g_ptr_array_add (temp_arr, (gpointer)attachment); - g_ptr_array_remove_index (priv->attachments, id); - nrem++; - } - - items = items->next; - } - - g_ptr_array_foreach (temp_arr, (GFunc)g_object_unref, NULL); - g_ptr_array_free (temp_arr, TRUE); - - update (bar); - - g_signal_emit (bar, signals[CHANGED], 0); - - id++; - - if ((left = gnome_icon_list_get_num_icons ((GnomeIconList *) bar)) > 0) - gnome_icon_list_focus_icon ((GnomeIconList *) bar, left > id ? id : left - 1); -} - -void e_attachment_bar_set_width(EAttachmentBar *bar, int bar_width) { int per_col, rows, height, width; @@ -615,37 +763,6 @@ e_attachment_bar_set_width(EAttachmentBar *bar, int bar_width) gtk_widget_set_size_request ((GtkWidget *)bar, bar_width, rows * height); } -void -e_attachment_bar_edit_selected (EAttachmentBar *bar) -{ - struct _EAttachmentBarPrivate *priv; - EAttachment *attachment; - GList *items; - int id; - - g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - - priv = bar->priv; - - items = gnome_icon_list_get_selection ((GnomeIconList *) bar); - while (items != NULL) { - if ((id = GPOINTER_TO_INT (items->data)) < priv->attachments->len) { - attachment = priv->attachments->pdata[id]; - e_attachment_edit (attachment, GTK_WIDGET (bar)); - } - - items = items->next; - } -} - -GtkWidget ** -e_attachment_bar_get_selector(EAttachmentBar *bar) -{ - g_return_val_if_fail (E_IS_ATTACHMENT_BAR (bar), NULL); - - return &bar->priv->attach; -} - /** * e_attachment_bar_get_selected: * @bar: an #EAttachmentBar object @@ -656,7 +773,7 @@ e_attachment_bar_get_selector(EAttachmentBar *bar) GSList * e_attachment_bar_get_selected (EAttachmentBar *bar) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; GSList *attachments = NULL; EAttachment *attachment; GList *items; @@ -696,7 +813,7 @@ e_attachment_bar_get_selected (EAttachmentBar *bar) GSList * e_attachment_bar_get_attachment (EAttachmentBar *bar, int id) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; EAttachment *attachment; GSList *attachments; @@ -724,7 +841,7 @@ e_attachment_bar_get_attachment (EAttachmentBar *bar, int id) GSList * e_attachment_bar_get_all_attachments (EAttachmentBar *bar) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; GSList *attachments = NULL; EAttachment *attachment; int i; @@ -748,7 +865,7 @@ e_attachment_bar_get_all_attachments (EAttachmentBar *bar) GSList * e_attachment_bar_get_parts (EAttachmentBar *bar) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; EAttachment *attachment; GSList *parts = NULL; int i; @@ -758,50 +875,22 @@ e_attachment_bar_get_parts (EAttachmentBar *bar) priv = bar->priv; for (i = 0; i < priv->attachments->len; i++) { + CamelMimePart *mime_part; + attachment = priv->attachments->pdata[i]; + mime_part = e_attachment_get_mime_part (attachment); + if (attachment->is_available_local) - parts = g_slist_prepend (parts, attachment->body); + parts = g_slist_prepend (parts, mime_part); } return parts; } -/* GtkObject methods. */ - -static void -destroy (GtkObject *object) -{ - EAttachmentBar *bar = (EAttachmentBar *) object; - struct _EAttachmentBarPrivate *priv = bar->priv; - EAttachment *attachment; - int i; - - if ((priv = bar->priv)) { - priv->batch_unref = TRUE; - for (i = 0; i < priv->attachments->len; i++) { - attachment = priv->attachments->pdata[i]; - g_object_weak_unref ((GObject *) attachment, (GWeakNotify) attachment_destroy, bar); - g_object_unref (attachment); - } - g_ptr_array_free (priv->attachments, TRUE); - - if (priv->attach) - gtk_widget_destroy (priv->attach); - - if (priv->path) - g_free (priv->path); - - g_free (priv); - bar->priv = NULL; - } - - if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL) - (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); -} - static char * -temp_save_part (CamelMimePart *part, gboolean readonly) +temp_save_part (EAttachment *attachment, gboolean readonly) { + CamelMimePart *mime_part; const char *filename; char *tmpdir, *path, *mfilename = NULL, *utf8_mfilename = NULL; CamelStream *stream; @@ -810,7 +899,9 @@ temp_save_part (CamelMimePart *part, gboolean readonly) if (!(tmpdir = e_mkdtemp ("evolution-tmp-XXXXXX"))) return NULL; - if (!(filename = camel_mime_part_get_filename (part))) { + mime_part = e_attachment_get_mime_part (attachment); + + if (!(filename = camel_mime_part_get_filename (mime_part))) { /* This is the default filename used for temporary file creation */ filename = _("Unknown"); } else { @@ -825,7 +916,7 @@ temp_save_part (CamelMimePart *part, gboolean readonly) g_free (tmpdir); g_free (mfilename); - wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part)); + wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part)); if (readonly) stream = camel_stream_fs_new_with_name (path, O_RDWR|O_CREAT|O_TRUNC, 0444); else @@ -853,9 +944,344 @@ temp_save_part (CamelMimePart *part, gboolean readonly) } static void -eab_drag_data_get(EAttachmentBar *bar, GdkDragContext *drag, GtkSelectionData *data, guint info, guint time) +attachment_bar_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + switch (property_id) { + case PROP_BACKGROUND_FILENAME: + e_attachment_bar_set_background_filename ( + E_ATTACHMENT_BAR (object), + g_value_get_string (value)); + return; + + case PROP_BACKGROUND_OPTIONS: + e_attachment_bar_set_background_options ( + E_ATTACHMENT_BAR (object), + g_value_get_string (value)); + return; + + case PROP_CURRENT_FOLDER: + e_attachment_bar_set_current_folder ( + E_ATTACHMENT_BAR (object), + g_value_get_string (value)); + return; + + case PROP_EDITABLE: + e_attachment_bar_set_editable ( + E_ATTACHMENT_BAR (object), + g_value_get_boolean (value)); + return; + } + + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); +} + +static void +attachment_bar_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + switch (property_id) { + case PROP_BACKGROUND_FILENAME: + g_value_set_string ( + value, + e_attachment_bar_get_background_filename ( + E_ATTACHMENT_BAR (object))); + return; + + case PROP_BACKGROUND_OPTIONS: + g_value_set_string ( + value, + e_attachment_bar_get_background_options ( + E_ATTACHMENT_BAR (object))); + return; + + case PROP_CURRENT_FOLDER: + g_value_set_string ( + value, + e_attachment_bar_get_current_folder ( + E_ATTACHMENT_BAR (object))); + return; + + case PROP_EDITABLE: + g_value_set_boolean ( + value, + e_attachment_bar_get_editable ( + E_ATTACHMENT_BAR (object))); + return; + + case PROP_UI_MANAGER: + g_value_set_object ( + value, + e_attachment_bar_get_ui_manager ( + E_ATTACHMENT_BAR (object))); + return; + } + + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); +} + +static void +attachment_bar_dispose (GObject *object) +{ + EAttachmentBarPrivate *priv; + guint ii; + + priv = E_ATTACHMENT_BAR_GET_PRIVATE (object); + + priv->batch_unref = TRUE; + + for (ii = 0; ii < priv->attachments->len; ii++) { + EAttachment *attachment; + + attachment = priv->attachments->pdata[ii]; + g_object_weak_unref ( + G_OBJECT (attachment), (GWeakNotify) + attachment_destroy, object); + g_object_unref (attachment); + } + g_ptr_array_set_size (priv->attachments, 0); + + if (priv->ui_manager != NULL) { + g_object_unref (priv->ui_manager); + priv->ui_manager = NULL; + } + + if (priv->standard_actions != NULL) { + g_object_unref (priv->standard_actions); + priv->standard_actions = NULL; + } + + if (priv->editable_actions != NULL) { + g_object_unref (priv->editable_actions); + priv->editable_actions = NULL; + } + + if (priv->open_actions != NULL) { + g_object_unref (priv->open_actions); + priv->open_actions = NULL; + } + + /* Chain up to parent's dipose() method. */ + G_OBJECT_CLASS (parent_class)->dispose (object); +} + +static void +attachment_bar_finalize (GObject *object) +{ + EAttachmentBarPrivate *priv; + + priv = E_ATTACHMENT_BAR_GET_PRIVATE (object); + + g_ptr_array_free (priv->attachments, TRUE); + g_free (priv->current_folder); + g_free (priv->path); + + g_free (priv->background_filename); + g_free (priv->background_options); + + /* Chain up to parent's finalize() method. */ + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +attachment_bar_constructed (GObject *object) +{ + EAttachmentBarPrivate *priv; + GtkActionGroup *action_group; + GConfBridge *bridge; + const gchar *prop; + const gchar *key; + + priv = E_ATTACHMENT_BAR_GET_PRIVATE (object); + action_group = priv->editable_actions; + bridge = gconf_bridge_get (); + + e_mutual_binding_new ( + G_OBJECT (object), "editable", + G_OBJECT (action_group), "visible"); + + prop = "background-filename"; + key = "/desktop/gnome/background/picture_filename"; + gconf_bridge_bind_property (bridge, key, object, prop); + + prop = "background-options"; + key = "/desktop/gnome/background/picture_options"; + gconf_bridge_bind_property (bridge, key, object, prop); +} + +static gboolean +attachment_bar_event (GtkWidget *widget, + GdkEvent *event) { - struct _EAttachmentBarPrivate *priv = bar->priv; + EAttachment *attachment; + gboolean ret = FALSE; + gpointer parent; + CamelURL *url; + char *path; + GSList *p; + + if (event->type != GDK_2BUTTON_PRESS) + return FALSE; + + parent = gtk_widget_get_toplevel (widget); + parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL; + + p = e_attachment_bar_get_selected (E_ATTACHMENT_BAR (widget)); + /* check if has body already, remote files can take longer to fetch */ + if (p && p->next == NULL && e_attachment_get_mime_part (p->data) != NULL) { + attachment = p->data; + + /* Check if the file is stored already */ + if (!attachment->store_uri) { + path = temp_save_part (attachment, TRUE); + url = camel_url_new ("file://", NULL); + camel_url_set_path (url, path); + attachment->store_uri = camel_url_to_string (url, 0); + camel_url_free (url); + g_free (path); + } + + e_show_uri (parent, attachment->store_uri); + + ret = TRUE; + } + + g_slist_foreach (p, (GFunc) g_object_unref, NULL); + g_slist_free (p); + + return ret; +} + +static gboolean +attachment_bar_button_press_event (GtkWidget *widget, + GdkEventButton *event) +{ + GnomeIconList *icon_list; + GList *selected, *tmp; + int length, icon_number; + gboolean take_selected = FALSE; + + GtkTargetEntry drag_types[] = { + { "text/uri-list", 0, 0 }, + }; + + icon_list = GNOME_ICON_LIST (widget); + selected = gnome_icon_list_get_selection (icon_list); + length = g_list_length (selected); + + icon_number = gnome_icon_list_get_icon_at ( + icon_list, event->x, event->y); + if (icon_number < 0) { + /* When nothing is selected, deselect all */ + gnome_icon_list_unselect_all (icon_list); + length = 0; + selected = NULL; + } + + if (event->button == 1) { + /* If something is selected, then allow drag or else help to select */ + if (length) + gtk_drag_source_set ( + widget, GDK_BUTTON1_MASK, drag_types, + G_N_ELEMENTS (drag_types), GDK_ACTION_COPY); + else + gtk_drag_source_unset (widget); + goto exit; + } + + /* If not r-click dont progress any more.*/ + if (event->button != 3) + goto exit; + + /* When a r-click on something, if it is in the already selected list, consider a r-click of multiple things + * or deselect all and select only this for r-click + */ + if (icon_number >= 0) { + for (tmp = selected; tmp; tmp = tmp->next) { + if (GPOINTER_TO_INT (tmp->data) == icon_number) + take_selected = TRUE; + } + + if (!take_selected) { + gnome_icon_list_unselect_all (icon_list); + gnome_icon_list_select_icon (icon_list, icon_number); + } + } + + attachment_bar_show_popup_menu (E_ATTACHMENT_BAR (widget), event); + +exit: + /* Chain up to parent's button_press_event() method. */ + return GTK_WIDGET_CLASS (parent_class)-> + button_press_event (widget, event); +} + +static gboolean +attachment_bar_button_release_event (GtkWidget *widget, + GdkEventButton *event) +{ + GnomeIconList *icon_list; + GList *selected; + + GtkTargetEntry drag_types[] = { + { "text/uri-list", 0, 0 }, + }; + + if (event->button != 1) + goto exit; + + icon_list = GNOME_ICON_LIST (widget); + selected = gnome_icon_list_get_selection (icon_list); + + if (selected != NULL) + gtk_drag_source_set ( + widget, GDK_BUTTON1_MASK, drag_types, + G_N_ELEMENTS (drag_types), GDK_ACTION_COPY); + else + gtk_drag_source_unset (widget); + +exit: + /* Chain up to parent's button_release_event() method. */ + return GTK_WIDGET_CLASS (parent_class)-> + button_release_event (widget, event); +} + +static gboolean +attachment_bar_key_press_event (GtkWidget *widget, + GdkEventKey *event) +{ + EAttachmentBar *attachment_bar; + gboolean editable; + + attachment_bar = E_ATTACHMENT_BAR (widget); + editable = e_attachment_bar_get_editable (attachment_bar); + + if (editable && event->keyval == GDK_Delete) { + GtkActionGroup *action_group; + GtkAction *action; + + action_group = attachment_bar->priv->editable_actions; + action = gtk_action_group_get_action (action_group, "remove"); + gtk_action_activate (action); + } + + /* Chain up to parent's key_press_event() method. */ + return GTK_WIDGET_CLASS (parent_class)-> + key_press_event (widget, event); +} + +static void +attachment_bar_drag_data_get (GtkWidget *widget, + GdkDragContext *drag, + GtkSelectionData *data, + guint info, + guint time) +{ + EAttachmentBarPrivate *priv; EAttachment *attachment; char *path, **uris; int len, n, i = 0; @@ -865,7 +1291,8 @@ eab_drag_data_get(EAttachmentBar *bar, GdkDragContext *drag, GtkSelectionData *d if (info) return; - items = gnome_icon_list_get_selection (GNOME_ICON_LIST (bar)); + priv = E_ATTACHMENT_BAR_GET_PRIVATE (widget); + items = gnome_icon_list_get_selection (GNOME_ICON_LIST (widget)); len = g_list_length (items); uris = g_malloc0 (sizeof (char *) * (len + 1)); @@ -885,7 +1312,7 @@ eab_drag_data_get(EAttachmentBar *bar, GdkDragContext *drag, GtkSelectionData *d } /* If we are not able to save, ignore it */ - if (!(path = temp_save_part (attachment->body, FALSE))) + if (!(path = temp_save_part (attachment, FALSE))) continue; url = camel_url_new ("file://", NULL); @@ -902,227 +1329,280 @@ eab_drag_data_get(EAttachmentBar *bar, GdkDragContext *drag, GtkSelectionData *d gtk_selection_data_set_uris (data, uris); g_free (uris); - - return; } static gboolean -eab_button_release_event(EAttachmentBar *bar, GdkEventButton *event, gpointer dummy) +attachment_bar_popup_menu (GtkWidget *widget) { - GnomeIconList *icon_list = GNOME_ICON_LIST(bar); - GList *selected; - int length; - GtkTargetEntry drag_types[] = { - { "text/uri-list", 0, 0 }, - }; - - if (event && event->button == 1) { - selected = gnome_icon_list_get_selection(icon_list); - length = g_list_length (selected); - if (length) - gtk_drag_source_set((GtkWidget *)bar, GDK_BUTTON1_MASK, drag_types, G_N_ELEMENTS(drag_types), GDK_ACTION_COPY); - else - gtk_drag_source_unset((GtkWidget *)bar); - } + attachment_bar_show_popup_menu (E_ATTACHMENT_BAR (widget), NULL); - return FALSE; + return TRUE; } -static gboolean -eab_button_press_event(EAttachmentBar *bar, GdkEventButton *event, gpointer dummy) +static void +attachment_bar_update_actions (EAttachmentBar *attachment_bar) { - GnomeIconList *icon_list = GNOME_ICON_LIST(bar); - GList *selected = NULL, *tmp; - int length, icon_number; - gboolean take_selected = FALSE; - GtkTargetEntry drag_types[] = { - { "text/uri-list", 0, 0 }, - }; + GnomeIconList *icon_list; + CamelMimePart *mime_part; + GtkUIManager *ui_manager; + GtkActionGroup *action_group; + GtkAction *action; + GList *selection; + guint n_selected; + gboolean is_image; + gpointer parent; + guint merge_id; - selected = gnome_icon_list_get_selection(icon_list); - length = g_list_length (selected); + icon_list = GNOME_ICON_LIST (attachment_bar); + selection = gnome_icon_list_get_selection (icon_list); + n_selected = g_list_length (selection); - if (event) { - icon_number = gnome_icon_list_get_icon_at(icon_list, event->x, event->y); - if (icon_number < 0) { - /* When nothing is selected, deselect all */ - gnome_icon_list_unselect_all (icon_list); - length = 0; - selected = NULL; - } + parent = gtk_widget_get_toplevel (GTK_WIDGET (attachment_bar)); + parent = GTK_WIDGET_TOPLEVEL (parent) ? parent : NULL; - if (event->button == 1) { - /* If something is selected, then allow drag or else help to select */ - if (length) - gtk_drag_source_set((GtkWidget *)bar, GDK_BUTTON1_MASK, drag_types, G_N_ELEMENTS(drag_types), GDK_ACTION_COPY); - else - gtk_drag_source_unset((GtkWidget *)bar); - return FALSE; - } + is_image = FALSE; + mime_part = NULL; - /* If not r-click dont progress any more.*/ - if (event->button != 3) - return FALSE; - - /* When a r-click on something, if it is in the already selected list, consider a r-click of multiple things - * or deselect all and select only this for r-click - */ - if (icon_number >= 0) { - for (tmp = selected; tmp; tmp = tmp->next) { - if (GPOINTER_TO_INT(tmp->data) == icon_number) - take_selected = TRUE; - } + if (n_selected == 1) { + GPtrArray *array; + EAttachment *attachment; + gint index; - if (!take_selected) { - gnome_icon_list_unselect_all(icon_list); - gnome_icon_list_select_icon(icon_list, icon_number); - } - } + array = attachment_bar->priv->attachments; + index = GPOINTER_TO_INT (selection->data); + attachment = E_ATTACHMENT (array->pdata[index]); + mime_part = e_attachment_get_mime_part (attachment); + is_image = e_attachment_is_image (attachment); } - return FALSE; -} - -static gboolean -eab_icon_clicked_cb (EAttachmentBar *bar, GdkEvent *event, gpointer *dummy) -{ - EAttachment *attachment; - gboolean ret = FALSE; - CamelURL *url; - char *path; - GSList *p; - - if (E_IS_ATTACHMENT_BAR (bar) && event->type == GDK_2BUTTON_PRESS) { - p = e_attachment_bar_get_selected (bar); - /* check if has body already, remote files can take longer to fetch */ - if (p && p->next == NULL && ((EAttachment *)p->data)->body) { - attachment = p->data; - - /* Check if the file is stored already */ - if (!attachment->store_uri) { - path = temp_save_part (attachment->body, TRUE); - url = camel_url_new ("file://", NULL); - camel_url_set_path (url, path); - attachment->store_uri = camel_url_to_string (url, 0); - camel_url_free (url); - g_free (path); - } + ui_manager = e_attachment_bar_get_ui_manager (attachment_bar); - /* FIXME Pass a parent window. */ - e_show_uri (NULL, attachment->store_uri); + action_group = attachment_bar->priv->standard_actions; - ret = TRUE; - } + action = gtk_action_group_get_action (action_group, "save-as"); + gtk_action_set_visible (action, n_selected > 0); - if (p) { - g_slist_foreach (p, (GFunc) g_object_unref, NULL); - g_slist_free (p); - } - } + action = gtk_action_group_get_action (action_group, "set-background"); + gtk_action_set_visible (action, is_image); - return ret; -} + action_group = attachment_bar->priv->editable_actions; -/* Initialization. */ + action = gtk_action_group_get_action (action_group, "properties"); + gtk_action_set_visible (action, n_selected == 1); -static void -class_init (EAttachmentBarClass *klass) -{ - GtkObjectClass *object_class; + action = gtk_action_group_get_action (action_group, "remove"); + gtk_action_set_visible (action, n_selected > 0); - object_class = GTK_OBJECT_CLASS (klass); + action_group = attachment_bar->priv->open_actions; - parent_class = g_type_class_ref (gnome_icon_list_get_type ()); + merge_id = attachment_bar->priv->merge_id; + gtk_ui_manager_remove_ui (ui_manager, merge_id); + e_action_group_remove_all_actions (action_group); - object_class->destroy = destroy; + if (mime_part == NULL) + return; - /* Setup signals. */ + e_mime_part_utils_add_open_actions ( + mime_part, ui_manager, action_group, + "/attachment-popup/open-actions", parent, merge_id); +} - signals[CHANGED] = - g_signal_new ("changed", - E_TYPE_ATTACHMENT_BAR, - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (EAttachmentBarClass, changed), - NULL, NULL, - g_cclosure_marshal_VOID__VOID, - G_TYPE_NONE, 0); +static void +attachment_bar_class_init (EAttachmentBarClass *class) +{ + GObjectClass *object_class; + GtkWidgetClass *widget_class; + + parent_class = g_type_class_peek_parent (class); + g_type_class_add_private (class, sizeof (EAttachmentBarPrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->set_property = attachment_bar_set_property; + object_class->get_property = attachment_bar_get_property; + object_class->dispose = attachment_bar_dispose; + object_class->finalize = attachment_bar_finalize; + object_class->constructed = attachment_bar_constructed; + + widget_class = GTK_WIDGET_CLASS (class); + widget_class->event = attachment_bar_event; + widget_class->button_press_event = attachment_bar_button_press_event; + widget_class->button_release_event = attachment_bar_button_release_event; + widget_class->key_press_event = attachment_bar_key_press_event; + widget_class->drag_data_get = attachment_bar_drag_data_get; + widget_class->popup_menu = attachment_bar_popup_menu; + + class->update_actions = attachment_bar_update_actions; + + g_object_class_install_property ( + object_class, + PROP_BACKGROUND_FILENAME, + g_param_spec_string ( + "background-filename", + "Background Filename", + NULL, + NULL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + + g_object_class_install_property ( + object_class, + PROP_BACKGROUND_OPTIONS, + g_param_spec_string ( + "background-options", + "Background Options", + NULL, + NULL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + + g_object_class_install_property ( + object_class, + PROP_CURRENT_FOLDER, + g_param_spec_string ( + "current-folder", + "Current Folder", + NULL, + NULL, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + + g_object_class_install_property ( + object_class, + PROP_EDITABLE, + g_param_spec_boolean ( + "editable", + "Editable", + NULL, + TRUE, + G_PARAM_READWRITE | + G_PARAM_CONSTRUCT)); + + g_object_class_install_property ( + object_class, + PROP_UI_MANAGER, + g_param_spec_object ( + "ui-manager", + "UI Manager", + NULL, + GTK_TYPE_UI_MANAGER, + G_PARAM_READABLE)); + + signals[CHANGED] = g_signal_new ( + "changed", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EAttachmentBarClass, changed), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + signals[UPDATE_ACTIONS] = g_signal_new ( + "update-actions", + G_OBJECT_CLASS_TYPE (class), + G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, + G_STRUCT_OFFSET (EAttachmentBarClass, update_actions), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0); } static void -init (EAttachmentBar *bar) +attachment_bar_init (EAttachmentBar *bar) { - struct _EAttachmentBarPrivate *priv; + GnomeIconList *icon_list; + GtkUIManager *ui_manager; + GtkActionGroup *action_group; + gint icon_width, window_height; + const gchar *domain = GETTEXT_PACKAGE; + GError *error = NULL; + + bar->priv = E_ATTACHMENT_BAR_GET_PRIVATE (bar); + bar->priv->attachments = g_ptr_array_new (); + + GTK_WIDGET_SET_FLAGS (bar, GTK_CAN_FOCUS); + + icon_list = GNOME_ICON_LIST (bar); - priv = g_new (struct _EAttachmentBarPrivate, 1); + calculate_height_width (bar, &icon_width, &window_height); + gnome_icon_list_construct (icon_list, icon_width, NULL, 0); - priv->attach = NULL; - priv->batch_unref = FALSE; - priv->attachments = g_ptr_array_new (); + gtk_widget_set_size_request ( + GTK_WIDGET (bar), icon_width * 4, window_height); - priv->path = NULL; + atk_object_set_name ( + gtk_widget_get_accessible (GTK_WIDGET (bar)), + _("Attachment Bar")); - bar->priv = priv; - bar->expand = FALSE; + 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); + + ui_manager = gtk_ui_manager_new (); + bar->priv->ui_manager = ui_manager; + bar->priv->merge_id = gtk_ui_manager_new_merge_id (ui_manager); + + action_group = gtk_action_group_new ("standard"); + gtk_action_group_set_translation_domain (action_group, domain); + gtk_action_group_add_actions ( + action_group, standard_entries, + G_N_ELEMENTS (standard_entries), bar); + gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); + bar->priv->standard_actions = action_group; + + action_group = gtk_action_group_new ("editable"); + gtk_action_group_set_translation_domain (action_group, domain); + gtk_action_group_add_actions ( + action_group, editable_entries, + G_N_ELEMENTS (editable_entries), bar); + gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); + bar->priv->editable_actions = action_group; + + action_group = gtk_action_group_new ("open"); + gtk_action_group_set_translation_domain (action_group, domain); + gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); + bar->priv->open_actions = action_group; + + /* Because we are loading from a hard-coded string, there is + * no chance of I/O errors. Failure here imples a malformed + * UI definition. Full stop. */ + gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error); + if (error != NULL) + g_error ("%s", error->message); } - GType e_attachment_bar_get_type (void) { static GType type = 0; - if (type == 0) { - static const GTypeInfo info = { + if (G_UNLIKELY (type == 0)) { + static const GTypeInfo type_info = { sizeof (EAttachmentBarClass), - NULL, NULL, - (GClassInitFunc) class_init, - NULL, NULL, + (GBaseInitFunc) NULL, + (GBaseFinalizeFunc) NULL, + (GClassInitFunc) attachment_bar_class_init, + (GClassFinalizeFunc) NULL, + NULL, /* class_data */ sizeof (EAttachmentBar), - 0, - (GInstanceInitFunc) init, + 0, /* n_preallocs */ + (GInstanceInitFunc) attachment_bar_init, + NULL /* value_table */ }; - type = g_type_register_static (GNOME_TYPE_ICON_LIST, "EAttachmentBar", &info, 0); + type = g_type_register_static ( + GNOME_TYPE_ICON_LIST, "EAttachmentBar", &type_info, 0); } return type; } GtkWidget * -e_attachment_bar_new (GtkAdjustment *adj) +e_attachment_bar_new (void) { - EAttachmentBar *new; - GnomeIconList *icon_list; - int icon_width, window_height; - - new = g_object_new (e_attachment_bar_get_type (), NULL); - - icon_list = GNOME_ICON_LIST (new); - - calculate_height_width (new, &icon_width, &window_height); - - gnome_icon_list_construct (icon_list, icon_width, adj, 0); - - gtk_widget_set_size_request (GTK_WIDGET (new), icon_width * 4, window_height); - - GTK_WIDGET_SET_FLAGS (new, GTK_CAN_FOCUS); - - 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); - - atk_object_set_name (gtk_widget_get_accessible (GTK_WIDGET (new)), - _("Attachment Bar")); - - g_signal_connect (new, "button_release_event", G_CALLBACK(eab_button_release_event), NULL); - g_signal_connect (new, "button_press_event", G_CALLBACK(eab_button_press_event), NULL); - g_signal_connect (new, "drag-data-get", G_CALLBACK(eab_drag_data_get), NULL); - g_signal_connect (icon_list, "event", G_CALLBACK (eab_icon_clicked_cb), NULL); - - return GTK_WIDGET (new); + return g_object_new (E_TYPE_ATTACHMENT_BAR, NULL); } static char * @@ -1159,12 +1639,14 @@ attach_to_multipart (CamelMultipart *multipart, { CamelContentType *content_type; CamelDataWrapper *content; + CamelMimePart *mime_part; - if (!attachment->body) + mime_part = e_attachment_get_mime_part (attachment); + if (mime_part == NULL) return; - content_type = camel_mime_part_get_content_type (attachment->body); - content = camel_medium_get_content_object (CAMEL_MEDIUM (attachment->body)); + content_type = camel_mime_part_get_content_type (mime_part); + content = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part)); if (!CAMEL_IS_MULTIPART (content)) { if (camel_content_type_is (content_type, "text", "*")) { @@ -1188,7 +1670,7 @@ attach_to_multipart (CamelMultipart *multipart, 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); + camel_mime_part_set_encoding (mime_part, encoding); if (encoding == CAMEL_TRANSFER_ENCODING_7BIT) { /* the text fits within us-ascii so this is safe */ @@ -1207,24 +1689,26 @@ attach_to_multipart (CamelMultipart *multipart, /* looks kinda nasty, but this is how ya have to do it */ camel_content_type_set_param (content_type, "charset", default_charset); type = camel_content_type_format (content_type); - camel_mime_part_set_content_type (attachment->body, type); + camel_mime_part_set_content_type (mime_part, type); g_free (type); g_free (buf); } camel_object_unref (bestenc); } else if (!CAMEL_IS_MIME_MESSAGE (content)) { - camel_mime_part_set_encoding (attachment->body, CAMEL_TRANSFER_ENCODING_BASE64); + camel_mime_part_set_encoding (mime_part, CAMEL_TRANSFER_ENCODING_BASE64); } } - camel_multipart_add_part (multipart, attachment->body); + camel_multipart_add_part (multipart, mime_part); } void -e_attachment_bar_to_multipart (EAttachmentBar *bar, CamelMultipart *multipart, const char *default_charset) +e_attachment_bar_to_multipart (EAttachmentBar *bar, + CamelMultipart *multipart, + const gchar *default_charset) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; EAttachment *attachment; int i; @@ -1249,32 +1733,73 @@ e_attachment_bar_get_num_attachments (EAttachmentBar *bar) } void -e_attachment_bar_attach (EAttachmentBar *bar, const char *file_name, const char *disposition) +e_attachment_bar_attach (EAttachmentBar *bar, + const gchar *filename, + const gchar *disposition) { + EAttachment *attachment; + CamelException ex; + g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - g_return_if_fail (file_name != NULL && disposition != NULL); + g_return_if_fail (filename != NULL); + g_return_if_fail (disposition != NULL); + + camel_exception_init (&ex); + + attachment = e_attachment_new (filename, disposition, &ex); - add_from_file (bar, file_name, disposition); + if (attachment != NULL) + e_attachment_bar_add_attachment (bar, attachment); + else { + GtkWidget *toplevel; + + /* FIXME: Avoid using error from mailer */ + toplevel = gtk_widget_get_toplevel (GTK_WIDGET (bar)); + e_error_run ( + GTK_WINDOW (toplevel), "mail-composer:no-attach", + filename, camel_exception_get_description (&ex), NULL); + camel_exception_clear (&ex); + } } void -e_attachment_bar_add_attachment (EAttachmentBar *bar, EAttachment *attachment) +e_attachment_bar_add_attachment (EAttachmentBar *bar, + EAttachment *attachment) { g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); + g_return_if_fail (E_IS_ATTACHMENT (attachment)); + + g_ptr_array_add (bar->priv->attachments, attachment); - add_common (bar, attachment); + g_object_weak_ref ( + G_OBJECT (attachment), (GWeakNotify) + attachment_destroy, bar); + + g_signal_connect_swapped ( + attachment, "changed", + G_CALLBACK (e_attachment_bar_refresh), bar); + + e_attachment_bar_refresh (bar); + + g_signal_emit (bar, signals[CHANGED], 0); } void -e_attachment_bar_add_attachment_silent (EAttachmentBar *bar, EAttachment *attachment) +e_attachment_bar_add_attachment_silent (EAttachmentBar *bar, + EAttachment *attachment) { g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - g_return_if_fail (attachment != NULL); + g_return_if_fail (E_IS_ATTACHMENT (attachment)); g_ptr_array_add (bar->priv->attachments, attachment); - g_object_weak_ref ((GObject *) attachment, (GWeakNotify) attachment_destroy, bar); - g_signal_connect (attachment, "changed", G_CALLBACK (attachment_changed_cb), bar); + g_object_weak_ref ( + G_OBJECT (attachment), (GWeakNotify) + attachment_destroy, bar); + + g_signal_connect_swapped ( + attachment, "changed", + G_CALLBACK (e_attachment_bar_refresh), bar); g_signal_emit (bar, signals[CHANGED], 0); } @@ -1282,13 +1807,180 @@ e_attachment_bar_add_attachment_silent (EAttachmentBar *bar, EAttachment *attach void e_attachment_bar_refresh (EAttachmentBar *bar) { - update (bar); + EAttachmentBarPrivate *priv; + GnomeIconList *icon_list; + int bar_width, bar_height; + int i; + + g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); + + 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 (i = 0; i < priv->attachments->len; i++) { + EAttachment *attachment; + CamelContentType *content_type; + CamelMimePart *mime_part; + char *size_string, *label; + GdkPixbuf *pixbuf = NULL; + const char *desc; + + attachment = priv->attachments->pdata[i]; + mime_part = e_attachment_get_mime_part (attachment); + + if (!attachment->is_available_local || mime_part == NULL) { + if ((pixbuf = e_icon_factory_get_icon("mail-attachment", E_ICON_SIZE_DIALOG))) { + attachment->index = gnome_icon_list_append_pixbuf (icon_list, pixbuf, NULL, ""); + g_object_unref (pixbuf); + } + continue; + } + + content_type = camel_mime_part_get_content_type (mime_part); + /* Get the image out of the attachment + and create a thumbnail for it */ + pixbuf = e_attachment_get_thumbnail (attachment); + if (pixbuf != NULL) + g_object_ref (pixbuf); + else if (camel_content_type_is(content_type, "image", "*")) { + CamelDataWrapper *wrapper; + CamelStreamMem *mstream; + GdkPixbufLoader *loader; + gboolean error = TRUE; + + wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part)); + mstream = (CamelStreamMem *) camel_stream_mem_new (); + + camel_data_wrapper_decode_to_stream (wrapper, (CamelStream *) mstream); + + /* Stream image into pixbuf loader */ + loader = gdk_pixbuf_loader_new (); + error = !gdk_pixbuf_loader_write (loader, mstream->buffer->data, mstream->buffer->len, NULL); + gdk_pixbuf_loader_close (loader, NULL); + + if (!error) { + /* The loader owns the reference. */ + pixbuf = gdk_pixbuf_loader_get_pixbuf (loader); + + /* This returns a new GdkPixbuf. */ + pixbuf = scale_pixbuf (pixbuf); + e_attachment_set_thumbnail (attachment, pixbuf); + } else { + pixbuf = NULL; + g_warning ("GdkPixbufLoader Error"); + } + + /* Destroy everything */ + g_object_unref (loader); + camel_object_unref (mstream); + } else if (!bar->expand && (pixbuf = get_system_thumbnail (attachment, content_type))) { + /* This returns a new GdkPixbuf. */ + pixbuf = scale_pixbuf (pixbuf); + e_attachment_set_thumbnail (attachment, pixbuf); + } + + desc = camel_mime_part_get_description (mime_part); + if (desc == NULL || *desc == '\0') + desc = e_attachment_get_filename (attachment); + if (desc == NULL || *desc == '\0') + desc = camel_mime_part_get_filename (mime_part); + + if (!desc) + desc = _("attachment"); + + if (attachment->size && (size_string = g_format_size_for_display (attachment->size))) { + label = g_strdup_printf ("%s (%s)", desc, size_string); + g_free (size_string); + } else + label = g_strdup (desc); + + if (pixbuf == NULL) { + char *mime_type; + + mime_type = camel_content_type_simple (content_type); + pixbuf = e_icon_for_mime_type (mime_type, 48); + if (pixbuf == NULL) { + g_warning("cannot find icon for mime type %s (installation problem?)", mime_type); + pixbuf = e_icon_factory_get_icon("mail-attachment", E_ICON_SIZE_DIALOG); + } + g_free (mime_type); + + /* remember this picture and use it later again */ + if (pixbuf) + e_attachment_set_thumbnail (attachment, pixbuf); + } + + if (pixbuf) { + GdkPixbuf *pixbuf_orig = pixbuf; + pixbuf = gdk_pixbuf_add_alpha (pixbuf_orig, TRUE, 255, 255, 255); + + /* gdk_pixbuf_add_alpha returns a newly allocated pixbuf, + free the original one. + */ + g_object_unref (pixbuf_orig); + + /* In case of a attachment bar, in a signed/encrypted part, display the status as a emblem*/ + if (attachment->sign) { + /* Show the signature status at the right-bottom.*/ + GdkPixbuf *sign = NULL; + int x, y; + + if (attachment->sign == CAMEL_CIPHER_VALIDITY_SIGN_BAD) + sign = e_icon_factory_get_icon ("stock_signature-bad", E_ICON_SIZE_MENU); + else if (attachment->sign == CAMEL_CIPHER_VALIDITY_SIGN_GOOD) + sign = e_icon_factory_get_icon ("stock_signature-ok", E_ICON_SIZE_MENU); + else + sign = e_icon_factory_get_icon ("stock_signature", E_ICON_SIZE_MENU); + + x = gdk_pixbuf_get_width (pixbuf) - 17; + y = gdk_pixbuf_get_height (pixbuf) - 17; + + gdk_pixbuf_copy_area (sign, 0, 0, 16, 16, pixbuf, x, y); + g_object_unref (sign); + } + + if (attachment->encrypt) { + /* Show the encryption status at the top left.*/ + GdkPixbuf *encrypt = e_icon_factory_get_icon ("stock_lock-ok", E_ICON_SIZE_MENU); + + gdk_pixbuf_copy_area (encrypt, 0, 0, 16, 16, pixbuf, 1, 1); + g_object_unref (encrypt); + } + + gnome_icon_list_append_pixbuf (icon_list, pixbuf, NULL, label); + g_object_unref (pixbuf); + } + + g_free (label); + } + + gnome_icon_list_thaw (icon_list); + + /* Resize */ + if (bar->expand) { + gtk_widget_get_size_request ((GtkWidget *) bar, &bar_width, &bar_height); + + if (bar->priv->attachments->len) { + int per_col, rows, height, width; + + calculate_height_width(bar, &width, &height); + per_col = bar_width / width; + per_col = (per_col ? per_col : 1); + rows = (bar->priv->attachments->len + per_col -1) / per_col; + gtk_widget_set_size_request ((GtkWidget *) bar, bar_width, rows * height); + } + } } int e_attachment_bar_get_download_count (EAttachmentBar *bar) { - struct _EAttachmentBarPrivate *priv; + EAttachmentBarPrivate *priv; EAttachment *attachment; int i, n = 0; @@ -1306,68 +1998,57 @@ e_attachment_bar_get_download_count (EAttachmentBar *bar) } void -e_attachment_bar_attach_remote_file (EAttachmentBar *bar, const char *url, const char *disposition) +e_attachment_bar_attach_remote_file (EAttachmentBar *bar, + const gchar *url, + const gchar *disposition) { EAttachment *attachment; CamelException ex; - GtkWindow *parent; + GtkWidget *parent; g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - if (!bar->priv->path) + if (bar->priv->path == NULL) bar->priv->path = e_mkdtemp ("attach-XXXXXX"); - parent = (GtkWindow *) gtk_widget_get_toplevel ((GtkWidget *) bar); + parent = gtk_widget_get_toplevel (GTK_WIDGET (bar)); camel_exception_init (&ex); - if ((attachment = e_attachment_new_remote_file (parent, url, disposition, bar->priv->path, &ex))) { - add_common (bar, attachment); - g_signal_connect (attachment, "update", G_CALLBACK (update_remote_file), bar); + + attachment = e_attachment_new_remote_file ( + GTK_WINDOW (parent), url, disposition, bar->priv->path, &ex); + + if (attachment != NULL) { + e_attachment_bar_add_attachment (bar, attachment); + g_signal_connect ( + attachment, "update", + G_CALLBACK (update_remote_file), bar); } else { - e_error_run (parent, "mail-composer:no-attach", - url, camel_exception_get_description (&ex), NULL); + e_error_run ( + GTK_WINDOW (parent), "mail-composer:no-attach", + url, camel_exception_get_description (&ex), NULL); camel_exception_clear (&ex); } } void -e_attachment_bar_attach_mime_part (EAttachmentBar *bar, CamelMimePart *part) +e_attachment_bar_attach_mime_part (EAttachmentBar *bar, + CamelMimePart *part) { - g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); - - add_from_mime_part (bar, part); -} - -static void -action_recent_cb (GtkAction *action, - EAttachmentBar *attachment_bar) -{ - GtkRecentChooser *chooser; - GFile *file; - gchar *uri; - - chooser = GTK_RECENT_CHOOSER (action); + EAttachment *attachment; - /* Wish: gtk_recent_chooser_get_current_file() */ - uri = gtk_recent_chooser_get_current_uri (chooser); - file = g_file_new_for_uri (uri); - g_free (uri); + /* XXX Is this function really worth keeping? */ - if (g_file_is_native (file)) - e_attachment_bar_attach ( - E_ATTACHMENT_BAR (attachment_bar), - g_file_get_path (file), "attachment"); - else - e_attachment_bar_attach_remote_file ( - E_ATTACHMENT_BAR (attachment_bar), - g_file_get_uri (file), "attachment"); + g_return_if_fail (E_IS_ATTACHMENT_BAR (bar)); + g_return_if_fail (CAMEL_IS_MIME_PART (part)); - g_object_unref (file); + attachment = e_attachment_new_from_mime_part (part); + e_attachment_bar_add_attachment (bar, attachment); } GtkAction * -e_attachment_bar_recent_action_new (EAttachmentBar *bar, - const gchar *action_name, - const gchar *action_label) +e_attachment_bar_recent_action_new (EAttachmentBar *bar, + const gchar *action_name, + const gchar *action_label) { GtkAction *action; GtkRecentChooser *chooser; @@ -1392,3 +2073,148 @@ e_attachment_bar_recent_action_new (EAttachmentBar *bar, return action; } +gint +e_attachment_bar_file_chooser_dialog_run (EAttachmentBar *attachment_bar, + GtkWidget *dialog) +{ + GtkFileChooser *file_chooser; + gint response = GTK_RESPONSE_NONE; + const gchar *current_folder; + gboolean save_folder; + + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), response); + g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), response); + + file_chooser = GTK_FILE_CHOOSER (dialog); + current_folder = e_attachment_bar_get_current_folder (attachment_bar); + gtk_file_chooser_set_current_folder (file_chooser, current_folder); + + response = gtk_dialog_run (GTK_DIALOG (dialog)); + + save_folder = + (response == GTK_RESPONSE_ACCEPT) || + (response == GTK_RESPONSE_OK) || + (response == GTK_RESPONSE_YES) || + (response == GTK_RESPONSE_APPLY); + + if (save_folder) { + gchar *folder; + + folder = gtk_file_chooser_get_current_folder (file_chooser); + e_attachment_bar_set_current_folder (attachment_bar, folder); + g_free (folder); + } + + return response; +} + +void +e_attachment_bar_update_actions (EAttachmentBar *attachment_bar) +{ + g_return_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar)); + + g_signal_emit (attachment_bar, signals[UPDATE_ACTIONS], 0); +} + +const gchar * +e_attachment_bar_get_background_filename (EAttachmentBar *attachment_bar) +{ + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), NULL); + + return attachment_bar->priv->background_filename; +} + +void +e_attachment_bar_set_background_filename (EAttachmentBar *attachment_bar, + const gchar *background_filename) +{ + EAttachmentBarPrivate *priv; + + g_return_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar)); + + if (background_filename == NULL) + background_filename = ""; + + priv = attachment_bar->priv; + g_free (priv->background_filename); + priv->background_filename = g_strdup (background_filename); + + g_object_notify (G_OBJECT (attachment_bar), "background-filename"); +} + +const gchar * +e_attachment_bar_get_background_options (EAttachmentBar *attachment_bar) +{ + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), NULL); + + return attachment_bar->priv->background_options; +} + +void +e_attachment_bar_set_background_options (EAttachmentBar *attachment_bar, + const gchar *background_options) +{ + EAttachmentBarPrivate *priv; + + g_return_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar)); + + if (background_options == NULL) + background_options = "none"; + + priv = attachment_bar->priv; + g_free (priv->background_options); + priv->background_options = g_strdup (background_options); + + g_object_notify (G_OBJECT (attachment_bar), "background-options"); +} + +const gchar * +e_attachment_bar_get_current_folder (EAttachmentBar *attachment_bar) +{ + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), NULL); + + return attachment_bar->priv->current_folder; +} + +void +e_attachment_bar_set_current_folder (EAttachmentBar *attachment_bar, + const gchar *current_folder) +{ + + g_return_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar)); + + if (current_folder == NULL) + current_folder = g_get_home_dir (); + + g_free (attachment_bar->priv->current_folder); + attachment_bar->priv->current_folder = g_strdup (current_folder); + + g_object_notify (G_OBJECT (attachment_bar), "current-folder"); +} + +gboolean +e_attachment_bar_get_editable (EAttachmentBar *attachment_bar) +{ + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), FALSE); + + return attachment_bar->priv->editable; +} + +void +e_attachment_bar_set_editable (EAttachmentBar *attachment_bar, + gboolean editable) +{ + g_return_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar)); + + attachment_bar->priv->editable = editable; + + g_object_notify (G_OBJECT (attachment_bar), "editable"); +} + +GtkUIManager * +e_attachment_bar_get_ui_manager (EAttachmentBar *attachment_bar) +{ + g_return_val_if_fail (E_IS_ATTACHMENT_BAR (attachment_bar), NULL); + + return attachment_bar->priv->ui_manager; +} |