1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "unogalitem.hxx"
28 #include "unogaltheme.hxx"
29 #include "svx/galtheme.hxx"
30 #include "svx/galmisc.hxx"
31 #include <svx/fmmodel.hxx>
32 #include <rtl/uuid.h>
33 #include <vos/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/graph.hxx>
36 #include <svl/itemprop.hxx>
37 #include <svl/itempool.hxx>
38 #include "galobj.hxx"
39 
40 #ifndef _COM_SUN_STAR_BEANS_PROPERTYSTATE_HDL_
41 #include <com/sun/star/beans/PropertyState.hpp>
42 #endif
43 #ifndef _COM_SUN_STAR_BEANS_PROPERTYATTRIBUTE_HDL_
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
45 #endif
46 #include <com/sun/star/gallery/GalleryItemType.hpp>
47 
48 #define UNOGALLERY_GALLERYITEMTYPE	1
49 #define UNOGALLERY_URL				2
50 #define UNOGALLERY_TITLE			3
51 #define UNOGALLERY_THUMBNAIL		4
52 #define UNOGALLERY_GRAPHIC			5
53 #define UNOGALLERY_DRAWING			6
54 
55 using namespace ::com::sun::star;
56 
57 namespace unogallery {
58 
59 // -----------------
60 // - GalleryItem -
61 // -----------------
62 
GalleryItem(::unogallery::GalleryTheme & rTheme,const GalleryObject & rObject)63 GalleryItem::GalleryItem( ::unogallery::GalleryTheme& rTheme, const GalleryObject& rObject ) :
64 	::comphelper::PropertySetHelper( createPropertySetInfo() ),
65 	mpTheme( &rTheme ),
66 	mpGalleryObject( &rObject )
67 {
68 	mpTheme->implRegisterGalleryItem( *this );
69 }
70 
71 // ------------------------------------------------------------------------------
72 
~GalleryItem()73 GalleryItem::~GalleryItem()
74 	throw()
75 {
76 	if( mpTheme )
77 		mpTheme->implDeregisterGalleryItem( *this );
78 }
79 
80 // ------------------------------------------------------------------------------
81 
isValid() const82 bool GalleryItem::isValid() const
83 {
84 	return( mpTheme != NULL );
85 }
86 
87 // ------------------------------------------------------------------------------
88 
queryAggregation(const uno::Type & rType)89 uno::Any SAL_CALL GalleryItem::queryAggregation( const uno::Type & rType )
90 	throw( uno::RuntimeException )
91 {
92 	uno::Any aAny;
93 
94 	if( rType == ::getCppuType((const uno::Reference< lang::XServiceInfo >*)0) )
95 		aAny <<= uno::Reference< lang::XServiceInfo >(this);
96 	else if( rType == ::getCppuType((const uno::Reference< lang::XTypeProvider >*)0) )
97 		aAny <<= uno::Reference< lang::XTypeProvider >(this);
98 	else if( rType == ::getCppuType((const uno::Reference< gallery::XGalleryItem >*)0) )
99 		aAny <<= uno::Reference< gallery::XGalleryItem >(this);
100 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertySet >*)0) )
101 		aAny <<= uno::Reference< beans::XPropertySet >(this);
102 	else if( rType == ::getCppuType((const uno::Reference< beans::XPropertyState >*)0) )
103 		aAny <<= uno::Reference< beans::XPropertyState >(this);
104 	else if( rType == ::getCppuType((const uno::Reference< beans::XMultiPropertySet >*)0) )
105 		aAny <<= uno::Reference< beans::XMultiPropertySet >(this);
106 	else
107 		aAny <<= OWeakAggObject::queryAggregation( rType );
108 
109 	return aAny;
110 }
111 
112 // ------------------------------------------------------------------------------
113 
queryInterface(const uno::Type & rType)114 uno::Any SAL_CALL GalleryItem::queryInterface( const uno::Type & rType )
115 	throw( uno::RuntimeException )
116 {
117 	return OWeakAggObject::queryInterface( rType );
118 }
119 
120 // ------------------------------------------------------------------------------
121 
acquire()122 void SAL_CALL GalleryItem::acquire()
123 	throw()
124 {
125 	OWeakAggObject::acquire();
126 }
127 
128 // ------------------------------------------------------------------------------
129 
release()130 void SAL_CALL GalleryItem::release()
131 	throw()
132 {
133 	OWeakAggObject::release();
134 }
135 
136 // ------------------------------------------------------------------------------
137 
getImplementationName_Static()138 ::rtl::OUString GalleryItem::getImplementationName_Static()
139 	throw()
140 {
141 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.gallery.GalleryItem" ) );
142 }
143 
144 // ------------------------------------------------------------------------------
145 
getSupportedServiceNames_Static()146 uno::Sequence< ::rtl::OUString > GalleryItem::getSupportedServiceNames_Static()
147 	throw()
148 {
149 	uno::Sequence< ::rtl::OUString > aSeq( 1 );
150 
151 	aSeq.getArray()[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.gallery.GalleryItem" ) );
152 
153 	return aSeq;
154 }
155 
156 // ------------------------------------------------------------------------------
157 
getImplementationName()158 ::rtl::OUString SAL_CALL GalleryItem::getImplementationName()
159 	throw( uno::RuntimeException )
160 {
161 	return getImplementationName_Static();
162 }
163 
164 // ------------------------------------------------------------------------------
165 
supportsService(const::rtl::OUString & ServiceName)166 sal_Bool SAL_CALL GalleryItem::supportsService( const ::rtl::OUString& ServiceName )
167 	throw( uno::RuntimeException )
168 {
169     uno::Sequence< ::rtl::OUString >	aSNL( getSupportedServiceNames() );
170     const ::rtl::OUString*				pArray = aSNL.getConstArray();
171 
172     for( int i = 0; i < aSNL.getLength(); i++ )
173         if( pArray[i] == ServiceName )
174             return true;
175 
176     return false;
177 }
178 
179 // ------------------------------------------------------------------------------
180 
getSupportedServiceNames()181 uno::Sequence< ::rtl::OUString > SAL_CALL GalleryItem::getSupportedServiceNames()
182 	throw( uno::RuntimeException )
183 {
184 	return getSupportedServiceNames_Static();
185 }
186 
187 // ------------------------------------------------------------------------------
188 
getTypes()189 uno::Sequence< uno::Type > SAL_CALL GalleryItem::getTypes()
190 	throw(uno::RuntimeException)
191 {
192 	uno::Sequence< uno::Type >	aTypes( 6 );
193 	uno::Type* 					pTypes = aTypes.getArray();
194 
195 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
196 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
197 	*pTypes++ = ::getCppuType((const uno::Reference< gallery::XGalleryItem>*)0);
198 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertySet>*)0);
199 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XPropertyState>*)0);
200 	*pTypes++ = ::getCppuType((const uno::Reference< beans::XMultiPropertySet>*)0);
201 
202 	return aTypes;
203 }
204 
205 // ------------------------------------------------------------------------------
206 
getImplementationId()207 uno::Sequence< sal_Int8 > SAL_CALL GalleryItem::getImplementationId()
208 	throw(uno::RuntimeException)
209 {
210 	const vos::OGuard 					aGuard( Application::GetSolarMutex() );
211 	static uno::Sequence< sal_Int8 >	aId;
212 
213 	if( aId.getLength() == 0 )
214 	{
215 		aId.realloc( 16 );
216 		rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
217 	}
218 
219 	return aId;
220 }
221 
222 // ------------------------------------------------------------------------------
223 
getType()224 sal_Int8 SAL_CALL GalleryItem::getType()
225 	throw (uno::RuntimeException)
226 {
227 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
228 	sal_Int8 			nRet = gallery::GalleryItemType::EMPTY;
229 
230 	if( isValid() )
231 	{
232 		switch( implGetObject()->eObjKind )
233 		{
234 			case( SGA_OBJ_SOUND ):
235 			case( SGA_OBJ_VIDEO ):
236 				nRet = gallery::GalleryItemType::MEDIA;
237 			break;
238 
239 			case( SGA_OBJ_SVDRAW ):
240 				nRet = gallery::GalleryItemType::DRAWING;
241 			break;
242 
243 			default:
244 				nRet = gallery::GalleryItemType::GRAPHIC;
245 			break;
246 		}
247 	}
248 
249 	return nRet;
250 }
251 
252 // ------------------------------------------------------------------------------
253 
createPropertySetInfo()254 ::comphelper::PropertySetInfo* GalleryItem::createPropertySetInfo()
255 {
256 	vos::OGuard 					aGuard( Application::GetSolarMutex() );
257 	::comphelper::PropertySetInfo*	pRet = new ::comphelper::PropertySetInfo();
258 
259 	static ::comphelper::PropertyMapEntry aEntries[] =
260 	{
261 		{ MAP_CHAR_LEN( "GalleryItemType" ), UNOGALLERY_GALLERYITEMTYPE, &::getCppuType( (const sal_Int8*)(0)),
262 		  beans::PropertyAttribute::READONLY, 0 },
263 
264 		{ MAP_CHAR_LEN( "URL" ), UNOGALLERY_URL, &::getCppuType( (const ::rtl::OUString*)(0)),
265 		  beans::PropertyAttribute::READONLY, 0 },
266 
267 		{ MAP_CHAR_LEN( "Title" ), UNOGALLERY_TITLE, &::getCppuType( (const ::rtl::OUString*)(0)),
268 		  0, 0 },
269 
270 		{ MAP_CHAR_LEN( "Thumbnail" ), UNOGALLERY_THUMBNAIL, &::getCppuType( (const uno::Reference< graphic::XGraphic >*)(0)),
271 		  beans::PropertyAttribute::READONLY, 0 },
272 
273 		{ MAP_CHAR_LEN( "Graphic" ), UNOGALLERY_GRAPHIC, &::getCppuType( (const uno::Reference< graphic::XGraphic >*)(0)),
274 		  beans::PropertyAttribute::READONLY, 0 },
275 
276 		{ MAP_CHAR_LEN( "Drawing" ), UNOGALLERY_DRAWING, &::getCppuType( (const uno::Reference< lang::XComponent >*)(0) ),
277 		  beans::PropertyAttribute::READONLY, 0 },
278 
279 		{ 0,0,0,0,0,0}
280 	};
281 
282 	pRet->acquire();
283 	pRet->add( aEntries );
284 
285 	return pRet;
286 }
287 
288 // ------------------------------------------------------------------------------
289 
_setPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,const uno::Any * pValues)290 void GalleryItem::_setPropertyValues( const comphelper::PropertyMapEntry** ppEntries, const uno::Any* pValues )
291 	throw( beans::UnknownPropertyException,
292 		   beans::PropertyVetoException,
293 		   lang::IllegalArgumentException,
294 		   lang::WrappedTargetException )
295 {
296 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
297 
298 	while( *ppEntries )
299 	{
300 		if( UNOGALLERY_TITLE == (*ppEntries)->mnHandle )
301 		{
302 			::rtl::OUString aNewTitle;
303 
304 			if( *pValues >>= aNewTitle )
305 			{
306 				::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
307 
308 				if( pGalTheme )
309 				{
310 					SgaObject* pObj = pGalTheme->ImplReadSgaObject( const_cast< GalleryObject* >( implGetObject() ) );
311 
312 					if( pObj )
313 					{
314 						if( ::rtl::OUString( pObj->GetTitle() ) != aNewTitle )
315 						{
316 							pObj->SetTitle( aNewTitle );
317 							pGalTheme->InsertObject( *pObj );
318 						}
319 
320 						delete pObj;
321 					}
322 				}
323 			}
324 			else
325 			{
326 				throw lang::IllegalArgumentException();
327 			}
328 		}
329 
330 		++ppEntries;
331 		++pValues;
332 	}
333 }
334 
335 // ------------------------------------------------------------------------------
336 
_getPropertyValues(const comphelper::PropertyMapEntry ** ppEntries,uno::Any * pValue)337 void GalleryItem::_getPropertyValues( const comphelper::PropertyMapEntry** ppEntries, uno::Any* pValue )
338 	throw( beans::UnknownPropertyException,
339 		   lang::WrappedTargetException )
340 {
341 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
342 
343 	while( *ppEntries )
344 	{
345 		switch( (*ppEntries)->mnHandle )
346 		{
347 			case( UNOGALLERY_GALLERYITEMTYPE ):
348 			{
349 				*pValue <<= sal_Int8( getType() );
350 			}
351 			break;
352 
353 			case( UNOGALLERY_URL ):
354 			{
355 				::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
356 
357 				if( pGalTheme )
358 					*pValue <<= ::rtl::OUString( implGetObject()->aURL.GetMainURL( INetURLObject::NO_DECODE ) );
359 			}
360 			break;
361 
362 			case( UNOGALLERY_TITLE ):
363 			{
364 				::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
365 
366 				if( pGalTheme )
367 				{
368 					SgaObject* pObj = pGalTheme->AcquireObject( pGalTheme->ImplGetGalleryObjectPos( implGetObject() ) );
369 
370 					if( pObj )
371 					{
372 						*pValue <<= ::rtl::OUString( pObj->GetTitle() );
373 						pGalTheme->ReleaseObject( pObj );
374 					}
375 				}
376 			}
377 			break;
378 
379 			case( UNOGALLERY_THUMBNAIL ):
380 			{
381 				::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
382 
383 				if( pGalTheme )
384 				{
385 					SgaObject* pObj = pGalTheme->AcquireObject( pGalTheme->ImplGetGalleryObjectPos( implGetObject() ) );
386 
387 					if( pObj )
388 					{
389 						Graphic aThumbnail;
390 
391 						if( pObj->IsThumbBitmap() )
392 							aThumbnail = pObj->GetThumbBmp();
393 						else
394 							aThumbnail = pObj->GetThumbMtf();
395 
396 						*pValue <<= aThumbnail.GetXGraphic();
397 						pGalTheme->ReleaseObject( pObj );
398 					}
399 				}
400 			}
401 			break;
402 
403 			case( UNOGALLERY_GRAPHIC ):
404 			{
405 				::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
406 				Graphic			aGraphic;
407 
408 				if( pGalTheme && pGalTheme->GetGraphic( pGalTheme->ImplGetGalleryObjectPos( implGetObject() ), aGraphic ) )
409 					*pValue <<= aGraphic.GetXGraphic();
410 			}
411 			break;
412 
413 			case( UNOGALLERY_DRAWING ):
414 			{
415 				if( gallery::GalleryItemType::DRAWING == getType() )
416 				{
417 					::GalleryTheme*	pGalTheme = ( isValid() ? mpTheme->implGetTheme() : NULL );
418 					FmFormModel*	pModel = new FmFormModel;
419 
420 					pModel->GetItemPool().FreezeIdRanges();
421 
422 					if( pGalTheme && pGalTheme->GetModel( pGalTheme->ImplGetGalleryObjectPos( implGetObject() ), *pModel ) )
423 					{
424 						uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pModel ) );
425 
426 						pModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
427 						*pValue <<= xDrawing;
428 					}
429 					else
430 						delete pModel;
431 				}
432 			}
433 			break;
434 		}
435 
436 		++ppEntries;
437 		++pValue;
438 	}
439 }
440 
441 // ------------------------------------------------------------------------------
442 
implGetObject() const443 const ::GalleryObject* GalleryItem::implGetObject() const
444 {
445 	return mpGalleryObject;
446 }
447 
448 // ------------------------------------------------------------------------------
449 
implSetInvalid()450 void GalleryItem::implSetInvalid()
451 {
452 	if( mpTheme )
453 	{
454 		mpTheme = NULL;
455 		mpGalleryObject = NULL;
456 	}
457 }
458 
459 // -----------------------
460 // - GalleryDrawingModel -
461 // -----------------------
462 
GalleryDrawingModel(SdrModel * pDoc)463 GalleryDrawingModel::GalleryDrawingModel( SdrModel* pDoc )
464 	throw() :
465 	SvxUnoDrawingModel( pDoc )
466 {
467 }
468 
469 // -----------------------------------------------------------------------------
470 
~GalleryDrawingModel()471 GalleryDrawingModel::~GalleryDrawingModel()
472 	throw()
473 {
474 	delete GetDoc();
475 }
476 
477 // -----------------------------------------------------------------------------
478 
479 UNO3_GETIMPLEMENTATION_IMPL( GalleryDrawingModel );
480 
481 }
482