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);
}