Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / oox / source / ppt / pptimport.cxx
blob01f572a54dcfb4fe35a6f09e617aa3071a48a033
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
24 #include <com/sun/star/uno/XComponentContext.hpp>
25 #include <com/sun/star/document/XUndoManager.hpp>
26 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
27 #include <comphelper/propertysequence.hxx>
28 #include <comphelper/scopeguard.hxx>
29 #include <osl/diagnose.h>
30 #include <vcl/svapp.hxx>
31 #include <vcl/weld.hxx>
32 #include <svtools/sfxecode.hxx>
33 #include <svtools/ehdl.hxx>
34 #include <tools/urlobj.hxx>
35 #include <svx/dialmgr.hxx>
36 #include <svx/strings.hrc>
37 #include <oox/ppt/pptimport.hxx>
38 #include <oox/drawingml/chart/chartconverter.hxx>
39 #include <oox/dump/pptxdumper.hxx>
40 #include <drawingml/table/tablestylelistfragmenthandler.hxx>
41 #include <oox/helper/graphichelper.hxx>
42 #include <oox/ole/vbaproject.hxx>
43 #include <oox/ppt/presentationfragmenthandler.hxx>
44 #include <oox/ppt/presPropsfragmenthandler.hxx>
45 #include <oox/token/tokens.hxx>
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::xml::sax;
50 using namespace oox::core;
52 using ::com::sun::star::beans::PropertyValue;
53 using ::com::sun::star::lang::XComponent;
55 namespace oox { namespace ppt {
57 #if OSL_DEBUG_LEVEL > 0
58 XmlFilterBase* PowerPointImport::mpDebugFilterBase = nullptr;
59 #endif
61 PowerPointImport::PowerPointImport( const Reference< XComponentContext >& rxContext ) :
62 XmlFilterBase( rxContext ),
63 mxChartConv( new ::oox::drawingml::chart::ChartConverter )
66 #if OSL_DEBUG_LEVEL > 0
67 mpDebugFilterBase = this;
68 #endif
71 PowerPointImport::~PowerPointImport()
75 /// Visits the relations from pRelations which are of type rType.
76 static void visitRelations(PowerPointImport& rImport, const core::RelationsRef& pRelations, const OUString& rType, std::vector<OUString>& rImageFragments)
78 if (core::RelationsRef pRelationsOfType = pRelations->getRelationsFromTypeFromOfficeDoc(rType))
80 for (const auto& rRelation : *pRelationsOfType)
82 OUString aFragment = pRelationsOfType->getFragmentPathFromRelation(rRelation.second);
83 if (core::RelationsRef pFragmentRelations = rImport.importRelations(aFragment))
85 // See if the fragment has images.
86 if (core::RelationsRef pImages = pFragmentRelations->getRelationsFromTypeFromOfficeDoc("image"))
88 for (const auto& rImage : *pImages)
90 OUString aPath = pImages->getFragmentPathFromRelation(rImage.second);
91 // Safe subset: e.g. WMF may have an external header from the
92 // referencing fragment.
93 if (aPath.endsWith(".jpg") || aPath.endsWith(".jpeg"))
94 rImageFragments.push_back(aPath);
98 // See if the fragment has a slide layout, and recurse.
99 visitRelations(rImport, pFragmentRelations, "slideLayout", rImageFragments);
105 bool PowerPointImport::importDocument()
107 /* to activate the PPTX dumper, define the environment variable
108 OOO_PPTXDUMPER and insert the full path to the file
109 file:///<path-to-oox-module>/source/dump/pptxdumper.ini. */
110 OOX_DUMP_FILE( ::oox::dump::pptx::Dumper );
112 uno::Reference< document::XUndoManagerSupplier > xUndoManagerSupplier (getModel(), UNO_QUERY );
113 uno::Reference< util::XLockable > xUndoManager;
114 bool bWasUnLocked = true;
115 if(xUndoManagerSupplier.is())
117 xUndoManager = xUndoManagerSupplier->getUndoManager();
118 if(xUndoManager.is())
120 bWasUnLocked = !xUndoManager->isLocked();
121 xUndoManager->lock();
125 importDocumentProperties();
127 OUString aFragmentPath = getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
128 FragmentHandlerRef xPresentationFragmentHandler( new PresentationFragmentHandler( *this, aFragmentPath ) );
129 maTableStyleListPath = xPresentationFragmentHandler->getFragmentPathFromFirstTypeFromOfficeDoc( "tableStyles" );
130 const OUString sPresPropsPath
131 = xPresentationFragmentHandler->getFragmentPathFromFirstTypeFromOfficeDoc("presProps");
133 // importRelations() is cheap, it will do an actual import for the first time only.
134 if (core::RelationsRef pFragmentRelations = importRelations(aFragmentPath))
136 std::vector<OUString> aImageFragments;
137 visitRelations(*this, pFragmentRelations, "slide", aImageFragments);
138 visitRelations(*this, pFragmentRelations, "slideMaster", aImageFragments);
140 getGraphicHelper().importEmbeddedGraphics(aImageFragments);
143 bool bRet = importFragment(xPresentationFragmentHandler);
144 if (bRet && !sPresPropsPath.isEmpty())
146 FragmentHandlerRef xPresPropsFragmentHandler(
147 new PresPropsFragmentHandler(*this, sPresPropsPath));
148 importFragment(xPresPropsFragmentHandler);
151 static bool bNoSmartartWarning = getenv("OOX_NO_SMARTART_WARNING");
152 if (!bNoSmartartWarning && mbMissingExtDrawing)
154 // Construct a warning message.
155 INetURLObject aURL(getFileUrl());
156 SfxErrorContext aContext(ERRCTX_SFX_OPENDOC, aURL.getName(INetURLObject::LAST_SEGMENT, true, INetURLObject::DecodeMechanism::WithCharset), nullptr, RID_ERRCTX);
157 OUString aWarning;
158 aContext.GetString(ERRCODE_NONE.MakeWarning(), aWarning);
159 aWarning += ":\n" + SvxResId(RID_SVXSTR_WARN_MISSING_SMARTART);
161 // Show it.
162 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(nullptr,
163 VclMessageType::Warning, VclButtonsType::Ok,
164 aWarning));
165 xWarn->run();
168 if(xUndoManager.is() && bWasUnLocked)
169 xUndoManager->unlock();
171 return bRet;
175 bool PowerPointImport::exportDocument() throw()
177 return false;
180 ::Color PowerPointImport::getSchemeColor( sal_Int32 nToken ) const
182 ::Color nColor;
183 if ( mpActualSlidePersist )
185 bool bColorMapped = false;
186 oox::drawingml::ClrMapPtr pClrMapPtr( mpActualSlidePersist->getClrMap() );
187 if ( pClrMapPtr )
188 bColorMapped = pClrMapPtr->getColorMap( nToken );
190 if ( !bColorMapped ) // try masterpage mapping
192 SlidePersistPtr pMasterPersist = mpActualSlidePersist->getMasterPersist();
193 if ( pMasterPersist )
195 pClrMapPtr = pMasterPersist->getClrMap();
196 if ( pClrMapPtr )
197 pClrMapPtr->getColorMap( nToken );
201 ::oox::drawingml::ThemePtr pTheme = mpActualSlidePersist->getTheme();
202 if( pTheme )
204 pTheme->getClrScheme().getColor( nToken, nColor );
206 else
208 SAL_WARN("oox", "OOX: PowerPointImport::mpThemePtr is NULL");
211 return nColor;
214 const ::oox::drawingml::Theme* PowerPointImport::getCurrentTheme() const
216 return mpActualSlidePersist ? mpActualSlidePersist->getTheme().get() : nullptr;
219 sal_Bool SAL_CALL PowerPointImport::filter( const Sequence< PropertyValue >& rDescriptor )
221 if( XmlFilterBase::filter( rDescriptor ) )
222 return true;
224 if (isExportFilter())
226 uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
228 {"IsPPTM", uno::makeAny(exportVBA())},
229 {"IsTemplate", uno::makeAny(isExportTemplate())},
230 }));
232 Reference<css::lang::XMultiServiceFactory> aFactory(getComponentContext()->getServiceManager(), UNO_QUERY_THROW);
233 Reference< XExporter > xExporter(aFactory->createInstanceWithArguments("com.sun.star.comp.Impress.oox.PowerPointExport", aArguments), UNO_QUERY);
235 if (Reference<XFilter> xFilter{ xExporter, UNO_QUERY })
237 Reference<util::XLockable> xUndoManager;
238 bool bWasUnLocked = true;
239 if (Reference<document::XUndoManagerSupplier> xUMS{ getModel(), UNO_QUERY })
241 xUndoManager = xUMS->getUndoManager();
242 if (xUndoManager.is())
244 bWasUnLocked = !xUndoManager->isLocked();
245 xUndoManager->lock();
248 comphelper::ScopeGuard aGuard([xUndoManager, bWasUnLocked] {
249 if (xUndoManager && bWasUnLocked)
250 xUndoManager->unlock();
253 Reference< XComponent > xDocument(getModel(), UNO_QUERY);
254 xExporter->setSourceDocument(xDocument);
255 if (xFilter->filter(rDescriptor))
256 return true;
260 return false;
263 ::oox::vml::Drawing* PowerPointImport::getVmlDrawing()
265 return mpActualSlidePersist ? mpActualSlidePersist->getDrawing() : nullptr;
268 oox::drawingml::table::TableStyleListPtr PowerPointImport::getTableStyles()
270 if ( !mpTableStyleList && !maTableStyleListPath.isEmpty() )
272 mpTableStyleList = std::make_shared<oox::drawingml::table::TableStyleList>( );
273 importFragment( new oox::drawingml::table::TableStyleListFragmentHandler(
274 *this, maTableStyleListPath, *mpTableStyleList ) );
276 return mpTableStyleList;
279 ::oox::drawingml::chart::ChartConverter* PowerPointImport::getChartConverter()
281 return mxChartConv.get();
284 namespace {
286 class PptGraphicHelper : public GraphicHelper
288 public:
289 explicit PptGraphicHelper( const PowerPointImport& rFilter );
290 virtual ::Color getSchemeColor( sal_Int32 nToken ) const override;
291 virtual sal_Int32 getDefaultChartAreaFillStyle() const override;
292 private:
293 const PowerPointImport& mrFilter;
296 PptGraphicHelper::PptGraphicHelper( const PowerPointImport& rFilter ) :
297 GraphicHelper( rFilter.getComponentContext(), rFilter.getTargetFrame(), rFilter.getStorage() ),
298 mrFilter( rFilter )
302 ::Color PptGraphicHelper::getSchemeColor( sal_Int32 nToken ) const
304 return mrFilter.getSchemeColor( nToken );
307 sal_Int32 PptGraphicHelper::getDefaultChartAreaFillStyle() const
309 return XML_noFill;
312 } // namespace
314 GraphicHelper* PowerPointImport::implCreateGraphicHelper() const
316 return new PptGraphicHelper( *this );
319 ::oox::ole::VbaProject* PowerPointImport::implCreateVbaProject() const
321 return new ::oox::ole::VbaProject( getComponentContext(), getModel(), "Impress" );
324 OUString PowerPointImport::getImplementationName()
326 return "com.sun.star.comp.oox.ppt.PowerPointImport";
331 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
332 com_sun_star_comp_oox_ppt_PowerPointImport_get_implementation(
333 uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
335 return cppu::acquire(new oox::ppt::PowerPointImport(pCtx));
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */