LibreOffice Module sc (master) 1
orcusfiltersimpl.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 <orcusfiltersimpl.hxx>
11#include <orcusinterface.hxx>
12#include <tokenarray.hxx>
13
14#include <osl/thread.hxx>
15#include <sfx2/docfile.hxx>
16#include <sfx2/frame.hxx>
17#include <sfx2/sfxsids.hrc>
18#include <svl/itemset.hxx>
19#include <rtl/ustring.hxx>
20#include <sal/log.hxx>
21
22#include <orcus/orcus_csv.hpp>
23#include <orcus/orcus_gnumeric.hpp>
24#include <orcus/orcus_xlsx.hpp>
25#include <orcus/orcus_xls_xml.hpp>
26#include <orcus/orcus_ods.hpp>
27#include <orcus/orcus_import_ods.hpp>
28#include <orcus/stream.hpp>
29#include <com/sun/star/task/XStatusIndicator.hpp>
30
31using namespace com::sun::star;
32
33namespace
34{
35uno::Reference<task::XStatusIndicator> getStatusIndicator(const SfxMedium& rMedium)
36{
37 uno::Reference<task::XStatusIndicator> xStatusIndicator;
38 const SfxUnoAnyItem* pItem
39 = rMedium.GetItemSet().GetItem<SfxUnoAnyItem>(SID_PROGRESS_STATUSBAR_CONTROL);
40 if (pItem)
41 xStatusIndicator.set(pItem->GetValue(), uno::UNO_QUERY);
42 return xStatusIndicator;
43}
44
45bool loadFileContent(SfxMedium& rMedium, orcus::iface::import_filter& filter)
46{
47 SvStream* pStream = rMedium.GetInStream();
48 pStream->Seek(0);
49 static const size_t nReadBuffer = 1024 * 32;
50 OStringBuffer aBuffer((int(nReadBuffer)));
51 size_t nRead = 0;
52 do
53 {
54 char pData[nReadBuffer];
55 nRead = pStream->ReadBytes(pData, nReadBuffer);
56 aBuffer.append(pData, nRead);
57 } while (nRead == nReadBuffer);
58
59 try
60 {
61 filter.read_stream(aBuffer);
62 }
63 catch (const std::exception& e)
64 {
65 SAL_WARN("sc", "Unable to load file via orcus filter! " << e.what());
66 return false;
67 }
68
69 return true;
70}
71}
72
74{
75 ScOrcusFactory aFactory(rDoc);
76 aFactory.setStatusIndicator(getStatusIndicator(rMedium));
77
78 orcus::orcus_csv filter(&aFactory);
79 return loadFileContent(rMedium, filter);
80}
81
83{
84 ScOrcusFactory aFactory(rDoc);
85 aFactory.setStatusIndicator(getStatusIndicator(rMedium));
86
87 orcus::orcus_gnumeric filter(&aFactory);
88 return loadFileContent(rMedium, filter);
89}
90
92{
93 ScOrcusFactory aFactory(rDoc);
94 aFactory.setStatusIndicator(getStatusIndicator(rMedium));
95
96 orcus::orcus_xls_xml filter(&aFactory);
97 return loadFileContent(rMedium, filter);
98}
99
101{
102 ScOrcusFactory aFactory(rDoc);
103 aFactory.setStatusIndicator(getStatusIndicator(rMedium));
104
105 orcus::orcus_xlsx filter(&aFactory);
106 return loadFileContent(rMedium, filter);
107}
108
110{
111 ScOrcusFactory aFactory(rDoc);
112 aFactory.setStatusIndicator(getStatusIndicator(rMedium));
113
114 orcus::orcus_ods filter(&aFactory);
115 return loadFileContent(rMedium, filter);
116}
117
118bool ScOrcusFiltersImpl::importODS_Styles(ScDocument& rDoc, OUString& aPath) const
119{
120 try
121 {
122#if defined _WIN32
123 OString aPath8 = OUStringToOString(aPath, RTL_TEXTENCODING_UTF8);
124#else
125 OString aPath8 = OUStringToOString(aPath, osl_getThreadTextEncoding());
126#endif
127 orcus::file_content content(aPath8);
128 ScOrcusFactory aFactory(rDoc);
129 ScOrcusStyles aStyles(aFactory);
130 orcus::import_ods::read_styles(content.str(), &aStyles);
131 }
132 catch (const std::exception& e)
133 {
134 SAL_WARN("sc", "Unable to load styles from xml file! " << e.what());
135 return false;
136 }
137
138 return true;
139}
140
141std::unique_ptr<ScOrcusXMLContext> ScOrcusFiltersImpl::createXMLContext(ScDocument& rDoc,
142 const OUString& rPath) const
143{
144 return std::make_unique<ScOrcusXMLContextImpl>(rDoc, rPath);
145}
146
147/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void setStatusIndicator(const css::uno::Reference< css::task::XStatusIndicator > &rIndicator)
Definition: interface.cxx:702
virtual bool importXLSX(ScDocument &rDoc, SfxMedium &rMedium) const override
virtual bool importGnumeric(ScDocument &rDoc, SfxMedium &rMedium) const override
virtual std::unique_ptr< ScOrcusXMLContext > createXMLContext(ScDocument &rDoc, const OUString &rPath) const override
Create a context for XML file.
virtual bool importODS(ScDocument &rDoc, SfxMedium &rMedium) const override
virtual bool importCSV(ScDocument &rDoc, SfxMedium &rMedium) const override
virtual bool importExcel2003XML(ScDocument &rDoc, SfxMedium &rMedium) const override
virtual bool importODS_Styles(ScDocument &rDoc, OUString &aFileName) const override
Used to import just the styles from an xml file.
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const
SfxItemSet & GetItemSet() const
SvStream * GetInStream()
const css::uno::Any & GetValue() const
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
#define SAL_WARN(area, stream)
std::unique_ptr< sal_Int32[]> pData
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::unique_ptr< char[]> aBuffer