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 <tools/urlobj.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 
31 using namespace com::sun::star;
32 
33 namespace
34 {
35 uno::Reference<task::XStatusIndicator> getStatusIndicator(const SfxMedium& rMedium)
36 {
37  uno::Reference<task::XStatusIndicator> xStatusIndicator;
38  SfxItemSet* pSet = rMedium.GetItemSet();
39  if (pSet)
40  {
41  const SfxUnoAnyItem* pItem = pSet->GetItem<SfxUnoAnyItem>(SID_PROGRESS_STATUSBAR_CONTROL);
42  if (pItem)
43  xStatusIndicator.set(pItem->GetValue(), uno::UNO_QUERY);
44  }
45  return xStatusIndicator;
46 }
47 
48 bool loadFileContent(SfxMedium& rMedium, orcus::iface::import_filter& filter)
49 {
50  SvStream* pStream = rMedium.GetInStream();
51  pStream->Seek(0);
52  static const size_t nReadBuffer = 1024 * 32;
53  OStringBuffer aBuffer((int(nReadBuffer)));
54  size_t nRead = 0;
55  do
56  {
57  char pData[nReadBuffer];
58  nRead = pStream->ReadBytes(pData, nReadBuffer);
59  aBuffer.append(pData, nRead);
60  } while (nRead == nReadBuffer);
61 
62  try
63  {
64  filter.read_stream(aBuffer.getStr(), aBuffer.getLength());
65  }
66  catch (const std::exception& e)
67  {
68  SAL_WARN("sc", "Unable to load file via orcus filter! " << e.what());
69  return false;
70  }
71 
72  return true;
73 }
74 }
75 
77 {
78  ScOrcusFactory aFactory(rDoc);
79  aFactory.setStatusIndicator(getStatusIndicator(rMedium));
80 
81  orcus::orcus_csv filter(&aFactory);
82  return loadFileContent(rMedium, filter);
83 }
84 
86 {
87  ScOrcusFactory aFactory(rDoc);
88  aFactory.setStatusIndicator(getStatusIndicator(rMedium));
89 
90  orcus::orcus_gnumeric filter(&aFactory);
91  return loadFileContent(rMedium, filter);
92 }
93 
95 {
96  ScOrcusFactory aFactory(rDoc);
97  aFactory.setStatusIndicator(getStatusIndicator(rMedium));
98 
99  orcus::orcus_xls_xml filter(&aFactory);
100  return loadFileContent(rMedium, filter);
101 }
102 
104 {
105  ScOrcusFactory aFactory(rDoc);
106  aFactory.setStatusIndicator(getStatusIndicator(rMedium));
107 
108  orcus::orcus_xlsx filter(&aFactory);
109  return loadFileContent(rMedium, filter);
110 }
111 
113 {
114  ScOrcusFactory aFactory(rDoc);
115  aFactory.setStatusIndicator(getStatusIndicator(rMedium));
116 
117  orcus::orcus_ods filter(&aFactory);
118  return loadFileContent(rMedium, filter);
119 }
120 
121 bool ScOrcusFiltersImpl::importODS_Styles(ScDocument& rDoc, OUString& aPath) const
122 {
123  OString aUrl = OUStringToOString(aPath, RTL_TEXTENCODING_UTF8);
124  const char* path = aUrl.getStr();
125 
126  try
127  {
128  orcus::file_content content(path);
129  ScOrcusFactory aFactory(rDoc);
130  ScOrcusStyles aStyles(aFactory);
131  orcus::import_ods::read_styles(content.data(), content.size(), &aStyles);
132  }
133  catch (const std::exception& e)
134  {
135  SAL_WARN("sc", "Unable to load styles from xml file! " << e.what());
136  return false;
137  }
138 
139  return true;
140 }
141 
142 std::unique_ptr<ScOrcusXMLContext> ScOrcusFiltersImpl::createXMLContext(ScDocument& rDoc,
143  const OUString& rPath) const
144 {
145  return std::make_unique<ScOrcusXMLContextImpl>(rDoc, rPath);
146 }
147 
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< ContentProperties > pData
virtual std::unique_ptr< ScOrcusXMLContext > createXMLContext(ScDocument &rDoc, const OUString &rPath) const override
Create a context for XML file.
sal_uInt64 Seek(sal_uInt64 nPos)
const css::uno::Any & GetValue() const
virtual bool importXLSX(ScDocument &rDoc, SfxMedium &rMedium) const override
SfxItemSet * GetItemSet() const
virtual bool importODS(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.
virtual bool importExcel2003XML(ScDocument &rDoc, SfxMedium &rMedium) const override
std::size_t ReadBytes(void *pData, std::size_t nSize)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
void setStatusIndicator(const css::uno::Reference< css::task::XStatusIndicator > &rIndicator)
Definition: interface.cxx:648
SvStream * GetInStream()
std::unique_ptr< char[]> aBuffer
virtual bool importGnumeric(ScDocument &rDoc, SfxMedium &rMedium) const override
#define SAL_WARN(area, stream)
virtual bool importCSV(ScDocument &rDoc, SfxMedium &rMedium) const override
const SfxPoolItem * GetItem(sal_uInt16 nWhich, bool bSearchInParent=true) const