GMime 2.0 tutorial | ||
---|---|---|
<<< Previous |
Since most people seem to want to know how to "save an attachment", lets start there.
Given a GMimePart object, the first step to saving an attachment is probably going to be figuring out what the filename is. To do that, you'lllikely want to do something like:
static void save_attachment (GMimePart *part) { GMimeDataWrapper *content; const char *filename; GMimeStream *stream; int fd; filename = g_mime_part_get_filename (part); ... |
The g_mime_part_get_filename() function will first check for a filename parameter in the Content-Disposition header. If that parameter exists, it will return the value as the filename. However, if that does not exist, it will fall back to checking for the name parameter sometimes found in the Content-Type header and return that value if it exists (Microsoft Outlook, for example, will set the name parameter, but will not set the filename parameter). If neither of these param values are found, it will simply return NULL.
Now that you've got a filename for the MIME part (well, assuming that it isn't NULL - in which case you'll have to prompt the user or make up your own filename or something), the next step is to open an output stream and write the MIME part's content to disk:
... if ((fd = open (filename, O_CREAT | O_WRONLY, 0666)) == -1) return; stream = g_mime_stream_fs_new (fd); content = g_mime_part_get_content_object (part); g_mime_data_wrapper_write_to_stream (content, stream); g_mime_stream_flush (stream); g_object_unref (content); g_object_unref (stream); } |
In order to get the content of a MIME part (eg. the body of a part, not including the headers), you'll want to use g_mime_part_get_content_object(). To write the content object to a stream, you can use g_mime_data_wrapper_write_to_stream(). On fail, this function will return -1, otherwise it will return some positive value which will usually equate to the number of bytes written (but not always); generally it's a good idea to not rely on the returned value for anything other than error-checking.
<<< Previous | Home | |
Filter Class Overview |