static char * save_attachment (CamelMimePart * part, const char *uid, const char *attachment_store) { CamelDataWrapper *content; CamelStream *stream = NULL; CamelStreamFs *stream_fs; char *attach_file = NULL; char *file_url = NULL; const char *mime_filename; content = camel_medium_get_content_object (CAMEL_MEDIUM (part)); mime_filename = camel_mime_part_get_filename (part); attach_file = g_strdup_printf ("%s/%s-%s", attachment_store, uid, mime_filename); stream = camel_stream_fs_new_with_name (attach_file, O_RDWR | O_CREAT | O_TRUNC, 0600); if (stream == NULL) goto end; stream_fs = (CamelStreamFs *) stream; camel_data_wrapper_decode_to_stream (content, stream); file_url = g_strdup_printf ("file://%s", attach_file); end: g_free (attach_file); camel_object_unref (stream); return file_url; }
/** * * It dumps attachments to /tmp * **/ void util_attachments_to_files(CamelMimeMessage *message) { gint parts, i = 0; char *store_dir; CamelDataWrapper *content = NULL; content = camel_medium_get_content_object((CamelMedium *) message); if (!content || !CAMEL_IS_MULTIPART(content)) return; parts = camel_multipart_get_number(CAMEL_MULTIPART(content)); if (parts < 1) return; store_dir = "/tmp"; // TODO home for (i = 1; i < parts; i++) { gchar *filename = NULL; gchar *path = NULL; gchar *tmp = NULL; const gchar *orig_filename = NULL; CamelMimePart *mime_part = camel_multipart_get_part(CAMEL_MULTIPART(content), i); orig_filename = camel_mime_part_get_filename(mime_part); g_debug("Orig filename : %s\n", orig_filename); if (orig_filename == NULL) { g_print("Esto es un adjunto distinto."); orig_filename = camel_mime_part_get_description(mime_part); } if (!orig_filename) continue; tmp = clean_name((const guchar *) orig_filename); filename = g_strdup_printf("%s-%s", "evosugar", tmp); path = g_build_filename(store_dir, filename, NULL); g_debug("Attach. to file : %s\n", path); BREAKPOINT; // TODO should be done in another thread if (em_utils_save_part_to_file(NULL, path, mime_part)) { gchar *uri; BREAKPOINT; uri = g_filename_to_uri(path, NULL, NULL); BREAKPOINT; g_free(uri); } BREAKPOINT; g_free(tmp); BREAKPOINT; g_free(filename); BREAKPOINT; g_free(path); BREAKPOINT; } //g_free (store_dir); }
static void export_as_attachments (CamelMultipart *mp, EMFormat *format, CamelStream *stream, CamelMimePart *except) { int i, nparts, partidlen; CamelMimePart *part; if (!mp || !CAMEL_IS_MULTIPART (mp)) return; partidlen = format->part_id->len; nparts = camel_multipart_get_number(mp); for (i = 0; i < nparts; i++) { part = camel_multipart_get_part (mp, i); if (part != except) { CamelMultipart *multipart = (CamelMultipart *)camel_medium_get_content_object((CamelMedium *)part); if (CAMEL_IS_MULTIPART (multipart)) { export_as_attachments (multipart, format, stream, except); } else { g_string_append_printf (format->part_id, ".alternative.%d", i); if (camel_content_type_is (camel_mime_part_get_content_type (part), "text", "html")) { /* always show HTML as attachments and not inline */ camel_mime_part_set_disposition (part, "attachment"); if (!camel_mime_part_get_filename (part)) { char *str = g_strdup_printf ("%s.html", _("attachment")); camel_mime_part_set_filename (part, str); g_free (str); } em_format_part_as (format, stream, part, "application/octet-stream"); } else em_format_part (format, stream, part); g_string_truncate (format->part_id, partidlen); } } } }
/** * * It stores the attachment list into the model. * */ GtkTreeModel * util_get_msg_attachments(CamelMimeMessage *message) { gint parts, i = 0; GtkTreeIter iter; CamelDataWrapper *content = NULL; GtkTreeStore *tree_store = gtk_tree_store_new(2, G_TYPE_BOOLEAN, G_TYPE_STRING); content = camel_medium_get_content_object((CamelMedium *) message); if (!content || !CAMEL_IS_MULTIPART(content)) return; parts = camel_multipart_get_number(CAMEL_MULTIPART(content)); if (parts < 1) return; for (i = 1; i < parts; i++) { const gchar *orig_filename; CamelMimePart *mime_part; mime_part = camel_multipart_get_part(CAMEL_MULTIPART(content), i); orig_filename = camel_mime_part_get_filename(mime_part); if (orig_filename == NULL) { g_debug("No hay filename, esto es un adjunto distinto.\n"); orig_filename = camel_mime_part_get_description(mime_part); } gtk_tree_store_append(tree_store, &iter, NULL); gtk_tree_store_set(tree_store, &iter, 0, TRUE, 1, orig_filename, -1); g_print("Orig filename : %s\n", orig_filename); if (!orig_filename) continue; BREAKPOINT; } return GTK_TREE_MODEL(tree_store); }
gboolean scalix_appointment_init_from_mime_message (ScalixObject * object, CamelMimeMessage * msg) { const char *msgid; const char *attachment_store; const char *mime_type, *alternative_mime_type; const char *content_disposition; const char *mime_filename; const char *cid; const char *cuid; const char *attachment_file_url; GSList *attachments = NULL; GSList *attachments_new = NULL; GSList *siter = NULL; CamelMimePart *part, *alternative; CamelMultipart *multipart, *nested_multipart; CamelDataWrapper *content, *alternative_content; int i, j, num_parts, num_alternatives; gboolean found_ical = FALSE; attachment_store = g_object_get_data (G_OBJECT (object), "attachment-store"); part = CAMEL_MIME_PART (msg); content = camel_medium_get_content_object (CAMEL_MEDIUM (part)); if (content == NULL) { return FALSE; } mime_type = camel_content_type_simple (content->mime_type); if (CAMEL_IS_MULTIPART (content)) { multipart = (CamelMultipart *) content; num_parts = (int) camel_multipart_get_number (multipart); for (i = 0; i < num_parts; i++) { part = camel_multipart_get_part (multipart, i); content = camel_medium_get_content_object (CAMEL_MEDIUM (part)); mime_type = camel_content_type_simple (content->mime_type); mime_filename = camel_mime_part_get_filename (part); content_disposition = camel_mime_part_get_disposition (part); if (CAMEL_IS_MULTIPART (content)) { nested_multipart = (CamelMultipart *) content; num_alternatives = (int) camel_multipart_get_number (nested_multipart); for (j = 0; j < num_alternatives; j++) { alternative = camel_multipart_get_part (nested_multipart, j); alternative_content = camel_medium_get_content_object (CAMEL_MEDIUM (alternative)); alternative_mime_type = camel_content_type_simple (alternative_content-> mime_type); if (g_str_equal (alternative_mime_type, "text/calendar") && found_ical == FALSE) { if (set_ical_from_mime_part (alternative, object) == TRUE) { e_cal_component_get_uid (E_CAL_COMPONENT (object), &cuid); e_cal_component_get_attachment_list (E_CAL_COMPONENT (object), &attachments); /* we are only interested in the first ical body part */ found_ical = TRUE; } } } } else if (g_str_equal (mime_type, "text/calendar") && found_ical == FALSE) { if (set_ical_from_mime_part (part, object) == TRUE) { e_cal_component_get_uid (E_CAL_COMPONENT (object), &cuid); e_cal_component_get_attachment_list (E_CAL_COMPONENT (object), &attachments); /* we are only interested in the first ical body part */ found_ical = TRUE; } } else if (content_disposition && strcmp (content_disposition, "attachment") == 0) { cid = camel_mime_part_get_content_id (part); if (cid != NULL) { for (siter = attachments; siter; siter = siter->next) { if (strstr (siter->data, cid) == (char *) (siter->data) + 4) { attachment_file_url = save_attachment (part, cuid, attachment_store); if (attachment_file_url != NULL) { attachments_new = g_slist_append (attachments_new, g_strdup (attachment_file_url)); } } } } } else { g_print ("XXXXX Unhandled mime part: %s\n", mime_type); } } } else { /* just a simple ical message */ if (g_str_equal (mime_type, "text/calendar")) { set_ical_from_mime_part (part, object); } } if (attachments_new != NULL) { e_cal_component_set_attachment_list (E_CAL_COMPONENT (object), attachments_new); } msgid = camel_mime_message_get_message_id (msg); scalix_appointment_unset (SCALIX_APPOINTMENT (object), X_SCALIX_MSG_ID); scalix_appointment_set (SCALIX_APPOINTMENT (object), X_SCALIX_MSG_ID, msgid); return TRUE; }
static void fill_model_rec(CamelMimeMessage *msg, CamelMimePart *part, GtkTreeStore *model, GtkTreeIter *parent, GString *name) { CamelDataWrapper *containee; int parts, i; char *type; GtkTreeIter iter; int len = name->len; CamelContentType *mime; containee = camel_medium_get_content_object((CamelMedium *)part); if (containee == NULL) return; mime = ((CamelDataWrapper *)containee)->mime_type; type = camel_content_type_simple(mime); if (CAMEL_IS_MULTIPART(containee)) { gtk_tree_store_append(model, &iter, parent); g_string_append_printf(name, ".multipart"); gtk_tree_store_set(model, &iter, 0, FALSE, 1, type, 2, name->str, 3, name->str, 4, part, -1); parts = camel_multipart_get_number((CamelMultipart *)containee); for (i = 0; i < parts; i++) { CamelMimePart *mpart = camel_multipart_get_part((CamelMultipart *)containee, i); g_string_truncate(name, len); g_string_append_printf(name, ".%d", i); fill_model_rec(msg, mpart, model, &iter, name); } } else if (CAMEL_IS_MIME_MESSAGE(containee)) { gtk_tree_store_append(model, &iter, parent); g_string_append_printf(name, ".msg"); gtk_tree_store_set(model, &iter, 0, FALSE, 1, type, 2, name->str, 3, name->str, 4, part, -1); fill_model_rec(msg, (CamelMimePart *)containee, model, &iter, name); } else { char *filename = NULL; const char *ext = NULL, *tmp; int save = FALSE; gtk_tree_store_append(model, &iter, parent); tmp = camel_mime_part_get_filename(part); if (tmp) { filename = clean_name(tmp); ext = strrchr(filename, '.'); } tmp = camel_mime_part_get_disposition(part); if (tmp && !strcmp(tmp, "attachment")) save = TRUE; if (camel_content_type_is(mime, "text", "*")) { if (ext == NULL) { if ((ext = mime->subtype) == NULL || !strcmp(ext, "plain")) ext = "text"; } } else if (camel_content_type_is(mime, "image", "*")) { if (ext == NULL) { if ((ext = mime->subtype) == NULL) ext = "image"; } save = TRUE; } g_string_append_printf(name, ".%s", ext); gtk_tree_store_set(model, &iter, 0, save, 1, type, 2, filename?filename:name->str, 3, filename?NULL:name->str, 4, part, -1); g_free(filename); } g_free(type); g_string_truncate(name, len); }