Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to set text without conversion #26

Open
wants to merge 1 commit into
base: gmime3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion gmime/gmime.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <string.h>
#include "gmime.h"


Expand All @@ -8,7 +9,7 @@ GMimeMessage *gmime_parse (const char *buffer, size_t len) {
GMimeMessage *message = g_mime_parser_construct_message (parser, NULL);
g_object_unref (parser);
if (!message) {
return NULL;
return NULL;
}

return message;
Expand Down Expand Up @@ -55,3 +56,53 @@ GByteArray *gmime_get_bytes (GMimeObject *object) {
g_object_unref (stream);
return buf;
}

/**
* gmime_text_part_set_text:
* @mime_part: a #GMimeTextPart
* @text: the text in utf-8
*
* Sets the specified text as the content and updates the charset parameter on the Content-Type header.
**/
void
gmime_text_part_set_text (GMimeTextPart *mime_part, const char *text)
{
GMimeContentType *content_type;
GMimeStream *filtered, *stream;
GMimeContentEncoding encoding;
GMimeDataWrapper *content;
GMimeFilter *filter;
const char *charset;
GMimeCharset mask;
size_t len;

g_return_if_fail (GMIME_IS_TEXT_PART (mime_part));
g_return_if_fail (text != NULL);

len = strlen (text);


charset = "utf-8";

content_type = g_mime_object_get_content_type ((GMimeObject *) mime_part);
g_mime_content_type_set_parameter (content_type, "charset", charset);

stream = g_mime_stream_mem_new_with_buffer (text, len);


content = g_mime_data_wrapper_new_with_stream (stream, GMIME_CONTENT_ENCODING_DEFAULT);
g_object_unref (stream);

g_mime_part_set_content ((GMimePart *) mime_part, content);
g_object_unref (content);

encoding = g_mime_part_get_content_encoding ((GMimePart *) mime_part);

/* if the user has already specified encoding the content with base64/qp/uu, don't change it */
if (encoding > GMIME_CONTENT_ENCODING_BINARY)
return;


g_mime_part_set_content_encoding ((GMimePart *) mime_part, GMIME_CONTENT_ENCODING_8BIT);

}
1 change: 1 addition & 0 deletions gmime/gmime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gboolean gmime_is_multi_part (GMimeObject *object);
gboolean gmime_is_text_part (GMimeObject *object);
void gmime_type_name(GMimeObject *object);
GByteArray *gmime_get_bytes (GMimeObject *object);
void gmime_text_part_set_text (GMimeTextPart *mime_part, const char *text);
2 changes: 1 addition & 1 deletion gmime/part.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *Part) SetText(text string) error {
// TODO: Optimize this
cstr := C.CString(text)
defer C.free(unsafe.Pointer(cstr))
C.g_mime_text_part_set_text((*C.GMimeTextPart)(unsafe.Pointer(p.gmimePart)), cstr)
C.gmime_text_part_set_text((*C.GMimeTextPart)(unsafe.Pointer(p.gmimePart)), cstr)
return nil
}

Expand Down