From 4cec9fc7169dc3b810321555a70cda916720867d Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Fri, 20 Mar 2009 19:06:59 +0000 Subject: Saving progress on a massive attachment handling rewrite. svn path=/branches/kill-bonobo/; revision=37465 --- calendar/gui/e-cal-popup.c | 442 --------------------------------------------- 1 file changed, 442 deletions(-) (limited to 'calendar/gui/e-cal-popup.c') diff --git a/calendar/gui/e-cal-popup.c b/calendar/gui/e-cal-popup.c index 1495e10506..5839991060 100644 --- a/calendar/gui/e-cal-popup.c +++ b/calendar/gui/e-cal-popup.c @@ -86,395 +86,11 @@ ecalp_target_free(EPopup *ep, EPopupTarget *t) /* Standard menu code */ -static char * -temp_save_part(CamelMimePart *part, char *path, gboolean file) -{ - const char *filename; - char *tmpdir, *utf8_mfilename = NULL, *mfilename = NULL, *usepath; - CamelStream *stream; - CamelDataWrapper *wrapper; - - if (!path) { - tmpdir = e_mkdtemp("evolution-tmp-XXXXXX"); - if (tmpdir == NULL) { - return NULL; - } - - filename = camel_mime_part_get_filename (part); - if (filename == NULL) { - /* This is the default filename used for temporary file creation */ - filename = _("Unknown"); - } else { - utf8_mfilename = g_strdup (filename); - e_filename_make_safe (utf8_mfilename); - mfilename = g_filename_from_utf8 ((const char *) utf8_mfilename, -1, NULL, NULL, NULL); - g_free (utf8_mfilename); - filename = (const char *) mfilename; - } - - path = g_build_filename(tmpdir, filename, NULL); - g_free(tmpdir); - g_free(mfilename); - } else if (!file) { - tmpdir = path; - filename = camel_mime_part_get_filename (part); - if (filename == NULL) { - /* This is the default filename used for temporary file creation */ - filename = _("Unknown"); - } else { - utf8_mfilename = g_strdup (filename); - e_filename_make_safe (utf8_mfilename); - mfilename = g_filename_from_utf8 ((const char *)utf8_mfilename, -1, NULL, NULL, NULL); - g_free (utf8_mfilename); - filename = (const char *) mfilename; - } - - path = g_build_filename(tmpdir, filename, NULL); - g_free(mfilename); - } - - if (strstr (path, "://")) - usepath = path; - else - usepath = g_strjoin (NULL, "file://", path, NULL); - - wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (part)); - stream = camel_stream_vfs_new_with_uri (usepath, CAMEL_STREAM_VFS_CREATE); - - if (usepath != path) - g_free (usepath); - - if (!stream) { - /* TODO handle error conditions */ - g_message ("DEBUG: could not open the file to write\n"); - return NULL; - } - - if (camel_data_wrapper_decode_to_stream (wrapper, (CamelStream *) stream) == -1) { - camel_stream_close (stream); - camel_object_unref (stream); - g_message ("DEBUG: could not write to file\n"); - return NULL; - } - - camel_stream_close(stream); - camel_object_unref(stream); - - return path; -} - -static void -ecalp_part_popup_saveas(EPopup *ep, EPopupItem *item, void *data) -{ - EPopupTarget *t = ep->target; - EAttachment *attachment; - CamelMimePart *part = NULL; - char *file, *mfilename = NULL; - const char *filename; - - attachment = E_ATTACHMENT (((ECalPopupTargetAttachments *) t)->attachments->data); - part = e_attachment_get_mime_part (attachment); - filename = camel_mime_part_get_filename (part); - if (filename == NULL) { - /* This is the default filename used for temporary file creation */ - filename = _("Unknown"); - } else { - mfilename = g_strdup(filename); - e_filename_make_safe(mfilename); - filename = mfilename; - } - file = e_file_dialog_save (_("Save As..."), filename); - - if (file) - temp_save_part (part, file, TRUE); - - g_free (file); - g_free (mfilename); -} - -static void -ecalp_part_popup_save_selected(EPopup *ep, EPopupItem *item, void *data) -{ - GSList *parts; - EPopupTarget *t = ep->target; - char *dir, *path; - - dir = e_file_dialog_save_folder (_("Select folder to save selected attachments...")); - parts = ((ECalPopupTargetAttachments *) t)->attachments; - - for (;parts; parts=parts->next) { - EAttachment *attachment = parts->data; - CamelMimePart *mime_part; - - mime_part = e_attachment_get_mime_part (attachment); - path = temp_save_part (mime_part, dir, FALSE); - /* Probably we 'll do some reporting in next release, like listing the saved files and locations */ - g_free (path); - } -} - -static void -ecalp_part_popup_set_background(EPopup *ep, EPopupItem *item, void *data) -{ - EAttachment *attachment; - EPopupTarget *t = ep->target; - GConfClient *gconf; - char *str, *filename, *path, *extension; - unsigned int i=1; - CamelMimePart *part; - - attachment = E_ATTACHMENT (((ECalPopupTargetAttachments *) t)->attachments->data); - part = e_attachment_get_mime_part (attachment); - - if (!part) - return; - - filename = g_strdup(camel_mime_part_get_filename(part)); - - /* if filename is blank, create a default filename based on MIME type */ - if (!filename || !filename[0]) { - CamelContentType *ct; - - ct = camel_mime_part_get_content_type(part); - g_free (filename); - filename = g_strdup_printf (_("untitled_image.%s"), ct->subtype); - } - - e_filename_make_safe(filename); - - path = g_build_filename(g_get_home_dir(), ".gnome2", "wallpapers", filename, NULL); - - extension = strrchr(filename, '.'); - if (extension) - *extension++ = 0; - - /* if file exists, stick a (number) on the end */ - while (g_file_test(path, G_FILE_TEST_EXISTS)) { - char *name; - name = g_strdup_printf(extension?"%s (%d).%s":"%s (%d)", filename, i++, extension); - g_free(path); - path = g_build_filename(g_get_home_dir(), ".gnome2", "wallpapers", name, NULL); - g_free(name); - } - - g_free(filename); - - if (temp_save_part(part, path, TRUE)) { - gconf = gconf_client_get_default(); - - /* if the filename hasn't changed, blank the filename before - * setting it so that gconf detects a change and updates it */ - if ((str = gconf_client_get_string(gconf, "/desktop/gnome/background/picture_filename", NULL)) != NULL - && strcmp (str, path) == 0) { - gconf_client_set_string(gconf, "/desktop/gnome/background/picture_filename", "", NULL); - } - - g_free (str); - gconf_client_set_string(gconf, "/desktop/gnome/background/picture_filename", path, NULL); - - /* if GNOME currently doesn't display a picture, set to "wallpaper" - * display mode, otherwise leave it alone */ - if ((str = gconf_client_get_string(gconf, "/desktop/gnome/background/picture_options", NULL)) == NULL - || strcmp(str, "none") == 0) { - gconf_client_set_string(gconf, "/desktop/gnome/background/picture_options", "wallpaper", NULL); - } - - gconf_client_suggest_sync(gconf, NULL); - - g_free(str); - g_object_unref(gconf); - } - - g_free(path); -} - -static const EPopupItem ecalp_standard_part_apps_bar = { E_POPUP_BAR, "99.object" }; - -static ECalPopupItem ecalp_attachment_object_popups[] = { - { E_POPUP_ITEM, "00.attach.00", N_("_Save As..."), ecalp_part_popup_saveas, NULL, "document-save-as", E_CAL_POPUP_ATTACHMENTS_ONE }, - { E_POPUP_ITEM, "00.attach.10", N_("Set as _Background"), ecalp_part_popup_set_background, NULL, NULL, E_CAL_POPUP_ATTACHMENTS_IMAGE }, - { E_POPUP_ITEM, "00.attach.20", N_("_Save Selected"), ecalp_part_popup_save_selected, NULL, "document-save-as", E_CAL_POPUP_ATTACHMENTS_MULTIPLE }, - { E_POPUP_BAR, "05.attach", }, -}; - -static void -ecalp_apps_open_in(EPopup *ep, EPopupItem *item, void *data) -{ - char *path; - EAttachment *attachment; - EPopupTarget *target = ep->target; - CamelMimePart *part; - - attachment = E_ATTACHMENT (((ECalPopupTargetAttachments *) target)->attachments->data); - part = e_attachment_get_mime_part (attachment); - - path = temp_save_part(part, NULL, FALSE); - if (path) { - GAppInfo *app = item->user_data; - GList *uris = NULL; - GError *error = NULL; - - if (g_app_info_supports_files (app)) { - GFile *file = g_file_new_for_path (path); - - uris = g_list_append (uris, file); - g_app_info_launch (app, uris, NULL, &error); - g_object_unref (file); - } else { - char *uri; - - uri = e_util_filename_to_uri (path); - uris = g_list_append (uris, uri); - - g_app_info_launch_uris (app, uris, NULL, &error); - g_free (uri); - } - - if (error) { - g_warning ("%s", error->message); - g_error_free (error); - } - - g_list_free (uris); - g_free (path); - } -} - -static void -ecalp_apps_popup_free(EPopup *ep, GSList *free_list, void *data) -{ - while (free_list) { - GSList *n = free_list->next; - EPopupItem *item = free_list->data; - - if (item->user_data && item->activate == ecalp_apps_open_in) - g_object_unref (item->user_data); - - g_free(item->path); - g_free(item->label); - g_free(item); - g_slist_free_1(free_list); - - free_list = n; - } -} - -static void -ecalp_standard_items_free(EPopup *ep, GSList *items, void *data) -{ - g_slist_free(items); -} - -static void -ecalp_standard_menu_factory (EPopup *ecalp, void *data) -{ - int i, len; - EPopupItem *items; - GSList *menus = NULL; - GList *apps = NULL; - char *mime_type = NULL; - const char *filename = NULL; - - switch (ecalp->target->type) { - case E_CAL_POPUP_TARGET_ATTACHMENTS: { - ECalPopupTargetAttachments *t = (ECalPopupTargetAttachments *)ecalp->target; - GSList *list = t->attachments; - EAttachment *attachment; - CamelMimePart *mime_part; - - items = ecalp_attachment_object_popups; - len = G_N_ELEMENTS(ecalp_attachment_object_popups); - - if (g_slist_length(list) != 1 || !((EAttachment *)list->data)->is_available_local) { - break; - } - - /* Only one attachment selected */ - attachment = list->data; - mime_part = e_attachment_get_mime_part (attachment); - mime_type = camel_data_wrapper_get_mime_type (CAMEL_DATA_WRAPPER (mime_part)); - filename = camel_mime_part_get_filename (mime_part); - - - break; } - default: - items = NULL; - len = 0; - } - - if (mime_type) { - gchar *cp; - - /* does gvfs expect lowercase MIME types? */ - for (cp = mime_type; *cp != '\0'; cp++) - *cp = g_ascii_tolower (*cp); - - cp = g_content_type_from_mime_type (mime_type); - apps = g_app_info_get_all_for_type (cp ? cp : mime_type); - g_free (cp); - - if (apps == NULL && strcmp(mime_type, "application/octet-stream") == 0) { - if (filename) { - gchar *name_type; - - name_type = e_util_guess_mime_type (filename, FALSE); - cp = g_content_type_from_mime_type (name_type); - apps = g_app_info_get_all_for_type (cp ? cp : name_type); - g_free (cp); - g_free (name_type); - } - } - g_free (mime_type); - - if (apps) { - GSList *open_menus = NULL; - GList *l; - - menus = g_slist_prepend(menus, (void *)&ecalp_standard_part_apps_bar); - - for (l = apps, i = 0; l; l = l->next, i++) { - GAppInfo *app = l->data; - EPopupItem *item; - - if (!g_app_info_should_show (app)) { - g_object_unref (app); - l->data = NULL; - continue; - } - - item = g_malloc0(sizeof(*item)); - item->type = E_POPUP_ITEM; - item->path = g_strdup_printf("99.object.%02d", i); - item->label = g_strdup_printf(_("Open in %s..."), g_app_info_get_name (app)); - item->activate = ecalp_apps_open_in; - item->user_data = app; - - open_menus = g_slist_prepend(open_menus, item); - } - - if (open_menus) - e_popup_add_items(ecalp, open_menus, NULL, ecalp_apps_popup_free, NULL); - - g_list_free (apps); - } - } - - for (i=0;itarget->mask) == 0) - menus = g_slist_prepend(menus, &items[i]); - } - - if (menus) - e_popup_add_items(ecalp, menus, NULL, ecalp_standard_items_free, NULL); -} - static void ecalp_class_init(GObjectClass *klass) { klass->finalize = ecalp_finalise; ((EPopupClass *)klass)->target_free = ecalp_target_free; - - e_popup_class_add_factory((EPopupClass *)klass, NULL, ecalp_standard_menu_factory, NULL); } GType @@ -763,64 +379,6 @@ e_cal_popup_target_new_source(ECalPopup *eabp, ESourceSelector *selector) return t; } -/** - * e_cal_popup_target_new_attachments: - * @ecp: - * @attachments: A list of CalAttachment objects, reffed for - * the list. Will be unreff'd once finished with. - * - * Owns the list @attachments and their items after they're passed in. - * - * Return value: - **/ -ECalPopupTargetAttachments * -e_cal_popup_target_new_attachments(ECalPopup *ecp, CompEditor *editor, GSList *attachments) -{ - ECalPopupTargetAttachments *t = e_popup_target_new(&ecp->popup, E_CAL_POPUP_TARGET_ATTACHMENTS, sizeof(*t)); - guint32 mask = ~0; - int len = g_slist_length(attachments); - ECal *client = comp_editor_get_client (editor); - CompEditorFlags flags = comp_editor_get_flags (editor); - gboolean read_only = FALSE; - GError *error = NULL; - - if (!e_cal_is_read_only (client, &read_only, &error)) { - if (error->code != E_CALENDAR_STATUS_BUSY) - read_only = TRUE; - g_error_free (error); - } - - if (!read_only && (!(flags & COMP_EDITOR_MEETING) || - (flags & COMP_EDITOR_NEW_ITEM) || - (flags & COMP_EDITOR_USER_ORG))) - mask &= ~ E_CAL_POPUP_ATTACHMENTS_MODIFY; - - t->attachments = attachments; - if (len > 0) - mask &= ~ E_CAL_POPUP_ATTACHMENTS_MANY; - - if (len == 1 && ((EAttachment *)attachments->data)->is_available_local) { - EAttachment *attachment; - CamelMimePart *mime_part; - CamelContentType *mime_type; - - attachment = attachments->data; - mime_part = e_attachment_get_mime_part (attachment); - mime_type = CAMEL_DATA_WRAPPER (mime_part)->mime_type; - - if (camel_content_type_is (mime_type, "image", "*")) - mask &= ~ E_CAL_POPUP_ATTACHMENTS_IMAGE; - mask &= ~ E_CAL_POPUP_ATTACHMENTS_ONE; - } - - if (len > 1) - mask &= ~ E_CAL_POPUP_ATTACHMENTS_MULTIPLE; - - t->target.mask = mask; - - return t; -} - /* ********************************************************************** */ /* Popup menu plugin handler */ -- cgit v1.2.3