tdf#131936 Correctly detect OOXML variant on import

Change-Id: I29a6b0454bf741ce8ad49078597b3412a83dedb9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/92278
Tested-by: Jenkins
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
diff --git a/include/oox/core/filterdetect.hxx b/include/oox/core/filterdetect.hxx
index 47de2305..9b34de7 100644
--- a/include/oox/core/filterdetect.hxx
+++ b/include/oox/core/filterdetect.hxx
@@ -49,6 +49,12 @@ namespace oox { class AttributeList; }
namespace oox {
namespace core {

enum class OOXMLVariant {
    ECMA_Transitional,
    ISO_Transitional,
    ISO_Strict
};


/** Document handler specifically designed for detecting OOXML file formats.

@@ -79,7 +85,7 @@ public:
private:
    void                parseRelationship( const AttributeList& rAttribs );

    static OUString     getFilterNameFromContentType( const OUString& rContentType, const OUString& rFileName );
    OUString            getFilterNameFromContentType( const OUString& rContentType, const OUString& rFileName );
    void                parseContentTypesDefault( const AttributeList& rAttribs );
    void                parseContentTypesOverride( const AttributeList& rAttribs );

@@ -90,6 +96,7 @@ private:
    OUString            maFileName;
    ContextVector       maContextStack;
    OUString            maTargetPath;
    OOXMLVariant        maOOXMLVariant;
    css::uno::Reference< css::uno::XComponentContext > mxContext;
};

diff --git a/oox/source/core/filterdetect.cxx b/oox/source/core/filterdetect.cxx
index c8ce03ac..4a6edbd 100644
--- a/oox/source/core/filterdetect.cxx
+++ b/oox/source/core/filterdetect.cxx
@@ -54,6 +54,7 @@ using comphelper::DocPasswordVerifierResult;
FilterDetectDocHandler::FilterDetectDocHandler( const  Reference< XComponentContext >& rxContext, OUString& rFilterName, const OUString& rFileName ) :
    mrFilterName( rFilterName ),
    maFileName(rFileName),
    maOOXMLVariant( OOXMLVariant::ECMA_Transitional ),
    mxContext( rxContext )
{
    maContextStack.reserve( 2 );
@@ -142,6 +143,15 @@ void SAL_CALL FilterDetectDocHandler::characters( const OUString& /*aChars*/ )
void FilterDetectDocHandler::parseRelationship( const AttributeList& rAttribs )
{
    OUString aType = rAttribs.getString( XML_Type, OUString() );

    // tdf#131936 Remember filter when opening file as 'Office Open XML Text'
    if (aType.startsWithIgnoreAsciiCase("http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties"))
        maOOXMLVariant = OOXMLVariant::ISO_Transitional;
    else if (aType.startsWithIgnoreAsciiCase("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"))
        maOOXMLVariant = OOXMLVariant::ECMA_Transitional;
    else if (aType.startsWithIgnoreAsciiCase("http://purl.oclc.org/ooxml/officeDocument"))
        maOOXMLVariant = OOXMLVariant::ISO_Strict;

    if ( !(aType == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" // OOXML Transitional
            || aType == "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument") ) //OOXML strict
        return;
@@ -169,14 +179,32 @@ OUString FilterDetectDocHandler::getFilterNameFromContentType( const OUString& r
    bool bDocm = rFileName.endsWithIgnoreAsciiCase(".docm");

    if( rContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" && !bDocm )
        return "writer_MS_Word_2007";
    {
        switch (maOOXMLVariant)
        {
            case OOXMLVariant::ISO_Transitional:
            case OOXMLVariant::ISO_Strict: // Not supported, map to ISO transitional
                return "writer_OOXML";
            case OOXMLVariant::ECMA_Transitional:
                return "writer_MS_Word_2007";
        }
    }

    if( rContentType == "application/vnd.ms-word.document.macroEnabled.main+xml" || bDocm )
        return "writer_MS_Word_2007_VBA";

    if( rContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml" ||
        rContentType == "application/vnd.ms-word.template.macroEnabledTemplate.main+xml" )
        return "writer_MS_Word_2007_Template";
    {
        switch (maOOXMLVariant)
        {
            case OOXMLVariant::ISO_Transitional:
            case OOXMLVariant::ISO_Strict: // Not supported, map to ISO transitional
                return "writer_OOXML_Text_Template";
            case OOXMLVariant::ECMA_Transitional:
                return "writer_MS_Word_2007_Template";
        }
    }

    if( rContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
        return "MS Excel 2007 XML";
diff --git a/oox/source/core/xmlfilterbase.cxx b/oox/source/core/xmlfilterbase.cxx
index 84aaef6..a8dfbb1 100644
--- a/oox/source/core/xmlfilterbase.cxx
+++ b/oox/source/core/xmlfilterbase.cxx
@@ -620,7 +620,11 @@ writeCoreProperties( XmlFilterBase& rSelf, const Reference< XDocumentProperties 
{
    OUString sValue;
    if( rSelf.getVersion() == oox::core::ISOIEC_29500_2008  )
    {
        // The lowercase "officedocument" is intentional and according to the spec
        // (although most other places are written "officeDocument")
        sValue = "http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties";
    }
    else
        sValue = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";

diff --git a/sw/qa/uitest/writer_tests7/data/tdf131936.docx b/sw/qa/uitest/writer_tests7/data/tdf131936.docx
new file mode 100644
index 0000000..f993d6e
--- /dev/null
+++ b/sw/qa/uitest/writer_tests7/data/tdf131936.docx
Binary files differ
diff --git a/sw/qa/uitest/writer_tests7/tdf131936.py b/sw/qa/uitest/writer_tests7/tdf131936.py
new file mode 100644
index 0000000..5d2de2c
--- /dev/null
+++ b/sw/qa/uitest/writer_tests7/tdf131936.py
@@ -0,0 +1,32 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict
from libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.path import get_srcdir_url
from uitest.config import DEFAULT_SLEEP
import time

def get_url_for_data_file(file_name):
    return get_srcdir_url() + "/sw/qa/uitest/writer_tests7/data/" + file_name

class tdf131936(UITestCase):

    def test_tdf131936_saveas_docx_version(self):
        self.ui_test.load_file(get_url_for_data_file("tdf131936.docx"))

        self.ui_test.execute_dialog_through_command(".uno:SaveAs")
        time.sleep(DEFAULT_SLEEP)
        xDialog = self.xUITest.getTopFocusWindow()
        xFileTypeCombo = xDialog.getChild("file_type")
        state = get_state_as_dict(xFileTypeCombo)
        self.assertEqual(state["SelectEntryText"], "Office Open XML Text (Transitional) (.docx)")

        xCancel = xDialog.getChild("cancel")
        self.ui_test.close_dialog_through_button(xCancel)

        self.ui_test.close_doc()

# vim: set shiftwidth=4 softtabstop=4 expandtab: