xref: /core/sd/source/ui/unoidl/sddetect.cxx (revision 15abfe9a)
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 "sddetect.hxx"
21 
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/ucb/ContentCreationException.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <vcl/graphicfilter.hxx>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/docfilt.hxx>
30 #include <sfx2/fcontnr.hxx>
31 #include <vcl/FilterConfigItem.hxx>
32 #include <sot/storage.hxx>
33 #include <unotools/mediadescriptor.hxx>
34 
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::io;
38 using namespace ::com::sun::star::task;
39 using namespace ::com::sun::star::beans;
40 using namespace ::com::sun::star::lang;
41 using utl::MediaDescriptor;
42 
43 SdFilterDetect::SdFilterDetect()
44 {
45 }
46 
47 SdFilterDetect::~SdFilterDetect()
48 {
49 }
50 
51 OUString SAL_CALL SdFilterDetect::detect( Sequence< beans::PropertyValue >& lDescriptor )
52 {
53     MediaDescriptor aMediaDesc( lDescriptor );
54     OUString aTypeName = aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_TYPENAME(), OUString() );
55     uno::Reference< io::XInputStream > xInStream ( aMediaDesc[MediaDescriptor::PROP_INPUTSTREAM()], uno::UNO_QUERY );
56     if ( !xInStream.is() )
57         return OUString();
58 
59     SfxMedium aMedium;
60     aMedium.UseInteractionHandler( false );
61     aMedium.setStreamToLoadFrom( xInStream, true );
62 
63     SvStream *pInStrm = aMedium.GetInStream();
64     if ( !pInStrm || pInStrm->GetError() )
65         return OUString();
66 
67     if ( aTypeName.startsWith( "impress_MS_PowerPoint_97" ) )
68     {
69         // Do not attempt to create an SotStorage on a
70         // 0-length stream as that would create the compound
71         // document header on the stream and effectively write to
72         // disk!
73         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
74         if ( pInStrm->remainingSize() == 0 )
75             return OUString();
76 
77         try
78         {
79             tools::SvRef<SotStorage> aStorage = new SotStorage( pInStrm, false );
80             if ( !aStorage->GetError() && aStorage->IsStream( "PowerPoint Document" ) )
81                 return aTypeName;
82         }
83         catch (const css::ucb::ContentCreationException&)
84         {
85         }
86     }
87     else
88     {
89         pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
90 
91         const OUString aFileName( aMediaDesc.getUnpackedValueOrDefault( MediaDescriptor::PROP_URL(), OUString() ) );
92         GraphicDescriptor aDesc( *pInStrm, &aFileName );
93         if( !aDesc.Detect() )
94         {
95             INetURLObject aCheckURL( aFileName );
96             if( aCheckURL.getExtension().equalsIgnoreAsciiCase("cgm") )
97             {
98                 sal_uInt8 n8;
99                 pInStrm->Seek( STREAM_SEEK_TO_BEGIN );
100                 pInStrm->ReadUChar( n8 );
101                 if ( ( n8 & 0xf0 ) == 0 )
102                     // we are supporting binary cgm format only, so
103                     // this is a small test to exclude cgm text
104                     return "impress_CGM_Computer_Graphics_Metafile";
105             }
106         }
107         else
108         {
109             OUString aShortName( GraphicDescriptor::GetImportFormatShortName( aDesc.GetFileFormat() ) );
110             GraphicFilter &rGrfFilter = GraphicFilter::GetGraphicFilter();
111             const OUString aName( rGrfFilter.GetImportFormatTypeName( rGrfFilter.GetImportFormatNumberForShortName( aShortName ) ) );
112 
113             if ( aShortName.equalsIgnoreAsciiCase( "PCD" ) )    // there is a multiple pcd selection possible
114             {
115                 sal_Int32 nBase = 2;    // default Base0
116                 if ( aTypeName == "pcd_Photo_CD_Base4" )
117                     nBase = 1;
118                 else if ( aTypeName == "pcd_Photo_CD_Base16" )
119                     nBase = 0;
120                 FilterConfigItem aFilterConfigItem( "Office.Common/Filter/Graphic/Import/PCD" );
121                 aFilterConfigItem.WriteInt32( "Resolution" , nBase );
122             }
123 
124             SfxFilterMatcher aMatch("sdraw");
125             std::shared_ptr<const SfxFilter> pFilter = aMatch.GetFilter4FilterName( aName );
126             if ( pFilter )
127                 return pFilter->GetRealTypeName();
128         }
129     }
130 
131     return OUString();
132 }
133 
134 // XServiceInfo
135 OUString SAL_CALL SdFilterDetect::getImplementationName()
136 {
137     return "com.sun.star.comp.draw.FormatDetector";
138 }
139 
140 // XServiceInfo
141 sal_Bool SAL_CALL SdFilterDetect::supportsService( const OUString& sServiceName )
142 {
143     return cppu::supportsService(this, sServiceName);
144 }
145 
146 // XServiceInfo
147 Sequence< OUString > SAL_CALL SdFilterDetect::getSupportedServiceNames()
148 {
149     return { "com.sun.star.frame.ExtendedTypeDetection" };
150 }
151 
152 
153 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
154 com_sun_star_comp_draw_FormatDetector_get_implementation(css::uno::XComponentContext*,
155                                                          css::uno::Sequence<css::uno::Any> const &)
156 {
157     return cppu::acquire(new SdFilterDetect());
158 }
159 
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
161