Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "smdetect.hxx"
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/beans/PropertyValue.hpp>
24 : #include <com/sun/star/frame/XFrame.hpp>
25 : #include <com/sun/star/frame/XModel.hpp>
26 : #include <com/sun/star/awt/XWindow.hpp>
27 : #include <com/sun/star/lang/XUnoTunnel.hpp>
28 : #include <comphelper/processfactory.hxx>
29 : #include <cppuhelper/supportsservice.hxx>
30 : #include <com/sun/star/io/XInputStream.hpp>
31 : #include <com/sun/star/task/XInteractionHandler.hpp>
32 : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 : #include <com/sun/star/ucb/CommandAbortedException.hpp>
34 : #include <com/sun/star/ucb/InteractiveAppException.hpp>
35 : #include <com/sun/star/ucb/XContent.hpp>
36 : #include <com/sun/star/packages/zip/ZipIOException.hpp>
37 : #include <toolkit/helper/vclunohelper.hxx>
38 : #include <ucbhelper/simpleinteractionrequest.hxx>
39 : #include <rtl/ustring.h>
40 : #include <rtl/logfile.hxx>
41 : #include <svl/itemset.hxx>
42 : #include <vcl/window.hxx>
43 : #include <svl/eitem.hxx>
44 : #include <svl/stritem.hxx>
45 : #include <tools/urlobj.hxx>
46 : #include <osl/mutex.hxx>
47 : #include <svtools/sfxecode.hxx>
48 : #include <svtools/ehdl.hxx>
49 : #include <sot/storinfo.hxx>
50 : #include <vcl/svapp.hxx>
51 : #include <sfx2/app.hxx>
52 : #include <sfx2/sfxsids.hrc>
53 : #include <sfx2/request.hxx>
54 : #include <sfx2/docfile.hxx>
55 : #include <sfx2/docfilt.hxx>
56 : #include <sfx2/fcontnr.hxx>
57 : #include <sfx2/brokenpackageint.hxx>
58 :
59 : #include "document.hxx"
60 : #include "eqnolefilehdr.hxx"
61 :
62 : using namespace ::com::sun::star;
63 : using namespace ::com::sun::star::uno;
64 : using namespace ::com::sun::star::io;
65 : using namespace ::com::sun::star::frame;
66 : using namespace ::com::sun::star::task;
67 : using namespace ::com::sun::star::beans;
68 : using namespace ::com::sun::star::lang;
69 : using namespace ::com::sun::star::ucb;
70 :
71 0 : SmFilterDetect::SmFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
72 : {
73 0 : }
74 :
75 0 : SmFilterDetect::~SmFilterDetect()
76 : {
77 0 : }
78 :
79 0 : OUString SAL_CALL SmFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
80 : {
81 0 : Reference< XInputStream > xStream;
82 0 : Reference< XContent > xContent;
83 0 : Reference< XInteractionHandler > xInteraction;
84 0 : OUString aURL;
85 0 : OUString sTemp;
86 0 : OUString aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection)
87 0 : OUString aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action)
88 :
89 0 : OUString aDocumentTitle; // interesting only if set in this method
90 :
91 : // opening as template is done when a parameter tells to do so and a template filter can be detected
92 : // (otherwise no valid filter would be found) or if the detected filter is a template filter and
93 : // there is no parameter that forbids to open as template
94 0 : sal_Bool bOpenAsTemplate = sal_False;
95 0 : sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
96 :
97 0 : sal_Bool bRepairPackage = sal_False;
98 0 : sal_Bool bRepairAllowed = sal_False;
99 0 : bool bDeepDetection = false;
100 :
101 : // now some parameters that can already be in the array, but may be overwritten or new inserted here
102 : // remember their indices in the case new values must be added to the array
103 0 : sal_Int32 nPropertyCount = lDescriptor.getLength();
104 0 : sal_Int32 nIndexOfInputStream = -1;
105 0 : sal_Int32 nIndexOfContent = -1;
106 0 : sal_Int32 nIndexOfReadOnlyFlag = -1;
107 0 : sal_Int32 nIndexOfTemplateFlag = -1;
108 0 : sal_Int32 nIndexOfDocumentTitle = -1;
109 :
110 0 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
111 : {
112 : // extract properties
113 0 : if( lDescriptor[nProperty].Name == "URL" )
114 : {
115 0 : lDescriptor[nProperty].Value >>= sTemp;
116 0 : aURL = sTemp;
117 : }
118 0 : else if( aURL.isEmpty() && lDescriptor[nProperty].Name == "FileName" )
119 : {
120 0 : lDescriptor[nProperty].Value >>= sTemp;
121 0 : aURL = sTemp;
122 : }
123 0 : else if( lDescriptor[nProperty].Name == "TypeName" )
124 : {
125 0 : lDescriptor[nProperty].Value >>= sTemp;
126 0 : aTypeName = sTemp;
127 : }
128 0 : else if( lDescriptor[nProperty].Name == "FilterName" )
129 : {
130 0 : lDescriptor[nProperty].Value >>= sTemp;
131 0 : aPreselectedFilterName = sTemp;
132 : }
133 0 : else if( lDescriptor[nProperty].Name == "InputStream" )
134 0 : nIndexOfInputStream = nProperty;
135 0 : else if( lDescriptor[nProperty].Name == "ReadOnly" )
136 0 : nIndexOfReadOnlyFlag = nProperty;
137 0 : else if( lDescriptor[nProperty].Name == "UCBContent" )
138 0 : nIndexOfContent = nProperty;
139 0 : else if( lDescriptor[nProperty].Name == "AsTemplate" )
140 : {
141 0 : lDescriptor[nProperty].Value >>= bOpenAsTemplate;
142 0 : nIndexOfTemplateFlag = nProperty;
143 : }
144 0 : else if( lDescriptor[nProperty].Name == "InteractionHandler" )
145 0 : lDescriptor[nProperty].Value >>= xInteraction;
146 0 : else if( lDescriptor[nProperty].Name == "RepairPackage" )
147 0 : lDescriptor[nProperty].Value >>= bRepairPackage;
148 0 : else if( lDescriptor[nProperty].Name == "DocumentTitle" )
149 0 : nIndexOfDocumentTitle = nProperty;
150 0 : else if (lDescriptor[nProperty].Name == "DeepDetection")
151 0 : bDeepDetection = lDescriptor[nProperty].Value.get<sal_Bool>();
152 : }
153 :
154 : // can't check the type for external filters, so set the "dont" flag accordingly
155 0 : SolarMutexGuard aGuard;
156 :
157 0 : SfxApplication* pApp = SFX_APP();
158 0 : SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
159 0 : TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
160 0 : SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, false );
161 :
162 0 : bWasReadOnly = pItem && pItem->GetValue();
163 :
164 0 : OUString aFilterName;
165 0 : OUString aPrefix( "private:factory/" );
166 0 : if( aURL.startsWith( aPrefix ) )
167 : {
168 0 : const SfxFilter* pFilter = 0;
169 0 : OUString aPattern( aPrefix );
170 0 : aPattern += "smath";
171 0 : if ( aURL.startsWith( aPattern ) )
172 : {
173 0 : pFilter = SfxFilter::GetDefaultFilterFromFactory( aURL );
174 0 : aTypeName = pFilter->GetTypeName();
175 0 : aFilterName = pFilter->GetName();
176 0 : }
177 : }
178 : else
179 : {
180 : // ctor of SfxMedium uses owner transition of ItemSet
181 0 : SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, NULL, pSet );
182 0 : aMedium.UseInteractionHandler( true );
183 :
184 0 : bool bIsStorage = aMedium.IsStorage();
185 0 : if ( aMedium.GetErrorCode() == ERRCODE_NONE )
186 : {
187 : // remember input stream and content and put them into the descriptor later
188 : // should be done here since later the medium can switch to a version
189 0 : xStream = aMedium.GetInputStream();
190 0 : xContent = aMedium.GetContent();
191 0 : bReadOnly = aMedium.IsReadOnly();
192 :
193 0 : if ( bIsStorage )
194 : {
195 : //TODO/LATER: factor this out!
196 0 : Reference < embed::XStorage > xStorage = aMedium.GetStorage( false );
197 0 : if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
198 : {
199 : // error during storage creation means _here_ that the medium
200 : // is broken, but we can not handle it in medium since unpossibility
201 : // to create a storage does not _always_ means that the medium is broken
202 0 : aMedium.SetError( aMedium.GetLastStorageCreationState(), OSL_LOG_PREFIX );
203 0 : if ( xInteraction.is() )
204 : {
205 0 : OUString empty;
206 : try
207 : {
208 : InteractiveAppException xException( empty,
209 : Reference< XInterface >(),
210 : InteractionClassification_ERROR,
211 0 : aMedium.GetError() );
212 :
213 : Reference< XInteractionRequest > xRequest(
214 : new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
215 0 : ucbhelper::CONTINUATION_APPROVE ) );
216 0 : xInteraction->handle( xRequest );
217 : }
218 0 : catch ( Exception & ) {};
219 : }
220 : }
221 : else
222 : {
223 0 : aFilterName = OUString();
224 :
225 : try
226 : {
227 0 : const SfxFilter* pFilter = !aPreselectedFilterName.isEmpty() ?
228 0 : SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : !aTypeName.isEmpty() ?
229 0 : SfxFilterMatcher( "smath" ).GetFilter4EA( aTypeName ) : 0;
230 0 : OUString aTmpFilterName;
231 0 : if ( pFilter )
232 0 : aTmpFilterName = pFilter->GetName();
233 0 : aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pFilter && pFilter->IsAllowedAsTemplate(), &aTmpFilterName );
234 : }
235 0 : catch( const WrappedTargetException& aWrap )
236 : {
237 0 : if (!bDeepDetection)
238 : // Bail out early unless it's a deep detection.
239 0 : return OUString();
240 :
241 0 : packages::zip::ZipIOException aZipException;
242 :
243 : // repairing is done only if this type is requested from outside
244 0 : if ( ( aWrap.TargetException >>= aZipException ) && !aTypeName.isEmpty() )
245 : {
246 0 : if ( xInteraction.is() )
247 : {
248 : // the package is broken one
249 0 : aDocumentTitle = aMedium.GetURLObject().getName(
250 : INetURLObject::LAST_SEGMENT,
251 : true,
252 0 : INetURLObject::DECODE_WITH_CHARSET );
253 :
254 0 : if ( !bRepairPackage )
255 : {
256 : // ask the user whether he wants to try to repair
257 0 : RequestPackageReparation aRequest( aDocumentTitle );
258 0 : xInteraction->handle( aRequest.GetRequest() );
259 0 : bRepairAllowed = aRequest.isApproved();
260 : }
261 :
262 0 : if ( !bRepairAllowed )
263 : {
264 : // repair either not allowed or not successful
265 0 : NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
266 0 : xInteraction->handle( aNotifyRequest.GetRequest() );
267 : }
268 : }
269 :
270 0 : if ( !bRepairAllowed )
271 0 : aTypeName = OUString();
272 0 : }
273 0 : }
274 0 : catch( RuntimeException& )
275 : {
276 0 : throw;
277 : }
278 0 : catch( Exception& )
279 : {
280 0 : aTypeName = OUString();
281 : }
282 :
283 0 : if ( !aTypeName.isEmpty() )
284 : {
285 : const SfxFilter* pFilter =
286 0 : SfxFilterMatcher( "smath" ).GetFilter4EA( aTypeName );
287 0 : if ( pFilter )
288 0 : aFilterName = pFilter->GetName();
289 : }
290 0 : }
291 : }
292 : else
293 : {
294 : //Test to see if this begins with xml and if so run it through
295 : //the MathML filter. There are all sorts of things wrong with
296 : //this approach, to be fixed at a better level than here
297 0 : SvStream *pStrm = aMedium.GetInStream();
298 0 : aTypeName = OUString();
299 :
300 0 : sal_Size nSize = 0;
301 0 : if ( pStrm && !pStrm->GetError() )
302 : {
303 0 : pStrm->Seek( STREAM_SEEK_TO_END );
304 0 : nSize = pStrm->Tell();
305 0 : pStrm->Seek( STREAM_SEEK_TO_BEGIN );
306 : }
307 :
308 : // Do not attempt to create an SotStorage on a
309 : // 0-length stream as that would create the compound
310 : // document header on the stream and effectively write to
311 : // disk!
312 0 : if ( nSize > 0 )
313 : {
314 0 : SotStorageRef aStorage = new SotStorage ( pStrm, false );
315 0 : if ( !aStorage->GetError() )
316 : {
317 0 : if (aStorage->IsStream("Equation Native"))
318 : {
319 : sal_uInt8 nVersion;
320 0 : if (GetMathTypeVersion( aStorage, nVersion ) && nVersion <=3)
321 0 : aTypeName = "math_MathType_3x";
322 : }
323 : }
324 : else
325 : {
326 : // 200 should be enough for the XML
327 : // version, encoding and !DOCTYPE
328 : // stuff I hope?
329 0 : const sal_uInt16 nBufferSize = 200;
330 : sal_Char aBuffer[nBufferSize+1];
331 0 : aBuffer[nBufferSize] = 0;
332 0 : pStrm->Seek( STREAM_SEEK_TO_BEGIN );
333 0 : pStrm->StartReadingUnicodeText(RTL_TEXTENCODING_DONTKNOW); // avoid BOM marker
334 0 : sal_uLong nBytesRead = pStrm->Read( aBuffer, nBufferSize );
335 0 : if (nBytesRead >= 6)
336 : {
337 0 : bool bIsMathType = false;
338 0 : if (0 == strncmp( "<?xml", aBuffer, 5))
339 : {
340 0 : if (strstr( aBuffer, "<math>" ) ||
341 0 : strstr( aBuffer, "<math " ) ||
342 0 : strstr( aBuffer, "<math:math " ))
343 0 : bIsMathType = true;
344 : }
345 : // this is the old <math tag to MathML in the beginning of the XML file
346 0 : else if (0 == strncmp( "<math ", aBuffer, 6) ||
347 0 : 0 == strncmp( "<math> ", aBuffer, 7) ||
348 0 : 0 == strncmp( "<math:math> ", aBuffer, 12))
349 0 : bIsMathType = true;
350 :
351 0 : if (bIsMathType){
352 0 : aFilterName = OUString( MATHML_XML );
353 0 : aTypeName = "math_MathML_XML_Math";
354 : }
355 : }
356 : }
357 :
358 0 : if ( !aTypeName.isEmpty() )
359 : {
360 0 : const SfxFilter* pFilt = SfxFilterMatcher( "smath" ).GetFilter4EA( aTypeName );
361 0 : if ( pFilt )
362 0 : aFilterName = pFilt->GetName();
363 0 : }
364 : }
365 : }
366 0 : }
367 : }
368 :
369 0 : if ( nIndexOfInputStream == -1 && xStream.is() )
370 : {
371 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opened twice
372 0 : lDescriptor.realloc( nPropertyCount + 1 );
373 0 : lDescriptor[nPropertyCount].Name = "InputStream";
374 0 : lDescriptor[nPropertyCount].Value <<= xStream;
375 0 : nPropertyCount++;
376 : }
377 :
378 0 : if ( nIndexOfContent == -1 && xContent.is() )
379 : {
380 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opened twice
381 0 : lDescriptor.realloc( nPropertyCount + 1 );
382 0 : lDescriptor[nPropertyCount].Name = "UCBContent";
383 0 : lDescriptor[nPropertyCount].Value <<= xContent;
384 0 : nPropertyCount++;
385 : }
386 :
387 0 : if ( bReadOnly != bWasReadOnly )
388 : {
389 0 : if ( nIndexOfReadOnlyFlag == -1 )
390 : {
391 0 : lDescriptor.realloc( nPropertyCount + 1 );
392 0 : lDescriptor[nPropertyCount].Name = "ReadOnly";
393 0 : lDescriptor[nPropertyCount].Value <<= bReadOnly;
394 0 : nPropertyCount++;
395 : }
396 : else
397 0 : lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
398 : }
399 :
400 0 : if ( !bRepairPackage && bRepairAllowed )
401 : {
402 0 : lDescriptor.realloc( nPropertyCount + 1 );
403 0 : lDescriptor[nPropertyCount].Name = "RepairPackage";
404 0 : lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
405 0 : nPropertyCount++;
406 :
407 0 : bOpenAsTemplate = sal_True;
408 :
409 : // TODO/LATER: set progress bar that should be used
410 : }
411 :
412 0 : if ( bOpenAsTemplate )
413 : {
414 0 : if ( nIndexOfTemplateFlag == -1 )
415 : {
416 0 : lDescriptor.realloc( nPropertyCount + 1 );
417 0 : lDescriptor[nPropertyCount].Name = "AsTemplate";
418 0 : lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
419 0 : nPropertyCount++;
420 : }
421 : else
422 0 : lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
423 : }
424 :
425 0 : if ( !aDocumentTitle.isEmpty() )
426 : {
427 : // the title was set here
428 0 : if ( nIndexOfDocumentTitle == -1 )
429 : {
430 0 : lDescriptor.realloc( nPropertyCount + 1 );
431 0 : lDescriptor[nPropertyCount].Name = "DocumentTitle";
432 0 : lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
433 0 : nPropertyCount++;
434 : }
435 : else
436 0 : lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
437 : }
438 :
439 0 : if ( aFilterName.isEmpty() )
440 0 : aTypeName = OUString();
441 :
442 0 : return aTypeName;
443 : }
444 :
445 : /* XServiceInfo */
446 0 : OUString SAL_CALL SmFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
447 : {
448 0 : return impl_getStaticImplementationName();
449 : }
450 : \
451 : /* XServiceInfo */
452 0 : sal_Bool SAL_CALL SmFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
453 : {
454 0 : return cppu::supportsService(this, sServiceName);
455 : }
456 :
457 : /* XServiceInfo */
458 0 : Sequence< OUString > SAL_CALL SmFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
459 : {
460 0 : return impl_getStaticSupportedServiceNames();
461 : }
462 :
463 : /* Helper for XServiceInfo */
464 0 : Sequence< OUString > SmFilterDetect::impl_getStaticSupportedServiceNames()
465 : {
466 0 : Sequence< OUString > seqServiceNames( 1 );
467 0 : seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
468 0 : return seqServiceNames ;
469 : }
470 :
471 : /* Helper for XServiceInfo */
472 0 : OUString SmFilterDetect::impl_getStaticImplementationName()
473 : {
474 0 : return OUString("com.sun.star.comp.math.FormatDetector");
475 : }
476 :
477 : /* Helper for registry */
478 0 : Reference< XInterface > SAL_CALL SmFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
479 : {
480 0 : return Reference< XInterface >( *new SmFilterDetect( xServiceManager ) );
481 0 : }
482 :
483 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|