diff options
Diffstat (limited to 'filter')
-rw-r--r-- | filter/CppunitTest_filter_textfilterdetect.mk | 7 | ||||
-rw-r--r-- | filter/Library_textfd.mk | 1 | ||||
-rw-r--r-- | filter/qa/unit/data/empty.pptx | 0 | ||||
-rw-r--r-- | filter/qa/unit/textfilterdetect.cxx | 48 | ||||
-rw-r--r-- | filter/source/textfilterdetect/filterdetect.cxx | 32 |
5 files changed, 79 insertions, 9 deletions
diff --git a/filter/CppunitTest_filter_textfilterdetect.mk b/filter/CppunitTest_filter_textfilterdetect.mk index dfcaee9ce16a..49cd09fd79b4 100644 --- a/filter/CppunitTest_filter_textfilterdetect.mk +++ b/filter/CppunitTest_filter_textfilterdetect.mk @@ -34,12 +34,7 @@ $(eval $(call gb_CppunitTest_use_ure,filter_textfilterdetect)) $(eval $(call gb_CppunitTest_use_vcl,filter_textfilterdetect)) -$(eval $(call gb_CppunitTest_use_components,filter_textfilterdetect,\ - configmgr/source/configmgr \ - filter/source/textfilterdetect/textfd \ - ucb/source/core/ucb1 \ - ucb/source/ucp/file/ucpfile1 \ -)) +$(eval $(call gb_CppunitTest_use_rdb,filter_textfilterdetect,services)) $(eval $(call gb_CppunitTest_use_configuration,filter_textfilterdetect)) diff --git a/filter/Library_textfd.mk b/filter/Library_textfd.mk index e6d3889410af..c6155f1e9876 100644 --- a/filter/Library_textfd.mk +++ b/filter/Library_textfd.mk @@ -23,6 +23,7 @@ $(eval $(call gb_Library_use_libraries,textfd,\ cppuhelper \ cppu \ sal \ + sfx \ tl \ utl \ svt \ diff --git a/filter/qa/unit/data/empty.pptx b/filter/qa/unit/data/empty.pptx new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/filter/qa/unit/data/empty.pptx diff --git a/filter/qa/unit/textfilterdetect.cxx b/filter/qa/unit/textfilterdetect.cxx index f5bf6fc98b42..4e81a333ab71 100644 --- a/filter/qa/unit/textfilterdetect.cxx +++ b/filter/qa/unit/textfilterdetect.cxx @@ -7,11 +7,15 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <test/bootstrapfixture.hxx> +#include <unotest/macros_test.hxx> + #include <com/sun/star/document/XExtendedFilterDetection.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> #include <comphelper/processfactory.hxx> #include <comphelper/propertyvalue.hxx> -#include <test/bootstrapfixture.hxx> #include <unotools/mediadescriptor.hxx> #include <unotools/streamwrap.hxx> #include <tools/stream.hxx> @@ -26,10 +30,33 @@ using namespace com::sun::star; namespace { /// Test class for PlainTextFilterDetect. -class TextFilterDetectTest : public test::BootstrapFixture +class TextFilterDetectTest : public test::BootstrapFixture, public unotest::MacrosTest { + uno::Reference<uno::XComponentContext> mxComponentContext; + uno::Reference<lang::XComponent> mxComponent; + +public: + void setUp() override; + void tearDown() override; + uno::Reference<lang::XComponent>& getComponent() { return mxComponent; } }; +void TextFilterDetectTest::setUp() +{ + test::BootstrapFixture::setUp(); + + mxComponentContext.set(comphelper::getComponentContext(getMultiServiceFactory())); + mxDesktop.set(frame::Desktop::create(mxComponentContext)); +} + +void TextFilterDetectTest::tearDown() +{ + if (mxComponent.is()) + mxComponent->dispose(); + + test::BootstrapFixture::tearDown(); +} + char const DATA_DIRECTORY[] = "/filter/qa/unit/data/"; CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf114428) @@ -53,6 +80,23 @@ CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testTdf114428) // This was empty, XML declaration caused HTML detect to not handle XHTML. CPPUNIT_ASSERT_EQUAL(OUString("HTML (StarWriter)"), aFilterName); } + +CPPUNIT_TEST_FIXTURE(TextFilterDetectTest, testEmptyFile) +{ + // Given an empty file, with a pptx extension + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "empty.pptx"; + + // When loading the file + getComponent() = loadFromDesktop(aURL); + + // Then make sure it is opened in Impress. + uno::Reference<lang::XServiceInfo> xServiceInfo(getComponent(), uno::UNO_QUERY); + CPPUNIT_ASSERT(xServiceInfo.is()); + + // Without the accompanying fix in place, this test would have failed, as it was opened in + // Writer instead. + CPPUNIT_ASSERT(xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument")); +} } CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/filter/source/textfilterdetect/filterdetect.cxx b/filter/source/textfilterdetect/filterdetect.cxx index b4959639c574..1f5080ad0951 100644 --- a/filter/source/textfilterdetect/filterdetect.cxx +++ b/filter/source/textfilterdetect/filterdetect.cxx @@ -21,6 +21,8 @@ #include <com/sun/star/io/XInputStream.hpp> #include <cppuhelper/supportsservice.hxx> #include <memory> +#include <sfx2/fcontnr.hxx> +#include <sfx2/docfilt.hxx> #define WRITER_TEXT_FILTER "Text" #define CALC_TEXT_FILTER "Text - txt - csv (StarCalc)" @@ -129,6 +131,34 @@ bool IsHTMLStream( const uno::Reference<io::XInputStream>& xInStream ) return GetHTMLToken( OStringToOUString( aToken.toAsciiLowerCase(), RTL_TEXTENCODING_ASCII_US ) ) != HtmlTokenId::NONE; } +/** + * Given an (empty) file URL in rMediaDesc and rExt, looks up the best filter type for it and + * writes the type name to rType, the filter name to rMediaDesc. + */ +bool HandleEmptyFileUrlByExtension(MediaDescriptor& rMediaDesc, const OUString& rExt, + OUString& rType) +{ + OUString aURL = rMediaDesc.getUnpackedValueOrDefault(MediaDescriptor::PROP_URL(), OUString()); + if (!tools::isEmptyFileUrl(aURL)) + { + return false; + } + + if (rExt.isEmpty()) + { + return false; + } + + std::shared_ptr<const SfxFilter> pFilter(SfxFilterMatcher().GetFilter4Extension(rExt)); + if (!pFilter) + { + return false; + } + + rMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= pFilter->GetFilterName(); + rType = pFilter->GetTypeName(); + return true; +} } PlainTextFilterDetect::PlainTextFilterDetect() {} @@ -194,7 +224,7 @@ OUString SAL_CALL PlainTextFilterDetect::detect(uno::Sequence<beans::PropertyVal aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= OUString(WRITER_TEXT_FILTER); else if (aExt == "csv" || aExt == "tsv" || aExt == "tab" || aExt == "xls" || aName.endsWith(".csv.gz")) aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= OUString(CALC_TEXT_FILTER); - else + else if (!HandleEmptyFileUrlByExtension(aMediaDesc, aExt, aType)) aMediaDesc[MediaDescriptor::PROP_FILTERNAME()] <<= OUString(WRITER_TEXT_FILTER); } |