1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/diagnose.h>
29 #include <vcl/svapp.hxx>
30 #include <vcl/weld.hxx>
31 #include <svtools/sfxecode.hxx>
32 #include <svtools/ehdl.hxx>
33 #include <tools/urlobj.hxx>
34 #include <svx/dialmgr.hxx>
35 #include <svx/strings.hrc>
36 #include <oox/ppt/pptimport.hxx>
37 #include <oox/drawingml/chart/chartconverter.hxx>
38 #include <oox/dump/pptxdumper.hxx>
39 #include <drawingml/table/tablestylelistfragmenthandler.hxx>
40 #include <oox/helper/graphichelper.hxx>
41 #include <oox/ole/vbaproject.hxx>
42 #include <oox/ppt/presentationfragmenthandler.hxx>
43 #include <oox/ppt/presPropsfragmenthandler.hxx>
44 #include <oox/token/tokens.hxx>
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::xml::sax
;
49 using namespace oox::core
;
51 using ::com::sun::star::beans::PropertyValue
;
52 using ::com::sun::star::lang::XComponent
;
54 namespace oox
{ namespace ppt
{
56 #if OSL_DEBUG_LEVEL > 0
57 XmlFilterBase
* PowerPointImport::mpDebugFilterBase
= nullptr;
60 PowerPointImport::PowerPointImport( const Reference
< XComponentContext
>& rxContext
) :
61 XmlFilterBase( rxContext
),
62 mxChartConv( new ::oox::drawingml::chart::ChartConverter
)
65 #if OSL_DEBUG_LEVEL > 0
66 mpDebugFilterBase
= this;
70 PowerPointImport::~PowerPointImport()
74 /// Visits the relations from pRelations which are of type rType.
75 static void visitRelations(PowerPointImport
& rImport
, const core::RelationsRef
& pRelations
, const OUString
& rType
, std::vector
<OUString
>& rImageFragments
)
77 if (core::RelationsRef pRelationsOfType
= pRelations
->getRelationsFromTypeFromOfficeDoc(rType
))
79 for (const auto& rRelation
: *pRelationsOfType
)
81 OUString aFragment
= pRelationsOfType
->getFragmentPathFromRelation(rRelation
.second
);
82 if (core::RelationsRef pFragmentRelations
= rImport
.importRelations(aFragment
))
84 // See if the fragment has images.
85 if (core::RelationsRef pImages
= pFragmentRelations
->getRelationsFromTypeFromOfficeDoc("image"))
87 for (const auto& rImage
: *pImages
)
89 OUString aPath
= pImages
->getFragmentPathFromRelation(rImage
.second
);
90 // Safe subset: e.g. WMF may have an external header from the
91 // referencing fragment.
92 if (aPath
.endsWith(".jpg") || aPath
.endsWith(".jpeg"))
93 rImageFragments
.push_back(aPath
);
97 // See if the fragment has a slide layout, and recurse.
98 visitRelations(rImport
, pFragmentRelations
, "slideLayout", rImageFragments
);
104 bool PowerPointImport::importDocument()
106 /* to activate the PPTX dumper, define the environment variable
107 OOO_PPTXDUMPER and insert the full path to the file
108 file:///<path-to-oox-module>/source/dump/pptxdumper.ini. */
109 OOX_DUMP_FILE( ::oox::dump::pptx::Dumper
);
111 uno::Reference
< document::XUndoManagerSupplier
> xUndoManagerSupplier (getModel(), UNO_QUERY
);
112 uno::Reference
< util::XLockable
> xUndoManager
;
113 bool bWasUnLocked
= true;
114 if(xUndoManagerSupplier
.is())
116 xUndoManager
= xUndoManagerSupplier
->getUndoManager();
117 if(xUndoManager
.is())
119 bWasUnLocked
= !xUndoManager
->isLocked();
120 xUndoManager
->lock();
124 importDocumentProperties();
126 OUString aFragmentPath
= getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
127 FragmentHandlerRef
xPresentationFragmentHandler( new PresentationFragmentHandler( *this, aFragmentPath
) );
128 maTableStyleListPath
= xPresentationFragmentHandler
->getFragmentPathFromFirstTypeFromOfficeDoc( "tableStyles" );
129 const OUString sPresPropsPath
130 = xPresentationFragmentHandler
->getFragmentPathFromFirstTypeFromOfficeDoc("presProps");
132 // importRelations() is cheap, it will do an actual import for the first time only.
133 if (core::RelationsRef pFragmentRelations
= importRelations(aFragmentPath
))
135 std::vector
<OUString
> aImageFragments
;
136 visitRelations(*this, pFragmentRelations
, "slide", aImageFragments
);
137 visitRelations(*this, pFragmentRelations
, "slideMaster", aImageFragments
);
139 getGraphicHelper().importEmbeddedGraphics(aImageFragments
);
142 bool bRet
= importFragment(xPresentationFragmentHandler
);
143 if (bRet
&& !sPresPropsPath
.isEmpty())
145 FragmentHandlerRef
xPresPropsFragmentHandler(
146 new PresPropsFragmentHandler(*this, sPresPropsPath
));
147 importFragment(xPresPropsFragmentHandler
);
150 static bool bNoSmartartWarning
= getenv("OOX_NO_SMARTART_WARNING");
151 if (!bNoSmartartWarning
&& mbMissingExtDrawing
)
153 // Construct a warning message.
154 INetURLObject
aURL(getFileUrl());
155 SfxErrorContext
aContext(ERRCTX_SFX_OPENDOC
, aURL
.getName(INetURLObject::LAST_SEGMENT
, true, INetURLObject::DecodeMechanism::WithCharset
), nullptr, RID_ERRCTX
);
157 aContext
.GetString(ERRCODE_NONE
.MakeWarning(), aWarning
);
159 aWarning
+= SvxResId(RID_SVXSTR_WARN_MISSING_SMARTART
);
162 std::unique_ptr
<weld::MessageDialog
> xWarn(Application::CreateMessageDialog(nullptr,
163 VclMessageType::Warning
, VclButtonsType::Ok
,
168 if(xUndoManager
.is() && bWasUnLocked
)
169 xUndoManager
->unlock();
175 bool PowerPointImport::exportDocument() throw()
180 ::Color
PowerPointImport::getSchemeColor( sal_Int32 nToken
) const
183 if ( mpActualSlidePersist
)
185 bool bColorMapped
= false;
186 oox::drawingml::ClrMapPtr
pClrMapPtr( mpActualSlidePersist
->getClrMap() );
188 bColorMapped
= pClrMapPtr
->getColorMap( nToken
);
190 if ( !bColorMapped
) // try masterpage mapping
192 SlidePersistPtr pMasterPersist
= mpActualSlidePersist
->getMasterPersist();
193 if ( pMasterPersist
)
195 pClrMapPtr
= pMasterPersist
->getClrMap();
197 pClrMapPtr
->getColorMap( nToken
);
201 ::oox::drawingml::ThemePtr pTheme
= mpActualSlidePersist
->getTheme();
204 pTheme
->getClrScheme().getColor( nToken
, nColor
);
208 SAL_WARN("oox", "OOX: PowerPointImport::mpThemePtr is NULL");
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
) )
224 if (isExportFilter())
226 uno::Sequence
<uno::Any
> aArguments(comphelper::InitAnyPropertySequence(
228 {"IsPPTM", uno::makeAny(exportVBA())},
229 {"IsTemplate", uno::makeAny(isExportTemplate())},
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
);
237 Reference
< XComponent
> xDocument( getModel(), UNO_QUERY
);
238 Reference
< XFilter
> xFilter( xExporter
, UNO_QUERY
);
242 xExporter
->setSourceDocument( xDocument
);
243 if( xFilter
->filter( rDescriptor
) )
252 ::oox::vml::Drawing
* PowerPointImport::getVmlDrawing()
254 return mpActualSlidePersist
? mpActualSlidePersist
->getDrawing() : nullptr;
257 const oox::drawingml::table::TableStyleListPtr
PowerPointImport::getTableStyles()
259 if ( !mpTableStyleList
&& !maTableStyleListPath
.isEmpty() )
261 mpTableStyleList
= std::make_shared
<oox::drawingml::table::TableStyleList
>( );
262 importFragment( new oox::drawingml::table::TableStyleListFragmentHandler(
263 *this, maTableStyleListPath
, *mpTableStyleList
) );
265 return mpTableStyleList
;
268 ::oox::drawingml::chart::ChartConverter
* PowerPointImport::getChartConverter()
270 return mxChartConv
.get();
275 class PptGraphicHelper
: public GraphicHelper
278 explicit PptGraphicHelper( const PowerPointImport
& rFilter
);
279 virtual ::Color
getSchemeColor( sal_Int32 nToken
) const override
;
280 virtual sal_Int32
getDefaultChartAreaFillStyle() const override
;
282 const PowerPointImport
& mrFilter
;
285 PptGraphicHelper::PptGraphicHelper( const PowerPointImport
& rFilter
) :
286 GraphicHelper( rFilter
.getComponentContext(), rFilter
.getTargetFrame(), rFilter
.getStorage() ),
291 ::Color
PptGraphicHelper::getSchemeColor( sal_Int32 nToken
) const
293 return mrFilter
.getSchemeColor( nToken
);
296 sal_Int32
PptGraphicHelper::getDefaultChartAreaFillStyle() const
303 GraphicHelper
* PowerPointImport::implCreateGraphicHelper() const
305 return new PptGraphicHelper( *this );
308 ::oox::ole::VbaProject
* PowerPointImport::implCreateVbaProject() const
310 return new ::oox::ole::VbaProject( getComponentContext(), getModel(), "Impress" );
313 OUString
PowerPointImport::getImplementationName()
315 return OUString( "com.sun.star.comp.oox.ppt.PowerPointImport" );
320 extern "C" SAL_DLLPUBLIC_EXPORT
uno::XInterface
*
321 com_sun_star_comp_oox_ppt_PowerPointImport_get_implementation(
322 uno::XComponentContext
* pCtx
, uno::Sequence
<uno::Any
> const& /*rSeq*/)
324 return cppu::acquire(new oox::ppt::PowerPointImport(pCtx
));
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */