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