LibreOffice Module sc (master) 1
filterdetect.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <com/sun/star/document/XExtendedFilterDetection.hpp>
11#include <com/sun/star/lang/XServiceInfo.hpp>
12#include <com/sun/star/io/XInputStream.hpp>
15
17
18#include <tools/stream.hxx>
19
20#include <orcus/format_detection.hpp>
21
22namespace com::sun::star::uno { class XComponentContext; }
23
24namespace {
25
26class OrcusFormatDetect : public ::cppu::WeakImplHelper<
27 css::document::XExtendedFilterDetection,
28 css::lang::XServiceInfo >
29{
30public:
31 explicit OrcusFormatDetect();
32
33 virtual OUString SAL_CALL getImplementationName() override;
34
35 virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
36
37 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
38
39 virtual OUString SAL_CALL
40 detect( css::uno::Sequence< css::beans::PropertyValue >& rMediaDescSeq ) override;
41
42private:
43};
44
45OrcusFormatDetect::OrcusFormatDetect()
46{
47}
48
49OUString OrcusFormatDetect::getImplementationName()
50{
51 return "com.sun.star.comp.sc.OrcusFilterDetect";
52}
53
54sal_Bool OrcusFormatDetect::supportsService(const OUString& rServiceName)
55{
56 return cppu::supportsService(this, rServiceName);
57}
58
59css::uno::Sequence<OUString> OrcusFormatDetect::getSupportedServiceNames()
60{
61 return {"com.sun.star.frame.ExtendedTypeDetection"};
62}
63
64OUString OrcusFormatDetect::detect(css::uno::Sequence<css::beans::PropertyValue>& rMediaDescSeq)
65{
66 utl::MediaDescriptor aMediaDescriptor( rMediaDescSeq );
67 bool bAborted = aMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_ABORTED, false);
68 if (bAborted)
69 return OUString();
70
71 css::uno::Reference<css::io::XInputStream> xInputStream(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM], css::uno::UNO_QUERY );
72 SvMemoryStream aContent(xInputStream->available());
73
74 static const sal_Int32 nBytes = 4096;
75 css::uno::Sequence<sal_Int8> aSeq(nBytes);
76 bool bEnd = false;
77 while(!bEnd)
78 {
79 sal_Int32 nReadBytes = xInputStream->readBytes(aSeq, nBytes);
80 bEnd = (nReadBytes != nBytes);
81 aContent.WriteBytes(aSeq.getConstArray(), nReadBytes);
82 }
83
84 std::string_view aStream(static_cast<const char*>(aContent.GetData()), aContent.GetSize());
85 orcus::format_t eFormat = orcus::detect(aStream);
86
87 switch (eFormat)
88 {
89 case orcus::format_t::gnumeric:
90 return "Gnumeric XML";
91 case orcus::format_t::xls_xml:
92 return "calc_MS_Excel_2003_XML";
93 default:
94 ;
95 }
96
97 return OUString();
98}
99
100}
101
102extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
104 css::uno::Sequence<css::uno::Any> const &)
105{
106 return cppu::acquire(new OrcusFormatDetect());
107}
108
109/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static constexpr OUStringLiteral PROP_INPUTSTREAM
static constexpr OUStringLiteral PROP_ABORTED
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sc_OrcusFormatDetect_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Sequence< sal_Int8 > aSeq
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool