tdf#131098 docx export: write fill property of graphic
[LibreOffice.git] / sd / source / filter / sdpptwrp.cxx
blob4f701382459287b140331e577793efa6faa65f98
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 <sfx2/docfile.hxx>
21 #include <sfx2/docfilt.hxx>
22 #include <sfx2/sfxsids.hrc>
23 #include <filter/msfilter/msoleexp.hxx>
24 #include <svx/svxerr.hxx>
25 #include <unotools/streamwrap.hxx>
26 #include <sot/storage.hxx>
27 #include <comphelper/sequenceashashmap.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <officecfg/Office/Impress.hxx>
30 #include <officecfg/Office/Common.hxx>
32 #include <com/sun/star/packages/XPackageEncryption.hpp>
33 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <sdpptwrp.hxx>
36 #include <DrawDocShell.hxx>
37 #include <sfx2/frame.hxx>
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::task;
42 using namespace ::com::sun::star::frame;
44 SdPPTFilter::SdPPTFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell ) :
45 SdFilter( rMedium, rDocShell ),
46 pBas ( nullptr )
50 SdPPTFilter::~SdPPTFilter()
52 delete pBas; // deleting the compressed basic storage
55 static void lcl_getListOfStreams(SotStorage * pStorage, comphelper::SequenceAsHashMap& aStreamsData, std::u16string_view sPrefix)
57 SvStorageInfoList aElements;
58 pStorage->FillInfoList(&aElements);
59 for (const auto & aElement : aElements)
61 OUString sStreamFullName = sPrefix.size() ? OUString::Concat(sPrefix) + "/" + aElement.GetName() : aElement.GetName();
62 if (aElement.IsStorage())
64 rtl::Reference<SotStorage> xSubStorage = pStorage->OpenSotStorage(aElement.GetName(), StreamMode::STD_READ | StreamMode::SHARE_DENYALL);
65 lcl_getListOfStreams(xSubStorage.get(), aStreamsData, sStreamFullName);
67 else
69 // Read stream
70 rtl::Reference<SotStorageStream> rStream = pStorage->OpenSotStream(aElement.GetName(), StreamMode::READ | StreamMode::SHARE_DENYALL);
71 if (rStream.is())
73 sal_Int32 nStreamSize = rStream->GetSize();
74 Sequence< sal_Int8 > oData;
75 oData.realloc(nStreamSize);
76 sal_Int32 nReadBytes = rStream->ReadBytes(oData.getArray(), nStreamSize);
77 if (nStreamSize == nReadBytes)
78 aStreamsData[sStreamFullName] <<= oData;
84 static rtl::Reference<SotStorage> lcl_DRMDecrypt(const SfxMedium& rMedium, const rtl::Reference<SotStorage>& rStorage, std::shared_ptr<SvStream>& rNewStorageStrm)
86 rtl::Reference<SotStorage> aNewStorage;
88 // We have DRM encrypted storage. We should try to decrypt it first, if we can
89 Sequence< Any > aArguments;
90 Reference<XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
91 Reference< css::packages::XPackageEncryption > xPackageEncryption(
92 xComponentContext->getServiceManager()->createInstanceWithArgumentsAndContext(
93 u"com.sun.star.comp.oox.crypto.DRMDataSpace"_ustr, aArguments, xComponentContext), UNO_QUERY);
95 if (!xPackageEncryption.is())
97 // We do not know how to decrypt this
98 return aNewStorage;
101 comphelper::SequenceAsHashMap aStreamsData;
102 lcl_getListOfStreams(rStorage.get(), aStreamsData, u"");
104 try {
105 Sequence<NamedValue> aStreams = aStreamsData.getAsConstNamedValueList();
106 if (!xPackageEncryption->readEncryptionInfo(aStreams))
108 // We failed with decryption
109 return aNewStorage;
112 rtl::Reference<SotStorageStream> rContentStream = rStorage->OpenSotStream(u"\011DRMContent"_ustr, StreamMode::READ | StreamMode::SHARE_DENYALL);
113 if (!rContentStream.is())
115 return aNewStorage;
118 rNewStorageStrm = std::make_shared<SvMemoryStream>();
120 Reference<css::io::XInputStream > xInputStream(new utl::OSeekableInputStreamWrapper(rContentStream.get(), false));
121 Reference<css::io::XOutputStream > xDecryptedStream(new utl::OSeekableOutputStreamWrapper(*rNewStorageStrm));
123 if (!xPackageEncryption->decrypt(xInputStream, xDecryptedStream))
125 // We failed with decryption
126 return aNewStorage;
129 rNewStorageStrm->Seek(0);
131 // Further reading is done from new document
132 aNewStorage = new SotStorage(*rNewStorageStrm);
134 // Set the media descriptor data
135 Sequence<NamedValue> aEncryptionData = xPackageEncryption->createEncryptionData(u""_ustr);
136 rMedium.GetItemSet().Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, Any(aEncryptionData)));
138 catch (const std::exception&)
140 return aNewStorage;
143 return aNewStorage;
146 bool SdPPTFilter::Import()
148 bool bRet = false;
149 std::shared_ptr<SvStream> aDecryptedStorageStrm;
150 rtl::Reference<SotStorage> pStorage = new SotStorage(mrMedium.GetInStream(), false);
151 if( !pStorage->GetError() )
153 /* check if there is a dualstorage, then the
154 document is probably a PPT95 containing PPT97 */
155 rtl::Reference<SotStorage> xDualStorage;
156 OUString sDualStorage( u"PP97_DUALSTORAGE"_ustr );
157 if ( pStorage->IsContained( sDualStorage ) )
159 xDualStorage = pStorage->OpenSotStorage( sDualStorage, StreamMode::STD_READ );
160 pStorage = xDualStorage;
162 if (pStorage->IsContained(u"\011DRMContent"_ustr))
164 // Document is DRM encrypted
165 pStorage = lcl_DRMDecrypt(mrMedium, pStorage, aDecryptedStorageStrm);
167 rtl::Reference<SotStorageStream> pDocStream(pStorage->OpenSotStream( u"PowerPoint Document"_ustr , StreamMode::STD_READ ));
168 if( pDocStream )
170 pDocStream->SetVersion( pStorage->GetVersion() );
171 pDocStream->SetCryptMaskKey(pStorage->GetKey());
173 if ( pStorage->IsStream( u"EncryptedSummary"_ustr ) )
174 mrMedium.SetError(ERRCODE_SVX_READ_FILTER_PPOINT);
175 else
177 bRet = ImportPPT( &mrDocument, *pDocStream, *pStorage, mrMedium );
179 if ( !bRet )
180 mrMedium.SetError(SVSTREAM_WRONGVERSION);
185 return bRet;
188 bool SdPPTFilter::Export()
190 bool bRet = false;
192 if( mxModel.is() )
194 sal_uInt32 nCnvrtFlags = 0;
195 if ( officecfg::Office::Common::Filter::Microsoft::Export::MathToMathType::get() )
196 nCnvrtFlags |= OLE_STARMATH_2_MATHTYPE;
197 if ( officecfg::Office::Common::Filter::Microsoft::Export::WriterToWinWord::get() )
198 nCnvrtFlags |= OLE_STARWRITER_2_WINWORD;
199 if ( officecfg::Office::Common::Filter::Microsoft::Export::CalcToExcel::get() )
200 nCnvrtFlags |= OLE_STARCALC_2_EXCEL;
201 if ( officecfg::Office::Common::Filter::Microsoft::Export::ImpressToPowerPoint::get() )
202 nCnvrtFlags |= OLE_STARIMPRESS_2_POWERPOINT;
203 if ( officecfg::Office::Common::Filter::Microsoft::Export::EnablePowerPointPreview::get() )
204 nCnvrtFlags |= 0x8000;
206 CreateStatusIndicator();
208 //OUString sBaseURI( "BaseURI");
209 std::vector< PropertyValue > aProperties;
210 PropertyValue aProperty;
211 aProperty.Name = "BaseURI";
212 aProperty.Value <<= mrMedium.GetBaseURL( true );
213 aProperties.push_back( aProperty );
215 SvStream * pOutputStrm = mrMedium.GetOutStream();
217 Sequence< NamedValue > aEncryptionData;
218 Reference< css::packages::XPackageEncryption > xPackageEncryption;
219 const SfxUnoAnyItem* pEncryptionDataItem = mrMedium.GetItemSet().GetItem(SID_ENCRYPTIONDATA, false);
220 std::shared_ptr<SvStream> pMediaStrm;
221 if (pEncryptionDataItem && (pEncryptionDataItem->GetValue() >>= aEncryptionData))
223 ::comphelper::SequenceAsHashMap aHashData(aEncryptionData);
224 OUString sCryptoType = aHashData.getUnpackedValueOrDefault(u"CryptoType"_ustr, OUString());
226 if (sCryptoType.getLength())
228 Reference<XComponentContext> xComponentContext(comphelper::getProcessComponentContext());
229 Sequence<Any> aArguments{
230 Any(NamedValue(u"Binary"_ustr, Any(true))) };
231 xPackageEncryption.set(
232 xComponentContext->getServiceManager()->createInstanceWithArgumentsAndContext(
233 "com.sun.star.comp.oox.crypto." + sCryptoType, aArguments, xComponentContext), UNO_QUERY);
235 if (xPackageEncryption.is())
237 // We have an encryptor. Export document into memory stream and encrypt it later
238 pMediaStrm = std::make_shared<SvMemoryStream>();
239 pOutputStrm = pMediaStrm.get();
241 // Temp removal of EncryptionData to avoid password protection triggering
242 mrMedium.GetItemSet().ClearItem(SID_ENCRYPTIONDATA);
247 rtl::Reference<SotStorage> xStorRef = new SotStorage(pOutputStrm, false);
249 if (xStorRef.is())
251 bRet = ExportPPT(aProperties, xStorRef, mxModel, mxStatusIndicator, pBas, nCnvrtFlags);
252 xStorRef->Commit();
254 if (xPackageEncryption.is())
256 // Perform DRM encryption
257 pOutputStrm->Seek(0);
259 xPackageEncryption->setupEncryption(aEncryptionData);
261 Reference<css::io::XInputStream > xInputStream(new utl::OSeekableInputStreamWrapper(pOutputStrm, false));
262 Sequence<NamedValue> aStreams = xPackageEncryption->encrypt(xInputStream);
264 rtl::Reference<SotStorage> xEncryptedRootStrg = new SotStorage(mrMedium.GetOutStream(), false);
265 for (const NamedValue& aStreamData : aStreams)
267 // To avoid long paths split and open substorages recursively
268 // Splitting paths manually, since comphelper::string::split is trimming special characters like \0x01, \0x09
269 rtl::Reference<SotStorage> pStorage = xEncryptedRootStrg;
270 OUString sFileName;
271 sal_Int32 idx = 0;
274 OUString sPathElem = aStreamData.Name.getToken(0, L'/', idx);
275 if (!sPathElem.isEmpty())
277 if (idx < 0)
279 sFileName = sPathElem;
281 else
283 pStorage = pStorage->OpenSotStorage(sPathElem);
286 } while (pStorage && idx >= 0);
288 if (!pStorage)
290 bRet = false;
291 break;
294 rtl::Reference<SotStorageStream> pStream = pStorage->OpenSotStream(sFileName);
295 if (!pStream)
297 bRet = false;
298 break;
300 Sequence<sal_Int8> aStreamContent;
301 aStreamData.Value >>= aStreamContent;
302 size_t nBytesWritten = pStream->WriteBytes(aStreamContent.getConstArray(), aStreamContent.getLength());
303 if (nBytesWritten != static_cast<size_t>(aStreamContent.getLength()))
305 bRet = false;
306 break;
309 xEncryptedRootStrg->Commit();
311 // Restore encryption data
312 mrMedium.GetItemSet().Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, Any(aEncryptionData)));
317 return bRet;
320 void SdPPTFilter::PreSaveBasic()
322 if( officecfg::Office::Impress::Filter::Import::VBA::Save::get() )
324 SaveVBA( static_cast<SfxObjectShell&>(mrDocShell), pBas );
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */