--- gnumeric/plugins/openoffice/openoffice-read.c	2005-09-13 16:40:51.000000000 -0300
+++ gnumeric/plugins/openoffice/openoffice-read.c	2005-09-28 15:40:57.131500248 -0300
@@ -48,13 +48,23 @@
 #include <gsf/gsf-input.h>
 #include <gsf/gsf-infile.h>
 #include <gsf/gsf-infile-zip.h>
+#include <gsf/gsf-meta-names.h>
+#include <gsf/gsf-doc-meta-data.h>
 #include <glib/gi18n.h>
+#include <gsf/gsf-opendocument-meta.h>
 
 #include <string.h>
 #include <locale.h>
 
 GNM_PLUGIN_MODULE_HEADER;
 
+/* Filter Type */
+#define mime_openofficeorg1 "application/vnd.sun.xml.calc"
+#define mime_opendocument "application/vnd.oasis.opendocument.spreadsheet"
+#define UNKNOWN -1
+#define OPENOFFICEORG1 0
+#define OPENDOCUMENT 1
+
 typedef enum {
 	OO_STYLE_UNKNOWN,
 	OO_STYLE_CELL,
@@ -311,38 +321,7 @@ oo_attr_enum (OOParseState *state, xmlCh
 	gnm_expr_parse_str (str, pp,		\
 		flags, state->exprconv, err)
 
-/****************************************************************************/
-
-enum {
-	OO_NS_OFFICE,
-	OO_NS_STYLE,
-	OO_NS_TEXT,
-	OO_NS_TABLE,
-	OO_NS_DRAW,
-	OO_NS_NUMBER,
-	OO_NS_CHART,
-	OO_NS_DR3D,
-	OO_NS_FORM,
-	OO_NS_SCRIPT,
-	OO_NS_CONFIG,
-	OO_NS_MATH,
-	OO_NS_FO,
-	OO_NS_DC,
-	OO_NS_META,
-	OO_NS_XLINK,
-	OO_NS_SVG,
-
-	/* new in 2.0 */
-	OO_NS_OOO,
-	OO_NS_OOOW,
-	OO_NS_OOOC,
-	OO_NS_DOM,
-	OO_NS_XFORMS,
-	OO_NS_XSD,
-	OO_NS_XSI
-};
-
-static GsfXMLInNS ooo_ns[] = {
+GsfXMLInNS ooo_ns[] = {
 	/* OOo 1.0.x & 1.1.x */
 	GSF_XML_IN_NS (OO_NS_OFFICE,	"http://openoffice.org/2000/office"),
 	GSF_XML_IN_NS (OO_NS_STYLE,	"http://openoffice.org/2000/style"),
@@ -1458,15 +1437,22 @@ openoffice_file_open (GOFileOpener const
 {
 	char *old_num_locale, *old_monetary_locale;
 	OOParseState state;
+	GsfInput *mimetype = NULL;
+	GsfInput *meta_file = NULL;
 	GsfInput *content = NULL;
 	GsfInput *styles = NULL;
 	GError   *err = NULL;
 	GsfInfile *zip;
 	int i;
-
+	GsfDocMetaData *meta_data;
+	const guint8 *header = NULL;
+	gint filter = UNKNOWN;
+	
 	g_return_if_fail (IS_WORKBOOK_VIEW (wb_view));
 	g_return_if_fail (GSF_IS_INPUT (input));
 
+	meta_data = gsf_doc_meta_data_new ();
+	
 	zip = gsf_infile_zip_new (input, &err);
 	if (zip == NULL) {
 		g_return_if_fail (err != NULL);
@@ -1476,6 +1462,30 @@ openoffice_file_open (GOFileOpener const
 		return;
 	}
 
+	mimetype = gsf_infile_child_by_name (zip, "mimetype");
+	if (mimetype == NULL) {
+		go_cmd_context_error_import (GO_CMD_CONTEXT (io_context),
+			 _("No stream named mimetype found."));
+		g_object_unref (G_OBJECT (zip));
+		return;
+	}
+
+	if (!gsf_input_seek (mimetype, 0, G_SEEK_SET))
+		header = gsf_input_read (mimetype, gsf_input_size (mimetype), NULL);
+
+	if (!strncmp (mime_openofficeorg1, header, gsf_input_size (mimetype)))
+		filter = OPENOFFICEORG1;
+	else if (!strncmp (mime_opendocument, header, gsf_input_size (mimetype)))
+		filter = OPENDOCUMENT;
+	else{
+		g_object_unref (G_OBJECT (zip));
+		return;
+	}
+	
+	if (filter == OPENDOCUMENT)
+		meta_file = gsf_infile_child_by_name (zip, "meta.xml");
+	/* ps: The function 'paste' doesn't generate this file. */
+
 	content = gsf_infile_child_by_name (zip, "content.xml");
 	if (content == NULL) {
 		go_cmd_context_error_import (GO_CMD_CONTEXT (io_context),
@@ -1520,6 +1530,12 @@ openoffice_file_open (GOFileOpener const
 	state.exprconv = oo_conventions ();
 	state.accum_fmt = NULL;
 
+	if ((meta_file != NULL) && (filter == OPENDOCUMENT)) {
+		err = gsf_opendocument_metadata_read (meta_file, meta_data);
+		g_object_set_data (G_OBJECT(state.pos.wb), "GsfDocMetaData", meta_data);
+		g_object_unref (meta_file);
+	}
+
 	if (styles != NULL) {
 		state.base.doc = styles_doc;
 		gsf_xml_in_parse (&state.base, styles);
@@ -1538,7 +1554,7 @@ openoffice_file_open (GOFileOpener const
 
 		/* look for the view settings */
 		settings = gsf_infile_child_by_name (zip, "settings.xml");
-		if (settings != NULL) {
+		if ((settings != NULL) && (filter == OPENOFFICEORG1)) {
 			state.base.doc = settings_doc;
 			gsf_xml_in_parse (&state.base, settings);
 			g_object_unref (G_OBJECT (settings));
@@ -1574,6 +1590,7 @@ go_plugin_init (GOPlugin *plugin, GOCmdC
 	styles_doc   = gsf_xml_in_doc_new (opencalc_styles_dtd, ooo_ns);
 	content_doc  = gsf_xml_in_doc_new (opencalc_content_dtd, ooo_ns);
 	settings_doc = gsf_xml_in_doc_new (opencalc_settings_dtd, ooo_ns);
+	load_opendocument_meta();
 }
 G_MODULE_EXPORT void
 go_plugin_shutdown (GOPlugin *plugin, GOCmdContext *cc)
@@ -1581,4 +1598,5 @@ go_plugin_shutdown (GOPlugin *plugin, GO
 	gsf_xml_in_doc_free (styles_doc);
 	gsf_xml_in_doc_free (content_doc);
 	gsf_xml_in_doc_free (settings_doc);
+	free_opendocument_meta();
 }
