Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / oox / excelfilter.cxx
blob820c174c37bde2b21c01f3b4a9e415ca017e134f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <excelfilter.hxx>
22 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
26 #include <excelvbaproject.hxx>
27 #include <stylesbuffer.hxx>
28 #include <themebuffer.hxx>
29 #include <workbookfragment.hxx>
30 #include <xestream.hxx>
32 #include <addressconverter.hxx>
33 #include <document.hxx>
34 #include <docsh.hxx>
35 #include <scerrors.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/weld.hxx>
38 #include <svtools/sfxecode.hxx>
39 #include <svtools/ehdl.hxx>
40 #include <tools/urlobj.hxx>
41 #include <tools/diagnose_ex.h>
43 namespace oox {
44 namespace xls {
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::sheet;
48 using namespace ::com::sun::star::uno;
49 using namespace ::oox::core;
51 using ::oox::drawingml::table::TableStyleListPtr;
53 ExcelFilter::ExcelFilter( const Reference< XComponentContext >& rxContext ) :
54 XmlFilterBase( rxContext ),
55 mpBookGlob( nullptr )
59 ExcelFilter::~ExcelFilter()
61 OSL_ENSURE( !mpBookGlob, "ExcelFilter::~ExcelFilter - workbook data not cleared" );
64 void ExcelFilter::registerWorkbookGlobals( WorkbookGlobals& rBookGlob )
66 mpBookGlob = &rBookGlob;
69 WorkbookGlobals& ExcelFilter::getWorkbookGlobals() const
71 OSL_ENSURE( mpBookGlob, "ExcelFilter::getWorkbookGlobals - missing workbook data" );
72 return *mpBookGlob;
75 void ExcelFilter::unregisterWorkbookGlobals()
77 mpBookGlob = nullptr;
80 bool ExcelFilter::importDocument()
82 /* To activate the XLSX/XLSB dumper, insert the full path to the file
83 file:///<path-to-oox-module>/source/dump/xlsbdumper.ini
84 into the environment variable OOO_XLSBDUMPER and start the office with
85 this variable (nonpro only). */
86 //OOX_DUMP_FILE( ::oox::dump::xlsb::Dumper );
88 OUString aWorkbookPath = getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
89 if( aWorkbookPath.isEmpty() )
90 return false;
92 try
94 try
96 importDocumentProperties();
98 catch( const Exception& )
100 TOOLS_WARN_EXCEPTION("sc", "exception when importing document properties");
102 catch( ... )
104 SAL_WARN("sc", "exception when importing document properties");
106 /* Construct the WorkbookGlobals object referred to by every instance of
107 the class WorkbookHelper, and execute the import filter by constructing
108 an instance of WorkbookFragment and loading the file. */
109 WorkbookGlobalsRef xBookGlob(WorkbookHelper::constructGlobals(*this));
110 if (xBookGlob.get())
112 rtl::Reference<FragmentHandler> xWorkbookFragment( new WorkbookFragment(*xBookGlob, aWorkbookPath));
113 bool bRet = importFragment( xWorkbookFragment);
114 if (bRet)
116 const WorkbookFragment* pWF = static_cast<const WorkbookFragment*>(xWorkbookFragment.get());
117 const AddressConverter& rAC = pWF->getAddressConverter();
118 if (rAC.isTabOverflow() || rAC.isColOverflow() || rAC.isRowOverflow())
120 const ScDocument& rDoc = pWF->getScDocument();
121 if (rDoc.IsUserInteractionEnabled())
123 // Show data loss warning.
125 INetURLObject aURL( getFileUrl());
126 SfxErrorContext aContext( ERRCTX_SFX_OPENDOC,
127 aURL.getName( INetURLObject::LAST_SEGMENT, true,
128 INetURLObject::DecodeMechanism::WithCharset),
129 nullptr, RID_ERRCTX);
131 OUString aWarning;
132 aContext.GetString( ERRCODE_NONE.MakeWarning(), aWarning);
133 aWarning += ":\n";
135 OUString aMsg;
136 if (rAC.isTabOverflow())
138 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_SHEET_OVERFLOW, aMsg))
139 aWarning += aMsg;
141 if (rAC.isColOverflow())
143 if (!aMsg.isEmpty())
144 aWarning += "\n";
145 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_COLUMN_OVERFLOW, aMsg))
146 aWarning += aMsg;
148 if (rAC.isRowOverflow())
150 if (!aMsg.isEmpty())
151 aWarning += "\n";
152 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_ROW_OVERFLOW, aMsg))
153 aWarning += aMsg;
156 /* XXX displaying a dialog here is ugly and should
157 * rather happen at UI level instead of at the filter
158 * level, but it seems there's no way to transport
159 * detailed information other than returning true or
160 * false at this point? */
162 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(ScDocShell::GetActiveDialogParent(),
163 VclMessageType::Warning, VclButtonsType::Ok,
164 aWarning));
165 xWarn->run();
169 return bRet;
172 catch (...)
176 return false;
179 bool ExcelFilter::exportDocument() throw()
181 return false;
184 const ::oox::drawingml::Theme* ExcelFilter::getCurrentTheme() const
186 return &WorkbookHelper( getWorkbookGlobals() ).getTheme();
189 ::oox::vml::Drawing* ExcelFilter::getVmlDrawing()
191 return nullptr;
194 TableStyleListPtr ExcelFilter::getTableStyles()
196 return TableStyleListPtr();
199 ::oox::drawingml::chart::ChartConverter* ExcelFilter::getChartConverter()
201 return WorkbookHelper( getWorkbookGlobals() ).getChartConverter();
204 void ExcelFilter::useInternalChartDataTable( bool bInternal )
206 return WorkbookHelper( getWorkbookGlobals() ).useInternalChartDataTable( bInternal );
209 GraphicHelper* ExcelFilter::implCreateGraphicHelper() const
211 return new ExcelGraphicHelper( getWorkbookGlobals() );
214 ::oox::ole::VbaProject* ExcelFilter::implCreateVbaProject() const
216 return new ExcelVbaProject( getComponentContext(), Reference< XSpreadsheetDocument >( getModel(), UNO_QUERY ) );
219 sal_Bool SAL_CALL ExcelFilter::filter( const css::uno::Sequence< css::beans::PropertyValue >& rDescriptor )
221 if ( XmlFilterBase::filter( rDescriptor ) )
222 return true;
224 if ( isExportFilter() )
226 bool bExportVBA = exportVBA();
227 Reference< XExporter > xExporter(
228 new XclExpXmlStream( getComponentContext(), bExportVBA, isExportTemplate() ) );
230 Reference< XComponent > xDocument = getModel();
231 Reference< XFilter > xFilter( xExporter, UNO_QUERY );
233 if ( xFilter.is() )
235 xExporter->setSourceDocument( xDocument );
236 if ( xFilter->filter( rDescriptor ) )
237 return true;
241 return false;
244 OUString ExcelFilter::getImplementationName()
246 return "com.sun.star.comp.oox.xls.ExcelFilter";
249 } // namespace xls
250 } // namespace oox
253 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
254 com_sun_star_comp_oox_xls_ExcelFilter_get_implementation(css::uno::XComponentContext* context,
255 css::uno::Sequence<css::uno::Any> const &)
257 return cppu::acquire(new oox::xls::ExcelFilter(context));
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */