xref: /trunk/main/starmath/source/smdetect.cxx (revision d107581f588b53ce11339a8a18091c0f86ec8bb4)
1*d107581fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d107581fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d107581fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d107581fSAndrew Rist  * distributed with this work for additional information
6*d107581fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d107581fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d107581fSAndrew Rist  * "License"); you may not use this file except in compliance
9*d107581fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d107581fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d107581fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d107581fSAndrew Rist  * software distributed under the License is distributed on an
15*d107581fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d107581fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d107581fSAndrew Rist  * specific language governing permissions and limitations
18*d107581fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d107581fSAndrew Rist  *************************************************************/
21*d107581fSAndrew Rist 
22*d107581fSAndrew 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                     {
329cdf0e10cSrcweir                         const sal_uInt16 nSize = 5;
330cdf0e10cSrcweir                         sal_Char aBuffer[nSize+1];
331cdf0e10cSrcweir                         aBuffer[nSize] = 0;
332cdf0e10cSrcweir                         pStrm->Seek( STREAM_SEEK_TO_BEGIN );
333cdf0e10cSrcweir                         sal_uLong nBytesRead = pStrm->Read( aBuffer, nSize );
334cdf0e10cSrcweir                         if (nBytesRead == nSize)
335cdf0e10cSrcweir                         {
336cdf0e10cSrcweir                             if (0 == strncmp( "<?xml",aBuffer,nSize))
337cdf0e10cSrcweir                             {
338cdf0e10cSrcweir                                 static const sal_Char sFltrNm_2[] = MATHML_XML;
339cdf0e10cSrcweir                                 static const sal_Char sTypeNm_2[] = "math_MathML_XML_Math";
340cdf0e10cSrcweir                                 aFilterName.AssignAscii( sFltrNm_2 );
341cdf0e10cSrcweir                                 aTypeName.AssignAscii( sTypeNm_2 );
342cdf0e10cSrcweir                             }
343cdf0e10cSrcweir                         }
344cdf0e10cSrcweir                     }
345cdf0e10cSrcweir 
346cdf0e10cSrcweir                     if ( aTypeName.Len() )
347cdf0e10cSrcweir                     {
348cdf0e10cSrcweir                         const SfxFilter* pFilt = SfxFilterMatcher( String::CreateFromAscii("smath") ).GetFilter4EA( aTypeName );
349cdf0e10cSrcweir                         if ( pFilt )
350cdf0e10cSrcweir                             aFilterName = pFilt->GetName();
351cdf0e10cSrcweir                     }
352cdf0e10cSrcweir                 }
353cdf0e10cSrcweir             }
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir     }
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     if ( nIndexOfInputStream == -1 && xStream.is() )
358cdf0e10cSrcweir     {
359cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
360cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
361cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("InputStream");
362cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xStream;
363cdf0e10cSrcweir         nPropertyCount++;
364cdf0e10cSrcweir     }
365cdf0e10cSrcweir 
366cdf0e10cSrcweir     if ( nIndexOfContent == -1 && xContent.is() )
367cdf0e10cSrcweir     {
368cdf0e10cSrcweir         // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
369cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
370cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("UCBContent");
371cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= xContent;
372cdf0e10cSrcweir         nPropertyCount++;
373cdf0e10cSrcweir     }
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     if ( bReadOnly != bWasReadOnly )
376cdf0e10cSrcweir     {
377cdf0e10cSrcweir         if ( nIndexOfReadOnlyFlag == -1 )
378cdf0e10cSrcweir         {
379cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
380cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("ReadOnly");
381cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= bReadOnly;
382cdf0e10cSrcweir             nPropertyCount++;
383cdf0e10cSrcweir         }
384cdf0e10cSrcweir         else
385cdf0e10cSrcweir             lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
386cdf0e10cSrcweir     }
387cdf0e10cSrcweir 
388cdf0e10cSrcweir     if ( !bRepairPackage && bRepairAllowed )
389cdf0e10cSrcweir     {
390cdf0e10cSrcweir         lDescriptor.realloc( nPropertyCount + 1 );
391cdf0e10cSrcweir         lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("RepairPackage");
392cdf0e10cSrcweir         lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
393cdf0e10cSrcweir         nPropertyCount++;
394cdf0e10cSrcweir 
395cdf0e10cSrcweir         bOpenAsTemplate = sal_True;
396cdf0e10cSrcweir 
397cdf0e10cSrcweir         // TODO/LATER: set progress bar that should be used
398cdf0e10cSrcweir     }
399cdf0e10cSrcweir 
400cdf0e10cSrcweir     if ( bOpenAsTemplate )
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         if ( nIndexOfTemplateFlag == -1 )
403cdf0e10cSrcweir         {
404cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
405cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("AsTemplate");
406cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
407cdf0e10cSrcweir             nPropertyCount++;
408cdf0e10cSrcweir         }
409cdf0e10cSrcweir         else
410cdf0e10cSrcweir             lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
411cdf0e10cSrcweir     }
412cdf0e10cSrcweir 
413cdf0e10cSrcweir     if ( aDocumentTitle.getLength() )
414cdf0e10cSrcweir     {
415cdf0e10cSrcweir         // the title was set here
416cdf0e10cSrcweir         if ( nIndexOfDocumentTitle == -1 )
417cdf0e10cSrcweir         {
418cdf0e10cSrcweir             lDescriptor.realloc( nPropertyCount + 1 );
419cdf0e10cSrcweir             lDescriptor[nPropertyCount].Name = ::rtl::OUString::createFromAscii("DocumentTitle");
420cdf0e10cSrcweir             lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
421cdf0e10cSrcweir             nPropertyCount++;
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir         else
424cdf0e10cSrcweir             lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
425cdf0e10cSrcweir     }
426cdf0e10cSrcweir 
427cdf0e10cSrcweir     if ( !aFilterName.Len() )
428cdf0e10cSrcweir         aTypeName.Erase();
429cdf0e10cSrcweir 
430cdf0e10cSrcweir     return aTypeName;
431cdf0e10cSrcweir }
432cdf0e10cSrcweir 
433cdf0e10cSrcweir SFX_IMPL_SINGLEFACTORY( SmFilterDetect )
434cdf0e10cSrcweir 
435cdf0e10cSrcweir /* XServiceInfo */
436cdf0e10cSrcweir UNOOUSTRING SAL_CALL SmFilterDetect::getImplementationName() throw( UNORUNTIMEEXCEPTION )
437cdf0e10cSrcweir {
438cdf0e10cSrcweir     return impl_getStaticImplementationName();
439cdf0e10cSrcweir }
440cdf0e10cSrcweir                                                                                                                                 \
441cdf0e10cSrcweir /* XServiceInfo */
442cdf0e10cSrcweir sal_Bool SAL_CALL SmFilterDetect::supportsService( const UNOOUSTRING& sServiceName ) throw( UNORUNTIMEEXCEPTION )
443cdf0e10cSrcweir {
444cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING >  seqServiceNames =   getSupportedServiceNames();
445cdf0e10cSrcweir     const UNOOUSTRING*          pArray          =   seqServiceNames.getConstArray();
446cdf0e10cSrcweir     for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
447cdf0e10cSrcweir     {
448cdf0e10cSrcweir         if ( pArray[nCounter] == sServiceName )
449cdf0e10cSrcweir         {
450cdf0e10cSrcweir             return sal_True ;
451cdf0e10cSrcweir         }
452cdf0e10cSrcweir     }
453cdf0e10cSrcweir     return sal_False ;
454cdf0e10cSrcweir }
455cdf0e10cSrcweir 
456cdf0e10cSrcweir /* XServiceInfo */
457cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( UNORUNTIMEEXCEPTION )
458cdf0e10cSrcweir {
459cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
460cdf0e10cSrcweir }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir /* Helper for XServiceInfo */
463cdf0e10cSrcweir UNOSEQUENCE< UNOOUSTRING > SmFilterDetect::impl_getStaticSupportedServiceNames()
464cdf0e10cSrcweir {
465cdf0e10cSrcweir     UNOMUTEXGUARD aGuard( UNOMUTEX::getGlobalMutex() );
466cdf0e10cSrcweir     UNOSEQUENCE< UNOOUSTRING > seqServiceNames( 1 );
467cdf0e10cSrcweir     seqServiceNames.getArray() [0] = UNOOUSTRING::createFromAscii( "com.sun.star.frame.ExtendedTypeDetection"  );
468cdf0e10cSrcweir     return seqServiceNames ;
469cdf0e10cSrcweir }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir /* Helper for XServiceInfo */
472cdf0e10cSrcweir UNOOUSTRING SmFilterDetect::impl_getStaticImplementationName()
473cdf0e10cSrcweir {
474cdf0e10cSrcweir     return UNOOUSTRING::createFromAscii( "com.sun.star.comp.math.FormatDetector" );
475cdf0e10cSrcweir }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir /* Helper for registry */
478cdf0e10cSrcweir UNOREFERENCE< UNOXINTERFACE > SAL_CALL SmFilterDetect::impl_createInstance( const UNOREFERENCE< UNOXMULTISERVICEFACTORY >& xServiceManager ) throw( UNOEXCEPTION )
479cdf0e10cSrcweir {
480cdf0e10cSrcweir     return UNOREFERENCE< UNOXINTERFACE >( *new SmFilterDetect( xServiceManager ) );
481cdf0e10cSrcweir }
482cdf0e10cSrcweir 
483