xref: /AOO41X/main/starmath/source/smdetect.cxx (revision f2113f943514e86b51ffcdf893c6f333de5a4d6f)
1d107581fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3d107581fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4d107581fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5d107581fSAndrew Rist  * distributed with this work for additional information
6d107581fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7d107581fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8d107581fSAndrew Rist  * "License"); you may not use this file except in compliance
9d107581fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11d107581fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13d107581fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14d107581fSAndrew Rist  * software distributed under the License is distributed on an
15d107581fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16d107581fSAndrew Rist  * KIND, either express or implied.  See the License for the
17d107581fSAndrew Rist  * specific language governing permissions and limitations
18d107581fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20d107581fSAndrew Rist  *************************************************************/
21d107581fSAndrew Rist 
22d107581fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_starmath.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "smdetect.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir //#include <framework/interaction.hxx>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
32cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
33cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
34cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
36cdf0e10cSrcweir #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
37cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
40cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
41cdf0e10cSrcweir #include <com/sun/star/task/XInteractionHandler.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
43cdf0e10cSrcweir #include <com/sun/star/ucb/CommandAbortedException.hpp>
44cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAppException.hpp>
45cdf0e10cSrcweir #include <com/sun/star/ucb/XContent.hpp>
46cdf0e10cSrcweir #include <com/sun/star/packages/zip/ZipIOException.hpp>
47cdf0e10cSrcweir #include <framework/interaction.hxx>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #ifndef _TOOLKIT_UNOHLP_HXX
50cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir #include <ucbhelper/simpleinteractionrequest.hxx>
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #include <rtl/ustring.h>
55cdf0e10cSrcweir #include <rtl/logfile.hxx>
56cdf0e10cSrcweir #include <svl/itemset.hxx>
57cdf0e10cSrcweir #include <vcl/window.hxx>
58cdf0e10cSrcweir #include <svl/eitem.hxx>
59cdf0e10cSrcweir #include <svl/stritem.hxx>
60cdf0e10cSrcweir #include <tools/urlobj.hxx>
61cdf0e10cSrcweir #include <vos/mutex.hxx>
62cdf0e10cSrcweir #include <svtools/sfxecode.hxx>
63cdf0e10cSrcweir #include <svtools/ehdl.hxx>
64cdf0e10cSrcweir #include <sot/storinfo.hxx>
65cdf0e10cSrcweir #include <vcl/svapp.hxx>
66cdf0e10cSrcweir #include <sfx2/app.hxx>
67cdf0e10cSrcweir #include <sfx2/sfxsids.hrc>
68cdf0e10cSrcweir #include <sfx2/request.hxx>
69cdf0e10cSrcweir #include <sfx2/docfile.hxx>
70cdf0e10cSrcweir #include <sfx2/docfilt.hxx>
71cdf0e10cSrcweir #include <sfx2/fcontnr.hxx>
72cdf0e10cSrcweir #include <sfx2/brokenpackageint.hxx>
73cdf0e10cSrcweir 
74cdf0e10cSrcweir #include "document.hxx"
75cdf0e10cSrcweir #include "eqnolefilehdr.hxx"
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir using namespace ::com::sun::star;
79cdf0e10cSrcweir using namespace ::com::sun::star::uno;
80cdf0e10cSrcweir using namespace ::com::sun::star::io;
81cdf0e10cSrcweir using namespace ::com::sun::star::frame;
82cdf0e10cSrcweir using namespace ::com::sun::star::task;
83cdf0e10cSrcweir using namespace ::com::sun::star::beans;
84cdf0e10cSrcweir using namespace ::com::sun::star::lang;
85cdf0e10cSrcweir using namespace ::com::sun::star::ucb;
86cdf0e10cSrcweir using namespace ::rtl;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir SmFilterDetect::SmFilterDetect( const REFERENCE < ::com::sun::star::lang::XMultiServiceFactory >& /*xFactory*/ )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir SmFilterDetect::~SmFilterDetect()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir ::rtl::OUString SAL_CALL SmFilterDetect::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& lDescriptor ) throw( ::com::sun::star::uno::RuntimeException )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     REFERENCE< XInputStream > xStream;
99cdf0e10cSrcweir     REFERENCE< XContent > xContent;
100cdf0e10cSrcweir     REFERENCE< XInteractionHandler > xInteraction;
101cdf0e10cSrcweir     String aURL;
102cdf0e10cSrcweir 	::rtl::OUString sTemp;
103cdf0e10cSrcweir     String aTypeName;            // a name describing the type (from MediaDescriptor, usually from flat detection)
104cdf0e10cSrcweir     String aPreselectedFilterName;      // a name describing the filter to use (from MediaDescriptor, usually from UI action)
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	::rtl::OUString aDocumentTitle; // interesting only if set in this method
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	// opening as template is done when a parameter tells to do so and a template filter can be detected
109cdf0e10cSrcweir     // (otherwise no valid filter would be found) or if the detected filter is a template filter and
110cdf0e10cSrcweir 	// there is no parameter that forbids to open as template
111cdf0e10cSrcweir 	sal_Bool bOpenAsTemplate = sal_False;
112cdf0e10cSrcweir     sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	sal_Bool bRepairPackage = sal_False;
115cdf0e10cSrcweir 	sal_Bool bRepairAllowed = sal_False;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 	// now some parameters that can already be in the array, but may be overwritten or new inserted here
118cdf0e10cSrcweir 	// remember their indices in the case new values must be added to the array
119cdf0e10cSrcweir 	sal_Int32 nPropertyCount = lDescriptor.getLength();
120cdf0e10cSrcweir     sal_Int32 nIndexOfFilterName = -1;
121cdf0e10cSrcweir     sal_Int32 nIndexOfInputStream = -1;
122cdf0e10cSrcweir     sal_Int32 nIndexOfContent = -1;
123cdf0e10cSrcweir     sal_Int32 nIndexOfReadOnlyFlag = -1;
124cdf0e10cSrcweir     sal_Int32 nIndexOfTemplateFlag = -1;
125cdf0e10cSrcweir     sal_Int32 nIndexOfDocumentTitle = -1;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
128cdf0e10cSrcweir 	{
129cdf0e10cSrcweir         // extract properties
130cdf0e10cSrcweir         if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("URL")) )
131cdf0e10cSrcweir 		{
132cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
133cdf0e10cSrcweir 			aURL = sTemp;
134cdf0e10cSrcweir 		}
135cdf0e10cSrcweir         else if( !aURL.Len() && lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FileName")) )
136cdf0e10cSrcweir 		{
137cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
138cdf0e10cSrcweir 			aURL = sTemp;
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("TypeName")) )
141cdf0e10cSrcweir 		{
142cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
143cdf0e10cSrcweir             aTypeName = sTemp;
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("FilterName")) )
146cdf0e10cSrcweir 		{
147cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= sTemp;
148cdf0e10cSrcweir             aPreselectedFilterName = sTemp;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             // if the preselected filter name is not correct, it must be erased after detection
151cdf0e10cSrcweir             // remember index of property to get access to it later
152cdf0e10cSrcweir             nIndexOfFilterName = nProperty;
153cdf0e10cSrcweir 		}
154cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InputStream")) )
155cdf0e10cSrcweir             nIndexOfInputStream = nProperty;
156cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("ReadOnly")) )
157cdf0e10cSrcweir             nIndexOfReadOnlyFlag = nProperty;
158cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("UCBContent")) )
159cdf0e10cSrcweir             nIndexOfContent = nProperty;
160cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("AsTemplate")) )
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			lDescriptor[nProperty].Value >>= bOpenAsTemplate;
163cdf0e10cSrcweir             nIndexOfTemplateFlag = nProperty;
164cdf0e10cSrcweir 		}
165cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("InteractionHandler")) )
166cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= xInteraction;
167cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("RepairPackage")) )
168cdf0e10cSrcweir             lDescriptor[nProperty].Value >>= bRepairPackage;
169cdf0e10cSrcweir         else if( lDescriptor[nProperty].Name == OUString(RTL_CONSTASCII_USTRINGPARAM("DocumentTitle")) )
170cdf0e10cSrcweir             nIndexOfDocumentTitle = nProperty;
171cdf0e10cSrcweir 	}
172cdf0e10cSrcweir 
173cdf0e10cSrcweir     // can't check the type for external filters, so set the "dont" flag accordingly
174cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
175cdf0e10cSrcweir     //SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     SfxApplication* pApp = SFX_APP();
178cdf0e10cSrcweir     SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
179cdf0e10cSrcweir     TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
180cdf0e10cSrcweir     SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     bWasReadOnly = pItem && pItem->GetValue();
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	String aFilterName;
185cdf0e10cSrcweir 	String aPrefix = String::CreateFromAscii( "private:factory/" );
186cdf0e10cSrcweir 	if( aURL.Match( aPrefix ) == aPrefix.Len() )
187cdf0e10cSrcweir 	{
188cdf0e10cSrcweir 		const SfxFilter* pFilter = 0;
189cdf0e10cSrcweir 		String aPattern( aPrefix );
190cdf0e10cSrcweir 		aPattern += String::CreateFromAscii("smath");
191cdf0e10cSrcweir 		if ( aURL.Match( aPattern ) >= aPattern.Len() )
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 			pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
194cdf0e10cSrcweir 			aTypeName = pFilter->GetTypeName();
195cdf0e10cSrcweir 			aFilterName = pFilter->GetName();
196cdf0e10cSrcweir 		}
197cdf0e10cSrcweir 	}
198cdf0e10cSrcweir 	else
199cdf0e10cSrcweir 	{
200cdf0e10cSrcweir 	    // ctor of SfxMedium uses owner transition of ItemSet
201cdf0e10cSrcweir 	    SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, sal_False, NULL, pSet );
202cdf0e10cSrcweir 	    aMedium.UseInteractionHandler( sal_True );
203cdf0e10cSrcweir 
204cdf0e10cSrcweir 	    sal_Bool bIsStorage = aMedium.IsStorage();
205cdf0e10cSrcweir 	    if ( aMedium.GetErrorCode() == ERRCODE_NONE )
206cdf0e10cSrcweir 	    {
207cdf0e10cSrcweir 	        // remember input stream and content and put them into the descriptor later
208cdf0e10cSrcweir 			// should be done here since later the medium can switch to a version
209cdf0e10cSrcweir 	        xStream = aMedium.GetInputStream();
210cdf0e10cSrcweir 			xContent = aMedium.GetContent();
211cdf0e10cSrcweir         	bReadOnly = aMedium.IsReadOnly();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 	        if ( bIsStorage )
214cdf0e10cSrcweir 			{
215cdf0e10cSrcweir                 //TODO/LATER: factor this out!
216cdf0e10cSrcweir                 uno::Reference < embed::XStorage > xStorage = aMedium.GetStorage( sal_False );
217cdf0e10cSrcweir 				if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
218cdf0e10cSrcweir 				{
219cdf0e10cSrcweir 					// error during storage creation means _here_ that the medium
220cdf0e10cSrcweir 					// is broken, but we can not handle it in medium since unpossibility
221cdf0e10cSrcweir 					// to create a storage does not _always_ means that the medium is broken
222cdf0e10cSrcweir 					aMedium.SetError( aMedium.GetLastStorageCreationState(), ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( OSL_LOG_PREFIX ) ) );
223cdf0e10cSrcweir 					if ( xInteraction.is() )
224cdf0e10cSrcweir 					{
225cdf0e10cSrcweir 						OUString empty;
226cdf0e10cSrcweir 						try
227cdf0e10cSrcweir 						{
228cdf0e10cSrcweir 							InteractiveAppException xException( empty,
229cdf0e10cSrcweir 															REFERENCE< XInterface >(),
230cdf0e10cSrcweir 															InteractionClassification_ERROR,
231cdf0e10cSrcweir 															aMedium.GetError() );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 							REFERENCE< XInteractionRequest > xRequest(
234cdf0e10cSrcweir 								new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
235cdf0e10cSrcweir 																 	 ucbhelper::CONTINUATION_APPROVE ) );
236cdf0e10cSrcweir 							xInteraction->handle( xRequest );
237cdf0e10cSrcweir 						}
238cdf0e10cSrcweir 						catch ( Exception & ) {};
239cdf0e10cSrcweir 					}
240cdf0e10cSrcweir 				}
241cdf0e10cSrcweir 				else
242cdf0e10cSrcweir 				{
243cdf0e10cSrcweir                     aFilterName.Erase();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 					try
246cdf0e10cSrcweir 					{
247cdf0e10cSrcweir                         const SfxFilter* pFilter = aPreselectedFilterName.Len() ?
248cdf0e10cSrcweir                                 SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : aTypeName.Len() ?
249cdf0e10cSrcweir                                 SfxFilterMatcher(String::CreateFromAscii("smath")).GetFilter4EA( aTypeName ) : 0;
250cdf0e10cSrcweir                         String aTmpFilterName;
251cdf0e10cSrcweir                         if ( pFilter )
252cdf0e10cSrcweir                             aTmpFilterName = pFilter->GetName();
253cdf0e10cSrcweir                         aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter ? pFilter->IsAllowedAsTemplate() : sal_False, &aTmpFilterName );
254cdf0e10cSrcweir 					}
255cdf0e10cSrcweir 					catch( lang::WrappedTargetException& aWrap )
256cdf0e10cSrcweir 					{
257cdf0e10cSrcweir 						packages::zip::ZipIOException aZipException;
258cdf0e10cSrcweir 
259cdf0e10cSrcweir 						// repairing is done only if this type is requested from outside
260cdf0e10cSrcweir 						if ( ( aWrap.TargetException >>= aZipException ) && aTypeName.Len() )
261cdf0e10cSrcweir 						{
262cdf0e10cSrcweir 							if ( xInteraction.is() )
263cdf0e10cSrcweir 							{
264cdf0e10cSrcweir 								// the package is broken one
265cdf0e10cSrcweir        							aDocumentTitle = aMedium.GetURLObject().getName(
266cdf0e10cSrcweir 															INetURLObject::LAST_SEGMENT,
267cdf0e10cSrcweir 															true,
268cdf0e10cSrcweir 															INetURLObject::DECODE_WITH_CHARSET );
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 								if ( !bRepairPackage )
271cdf0e10cSrcweir 								{
272cdf0e10cSrcweir 									// ask the user whether he wants to try to repair
273cdf0e10cSrcweir                                     RequestPackageReparation aRequest( aDocumentTitle );
274cdf0e10cSrcweir                                     xInteraction->handle( aRequest.GetRequest() );
275cdf0e10cSrcweir                                     bRepairAllowed = aRequest.isApproved();
276cdf0e10cSrcweir 								}
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 								if ( !bRepairAllowed )
279cdf0e10cSrcweir 								{
280cdf0e10cSrcweir 									// repair either not allowed or not successful
281cdf0e10cSrcweir                                     NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
282cdf0e10cSrcweir                                     xInteraction->handle( aNotifyRequest.GetRequest() );
283cdf0e10cSrcweir 								}
284cdf0e10cSrcweir 							}
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 							if ( !bRepairAllowed )
287cdf0e10cSrcweir 								aTypeName.Erase();
288cdf0e10cSrcweir 						}
289cdf0e10cSrcweir 					}
290cdf0e10cSrcweir 					catch( uno::RuntimeException& )
291cdf0e10cSrcweir 					{
292cdf0e10cSrcweir 						throw;
293cdf0e10cSrcweir 					}
294cdf0e10cSrcweir 					catch( uno::Exception& )
295cdf0e10cSrcweir 					{
296cdf0e10cSrcweir 						aTypeName.Erase();
297cdf0e10cSrcweir 					}
298cdf0e10cSrcweir 
299cdf0e10cSrcweir                    	if ( aTypeName.Len() )
300cdf0e10cSrcweir 					{
301cdf0e10cSrcweir                        	const SfxFilter* pFilter =
302cdf0e10cSrcweir 									SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName );
303cdf0e10cSrcweir 						if ( pFilter )
304cdf0e10cSrcweir 							aFilterName = pFilter->GetName();
305cdf0e10cSrcweir 					}
306cdf0e10cSrcweir 				}
307cdf0e10cSrcweir 			}
308cdf0e10cSrcweir 			else
309cdf0e10cSrcweir 			{
310cdf0e10cSrcweir 				//Test to see if this begins with xml and if so run it through
311cdf0e10cSrcweir 				//the MathML filter. There are all sorts of things wrong with
312cdf0e10cSrcweir 				//this approach, to be fixed at a better level than here
313cdf0e10cSrcweir 				SvStream *pStrm = aMedium.GetInStream();
314cdf0e10cSrcweir                 aTypeName.Erase();
315cdf0e10cSrcweir 				if (pStrm && !pStrm->GetError())
316cdf0e10cSrcweir 				{
317cdf0e10cSrcweir                     SotStorageRef aStorage = new SotStorage ( pStrm, sal_False );
318cdf0e10cSrcweir                     if ( !aStorage->GetError() )
319cdf0e10cSrcweir                     {
320cdf0e10cSrcweir                         if ( aStorage->IsStream( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "Equation Native" ) ) ) )
321cdf0e10cSrcweir                         {
322cdf0e10cSrcweir                             sal_uInt8 nVersion;
323cdf0e10cSrcweir                             if (GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3)
324cdf0e10cSrcweir                                 aTypeName.AssignAscii( "math_MathType_3x" );
325cdf0e10cSrcweir                         }
326cdf0e10cSrcweir                     }
327cdf0e10cSrcweir                     else
328cdf0e10cSrcweir                     {
329*f2113f94SArmin Le Grand                         // #124636# detection should not only check for xml, but at least also for
330*f2113f94SArmin Le Grand                         // the math start element and the MathML URL. Additionally take their order
331*f2113f94SArmin Le Grand                         // into account. Also allow the case where the start element has a namespace
332*f2113f94SArmin Le Grand                         // (e.g. <bla:math), but in that case ensure that it is in front of an evtl.
333*f2113f94SArmin Le Grand                         // xmlns:math namespace declaration and thus not part of that
334*f2113f94SArmin Le Grand                         const sal_uInt16 nReadSize(4095);
335*f2113f94SArmin Le Grand                         sal_Char aBuffer[nReadSize+1];
336cdf0e10cSrcweir                         pStrm->Seek( STREAM_SEEK_TO_BEGIN );
337*f2113f94SArmin Le Grand                         const sal_uLong nBytesRead(pStrm->Read( aBuffer, nReadSize ));
338*f2113f94SArmin Le Grand 
339*f2113f94SArmin Le Grand                         if(nBytesRead > (5 + 1 + 34 + 5)) // xml + '>' + URL + '(<|:)math'
340cdf0e10cSrcweir                         {
341*f2113f94SArmin Le Grand                             // end string with null
342*f2113f94SArmin Le Grand                             aBuffer[nBytesRead + 1] = 0;
343*f2113f94SArmin Le Grand 
344*f2113f94SArmin Le Grand                             // is it a xml file?
345*f2113f94SArmin Le Grand                             const sal_Char* pXML = strstr(aBuffer, "<?xml");
346*f2113f94SArmin Le Grand                             bool isMathFile(false);
347*f2113f94SArmin Le Grand 
348*f2113f94SArmin Le Grand                             if(pXML)
349*f2113f94SArmin Le Grand                             {
350*f2113f94SArmin Le Grand                                 // does it have the MathML URL?
351*f2113f94SArmin Le Grand                                 const sal_Char* pURL = strstr(aBuffer, "http://www.w3.org/1998/Math/MathML");
352*f2113f94SArmin Le Grand 
353*f2113f94SArmin Le Grand                                 // URL has to be after XML start
354*f2113f94SArmin Le Grand                                 if(pURL && pURL > pXML)
355*f2113f94SArmin Le Grand                                 {
356*f2113f94SArmin Le Grand                                     // look if we have a direct math start element
357*f2113f94SArmin Le Grand                                     sal_Char* pMathStart = strstr(aBuffer, "<math");
358*f2113f94SArmin Le Grand 
359*f2113f94SArmin Le Grand                                     if(!pMathStart)
360*f2113f94SArmin Le Grand                                     {
361*f2113f94SArmin Le Grand                                         // if not, look if we have a math start element in another namespace
362*f2113f94SArmin Le Grand                                         pMathStart = strstr(aBuffer, ":math");
363*f2113f94SArmin Le Grand 
364*f2113f94SArmin Le Grand                                         if(pMathStart)
365*f2113f94SArmin Le Grand                                         {
366*f2113f94SArmin Le Grand                                             // if found, this has to be in front of the evtl. also existing namespace
367*f2113f94SArmin Le Grand                                             // declaration also containing :math to be the start element
368*f2113f94SArmin Le Grand                                             sal_Char* pNamespaceMath = strstr(aBuffer, "xmlns:math");
369*f2113f94SArmin Le Grand 
370*f2113f94SArmin Le Grand                                             if(pNamespaceMath && pMathStart > pNamespaceMath)
371*f2113f94SArmin Le Grand                                             {
372*f2113f94SArmin Le Grand                                                 // invalid :math found (probably part of the namespace declaration)
373*f2113f94SArmin Le Grand                                                 // -> this cannot be the math start element
374*f2113f94SArmin Le Grand                                                 pMathStart = 0;
375*f2113f94SArmin Le Grand                                             }
376*f2113f94SArmin Le Grand                                         }
377*f2113f94SArmin Le Grand                                     }
378*f2113f94SArmin Le Grand 
379*f2113f94SArmin Le Grand                                     // MathStart has to be before the URL
380*f2113f94SArmin Le Grand                                     if(pMathStart && pMathStart < pURL)
381*f2113f94SArmin Le Grand                                     {
382*f2113f94SArmin Le Grand                                         isMathFile = true;
383*f2113f94SArmin Le Grand                                     }
384*f2113f94SArmin Le Grand                                 }
385*f2113f94SArmin Le Grand                             }
386*f2113f94SArmin Le Grand 
387*f2113f94SArmin Le Grand                             if(isMathFile)
388cdf0e10cSrcweir                             {
389cdf0e10cSrcweir                                 static const sal_Char sFltrNm_2[] = MATHML_XML;
390cdf0e10cSrcweir                                 static const sal_Char sTypeNm_2[] = "math_MathML_XML_Math";
391*f2113f94SArmin Le Grand 
392cdf0e10cSrcweir                                 aFilterName.AssignAscii( sFltrNm_2 );
393cdf0e10cSrcweir                                 aTypeName.AssignAscii( sTypeNm_2 );
394cdf0e10cSrcweir                             }
395cdf0e10cSrcweir                         }
396cdf0e10cSrcweir                     }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir                     if ( aTypeName.Len() )
399cdf0e10cSrcweir                     {
400cdf0e10cSrcweir                         const SfxFilter* pFilt = SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName );
401cdf0e10cSrcweir                         if ( pFilt )
402cdf0e10cSrcweir                             aFilterName = pFilt->GetName();
403cdf0e10cSrcweir                     }
404cdf0e10cSrcweir 				}
405cdf0e10cSrcweir 			}
406cdf0e10cSrcweir 		}
407cdf0e10cSrcweir 	}
408cdf0e10cSrcweir 
409cdf0e10cSrcweir     if ( nIndexOfInputStream == -1 && xStream.is() )
410cdf0e10cSrcweir     {
411cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
412cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
413cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
414cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xStream;
415cdf0e10cSrcweir         nPropertyCount++;
416cdf0e10cSrcweir     }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir     if ( nIndexOfContent == -1 && xContent.is() )
419cdf0e10cSrcweir     {
420cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
421cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
422cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
423cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xContent;
424cdf0e10cSrcweir         nPropertyCount++;
425cdf0e10cSrcweir     }
426cdf0e10cSrcweir 
427cdf0e10cSrcweir     if ( bReadOnly != bWasReadOnly )
428cdf0e10cSrcweir     {
429cdf0e10cSrcweir         if ( nIndexOfReadOnlyFlag == -1 )
430cdf0e10cSrcweir         {
431cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
432cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
433cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= bReadOnly;
434cdf0e10cSrcweir             nPropertyCount++;
435cdf0e10cSrcweir         }
436cdf0e10cSrcweir         else
437cdf0e10cSrcweir             lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
438cdf0e10cSrcweir     }
439cdf0e10cSrcweir 
440cdf0e10cSrcweir 	if ( !bRepairPackage && bRepairAllowed )
441cdf0e10cSrcweir 	{
442cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
443cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
444cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
445cdf0e10cSrcweir         nPropertyCount++;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 		bOpenAsTemplate = sal_True;
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 		// TODO/LATER: set progress bar that should be used
450cdf0e10cSrcweir 	}
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 	if ( bOpenAsTemplate )
453cdf0e10cSrcweir 	{
454cdf0e10cSrcweir 		if ( nIndexOfTemplateFlag == -1 )
455cdf0e10cSrcweir 		{
456cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
457cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
458cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
459cdf0e10cSrcweir         	nPropertyCount++;
460cdf0e10cSrcweir 		}
461cdf0e10cSrcweir 		else
462cdf0e10cSrcweir         	lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
463cdf0e10cSrcweir 	}
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 	if ( aDocumentTitle.getLength() )
466cdf0e10cSrcweir 	{
467cdf0e10cSrcweir 		// the title was set here
468cdf0e10cSrcweir 		if ( nIndexOfDocumentTitle == -1 )
469cdf0e10cSrcweir 		{
470cdf0e10cSrcweir         	lDescriptor.realloc( nPropertyCount + 1 );
471cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
472cdf0e10cSrcweir         	lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
473cdf0e10cSrcweir         	nPropertyCount++;
474cdf0e10cSrcweir 		}
475cdf0e10cSrcweir 		else
476cdf0e10cSrcweir         	lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
477cdf0e10cSrcweir 	}
478cdf0e10cSrcweir 
479cdf0e10cSrcweir     if ( !aFilterName.Len() )
480cdf0e10cSrcweir         aTypeName.Erase();
481cdf0e10cSrcweir 
482cdf0e10cSrcweir     return aTypeName;
483cdf0e10cSrcweir }
484cdf0e10cSrcweir 
485cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( SmFilterDetect )
486cdf0e10cSrcweir 
487cdf0e10cSrcweir /* XServiceInfo */
488cdf0e10cSrcweir UNOOUSTRING SAL_CALL SmFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
489cdf0e10cSrcweir {
490cdf0e10cSrcweir     return impl_getStaticImplementationName();
491cdf0e10cSrcweir }
492cdf0e10cSrcweir                                                                                                                                 \
493cdf0e10cSrcweir /* XServiceInfo */
494cdf0e10cSrcweir sal_Bool SAL_CALL SmFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
495cdf0e10cSrcweir {
496cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING >  seqServiceNames =   getSupportedServiceNames();
497cdf0e10cSrcweir     const UNOOUSTRING*          pArray          =   seqServiceNames.getConstArray();
498cdf0e10cSrcweir     for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
499cdf0e10cSrcweir     {
500cdf0e10cSrcweir         if ( pArray[nCounter] == sServiceName )
501cdf0e10cSrcweir         {
502cdf0e10cSrcweir             return sal_True ;
503cdf0e10cSrcweir         }
504cdf0e10cSrcweir     }
505cdf0e10cSrcweir     return sal_False ;
506cdf0e10cSrcweir }
507cdf0e10cSrcweir 
508cdf0e10cSrcweir /* XServiceInfo */
509cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
510cdf0e10cSrcweir {
511cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
512cdf0e10cSrcweir }
513cdf0e10cSrcweir 
514cdf0e10cSrcweir /* Helper for XServiceInfo */
515cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SmFilterDetect::impl_getStaticSupportedServiceNames()
516cdf0e10cSrcweir {
517cdf0e10cSrcweir     UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
518cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 );
519cdf0e10cSrcweir     seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection"  );
520cdf0e10cSrcweir     return seqServiceNames ;
521cdf0e10cSrcweir }
522cdf0e10cSrcweir 
523cdf0e10cSrcweir /* Helper for XServiceInfo */
524cdf0e10cSrcweir UNOOUSTRING SmFilterDetect::impl_getStaticImplementationName()
525cdf0e10cSrcweir {
526cdf0e10cSrcweir     return UNOOUSTRING::createFromAscii( "com.sun.star.comp.math.FormatDetector" );
527cdf0e10cSrcweir }
528cdf0e10cSrcweir 
529cdf0e10cSrcweir /* Helper for registry */
530cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL SmFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
531cdf0e10cSrcweir {
532cdf0e10cSrcweir     return UNOREFERENCE< UNOXINTERFACE >( *new SmFilterDetect( xServiceManager ) );
533cdf0e10cSrcweir }
534cdf0e10cSrcweir 
535