1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mimeconfighelper.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/container/XContainerQuery.hpp>
35 #include <com/sun/star/document/XTypeDetection.hpp>
37 #include <comphelper/fileformat.h>
38 #include <comphelper/mimeconfighelper.hxx>
39 #include <comphelper/classids.hxx>
40 #include <comphelper/sequenceashashmap.hxx>
43 using namespace ::com::sun::star
;
44 using namespace comphelper
;
46 //-----------------------------------------------------------------------
47 MimeConfigurationHelper::MimeConfigurationHelper( const uno::Reference
< lang::XMultiServiceFactory
>& xFactory
)
48 : m_xFactory( xFactory
)
50 if ( !m_xFactory
.is() )
51 throw uno::RuntimeException();
54 //-----------------------------------------------------------------------
55 ::rtl::OUString
MimeConfigurationHelper::GetStringClassIDRepresentation( const uno::Sequence
< sal_Int8
>& aClassID
)
57 ::rtl::OUString aResult
;
59 if ( aClassID
.getLength() == 16 )
61 for ( sal_Int32 nInd
= 0; nInd
< aClassID
.getLength(); nInd
++ )
63 if ( nInd
== 4 || nInd
== 6 || nInd
== 8 || nInd
== 10 )
64 aResult
+= ::rtl::OUString::createFromAscii( "-" );
66 sal_Int32 nDigit1
= (sal_Int32
)( (sal_uInt8
)aClassID
[nInd
] / 16 );
67 sal_Int32 nDigit2
= (sal_uInt8
)aClassID
[nInd
] % 16;
68 aResult
+= ::rtl::OUString::valueOf( nDigit1
, 16 );
69 aResult
+= ::rtl::OUString::valueOf( nDigit2
, 16 );
76 //-----------------------------------------------------------------------
77 sal_uInt8
GetDigit_Impl( sal_Char aChar
)
79 if ( aChar
>= '0' && aChar
<= '9' )
81 else if ( aChar
>= 'a' && aChar
<= 'f' )
82 return aChar
- 'a' + 10;
83 else if ( aChar
>= 'A' && aChar
<= 'F' )
84 return aChar
- 'A' + 10;
89 //-----------------------------------------------------------------------
90 uno::Sequence
< sal_Int8
> MimeConfigurationHelper::GetSequenceClassIDRepresentation( const ::rtl::OUString
& aClassID
)
92 sal_Int32 nLength
= aClassID
.getLength();
95 ::rtl::OString aCharClassID
= ::rtl::OUStringToOString( aClassID
, RTL_TEXTENCODING_ASCII_US
);
96 const sal_Char
* pString
= aCharClassID
.getStr();
99 uno::Sequence
< sal_Int8
> aResult( 16 );
101 sal_Int32 nStrPointer
= 0;
102 sal_Int32 nSeqInd
= 0;
103 while( nSeqInd
< 16 && nStrPointer
+ 1 < nLength
)
105 sal_uInt8 nDigit1
= GetDigit_Impl( pString
[nStrPointer
++] );
106 sal_uInt8 nDigit2
= GetDigit_Impl( pString
[nStrPointer
++] );
108 if ( nDigit1
> 15 || nDigit2
> 15 )
111 aResult
[nSeqInd
++] = (sal_Int8
)( nDigit1
* 16 + nDigit2
);
113 if ( nStrPointer
< nLength
&& pString
[nStrPointer
] == '-' )
117 if ( nSeqInd
== 16 && nStrPointer
== nLength
)
122 return uno::Sequence
< sal_Int8
>();
125 //-----------------------------------------------------------------------
126 uno::Reference
< container::XNameAccess
> MimeConfigurationHelper::GetConfigurationByPath( const ::rtl::OUString
& aPath
)
128 osl::MutexGuard
aGuard( m_aMutex
);
130 uno::Reference
< container::XNameAccess
> xConfig
;
134 if ( !m_xConfigProvider
.is() )
135 m_xConfigProvider
= uno::Reference
< lang::XMultiServiceFactory
>(
136 m_xFactory
->createInstance(
137 ::rtl::OUString::createFromAscii( "com.sun.star.configuration.ConfigurationProvider" ) ),
138 uno::UNO_QUERY_THROW
);
140 uno::Sequence
< uno::Any
> aArgs( 1 );
141 beans::PropertyValue aPathProp
;
142 aPathProp
.Name
= ::rtl::OUString::createFromAscii( "nodepath" );
143 aPathProp
.Value
<<= aPath
;
144 aArgs
[0] <<= aPathProp
;
146 xConfig
= uno::Reference
< container::XNameAccess
>(
147 m_xConfigProvider
->createInstanceWithArguments(
148 ::rtl::OUString::createFromAscii( "com.sun.star.configuration.ConfigurationAccess" ),
152 catch( uno::Exception
& )
158 //-----------------------------------------------------------------------
159 uno::Reference
< container::XNameAccess
> MimeConfigurationHelper::GetObjConfiguration()
161 osl::MutexGuard
aGuard( m_aMutex
);
163 if ( !m_xObjectConfig
.is() )
164 m_xObjectConfig
= GetConfigurationByPath(
165 ::rtl::OUString::createFromAscii( "/org.openoffice.Office.Embedding/Objects" ) );
167 return m_xObjectConfig
;
170 //-----------------------------------------------------------------------
171 uno::Reference
< container::XNameAccess
> MimeConfigurationHelper::GetVerbsConfiguration()
173 osl::MutexGuard
aGuard( m_aMutex
);
175 if ( !m_xVerbsConfig
.is() )
176 m_xVerbsConfig
= GetConfigurationByPath(
177 ::rtl::OUString::createFromAscii( "/org.openoffice.Office.Embedding/Verbs" ) );
179 return m_xVerbsConfig
;
182 //-----------------------------------------------------------------------
183 uno::Reference
< container::XNameAccess
> MimeConfigurationHelper::GetMediaTypeConfiguration()
185 osl::MutexGuard
aGuard( m_aMutex
);
187 if ( !m_xMediaTypeConfig
.is() )
188 m_xMediaTypeConfig
= GetConfigurationByPath(
189 ::rtl::OUString::createFromAscii( "/org.openoffice.Office.Embedding/MimeTypeClassIDRelations" ) );
191 return m_xMediaTypeConfig
;
193 //-------------------------------------------------------------------------
194 ::rtl::OUString
MimeConfigurationHelper::GetDocServiceNameFromFilter( const ::rtl::OUString
& aFilterName
)
196 ::rtl::OUString aDocServiceName
;
200 uno::Reference
< container::XNameAccess
> xFilterFactory(
201 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.FilterFactory" ) ),
202 uno::UNO_QUERY_THROW
);
204 uno::Any aFilterAnyData
= xFilterFactory
->getByName( aFilterName
);
205 uno::Sequence
< beans::PropertyValue
> aFilterData
;
206 if ( aFilterAnyData
>>= aFilterData
)
208 for ( sal_Int32 nInd
= 0; nInd
< aFilterData
.getLength(); nInd
++ )
209 if ( aFilterData
[nInd
].Name
.equalsAscii( "DocumentService" ) )
210 aFilterData
[nInd
].Value
>>= aDocServiceName
;
213 catch( uno::Exception
& )
216 return aDocServiceName
;
219 //-------------------------------------------------------------------------
220 ::rtl::OUString
MimeConfigurationHelper::GetDocServiceNameFromMediaType( const ::rtl::OUString
& aMediaType
)
222 uno::Reference
< container::XContainerQuery
> xTypeCFG(
223 m_xFactory
->createInstance(
224 ::rtl::OUString::createFromAscii( "com.sun.star.document.TypeDetection" ) ),
231 // make query for all types matching the properties
232 uno::Sequence
< beans::NamedValue
> aSeq( 1 );
233 aSeq
[0].Name
= ::rtl::OUString::createFromAscii( "MediaType" );
234 aSeq
[0].Value
<<= aMediaType
;
236 uno::Reference
< container::XEnumeration
> xEnum
= xTypeCFG
->createSubSetEnumerationByProperties( aSeq
);
237 while ( xEnum
->hasMoreElements() )
239 uno::Sequence
< beans::PropertyValue
> aType
;
240 if ( xEnum
->nextElement() >>= aType
)
242 for ( sal_Int32 nInd
= 0; nInd
< aType
.getLength(); nInd
++ )
244 ::rtl::OUString aFilterName
;
245 if ( aType
[nInd
].Name
.equalsAscii( "PreferredFilter" )
246 && ( aType
[nInd
].Value
>>= aFilterName
) && aFilterName
.getLength() )
248 ::rtl::OUString aDocumentName
= GetDocServiceNameFromFilter( aFilterName
);
249 if ( aDocumentName
.getLength() )
250 return aDocumentName
;
256 catch( uno::Exception
& )
260 return ::rtl::OUString();
263 //-------------------------------------------------------------------------
264 sal_Bool
MimeConfigurationHelper::GetVerbByShortcut( const ::rtl::OUString
& aVerbShortcut
,
265 embed::VerbDescriptor
& aDescriptor
)
267 sal_Bool bResult
= sal_False
;
269 uno::Reference
< container::XNameAccess
> xVerbsConfig
= GetVerbsConfiguration();
270 uno::Reference
< container::XNameAccess
> xVerbsProps
;
273 if ( xVerbsConfig
.is() && ( xVerbsConfig
->getByName( aVerbShortcut
) >>= xVerbsProps
) && xVerbsProps
.is() )
275 embed::VerbDescriptor aTempDescr
;
276 if ( ( xVerbsProps
->getByName( ::rtl::OUString::createFromAscii( "VerbID" ) ) >>= aTempDescr
.VerbID
)
277 && ( xVerbsProps
->getByName( ::rtl::OUString::createFromAscii( "VerbUIName" ) ) >>= aTempDescr
.VerbName
)
278 && ( xVerbsProps
->getByName( ::rtl::OUString::createFromAscii( "VerbFlags" ) ) >>= aTempDescr
.VerbFlags
)
279 && ( xVerbsProps
->getByName( ::rtl::OUString::createFromAscii( "VerbAttributes" ) ) >>= aTempDescr
.VerbAttributes
) )
281 aDescriptor
= aTempDescr
;
286 catch( uno::Exception
& )
293 //-------------------------------------------------------------------------
294 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjPropsFromConfigEntry(
295 const uno::Sequence
< sal_Int8
>& aClassID
,
296 const uno::Reference
< container::XNameAccess
>& xObjectProps
)
298 uno::Sequence
< beans::NamedValue
> aResult
;
300 if ( aClassID
.getLength() == 16 )
304 uno::Sequence
< ::rtl::OUString
> aObjPropNames
= xObjectProps
->getElementNames();
306 aResult
.realloc( aObjPropNames
.getLength() + 1 );
307 aResult
[0].Name
= ::rtl::OUString::createFromAscii( "ClassID" );
308 aResult
[0].Value
<<= aClassID
;
310 for ( sal_Int32 nInd
= 0; nInd
< aObjPropNames
.getLength(); nInd
++ )
312 aResult
[nInd
+ 1].Name
= aObjPropNames
[nInd
];
314 if ( aObjPropNames
[nInd
].equalsAscii( "ObjectVerbs" ) )
316 uno::Sequence
< ::rtl::OUString
> aVerbShortcuts
;
317 if ( xObjectProps
->getByName( aObjPropNames
[nInd
] ) >>= aVerbShortcuts
)
319 uno::Sequence
< embed::VerbDescriptor
> aVerbDescriptors( aVerbShortcuts
.getLength() );
320 for ( sal_Int32 nVerbI
= 0; nVerbI
< aVerbShortcuts
.getLength(); nVerbI
++ )
321 if ( !GetVerbByShortcut( aVerbShortcuts
[nVerbI
], aVerbDescriptors
[nVerbI
] ) )
322 throw uno::RuntimeException();
324 aResult
[nInd
+1].Value
<<= aVerbDescriptors
;
327 throw uno::RuntimeException();
330 aResult
[nInd
+1].Value
= xObjectProps
->getByName( aObjPropNames
[nInd
] );
333 catch( uno::Exception
& )
335 aResult
.realloc( 0 );
342 //-----------------------------------------------------------------------
343 ::rtl::OUString
MimeConfigurationHelper::GetExplicitlyRegisteredObjClassID( const ::rtl::OUString
& aMediaType
)
345 ::rtl::OUString aStringClassID
;
347 uno::Reference
< container::XNameAccess
> xMediaTypeConfig
= GetMediaTypeConfiguration();
350 if ( xMediaTypeConfig
.is() )
351 xMediaTypeConfig
->getByName( aMediaType
) >>= aStringClassID
;
353 catch( uno::Exception
& )
357 return aStringClassID
;
361 //-----------------------------------------------------------------------
362 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjectPropsByStringClassID(
363 const ::rtl::OUString
& aStringClassID
)
365 uno::Sequence
< beans::NamedValue
> aObjProps
;
367 uno::Sequence
< sal_Int8
> aClassID
= GetSequenceClassIDRepresentation( aStringClassID
);
368 if ( ClassIDsEqual( aClassID
, GetSequenceClassID( SO3_DUMMY_CLASSID
) ) )
370 aObjProps
.realloc(2);
371 aObjProps
[0].Name
= ::rtl::OUString::createFromAscii("ObjectFactory");
372 aObjProps
[0].Value
<<= ::rtl::OUString::createFromAscii("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory");
373 aObjProps
[1].Name
= ::rtl::OUString::createFromAscii("ClassID");
374 aObjProps
[1].Value
<<= aClassID
;
378 if ( aClassID
.getLength() == 16 )
380 uno::Reference
< container::XNameAccess
> xObjConfig
= GetObjConfiguration();
381 uno::Reference
< container::XNameAccess
> xObjectProps
;
384 // TODO/LATER: allow to provide ClassID string in any format, only digits are counted
385 if ( xObjConfig
.is() && ( xObjConfig
->getByName( aStringClassID
.toAsciiUpperCase() ) >>= xObjectProps
) && xObjectProps
.is() )
386 aObjProps
= GetObjPropsFromConfigEntry( aClassID
, xObjectProps
);
388 catch( uno::Exception
& )
396 //-----------------------------------------------------------------------
397 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjectPropsByClassID(
398 const uno::Sequence
< sal_Int8
>& aClassID
)
400 uno::Sequence
< beans::NamedValue
> aObjProps
;
401 if ( ClassIDsEqual( aClassID
, GetSequenceClassID( SO3_DUMMY_CLASSID
) ) )
403 aObjProps
.realloc(2);
404 aObjProps
[0].Name
= ::rtl::OUString::createFromAscii("ObjectFactory");
405 aObjProps
[0].Value
<<= ::rtl::OUString::createFromAscii("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory");
406 aObjProps
[1].Name
= ::rtl::OUString::createFromAscii("ClassID");
407 aObjProps
[1].Value
<<= aClassID
;
410 ::rtl::OUString aStringClassID
= GetStringClassIDRepresentation( aClassID
);
411 if ( aStringClassID
.getLength() )
413 uno::Reference
< container::XNameAccess
> xObjConfig
= GetObjConfiguration();
414 uno::Reference
< container::XNameAccess
> xObjectProps
;
417 if ( xObjConfig
.is() && ( xObjConfig
->getByName( aStringClassID
.toAsciiUpperCase() ) >>= xObjectProps
) && xObjectProps
.is() )
418 aObjProps
= GetObjPropsFromConfigEntry( aClassID
, xObjectProps
);
420 catch( uno::Exception
& )
428 //-----------------------------------------------------------------------
429 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjectPropsByMediaType( const ::rtl::OUString
& aMediaType
)
431 uno::Sequence
< beans::NamedValue
> aObject
=
432 GetObjectPropsByStringClassID( GetExplicitlyRegisteredObjClassID( aMediaType
) );
433 if ( aObject
.getLength() )
436 ::rtl::OUString aDocumentName
= GetDocServiceNameFromMediaType( aMediaType
);
437 if ( aDocumentName
.getLength() )
438 return GetObjectPropsByDocumentName( aDocumentName
);
440 return uno::Sequence
< beans::NamedValue
>();
443 //-----------------------------------------------------------------------
444 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjectPropsByFilter( const ::rtl::OUString
& aFilterName
)
446 ::rtl::OUString aDocumentName
= GetDocServiceNameFromFilter( aFilterName
);
447 if ( aDocumentName
.getLength() )
448 return GetObjectPropsByDocumentName( aDocumentName
);
450 return uno::Sequence
< beans::NamedValue
>();
453 //-----------------------------------------------------------------------
454 uno::Sequence
< beans::NamedValue
> MimeConfigurationHelper::GetObjectPropsByDocumentName( const ::rtl::OUString
& aDocName
)
456 if ( aDocName
.getLength() )
458 uno::Reference
< container::XNameAccess
> xObjConfig
= GetObjConfiguration();
459 if ( xObjConfig
.is() )
463 uno::Sequence
< ::rtl::OUString
> aClassIDs
= xObjConfig
->getElementNames();
464 for ( sal_Int32 nInd
= 0; nInd
< aClassIDs
.getLength(); nInd
++ )
466 uno::Reference
< container::XNameAccess
> xObjectProps
;
467 ::rtl::OUString aEntryDocName
;
469 if ( ( xObjConfig
->getByName( aClassIDs
[nInd
] ) >>= xObjectProps
) && xObjectProps
.is()
470 && ( xObjectProps
->getByName(
471 ::rtl::OUString::createFromAscii( "ObjectDocumentServiceName" ) ) >>= aEntryDocName
)
472 && aEntryDocName
.equals( aDocName
) )
474 return GetObjPropsFromConfigEntry( GetSequenceClassIDRepresentation( aClassIDs
[nInd
] ),
479 catch( uno::Exception
& )
484 return uno::Sequence
< beans::NamedValue
>();
487 //-----------------------------------------------------------------------
488 ::rtl::OUString
MimeConfigurationHelper::GetFactoryNameByClassID( const uno::Sequence
< sal_Int8
>& aClassID
)
490 return GetFactoryNameByStringClassID( GetStringClassIDRepresentation( aClassID
) );
493 //-----------------------------------------------------------------------
494 ::rtl::OUString
MimeConfigurationHelper::GetFactoryNameByStringClassID( const ::rtl::OUString
& aStringClassID
)
496 ::rtl::OUString aResult
;
498 if ( aStringClassID
.getLength() )
500 uno::Reference
< container::XNameAccess
> xObjConfig
= GetObjConfiguration();
501 uno::Reference
< container::XNameAccess
> xObjectProps
;
504 if ( xObjConfig
.is() && ( xObjConfig
->getByName( aStringClassID
.toAsciiUpperCase() ) >>= xObjectProps
) && xObjectProps
.is() )
505 xObjectProps
->getByName( ::rtl::OUString::createFromAscii( "ObjectFactory" ) ) >>= aResult
;
507 catch( uno::Exception
& )
509 uno::Sequence
< sal_Int8
> aClassID
= GetSequenceClassIDRepresentation( aStringClassID
);
510 if ( ClassIDsEqual( aClassID
, GetSequenceClassID( SO3_DUMMY_CLASSID
) ) )
511 return ::rtl::OUString::createFromAscii("com.sun.star.embed.OOoSpecialEmbeddedObjectFactory");
518 //-----------------------------------------------------------------------
519 ::rtl::OUString
MimeConfigurationHelper::GetFactoryNameByDocumentName( const ::rtl::OUString
& aDocName
)
521 ::rtl::OUString aResult
;
523 if ( aDocName
.getLength() )
525 uno::Reference
< container::XNameAccess
> xObjConfig
= GetObjConfiguration();
526 if ( xObjConfig
.is() )
530 uno::Sequence
< ::rtl::OUString
> aClassIDs
= xObjConfig
->getElementNames();
531 for ( sal_Int32 nInd
= 0; nInd
< aClassIDs
.getLength(); nInd
++ )
533 uno::Reference
< container::XNameAccess
> xObjectProps
;
534 ::rtl::OUString aEntryDocName
;
536 if ( ( xObjConfig
->getByName( aClassIDs
[nInd
] ) >>= xObjectProps
) && xObjectProps
.is()
537 && ( xObjectProps
->getByName(
538 ::rtl::OUString::createFromAscii( "ObjectDocumentServiceName" ) ) >>= aEntryDocName
)
539 && aEntryDocName
.equals( aDocName
) )
541 xObjectProps
->getByName( ::rtl::OUString::createFromAscii( "ObjectFactory" ) ) >>= aResult
;
546 catch( uno::Exception
& )
555 //-----------------------------------------------------------------------
556 ::rtl::OUString
MimeConfigurationHelper::GetFactoryNameByMediaType( const ::rtl::OUString
& aMediaType
)
558 ::rtl::OUString aResult
= GetFactoryNameByStringClassID( GetExplicitlyRegisteredObjClassID( aMediaType
) );
560 if ( !aResult
.getLength() )
562 ::rtl::OUString aDocumentName
= GetDocServiceNameFromMediaType( aMediaType
);
563 if ( aDocumentName
.getLength() )
564 aResult
= GetFactoryNameByDocumentName( aDocumentName
);
570 //-----------------------------------------------------------------------
571 ::rtl::OUString
MimeConfigurationHelper::UpdateMediaDescriptorWithFilterName(
572 uno::Sequence
< beans::PropertyValue
>& aMediaDescr
,
573 sal_Bool bIgnoreType
)
575 ::rtl::OUString aFilterName
;
577 for ( sal_Int32 nInd
= 0; nInd
< aMediaDescr
.getLength(); nInd
++ )
578 if ( aMediaDescr
[nInd
].Name
.equalsAscii( "FilterName" ) )
579 aMediaDescr
[nInd
].Value
>>= aFilterName
;
581 if ( !aFilterName
.getLength() )
583 // filter name is not specified, so type detection should be done
585 uno::Reference
< document::XTypeDetection
> xTypeDetection(
586 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.TypeDetection" ) ),
589 if ( !xTypeDetection
.is() )
590 throw uno::RuntimeException(); // TODO
592 // typedetection can change the mode, add a stream and so on, thus a copy should be used
593 uno::Sequence
< beans::PropertyValue
> aTempMD( aMediaDescr
);
596 ::rtl::OUString aTypeName
= xTypeDetection
->queryTypeByDescriptor( aTempMD
, sal_True
);
599 for ( sal_Int32 nInd
= 0; nInd
< aTempMD
.getLength(); nInd
++ )
600 if ( aTempMD
[nInd
].Name
.equalsAscii( "FilterName" ) )
601 aTempMD
[nInd
].Value
>>= aFilterName
;
603 if ( aFilterName
.getLength() )
605 sal_Int32 nOldLen
= aMediaDescr
.getLength();
606 aMediaDescr
.realloc( nOldLen
+ 1 );
607 aMediaDescr
[nOldLen
].Name
= ::rtl::OUString::createFromAscii( "FilterName" );
608 aMediaDescr
[ nOldLen
].Value
<<= aFilterName
;
611 else if ( aTypeName
.getLength() && !bIgnoreType
)
613 uno::Reference
< container::XNameAccess
> xNameAccess( xTypeDetection
, uno::UNO_QUERY
);
614 uno::Sequence
< beans::PropertyValue
> aTypes
;
616 if ( xNameAccess
.is() && ( xNameAccess
->getByName( aTypeName
) >>= aTypes
) )
618 for ( sal_Int32 nInd
= 0; nInd
< aTypes
.getLength(); nInd
++ )
620 if ( aTypes
[nInd
].Name
.equalsAscii( "PreferredFilter" ) && ( aTypes
[nInd
].Value
>>= aFilterName
) )
622 sal_Int32 nOldLen
= aMediaDescr
.getLength();
623 aMediaDescr
.realloc( nOldLen
+ 1 );
624 aMediaDescr
[nOldLen
].Name
= ::rtl::OUString::createFromAscii( "FilterName" );
625 aMediaDescr
[ nOldLen
].Value
= aTypes
[nInd
].Value
;
636 ::rtl::OUString
MimeConfigurationHelper::UpdateMediaDescriptorWithFilterName(
637 uno::Sequence
< beans::PropertyValue
>& aMediaDescr
,
638 uno::Sequence
< beans::NamedValue
>& aObject
)
640 ::rtl::OUString aDocName
;
641 for ( sal_Int32 nInd
= 0; nInd
< aObject
.getLength(); nInd
++ )
642 if ( aObject
[nInd
].Name
.equalsAscii( "ObjectDocumentServiceName" ) )
644 aObject
[nInd
].Value
>>= aDocName
;
648 OSL_ENSURE( aDocName
.getLength(), "The name must exist at this point!\n" );
651 sal_Bool bNeedsAddition
= sal_True
;
652 for ( sal_Int32 nMedInd
= 0; nMedInd
< aMediaDescr
.getLength(); nMedInd
++ )
653 if ( aMediaDescr
[nMedInd
].Name
.equalsAscii( "DocumentService" ) )
655 aMediaDescr
[nMedInd
].Value
<<= aDocName
;
656 bNeedsAddition
= sal_False
;
660 if ( bNeedsAddition
)
662 sal_Int32 nOldLen
= aMediaDescr
.getLength();
663 aMediaDescr
.realloc( nOldLen
+ 1 );
664 aMediaDescr
[nOldLen
].Name
= ::rtl::OUString::createFromAscii( "DocumentService" );
665 aMediaDescr
[nOldLen
].Value
<<= aDocName
;
668 return UpdateMediaDescriptorWithFilterName( aMediaDescr
, sal_True
);
671 sal_Bool
MimeConfigurationHelper::AddFilterNameCheckOwnFile(
672 uno::Sequence
< beans::PropertyValue
>& aMediaDescr
)
674 ::rtl::OUString aFilterName
= UpdateMediaDescriptorWithFilterName( aMediaDescr
, sal_False
);
675 if ( aFilterName
.getLength() )
679 uno::Reference
< container::XNameAccess
> xFilterFactory(
680 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.FilterFactory" ) ),
681 uno::UNO_QUERY_THROW
);
683 uno::Any aFilterAnyData
= xFilterFactory
->getByName( aFilterName
);
684 uno::Sequence
< beans::PropertyValue
> aFilterData
;
685 if ( aFilterAnyData
>>= aFilterData
)
687 for ( sal_Int32 nInd
= 0; nInd
< aFilterData
.getLength(); nInd
++ )
688 if ( aFilterData
[nInd
].Name
.equalsAscii( "Flags" ) )
690 uno::Any aVal
= aFilterData
[nInd
].Value
;
691 sal_Int32 nFlags
= 0;
692 // check the OWN flag
693 if ( ( aFilterData
[nInd
].Value
>>= nFlags
) && ( nFlags
& 0x20 ) )
699 catch( uno::Exception
& )
706 //-----------------------------------------------------------
707 ::rtl::OUString
MimeConfigurationHelper::GetDefaultFilterFromServiceName( const ::rtl::OUString
& aServiceName
, sal_Int32 nVersion
)
709 rtl::OUString aResult
;
711 if ( aServiceName
.getLength() && nVersion
)
714 uno::Reference
< container::XContainerQuery
> xFilterQuery(
715 m_xFactory
->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.document.FilterFactory" ) ),
716 uno::UNO_QUERY_THROW
);
718 uno::Sequence
< beans::NamedValue
> aSearchRequest( 2 );
719 aSearchRequest
[0].Name
= ::rtl::OUString::createFromAscii( "DocumentService" );
720 aSearchRequest
[0].Value
<<= aServiceName
;
721 aSearchRequest
[1].Name
= ::rtl::OUString::createFromAscii( "FileFormatVersion" );
722 aSearchRequest
[1].Value
<<= nVersion
;
724 uno::Sequence
< beans::PropertyValue
> aFilterProps
;
725 uno::Reference
< container::XEnumeration
> xFilterEnum
=
726 xFilterQuery
->createSubSetEnumerationByProperties( aSearchRequest
);
728 // use the first filter that is found
729 if ( xFilterEnum
.is() )
730 while ( xFilterEnum
->hasMoreElements() )
732 uno::Sequence
< beans::PropertyValue
> aProps
;
733 if ( xFilterEnum
->nextElement() >>= aProps
)
735 SequenceAsHashMap
aPropsHM( aProps
);
736 sal_Int32 nFlags
= aPropsHM
.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Flags" ),
739 // that should be import, export, own filter and not a template filter ( TemplatePath flag )
740 if ( ( ( nFlags
& 0x23L
) == 0x23L
) && !( nFlags
& 0x10 ) )
742 // if there are more than one filter the preffered one should be used
743 // if there is no preffered filter the first one will be used
744 if ( !aResult
.getLength() || ( nFlags
& 0x10000000L
) )
745 aResult
= aPropsHM
.getUnpackedValueOrDefault( ::rtl::OUString::createFromAscii( "Name" ),
747 if ( nFlags
& 0x10000000L
)
748 break; // the preffered filter was found
753 catch( uno::Exception
& )
758 //-------------------------------------------------------------------------
759 sal_Bool
MimeConfigurationHelper::ClassIDsEqual( const uno::Sequence
< sal_Int8
>& aClassID1
, const uno::Sequence
< sal_Int8
>& aClassID2
)
761 if ( aClassID1
.getLength() != aClassID2
.getLength() )
764 for ( sal_Int32 nInd
= 0; nInd
< aClassID1
.getLength(); nInd
++ )
765 if ( aClassID1
[nInd
] != aClassID2
[nInd
] )
770 //----------------------------------------------
771 uno::Sequence
< sal_Int8
> MimeConfigurationHelper::GetSequenceClassID( sal_uInt32 n1
, sal_uInt16 n2
, sal_uInt16 n3
,
772 sal_uInt8 b8
, sal_uInt8 b9
, sal_uInt8 b10
, sal_uInt8 b11
,
773 sal_uInt8 b12
, sal_uInt8 b13
, sal_uInt8 b14
, sal_uInt8 b15
)
775 uno::Sequence
< sal_Int8
> aResult( 16 );
776 aResult
[0] = (sal_Int8
)( n1
>> 24 );
777 aResult
[1] = (sal_Int8
)( ( n1
<< 8 ) >> 24 );
778 aResult
[2] = (sal_Int8
)( ( n1
<< 16 ) >> 24 );
779 aResult
[3] = (sal_Int8
)( ( n1
<< 24 ) >> 24 );
780 aResult
[4] = (sal_Int8
)( n2
>> 8 );
781 aResult
[5] = (sal_Int8
)( ( n2
<< 8 ) >> 8 );
782 aResult
[6] = (sal_Int8
)( n3
>> 8 );
783 aResult
[7] = (sal_Int8
)( ( n3
<< 8 ) >> 8 );
795 uno::Sequence
<sal_Int8
> MimeConfigurationHelper::GetSequenceClassIDFromObjectName(const ::rtl::OUString
& _sObjectName
)
797 uno::Sequence
<sal_Int8
> aClassId
;
798 uno::Reference
< container::XNameAccess
> xObjectNames
= GetConfigurationByPath(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/org.openoffice.Office.Embedding/ObjectNames")));
799 uno::Reference
< container::XNameAccess
> xProps
;
800 if ( xObjectNames
.is() && (xObjectNames
->getByName(_sObjectName
) >>= xProps
) && xProps
.is() )
802 ::rtl::OUString sValue
;
803 xProps
->getByName(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ClassID"))) >>= sValue
;
804 aClassId
= GetSequenceClassIDRepresentation(sValue
);