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 <cppuhelper/supportsservice.hxx>
28 : #include <com/sun/star/container/XNameAccess.hpp>
29 : #include <com/sun/star/io/XInputStream.hpp>
30 : #include <com/sun/star/task/XInteractionHandler.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 <svl/itemset.hxx>
39 : #include <vcl/window.hxx>
40 : #include <svl/eitem.hxx>
41 : #include <svl/stritem.hxx>
42 : #include <tools/urlobj.hxx>
43 : #include <osl/mutex.hxx>
44 : #include <svtools/sfxecode.hxx>
45 : #include <svtools/ehdl.hxx>
46 : #include <sot/storinfo.hxx>
47 : #include <vcl/svapp.hxx>
48 : #include <sfx2/app.hxx>
49 : #include <sfx2/sfxsids.hrc>
50 : #include <sfx2/request.hxx>
51 : #include <sfx2/docfile.hxx>
52 : #include <sfx2/docfilt.hxx>
53 : #include <sfx2/fcontnr.hxx>
54 : #include <sfx2/brokenpackageint.hxx>
55 : #include <vcl/FilterConfigItem.hxx>
56 : #include <unotools/moduleoptions.hxx>
57 : #include <comphelper/ihwrapnofilter.hxx>
58 : #include <iodetect.hxx>
59 :
60 : using namespace ::com::sun::star;
61 : using namespace ::com::sun::star::uno;
62 : using namespace ::com::sun::star::io;
63 : using namespace ::com::sun::star::frame;
64 : using namespace ::com::sun::star::task;
65 : using namespace ::com::sun::star::beans;
66 : using namespace ::com::sun::star::lang;
67 : using namespace ::com::sun::star::ucb;
68 :
69 0 : SwFilterDetect::SwFilterDetect( const Reference < XMultiServiceFactory >& /*xFactory*/ )
70 : {
71 0 : }
72 :
73 0 : SwFilterDetect::~SwFilterDetect()
74 : {
75 0 : }
76 :
77 0 : OUString SAL_CALL SwFilterDetect::detect( Sequence< PropertyValue >& lDescriptor ) throw( RuntimeException, std::exception )
78 : {
79 0 : Reference< XInputStream > xStream;
80 0 : Reference< XContent > xContent;
81 0 : Reference< XInteractionHandler > xInteraction;
82 0 : OUString aURL;
83 0 : OUString sTemp;
84 0 : OUString aTypeName; // a name describing the type (from MediaDescriptor, usually from flat detection)
85 0 : OUString aPreselectedFilterName; // a name describing the filter to use (from MediaDescriptor, usually from UI action)
86 :
87 0 : OUString aDocumentTitle; // interesting only if set in this method
88 :
89 : // opening as template is done when a parameter tells to do so and a template filter can be detected
90 : // (otherwise no valid filter would be found) or if the detected filter is a template filter and
91 : // there is no parameter that forbids to open as template
92 0 : sal_Bool bOpenAsTemplate = sal_False;
93 0 : sal_Bool bWasReadOnly = sal_False, bReadOnly = sal_False;
94 :
95 0 : sal_Bool bRepairPackage = sal_False;
96 0 : sal_Bool bRepairAllowed = sal_False;
97 0 : bool bDeepDetection = false;
98 :
99 : // now some parameters that can already be in the array, but may be overwritten or new inserted here
100 : // remember their indices in the case new values must be added to the array
101 0 : sal_Int32 nPropertyCount = lDescriptor.getLength();
102 0 : sal_Int32 nIndexOfInputStream = -1;
103 0 : sal_Int32 nIndexOfContent = -1;
104 0 : sal_Int32 nIndexOfReadOnlyFlag = -1;
105 0 : sal_Int32 nIndexOfTemplateFlag = -1;
106 0 : sal_Int32 nIndexOfDocumentTitle = -1;
107 0 : sal_Int32 nIndexOfInteractionHandler = -1;
108 :
109 0 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
110 : {
111 : // extract properties
112 0 : if ( lDescriptor[nProperty].Name == "URL" )
113 : {
114 0 : lDescriptor[nProperty].Value >>= sTemp;
115 0 : aURL = sTemp;
116 : }
117 0 : else if( aURL.isEmpty() && lDescriptor[nProperty].Name == "FileName" )
118 : {
119 0 : lDescriptor[nProperty].Value >>= sTemp;
120 0 : aURL = sTemp;
121 : }
122 0 : else if ( lDescriptor[nProperty].Name == "TypeName" )
123 : {
124 0 : lDescriptor[nProperty].Value >>= sTemp;
125 0 : aTypeName = sTemp;
126 : }
127 0 : else if ( lDescriptor[nProperty].Name == "FilterName" )
128 : {
129 0 : lDescriptor[nProperty].Value >>= sTemp;
130 0 : aPreselectedFilterName = sTemp;
131 : }
132 0 : else if ( lDescriptor[nProperty].Name == "InputStream" )
133 0 : nIndexOfInputStream = nProperty;
134 0 : else if ( lDescriptor[nProperty].Name == "ReadOnly" )
135 0 : nIndexOfReadOnlyFlag = nProperty;
136 0 : else if ( lDescriptor[nProperty].Name == "UCBContent" )
137 0 : nIndexOfContent = nProperty;
138 0 : else if ( lDescriptor[nProperty].Name == "AsTemplate" )
139 : {
140 0 : lDescriptor[nProperty].Value >>= bOpenAsTemplate;
141 0 : nIndexOfTemplateFlag = nProperty;
142 : }
143 0 : else if ( lDescriptor[nProperty].Name == "InteractionHandler" )
144 : {
145 0 : lDescriptor[nProperty].Value >>= xInteraction;
146 0 : nIndexOfInteractionHandler = nProperty;
147 : }
148 0 : else if ( lDescriptor[nProperty].Name == "RepairPackage" )
149 0 : lDescriptor[nProperty].Value >>= bRepairPackage;
150 0 : else if ( lDescriptor[nProperty].Name == "DocumentTitle" )
151 0 : nIndexOfDocumentTitle = nProperty;
152 0 : else if (lDescriptor[nProperty].Name == "DeepDetection")
153 0 : bDeepDetection = lDescriptor[nProperty].Value.get<sal_Bool>();
154 : }
155 :
156 0 : SolarMutexGuard aGuard;
157 :
158 0 : SfxApplication* pApp = SFX_APP();
159 0 : SfxAllItemSet *pSet = new SfxAllItemSet( pApp->GetPool() );
160 0 : TransformParameters( SID_OPENDOC, lDescriptor, *pSet );
161 0 : SFX_ITEMSET_ARG( pSet, pItem, SfxBoolItem, SID_DOC_READONLY, false );
162 :
163 0 : bWasReadOnly = pItem && pItem->GetValue();
164 :
165 0 : const SfxFilter* pFilter = 0;
166 0 : OUString aPrefix = "private:factory/";
167 0 : if( aURL.startsWith( aPrefix ) )
168 : {
169 0 : if( SvtModuleOptions().IsWriter() )
170 : {
171 0 : OUString aPattern = aPrefix + "swriter";
172 0 : if ( aURL.startsWith( aPattern ) )
173 0 : return aTypeName;
174 : }
175 : }
176 : else
177 : {
178 : try
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 0 : if ( aMedium.GetErrorCode() == ERRCODE_NONE )
184 : {
185 : // remember input stream and content and put them into the descriptor later
186 : // should be done here since later the medium can switch to a version
187 0 : xStream = aMedium.GetInputStream();
188 0 : xContent = aMedium.GetContent();
189 0 : bReadOnly = aMedium.IsReadOnly();
190 :
191 0 : sal_Bool bIsStorage = aMedium.IsStorage();
192 0 : if ( bIsStorage )
193 : {
194 0 : Reference< embed::XStorage > xStorage = aMedium.GetStorage( false );
195 0 : if ( aMedium.GetLastStorageCreationState() != ERRCODE_NONE )
196 : {
197 : // error during storage creation means _here_ that the medium
198 : // is broken, but we can not handle it in medium since impossibility
199 : // to create a storage does not _always_ means that the medium is broken
200 0 : aMedium.SetError( aMedium.GetLastStorageCreationState(), OUString( OSL_LOG_PREFIX ) );
201 0 : if ( xInteraction.is() )
202 : {
203 0 : OUString empty;
204 : try
205 : {
206 : InteractiveAppException xException( empty,
207 : Reference< XInterface >(),
208 : InteractionClassification_ERROR,
209 0 : aMedium.GetError() );
210 :
211 : Reference< XInteractionRequest > xRequest(
212 : new ucbhelper::SimpleInteractionRequest( makeAny( xException ),
213 0 : ucbhelper::CONTINUATION_APPROVE ) );
214 0 : xInteraction->handle( xRequest );
215 : }
216 0 : catch (const Exception&)
217 : {
218 0 : }
219 : }
220 : }
221 : else
222 : {
223 : OSL_ENSURE( xStorage.is(), "At this point storage must exist!" );
224 :
225 : try
226 : {
227 0 : const SfxFilter* pPreFilter = !aPreselectedFilterName.isEmpty() ?
228 0 : SfxFilterMatcher().GetFilter4FilterName( aPreselectedFilterName ) : !aTypeName.isEmpty() ?
229 0 : SfxFilterMatcher(OUString("swriter")).GetFilter4EA( aTypeName ) : 0;
230 0 : if (!pPreFilter)
231 0 : pPreFilter = SfxFilterMatcher(OUString("sweb")).GetFilter4EA( aTypeName );
232 0 : OUString aFilterName;
233 0 : if ( pPreFilter )
234 : {
235 0 : aFilterName = pPreFilter->GetName();
236 0 : aTypeName = pPreFilter->GetTypeName();
237 : }
238 :
239 0 : aTypeName = SfxFilter::GetTypeFromStorage( xStorage, pPreFilter && pPreFilter->IsOwnTemplateFormat(), &aFilterName );
240 : }
241 0 : catch (const WrappedTargetException& aWrap)
242 : {
243 0 : if (!bDeepDetection)
244 : // Bail out early unless it's a deep detection.
245 0 : return OUString();
246 :
247 0 : packages::zip::ZipIOException aZipException;
248 :
249 : // repairing is done only if this type is requested from outside
250 : // we don't do any type detection on broken packages (f.e. because it might be impossible), so any requested
251 : // type will be accepted if the user allows to repair the file
252 0 : if ( ( aWrap.TargetException >>= aZipException ) && ( !aTypeName.isEmpty() || !aPreselectedFilterName.isEmpty() ) )
253 : {
254 0 : if ( xInteraction.is() )
255 : {
256 : // the package is a broken one
257 0 : aDocumentTitle = aMedium.GetURLObject().getName(
258 : INetURLObject::LAST_SEGMENT,
259 : true,
260 0 : INetURLObject::DECODE_WITH_CHARSET );
261 :
262 0 : if ( !bRepairPackage )
263 : {
264 : // ask the user whether he wants to try to repair
265 0 : RequestPackageReparation aRequest( aDocumentTitle );
266 0 : xInteraction->handle( aRequest.GetRequest() );
267 0 : bRepairAllowed = aRequest.isApproved();
268 : }
269 :
270 0 : if ( !bRepairAllowed )
271 : {
272 : // repair either not allowed or not successful
273 : // repair either not allowed or not successful
274 0 : NotifyBrokenPackage aNotifyRequest( aDocumentTitle );
275 0 : xInteraction->handle( aNotifyRequest.GetRequest() );
276 :
277 0 : Reference< ::comphelper::OIHWrapNoFilterDialog > xHandler = new ::comphelper::OIHWrapNoFilterDialog( xInteraction );
278 0 : if ( nIndexOfInteractionHandler != -1 )
279 0 : lDescriptor[nIndexOfInteractionHandler].Value <<= Reference< XInteractionHandler >( static_cast< XInteractionHandler* >( xHandler.get() ) );
280 :
281 0 : aMedium.SetError( ERRCODE_ABORT, OUString( OSL_LOG_PREFIX ) );
282 : }
283 : }
284 : else
285 : // no interaction, error handling as usual
286 0 : aMedium.SetError( ERRCODE_IO_BROKENPACKAGE, OUString( OSL_LOG_PREFIX ) );
287 :
288 0 : if ( !bRepairAllowed )
289 : {
290 0 : aTypeName = "";
291 0 : aPreselectedFilterName = "";
292 : }
293 0 : }
294 0 : }
295 0 : }
296 : }
297 : else
298 : {
299 0 : aMedium.GetInStream();
300 0 : if ( aMedium.GetErrorCode() == ERRCODE_NONE )
301 : {
302 0 : if ( !aPreselectedFilterName.isEmpty() )
303 0 : pFilter = SfxFilter::GetFilterByName( aPreselectedFilterName );
304 : else
305 0 : pFilter = SfxFilterMatcher().GetFilter4EA( aTypeName );
306 :
307 0 : bool bTestWriter = !pFilter || pFilter->GetServiceName() == "com.sun.star.text.TextDocument" ||
308 0 : pFilter->GetServiceName() == "com.sun.star.text.WebDocument";
309 0 : bool bTestGlobal = !pFilter || pFilter->GetServiceName() == "com.sun.star.text.GlobalDocument";
310 :
311 0 : const SfxFilter* pOrigFilter = NULL;
312 0 : if ( !bTestWriter && !bTestGlobal && pFilter )
313 : {
314 : // cross filter; now this should be a type detection only, not a filter detection
315 : // we can simulate it by preserving the preselected filter if the type matches
316 : // example: HTML filter for Calc
317 0 : pOrigFilter = pFilter;
318 0 : pFilter = SfxFilterMatcher().GetFilter4EA( pFilter->GetTypeName() );
319 0 : bTestWriter = true;
320 : }
321 :
322 0 : sal_uLong nErr = ERRCODE_NONE;
323 0 : if ( pFilter || bTestWriter )
324 0 : nErr = DetectFilter( aMedium, &pFilter );
325 0 : if ( nErr != ERRCODE_NONE )
326 0 : pFilter = NULL;
327 0 : else if ( pOrigFilter && pFilter && pFilter->GetTypeName() == pOrigFilter->GetTypeName() )
328 : // cross filter, see above
329 0 : pFilter = pOrigFilter;
330 : }
331 :
332 0 : if ( pFilter )
333 0 : aTypeName = pFilter->GetTypeName();
334 : else
335 0 : aTypeName = "";
336 : }
337 0 : }
338 : }
339 0 : catch (const RuntimeException&)
340 : {
341 0 : throw;
342 : }
343 0 : catch (const Exception&)
344 : {
345 0 : aTypeName = "";
346 0 : aPreselectedFilterName = "";
347 : }
348 : }
349 :
350 0 : if ( nIndexOfInputStream == -1 && xStream.is() )
351 : {
352 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opened twice
353 0 : lDescriptor.realloc( nPropertyCount + 1 );
354 0 : lDescriptor[nPropertyCount].Name = "InputStream";
355 0 : lDescriptor[nPropertyCount].Value <<= xStream;
356 0 : nPropertyCount++;
357 : }
358 :
359 0 : if ( nIndexOfContent == -1 && xContent.is() )
360 : {
361 : // if input stream wasn't part of the descriptor, now it should be, otherwise the content would be opened twice
362 0 : lDescriptor.realloc( nPropertyCount + 1 );
363 0 : lDescriptor[nPropertyCount].Name = "UCBContent";
364 0 : lDescriptor[nPropertyCount].Value <<= xContent;
365 0 : nPropertyCount++;
366 : }
367 :
368 0 : if ( bReadOnly != bWasReadOnly )
369 : {
370 0 : if ( nIndexOfReadOnlyFlag == -1 )
371 : {
372 0 : lDescriptor.realloc( nPropertyCount + 1 );
373 0 : lDescriptor[nPropertyCount].Name = "ReadOnly";
374 0 : lDescriptor[nPropertyCount].Value <<= bReadOnly;
375 0 : nPropertyCount++;
376 : }
377 : else
378 0 : lDescriptor[nIndexOfReadOnlyFlag].Value <<= bReadOnly;
379 : }
380 :
381 0 : if ( !bRepairPackage && bRepairAllowed )
382 : {
383 0 : lDescriptor.realloc( nPropertyCount + 1 );
384 0 : lDescriptor[nPropertyCount].Name = "RepairPackage";
385 0 : lDescriptor[nPropertyCount].Value <<= bRepairAllowed;
386 0 : nPropertyCount++;
387 0 : bOpenAsTemplate = sal_True;
388 : // TODO/LATER: set progress bar that should be used
389 : }
390 :
391 0 : if ( bOpenAsTemplate )
392 : {
393 0 : if ( nIndexOfTemplateFlag == -1 )
394 : {
395 0 : lDescriptor.realloc( nPropertyCount + 1 );
396 0 : lDescriptor[nPropertyCount].Name = "AsTemplate";
397 0 : lDescriptor[nPropertyCount].Value <<= bOpenAsTemplate;
398 0 : nPropertyCount++;
399 : }
400 : else
401 0 : lDescriptor[nIndexOfTemplateFlag].Value <<= bOpenAsTemplate;
402 : }
403 :
404 0 : if ( !aDocumentTitle.isEmpty() )
405 : {
406 : // the title was set here
407 0 : if ( nIndexOfDocumentTitle == -1 )
408 : {
409 0 : lDescriptor.realloc( nPropertyCount + 1 );
410 0 : lDescriptor[nPropertyCount].Name = "DocumentTitle";
411 0 : lDescriptor[nPropertyCount].Value <<= aDocumentTitle;
412 0 : nPropertyCount++;
413 : }
414 : else
415 0 : lDescriptor[nIndexOfDocumentTitle].Value <<= aDocumentTitle;
416 : }
417 :
418 0 : return aTypeName;
419 : }
420 :
421 0 : sal_uLong SwFilterDetect::DetectFilter( SfxMedium& rMedium, const SfxFilter** ppFilter )
422 : {
423 0 : sal_uLong nRet = ERRCODE_NONE;
424 0 : if( *ppFilter )
425 : {
426 : // verify the given filter
427 0 : OUString aPrefFlt = (*ppFilter)->GetUserData();
428 :
429 : // detection for TextFilter needs an additional checking
430 0 : sal_Bool bDetected = SwIoSystem::IsFileFilter(rMedium, aPrefFlt);
431 0 : return bDetected ? nRet : ERRCODE_ABORT;
432 : }
433 :
434 : // mba: without preselection there is no PrefFlt
435 0 : OUString aPrefFlt;
436 0 : const SfxFilter* pTmp = SwIoSystem::GetFileFilter( rMedium.GetPhysicalName(), aPrefFlt, &rMedium );
437 0 : if( !pTmp )
438 0 : return ERRCODE_ABORT;
439 :
440 : else
441 : {
442 : //Bug 41417: JP 09.07.97: HTML documents should be loaded by WebWriter
443 0 : SfxFilterContainer aFilterContainer( OUString("swriter/web") );
444 0 : if( !pTmp->GetUserData().equals(sHTML) ||
445 0 : pTmp->GetServiceName() == "com.sun.star.text.WebDocument" ||
446 0 : 0 == ( (*ppFilter) = SwIoSystem::GetFilterOfFormat( OUString(sHTML),
447 0 : &aFilterContainer ) ) )
448 0 : *ppFilter = pTmp;
449 : }
450 :
451 0 : return nRet;
452 : }
453 :
454 : /* XServiceInfo */
455 0 : OUString SAL_CALL SwFilterDetect::getImplementationName() throw( RuntimeException, std::exception )
456 : {
457 0 : return impl_getStaticImplementationName();
458 : }
459 : \
460 : /* XServiceInfo */
461 0 : sal_Bool SAL_CALL SwFilterDetect::supportsService( const OUString& sServiceName ) throw( RuntimeException, std::exception )
462 : {
463 0 : return cppu::supportsService(this, sServiceName);
464 : }
465 :
466 : /* XServiceInfo */
467 0 : Sequence< OUString > SAL_CALL SwFilterDetect::getSupportedServiceNames() throw( RuntimeException, std::exception )
468 : {
469 0 : return impl_getStaticSupportedServiceNames();
470 : }
471 :
472 : /* Helper for XServiceInfo */
473 0 : Sequence< OUString > SwFilterDetect::impl_getStaticSupportedServiceNames()
474 : {
475 0 : Sequence< OUString > seqServiceNames( 3 );
476 0 : seqServiceNames.getArray() [0] = "com.sun.star.frame.ExtendedTypeDetection";
477 0 : seqServiceNames.getArray() [1] = "com.sun.star.text.FormatDetector";
478 0 : seqServiceNames.getArray() [2] = "com.sun.star.text.W4WFormatDetector";
479 0 : return seqServiceNames ;
480 : }
481 :
482 : /* Helper for XServiceInfo */
483 0 : OUString SwFilterDetect::impl_getStaticImplementationName()
484 : {
485 0 : return OUString("com.sun.star.comp.writer.FormatDetector" );
486 : }
487 :
488 : /* Helper for registry */
489 0 : Reference< XInterface > SAL_CALL SwFilterDetect::impl_createInstance( const Reference< XMultiServiceFactory >& xServiceManager ) throw( Exception )
490 : {
491 0 : return Reference< XInterface >( *new SwFilterDetect( xServiceManager ) );
492 : }
493 :
494 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|