nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / filter / oox / excelfilter.cxx
blobb015f82500c236317641257277591ad5e849e2f2
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::xls {
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::sheet;
47 using namespace ::com::sun::star::uno;
48 using namespace ::oox::core;
50 using ::oox::drawingml::table::TableStyleListPtr;
52 ExcelFilter::ExcelFilter( const Reference< XComponentContext >& rxContext ) :
53 XmlFilterBase( rxContext ),
54 mpBookGlob( nullptr )
58 ExcelFilter::~ExcelFilter()
60 OSL_ENSURE( !mpBookGlob, "ExcelFilter::~ExcelFilter - workbook data not cleared" );
63 void ExcelFilter::registerWorkbookGlobals( WorkbookGlobals& rBookGlob )
65 mpBookGlob = &rBookGlob;
68 WorkbookGlobals& ExcelFilter::getWorkbookGlobals() const
70 OSL_ENSURE( mpBookGlob, "ExcelFilter::getWorkbookGlobals - missing workbook data" );
71 return *mpBookGlob;
74 void ExcelFilter::unregisterWorkbookGlobals()
76 mpBookGlob = nullptr;
79 bool ExcelFilter::importDocument()
81 /* To activate the XLSX/XLSB dumper, insert the full path to the file
82 file:///<path-to-oox-module>/source/dump/xlsbdumper.ini
83 into the environment variable OOO_XLSBDUMPER and start the office with
84 this variable (nonpro only). */
85 //OOX_DUMP_FILE( ::oox::dump::xlsb::Dumper );
87 OUString aWorkbookPath = getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
88 if( aWorkbookPath.isEmpty() )
89 return false;
91 try
93 try
95 importDocumentProperties();
97 catch( const Exception& )
99 TOOLS_WARN_EXCEPTION("sc", "exception when importing document properties");
101 catch( ... )
103 SAL_WARN("sc", "exception when importing document properties");
105 /* Construct the WorkbookGlobals object referred to by every instance of
106 the class WorkbookHelper, and execute the import filter by constructing
107 an instance of WorkbookFragment and loading the file. */
108 WorkbookGlobalsRef xBookGlob(WorkbookHelper::constructGlobals(*this));
109 if (xBookGlob)
111 rtl::Reference<FragmentHandler> xWorkbookFragment( new WorkbookFragment(*xBookGlob, aWorkbookPath));
113 const WorkbookFragment* pWF = static_cast<const WorkbookFragment*>(xWorkbookFragment.get());
114 const ScDocument& rDoc = pWF->getScDocument();
115 if (ScDocShell* pDocSh = static_cast<ScDocShell*>(rDoc.GetDocumentShell()))
116 pDocSh->SetInitialLinkUpdate( pDocSh->GetMedium());
118 bool bRet = importFragment( xWorkbookFragment);
119 if (bRet)
121 const AddressConverter& rAC = pWF->getAddressConverter();
122 if (rAC.isTabOverflow() || rAC.isColOverflow() || rAC.isRowOverflow())
124 if (rDoc.IsUserInteractionEnabled())
126 // Show data loss warning.
128 INetURLObject aURL( getFileUrl());
129 SfxErrorContext aContext( ERRCTX_SFX_OPENDOC,
130 aURL.getName( INetURLObject::LAST_SEGMENT, true,
131 INetURLObject::DecodeMechanism::WithCharset),
132 nullptr, RID_ERRCTX);
134 OUString aWarning;
135 aContext.GetString( ERRCODE_NONE.MakeWarning(), aWarning);
136 aWarning += ":\n";
138 OUString aMsg;
139 if (rAC.isTabOverflow())
141 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_SHEET_OVERFLOW, aMsg))
142 aWarning += aMsg;
144 if (rAC.isColOverflow())
146 if (!aMsg.isEmpty())
147 aWarning += "\n";
148 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_COLUMN_OVERFLOW, aMsg))
149 aWarning += aMsg;
151 if (rAC.isRowOverflow())
153 if (!aMsg.isEmpty())
154 aWarning += "\n";
155 if (ErrorHandler::GetErrorString( SCWARN_IMPORT_ROW_OVERFLOW, aMsg))
156 aWarning += aMsg;
159 /* XXX displaying a dialog here is ugly and should
160 * rather happen at UI level instead of at the filter
161 * level, but it seems there's no way to transport
162 * detailed information other than returning true or
163 * false at this point? */
165 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(ScDocShell::GetActiveDialogParent(),
166 VclMessageType::Warning, VclButtonsType::Ok,
167 aWarning));
168 xWarn->run();
172 return bRet;
175 catch (...)
179 return false;
182 bool ExcelFilter::exportDocument() throw()
184 return false;
187 const ::oox::drawingml::Theme* ExcelFilter::getCurrentTheme() const
189 return &WorkbookHelper( getWorkbookGlobals() ).getTheme();
192 ::oox::vml::Drawing* ExcelFilter::getVmlDrawing()
194 return nullptr;
197 TableStyleListPtr ExcelFilter::getTableStyles()
199 return TableStyleListPtr();
202 ::oox::drawingml::chart::ChartConverter* ExcelFilter::getChartConverter()
204 return WorkbookHelper( getWorkbookGlobals() ).getChartConverter();
207 void ExcelFilter::useInternalChartDataTable( bool bInternal )
209 return WorkbookHelper( getWorkbookGlobals() ).useInternalChartDataTable( bInternal );
212 GraphicHelper* ExcelFilter::implCreateGraphicHelper() const
214 return new ExcelGraphicHelper( getWorkbookGlobals() );
217 ::oox::ole::VbaProject* ExcelFilter::implCreateVbaProject() const
219 return new ExcelVbaProject( getComponentContext(), Reference< XSpreadsheetDocument >( getModel(), UNO_QUERY ) );
222 sal_Bool SAL_CALL ExcelFilter::filter( const css::uno::Sequence< css::beans::PropertyValue >& rDescriptor )
224 if ( XmlFilterBase::filter( rDescriptor ) )
225 return true;
227 if ( isExportFilter() )
229 bool bExportVBA = exportVBA();
230 Reference< XExporter > xExporter(
231 new XclExpXmlStream( getComponentContext(), bExportVBA, isExportTemplate() ) );
233 Reference< XComponent > xDocument = getModel();
234 Reference< XFilter > xFilter( xExporter, UNO_QUERY );
236 if ( xFilter.is() )
238 xExporter->setSourceDocument( xDocument );
239 if ( xFilter->filter( rDescriptor ) )
240 return true;
244 return false;
247 OUString ExcelFilter::getImplementationName()
249 return "com.sun.star.comp.oox.xls.ExcelFilter";
252 } // namespace oox::xls
255 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
256 com_sun_star_comp_oox_xls_ExcelFilter_get_implementation(css::uno::XComponentContext* context,
257 css::uno::Sequence<css::uno::Any> const &)
259 return cppu::acquire(new oox::xls::ExcelFilter(context));
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */