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>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/uno/XComponentContext.hpp>
24 #include <com/sun/star/document/XUndoManager.hpp>
25 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
26 #include <comphelper/propertysequence.hxx>
27 #include <osl/diagnose.h>
28 #include <vcl/svapp.hxx>
29 #include <vcl/weld.hxx>
30 #include <svtools/sfxecode.hxx>
31 #include <svtools/ehdl.hxx>
32 #include <tools/urlobj.hxx>
33 #include <svx/dialmgr.hxx>
34 #include <svx/strings.hrc>
35 #include <oox/ppt/pptimport.hxx>
36 #include <oox/drawingml/chart/chartconverter.hxx>
37 #include <oox/dump/pptxdumper.hxx>
38 #include <drawingml/table/tablestylelistfragmenthandler.hxx>
39 #include <oox/helper/graphichelper.hxx>
40 #include <oox/ole/vbaproject.hxx>
41 #include <oox/ppt/presentationfragmenthandler.hxx>
42 #include <oox/token/tokens.hxx>
44 #include <services.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 OUString
PowerPointImport_getImplementationName()
58 return OUString( "com.sun.star.comp.oox.ppt.PowerPointImport" );
61 uno::Sequence
< OUString
> PowerPointImport_getSupportedServiceNames()
63 Sequence
< OUString
> aSeq( 2 );
64 aSeq
[ 0 ] = "com.sun.star.document.ImportFilter";
65 aSeq
[ 1 ] = "com.sun.star.document.ExportFilter";
69 uno::Reference
< uno::XInterface
> PowerPointImport_createInstance( const Reference
< XComponentContext
>& rxContext
)
71 return static_cast< ::cppu::OWeakObject
* >( new PowerPointImport( rxContext
) );
74 #if OSL_DEBUG_LEVEL > 0
75 XmlFilterBase
* PowerPointImport::mpDebugFilterBase
= nullptr;
78 PowerPointImport::PowerPointImport( const Reference
< XComponentContext
>& rxContext
) :
79 XmlFilterBase( rxContext
),
80 mxChartConv( new ::oox::drawingml::chart::ChartConverter
)
83 #if OSL_DEBUG_LEVEL > 0
84 mpDebugFilterBase
= this;
88 PowerPointImport::~PowerPointImport()
92 /// Visits the relations from pRelations which are of type rType.
93 static void visitRelations(PowerPointImport
& rImport
, const core::RelationsRef
& pRelations
, const OUString
& rType
, std::vector
<OUString
>& rImageFragments
)
95 if (core::RelationsRef pRelationsOfType
= pRelations
->getRelationsFromTypeFromOfficeDoc(rType
))
97 for (const auto& rRelation
: *pRelationsOfType
)
99 OUString aFragment
= pRelationsOfType
->getFragmentPathFromRelation(rRelation
.second
);
100 if (core::RelationsRef pFragmentRelations
= rImport
.importRelations(aFragment
))
102 // See if the fragment has images.
103 if (core::RelationsRef pImages
= pFragmentRelations
->getRelationsFromTypeFromOfficeDoc("image"))
105 for (const auto& rImage
: *pImages
)
107 OUString aPath
= pImages
->getFragmentPathFromRelation(rImage
.second
);
108 // Safe subset: e.g. WMF may have an external header from the
109 // referencing fragment.
110 if (aPath
.endsWith(".jpg") || aPath
.endsWith(".jpeg"))
111 rImageFragments
.push_back(aPath
);
115 // See if the fragment has a slide layout, and recurse.
116 visitRelations(rImport
, pFragmentRelations
, "slideLayout", rImageFragments
);
122 bool PowerPointImport::importDocument()
124 /* to activate the PPTX dumper, define the environment variable
125 OOO_PPTXDUMPER and insert the full path to the file
126 file:///<path-to-oox-module>/source/dump/pptxdumper.ini. */
127 OOX_DUMP_FILE( ::oox::dump::pptx::Dumper
);
129 uno::Reference
< document::XUndoManagerSupplier
> xUndoManagerSupplier (getModel(), UNO_QUERY
);
130 uno::Reference
< util::XLockable
> xUndoManager
;
131 bool bWasUnLocked
= true;
132 if(xUndoManagerSupplier
.is())
134 xUndoManager
= xUndoManagerSupplier
->getUndoManager();
135 if(xUndoManager
.is())
137 bWasUnLocked
= !xUndoManager
->isLocked();
138 xUndoManager
->lock();
142 importDocumentProperties();
144 OUString aFragmentPath
= getFragmentPathFromFirstTypeFromOfficeDoc( "officeDocument" );
145 FragmentHandlerRef
xPresentationFragmentHandler( new PresentationFragmentHandler( *this, aFragmentPath
) );
146 maTableStyleListPath
= xPresentationFragmentHandler
->getFragmentPathFromFirstTypeFromOfficeDoc( "tableStyles" );
148 // importRelations() is cheap, it will do an actual import for the first time only.
149 if (core::RelationsRef pFragmentRelations
= importRelations(aFragmentPath
))
151 std::vector
<OUString
> aImageFragments
;
152 visitRelations(*this, pFragmentRelations
, "slide", aImageFragments
);
153 visitRelations(*this, pFragmentRelations
, "slideMaster", aImageFragments
);
155 getGraphicHelper().importEmbeddedGraphics(aImageFragments
);
158 bool bRet
= importFragment(xPresentationFragmentHandler
);
160 if (mbMissingExtDrawing
)
162 // Construct a warning message.
163 INetURLObject
aURL(getFileUrl());
164 SfxErrorContext
aContext(ERRCTX_SFX_OPENDOC
, aURL
.getName(INetURLObject::LAST_SEGMENT
, true, INetURLObject::DecodeMechanism::WithCharset
), nullptr, RID_ERRCTX
);
166 aContext
.GetString(ERRCODE_NONE
.MakeWarning(), aWarning
);
168 aWarning
+= SvxResId(RID_SVXSTR_WARN_MISSING_SMARTART
);
171 std::unique_ptr
<weld::MessageDialog
> xWarn(Application::CreateMessageDialog(nullptr,
172 VclMessageType::Warning
, VclButtonsType::Ok
,
177 if(xUndoManager
.is() && bWasUnLocked
)
178 xUndoManager
->unlock();
184 bool PowerPointImport::exportDocument() throw()
189 ::Color
PowerPointImport::getSchemeColor( sal_Int32 nToken
) const
192 if ( mpActualSlidePersist
)
194 bool bColorMapped
= false;
195 oox::drawingml::ClrMapPtr
pClrMapPtr( mpActualSlidePersist
->getClrMap() );
197 bColorMapped
= pClrMapPtr
->getColorMap( nToken
);
199 if ( !bColorMapped
) // try masterpage mapping
201 SlidePersistPtr pMasterPersist
= mpActualSlidePersist
->getMasterPersist();
202 if ( pMasterPersist
)
204 pClrMapPtr
= pMasterPersist
->getClrMap();
206 pClrMapPtr
->getColorMap( nToken
);
210 ::oox::drawingml::ThemePtr pTheme
= mpActualSlidePersist
->getTheme();
213 pTheme
->getClrScheme().getColor( nToken
, nColor
);
217 SAL_WARN("oox", "OOX: PowerPointImport::mpThemePtr is NULL");
223 const ::oox::drawingml::Theme
* PowerPointImport::getCurrentTheme() const
225 return mpActualSlidePersist
? mpActualSlidePersist
->getTheme().get() : nullptr;
228 sal_Bool SAL_CALL
PowerPointImport::filter( const Sequence
< PropertyValue
>& rDescriptor
)
230 if( XmlFilterBase::filter( rDescriptor
) )
233 if (isExportFilter())
235 uno::Sequence
<uno::Any
> aArguments(comphelper::InitAnyPropertySequence(
237 {"IsPPTM", uno::makeAny(exportVBA())},
240 Reference
<css::lang::XMultiServiceFactory
> aFactory(getComponentContext()->getServiceManager(), UNO_QUERY_THROW
);
241 Reference
< XExporter
> xExporter(aFactory
->createInstanceWithArguments("com.sun.star.comp.Impress.oox.PowerPointExport", aArguments
), UNO_QUERY
);
245 Reference
< XComponent
> xDocument( getModel(), UNO_QUERY
);
246 Reference
< XFilter
> xFilter( xExporter
, UNO_QUERY
);
250 xExporter
->setSourceDocument( xDocument
);
251 if( xFilter
->filter( rDescriptor
) )
260 ::oox::vml::Drawing
* PowerPointImport::getVmlDrawing()
262 return mpActualSlidePersist
? mpActualSlidePersist
->getDrawing() : nullptr;
265 const oox::drawingml::table::TableStyleListPtr
PowerPointImport::getTableStyles()
267 if ( !mpTableStyleList
&& !maTableStyleListPath
.isEmpty() )
269 mpTableStyleList
= std::make_shared
<oox::drawingml::table::TableStyleList
>( );
270 importFragment( new oox::drawingml::table::TableStyleListFragmentHandler(
271 *this, maTableStyleListPath
, *mpTableStyleList
) );
273 return mpTableStyleList
;
276 ::oox::drawingml::chart::ChartConverter
* PowerPointImport::getChartConverter()
278 return mxChartConv
.get();
283 class PptGraphicHelper
: public GraphicHelper
286 explicit PptGraphicHelper( const PowerPointImport
& rFilter
);
287 virtual ::Color
getSchemeColor( sal_Int32 nToken
) const override
;
288 virtual sal_Int32
getDefaultChartAreaFillStyle() const override
;
290 const PowerPointImport
& mrFilter
;
293 PptGraphicHelper::PptGraphicHelper( const PowerPointImport
& rFilter
) :
294 GraphicHelper( rFilter
.getComponentContext(), rFilter
.getTargetFrame(), rFilter
.getStorage() ),
299 ::Color
PptGraphicHelper::getSchemeColor( sal_Int32 nToken
) const
301 return mrFilter
.getSchemeColor( nToken
);
304 sal_Int32
PptGraphicHelper::getDefaultChartAreaFillStyle() const
311 GraphicHelper
* PowerPointImport::implCreateGraphicHelper() const
313 return new PptGraphicHelper( *this );
316 ::oox::ole::VbaProject
* PowerPointImport::implCreateVbaProject() const
318 return new ::oox::ole::VbaProject( getComponentContext(), getModel(), "Impress" );
321 OUString
PowerPointImport::getImplementationName()
323 return PowerPointImport_getImplementationName();
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */