LibreOffice Module comphelper (master) 1
mimeconfighelper.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 <com/sun/star/beans/PropertyValue.hpp>
21#include <com/sun/star/configuration/theDefaultProvider.hpp>
22#include <com/sun/star/container/XContainerQuery.hpp>
23#include <com/sun/star/container/XNameAccess.hpp>
24#include <com/sun/star/embed/VerbDescriptor.hpp>
25#include <com/sun/star/document/XTypeDetection.hpp>
26
27#include <osl/diagnose.h>
28
35#include <rtl/ustrbuf.hxx>
36#include <utility>
37
38
39using namespace ::com::sun::star;
40using namespace comphelper;
41
42
43MimeConfigurationHelper::MimeConfigurationHelper( uno::Reference< uno::XComponentContext > xContext )
44: m_xContext(std::move( xContext ))
45{
46 if ( !m_xContext.is() )
47 throw uno::RuntimeException();
48}
49
50
51OUString MimeConfigurationHelper::GetStringClassIDRepresentation( const uno::Sequence< sal_Int8 >& aClassID )
52{
53 OUStringBuffer aResult;
54
55 if ( aClassID.getLength() == 16 )
56 {
57 for ( sal_Int32 nInd = 0; nInd < aClassID.getLength(); nInd++ )
58 {
59 if ( nInd == 4 || nInd == 6 || nInd == 8 || nInd == 10 )
60 aResult.append("-");
61
62 sal_Int32 nDigit1 = static_cast<sal_Int32>( static_cast<sal_uInt8>(aClassID[nInd]) / 16 );
63 sal_Int32 nDigit2 = static_cast<sal_uInt8>(aClassID[nInd]) % 16;
64 aResult.append( OUString::number(nDigit1, 16) + OUString::number( nDigit2, 16 ) );
65 }
66 }
67
68 return aResult.makeStringAndClear();
69}
70
71
72static sal_uInt8 GetDigit_Impl( char aChar )
73{
74 if ( aChar >= '0' && aChar <= '9' )
75 return aChar - '0';
76 else if ( aChar >= 'a' && aChar <= 'f' )
77 return aChar - 'a' + 10;
78 else if ( aChar >= 'A' && aChar <= 'F' )
79 return aChar - 'A' + 10;
80 else
81 return 16;
82}
83
84
85uno::Sequence< sal_Int8 > MimeConfigurationHelper::GetSequenceClassIDRepresentation( std::u16string_view aClassID )
86{
87 size_t nLength = aClassID.size();
88 if ( nLength == 36 )
89 {
90 OString aCharClassID = OUStringToOString( aClassID, RTL_TEXTENCODING_ASCII_US );
91 uno::Sequence< sal_Int8 > aResult( 16 );
92 auto pResult = aResult.getArray();
93
94 size_t nStrPointer = 0;
95 sal_Int32 nSeqInd = 0;
96 while( nSeqInd < 16 && nStrPointer + 1U < nLength )
97 {
98 sal_uInt8 nDigit1 = GetDigit_Impl( aCharClassID[nStrPointer++] );
99 sal_uInt8 nDigit2 = GetDigit_Impl( aCharClassID[nStrPointer++] );
100
101 if ( nDigit1 > 15 || nDigit2 > 15 )
102 break;
103
104 pResult[nSeqInd++] = static_cast<sal_Int8>( nDigit1 * 16 + nDigit2 );
105
106 if ( nStrPointer < nLength && aCharClassID[nStrPointer] == '-' )
107 nStrPointer++;
108 }
109
110 if ( nSeqInd == 16 && nStrPointer == nLength )
111 return aResult;
112 }
113
114 return uno::Sequence< sal_Int8 >();
115}
116
117
118uno::Reference< container::XNameAccess > MimeConfigurationHelper::GetConfigurationByPathImpl( const OUString& aPath )
119{
120 uno::Reference< container::XNameAccess > xConfig;
121
122 try
123 {
124 if ( !m_xConfigProvider.is() )
125 m_xConfigProvider = configuration::theDefaultProvider::get( m_xContext );
126
127 uno::Sequence<uno::Any> aArgs(comphelper::InitAnyPropertySequence(
128 {
129 {"nodepath", uno::Any(aPath)}
130 }));
131 xConfig.set( m_xConfigProvider->createInstanceWithArguments(
132 "com.sun.star.configuration.ConfigurationAccess",
133 aArgs ),
134 uno::UNO_QUERY );
135 }
136 catch( uno::Exception& )
137 {}
138
139 return xConfig;
140}
141
142
143uno::Reference< container::XNameAccess > MimeConfigurationHelper::GetObjConfiguration()
144{
145 std::unique_lock aGuard( m_aMutex );
146
147 if ( !m_xObjectConfig.is() )
149 "/org.openoffice.Office.Embedding/Objects" );
150
151 return m_xObjectConfig;
152}
153
154
155uno::Reference< container::XNameAccess > MimeConfigurationHelper::GetVerbsConfiguration()
156{
157 std::unique_lock aGuard( m_aMutex );
158
159 if ( !m_xVerbsConfig.is() )
161 "/org.openoffice.Office.Embedding/Verbs");
162
163 return m_xVerbsConfig;
164}
165
166
167uno::Reference< container::XNameAccess > MimeConfigurationHelper::GetMediaTypeConfiguration()
168{
169 std::unique_lock aGuard( m_aMutex );
170
171 if ( !m_xMediaTypeConfig.is() )
173 "/org.openoffice.Office.Embedding/MimeTypeClassIDRelations");
174
175 return m_xMediaTypeConfig;
176}
177
178
179uno::Reference< container::XNameAccess > MimeConfigurationHelper::GetFilterFactory()
180{
181 std::unique_lock aGuard( m_aMutex );
182
183 if ( !m_xFilterFactory.is() )
185 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.FilterFactory", m_xContext),
186 uno::UNO_QUERY );
187
188 return m_xFilterFactory;
189}
190
191
192OUString MimeConfigurationHelper::GetDocServiceNameFromFilter( const OUString& aFilterName )
193{
194 OUString aDocServiceName;
195
196 try
197 {
198 uno::Reference< container::XNameAccess > xFilterFactory(
200 uno::UNO_SET_THROW );
201
202 uno::Any aFilterAnyData = xFilterFactory->getByName( aFilterName );
203 uno::Sequence< beans::PropertyValue > aFilterData;
204 if ( aFilterAnyData >>= aFilterData )
205 {
206 for ( const auto & prop : std::as_const(aFilterData) )
207 if ( prop.Name == "DocumentService" )
208 prop.Value >>= aDocServiceName;
209 }
210 }
211 catch( uno::Exception& )
212 {}
213
214 return aDocServiceName;
215}
216
217
219{
220 uno::Reference< container::XContainerQuery > xTypeCFG(
221 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
222 uno::UNO_QUERY );
223
224 if ( xTypeCFG.is() )
225 {
226 try
227 {
228 // make query for all types matching the properties
229 uno::Sequence < beans::NamedValue > aSeq { { "MediaType", css::uno::Any(aMediaType) } };
230
231 uno::Reference < container::XEnumeration > xEnum = xTypeCFG->createSubSetEnumerationByProperties( aSeq );
232 while ( xEnum->hasMoreElements() )
233 {
234 uno::Sequence< beans::PropertyValue > aType;
235 if ( xEnum->nextElement() >>= aType )
236 {
237 for ( const auto & prop : std::as_const(aType) )
238 {
239 OUString aFilterName;
240 if ( prop.Name == "PreferredFilter"
241 && ( prop.Value >>= aFilterName ) && !aFilterName.isEmpty() )
242 {
243 OUString aDocumentName = GetDocServiceNameFromFilter( aFilterName );
244 if ( !aDocumentName.isEmpty() )
245 return aDocumentName;
246 }
247 }
248 }
249 }
250 }
251 catch( uno::Exception& )
252 {}
253 }
254
255 return OUString();
256}
257
258
259bool MimeConfigurationHelper::GetVerbByShortcut( const OUString& aVerbShortcut,
260 embed::VerbDescriptor& aDescriptor )
261{
262 bool bResult = false;
263
264 uno::Reference< container::XNameAccess > xVerbsConfig = GetVerbsConfiguration();
265 uno::Reference< container::XNameAccess > xVerbsProps;
266 try
267 {
268 if ( xVerbsConfig.is() && ( xVerbsConfig->getByName( aVerbShortcut ) >>= xVerbsProps ) && xVerbsProps.is() )
269 {
270 embed::VerbDescriptor aTempDescr;
271 static constexpr OUStringLiteral sVerbID = u"VerbID";
272 static constexpr OUStringLiteral sVerbUIName = u"VerbUIName";
273 static constexpr OUStringLiteral sVerbFlags = u"VerbFlags";
274 static constexpr OUStringLiteral sVerbAttributes = u"VerbAttributes";
275 if ( ( xVerbsProps->getByName(sVerbID) >>= aTempDescr.VerbID )
276 && ( xVerbsProps->getByName(sVerbUIName) >>= aTempDescr.VerbName )
277 && ( xVerbsProps->getByName(sVerbFlags) >>= aTempDescr.VerbFlags )
278 && ( xVerbsProps->getByName(sVerbAttributes) >>= aTempDescr.VerbAttributes ) )
279 {
280 aDescriptor = aTempDescr;
281 bResult = true;
282 }
283 }
284 }
285 catch( uno::Exception& )
286 {
287 }
288
289 return bResult;
290}
291
292
294 const uno::Sequence< sal_Int8 >& aClassID,
295 const uno::Reference< container::XNameAccess >& xObjectProps )
296{
297 uno::Sequence< beans::NamedValue > aResult;
298
299 if ( aClassID.getLength() == 16 )
300 {
301 try
302 {
303 const uno::Sequence< OUString > aObjPropNames = xObjectProps->getElementNames();
304
305 aResult.realloc( aObjPropNames.getLength() + 1 );
306 auto pResult = aResult.getArray();
307 pResult[0].Name = "ClassID";
308 pResult[0].Value <<= aClassID;
309
310 for ( sal_Int32 nInd = 0; nInd < aObjPropNames.getLength(); nInd++ )
311 {
312 pResult[nInd + 1].Name = aObjPropNames[nInd];
313
314 if ( aObjPropNames[nInd] == "ObjectVerbs" )
315 {
316 uno::Sequence< OUString > aVerbShortcuts;
317 if ( !(xObjectProps->getByName( aObjPropNames[nInd] ) >>= aVerbShortcuts) )
318 throw uno::RuntimeException();
319 uno::Sequence< embed::VerbDescriptor > aVerbDescriptors( aVerbShortcuts.getLength() );
320 auto aVerbDescriptorsRange = asNonConstRange(aVerbDescriptors);
321 for ( sal_Int32 nVerbI = 0; nVerbI < aVerbShortcuts.getLength(); nVerbI++ )
322 if ( !GetVerbByShortcut( aVerbShortcuts[nVerbI], aVerbDescriptorsRange[nVerbI] ) )
323 throw uno::RuntimeException();
324
325 pResult[nInd+1].Value <<= aVerbDescriptors;
326 }
327 else
328 pResult[nInd+1].Value = xObjectProps->getByName( aObjPropNames[nInd] );
329 }
330 }
331 catch( uno::Exception& )
332 {
333 aResult.realloc( 0 );
334 }
335 }
336
337 return aResult;
338}
339
340
342{
343 OUString aStringClassID;
344
345 uno::Reference< container::XNameAccess > xMediaTypeConfig = GetMediaTypeConfiguration();
346 try
347 {
348 if ( xMediaTypeConfig.is() )
349 xMediaTypeConfig->getByName( aMediaType ) >>= aStringClassID;
350 }
351 catch( uno::Exception& )
352 {
353 }
354
355 return aStringClassID;
356
357}
358
359
361 const OUString& aStringClassID )
362{
363 uno::Sequence< beans::NamedValue > aObjProps;
364
365 uno::Sequence< sal_Int8 > aClassID = GetSequenceClassIDRepresentation( aStringClassID );
367 {
368 aObjProps = { { "ObjectFactory",
369 uno::Any(OUString("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory")) },
370 { "ClassID", uno::Any(aClassID) } };
371 return aObjProps;
372 }
373
374 if ( aClassID.getLength() == 16 )
375 {
376 uno::Reference< container::XNameAccess > xObjConfig = GetObjConfiguration();
377 uno::Reference< container::XNameAccess > xObjectProps;
378 try
379 {
380 // TODO/LATER: allow to provide ClassID string in any format, only digits are counted
381 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
382 aObjProps = GetObjPropsFromConfigEntry( aClassID, xObjectProps );
383 }
384 catch( uno::Exception& )
385 {
386 }
387 }
388
389 return aObjProps;
390}
391
392
393uno::Sequence< beans::NamedValue > MimeConfigurationHelper::GetObjectPropsByClassID(
394 const uno::Sequence< sal_Int8 >& aClassID )
395{
396 uno::Sequence< beans::NamedValue > aObjProps;
398 {
399 aObjProps = { { "ObjectFactory",
400 uno::Any(OUString("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory")) },
401 { "ClassID", uno::Any(aClassID) } };
402 }
403
404 OUString aStringClassID = GetStringClassIDRepresentation( aClassID );
405 if ( !aStringClassID.isEmpty() )
406 {
407 uno::Reference< container::XNameAccess > xObjConfig = GetObjConfiguration();
408 uno::Reference< container::XNameAccess > xObjectProps;
409 try
410 {
411 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
412 aObjProps = GetObjPropsFromConfigEntry( aClassID, xObjectProps );
413 }
414 catch( uno::Exception& )
415 {
416 }
417 }
418
419 return aObjProps;
420}
421
422
423uno::Sequence< beans::NamedValue > MimeConfigurationHelper::GetObjectPropsByMediaType( const OUString& aMediaType )
424{
425 uno::Sequence< beans::NamedValue > aObject =
427 if ( aObject.hasElements() )
428 return aObject;
429
430 OUString aDocumentName = GetDocServiceNameFromMediaType( aMediaType );
431 if ( !aDocumentName.isEmpty() )
432 return GetObjectPropsByDocumentName( aDocumentName );
433
434 return uno::Sequence< beans::NamedValue >();
435}
436
437
438uno::Sequence< beans::NamedValue > MimeConfigurationHelper::GetObjectPropsByFilter( const OUString& aFilterName )
439{
440 OUString aDocumentName = GetDocServiceNameFromFilter( aFilterName );
441 if ( !aDocumentName.isEmpty() )
442 return GetObjectPropsByDocumentName( aDocumentName );
443
444 return uno::Sequence< beans::NamedValue >();
445}
446
447
448uno::Sequence< beans::NamedValue > MimeConfigurationHelper::GetObjectPropsByDocumentName( std::u16string_view aDocName )
449{
450 if ( !aDocName.empty() )
451 {
452 uno::Reference< container::XNameAccess > xObjConfig = GetObjConfiguration();
453 if ( xObjConfig.is() )
454 {
455 try
456 {
457 const uno::Sequence< OUString > aClassIDs = xObjConfig->getElementNames();
458 for ( const OUString & id : aClassIDs )
459 {
460 uno::Reference< container::XNameAccess > xObjectProps;
461 OUString aEntryDocName;
462
463 if ( ( xObjConfig->getByName( id ) >>= xObjectProps ) && xObjectProps.is()
464 && ( xObjectProps->getByName("ObjectDocumentServiceName") >>= aEntryDocName )
465 && aEntryDocName == aDocName )
466 {
468 xObjectProps );
469 }
470 }
471 }
472 catch( uno::Exception& )
473 {}
474 }
475 }
476
477 return uno::Sequence< beans::NamedValue >();
478}
479
480
481OUString MimeConfigurationHelper::GetFactoryNameByClassID( const uno::Sequence< sal_Int8 >& aClassID )
482{
484}
485
486
487OUString MimeConfigurationHelper::GetFactoryNameByStringClassID( const OUString& aStringClassID )
488{
489 OUString aResult;
490
491 if ( !aStringClassID.isEmpty() )
492 {
493 uno::Reference< container::XNameAccess > xObjConfig = GetObjConfiguration();
494 uno::Reference< container::XNameAccess > xObjectProps;
495 try
496 {
497 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
498 xObjectProps->getByName("ObjectFactory") >>= aResult;
499 }
500 catch( uno::Exception& )
501 {
502 uno::Sequence< sal_Int8 > aClassID = GetSequenceClassIDRepresentation( aStringClassID );
504 return "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
505 }
506 }
507
508 return aResult;
509}
510
511
512OUString MimeConfigurationHelper::GetFactoryNameByDocumentName( std::u16string_view aDocName )
513{
514 OUString aResult;
515
516 if ( !aDocName.empty() )
517 {
518 uno::Reference< container::XNameAccess > xObjConfig = GetObjConfiguration();
519 if ( xObjConfig.is() )
520 {
521 try
522 {
523 const uno::Sequence< OUString > aClassIDs = xObjConfig->getElementNames();
524 for ( const OUString & id : aClassIDs )
525 {
526 uno::Reference< container::XNameAccess > xObjectProps;
527 OUString aEntryDocName;
528
529 if ( ( xObjConfig->getByName( id ) >>= xObjectProps ) && xObjectProps.is()
530 && ( xObjectProps->getByName( "ObjectDocumentServiceName" ) >>= aEntryDocName )
531 && aEntryDocName == aDocName )
532 {
533 xObjectProps->getByName("ObjectFactory") >>= aResult;
534 break;
535 }
536 }
537 }
538 catch( uno::Exception& )
539 {}
540 }
541 }
542
543 return aResult;
544}
545
546
547OUString MimeConfigurationHelper::GetFactoryNameByMediaType( const OUString& aMediaType )
548{
549 OUString aResult = GetFactoryNameByStringClassID( GetExplicitlyRegisteredObjClassID( aMediaType ) );
550
551 if ( aResult.isEmpty() )
552 {
553 OUString aDocumentName = GetDocServiceNameFromMediaType( aMediaType );
554 if ( !aDocumentName.isEmpty() )
555 aResult = GetFactoryNameByDocumentName( aDocumentName );
556 }
557
558 return aResult;
559}
560
561
563 uno::Sequence< beans::PropertyValue >& aMediaDescr,
564 bool bIgnoreType )
565{
566 OUString aFilterName;
567
568 for ( const auto & prop : std::as_const(aMediaDescr) )
569 if ( prop.Name == "FilterName" )
570 prop.Value >>= aFilterName;
571
572 if ( aFilterName.isEmpty() )
573 {
574 // filter name is not specified, so type detection should be done
575
576 uno::Reference< document::XTypeDetection > xTypeDetection(
577 m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.document.TypeDetection", m_xContext),
578 uno::UNO_QUERY_THROW );
579
580 // typedetection can change the mode, add a stream and so on, thus a copy should be used
581 uno::Sequence< beans::PropertyValue > aTempMD( aMediaDescr );
582
583 // get TypeName
584 OUString aTypeName = xTypeDetection->queryTypeByDescriptor( aTempMD, true );
585
586 // get FilterName
587 for ( const auto & prop : std::as_const(aTempMD) )
588 if ( prop.Name == "FilterName" )
589 prop.Value >>= aFilterName;
590
591 if ( !aFilterName.isEmpty() )
592 {
593 sal_Int32 nOldLen = aMediaDescr.getLength();
594 aMediaDescr.realloc( nOldLen + 1 );
595 auto pMediaDescr = aMediaDescr.getArray();
596 pMediaDescr[nOldLen].Name = "FilterName";
597 pMediaDescr[ nOldLen ].Value <<= aFilterName;
598
599 }
600 else if ( !aTypeName.isEmpty() && !bIgnoreType )
601 {
602 uno::Reference< container::XNameAccess > xNameAccess( xTypeDetection, uno::UNO_QUERY );
603 uno::Sequence< beans::PropertyValue > aTypes;
604
605 if ( xNameAccess.is() && ( xNameAccess->getByName( aTypeName ) >>= aTypes ) )
606 {
607 for ( const auto & prop : std::as_const(aTypes) )
608 {
609 if ( prop.Name == "PreferredFilter" && ( prop.Value >>= aFilterName ) )
610 {
611 sal_Int32 nOldLen = aMediaDescr.getLength();
612 aMediaDescr.realloc( nOldLen + 1 );
613 auto pMediaDescr = aMediaDescr.getArray();
614 pMediaDescr[nOldLen].Name = "FilterName";
615 pMediaDescr[ nOldLen ].Value = prop.Value;
616 break;
617 }
618 }
619 }
620 }
621 }
622
623 return aFilterName;
624}
625
627 uno::Sequence< beans::PropertyValue >& aMediaDescr,
628 uno::Sequence< beans::NamedValue >& aObject )
629{
630 OUString aDocName;
631 for ( const auto & nv : std::as_const(aObject) )
632 if ( nv.Name == "ObjectDocumentServiceName" )
633 {
634 nv.Value >>= aDocName;
635 break;
636 }
637
638 OSL_ENSURE( !aDocName.isEmpty(), "The name must exist at this point!" );
639
640
641 bool bNeedsAddition = true;
642 for ( sal_Int32 nMedInd = 0; nMedInd < aMediaDescr.getLength(); nMedInd++ )
643 if ( aMediaDescr[nMedInd].Name == "DocumentService" )
644 {
645 aMediaDescr.getArray()[nMedInd].Value <<= aDocName;
646 bNeedsAddition = false;
647 break;
648 }
649
650 if ( bNeedsAddition )
651 {
652 sal_Int32 nOldLen = aMediaDescr.getLength();
653 aMediaDescr.realloc( nOldLen + 1 );
654 auto pMediaDescr = aMediaDescr.getArray();
655 pMediaDescr[nOldLen].Name = "DocumentService";
656 pMediaDescr[nOldLen].Value <<= aDocName;
657 }
658
659 return UpdateMediaDescriptorWithFilterName( aMediaDescr, true );
660}
661
662#ifdef _WIN32
663
664SfxFilterFlags MimeConfigurationHelper::GetFilterFlags( const OUString& aFilterName )
665{
667 try
668 {
669 if ( !aFilterName.isEmpty() )
670 {
671 uno::Reference< container::XNameAccess > xFilterFactory(
673 uno::UNO_SET_THROW );
674
675 uno::Any aFilterAny = xFilterFactory->getByName( aFilterName );
676 uno::Sequence< beans::PropertyValue > aData;
677 if ( aFilterAny >>= aData )
678 {
679 SequenceAsHashMap aFilterHM( aData );
680 nFlags = static_cast<SfxFilterFlags>(aFilterHM.getUnpackedValueOrDefault( "Flags", sal_Int32(0) ));
681 }
682 }
683 } catch( uno::Exception& )
684 {}
685
686 return nFlags;
687}
688
689bool MimeConfigurationHelper::AddFilterNameCheckOwnFile(
690 uno::Sequence< beans::PropertyValue >& aMediaDescr )
691{
692 OUString aFilterName = UpdateMediaDescriptorWithFilterName( aMediaDescr, false );
693 if ( !aFilterName.isEmpty() )
694 {
695 SfxFilterFlags nFlags = GetFilterFlags( aFilterName );
696 // check the OWN flag
697 return bool(nFlags & SfxFilterFlags::OWN);
698 }
699
700 return false;
701}
702
703#endif
704
705OUString MimeConfigurationHelper::GetDefaultFilterFromServiceName( const OUString& aServiceName, sal_Int32 nVersion )
706{
707 OUString aResult;
708
709 if ( !aServiceName.isEmpty() && nVersion )
710 try
711 {
712 uno::Reference< container::XContainerQuery > xFilterQuery(
714 uno::UNO_QUERY_THROW );
715
716 uno::Sequence< beans::NamedValue > aSearchRequest
717 {
718 { "DocumentService", css::uno::Any(aServiceName) },
719 { "FileFormatVersion", css::uno::Any(nVersion) }
720 };
721
722 uno::Reference< container::XEnumeration > xFilterEnum =
723 xFilterQuery->createSubSetEnumerationByProperties( aSearchRequest );
724
725 // use the first filter that is found
726 if ( xFilterEnum.is() )
727 while ( xFilterEnum->hasMoreElements() )
728 {
729 uno::Sequence< beans::PropertyValue > aProps;
730 if ( xFilterEnum->nextElement() >>= aProps )
731 {
733 OUString sName;
734 for (const auto & rPropVal : aProps)
735 {
736 if (rPropVal.Name == "Flags")
737 {
738 sal_Int32 nTmp(0);
739 if (rPropVal.Value >>= nTmp)
740 nFlags = static_cast<SfxFilterFlags>(nTmp);
741 }
742 else if (rPropVal.Name == "Name")
743 rPropVal.Value >>= sName;
744 }
745
746 // that should be import, export, own filter and not a template filter ( TemplatePath flag )
747 SfxFilterFlags const nRequired = SfxFilterFlags::OWN
748 // fdo#78159 for OOoXML, there is code to convert
749 // to ODF in OCommonEmbeddedObject::store*
750 // so accept it even though there's no export
753 if ( ( ( nFlags & nRequired ) == nRequired ) && !( nFlags & SfxFilterFlags::TEMPLATEPATH ) )
754 {
755 // if there are more than one filter the preferred one should be used
756 // if there is no preferred filter the first one will be used
757 if ( aResult.isEmpty() || ( nFlags & SfxFilterFlags::PREFERED ) )
758 aResult = sName;
759 if ( nFlags & SfxFilterFlags::PREFERED )
760 break; // the preferred filter was found
761 }
762 }
763 }
764 }
765 catch( uno::Exception& )
766 {}
767
768 return aResult;
769}
770
771
772OUString MimeConfigurationHelper::GetExportFilterFromImportFilter( const OUString& aImportFilterName )
773{
774 OUString aExportFilterName;
775
776 try
777 {
778 if ( !aImportFilterName.isEmpty() )
779 {
780 uno::Reference< container::XNameAccess > xFilterFactory(
782 uno::UNO_SET_THROW );
783
784 uno::Any aImpFilterAny = xFilterFactory->getByName( aImportFilterName );
785 uno::Sequence< beans::PropertyValue > aImpData;
786 if ( aImpFilterAny >>= aImpData )
787 {
788 SequenceAsHashMap aImpFilterHM( aImpData );
789 SfxFilterFlags nFlags = static_cast<SfxFilterFlags>(aImpFilterHM.getUnpackedValueOrDefault( "Flags", sal_Int32(0) ));
790
791 if ( !( nFlags & SfxFilterFlags::IMPORT ) )
792 {
793 OSL_FAIL( "This is no import filter!" );
794 throw uno::Exception("this is no import filter", nullptr);
795 }
796
797 if ( nFlags & SfxFilterFlags::EXPORT )
798 {
799 aExportFilterName = aImportFilterName;
800 }
801 else
802 {
803 OUString aDocumentServiceName = aImpFilterHM.getUnpackedValueOrDefault( "DocumentService", OUString() );
804 OUString aTypeName = aImpFilterHM.getUnpackedValueOrDefault( "Type", OUString() );
805
806 OSL_ENSURE( !aDocumentServiceName.isEmpty() && !aTypeName.isEmpty(), "Incomplete filter data!" );
807 if ( !(aDocumentServiceName.isEmpty() || aTypeName.isEmpty()) )
808 {
809 uno::Sequence< beans::NamedValue > aSearchRequest
810 {
811 { "Type", css::uno::Any(aTypeName) },
812 { "DocumentService", css::uno::Any(aDocumentServiceName) }
813 };
814
815 uno::Sequence< beans::PropertyValue > aExportFilterProps = SearchForFilter(
816 uno::Reference< container::XContainerQuery >( xFilterFactory, uno::UNO_QUERY_THROW ),
817 aSearchRequest,
820
821 if ( aExportFilterProps.hasElements() )
822 {
823 SequenceAsHashMap aExpPropsHM( aExportFilterProps );
824 aExportFilterName = aExpPropsHM.getUnpackedValueOrDefault( "Name", OUString() );
825 }
826 }
827 }
828 }
829 }
830 }
831 catch( uno::Exception& )
832 {}
833
834 return aExportFilterName;
835}
836
837
838// static
839uno::Sequence< beans::PropertyValue > MimeConfigurationHelper::SearchForFilter(
840 const uno::Reference< container::XContainerQuery >& xFilterQuery,
841 const uno::Sequence< beans::NamedValue >& aSearchRequest,
842 SfxFilterFlags nMustFlags,
843 SfxFilterFlags nDontFlags )
844{
845 uno::Sequence< beans::PropertyValue > aFilterProps;
846 uno::Reference< container::XEnumeration > xFilterEnum =
847 xFilterQuery->createSubSetEnumerationByProperties( aSearchRequest );
848
849 // the first default filter will be taken,
850 // if there is no filter with flag default the first acceptable filter will be taken
851 if ( xFilterEnum.is() )
852 {
853 while ( xFilterEnum->hasMoreElements() )
854 {
855 uno::Sequence< beans::PropertyValue > aProps;
856 if ( xFilterEnum->nextElement() >>= aProps )
857 {
858 SequenceAsHashMap aPropsHM( aProps );
859 SfxFilterFlags nFlags = static_cast<SfxFilterFlags>(aPropsHM.getUnpackedValueOrDefault("Flags",
860 sal_Int32(0) ));
861 if ( ( ( nFlags & nMustFlags ) == nMustFlags ) && !( nFlags & nDontFlags ) )
862 {
864 {
865 aFilterProps = aProps;
866 break;
867 }
868 else if ( !aFilterProps.hasElements() )
869 aFilterProps = aProps;
870 }
871 }
872 }
873 }
874
875 return aFilterProps;
876}
877
878
879bool MimeConfigurationHelper::ClassIDsEqual( const uno::Sequence< sal_Int8 >& aClassID1, const uno::Sequence< sal_Int8 >& aClassID2 )
880{
881 return aClassID1 == aClassID2;
882}
883
884
885uno::Sequence< sal_Int8 > MimeConfigurationHelper::GetSequenceClassID( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
886 sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
887 sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
888{
889 uno::Sequence< sal_Int8 > aResult{ /* [ 0] */ static_cast<sal_Int8>( n1 >> 24 ),
890 /* [ 1] */ static_cast<sal_Int8>( ( n1 << 8 ) >> 24 ),
891 /* [ 2] */ static_cast<sal_Int8>( ( n1 << 16 ) >> 24 ),
892 /* [ 3] */ static_cast<sal_Int8>( ( n1 << 24 ) >> 24 ),
893 /* [ 4] */ static_cast<sal_Int8>( n2 >> 8 ),
894 /* [ 5] */ static_cast<sal_Int8>( ( n2 << 8 ) >> 8 ),
895 /* [ 6] */ static_cast<sal_Int8>( n3 >> 8 ),
896 /* [ 7] */ static_cast<sal_Int8>( ( n3 << 8 ) >> 8 ),
897 /* [ 8] */ static_cast<sal_Int8>( b8 ),
898 /* [ 9] */ static_cast<sal_Int8>( b9 ),
899 /* [10] */ static_cast<sal_Int8>( b10 ),
900 /* [11] */ static_cast<sal_Int8>( b11 ),
901 /* [12] */ static_cast<sal_Int8>( b12 ),
902 /* [13] */ static_cast<sal_Int8>( b13 ),
903 /* [14] */ static_cast<sal_Int8>( b14 ),
904 /* [15] */ static_cast<sal_Int8>( b15 ) };
905
906 return aResult;
907}
908
909/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
css::uno::Reference< css::container::XNameAccess > m_xFilterFactory
OUString GetFactoryNameByStringClassID(const OUString &aStringClassID)
css::uno::Reference< css::container::XNameAccess > m_xMediaTypeConfig
css::uno::Sequence< css::beans::NamedValue > GetObjPropsFromConfigEntry(const css::uno::Sequence< sal_Int8 > &aClassID, const css::uno::Reference< css::container::XNameAccess > &xObjectProps)
css::uno::Reference< css::container::XNameAccess > GetVerbsConfiguration()
css::uno::Reference< css::container::XNameAccess > m_xObjectConfig
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
OUString GetExportFilterFromImportFilter(const OUString &aImportFilterName)
OUString GetDefaultFilterFromServiceName(const OUString &aServName, sal_Int32 nVersion)
css::uno::Reference< css::container::XNameAccess > GetConfigurationByPathImpl(const OUString &aPath)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByDocumentName(std::u16string_view aDocumentName)
OUString GetDocServiceNameFromMediaType(const OUString &aMediaType)
static OUString GetStringClassIDRepresentation(const css::uno::Sequence< sal_Int8 > &aClassID)
static css::uno::Sequence< sal_Int8 > GetSequenceClassIDRepresentation(std::u16string_view aClassID)
static css::uno::Sequence< css::beans::PropertyValue > SearchForFilter(const css::uno::Reference< css::container::XContainerQuery > &xFilterQuery, const css::uno::Sequence< css::beans::NamedValue > &aSearchRequest, SfxFilterFlags nMustFlags, SfxFilterFlags nDontFlags)
OUString GetFactoryNameByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
OUString GetFactoryNameByMediaType(const OUString &aMediaType)
OUString UpdateMediaDescriptorWithFilterName(css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, bool bIgnoreType)
static css::uno::Sequence< sal_Int8 > GetSequenceClassID(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3, sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11, sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15)
css::uno::Reference< css::container::XNameAccess > m_xVerbsConfig
css::uno::Reference< css::uno::XComponentContext > m_xContext
OUString GetFactoryNameByDocumentName(std::u16string_view aDocName)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByMediaType(const OUString &aMediaType)
OUString GetExplicitlyRegisteredObjClassID(const OUString &aMediaType)
css::uno::Reference< css::container::XNameAccess > GetFilterFactory()
css::uno::Reference< css::container::XNameAccess > GetMediaTypeConfiguration()
css::uno::Reference< css::container::XNameAccess > GetObjConfiguration()
static bool ClassIDsEqual(const css::uno::Sequence< sal_Int8 > &aClassID1, const css::uno::Sequence< sal_Int8 > &aClassID2)
bool GetVerbByShortcut(const OUString &aVerbShortcut, css::embed::VerbDescriptor &aDescriptor)
css::uno::Reference< css::lang::XMultiServiceFactory > m_xConfigProvider
OUString GetDocServiceNameFromFilter(const OUString &aFilterName)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByFilter(const OUString &aFilterName)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByStringClassID(const OUString &aStringClassID)
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
check if the specified item exists and return its (unpacked!) value or it returns the specified defau...
#define SO3_DUMMY_CLASSID
Definition: classids.hxx:312
SfxFilterFlags
float u
sal_Int16 nVersion
#define SOFFICE_FILEFORMAT_60
Definition: fileformat.h:28
OUString sName
Sequence< sal_Int8 > aSeq
static sal_uInt8 GetDigit_Impl(char aChar)
int n2
int n1
sal_uInt32 n3
constexpr OUStringLiteral aData
css::uno::Sequence< css::uno::Any > InitAnyPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
Init list for property sequences that wrap the PropertyValues in Anys.
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
const char *const aClassID
Object Value
unsigned char sal_uInt8
signed char sal_Int8
const SvXMLTokenMapEntry aTypes[]
sal_Int32 nLength