LibreOffice Module oox (master)  1
pptimport.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include <com/sun/star/frame/XModel.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/document/XUndoManager.hpp>
27 #include <com/sun/star/document/XUndoManagerSupplier.hpp>
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>
39 #include <oox/dump/pptxdumper.hxx>
42 #include <oox/ole/vbaproject.hxx>
45 #include <oox/token/tokens.hxx>
46 
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;
51 
52 using ::com::sun::star::beans::PropertyValue;
53 using ::com::sun::star::lang::XComponent;
54 
55 namespace oox::ppt {
56 
57 #if OSL_DEBUG_LEVEL > 0
58 XmlFilterBase* PowerPointImport::mpDebugFilterBase = nullptr;
59 #endif
60 
61 PowerPointImport::PowerPointImport( const Reference< XComponentContext >& rxContext ) :
62  XmlFilterBase( rxContext ),
63  mxChartConv( std::make_shared<::oox::drawingml::chart::ChartConverter>() )
64 
65 {
66 #if OSL_DEBUG_LEVEL > 0
67  mpDebugFilterBase = this;
68 #endif
69 }
70 
72 {
73 }
74 
76 {
77  /* to activate the PPTX dumper, define the environment variable
78  OOO_PPTXDUMPER and insert the full path to the file
79  file:///<path-to-oox-module>/source/dump/pptxdumper.ini. */
81 
82  uno::Reference< document::XUndoManagerSupplier > xUndoManagerSupplier (getModel(), UNO_QUERY );
83  uno::Reference< util::XLockable > xUndoManager;
84  bool bWasUnLocked = true;
85  if(xUndoManagerSupplier.is())
86  {
87  xUndoManager = xUndoManagerSupplier->getUndoManager();
88  if(xUndoManager.is())
89  {
90  bWasUnLocked = !xUndoManager->isLocked();
91  xUndoManager->lock();
92  }
93  }
94 
96 
97  OUString aFragmentPath = getFragmentPathFromFirstTypeFromOfficeDoc( u"officeDocument" );
98  FragmentHandlerRef xPresentationFragmentHandler( new PresentationFragmentHandler( *this, aFragmentPath ) );
99  maTableStyleListPath = xPresentationFragmentHandler->getFragmentPathFromFirstTypeFromOfficeDoc( u"tableStyles" );
100  const OUString sPresPropsPath
101  = xPresentationFragmentHandler->getFragmentPathFromFirstTypeFromOfficeDoc(u"presProps");
102 
103  bool bRet = importFragment(xPresentationFragmentHandler);
104  if (bRet && !sPresPropsPath.isEmpty())
105  {
106  FragmentHandlerRef xPresPropsFragmentHandler(
107  new PresPropsFragmentHandler(*this, sPresPropsPath));
108  importFragment(xPresPropsFragmentHandler);
109  }
110 
111  static bool bNoSmartartWarning = getenv("OOX_NO_SMARTART_WARNING");
112  if (!bNoSmartartWarning && mbMissingExtDrawing)
113  {
114  // Construct a warning message.
117  OUString aWarning;
118  aContext.GetString(ERRCODE_NONE.MakeWarning(), aWarning);
119  aWarning += ":\n" + SvxResId(RID_SVXSTR_WARN_MISSING_SMARTART);
120 
121  // Show it.
122  std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(nullptr,
123  VclMessageType::Warning, VclButtonsType::Ok,
124  aWarning));
125  xWarn->run();
126  }
127 
128  if(xUndoManager.is() && bWasUnLocked)
129  xUndoManager->unlock();
130 
131  return bRet;
132 
133 }
134 
136 {
137  return false;
138 }
139 
140 ::Color PowerPointImport::getSchemeColor( sal_Int32 nToken ) const
141 {
142  ::Color nColor;
143  if ( mpActualSlidePersist )
144  {
145  bool bColorMapped = false;
146  oox::drawingml::ClrMapPtr pClrMapPtr( mpActualSlidePersist->getClrMap() );
147  if ( pClrMapPtr )
148  bColorMapped = pClrMapPtr->getColorMap( nToken );
149 
150  if ( !bColorMapped ) // try masterpage mapping
151  {
152  SlidePersistPtr pMasterPersist = mpActualSlidePersist->getMasterPersist();
153  if ( pMasterPersist )
154  {
155  pClrMapPtr = pMasterPersist->getClrMap();
156  if ( pClrMapPtr )
157  pClrMapPtr->getColorMap( nToken );
158  }
159  }
160 
162  if( pTheme )
163  {
164  pTheme->getClrScheme().getColor( nToken, nColor );
165  }
166  else
167  {
168  SAL_WARN("oox", "OOX: PowerPointImport::mpThemePtr is NULL");
169  }
170  }
171  return nColor;
172 }
173 
174 const ::oox::drawingml::Theme* PowerPointImport::getCurrentTheme() const
175 {
176  return mpActualSlidePersist ? mpActualSlidePersist->getTheme().get() : nullptr;
177 }
178 
179 sal_Bool SAL_CALL PowerPointImport::filter( const Sequence< PropertyValue >& rDescriptor )
180 {
181  if( XmlFilterBase::filter( rDescriptor ) )
182  return true;
183 
184  if (isExportFilter())
185  {
186  uno::Sequence<uno::Any> aArguments(comphelper::InitAnyPropertySequence(
187  {
188  {"IsPPTM", uno::makeAny(exportVBA())},
189  {"IsTemplate", uno::makeAny(isExportTemplate())},
190  }));
191 
192  Reference<css::lang::XMultiServiceFactory> aFactory(getComponentContext()->getServiceManager(), UNO_QUERY_THROW);
193  Reference< XExporter > xExporter(aFactory->createInstanceWithArguments("com.sun.star.comp.Impress.oox.PowerPointExport", aArguments), UNO_QUERY);
194 
195  if (Reference<XFilter> xFilter{ xExporter, UNO_QUERY })
196  {
197  Reference<util::XLockable> xUndoManager;
198  bool bWasUnLocked = true;
199  if (Reference<document::XUndoManagerSupplier> xUMS{ getModel(), UNO_QUERY })
200  {
201  xUndoManager = xUMS->getUndoManager();
202  if (xUndoManager.is())
203  {
204  bWasUnLocked = !xUndoManager->isLocked();
205  xUndoManager->lock();
206  }
207  }
208  comphelper::ScopeGuard aGuard([xUndoManager, bWasUnLocked] {
209  if (xUndoManager && bWasUnLocked)
210  xUndoManager->unlock();
211  });
212 
213  Reference< XComponent > xDocument = getModel();
214  xExporter->setSourceDocument(xDocument);
215  if (xFilter->filter(rDescriptor))
216  return true;
217  }
218  }
219 
220  return false;
221 }
222 
224 {
225  return mpActualSlidePersist ? mpActualSlidePersist->getDrawing() : nullptr;
226 }
227 
229 {
230  if ( !mpTableStyleList && !maTableStyleListPath.isEmpty() )
231  {
232  mpTableStyleList = std::make_shared<oox::drawingml::table::TableStyleList>( );
235  }
236  return mpTableStyleList;
237 }
238 
240 {
241  return mxChartConv.get();
242 }
243 
244 namespace {
245 
246 class PptGraphicHelper : public GraphicHelper
247 {
248 public:
249  explicit PptGraphicHelper( const PowerPointImport& rFilter );
250  virtual ::Color getSchemeColor( sal_Int32 nToken ) const override;
251  virtual sal_Int32 getDefaultChartAreaFillStyle() const override;
252 private:
253  const PowerPointImport& mrFilter;
254 };
255 
256 PptGraphicHelper::PptGraphicHelper( const PowerPointImport& rFilter ) :
257  GraphicHelper( rFilter.getComponentContext(), rFilter.getTargetFrame(), rFilter.getStorage() ),
258  mrFilter( rFilter )
259 {
260 }
261 
262 ::Color PptGraphicHelper::getSchemeColor( sal_Int32 nToken ) const
263 {
264  return mrFilter.getSchemeColor( nToken );
265 }
266 
267 sal_Int32 PptGraphicHelper::getDefaultChartAreaFillStyle() const
268 {
269  return XML_noFill;
270 }
271 
272 } // namespace
273 
274 GraphicHelper* PowerPointImport::implCreateGraphicHelper() const
275 {
276  return new PptGraphicHelper( *this );
277 }
278 
279 ::oox::ole::VbaProject* PowerPointImport::implCreateVbaProject() const
280 {
281  return new ::oox::ole::VbaProject( getComponentContext(), getModel(), u"Impress" );
282 }
283 
284 OUString PowerPointImport::getImplementationName()
285 {
286  return "com.sun.star.comp.oox.ppt.PowerPointImport";
287 }
288 
289 }
290 
291 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
293  uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
294 {
295  return cppu::acquire(new oox::ppt::PowerPointImport(pCtx));
296 }
297 
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Provides helper functions for colors, device measurement conversion, graphics, and graphic objects ha...
bool exportVBA() const
Definition: filterbase.cxx:581
virtual ::oox::drawingml::chart::ChartConverter * getChartConverter() override
Has to be implemented by each filter, returns a filter-specific chart converter object, that should be global per imported document.
Definition: pptimport.cxx:239
URL aURL
std::shared_ptr< TableStyleList > TableStyleListPtr
virtual ~PowerPointImport() override
Definition: pptimport.cxx:71
bool importFragment(const rtl::Reference< FragmentHandler > &rxHandler)
Imports a fragment using the passed fragment handler, which contains the full path to the fragment st...
std::shared_ptr< T > make_shared(Args &&...args)
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &rDescriptor) override
Definition: pptimport.cxx:179
virtual const ::oox::drawingml::Theme * getCurrentTheme() const override
Has to be implemented by each filter, returns the current theme.
Definition: pptimport.cxx:174
virtual bool importDocument() override
Derived classes implement import of the entire document.
Definition: pptimport.cxx:75
bool isExportTemplate() const
Definition: filterbase.cxx:586
oox::drawingml::table::TableStyleListPtr mpTableStyleList
Definition: pptimport.hxx:91
Sequence< PropertyValue > aArguments
OUString SvxResId(TranslateId aId)
bool isExportFilter() const
Returns true, if filter is an export filter.
Definition: filterbase.cxx:202
static XmlFilterBase * mpDebugFilterBase
Definition: pptimport.hxx:81
#define ERRCTX_SFX_OPENDOC
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
void importDocumentProperties()
Read the document properties and also the customXml entries (xlsx and pptx only). ...
OUString getName(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
SlidePersistPtr mpActualSlidePersist
Definition: pptimport.hxx:93
float u
unsigned char sal_Bool
const css::uno::Reference< css::uno::XComponentContext > & getComponentContext() const
Returns the component context passed in the filter constructor (always existing). ...
Definition: filterbase.cxx:212
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_oox_ppt_PowerPointImport_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
Definition: pptimport.cxx:292
const css::uno::Reference< css::frame::XModel > & getModel() const
Returns the document model (always existing).
Definition: filterbase.cxx:217
SVT_DLLPUBLIC const ErrMsgCode RID_ERRCTX[]
const OUString & getFileUrl() const
Returns the URL of the imported or exported file.
Definition: filterbase.cxx:247
#define ERRCODE_NONE
virtual oox::drawingml::table::TableStyleListPtr getTableStyles() override
Has to be implemented by each filter to return the table style list.
Definition: pptimport.cxx:228
virtual ::oox::vml::Drawing * getVmlDrawing() override
Has to be implemented by each filter to return the collection of VML shapes.
Definition: pptimport.cxx:223
std::shared_ptr< ClrMap > ClrMapPtr
Definition: clrscheme.hxx:78
#define OOX_DUMP_FILE(DumperClassName)
std::shared_ptr< SlidePersist > SlidePersistPtr
std::shared_ptr< ::oox::drawingml::chart::ChartConverter > mxChartConv
Definition: pptimport.hxx:100
OUString getFragmentPathFromFirstTypeFromOfficeDoc(std::u16string_view rPart)
Represents the collection of VML shapes for a complete draw page.
Definition: vmldrawing.hxx:93
virtual bool exportDocument() noexcept override
Derived classes implement export of the entire document.
Definition: pptimport.cxx:135
#define SAL_WARN(area, stream)
::Color getSchemeColor(sal_Int32 nToken) const
Definition: pptimport.cxx:140
const PowerPointImport & mrFilter
Definition: pptimport.cxx:253
std::shared_ptr< Theme > ThemePtr
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
bool GetString(ErrCode nErrId, OUString &rStr) override