summaryrefslogtreecommitdiff
path: root/sc/source/filter/orcus/filterdetect.cxx
blob: 9ae30ad86ce58aa2a3ee2f2db398630bbadcf788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * 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/.
 */

#include <com/sun/star/document/XExtendedFilterDetection.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <cppuhelper/implbase.hxx>

#include <unotools/mediadescriptor.hxx>

#include <rtl/strbuf.hxx>

#include <orcus/format_detection.hpp>

namespace {

class OrcusFormatDetect : public ::cppu::WeakImplHelper<
                          css::document::XExtendedFilterDetection,
                          css::lang::XServiceInfo >
{
public:
    explicit            OrcusFormatDetect();
    virtual             ~OrcusFormatDetect();

    virtual OUString SAL_CALL getImplementationName()
        throw( css::uno::RuntimeException, std::exception ) override;

    virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName)
        throw( css::uno::RuntimeException, std::exception ) override;

    virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames()
        throw( css::uno::RuntimeException, std::exception ) override;

    virtual OUString SAL_CALL
                        detect( css::uno::Sequence< css::beans::PropertyValue >& rMediaDescSeq )
                            throw( css::uno::RuntimeException, std::exception ) override;

private:
};

OrcusFormatDetect::OrcusFormatDetect()
{
}

OrcusFormatDetect::~OrcusFormatDetect()
{
}

OUString OrcusFormatDetect::getImplementationName()
        throw( css::uno::RuntimeException, std::exception )
{
    return OUString("");
}

sal_Bool OrcusFormatDetect::supportsService(const OUString& /*rServiceName*/)
        throw( css::uno::RuntimeException, std::exception )
{
    return false;
}

css::uno::Sequence<OUString> OrcusFormatDetect::getSupportedServiceNames()
        throw( css::uno::RuntimeException, std::exception )
{
    return css::uno::Sequence<OUString>();
}

OUString OrcusFormatDetect::detect(css::uno::Sequence<css::beans::PropertyValue>& rMediaDescSeq)
        throw( css::uno::RuntimeException, std::exception )
{
    utl::MediaDescriptor aMediaDescriptor( rMediaDescSeq );
    bool bAborted = aMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_ABORTED(), false);
    if (bAborted)
        return OUString();

    css::uno::Reference<css::io::XInputStream> xInputStream(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()], css::uno::UNO_QUERY );

    static const sal_Int32 nBytes = 4096;
    css::uno::Sequence<sal_Int8> aSeq(nBytes);
    bool bEnd = false;
    OStringBuffer aContent;
    while(!bEnd)
    {
        sal_Int32 nReadBytes = xInputStream->readBytes(aSeq, nBytes);
        bEnd = !(nReadBytes == nBytes);
        aContent.append(reinterpret_cast<const char*>(aSeq.getConstArray()), nReadBytes);
    }

    orcus::format_t eFormat = orcus::detect(reinterpret_cast<const unsigned char*>(aContent.getStr()), aContent.getLength());
    if (eFormat == orcus::format_t::gnumeric)
        return OUString("Gnumeric XML");

    return OUString();
}

}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* SAL_CALL
com_sun_star_comp_sc_OrcusFormatDetect_get_implementation(css::uno::XComponentContext* ,
                                                           css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(new OrcusFormatDetect());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */