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 : #include "swdetect.hxx"
21 :
22 : #include <framework/interaction.hxx>
23 : #include <com/sun/star/frame/XFrame.hpp>
24 : #include <com/sun/star/frame/XModel.hpp>
25 : #include <com/sun/star/lang/XUnoTunnel.hpp>
26 : #include <comphelper/processfactory.hxx>
27 : #include <com/sun/star/container/XNameAccess.hpp>
28 : #include <com/sun/star/io/XInputStream.hpp>
29 : #include <com/sun/star/task/XInteractionHandler.hpp>
30 : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
31 : #include <com/sun/star/ucb/CommandAbortedException.hpp>
32 : #include <com/sun/star/ucb/InteractiveAppException.hpp>
33 : #include <com/sun/star/ucb/XContent.hpp>
34 : #include <com/sun/star/packages/zip/ZipIOException.hpp>
35 : #include <toolkit/helper/vclunohelper.hxx>
36 : #include <ucbhelper/simpleinteractionrequest.hxx>
37 : #include <rtl/ustring.h>
38 : #include <rtl/logfile.hxx>
39 : #include <svl/itemset.hxx>
40 : #include <vcl/window.hxx>
41 : #include <svl/eitem.hxx>
42 : #include <svl/stritem.hxx>
43 : #include <tools/urlobj.hxx>
44 : #include <osl/mutex.hxx>
45 : #include <svtools/sfxecode.hxx>
46 : #include <svtools/ehdl.hxx>
47 : #include <sot/storinfo.hxx>
48 : #include <vcl/svapp.hxx>
49 : #include <sfx2/app.hxx>
50 : #include <sfx2/sfxsids.hrc>
51 : #include <sfx2/request.hxx>
52 : #include <sfx2/docfile.hxx>
53 : #include <sfx2/docfilt.hxx>
54 : #include <sfx2/fcontnr.hxx>
55 : #include <sfx2/brokenpackageint.hxx>
56 : #include <vcl/FilterConfigItem.hxx>
57 : #include <unotools/moduleoptions.hxx>
58 : #include <comphelper/ihwrapnofilter.hxx>
59 :
60 : #include <swdll.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 894 : SwFilterDetect::SwFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
72 : {
73 894 : }
74 :
75 1788 : SwFilterDetect::~SwFilterDetect()
76 : {
77 1788 : }
78 :
79 894 : OUString SAL_CALL SwFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException )
80 : {
81 894 : Reference< XInputStream > xStream;
82 1788 : Reference< XContent > xContent;
83 1788 : Reference< XInteractionHandler > xInteraction;
84 1788 : String aURL;
85 1788 : OUString sTemp;
86 1788 : String aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection)
87 1788 : String aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action)
88 :
89 1788 : 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 894 : sal_Bool bOpenAsTemplate = sal_False;
95 894 : sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
96 :
97 894 : sal_Bool bRepairPackage = sal_False;
98 894 : sal_Bool bRepairAllowed = sal_False;
99 894 : 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 894 : sal_Int32 nPropertyCount = lDescriptor.getLength();
104 894 : sal_Int32 nIndexOfInputStream = -1;
105 894 : sal_Int32 nIndexOfContent = -1;
106 894 : sal_Int32 nIndexOfReadOnlyFlag = -1;
107 894 : sal_Int32 nIndexOfTemplateFlag = -1;
108 894 : sal_Int32 nIndexOfDocumentTitle = -1;
109 894 : sal_Int32 nIndexOfInteractionHandler = -1;
110 :
111 8060 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
112 : {
113 : // extract properties
114 7166 : if ( lDescriptor[nProperty].Name == "URL" )
115 : {
116 894 : lDescriptor[nProperty].Value >>= sTemp;
117 894 : aURL = sTemp;
118 : }
119 6272 : else if( !aURL.Len() && lDescriptor[nProperty].Name == "FileName" )
120 : {
121 0 : lDescriptor[nProperty].Value >>= sTemp;
122 0 : aURL = sTemp;
123 : }
124 6272 : else if ( lDescriptor[nProperty].Name == "TypeName" )
125 : {
126 894 : lDescriptor[nProperty].Value >>= sTemp;
127 894 : aTypeName = sTemp;
128 : }
129 5378 : else if ( lDescriptor[nProperty].Name == "FilterName" )
130 : {
131 0 : lDescriptor[nProperty].Value >>= sTemp;
132 0 : aPreselectedFilterName = sTemp;
133 : }
134 5378 : else if ( lDescriptor[nProperty].Name == "InputStream" )
135 894 : nIndexOfInputStream = nProperty;
136 4484 : else if ( lDescriptor[nProperty].Name == "ReadOnly" )
137 1 : nIndexOfReadOnlyFlag = nProperty;
138 4483 : else if ( lDescriptor[nProperty].Name == "UCBContent" )
139 894 : nIndexOfContent = nProperty;
140 3589 : else if ( lDescriptor[nProperty].Name == "AsTemplate" )
141 : {
142 2 : lDescriptor[nProperty].Value >>= bOpenAsTemplate;
143 2 : nIndexOfTemplateFlag = nProperty;
144 : }
145 3587 : else if ( lDescriptor[nProperty].Name == "InteractionHandler" )
146 : {
147 894 : lDescriptor[nProperty].Value >>= xInteraction;
148 894 : nIndexOfInteractionHandler = nProperty;
149 : }
150 2693 : else if ( lDescriptor[nProperty].Name == "RepairPackage" )
151 0 : lDescriptor[nProperty].Value >>= bRepairPackage;
152 2693 : else if ( lDescriptor[nProperty].Name == "DocumentTitle" )
153 0 : nIndexOfDocumentTitle = nProperty;
154 2693 : else if (lDescriptor[nProperty].Name == "DeepDetection")
155 0 : bDeepDetection = lDescriptor[nProperty].Value.get<sal_Bool>();
156 : }
157 :
158 1788 : SolarMutexGuard aGuard;
159 :
160 894 : SfxApplication* pApp = SFX_APP();
161 894 : SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
162 894 : TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
163 894 : SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, sal_False );
164 :
165 894 : bWasReadOnly = pItem && pItem->GetValue();
166 :
167 894 : const SfxFilter* pFilter = 0;
168 1788 : String aPrefix = OUString("private:factory/");
169 894 : if( aURL.Match( aPrefix ) == aPrefix.Len() )
170 : {
171 0 : if( SvtModuleOptions().IsWriter() )
172 : {
173 0 : String aPattern( aPrefix );
174 0 : aPattern += OUString("swriter");
175 0 : if ( aURL.Match( aPattern ) >= aPattern.Len() )
176 0 : return aTypeName;
177 : }
178 : }
179 : else
180 : {
181 : // ctor of SfxMedium uses owner transition of ItemSet
182 894 : SfxMedium aMedium( aURL, bWasReadOnly ? STREAM_STD_READ : STREAM_STD_READWRITE, NULL, pSet );
183 894 : aMedium.UseInteractionHandler( sal_True );
184 894 : if ( aMedium.GetErrorCode() == ERRCODE_NONE )
185 : {
186 : // remember input stream and content and put them into the descriptor later
187 : // should be done here since later the medium can switch to a version
188 894 : xStream = aMedium.GetInputStream();
189 894 : xContent = aMedium.GetContent();
190 894 : bReadOnly = aMedium.IsReadOnly();
191 :
192 894 : sal_Bool bIsStorage = aMedium.IsStorage();
193 894 : if ( bIsStorage )
194 : {
195 53 : Reference< embed::XStorage > xStorage = aMedium.GetStorage( sal_False );
196 53 : if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
197 : {
198 : // error during storage creation means _here_ that the medium
199 : // is broken, but we can not handle it in medium since impossibility
200 : // to create a storage does not _always_ means that the medium is broken
201 0 : aMedium.SetError( aMedium.GetLastStorageCreationState(), OUString( OSL_LOG_PREFIX ) );
202 0 : if ( xInteraction.is() )
203 : {
204 0 : OUString empty;
205 : try
206 : {
207 : InteractiveAppException xException( empty,
208 : Reference< XInterface >(),
209 : InteractionClassification_ERROR,
210 0 : aMedium.GetError() );
211 :
212 : Reference< XInteractionRequest > xRequest(
213 : new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
214 0 : ucbhelper::CONTINUATION_APPROVE ) );
215 0 : xInteraction->handle( xRequest );
216 : }
217 0 : catch (const Exception&)
218 : {
219 0 : }
220 : }
221 : }
222 : else
223 : {
224 : OSL_ENSURE( xStorage.is(), "At this point storage must exist!" );
225 :
226 : try
227 : {
228 53 : const SfxFilter* pPreFilter = aPreselectedFilterName.Len() ?
229 106 : SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : aTypeName.Len() ?
230 159 : SfxFilterMatcher(OUString("swriter")).GetFilter4EA( aTypeName ) : 0;
231 53 : if (!pPreFilter)
232 0 : pPreFilter = SfxFilterMatcher(OUString("sweb")).GetFilter4EA( aTypeName );
233 53 : OUString aFilterName;
234 53 : if ( pPreFilter )
235 : {
236 53 : aFilterName = pPreFilter->GetName();
237 53 : aTypeName = pPreFilter->GetTypeName();
238 : }
239 :
240 53 : aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pPreFilter ? pPreFilter->IsOwnTemplateFormat() : sal_False, &aFilterName );
241 : }
242 0 : catch (const WrappedTargetException& aWrap)
243 : {
244 : /* Cater for non-compliant sxw files created once upon a time by old libwpd/wpd2sxw combo
245 : Don't bail-out early if the document is considered as corrupted. This allows us not to
246 : reach the -- apparently -- catch-all SmFilterDetect::detect.
247 : */
248 0 : if (!bDeepDetection && aTypeName != "writer_StarOffice_XML_Writer")
249 : // Bail out early unless it's a deep detection.
250 0 : return OUString();
251 :
252 0 : packages::zip::ZipIOException aZipException;
253 :
254 : // repairing is done only if this type is requested from outside
255 : // we don't do any type detection on broken packages (f.e. because it might be impossible), so any requested
256 : // type will be accepted if the user allows to repair the file
257 0 : if ( ( aWrap.TargetException >>= aZipException ) && ( aTypeName.Len() || aPreselectedFilterName.Len() ) )
258 : {
259 0 : if ( xInteraction.is() )
260 : {
261 : // the package is a broken one
262 0 : aDocumentTitle = aMedium.GetURLObject().getName(
263 : INetURLObject::LAST_SEGMENT,
264 : true,
265 0 : INetURLObject::DECODE_WITH_CHARSET );
266 :
267 0 : if ( !bRepairPackage )
268 : {
269 : // ask the user whether he wants to try to repair
270 0 : RequestPackageReparation aRequest( aDocumentTitle );
271 0 : xInteraction->handle( aRequest.GetRequest() );
272 0 : bRepairAllowed = aRequest.isApproved();
273 : }
274 :
275 0 : if ( !bRepairAllowed )
276 : {
277 : // repair either not allowed or not successful
278 : // repair either not allowed or not successful
279 0 : NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
280 0 : xInteraction->handle( aNotifyRequest.GetRequest() );
281 :
282 0 : Reference< ::comphelper::OIHWrapNoFilterDialog > xHandler = new ::comphelper::OIHWrapNoFilterDialog( xInteraction );
283 0 : if ( nIndexOfInteractionHandler != -1 )
284 0 : lDescriptor[nIndexOfInteractionHandler].Value <<= Reference< XInteractionHandler >( static_cast< XInteractionHandler* >( xHandler.get() ) );
285 :
286 0 : aMedium.SetError( ERRCODE_ABORT, OUString( OSL_LOG_PREFIX ) );
287 : }
288 : }
289 : else
290 : // no interaction, error handling as usual
291 0 : aMedium.SetError( ERRCODE_IO_BROKENPACKAGE, OUString( OSL_LOG_PREFIX ) );
292 :
293 0 : if ( !bRepairAllowed )
294 : {
295 0 : aTypeName.Erase();
296 0 : aPreselectedFilterName.Erase();
297 : }
298 0 : }
299 0 : }
300 0 : catch (const RuntimeException&)
301 : {
302 0 : throw;
303 : }
304 0 : catch (const Exception&)
305 : {
306 0 : aTypeName.Erase();
307 0 : aPreselectedFilterName.Erase();
308 : }
309 53 : }
310 : }
311 : else
312 : {
313 841 : aMedium.GetInStream();
314 841 : if ( aMedium.GetErrorCode() == ERRCODE_NONE )
315 : {
316 841 : if ( aPreselectedFilterName.Len() )
317 0 : pFilter = SfxFilter::GetFilterByName( aPreselectedFilterName );
318 : else
319 841 : pFilter = SfxFilterMatcher().GetFilter4EA( aTypeName );
320 :
321 1038 : bool bTestWriter = !pFilter || pFilter->GetServiceName() == "com.sun.star.text.TextDocument" ||
322 1038 : pFilter->GetServiceName() == "com.sun.star.text.WebDocument";
323 841 : bool bTestGlobal = !pFilter || pFilter->GetServiceName() == "com.sun.star.text.GlobalDocument";
324 :
325 841 : const SfxFilter* pOrigFilter = NULL;
326 841 : if ( !bTestWriter && !bTestGlobal && pFilter )
327 : {
328 : // cross filter; now this should be a type detection only, not a filter detection
329 : // we can simulate it by preserving the preselected filter if the type matches
330 : // example: HTML filter for Calc
331 0 : pOrigFilter = pFilter;
332 0 : pFilter = SfxFilterMatcher().GetFilter4EA( pFilter->GetTypeName() );
333 0 : bTestWriter = true;
334 : }
335 :
336 841 : sal_uLong nErr = ERRCODE_NONE;
337 841 : if ( pFilter || bTestWriter )
338 841 : nErr = DetectFilter( aMedium, &pFilter );
339 841 : if ( nErr != ERRCODE_NONE )
340 638 : pFilter = NULL;
341 203 : else if ( pOrigFilter && pFilter && pFilter->GetTypeName() == pOrigFilter->GetTypeName() )
342 : // cross filter, see above
343 0 : pFilter = pOrigFilter;
344 : }
345 :
346 841 : if ( pFilter )
347 203 : aTypeName = pFilter->GetTypeName();
348 : else
349 638 : aTypeName.Erase();
350 : }
351 894 : }
352 : }
353 :
354 894 : if ( nIndexOfInputStream == -1 && xStream.is() )
355 : {
356 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
357 0 : lDescriptor.realloc( nPropertyCount + 1 );
358 0 : lDescriptor[nPropertyCount].Name = OUString("InputStream");
359 0 : lDescriptor[nPropertyCount].Value <<= xStream;
360 0 : nPropertyCount++;
361 : }
362 :
363 894 : if ( nIndexOfContent == -1 && xContent.is() )
364 : {
365 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opend twice
366 0 : lDescriptor.realloc( nPropertyCount + 1 );
367 0 : lDescriptor[nPropertyCount].Name = OUString("UCBContent");
368 0 : lDescriptor[nPropertyCount].Value <<= xContent;
369 0 : nPropertyCount++;
370 : }
371 :
372 894 : if ( bReadOnly != bWasReadOnly )
373 : {
374 0 : if ( nIndexOfReadOnlyFlag == -1 )
375 : {
376 0 : lDescriptor.realloc( nPropertyCount + 1 );
377 0 : lDescriptor[nPropertyCount].Name = OUString("ReadOnly");
378 0 : lDescriptor[nPropertyCount].Value <<= bReadOnly;
379 0 : nPropertyCount++;
380 : }
381 : else
382 0 : lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
383 : }
384 :
385 894 : if ( !bRepairPackage && bRepairAllowed )
386 : {
387 0 : lDescriptor.realloc( nPropertyCount + 1 );
388 0 : lDescriptor[nPropertyCount].Name = OUString("RepairPackage");
389 0 : lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
390 0 : nPropertyCount++;
391 0 : bOpenAsTemplate = sal_True;
392 : // TODO/LATER: set progress bar that should be used
393 : }
394 :
395 894 : if ( bOpenAsTemplate )
396 : {
397 2 : if ( nIndexOfTemplateFlag == -1 )
398 : {
399 0 : lDescriptor.realloc( nPropertyCount + 1 );
400 0 : lDescriptor[nPropertyCount].Name = OUString("AsTemplate");
401 0 : lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
402 0 : nPropertyCount++;
403 : }
404 : else
405 2 : lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
406 : }
407 :
408 894 : if ( !aDocumentTitle.isEmpty() )
409 : {
410 : // the title was set here
411 0 : if ( nIndexOfDocumentTitle == -1 )
412 : {
413 0 : lDescriptor.realloc( nPropertyCount + 1 );
414 0 : lDescriptor[nPropertyCount].Name = OUString("DocumentTitle");
415 0 : lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
416 0 : nPropertyCount++;
417 : }
418 : else
419 0 : lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
420 : }
421 :
422 :
423 1788 : return aTypeName;
424 : }
425 :
426 : /* XServiceInfo */
427 0 : OUString SAL_CALL SwFilterDetect::getImplementationName() throw( RuntimeException )
428 : {
429 0 : return impl_getStaticImplementationName();
430 : }
431 : \
432 : /* XServiceInfo */
433 0 : sal_Bool SAL_CALL SwFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException )
434 : {
435 0 : Sequence< OUString > seqServiceNames = getSupportedServiceNames();
436 0 : const OUString* pArray = seqServiceNames.getConstArray();
437 0 : for ( sal_Int32 nCounter=0; nCounter<seqServiceNames.getLength(); nCounter++ )
438 : {
439 0 : if ( pArray[nCounter] == sServiceName )
440 : {
441 0 : return sal_True ;
442 : }
443 : }
444 0 : return sal_False ;
445 : }
446 :
447 : /* XServiceInfo */
448 0 : Sequence< OUString > SAL_CALL SwFilterDetect::getSupportedServiceNames() throw( RuntimeException )
449 : {
450 0 : return impl_getStaticSupportedServiceNames();
451 : }
452 :
453 : /* Helper for XServiceInfo */
454 17 : Sequence< OUString > SwFilterDetect::impl_getStaticSupportedServiceNames()
455 : {
456 17 : Sequence< OUString > seqServiceNames( 3 );
457 17 : seqServiceNames.getArray() [0] = OUString("com.sun.star.frame.ExtendedTypeDetection" );
458 17 : seqServiceNames.getArray() [1] = OUString("com.sun.star.text.FormatDetector" );
459 17 : seqServiceNames.getArray() [2] = OUString("com.sun.star.text.W4WFormatDetector" );
460 17 : return seqServiceNames ;
461 : }
462 :
463 : /* Helper for XServiceInfo */
464 34 : OUString SwFilterDetect::impl_getStaticImplementationName()
465 : {
466 34 : return OUString("com.sun.star.comp.writer.FormatDetector" );
467 : }
468 :
469 : /* Helper for registry */
470 894 : Reference< XInterface > SAL_CALL SwFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
471 : {
472 894 : return Reference< XInterface >( *new SwFilterDetect( xServiceManager ) );
473 102 : }
474 :
475 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|