vcl: Simplify MenuBar::ImplDestroy a bit
[LibreOffice.git] / sd / source / filter / sdpptwrp.cxx
blob5e2dd042152f5cb96591ad0556de889684e405c4
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 const 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 OUString sDualStorage( u"PP97_DUALSTORAGE"_ustr );
156 if ( pStorage->IsContained( sDualStorage ) )
158 pStorage = pStorage->OpenSotStorage(sDualStorage, StreamMode::STD_READ);
160 if (pStorage->IsContained(u"\011DRMContent"_ustr))
162 // Document is DRM encrypted
163 pStorage = lcl_DRMDecrypt(mrMedium, pStorage, aDecryptedStorageStrm);
165 rtl::Reference<SotStorageStream> pDocStream(pStorage->OpenSotStream( u"PowerPoint Document"_ustr , StreamMode::STD_READ ));
166 if( pDocStream )
168 pDocStream->SetVersion( pStorage->GetVersion() );
169 pDocStream->SetCryptMaskKey(pStorage->GetKey());
171 if ( pStorage->IsStream( u"EncryptedSummary"_ustr ) )
172 mrMedium.SetError(ERRCODE_SVX_READ_FILTER_PPOINT);
173 else
175 bRet = ImportPPT( &mrDocument, *pDocStream, *pStorage, mrMedium );
177 if ( !bRet )
178 mrMedium.SetError(SVSTREAM_WRONGVERSION);
183 return bRet;
186 bool SdPPTFilter::Export()
188 bool bRet = false;
190 if( mxModel.is() )
192 sal_uInt32 nCnvrtFlags = 0;
193 if ( officecfg::Office::Common::Filter::Microsoft::Export::MathToMathType::get() )
194 nCnvrtFlags |= OLE_STARMATH_2_MATHTYPE;
195 if ( officecfg::Office::Common::Filter::Microsoft::Export::WriterToWinWord::get() )
196 nCnvrtFlags |= OLE_STARWRITER_2_WINWORD;
197 if ( officecfg::Office::Common::Filter::Microsoft::Export::CalcToExcel::get() )
198 nCnvrtFlags |= OLE_STARCALC_2_EXCEL;
199 if ( officecfg::Office::Common::Filter::Microsoft::Export::ImpressToPowerPoint::get() )
200 nCnvrtFlags |= OLE_STARIMPRESS_2_POWERPOINT;
201 if ( officecfg::Office::Common::Filter::Microsoft::Export::EnablePowerPointPreview::get() )
202 nCnvrtFlags |= 0x8000;
204 CreateStatusIndicator();
206 //OUString sBaseURI( "BaseURI");
207 std::vector< PropertyValue > aProperties;
208 PropertyValue aProperty;
209 aProperty.Name = "BaseURI";
210 aProperty.Value <<= mrMedium.GetBaseURL( true );
211 aProperties.push_back( aProperty );
213 SvStream * pOutputStrm = mrMedium.GetOutStream();
215 Sequence< NamedValue > aEncryptionData;
216 Reference< css::packages::XPackageEncryption > xPackageEncryption;
217 const SfxUnoAnyItem* pEncryptionDataItem = mrMedium.GetItemSet().GetItem(SID_ENCRYPTIONDATA, false);
218 std::shared_ptr<SvStream> pMediaStrm;
219 if (pEncryptionDataItem && (pEncryptionDataItem->GetValue() >>= aEncryptionData))
221 ::comphelper::SequenceAsHashMap aHashData(aEncryptionData);
222 OUString sCryptoType = aHashData.getUnpackedValueOrDefault(u"CryptoType"_ustr, OUString());
224 if (sCryptoType.getLength())
226 const Reference<XComponentContext>& xComponentContext(comphelper::getProcessComponentContext());
227 Sequence<Any> aArguments{
228 Any(NamedValue(u"Binary"_ustr, Any(true))) };
229 xPackageEncryption.set(
230 xComponentContext->getServiceManager()->createInstanceWithArgumentsAndContext(
231 "com.sun.star.comp.oox.crypto." + sCryptoType, aArguments, xComponentContext), UNO_QUERY);
233 if (xPackageEncryption.is())
235 // We have an encryptor. Export document into memory stream and encrypt it later
236 pMediaStrm = std::make_shared<SvMemoryStream>();
237 pOutputStrm = pMediaStrm.get();
239 // Temp removal of EncryptionData to avoid password protection triggering
240 mrMedium.GetItemSet().ClearItem(SID_ENCRYPTIONDATA);
245 rtl::Reference<SotStorage> xStorRef = new SotStorage(pOutputStrm, false);
247 if (xStorRef.is())
249 bRet = ExportPPT(aProperties, xStorRef, mxModel, mxStatusIndicator, pBas, nCnvrtFlags);
250 xStorRef->Commit();
252 if (xPackageEncryption.is())
254 // Perform DRM encryption
255 pOutputStrm->Seek(0);
257 xPackageEncryption->setupEncryption(aEncryptionData);
259 Reference<css::io::XInputStream > xInputStream(new utl::OSeekableInputStreamWrapper(pOutputStrm, false));
260 Sequence<NamedValue> aStreams = xPackageEncryption->encrypt(xInputStream);
262 rtl::Reference<SotStorage> xEncryptedRootStrg = new SotStorage(mrMedium.GetOutStream(), false);
263 for (const NamedValue& aStreamData : aStreams)
265 // To avoid long paths split and open substorages recursively
266 // Splitting paths manually, since comphelper::string::split is trimming special characters like \0x01, \0x09
267 rtl::Reference<SotStorage> pStorage = xEncryptedRootStrg;
268 OUString sFileName;
269 sal_Int32 idx = 0;
272 OUString sPathElem = aStreamData.Name.getToken(0, L'/', idx);
273 if (!sPathElem.isEmpty())
275 if (idx < 0)
277 sFileName = sPathElem;
279 else
281 pStorage = pStorage->OpenSotStorage(sPathElem);
284 } while (pStorage && idx >= 0);
286 if (!pStorage)
288 bRet = false;
289 break;
292 rtl::Reference<SotStorageStream> pStream = pStorage->OpenSotStream(sFileName);
293 if (!pStream)
295 bRet = false;
296 break;
298 Sequence<sal_Int8> aStreamContent;
299 aStreamData.Value >>= aStreamContent;
300 size_t nBytesWritten = pStream->WriteBytes(aStreamContent.getConstArray(), aStreamContent.getLength());
301 if (nBytesWritten != static_cast<size_t>(aStreamContent.getLength()))
303 bRet = false;
304 break;
307 xEncryptedRootStrg->Commit();
309 // Restore encryption data
310 mrMedium.GetItemSet().Put(SfxUnoAnyItem(SID_ENCRYPTIONDATA, Any(aEncryptionData)));
315 return bRet;
318 void SdPPTFilter::PreSaveBasic()
320 if( officecfg::Office::Impress::Filter::Import::VBA::Save::get() )
322 SaveVBA( static_cast<SfxObjectShell&>(mrDocShell), pBas );
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */