recognise model/* mime type
[claws.git] / src / procmime.c
index e259adb0d3aaee332ff23b4ca718b59099b0aebb..2be6961e3762c3e8b60128d98691c0e93abb7332 100644 (file)
@@ -1324,6 +1324,7 @@ static struct TypeTable mime_type_table[] = {
        {"image", MIMETYPE_IMAGE},
        {"audio", MIMETYPE_AUDIO},
        {"video", MIMETYPE_VIDEO},
+       {"model", MIMETYPE_MODEL},
        {"application", MIMETYPE_APPLICATION},
        {"message", MIMETYPE_MESSAGE},
        {"multipart", MIMETYPE_MULTIPART},
@@ -2662,3 +2663,79 @@ gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
        return base;
 }
 
+void *procmime_get_part_as_string(MimeInfo *mimeinfo,
+               gboolean null_terminate)
+{
+       FILE *infp;
+       gchar *data;
+       gint length, readlength;
+
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+       if (mimeinfo->encoding_type != ENC_BINARY &&
+                       !procmime_decode_content(mimeinfo))
+               return NULL;
+
+       if (mimeinfo->content == MIMECONTENT_MEM)
+               return g_strdup(mimeinfo->data.mem);
+
+       if ((infp = claws_fopen(mimeinfo->data.filename, "rb")) == NULL) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "claws_fopen");
+               return NULL;
+       }
+
+       if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       length = mimeinfo->length;
+
+       data = g_malloc(null_terminate ? length + 1 : length);
+       if (data == NULL) {
+               g_warning("Could not allocate %d bytes for procmime_get_part_as_string.\n",
+                               (null_terminate ? length + 1 : length));
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       readlength = claws_fread(data, length, 1, infp);
+       if (readlength <= 0) {
+               FILE_OP_ERROR(mimeinfo->data.filename, "fread");
+               g_free(data);
+               claws_fclose(infp);
+               return NULL;
+       }
+
+       claws_fclose(infp);
+
+       if (null_terminate)
+               data[length] = '\0';
+
+       return data;
+}
+
+/* Returns an open GInputStream. The caller should just
+ * read mimeinfo->length bytes from it and then release it. */
+GInputStream *procmime_get_part_as_inputstream(MimeInfo *mimeinfo,
+               GError **error)
+{
+       cm_return_val_if_fail(mimeinfo != NULL, NULL);
+
+       if (mimeinfo->encoding_type != ENC_BINARY &&
+                       !procmime_decode_content(mimeinfo))
+               return NULL;
+
+       if (mimeinfo->content == MIMECONTENT_MEM) {
+               /* NULL for destroy func, since we're not copying
+                * the data for the stream. */
+               return g_memory_input_stream_new_from_data(
+                               mimeinfo->data.mem,
+                               mimeinfo->length, NULL);
+       } else {
+               return g_memory_input_stream_new_from_data(
+                               procmime_get_part_as_string(mimeinfo, FALSE),
+                               mimeinfo->length, g_free);
+       }
+}