Line data Source code
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 :
13 : #include "document.hxx"
14 :
15 : #include "tools/urlobj.hxx"
16 : #include "sfx2/docfile.hxx"
17 : #include "sfx2/frame.hxx"
18 : #include "sfx2/sfxsids.hrc"
19 : #include "svl/itemset.hxx"
20 :
21 : #include <orcus/spreadsheet/import_interface.hpp>
22 : #include <orcus/orcus_csv.hpp>
23 : #include <orcus/orcus_gnumeric.hpp>
24 : #include <orcus/orcus_xlsx.hpp>
25 : #include <orcus/orcus_ods.hpp>
26 : #include <orcus/global.hpp>
27 :
28 : #include <com/sun/star/task/XStatusIndicator.hpp>
29 :
30 : #ifdef WNT
31 : #define SYSTEM_PATH INetURLObject::FSYS_DOS
32 : #else
33 : #define SYSTEM_PATH INetURLObject::FSYS_UNX
34 : #endif
35 :
36 : using namespace com::sun::star;
37 :
38 : namespace {
39 :
40 0 : uno::Reference<task::XStatusIndicator> getStatusIndicator(SfxMedium& rMedium)
41 : {
42 0 : uno::Reference<task::XStatusIndicator> xStatusIndicator;
43 0 : SfxItemSet* pSet = rMedium.GetItemSet();
44 0 : if (pSet)
45 : {
46 0 : const SfxUnoAnyItem* pItem = static_cast<const SfxUsrAnyItem*>(pSet->GetItem(SID_PROGRESS_STATUSBAR_CONTROL));
47 0 : if (pItem)
48 0 : xStatusIndicator.set(pItem->GetValue(), uno::UNO_QUERY);
49 : }
50 0 : return xStatusIndicator;
51 : }
52 :
53 : }
54 :
55 0 : OString ScOrcusFiltersImpl::toSystemPath(const OUString& rPath)
56 : {
57 0 : INetURLObject aURL(rPath);
58 0 : return OUStringToOString(aURL.getFSysPath(SYSTEM_PATH), RTL_TEXTENCODING_UTF8);
59 : }
60 :
61 0 : bool ScOrcusFiltersImpl::importCSV(ScDocument& rDoc, SfxMedium& rMedium) const
62 : {
63 0 : ScOrcusFactory aFactory(rDoc);
64 0 : aFactory.setStatusIndicator(getStatusIndicator(rMedium));
65 0 : OString aSysPath = toSystemPath(rMedium.GetName());
66 0 : const char* path = aSysPath.getStr();
67 :
68 : try
69 : {
70 0 : orcus::orcus_csv filter(&aFactory);
71 0 : filter.read_file(path);
72 : }
73 0 : catch (const std::exception&)
74 : {
75 0 : rDoc.InsertTab(SC_TAB_APPEND, OUString("Foo"));
76 0 : rDoc.SetString(0, 0, 0, "Failed to load!!!");
77 0 : return false;
78 : }
79 0 : return true;
80 : }
81 :
82 0 : bool ScOrcusFiltersImpl::importGnumeric(ScDocument& rDoc, SfxMedium& rMedium) const
83 : {
84 0 : ScOrcusFactory aFactory(rDoc);
85 0 : aFactory.setStatusIndicator(getStatusIndicator(rMedium));
86 0 : OString aSysPath = toSystemPath(rMedium.GetName());
87 0 : const char* path = aSysPath.getStr();
88 :
89 : try
90 : {
91 0 : orcus::orcus_gnumeric filter(&aFactory);
92 0 : filter.read_file(path);
93 : }
94 0 : catch (const std::exception& e)
95 : {
96 : SAL_WARN("sc", "Unable to load gnumeric file! " << e.what());
97 0 : return false;
98 : }
99 :
100 0 : return true;
101 : }
102 :
103 0 : bool ScOrcusFiltersImpl::importXLSX(ScDocument& rDoc, SfxMedium& rMedium) const
104 : {
105 0 : ScOrcusFactory aFactory(rDoc);
106 0 : aFactory.setStatusIndicator(getStatusIndicator(rMedium));
107 0 : OString aSysPath = toSystemPath(rMedium.GetName());
108 0 : const char* path = aSysPath.getStr();
109 :
110 : try
111 : {
112 0 : orcus::orcus_xlsx filter(&aFactory);
113 0 : filter.read_file(path);
114 : }
115 0 : catch (const std::exception& e)
116 : {
117 : SAL_WARN("sc", "Unable to load xlsx file! " << e.what());
118 0 : return false;
119 : }
120 :
121 0 : return true;
122 : }
123 :
124 0 : bool ScOrcusFiltersImpl::importODS(ScDocument& rDoc, SfxMedium& rMedium) const
125 : {
126 0 : ScOrcusFactory aFactory(rDoc);
127 0 : aFactory.setStatusIndicator(getStatusIndicator(rMedium));
128 0 : OString aSysPath = toSystemPath(rMedium.GetName());
129 0 : const char* path = aSysPath.getStr();
130 :
131 : try
132 : {
133 0 : orcus::orcus_ods filter(&aFactory);
134 0 : filter.read_file(path);
135 : }
136 0 : catch (const std::exception& e)
137 : {
138 : SAL_WARN("sc", "Unable to load ods file! " << e.what());
139 0 : return false;
140 : }
141 :
142 0 : return true;
143 : }
144 :
145 0 : ScOrcusXMLContext* ScOrcusFiltersImpl::createXMLContext(ScDocument& rDoc, const OUString& rPath) const
146 : {
147 0 : return new ScOrcusXMLContextImpl(rDoc, rPath);
148 : }
149 :
150 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|