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>
27 #include <osl/diagnose.h>
35 #include <rtl/ustrbuf.hxx>
46 throw uno::RuntimeException();
52 OUStringBuffer aResult;
54 if ( aClassID.getLength() == 16 )
56 for ( sal_Int32 nInd = 0; nInd < aClassID.getLength(); nInd++ )
58 if ( nInd == 4 || nInd == 6 || nInd == 8 || nInd == 10 )
61 sal_Int32 nDigit1 =
static_cast<sal_Int32
>(
static_cast<sal_uInt8>(aClassID[nInd]) / 16 );
62 sal_Int32 nDigit2 =
static_cast<sal_uInt8>(aClassID[nInd]) % 16;
63 aResult.append(OUString::number( nDigit1, 16 )).append(OUString::number( nDigit2, 16 ));
67 return aResult.makeStringAndClear();
73 if ( aChar >=
'0' && aChar <=
'9' )
75 else if ( aChar >=
'a' && aChar <=
'f' )
76 return aChar -
'a' + 10;
77 else if ( aChar >=
'A' && aChar <=
'F' )
78 return aChar -
'A' + 10;
86 sal_Int32
nLength = aClassID.getLength();
90 uno::Sequence< sal_Int8 > aResult( 16 );
92 sal_Int32 nStrPointer = 0;
93 sal_Int32 nSeqInd = 0;
94 while( nSeqInd < 16 && nStrPointer + 1 < nLength )
99 if ( nDigit1 > 15 || nDigit2 > 15 )
102 aResult[nSeqInd++] =
static_cast<sal_Int8>( nDigit1 * 16 + nDigit2 );
104 if ( nStrPointer < nLength && aCharClassID[nStrPointer] ==
'-' )
108 if ( nSeqInd == 16 && nStrPointer == nLength )
112 return uno::Sequence< sal_Int8 >();
120 uno::Reference< container::XNameAccess > xConfig;
132 "com.sun.star.configuration.ConfigurationAccess",
136 catch( uno::Exception& )
149 "/org.openoffice.Office.Embedding/Objects" );
161 "/org.openoffice.Office.Embedding/Verbs");
173 "/org.openoffice.Office.Embedding/MimeTypeClassIDRelations");
185 m_xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.FilterFactory",
m_xContext),
194 OUString aDocServiceName;
198 uno::Reference< container::XNameAccess > xFilterFactory(
200 uno::UNO_SET_THROW );
202 uno::Any aFilterAnyData = xFilterFactory->getByName( aFilterName );
203 uno::Sequence< beans::PropertyValue > aFilterData;
204 if ( aFilterAnyData >>= aFilterData )
206 for (
const auto & prop : std::as_const(aFilterData) )
207 if ( prop.Name ==
"DocumentService" )
208 prop.
Value >>= aDocServiceName;
211 catch( uno::Exception& )
214 return aDocServiceName;
220 uno::Reference< container::XContainerQuery > xTypeCFG(
221 m_xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.TypeDetection",
m_xContext),
229 uno::Sequence < beans::NamedValue >
aSeq { {
"MediaType", css::uno::Any(aMediaType) } };
231 uno::Reference < container::XEnumeration > xEnum = xTypeCFG->createSubSetEnumerationByProperties(
aSeq );
232 while ( xEnum->hasMoreElements() )
234 uno::Sequence< beans::PropertyValue > aType;
235 if ( xEnum->nextElement() >>= aType )
237 for (
const auto & prop : std::as_const(aType) )
239 OUString aFilterName;
240 if ( prop.Name ==
"PreferredFilter"
241 && ( prop.Value >>= aFilterName ) && !aFilterName.isEmpty() )
244 if ( !aDocumentName.isEmpty() )
245 return aDocumentName;
251 catch( uno::Exception& )
260 embed::VerbDescriptor& aDescriptor )
262 bool bResult =
false;
265 uno::Reference< container::XNameAccess > xVerbsProps;
268 if ( xVerbsConfig.is() && ( xVerbsConfig->getByName( aVerbShortcut ) >>= xVerbsProps ) && xVerbsProps.is() )
270 embed::VerbDescriptor aTempDescr;
271 if ( ( xVerbsProps->getByName(
"VerbID") >>= aTempDescr.VerbID )
272 && ( xVerbsProps->getByName(
"VerbUIName") >>= aTempDescr.VerbName )
273 && ( xVerbsProps->getByName(
"VerbFlags") >>= aTempDescr.VerbFlags )
274 && ( xVerbsProps->getByName(
"VerbAttributes") >>= aTempDescr.VerbAttributes ) )
276 aDescriptor = aTempDescr;
281 catch( uno::Exception& )
290 const uno::Sequence< sal_Int8 >& aClassID,
291 const uno::Reference< container::XNameAccess >& xObjectProps )
293 uno::Sequence< beans::NamedValue > aResult;
295 if ( aClassID.getLength() == 16 )
299 uno::Sequence< OUString > aObjPropNames = xObjectProps->getElementNames();
301 aResult.realloc( aObjPropNames.getLength() + 1 );
302 aResult[0].Name =
"ClassID";
303 aResult[0].Value <<= aClassID;
305 for ( sal_Int32 nInd = 0; nInd < aObjPropNames.getLength(); nInd++ )
307 aResult[nInd + 1].Name = aObjPropNames[nInd];
309 if ( aObjPropNames[nInd] ==
"ObjectVerbs" )
311 uno::Sequence< OUString > aVerbShortcuts;
312 if ( !(xObjectProps->getByName( aObjPropNames[nInd] ) >>= aVerbShortcuts) )
313 throw uno::RuntimeException();
314 uno::Sequence< embed::VerbDescriptor > aVerbDescriptors( aVerbShortcuts.getLength() );
315 for ( sal_Int32 nVerbI = 0; nVerbI < aVerbShortcuts.getLength(); nVerbI++ )
317 throw uno::RuntimeException();
319 aResult[nInd+1].Value <<= aVerbDescriptors;
322 aResult[nInd+1].Value = xObjectProps->getByName( aObjPropNames[nInd] );
325 catch( uno::Exception& )
327 aResult.realloc( 0 );
337 OUString aStringClassID;
342 if ( xMediaTypeConfig.is() )
343 xMediaTypeConfig->getByName( aMediaType ) >>= aStringClassID;
345 catch( uno::Exception& )
349 return aStringClassID;
355 const OUString& aStringClassID )
357 uno::Sequence< beans::NamedValue > aObjProps;
362 aObjProps.realloc(2);
363 aObjProps[0].Name =
"ObjectFactory";
364 aObjProps[0].Value <<= OUString(
"com.sun.star.embed.OOoSpecialEmbeddedObjectFactory" );
365 aObjProps[1].Name =
"ClassID";
366 aObjProps[1].Value <<= aClassID;
370 if ( aClassID.getLength() == 16 )
373 uno::Reference< container::XNameAccess > xObjectProps;
377 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
380 catch( uno::Exception& )
390 const uno::Sequence< sal_Int8 >& aClassID )
392 uno::Sequence< beans::NamedValue > aObjProps;
395 aObjProps.realloc(2);
396 aObjProps[0].Name =
"ObjectFactory";
397 aObjProps[0].Value <<= OUString(
"com.sun.star.embed.OOoSpecialEmbeddedObjectFactory" );
398 aObjProps[1].Name =
"ClassID";
399 aObjProps[1].Value <<= aClassID;
403 if ( !aStringClassID.isEmpty() )
406 uno::Reference< container::XNameAccess > xObjectProps;
409 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
412 catch( uno::Exception& )
423 uno::Sequence< beans::NamedValue > aObject =
425 if ( aObject.hasElements() )
429 if ( !aDocumentName.isEmpty() )
432 return uno::Sequence< beans::NamedValue >();
439 if ( !aDocumentName.isEmpty() )
442 return uno::Sequence< beans::NamedValue >();
448 if ( !aDocName.empty() )
451 if ( xObjConfig.is() )
455 const uno::Sequence< OUString > aClassIDs = xObjConfig->getElementNames();
456 for (
const OUString &
id : aClassIDs )
458 uno::Reference< container::XNameAccess > xObjectProps;
459 OUString aEntryDocName;
461 if ( ( xObjConfig->getByName(
id ) >>= xObjectProps ) && xObjectProps.is()
462 && ( xObjectProps->getByName(
"ObjectDocumentServiceName") >>= aEntryDocName )
463 && aEntryDocName == aDocName )
470 catch( uno::Exception& )
475 return uno::Sequence< beans::NamedValue >();
489 if ( !aStringClassID.isEmpty() )
492 uno::Reference< container::XNameAccess > xObjectProps;
495 if ( xObjConfig.is() && ( xObjConfig->getByName( aStringClassID.toAsciiUpperCase() ) >>= xObjectProps ) && xObjectProps.is() )
496 xObjectProps->getByName(
"ObjectFactory") >>= aResult;
498 catch( uno::Exception& )
502 return "com.sun.star.embed.OOoSpecialEmbeddedObjectFactory";
514 if ( !aDocName.empty() )
517 if ( xObjConfig.is() )
521 const uno::Sequence< OUString > aClassIDs = xObjConfig->getElementNames();
522 for (
const OUString &
id : aClassIDs )
524 uno::Reference< container::XNameAccess > xObjectProps;
525 OUString aEntryDocName;
527 if ( ( xObjConfig->getByName(
id ) >>= xObjectProps ) && xObjectProps.is()
528 && ( xObjectProps->getByName(
"ObjectDocumentServiceName" ) >>= aEntryDocName )
529 && aEntryDocName == aDocName )
531 xObjectProps->getByName(
"ObjectFactory") >>= aResult;
536 catch( uno::Exception& )
549 if ( aResult.isEmpty() )
552 if ( !aDocumentName.isEmpty() )
561 uno::Sequence< beans::PropertyValue >& aMediaDescr,
564 OUString aFilterName;
566 for (
const auto & prop : std::as_const(aMediaDescr) )
567 if ( prop.Name ==
"FilterName" )
568 prop.Value >>= aFilterName;
570 if ( aFilterName.isEmpty() )
574 uno::Reference< document::XTypeDetection > xTypeDetection(
575 m_xContext->getServiceManager()->createInstanceWithContext(
"com.sun.star.document.TypeDetection",
m_xContext),
576 uno::UNO_QUERY_THROW );
579 uno::Sequence< beans::PropertyValue > aTempMD( aMediaDescr );
582 OUString aTypeName = xTypeDetection->queryTypeByDescriptor( aTempMD,
true );
585 for (
const auto & prop : std::as_const(aTempMD) )
586 if ( prop.Name ==
"FilterName" )
587 prop.Value >>= aFilterName;
589 if ( !aFilterName.isEmpty() )
591 sal_Int32 nOldLen = aMediaDescr.getLength();
592 aMediaDescr.realloc( nOldLen + 1 );
593 aMediaDescr[nOldLen].Name =
"FilterName";
594 aMediaDescr[ nOldLen ].Value <<= aFilterName;
597 else if ( !aTypeName.isEmpty() && !bIgnoreType )
599 uno::Reference< container::XNameAccess > xNameAccess( xTypeDetection, uno::UNO_QUERY );
600 uno::Sequence< beans::PropertyValue >
aTypes;
602 if ( xNameAccess.is() && ( xNameAccess->getByName( aTypeName ) >>= aTypes ) )
604 for (
const auto & prop : std::as_const(aTypes) )
606 if ( prop.Name ==
"PreferredFilter" && ( prop.Value >>= aFilterName ) )
608 sal_Int32 nOldLen = aMediaDescr.getLength();
609 aMediaDescr.realloc( nOldLen + 1 );
610 aMediaDescr[nOldLen].Name =
"FilterName";
611 aMediaDescr[ nOldLen ].Value = prop.Value;
623 uno::Sequence< beans::PropertyValue >& aMediaDescr,
624 uno::Sequence< beans::NamedValue >& aObject )
627 for (
const auto & nv : std::as_const(aObject) )
628 if ( nv.Name ==
"ObjectDocumentServiceName" )
630 nv.Value >>= aDocName;
634 OSL_ENSURE( !aDocName.isEmpty(),
"The name must exist at this point!" );
637 bool bNeedsAddition =
true;
638 for ( sal_Int32 nMedInd = 0; nMedInd < aMediaDescr.getLength(); nMedInd++ )
639 if ( aMediaDescr[nMedInd].Name ==
"DocumentService" )
641 aMediaDescr[nMedInd].Value <<= aDocName;
642 bNeedsAddition =
false;
646 if ( bNeedsAddition )
648 sal_Int32 nOldLen = aMediaDescr.getLength();
649 aMediaDescr.realloc( nOldLen + 1 );
650 aMediaDescr[nOldLen].Name =
"DocumentService";
651 aMediaDescr[nOldLen].Value <<= aDocName;
659 SfxFilterFlags MimeConfigurationHelper::GetFilterFlags(
const OUString& aFilterName )
664 if ( !aFilterName.isEmpty() )
666 uno::Reference< container::XNameAccess > xFilterFactory(
668 uno::UNO_SET_THROW );
670 uno::Any aFilterAny = xFilterFactory->getByName( aFilterName );
671 uno::Sequence< beans::PropertyValue >
aData;
672 if ( aFilterAny >>= aData )
675 nFlags =
static_cast<SfxFilterFlags>(aFilterHM.getUnpackedValueOrDefault(
"Flags", sal_Int32(0) ));
678 }
catch( uno::Exception& )
684 bool MimeConfigurationHelper::AddFilterNameCheckOwnFile(
685 uno::Sequence< beans::PropertyValue >& aMediaDescr )
688 if ( !aFilterName.isEmpty() )
704 if ( !aServiceName.isEmpty() &&
nVersion )
707 uno::Reference< container::XContainerQuery > xFilterQuery(
709 uno::UNO_QUERY_THROW );
711 uno::Sequence< beans::NamedValue > aSearchRequest
713 {
"DocumentService", css::uno::Any(aServiceName) },
714 {
"FileFormatVersion", css::uno::Any(nVersion) }
717 uno::Reference< container::XEnumeration > xFilterEnum =
718 xFilterQuery->createSubSetEnumerationByProperties( aSearchRequest );
721 if ( xFilterEnum.is() )
722 while ( xFilterEnum->hasMoreElements() )
724 uno::Sequence< beans::PropertyValue > aProps;
725 if ( xFilterEnum->nextElement() >>= aProps )
749 catch( uno::Exception& )
758 OUString aExportFilterName;
762 if ( !aImportFilterName.isEmpty() )
764 uno::Reference< container::XNameAccess > xFilterFactory(
766 uno::UNO_SET_THROW );
768 uno::Any aImpFilterAny = xFilterFactory->getByName( aImportFilterName );
769 uno::Sequence< beans::PropertyValue > aImpData;
770 if ( aImpFilterAny >>= aImpData )
777 OSL_FAIL(
"This is no import filter!" );
778 throw uno::Exception(
"this is no import filter",
nullptr);
783 aExportFilterName = aImportFilterName;
790 OSL_ENSURE( !aDocumentServiceName.isEmpty() && !aTypeName.isEmpty(),
"Incomplete filter data!" );
791 if ( !(aDocumentServiceName.isEmpty() || aTypeName.isEmpty()) )
793 uno::Sequence< beans::NamedValue > aSearchRequest
795 {
"Type", css::uno::Any(aTypeName) },
796 {
"DocumentService", css::uno::Any(aDocumentServiceName) }
799 uno::Sequence< beans::PropertyValue > aExportFilterProps =
SearchForFilter(
800 uno::Reference< container::XContainerQuery >( xFilterFactory, uno::UNO_QUERY_THROW ),
802 SfxFilterFlags::EXPORT,
805 if ( aExportFilterProps.hasElements() )
815 catch( uno::Exception& )
818 return aExportFilterName;
824 const uno::Reference< container::XContainerQuery >& xFilterQuery,
825 const uno::Sequence< beans::NamedValue >& aSearchRequest,
829 uno::Sequence< beans::PropertyValue > aFilterProps;
830 uno::Reference< container::XEnumeration > xFilterEnum =
831 xFilterQuery->createSubSetEnumerationByProperties( aSearchRequest );
835 if ( xFilterEnum.is() )
837 while ( xFilterEnum->hasMoreElements() )
839 uno::Sequence< beans::PropertyValue > aProps;
840 if ( xFilterEnum->nextElement() >>= aProps )
845 if ( ( ( nFlags & nMustFlags ) == nMustFlags ) && !( nFlags & nDontFlags ) )
849 aFilterProps = aProps;
852 else if ( !aFilterProps.hasElements() )
853 aFilterProps = aProps;
865 return aClassID1 == aClassID2;
873 uno::Sequence< sal_Int8 > aResult( 16 );
874 aResult[0] =
static_cast<sal_Int8>( n1 >> 24 );
875 aResult[1] =
static_cast<sal_Int8>( ( n1 << 8 ) >> 24 );
876 aResult[2] =
static_cast<sal_Int8>( ( n1 << 16 ) >> 24 );
877 aResult[3] =
static_cast<sal_Int8>( ( n1 << 24 ) >> 24 );
878 aResult[4] =
static_cast<sal_Int8>( n2 >> 8 );
879 aResult[5] =
static_cast<sal_Int8>( ( n2 << 8 ) >> 8 );
880 aResult[6] =
static_cast<sal_Int8>( n3 >> 8 );
881 aResult[7] =
static_cast<sal_Int8>( ( n3 << 8 ) >> 8 );
css::uno::Reference< css::container::XNameAccess > GetVerbsConfiguration()
const char *const aClassID
OUString GetDefaultFilterFromServiceName(const OUString &aServName, sal_Int32 nVersion)
css::uno::Reference< css::uno::XComponentContext > m_xContext
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByFilter(const OUString &aFilterName)
bool GetVerbByShortcut(const OUString &aVerbShortcut, css::embed::VerbDescriptor &aDescriptor)
OUString UpdateMediaDescriptorWithFilterName(css::uno::Sequence< css::beans::PropertyValue > &aMediaDescr, bool bIgnoreType)
OUString GetFactoryNameByDocumentName(std::u16string_view aDocName)
OUString GetExportFilterFromImportFilter(const OUString &aImportFilterName)
css::uno::Reference< css::container::XNameAccess > GetFilterFactory()
MimeConfigurationHelper(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
OUString GetFactoryNameByStringClassID(const OUString &aStringClassID)
OUString GetDocServiceNameFromMediaType(const OUString &aMediaType)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
css::uno::Reference< css::container::XNameAccess > GetMediaTypeConfiguration()
constexpr OUStringLiteral aData
css::uno::Reference< css::container::XNameAccess > m_xVerbsConfig
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...
OUString GetFactoryNameByMediaType(const OUString &aMediaType)
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.
#define SO3_DUMMY_CLASSID
OUString GetFactoryNameByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
static css::uno::Sequence< sal_Int8 > GetSequenceClassIDRepresentation(const OUString &aClassID)
OUString GetExplicitlyRegisteredObjClassID(const OUString &aMediaType)
OUString GetDocServiceNameFromFilter(const OUString &aFilterName)
static sal_uInt8 GetDigit_Impl(char aChar)
static bool ClassIDsEqual(const css::uno::Sequence< sal_Int8 > &aClassID1, const css::uno::Sequence< sal_Int8 > &aClassID2)
static OUString GetStringClassIDRepresentation(const css::uno::Sequence< sal_Int8 > &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)
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::Sequence< css::beans::NamedValue > GetObjectPropsByStringClassID(const OUString &aStringClassID)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByClassID(const css::uno::Sequence< sal_Int8 > &aClassID)
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByDocumentName(std::u16string_view aDocumentName)
css::uno::Reference< css::container::XNameAccess > GetConfigurationByPath(const OUString &aPath)
css::uno::Reference< css::container::XNameAccess > GetObjConfiguration()
Sequence< sal_Int8 > aSeq
css::uno::Sequence< css::beans::NamedValue > GetObjectPropsByMediaType(const OUString &aMediaType)
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 > m_xFilterFactory
css::uno::Reference< css::container::XNameAccess > m_xObjectConfig
css::uno::Reference< css::lang::XMultiServiceFactory > m_xConfigProvider
Reference< XComponentContext > m_xContext
css::uno::Reference< css::container::XNameAccess > m_xMediaTypeConfig