/src/libreoffice/unotools/source/misc/mediadescriptor.cxx
Line | Count | Source |
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 <comphelper/docpasswordhelper.hxx> |
21 | | #include <sal/log.hxx> |
22 | | #include <unotools/configmgr.hxx> |
23 | | #include <unotools/mediadescriptor.hxx> |
24 | | #include <unotools/securityoptions.hxx> |
25 | | #include <unotools/ucbhelper.hxx> |
26 | | #include <comphelper/namedvaluecollection.hxx> |
27 | | #include <comphelper/sequenceashashmap.hxx> |
28 | | #include <comphelper/stillreadwriteinteraction.hxx> |
29 | | |
30 | | #include <com/sun/star/ucb/ContentCreationException.hpp> |
31 | | #include <com/sun/star/ucb/XContent.hpp> |
32 | | #include <com/sun/star/task/XInteractionHandler.hpp> |
33 | | #include <com/sun/star/io/XStream.hpp> |
34 | | #include <com/sun/star/io/XActiveDataSink.hpp> |
35 | | #include <com/sun/star/io/XSeekable.hpp> |
36 | | #include <com/sun/star/lang/IllegalArgumentException.hpp> |
37 | | #include <com/sun/star/uri/UriReferenceFactory.hpp> |
38 | | #include <com/sun/star/uri/XUriReference.hpp> |
39 | | #include <com/sun/star/ucb/PostCommandArgument2.hpp> |
40 | | #include <officecfg/Office/Common.hxx> |
41 | | #include <ucbhelper/content.hxx> |
42 | | #include <ucbhelper/commandenvironment.hxx> |
43 | | #include <ucbhelper/activedatasink.hxx> |
44 | | #include <comphelper/processfactory.hxx> |
45 | | #include <tools/urlobj.hxx> |
46 | | #include <osl/diagnose.h> |
47 | | #include <comphelper/diagnose_ex.hxx> |
48 | | |
49 | | namespace utl::MediaDescriptor { |
50 | | |
51 | | namespace { |
52 | | |
53 | 21.7k | OUString removeFragment(OUString const & uri) { |
54 | 21.7k | css::uno::Reference< css::uri::XUriReference > ref( |
55 | 21.7k | css::uri::UriReferenceFactory::create( |
56 | 21.7k | comphelper::getProcessComponentContext())-> |
57 | 21.7k | parse(uri)); |
58 | 21.7k | if (ref.is()) { |
59 | 21.7k | ref->clearFragment(); |
60 | 21.7k | return ref->getUriReference(); |
61 | 21.7k | } else { |
62 | 0 | SAL_WARN("unotools.misc", "cannot parse <" << uri << ">"); |
63 | 0 | return uri; |
64 | 0 | } |
65 | 21.7k | } |
66 | | |
67 | | /** @short tries to open a stream by using the given URL. |
68 | | |
69 | | @descr First it tries to open the content in r/w mode (if it's |
70 | | allowed to do so). Only in case it's not allowed or it failed |
71 | | the stream will be tried to open in readonly mode. |
72 | | |
73 | | The MediaDescriptor itself is changed inside this method. |
74 | | Means: the stream is added internal and not returned by a value. |
75 | | |
76 | | @param sURL |
77 | | the URL for open. |
78 | | |
79 | | @param bLockFile |
80 | | specifies whether the file should be locked |
81 | | |
82 | | @return TRUE if the stream could be added successfully. |
83 | | Note: If FALSE is returned, the error was already handled inside! |
84 | | |
85 | | @throw [css::uno::RuntimeException] |
86 | | if the MediaDescriptor seems to be invalid! |
87 | | */ |
88 | | bool impl_openStreamWithURL(comphelper::SequenceAsHashMap& rMediaDescriptor, const OUString& sURL, |
89 | | bool bLockFile) |
90 | 21.7k | { |
91 | 21.7k | if (sURL.matchIgnoreAsciiCase(".component:") || sURL.matchIgnoreAsciiCase("private:factory/")) |
92 | 0 | return false; // No UCB content for .component URLs and factory URLs |
93 | | |
94 | 21.7k | if (INetURLObject(sURL).IsExoticProtocol()) |
95 | 0 | return false; |
96 | | |
97 | 21.7k | OUString referer(rMediaDescriptor.getUnpackedValueOrDefault(PROP_REFERRER, OUString())); |
98 | 21.7k | if (SvtSecurityOptions::isUntrustedReferer(referer)) |
99 | 0 | { |
100 | 0 | return false; |
101 | 0 | } |
102 | | |
103 | | // prepare the environment |
104 | 21.7k | auto xOrgInteraction = rMediaDescriptor.getUnpackedValueOrDefault( |
105 | 21.7k | PROP_INTERACTIONHANDLER, css::uno::Reference<css::task::XInteractionHandler>()); |
106 | | |
107 | 21.7k | auto xAuthenticationInteraction = rMediaDescriptor.getUnpackedValueOrDefault( |
108 | 21.7k | PROP_AUTHENTICATIONHANDLER, css::uno::Reference<css::task::XInteractionHandler>()); |
109 | | |
110 | 21.7k | rtl::Reference<comphelper::StillReadWriteInteraction> xInteraction |
111 | 21.7k | = new comphelper::StillReadWriteInteraction(xOrgInteraction, xAuthenticationInteraction); |
112 | | |
113 | 21.7k | css::uno::Reference<css::ucb::XProgressHandler> xProgress; |
114 | 21.7k | rtl::Reference<ucbhelper::CommandEnvironment> xCommandEnv |
115 | 21.7k | = new ucbhelper::CommandEnvironment(xInteraction, xProgress); |
116 | | |
117 | | // try to create the content |
118 | | // no content -> no stream => return immediately with FALSE |
119 | 21.7k | ucbhelper::Content aContent; |
120 | 21.7k | css::uno::Reference<css::ucb::XContent> xContent; |
121 | 21.7k | try |
122 | 21.7k | { |
123 | 21.7k | aContent = ucbhelper::Content(sURL, xCommandEnv, comphelper::getProcessComponentContext()); |
124 | 21.7k | xContent = aContent.get(); |
125 | 21.7k | } |
126 | 21.7k | catch (const css::uno::RuntimeException&) |
127 | 21.7k | { |
128 | 0 | throw; |
129 | 0 | } |
130 | 21.7k | catch (const css::ucb::ContentCreationException&) |
131 | 21.7k | { |
132 | 21.7k | TOOLS_WARN_EXCEPTION("unotools.misc", "url: '" << sURL << "'"); |
133 | 21.7k | return false; // TODO error handling |
134 | 21.7k | } |
135 | 21.7k | catch (const css::uno::Exception&) |
136 | 21.7k | { |
137 | 0 | TOOLS_WARN_EXCEPTION("unotools.misc", "url: '" << sURL << "'"); |
138 | 0 | return false; // TODO error handling |
139 | 0 | } |
140 | | |
141 | | // try to open the file in read/write mode |
142 | | // (if it's allowed to do so). |
143 | | // But handle errors in a "hidden mode". Because |
144 | | // we try it readonly later - if read/write is not an option. |
145 | 0 | css::uno::Reference<css::io::XStream> xStream; |
146 | 0 | css::uno::Reference<css::io::XInputStream> xInputStream; |
147 | |
|
148 | 0 | bool bReadOnly = false; |
149 | 0 | bool bModeRequestedExplicitly = false; |
150 | 0 | auto pIt = rMediaDescriptor.find(PROP_READONLY); |
151 | 0 | if (pIt != rMediaDescriptor.end()) |
152 | 0 | { |
153 | 0 | pIt->second >>= bReadOnly; |
154 | 0 | bModeRequestedExplicitly = true; |
155 | 0 | } |
156 | |
|
157 | 0 | if (!bReadOnly && bLockFile) |
158 | 0 | { |
159 | 0 | try |
160 | 0 | { |
161 | | // TODO: use "special" still interaction to suppress error messages |
162 | 0 | xStream = aContent.openWriteableStream(); |
163 | 0 | if (xStream.is()) |
164 | 0 | xInputStream = xStream->getInputStream(); |
165 | 0 | } |
166 | 0 | catch (const css::uno::RuntimeException&) |
167 | 0 | { |
168 | 0 | throw; |
169 | 0 | } |
170 | 0 | catch (const css::uno::Exception&) |
171 | 0 | { |
172 | 0 | css::uno::Any ex(cppu::getCaughtException()); |
173 | | // ignore exception, if reason was problem reasoned on |
174 | | // open it in WRITABLE mode! Then we try it READONLY |
175 | | // later a second time. |
176 | | // All other errors must be handled as real error an |
177 | | // break this method. |
178 | 0 | if (!xInteraction->wasWriteError() || bModeRequestedExplicitly) |
179 | 0 | { |
180 | 0 | SAL_WARN("unotools.misc", "url: '" << sURL << "' " << exceptionToString(ex)); |
181 | | // If the protocol is webdav, then we need to treat the stream as readonly, even if the |
182 | | // operation was requested as read/write explicitly (the WebDAV UCB implementation is monodirectional |
183 | | // read or write not both at the same time). |
184 | 0 | if (!INetURLObject(sURL).isAnyKnownWebDAVScheme()) |
185 | 0 | return false; |
186 | 0 | } |
187 | 0 | xStream.clear(); |
188 | 0 | xInputStream.clear(); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | | // If opening of the stream in read/write mode was not allowed |
193 | | // or failed by an error - we must try it in readonly mode. |
194 | 0 | if (!xInputStream.is()) |
195 | 0 | { |
196 | 0 | OUString aScheme; |
197 | |
|
198 | 0 | try |
199 | 0 | { |
200 | 0 | css::uno::Reference<css::ucb::XContentIdentifier> xContId( |
201 | 0 | aContent.get().is() ? aContent.get()->getIdentifier() : nullptr); |
202 | |
|
203 | 0 | if (xContId.is()) |
204 | 0 | aScheme = xContId->getContentProviderScheme(); |
205 | | |
206 | | // Only file system content provider is able to provide XStream |
207 | | // so for this content impossibility to create XStream triggers |
208 | | // switch to readonly mode in case of opening with locking on |
209 | 0 | if (bLockFile && aScheme.equalsIgnoreAsciiCase("file")) |
210 | 0 | bReadOnly = true; |
211 | 0 | else |
212 | 0 | { |
213 | 0 | bool bRequestReadOnly = bReadOnly; |
214 | 0 | aContent.getPropertyValue(u"IsReadOnly"_ustr) >>= bReadOnly; |
215 | 0 | if (bReadOnly && !bRequestReadOnly && bModeRequestedExplicitly) |
216 | 0 | return false; // the document is explicitly requested with WRITABLE mode |
217 | 0 | } |
218 | 0 | } |
219 | 0 | catch (const css::uno::RuntimeException&) |
220 | 0 | { |
221 | 0 | throw; |
222 | 0 | } |
223 | 0 | catch (const css::uno::Exception&) |
224 | 0 | { /* no error handling if IsReadOnly property does not exist for UCP */ |
225 | 0 | } |
226 | | |
227 | 0 | if (bReadOnly) |
228 | 0 | rMediaDescriptor[PROP_READONLY] <<= bReadOnly; |
229 | |
|
230 | 0 | xInteraction->resetInterceptions(); |
231 | 0 | xInteraction->resetErrorStates(); |
232 | 0 | try |
233 | 0 | { |
234 | | // all the contents except file-URLs should be opened as usual |
235 | 0 | if (bLockFile || !aScheme.equalsIgnoreAsciiCase("file")) |
236 | 0 | xInputStream = aContent.openStream(); |
237 | 0 | else |
238 | 0 | xInputStream = aContent.openStreamNoLock(); |
239 | 0 | } |
240 | 0 | catch (const css::uno::RuntimeException&) |
241 | 0 | { |
242 | 0 | throw; |
243 | 0 | } |
244 | 0 | catch (const css::uno::Exception&) |
245 | 0 | { |
246 | 0 | TOOLS_INFO_EXCEPTION("unotools.misc", "url: '" << sURL << "'"); |
247 | 0 | return false; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | // add streams to the descriptor |
252 | 0 | if (xContent.is()) |
253 | 0 | rMediaDescriptor[PROP_UCBCONTENT] <<= xContent; |
254 | 0 | if (xStream.is()) |
255 | 0 | rMediaDescriptor[PROP_STREAM] <<= xStream; |
256 | 0 | if (xInputStream.is()) |
257 | 0 | rMediaDescriptor[PROP_INPUTSTREAM] <<= xInputStream; |
258 | | |
259 | | // At least we need an input stream. The r/w stream is optional ... |
260 | 0 | return xInputStream.is(); |
261 | 0 | } |
262 | | |
263 | | /** @short tries to open a stream by using the given PostData stream. |
264 | | |
265 | | @descr The stream is used directly ... |
266 | | |
267 | | The MediaDescriptor itself is changed inside this method. |
268 | | Means: the stream is added internal and not returned by a value. |
269 | | |
270 | | @param _rxPostData |
271 | | the PostData stream. |
272 | | |
273 | | @return TRUE if the stream could be added successfully. |
274 | | Note: If FALSE is returned, the error was already handled inside! |
275 | | |
276 | | @throw [css::uno::RuntimeException] |
277 | | if the MediaDescriptor seems to be invalid! |
278 | | |
279 | | @throw [css::lang::IllegalArgumentException] |
280 | | if the given PostData stream is <NULL/>. |
281 | | */ |
282 | | bool impl_openStreamWithPostData(comphelper::SequenceAsHashMap& rMediaDescriptor, |
283 | | const css::uno::Reference<css::io::XInputStream>& _rxPostData) |
284 | 0 | { |
285 | 0 | if (!_rxPostData.is()) |
286 | 0 | throw css::lang::IllegalArgumentException(u"Found invalid PostData."_ustr, |
287 | 0 | css::uno::Reference<css::uno::XInterface>(), 1); |
288 | | |
289 | | // PostData can't be used in read/write mode! |
290 | 0 | rMediaDescriptor[PROP_READONLY] <<= true; |
291 | | |
292 | | // prepare the environment |
293 | 0 | auto xInteraction = rMediaDescriptor.getUnpackedValueOrDefault( |
294 | 0 | PROP_INTERACTIONHANDLER, css::uno::Reference<css::task::XInteractionHandler>()); |
295 | 0 | css::uno::Reference<css::ucb::XProgressHandler> xProgress; |
296 | 0 | rtl::Reference<::ucbhelper::CommandEnvironment> xCommandEnv |
297 | 0 | = new ::ucbhelper::CommandEnvironment(xInteraction, xProgress); |
298 | | |
299 | | // media type |
300 | 0 | OUString sMediaType = rMediaDescriptor.getUnpackedValueOrDefault(PROP_MEDIATYPE, OUString()); |
301 | 0 | if (sMediaType.isEmpty()) |
302 | 0 | { |
303 | 0 | sMediaType = "application/x-www-form-urlencoded"; |
304 | 0 | rMediaDescriptor[PROP_MEDIATYPE] <<= sMediaType; |
305 | 0 | } |
306 | | |
307 | | // url |
308 | 0 | OUString sURL(rMediaDescriptor.getUnpackedValueOrDefault(PROP_URL, OUString())); |
309 | |
|
310 | 0 | css::uno::Reference<css::io::XInputStream> xResultStream; |
311 | 0 | try |
312 | 0 | { |
313 | | // seek PostData stream to the beginning |
314 | 0 | css::uno::Reference<css::io::XSeekable> xSeek(_rxPostData, css::uno::UNO_QUERY); |
315 | 0 | if (xSeek.is()) |
316 | 0 | xSeek->seek(0); |
317 | | |
318 | | // a content for the URL |
319 | 0 | ::ucbhelper::Content aContent(sURL, xCommandEnv, comphelper::getProcessComponentContext()); |
320 | | |
321 | | // use post command |
322 | 0 | css::ucb::PostCommandArgument2 aPostArgument; |
323 | 0 | aPostArgument.Source = _rxPostData; |
324 | 0 | css::uno::Reference<css::io::XActiveDataSink> xSink(new ucbhelper::ActiveDataSink); |
325 | 0 | aPostArgument.Sink = xSink; |
326 | 0 | aPostArgument.MediaType = sMediaType; |
327 | 0 | aPostArgument.Referer |
328 | 0 | = rMediaDescriptor.getUnpackedValueOrDefault(PROP_REFERRER, OUString()); |
329 | |
|
330 | 0 | aContent.executeCommand(u"post"_ustr, css::uno::Any(aPostArgument)); |
331 | | |
332 | | // get result |
333 | 0 | xResultStream = xSink->getInputStream(); |
334 | 0 | } |
335 | 0 | catch (const css::uno::Exception&) |
336 | 0 | { |
337 | 0 | } |
338 | | |
339 | | // success? |
340 | 0 | if (!xResultStream.is()) |
341 | 0 | { |
342 | 0 | OSL_FAIL("no valid reply to the HTTP-Post"); |
343 | 0 | return false; |
344 | 0 | } |
345 | | |
346 | 0 | rMediaDescriptor[PROP_INPUTSTREAM] <<= xResultStream; |
347 | 0 | return true; |
348 | 0 | } |
349 | | |
350 | | /** @short it checks if the descriptor already has a valid |
351 | | InputStream item and creates a new one, if not. |
352 | | |
353 | | @descr This function uses the current items of this MediaDescriptor, |
354 | | to open the stream (as e.g. URL, ReadOnly, PostData etcpp.). |
355 | | It creates a seekable stream and put it into the descriptor. |
356 | | |
357 | | A might existing InteractionHandler will be used automatically, |
358 | | to solve problems! |
359 | | |
360 | | @param bLockFile |
361 | | specifies whether the file should be locked |
362 | | |
363 | | @return TRUE, if the stream was already part of the descriptor or could |
364 | | be created as new item. FALSE otherwise. |
365 | | */ |
366 | | bool impl_addInputStream(comphelper::SequenceAsHashMap& rMediaDescriptor, bool bLockFile) |
367 | 55.9k | { |
368 | | // check for an already existing stream item first |
369 | 55.9k | auto pIt = rMediaDescriptor.find(PROP_INPUTSTREAM); |
370 | 55.9k | if (pIt != rMediaDescriptor.end()) |
371 | 16.2k | return true; |
372 | | |
373 | 39.7k | try |
374 | 39.7k | { |
375 | | // No stream available - create a new one |
376 | | // a) data comes as PostData ... |
377 | 39.7k | pIt = rMediaDescriptor.find(PROP_POSTDATA); |
378 | 39.7k | if (pIt != rMediaDescriptor.end()) |
379 | 0 | { |
380 | 0 | const css::uno::Any& rPostData = pIt->second; |
381 | 0 | css::uno::Reference<css::io::XInputStream> xPostData; |
382 | 0 | rPostData >>= xPostData; |
383 | |
|
384 | 0 | return impl_openStreamWithPostData(rMediaDescriptor, xPostData); |
385 | 0 | } |
386 | | |
387 | | // b) ... or we must get it from the given URL |
388 | 39.7k | OUString sURL = rMediaDescriptor.getUnpackedValueOrDefault(PROP_URL, OUString()); |
389 | 39.7k | if (sURL.isEmpty()) |
390 | 18.0k | throw css::uno::Exception(u"Found no URL."_ustr, |
391 | 18.0k | css::uno::Reference<css::uno::XInterface>()); |
392 | | |
393 | 21.7k | return impl_openStreamWithURL(rMediaDescriptor, removeFragment(sURL), bLockFile); |
394 | 39.7k | } |
395 | 39.7k | catch (const css::uno::Exception&) |
396 | 39.7k | { |
397 | 18.0k | TOOLS_WARN_EXCEPTION("unotools.misc", "invalid MediaDescriptor detected"); |
398 | 18.0k | return false; |
399 | 18.0k | } |
400 | 39.7k | } |
401 | | |
402 | | } |
403 | | |
404 | | bool isStreamReadOnly(const comphelper::SequenceAsHashMap& rMediaDescriptor) |
405 | 0 | { |
406 | 0 | bool bReadOnly = false; |
407 | | |
408 | | // check for explicit readonly state |
409 | 0 | auto pIt = rMediaDescriptor.find(PROP_READONLY); |
410 | 0 | if (pIt != rMediaDescriptor.end()) |
411 | 0 | { |
412 | 0 | pIt->second >>= bReadOnly; |
413 | 0 | return bReadOnly; |
414 | 0 | } |
415 | | |
416 | | // streams based on post data are readonly by definition |
417 | 0 | pIt = rMediaDescriptor.find(PROP_POSTDATA); |
418 | 0 | if (pIt != rMediaDescriptor.end()) |
419 | 0 | return true; |
420 | | |
421 | | // A XStream capsulate XInputStream and XOutputStream ... |
422 | | // If it exists - the file must be open in read/write mode! |
423 | 0 | pIt = rMediaDescriptor.find(PROP_STREAM); |
424 | 0 | if (pIt != rMediaDescriptor.end()) |
425 | 0 | return false; |
426 | | |
427 | | // Only file system content provider is able to provide XStream |
428 | | // so for this content impossibility to create XStream triggers |
429 | | // switch to readonly mode. |
430 | 0 | try |
431 | 0 | { |
432 | 0 | auto xContent = rMediaDescriptor.getUnpackedValueOrDefault(PROP_UCBCONTENT, css::uno::Reference< css::ucb::XContent >()); |
433 | 0 | if (xContent.is()) |
434 | 0 | { |
435 | 0 | css::uno::Reference< css::ucb::XContentIdentifier > xId = xContent->getIdentifier(); |
436 | 0 | OUString aScheme; |
437 | 0 | if (xId.is()) |
438 | 0 | aScheme = xId->getContentProviderScheme(); |
439 | |
|
440 | 0 | if (aScheme.equalsIgnoreAsciiCase("file")) |
441 | 0 | bReadOnly = true; |
442 | 0 | else |
443 | 0 | { |
444 | 0 | ::ucbhelper::Content aContent(xContent, |
445 | 0 | utl::UCBContentHelper::getDefaultCommandEnvironment(), |
446 | 0 | comphelper::getProcessComponentContext()); |
447 | 0 | aContent.getPropertyValue(u"IsReadOnly"_ustr) >>= bReadOnly; |
448 | 0 | } |
449 | 0 | } |
450 | 0 | } |
451 | 0 | catch(const css::uno::RuntimeException& ) |
452 | 0 | { throw; } |
453 | 0 | catch(const css::uno::Exception&) |
454 | 0 | {} |
455 | | |
456 | 0 | return bReadOnly; |
457 | 0 | } |
458 | | |
459 | | css::uno::Any getComponentDataEntry(const comphelper::SequenceAsHashMap& rMediaDescriptor, |
460 | | const OUString& rName) |
461 | 8.24k | { |
462 | 8.24k | auto aPropertyIter = rMediaDescriptor.find(PROP_COMPONENTDATA); |
463 | 8.24k | if( aPropertyIter != rMediaDescriptor.end() ) |
464 | 2 | return comphelper::NamedValueCollection( aPropertyIter->second ).get( rName ); |
465 | 8.24k | return css::uno::Any(); |
466 | 8.24k | } |
467 | | |
468 | | void setComponentDataEntry(comphelper::SequenceAsHashMap& rMediaDescriptor, |
469 | | const OUString& rName, const css::uno::Any& rValue) |
470 | 24 | { |
471 | 24 | if( rValue.hasValue() ) |
472 | 24 | { |
473 | | // get or create the 'ComponentData' property entry |
474 | 24 | css::uno::Any& rCompDataAny = rMediaDescriptor[PROP_COMPONENTDATA]; |
475 | | // insert the value (retain sequence type, create NamedValue elements by default) |
476 | 24 | bool bHasNamedValues = !rCompDataAny.hasValue() || rCompDataAny.has< css::uno::Sequence< css::beans::NamedValue > >(); |
477 | 24 | bool bHasPropValues = rCompDataAny.has< css::uno::Sequence< css::beans::PropertyValue > >(); |
478 | 24 | OSL_ENSURE( bHasNamedValues || bHasPropValues, "MediaDescriptor::setComponentDataEntry - incompatible 'ComponentData' property in media descriptor" ); |
479 | 24 | if( bHasNamedValues || bHasPropValues ) |
480 | 24 | { |
481 | | // insert or overwrite the passed value |
482 | 24 | comphelper::SequenceAsHashMap aCompDataMap( rCompDataAny ); |
483 | 24 | aCompDataMap[ rName ] = rValue; |
484 | | // write back the sequence (restore sequence with correct element type) |
485 | 24 | rCompDataAny = aCompDataMap.getAsConstAny( bHasPropValues ); |
486 | 24 | } |
487 | 24 | } |
488 | 0 | else |
489 | 0 | { |
490 | | // if an empty Any is passed, clear the entry |
491 | 0 | clearComponentDataEntry(rMediaDescriptor, rName); |
492 | 0 | } |
493 | 24 | } |
494 | | |
495 | | void clearComponentDataEntry(comphelper::SequenceAsHashMap& rMediaDescriptor, |
496 | | const OUString& rName) |
497 | 0 | { |
498 | 0 | auto aPropertyIter = rMediaDescriptor.find(PROP_COMPONENTDATA); |
499 | 0 | if( aPropertyIter == rMediaDescriptor.end() ) |
500 | 0 | return; |
501 | | |
502 | 0 | css::uno::Any& rCompDataAny = aPropertyIter->second; |
503 | 0 | bool bHasNamedValues = rCompDataAny.has< css::uno::Sequence< css::beans::NamedValue > >(); |
504 | 0 | bool bHasPropValues = rCompDataAny.has< css::uno::Sequence< css::beans::PropertyValue > >(); |
505 | 0 | OSL_ENSURE( bHasNamedValues || bHasPropValues, "MediaDescriptor::clearComponentDataEntry - incompatible 'ComponentData' property in media descriptor" ); |
506 | 0 | if( bHasNamedValues || bHasPropValues ) |
507 | 0 | { |
508 | | // remove the value with the passed name |
509 | 0 | comphelper::SequenceAsHashMap aCompDataMap( rCompDataAny ); |
510 | 0 | aCompDataMap.erase( rName ); |
511 | | // write back the sequence, or remove it completely if it is empty |
512 | 0 | if( aCompDataMap.empty() ) |
513 | 0 | rMediaDescriptor.erase(aPropertyIter); |
514 | 0 | else |
515 | 0 | rCompDataAny = aCompDataMap.getAsConstAny( bHasPropValues ); |
516 | 0 | } |
517 | 0 | } |
518 | | |
519 | | css::uno::Sequence< css::beans::NamedValue > requestAndVerifyDocPassword( |
520 | | comphelper::SequenceAsHashMap& rMediaDescriptor, |
521 | | comphelper::IDocPasswordVerifier& rVerifier, |
522 | | comphelper::DocPasswordRequestType eRequestType, |
523 | | const ::std::vector< OUString >* pDefaultPasswords ) |
524 | 37 | { |
525 | 37 | auto aMediaEncData = rMediaDescriptor.getUnpackedValueOrDefault( |
526 | 37 | PROP_ENCRYPTIONDATA, css::uno::Sequence< css::beans::NamedValue >() ); |
527 | 37 | OUString aMediaPassword = rMediaDescriptor.getUnpackedValueOrDefault( |
528 | 37 | PROP_PASSWORD, OUString() ); |
529 | 37 | auto xInteractHandler = rMediaDescriptor.getUnpackedValueOrDefault( |
530 | 37 | PROP_INTERACTIONHANDLER, css::uno::Reference< css::task::XInteractionHandler >() ); |
531 | 37 | OUString aDocumentName = rMediaDescriptor.getUnpackedValueOrDefault( |
532 | 37 | PROP_URL, OUString() ); |
533 | | |
534 | 37 | bool bIsDefaultPassword = false; |
535 | 37 | css::uno::Sequence< css::beans::NamedValue > aEncryptionData = comphelper::DocPasswordHelper::requestAndVerifyDocPassword( |
536 | 37 | rVerifier, aMediaEncData, aMediaPassword, xInteractHandler, aDocumentName, eRequestType, pDefaultPasswords, &bIsDefaultPassword ); |
537 | | |
538 | 37 | rMediaDescriptor.erase(PROP_PASSWORD); |
539 | 37 | rMediaDescriptor.erase(PROP_ENCRYPTIONDATA); |
540 | | |
541 | | // insert encryption info into media descriptor |
542 | | // TODO |
543 | 37 | if( aEncryptionData.hasElements() ) |
544 | 24 | rMediaDescriptor[PROP_ENCRYPTIONDATA] <<= aEncryptionData; |
545 | | |
546 | 37 | return aEncryptionData; |
547 | 37 | } |
548 | | |
549 | | bool addInputStream(comphelper::SequenceAsHashMap& rMediaDescriptor) |
550 | 54.4k | { |
551 | 54.4k | return impl_addInputStream(rMediaDescriptor, true); |
552 | 54.4k | } |
553 | | |
554 | | /*-----------------------------------------------*/ |
555 | | bool addInputStreamOwnLock(comphelper::SequenceAsHashMap& rMediaDescriptor) |
556 | 1.52k | { |
557 | 1.52k | const bool bLock = !comphelper::IsFuzzing() |
558 | 0 | && officecfg::Office::Common::Misc::UseDocumentSystemFileLocking::get(); |
559 | 1.52k | return impl_addInputStream(rMediaDescriptor, bLock); |
560 | 1.52k | } |
561 | | |
562 | | } // namespace utl::MediaDescriptor |
563 | | |
564 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |