void test_message_set_content_simple (CamelMimePart *part, gint how, const gchar *type, const gchar *text, gint len) { CamelStreamMem *content = NULL; CamelDataWrapper *dw; static GByteArray *ba; switch (how) { case 0: camel_mime_part_set_content (part, text, len, type); break; case 1: content = (CamelStreamMem *) camel_stream_mem_new_with_buffer (text, len); break; case 2: content = (CamelStreamMem *) camel_stream_mem_new (); camel_stream_mem_set_buffer (content, text, len); break; case 3: ba = g_byte_array_new (); g_byte_array_append (ba, (guint8 *) text, len); content = (CamelStreamMem *) camel_stream_mem_new_with_byte_array (ba); ba = NULL; break; case 4: ba = g_byte_array_new (); g_byte_array_append (ba, (guint8 *) text, len); content = (CamelStreamMem *) camel_stream_mem_new (); camel_stream_mem_set_byte_array (content, ba); g_object_weak_ref ( G_OBJECT (content), (GWeakNotify) content_weak_notify, ba); break; } if (content != 0) { dw = camel_data_wrapper_new (); camel_data_wrapper_set_mime_type (dw, type); camel_data_wrapper_construct_from_stream_sync ( dw, (CamelStream *) content, NULL, NULL); camel_medium_set_content ((CamelMedium *) part, dw); check_unref (content, 2); check_unref (dw, 2); } }
CamelMimeMessage * scalix_appointment_to_mime_message (ScalixObject * object) { CamelMimeMessage *message; CamelMultipart *multipart; CamelMimePart *part; CamelMedium *medium; CamelStream *stream; CamelDataWrapper *wrapper; ECalComponentDateTime dtstart, dtend; ECalComponent *comp; ECalComponentText text; icalcomponent_kind kind; icalcomponent *icalcomp, *toplevel_comp; icaltimezone *zone = NULL; GSList *attachment_list = NULL; GSList *attachment_list_new = NULL; GSList *siter = NULL; GList *part_list = NULL; GList *iter = NULL; char *msgid; char *str, *meeting_status; const char *ouid = NULL; char *file_contents = NULL; char *full_path, *filename, *mime_filename; char *cid; int size; g_object_get (SCALIX_APPOINTMENT (object), "timezone", &zone, NULL); comp = E_CAL_COMPONENT (scalix_object_clone (object)); message = camel_mime_message_new (); medium = CAMEL_MEDIUM (message); camel_medium_add_header (medium, "X-Scalix-Class", "IPM.Appointment"); /* Preserve msg id if there is already one */ if (scalix_appointment_get (SCALIX_APPOINTMENT (comp), X_SCALIX_MSG_ID, &msgid)) { scalix_appointment_unset (SCALIX_APPOINTMENT (comp), X_SCALIX_MSG_ID); } else { msgid = camel_header_msgid_generate (); } camel_mime_message_set_message_id (message, msgid); /* subject */ e_cal_component_get_summary (comp, &text); if (text.value != NULL) { camel_mime_message_set_subject (message, text.value); } /* start day */ e_cal_component_get_dtstart (comp, &dtstart); if (!icaltime_get_timezone (*dtstart.value)) icaltime_set_timezone (dtstart.value, icaltimezone_get_builtin_timezone_from_tzid (dtstart.tzid)); /* end day */ e_cal_component_get_dtend (comp, &dtend); if (!icaltime_get_timezone (*dtend.value)) icaltime_set_timezone (dtend.value, icaltimezone_get_builtin_timezone_from_tzid (dtend.tzid)); /* set From: and Sender: */ if (e_cal_component_has_organizer (comp)) { ECalComponentOrganizer organizer; e_cal_component_get_organizer (comp, &organizer); if (!strncasecmp (organizer.value, "MAILTO:", 7)) { camel_medium_add_header (medium, "Sender", organizer.value + 7); camel_medium_add_header (medium, "From", organizer.value + 7); } } /* set the appropriate recipient headers from the recipient table */ if (e_cal_component_has_attendees (comp) && e_cal_component_has_organizer (comp)) { GSList *iter, *attendees = NULL; CamelInternetAddress *recipients_to = NULL; CamelInternetAddress *recipients_cc = NULL; meeting_status = "1"; e_cal_component_get_attendee_list (comp, &attendees); for (iter = attendees; iter; iter = iter->next) { ECalComponentAttendee *attendee = iter->data; const char *mail = NULL; /* attendee entries must start with MAILTO: */ if (strncasecmp (attendee->value, "MAILTO:", 7)) { continue; } mail = attendee->value + 7; if (attendee->role == ICAL_ROLE_REQPARTICIPANT) { if (recipients_to == NULL) { recipients_to = camel_internet_address_new (); } camel_internet_address_add (recipients_to, attendee->cn, mail); } else if (attendee->role == ICAL_ROLE_OPTPARTICIPANT) { if (recipients_cc == NULL) { recipients_cc = camel_internet_address_new (); } camel_internet_address_add (recipients_cc, attendee->cn, mail); } else { continue; } } if (recipients_to != NULL) { camel_mime_message_set_recipients (message, "To", recipients_to); camel_object_unref (recipients_to); } if (recipients_cc != NULL) { camel_mime_message_set_recipients (message, "Cc", recipients_cc); camel_object_unref (recipients_cc); } } else { meeting_status = "0"; } /* Clear properties */ scalix_appointment_unset (SCALIX_APPOINTMENT (comp), X_SCALIX_IMAP_UID); /* Render the text/calendar */ e_cal_component_commit_sequence (comp); icalcomp = e_cal_component_get_icalcomponent (comp); kind = icalcomponent_isa (icalcomp); if (kind != ICAL_VCALENDAR_COMPONENT) { /* If its not a VCALENDAR, make it one to simplify below */ toplevel_comp = e_cal_util_new_top_level (); icalcomponent_add_component (toplevel_comp, icalcomp); icalcomp = toplevel_comp; } /* set METHOD to PUSBLISH */ icalcomponent_set_method (icalcomp, ICAL_METHOD_PUBLISH); /* Add the VTIMEZONE components for start- and/or end-times */ if (zone) { icalcomponent_add_component (icalcomp, icaltimezone_get_component (zone)); } else if (dtstart.tzid) { icalcomponent_add_component (icalcomp, icaltimezone_get_component (icaltimezone_get_builtin_timezone_from_tzid (dtstart.tzid))); } if (dtstart.tzid && dtend.tzid && strcmp (dtstart.tzid, dtend.tzid) != 0) { icalcomponent_add_component (icalcomp, icaltimezone_get_component (icaltimezone_get_builtin_timezone_from_tzid (dtend.tzid))); } /* FIXME: do we leek icalcomponents here? */ if (e_cal_component_has_attachments (comp)) { multipart = camel_multipart_new (); camel_multipart_set_boundary (multipart, NULL); e_cal_component_get_uid (comp, &ouid); e_cal_component_get_attachment_list (comp, &attachment_list); for (siter = attachment_list; siter; siter = siter->next) { if (siter->data == NULL) continue; if (strstr (siter->data, "file://") != siter->data) continue; full_path = ((char *) siter->data) + strlen ("file://"); filename = g_strrstr (full_path, "/") + 1; mime_filename = filename + strlen (ouid) + 1; size = 0; file_contents = get_file_contents (full_path, &size); if (file_contents == NULL) continue; stream = camel_stream_mem_new_with_buffer (file_contents, size); wrapper = camel_data_wrapper_new (); camel_data_wrapper_construct_from_stream (wrapper, stream); camel_object_unref (stream); part = camel_mime_part_new (); camel_medium_set_content_object (CAMEL_MEDIUM (part), wrapper); camel_mime_part_set_filename (part, mime_filename); camel_mime_part_set_encoding (part, CAMEL_TRANSFER_ENCODING_BASE64); cid = camel_header_msgid_generate (); camel_mime_part_set_content_id (part, cid); camel_mime_part_set_description (part, mime_filename); camel_mime_part_set_disposition (part, "attachment"); part_list = g_list_append (part_list, part); attachment_list_new = g_slist_append (attachment_list_new, g_strdup_printf ("CID:%s", cid)); g_free (cid); } e_cal_component_set_attachment_list (comp, attachment_list_new); str = icalcomponent_as_ical_string (icalcomp); part = camel_mime_part_new (); camel_mime_part_set_content (part, str, strlen (str), "text/calendar; method=PUBLISH; charset=UTF-8"); part_list = g_list_prepend (part_list, part); for (iter = part_list; iter; iter = iter->next) { part = (CamelMimePart *) iter->data; camel_multipart_add_part (multipart, part); camel_object_unref (part); } camel_medium_set_content_object (CAMEL_MEDIUM (message), CAMEL_DATA_WRAPPER (multipart)); camel_object_unref (multipart); g_slist_free (attachment_list); g_slist_free (attachment_list_new); g_list_free (part_list); } else { str = icalcomponent_as_ical_string (icalcomp); camel_mime_part_set_content (CAMEL_MIME_PART (message), str, strlen (str), "text/calendar; method=PUBLISH; charset=UTF-8"); } scalix_appointment_set (SCALIX_APPOINTMENT (object), X_SCALIX_MSG_ID, msgid); return message; }
static void load_snapshot_loaded_cb (GFile *snapshot_file, GAsyncResult *result, GSimpleAsyncResult *simple) { EShell *shell; GObject *object; LoadContext *context; EMsgComposer *composer; CamelMimeMessage *message; CamelStream *camel_stream; gchar *contents = NULL; gsize length; GError *local_error = NULL; context = g_simple_async_result_get_op_res_gpointer (simple); g_file_load_contents_finish ( snapshot_file, result, &contents, &length, NULL, &local_error); if (local_error != NULL) { g_warn_if_fail (contents == NULL); g_simple_async_result_take_error (simple, local_error); g_simple_async_result_complete (simple); return; } /* Create an in-memory buffer for the MIME parser to read from. * We have to do this because CamelStreams are syncrhonous-only, * and feeding the parser a direct file stream would block. */ message = camel_mime_message_new (); camel_stream = camel_stream_mem_new_with_buffer (contents, length); camel_data_wrapper_construct_from_stream_sync ( CAMEL_DATA_WRAPPER (message), camel_stream, NULL, &local_error); g_object_unref (camel_stream); g_free (contents); if (local_error != NULL) { g_simple_async_result_take_error (simple, local_error); g_simple_async_result_complete (simple); g_object_unref (message); return; } /* g_async_result_get_source_object() returns a new reference. */ object = g_async_result_get_source_object (G_ASYNC_RESULT (simple)); /* Create a new composer window from the loaded message and * restore its snapshot file so it continues auto-saving to * the same file. */ shell = E_SHELL (object); g_object_ref (snapshot_file); composer = e_msg_composer_new_with_message (shell, message, TRUE, NULL); g_object_set_data_full ( G_OBJECT (composer), SNAPSHOT_FILE_KEY, snapshot_file, (GDestroyNotify) delete_snapshot_file); context->composer = g_object_ref_sink (composer); g_object_unref (message); g_object_unref (object); g_simple_async_result_complete (simple); g_object_unref (simple); }
void camel_ews_utils_sync_created_items (CamelEwsFolder *ews_folder, EEwsConnection *cnc, GSList *items_created, CamelFolderChangeInfo *change_info, GCancellable *cancellable) { CamelFolder *folder; CamelFolderSummary *folder_summary; GSList *l; if (!items_created) return; folder = CAMEL_FOLDER (ews_folder); folder_summary = camel_folder_get_folder_summary (folder); for (l = items_created; l != NULL; l = g_slist_next (l)) { EEwsItem *item = (EEwsItem *) l->data; CamelMessageInfo *mi; const EwsId *id; const EwsMailbox *from; gchar *tmp; EEwsItemType item_type; const gchar *msg_headers; gboolean has_attachments, found_property, message_requests_read_receipt = FALSE; guint32 server_flags; if (!item) continue; if (e_ews_item_get_item_type (item) == E_EWS_ITEM_TYPE_ERROR) { g_object_unref (item); continue; } id = e_ews_item_get_id (item); if (!id) { g_warning ("%s: Missing ItemId for item type %d (subject:%s)", G_STRFUNC, e_ews_item_get_item_type (item), e_ews_item_get_subject (item) ? e_ews_item_get_subject (item) : "???"); g_object_unref (item); continue; } mi = camel_folder_summary_get (folder_summary, id->id); if (mi) { g_clear_object (&mi); g_object_unref (item); continue; } /* PidTagTransportMessageHeaders */ found_property = FALSE; msg_headers = e_ews_item_get_extended_property_as_string (item, NULL, 0x007D, &found_property); if (!found_property) msg_headers = NULL; if (msg_headers && *msg_headers) { CamelMimePart *part = camel_mime_part_new (); CamelStream *stream; CamelMimeParser *parser; stream = camel_stream_mem_new_with_buffer (msg_headers, strlen (msg_headers)); parser = camel_mime_parser_new (); camel_mime_parser_init_with_stream (parser, stream, NULL); camel_mime_parser_scan_from (parser, FALSE); g_object_unref (stream); if (camel_mime_part_construct_from_parser_sync (part, parser, NULL, NULL)) { mi = camel_folder_summary_info_new_from_headers (folder_summary, camel_medium_get_headers (CAMEL_MEDIUM (part))); if (camel_medium_get_header (CAMEL_MEDIUM (part), "Disposition-Notification-To")) message_requests_read_receipt = TRUE; } g_object_unref (parser); g_object_unref (part); } if (!mi) mi = camel_message_info_new (folder_summary); camel_message_info_set_abort_notifications (mi, TRUE); item_type = e_ews_item_get_item_type (item); if (item_type == E_EWS_ITEM_TYPE_EVENT || item_type == E_EWS_ITEM_TYPE_MEETING_MESSAGE || item_type == E_EWS_ITEM_TYPE_MEETING_REQUEST || item_type == E_EWS_ITEM_TYPE_MEETING_RESPONSE || item_type == E_EWS_ITEM_TYPE_MEETING_RESPONSE) camel_message_info_set_user_flag (mi, "$has_cal", TRUE); camel_message_info_set_uid (mi, id->id); camel_message_info_set_size (mi, e_ews_item_get_size (item)); camel_message_info_set_subject (mi, e_ews_item_get_subject (item)); camel_ews_message_info_set_item_type (CAMEL_EWS_MESSAGE_INFO (mi), item_type); camel_ews_message_info_set_change_key (CAMEL_EWS_MESSAGE_INFO (mi), id->change_key); camel_message_info_set_date_sent (mi, e_ews_item_get_date_sent (item)); camel_message_info_set_date_received (mi, e_ews_item_get_date_received (item)); from = e_ews_item_get_from (item); if (!from) from = e_ews_item_get_sender (item); tmp = form_email_string_from_mb (cnc, from, cancellable); camel_message_info_set_from (mi, tmp); g_free (tmp); tmp = form_recipient_list (cnc, e_ews_item_get_to_recipients (item), cancellable); camel_message_info_set_to (mi, tmp); g_free (tmp); tmp = form_recipient_list (cnc, e_ews_item_get_cc_recipients (item), cancellable); camel_message_info_set_cc (mi, tmp); g_free (tmp); e_ews_item_has_attachments (item, &has_attachments); if (has_attachments) camel_message_info_set_flags (mi, CAMEL_MESSAGE_ATTACHMENTS, CAMEL_MESSAGE_ATTACHMENTS); ews_set_threading_data (mi, item); server_flags = ews_utils_get_server_flags (item); ews_utils_merge_server_user_flags (item, mi); camel_message_info_set_flags (mi, server_flags, server_flags); camel_ews_message_info_set_server_flags (CAMEL_EWS_MESSAGE_INFO (mi), server_flags); camel_ews_utils_update_follow_up_flags (item, mi); camel_ews_utils_update_read_receipt_flags (item, mi, server_flags, message_requests_read_receipt); camel_message_info_set_abort_notifications (mi, FALSE); camel_folder_summary_add (folder_summary, mi, FALSE); /* camel_folder_summary_add() sets folder_flagged flag * on the message info, but this is a fresh item downloaded * from the server, thus unset it, to avoid resync up to the server * on folder leave/store */ camel_message_info_set_folder_flagged (mi, FALSE); camel_folder_change_info_add_uid (change_info, id->id); camel_folder_change_info_recent_uid (change_info, id->id); g_object_unref (mi); g_object_unref (item); } g_slist_free (items_created); }