LibreOffice Module sd (master) 1
sddetect.cxx
Go to the documentation of this file.
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>
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>
32#include <sot/storage.hxx>
34
35using namespace ::com::sun::star;
36using namespace ::com::sun::star::uno;
37using namespace ::com::sun::star::io;
38using namespace ::com::sun::star::task;
39using namespace ::com::sun::star::beans;
40using namespace ::com::sun::star::lang;
42
44{
45}
46
48{
49}
50
51OUString 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() ) );
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( u"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
136{
137 return "com.sun.star.comp.draw.FormatDetector";
138}
139
140// XServiceInfo
141sal_Bool SAL_CALL SdFilterDetect::supportsService( const OUString& sServiceName )
142{
144}
145
146// XServiceInfo
147Sequence< OUString > SAL_CALL SdFilterDetect::getSupportedServiceNames()
148{
149 return { "com.sun.star.frame.ExtendedTypeDetection" };
150}
151
152
153extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
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: */
constexpr OUStringLiteral sServiceName
void WriteInt32(const OUString &rKey, sal_Int32 nValue)
static OUString GetImportFormatShortName(GraphicFileFormat nFormat)
bool Detect(bool bExtendedInfo=false)
GraphicFileFormat GetFileFormat() const
static GraphicFilter & GetGraphicFilter()
sal_uInt16 GetImportFormatNumberForShortName(std::u16string_view rShortName)
OUString GetImportFormatTypeName(sal_uInt16 nFormat)
OUString getExtension(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: sddetect.cxx:147
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: sddetect.cxx:141
virtual ~SdFilterDetect() override
Definition: sddetect.cxx:47
virtual OUString SAL_CALL detect(css::uno::Sequence< css::beans::PropertyValue > &lDescriptor) override
Definition: sddetect.cxx:51
virtual OUString SAL_CALL getImplementationName() override
Definition: sddetect.cxx:135
std::shared_ptr< const SfxFilter > GetFilter4FilterName(const OUString &rName, SfxFilterFlags nMust=SfxFilterFlags::NONE, SfxFilterFlags nDont=SFX_FILTER_NOTINSTALLED) const
void setStreamToLoadFrom(const css::uno::Reference< css::io::XInputStream > &xInputStream, bool bIsReadOnly)
void UseInteractionHandler(bool)
SvStream * GetInStream()
sal_uInt64 Seek(sal_uInt64 nPos)
ErrCode GetError() const
sal_uInt64 remainingSize()
SvStream & ReadUChar(unsigned char &rChar)
float u
OUString aName
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_draw_FormatDetector_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: sddetect.cxx:154
#define STREAM_SEEK_TO_BEGIN
unsigned char sal_uInt8
unsigned char sal_Bool