1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sddetect.hxx"
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <cppuhelper/supportsservice.hxx>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/ucb/ContentCreationException.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <vcl/graphicfilter.hxx>
30 #include <rtl/ustring.h>
31 #include <sfx2/docfile.hxx>
32 #include <sfx2/docfilt.hxx>
33 #include <sfx2/fcontnr.hxx>
34 #include <vcl/FilterConfigItem.hxx>
35 #include <sot/storage.hxx>
36 #include <unotools/mediadescriptor.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::io
;
41 using namespace ::com::sun::star::task
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::lang
;
44 using utl::MediaDescriptor
;
46 SdFilterDetect::SdFilterDetect()
50 SdFilterDetect::~SdFilterDetect()
54 OUString SAL_CALL
SdFilterDetect::detect( Sequence
< beans::PropertyValue
>& lDescriptor
)
56 MediaDescriptor
aMediaDesc( lDescriptor
);
57 OUString aTypeName
= aMediaDesc
.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
58 uno::Reference
< io::XInputStream
> xInStream ( aMediaDesc
[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY
);
59 if ( !xInStream
.is() )
63 aMedium
.UseInteractionHandler( false );
64 aMedium
.setStreamToLoadFrom( xInStream
, true );
66 SvStream
*pInStrm
= aMedium
.GetInStream();
67 if ( !pInStrm
|| pInStrm
->GetError() )
70 if ( aTypeName
.startsWith( "impress_MS_PowerPoint_97" ) )
72 // Do not attempt to create an SotStorage on a
73 // 0-length stream as that would create the compound
74 // document header on the stream and effectively write to
76 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
77 if ( pInStrm
->remainingSize() == 0 )
82 tools::SvRef
<SotStorage
> aStorage
= new SotStorage( pInStrm
, false );
83 if ( !aStorage
->GetError() && aStorage
->IsStream( "PowerPoint Document" ) )
86 catch (const css::ucb::ContentCreationException
&)
92 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
94 const OUString
aFileName( aMediaDesc
.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() ) );
95 GraphicDescriptor
aDesc( *pInStrm
, &aFileName
);
98 INetURLObject
aCheckURL( aFileName
);
99 if( aCheckURL
.getExtension().equalsIgnoreAsciiCase("cgm") )
102 pInStrm
->Seek( STREAM_SEEK_TO_BEGIN
);
103 pInStrm
->ReadUChar( n8
);
104 if ( ( n8
& 0xf0 ) == 0 )
105 // we are supporting binary cgm format only, so
106 // this is a small test to exclude cgm text
107 return OUString("impress_CGM_Computer_Graphics_Metafile");
112 OUString
aShortName( GraphicDescriptor::GetImportFormatShortName( aDesc
.GetFileFormat() ) );
113 GraphicFilter
&rGrfFilter
= GraphicFilter::GetGraphicFilter();
114 const OUString
aName( rGrfFilter
.GetImportFormatTypeName( rGrfFilter
.GetImportFormatNumberForShortName( aShortName
) ) );
116 if ( aShortName
.equalsIgnoreAsciiCase( "PCD" ) ) // there is a multiple pcd selection possible
118 sal_Int32 nBase
= 2; // default Base0
119 if ( aTypeName
== "pcd_Photo_CD_Base4" )
121 else if ( aTypeName
== "pcd_Photo_CD_Base16" )
123 FilterConfigItem
aFilterConfigItem( "Office.Common/Filter/Graphic/Import/PCD" );
124 aFilterConfigItem
.WriteInt32( "Resolution" , nBase
);
127 SfxFilterMatcher
aMatch("sdraw");
128 std::shared_ptr
<const SfxFilter
> pFilter
= aMatch
.GetFilter4FilterName( aName
);
130 return pFilter
->GetRealTypeName();
138 OUString SAL_CALL
SdFilterDetect::getImplementationName()
140 return OUString( "com.sun.star.comp.draw.FormatDetector" );
144 sal_Bool SAL_CALL
SdFilterDetect::supportsService( const OUString
& sServiceName
)
146 return cppu::supportsService(this, sServiceName
);
150 Sequence
< OUString
> SAL_CALL
SdFilterDetect::getSupportedServiceNames()
152 Sequence
<OUString
> seqServiceNames
{ "com.sun.star.frame.ExtendedTypeDetection" };
153 return seqServiceNames
;
157 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
158 com_sun_star_comp_draw_FormatDetector_get_implementation(css::uno::XComponentContext
*,
159 css::uno::Sequence
<css::uno::Any
> const &)
161 return cppu::acquire(new SdFilterDetect());
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */