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