Update ooo320-m1
[ooovba.git] / goodies / source / unographic / descriptor.cxx
blob663ba26911eb598736bc13ac48af4bca7f92d658
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: descriptor.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_goodies.hxx"
34 #include "descriptor.hxx"
36 #include <rtl/uuid.h>
37 #include <vos/mutex.hxx>
38 #include <unotools/ucbstreamhelper.hxx>
39 #include <svtools/filter.hxx>
40 #include <svtools/itemprop.hxx>
42 #ifndef _COM_SUN_STAR_BEANS_PROPERTYSTATE_HDL_
43 #include <com/sun/star/beans/PropertyState.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HDL_
46 #include <com/sun/star/beans/PropertyAttribute.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_AWT_SIZE_HDL_
49 #include <com/sun/star/awt/Size.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_GRAPHIC_GRAPHICTYPE_HDL_
52 #include <com/sun/star/graphic/GraphicType.hpp>
53 #endif
54 #include "vcl/graph.hxx"
55 #include "vcl/svapp.hxx"
57 #define UNOGRAPHIC_GRAPHICTYPE 1
58 #define UNOGRAPHIC_MIMETYPE 2
59 #define UNOGRAPHIC_SIZEPIXEL 3
60 #define UNOGRAPHIC_SIZE100THMM 4
61 #define UNOGRAPHIC_BITSPERPIXEL 5
62 #define UNOGRAPHIC_TRANSPARENT 6
63 #define UNOGRAPHIC_ALPHA 7
64 #define UNOGRAPHIC_ANIMATED 8
66 using namespace ::com::sun::star;
68 namespace unographic {
70 // ---------------------
71 // - GraphicDescriptor -
72 // ---------------------
74 GraphicDescriptor::GraphicDescriptor() :
75 ::comphelper::PropertySetHelper( createPropertySetInfo(), SAL_NO_ACQUIRE ),
76 mpGraphic( NULL ),
77 meType( GRAPHIC_NONE ),
78 mnBitsPerPixel ( 0 ),
79 mbTransparent ( false ),
80 mbAlpha( false ),
81 mbAnimated( false )
85 // ------------------------------------------------------------------------------
87 GraphicDescriptor::~GraphicDescriptor()
88 throw()
92 // ------------------------------------------------------------------------------
94 void GraphicDescriptor::init( const ::Graphic& rGraphic )
95 throw()
97 mpGraphic = &rGraphic;
100 // ------------------------------------------------------------------------------
102 void GraphicDescriptor::init( const ::rtl::OUString& rURL )
103 throw()
105 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rURL, STREAM_READ );
107 if( pIStm )
109 implCreate( *pIStm, &rURL );
110 delete pIStm;
114 // ------------------------------------------------------------------------------
116 void GraphicDescriptor::init( const uno::Reference< io::XInputStream >& rxIStm, const ::rtl::OUString& rURL )
117 throw()
119 SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( rxIStm );
121 if( pIStm )
123 implCreate( *pIStm, &rURL );
124 delete pIStm;
128 // ------------------------------------------------------------------------------
130 bool GraphicDescriptor::isValid() const
132 return( mpGraphic ? ( mpGraphic->GetType() != GRAPHIC_NONE ) : ( meType != GRAPHIC_NONE ) );
135 // ------------------------------------------------------------------------------
137 void GraphicDescriptor::implCreate( SvStream& rIStm, const ::rtl::OUString* pURL )
139 String aURL;
140 if( pURL )
141 aURL = *pURL;
142 ::GraphicDescriptor aDescriptor( rIStm, &aURL );
144 mpGraphic = NULL;
145 maMimeType = ::rtl::OUString();
146 meType = GRAPHIC_NONE;
147 mnBitsPerPixel = 0;
148 mbTransparent = false;
150 if( aDescriptor.Detect( true ) && aDescriptor.GetFileFormat() != GFF_NOT )
152 const char* pMimeType = NULL;
153 sal_uInt8 cType = graphic::GraphicType::EMPTY;
155 switch( aDescriptor.GetFileFormat() )
157 case( GFF_BMP ): pMimeType = MIMETYPE_BMP; cType = graphic::GraphicType::PIXEL; break;
158 case( GFF_GIF ): pMimeType = MIMETYPE_GIF; cType = graphic::GraphicType::PIXEL; break;
159 case( GFF_JPG ): pMimeType = MIMETYPE_JPG; cType = graphic::GraphicType::PIXEL; break;
160 case( GFF_PCD ): pMimeType = MIMETYPE_PCD; cType = graphic::GraphicType::PIXEL; break;
161 case( GFF_PCX ): pMimeType = MIMETYPE_PCX; cType = graphic::GraphicType::PIXEL; break;
162 case( GFF_PNG ): pMimeType = MIMETYPE_PNG; cType = graphic::GraphicType::PIXEL; break;
163 case( GFF_TIF ): pMimeType = MIMETYPE_TIF; cType = graphic::GraphicType::PIXEL; break;
164 case( GFF_XBM ): pMimeType = MIMETYPE_XBM; cType = graphic::GraphicType::PIXEL; break;
165 case( GFF_XPM ): pMimeType = MIMETYPE_XPM; cType = graphic::GraphicType::PIXEL; break;
166 case( GFF_PBM ): pMimeType = MIMETYPE_PBM; cType = graphic::GraphicType::PIXEL; break;
167 case( GFF_PGM ): pMimeType = MIMETYPE_PGM; cType = graphic::GraphicType::PIXEL; break;
168 case( GFF_PPM ): pMimeType = MIMETYPE_PPM; cType = graphic::GraphicType::PIXEL; break;
169 case( GFF_RAS ): pMimeType = MIMETYPE_RAS; cType = graphic::GraphicType::PIXEL; break;
170 case( GFF_TGA ): pMimeType = MIMETYPE_TGA; cType = graphic::GraphicType::PIXEL; break;
171 case( GFF_PSD ): pMimeType = MIMETYPE_PSD; cType = graphic::GraphicType::PIXEL; break;
173 case( GFF_EPS ): pMimeType = MIMETYPE_EPS; cType = graphic::GraphicType::VECTOR; break;
174 case( GFF_DXF ): pMimeType = MIMETYPE_DXF; cType = graphic::GraphicType::VECTOR; break;
175 case( GFF_MET ): pMimeType = MIMETYPE_MET; cType = graphic::GraphicType::VECTOR; break;
176 case( GFF_PCT ): pMimeType = MIMETYPE_PCT; cType = graphic::GraphicType::VECTOR; break;
177 case( GFF_SGF ): pMimeType = MIMETYPE_SGF; cType = graphic::GraphicType::VECTOR; break;
178 case( GFF_SVM ): pMimeType = MIMETYPE_SVM; cType = graphic::GraphicType::VECTOR; break;
179 case( GFF_WMF ): pMimeType = MIMETYPE_WMF; cType = graphic::GraphicType::VECTOR; break;
180 case( GFF_SGV ): pMimeType = MIMETYPE_SGV; cType = graphic::GraphicType::VECTOR; break;
181 case( GFF_EMF ): pMimeType = MIMETYPE_EMF; cType = graphic::GraphicType::VECTOR; break;
183 default:
184 break;
187 if( graphic::GraphicType::EMPTY != cType )
189 meType = ( ( graphic::GraphicType::PIXEL == cType ) ? GRAPHIC_BITMAP : GRAPHIC_GDIMETAFILE );
190 maMimeType = String( pMimeType, RTL_TEXTENCODING_ASCII_US );
191 maSizePixel = aDescriptor.GetSizePixel();
192 maSize100thMM = aDescriptor.GetSize_100TH_MM();
193 mnBitsPerPixel = aDescriptor.GetBitsPerPixel();
194 mbTransparent = ( graphic::GraphicType::VECTOR == cType );
195 mbAlpha = mbAnimated = false;
200 // ------------------------------------------------------------------------------
202 ::rtl::OUString GraphicDescriptor::getImplementationName_Static()
203 throw()
205 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.graphic.GraphicDescriptor" ) );
208 // ------------------------------------------------------------------------------
210 uno::Sequence< ::rtl::OUString > GraphicDescriptor::getSupportedServiceNames_Static()
211 throw( )
213 uno::Sequence< ::rtl::OUString > aSeq( 1 );
215 aSeq.getArray()[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.graphic.GraphicDescriptor" ) );
217 return aSeq;
220 // ------------------------------------------------------------------------------
222 uno::Any SAL_CALL GraphicDescriptor::queryAggregation( const uno::Type & rType )
223 throw( uno::RuntimeException )
225 uno::Any aAny;
227 if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
228 aAny <<= uno::Reference< lang::XServiceInfo >(this);
229 else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
230 aAny <<= uno::Reference< lang::XTypeProvider >(this);
231 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
232 aAny <<= uno::Reference< beans::XPropertySet >(this);
233 else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
234 aAny <<= uno::Reference< beans::XPropertyState >(this);
235 else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
236 aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
237 else
238 aAny <<= OWeakAggObject::queryAggregation( rType );
240 return aAny;
243 // ------------------------------------------------------------------------------
245 uno::Any SAL_CALL GraphicDescriptor::queryInterface( const uno::Type & rType )
246 throw( uno::RuntimeException )
248 return OWeakAggObject::queryInterface( rType );
251 // ------------------------------------------------------------------------------
253 void SAL_CALL GraphicDescriptor::acquire()
254 throw()
256 OWeakAggObject::acquire();
259 // ------------------------------------------------------------------------------
261 void SAL_CALL GraphicDescriptor::release()
262 throw()
264 OWeakAggObject::release();
267 // ------------------------------------------------------------------------------
269 ::rtl::OUString SAL_CALL GraphicDescriptor::getImplementationName()
270 throw( uno::RuntimeException )
272 return getImplementationName_Static();
275 // ------------------------------------------------------------------------------
277 sal_Bool SAL_CALL GraphicDescriptor::supportsService( const rtl::OUString& ServiceName )
278 throw( uno::RuntimeException )
280 uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
281 const ::rtl::OUString* pArray = aSNL.getConstArray();
283 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
284 if( pArray[i] == ServiceName )
285 return true;
287 return false;
290 // ------------------------------------------------------------------------------
292 uno::Sequence< rtl::OUString > SAL_CALL GraphicDescriptor::getSupportedServiceNames()
293 throw( uno::RuntimeException )
295 return getSupportedServiceNames_Static();
298 // ------------------------------------------------------------------------------
300 uno::Sequence< uno::Type > SAL_CALL GraphicDescriptor::getTypes()
301 throw( uno::RuntimeException )
303 uno::Sequence< uno::Type > aTypes( 6 );
304 uno::Type* pTypes = aTypes.getArray();
306 *pTypes++ = ::getCppuType((const uno::Reference< uno::XAggregation>*)0);
307 *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
308 *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
309 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
310 *pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
311 *pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
313 return aTypes;
316 // ------------------------------------------------------------------------------
318 uno::Sequence< sal_Int8 > SAL_CALL GraphicDescriptor::getImplementationId()
319 throw( uno::RuntimeException )
321 vos::OGuard aGuard( Application::GetSolarMutex() );
322 static uno::Sequence< sal_Int8 > aId;
324 if( aId.getLength() == 0 )
326 aId.realloc( 16 );
327 rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
330 return aId;
333 // ------------------------------------------------------------------------------
335 ::comphelper::PropertySetInfo* GraphicDescriptor::createPropertySetInfo()
337 vos::OGuard aGuard( Application::GetSolarMutex() );
338 ::comphelper::PropertySetInfo* pRet = new ::comphelper::PropertySetInfo();
340 static ::comphelper::PropertyMapEntry aEntries[] =
342 { MAP_CHAR_LEN( "GraphicType" ), UNOGRAPHIC_GRAPHICTYPE, &::getCppuType( (const sal_Int8*)(0)), beans::PropertyAttribute::READONLY, 0 },
343 { MAP_CHAR_LEN( "MimeType" ), UNOGRAPHIC_MIMETYPE, &::getCppuType( (const ::rtl::OUString*)(0)), beans::PropertyAttribute::READONLY, 0 },
344 { MAP_CHAR_LEN( "SizePixel" ), UNOGRAPHIC_SIZEPIXEL, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
345 { MAP_CHAR_LEN( "Size100thMM" ), UNOGRAPHIC_SIZE100THMM, &::getCppuType( (const awt::Size*)(0)), beans::PropertyAttribute::READONLY, 0 },
346 { MAP_CHAR_LEN( "BitsPerPixel" ), UNOGRAPHIC_BITSPERPIXEL, &::getCppuType( (const sal_uInt8*)(0)), beans::PropertyAttribute::READONLY, 0 },
347 { MAP_CHAR_LEN( "Transparent" ), UNOGRAPHIC_TRANSPARENT, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
348 { MAP_CHAR_LEN( "Alpha" ), UNOGRAPHIC_ALPHA, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
349 { MAP_CHAR_LEN( "Animated" ), UNOGRAPHIC_ANIMATED, &::getCppuType( (const sal_Bool*)(0)), beans::PropertyAttribute::READONLY, 0 },
351 { 0,0,0,0,0,0 }
354 pRet->acquire();
355 pRet->add( aEntries );
357 return pRet;
360 // ------------------------------------------------------------------------------
362 void GraphicDescriptor::_setPropertyValues( const comphelper::PropertyMapEntry** /*ppEntries*/, const uno::Any* /*pValues*/ )
363 throw( beans::UnknownPropertyException,
364 beans::PropertyVetoException,
365 lang::IllegalArgumentException,
366 lang::WrappedTargetException )
368 // we only have readonly attributes
371 // ------------------------------------------------------------------------------
373 void GraphicDescriptor::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValues )
374 throw( beans::UnknownPropertyException, lang::WrappedTargetException )
376 ::vos::OGuard aGuard( Application::GetSolarMutex() );
378 while( *ppEntries )
380 switch( (*ppEntries)->mnHandle )
382 case( UNOGRAPHIC_GRAPHICTYPE ):
384 const GraphicType eType( mpGraphic ? mpGraphic->GetType() : meType );
386 *pValues <<= ( ( eType == GRAPHIC_BITMAP ? graphic::GraphicType::PIXEL :
387 ( eType == GRAPHIC_GDIMETAFILE ? graphic::GraphicType::VECTOR :
388 graphic::GraphicType::EMPTY ) ) );
390 break;
392 case( UNOGRAPHIC_MIMETYPE ):
394 ::rtl::OUString aMimeType;
396 if( mpGraphic )
398 if( mpGraphic->IsLink() )
400 const char* pMimeType;
402 switch( const_cast< Graphic* >( mpGraphic )->GetLink().GetType() )
404 case( GFX_LINK_TYPE_NATIVE_GIF ): pMimeType = MIMETYPE_GIF; break;
405 case( GFX_LINK_TYPE_NATIVE_JPG ): pMimeType = MIMETYPE_JPG; break;
406 case( GFX_LINK_TYPE_NATIVE_PNG ): pMimeType = MIMETYPE_PNG; break;
407 case( GFX_LINK_TYPE_NATIVE_WMF ): pMimeType = MIMETYPE_WMF; break;
408 case( GFX_LINK_TYPE_NATIVE_MET ): pMimeType = MIMETYPE_MET; break;
409 case( GFX_LINK_TYPE_NATIVE_PCT ): pMimeType = MIMETYPE_PCT ; break;
411 default:
412 pMimeType = NULL;
413 break;
416 if( pMimeType )
417 aMimeType = ::rtl::OUString::createFromAscii( pMimeType );
420 if( !aMimeType.getLength() && ( mpGraphic->GetType() != GRAPHIC_NONE ) )
421 aMimeType = ::rtl::OUString::createFromAscii( MIMETYPE_VCLGRAPHIC );
423 else
424 aMimeType = maMimeType;
426 *pValues <<= aMimeType;
428 break;
430 case( UNOGRAPHIC_SIZEPIXEL ):
432 awt::Size aAWTSize( 0, 0 );
434 if( mpGraphic )
436 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
438 const Size aSizePix( mpGraphic->GetBitmapEx().GetSizePixel() );
439 aAWTSize = awt::Size( aSizePix.Width(), aSizePix.Height() );
442 else
443 aAWTSize = awt::Size( maSizePixel.Width(), maSizePixel.Height() );
445 *pValues <<= aAWTSize;
447 break;
449 case( UNOGRAPHIC_SIZE100THMM ):
451 awt::Size aAWTSize( 0, 0 );
453 if( mpGraphic )
455 if( mpGraphic->GetPrefMapMode().GetMapUnit() != MAP_PIXEL )
457 const Size aSizeLog( OutputDevice::LogicToLogic( mpGraphic->GetPrefSize(), mpGraphic->GetPrefMapMode(), MAP_100TH_MM ) );
458 aAWTSize = awt::Size( aSizeLog.Width(), aSizeLog.Height() );
461 else
462 aAWTSize = awt::Size( maSize100thMM.Width(), maSize100thMM.Height() );
464 *pValues <<= aAWTSize;
466 break;
468 case( UNOGRAPHIC_BITSPERPIXEL ):
470 USHORT nBitsPerPixel = 0;
472 if( mpGraphic )
474 if( mpGraphic->GetType() == GRAPHIC_BITMAP )
475 nBitsPerPixel = mpGraphic->GetBitmapEx().GetBitmap().GetBitCount();
477 else
478 nBitsPerPixel = mnBitsPerPixel;
480 *pValues <<= sal::static_int_cast< sal_Int8 >(nBitsPerPixel);
482 break;
484 case( UNOGRAPHIC_TRANSPARENT ):
486 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsTransparent() : mbTransparent );
488 break;
490 case( UNOGRAPHIC_ALPHA ):
492 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAlpha() : mbAlpha );
494 break;
496 case( UNOGRAPHIC_ANIMATED ):
498 *pValues <<= static_cast< sal_Bool >( mpGraphic ? mpGraphic->IsAnimated() : mbAnimated );
500 break;
503 ++ppEntries;
504 ++pValues;