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 <algorithm>
28 
29 #include "unogaltheme.hxx"
30 #include "unogalitem.hxx"
31 #include "svx/galtheme.hxx"
32 #include "svx/gallery1.hxx"
33 #include "svx/galmisc.hxx"
34 #include <svx/fmmodel.hxx>
35 #include <svx/svdpage.hxx>
36 #include <svx/unopage.hxx>
37 #include <svl/itempool.hxx>
38 #include <rtl/uuid.h>
39 #include <vos/mutex.hxx>
40 #ifndef _SV_SVAPP_HXX_
41 #include <vcl/svapp.hxx>
42 #endif
43 #include <unotools/pathoptions.hxx>
44 
45 using namespace ::com::sun::star;
46 
47 namespace unogallery {
48 
49 // -----------------
50 // - GalleryTheme -
51 // -----------------
52 
GalleryTheme(const::rtl::OUString & rThemeName)53 GalleryTheme::GalleryTheme( const ::rtl::OUString& rThemeName )
54 {
55 	mpGallery = ::Gallery::GetGalleryInstance();
56 	mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : NULL );
57 
58 	if( mpGallery )
59 		StartListening( *mpGallery );
60 }
61 
62 // ------------------------------------------------------------------------------
63 
~GalleryTheme()64 GalleryTheme::~GalleryTheme()
65 {
66 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
67 
68 	DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
69 
70 	implReleaseItems( NULL );
71 
72 	if( mpGallery )
73 	{
74 		EndListening( *mpGallery );
75 
76 		if( mpTheme )
77 			mpGallery->ReleaseTheme( mpTheme, *this );
78 	}
79 }
80 
81 // ------------------------------------------------------------------------------
82 
getImplementationName_Static()83 ::rtl::OUString GalleryTheme::getImplementationName_Static()
84 	throw()
85 {
86 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.gallery.GalleryTheme" ) );
87 }
88 
89 // ------------------------------------------------------------------------------
90 
getSupportedServiceNames_Static()91 uno::Sequence< ::rtl::OUString > GalleryTheme::getSupportedServiceNames_Static()
92 	throw()
93 {
94 	uno::Sequence< ::rtl::OUString > aSeq( 1 );
95 
96 	aSeq.getArray()[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.gallery.GalleryTheme" ) );
97 
98 	return aSeq;
99 }
100 
101 // ------------------------------------------------------------------------------
102 
getImplementationName()103 ::rtl::OUString SAL_CALL GalleryTheme::getImplementationName()
104 	throw( uno::RuntimeException )
105 {
106 	return getImplementationName_Static();
107 }
108 
109 // ------------------------------------------------------------------------------
110 
supportsService(const::rtl::OUString & ServiceName)111 sal_Bool SAL_CALL GalleryTheme::supportsService( const ::rtl::OUString& ServiceName )
112 	throw( uno::RuntimeException )
113 {
114     uno::Sequence< ::rtl::OUString >	aSNL( getSupportedServiceNames() );
115     const ::rtl::OUString*				pArray = aSNL.getConstArray();
116 
117     for( int i = 0; i < aSNL.getLength(); i++ )
118         if( pArray[i] == ServiceName )
119             return true;
120 
121     return false;
122 }
123 
124 // ------------------------------------------------------------------------------
125 
getSupportedServiceNames()126 uno::Sequence< ::rtl::OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
127 	throw( uno::RuntimeException )
128 {
129 	return getSupportedServiceNames_Static();
130 }
131 
132 // ------------------------------------------------------------------------------
133 
getTypes()134 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
135 	throw(uno::RuntimeException)
136 {
137 	uno::Sequence< uno::Type >	aTypes( 5 );
138 	uno::Type* 					pTypes = aTypes.getArray();
139 
140 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
141 	*pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
142 	*pTypes++ = ::getCppuType((const uno::Reference< container::XElementAccess>*)0);
143 	*pTypes++ = ::getCppuType((const uno::Reference< container::XIndexAccess>*)0);
144 	*pTypes++ = ::getCppuType((const uno::Reference< gallery::XGalleryTheme>*)0);
145 
146 	return aTypes;
147 }
148 
149 // ------------------------------------------------------------------------------
150 
getImplementationId()151 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
152 	throw(uno::RuntimeException)
153 {
154 	const vos::OGuard 					aGuard( Application::GetSolarMutex() );
155 	static uno::Sequence< sal_Int8 >	aId;
156 
157 	if( aId.getLength() == 0 )
158 	{
159 		aId.realloc( 16 );
160 		rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
161 	}
162 
163 	return aId;
164 }
165 
166 // ------------------------------------------------------------------------------
167 
getElementType()168 uno::Type SAL_CALL GalleryTheme::getElementType()
169 	throw (uno::RuntimeException)
170 {
171 	return ::getCppuType( (const uno::Reference< gallery::XGalleryItem >*) 0);
172 }
173 
174 // ------------------------------------------------------------------------------
175 
hasElements()176 sal_Bool SAL_CALL GalleryTheme::hasElements()
177 	throw (uno::RuntimeException)
178 {
179 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
180 
181 	return( ( mpTheme != NULL ) && ( mpTheme->GetObjectCount() > 0 ) );
182 }
183 
184 // ------------------------------------------------------------------------------
185 
getCount()186 sal_Int32 SAL_CALL GalleryTheme::getCount()
187 	throw (uno::RuntimeException)
188 {
189 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
190 
191 	return( mpTheme ? mpTheme->GetObjectCount() : 0 );
192 }
193 
194 // ------------------------------------------------------------------------------
195 
getByIndex(::sal_Int32 nIndex)196 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
197 	throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
198 {
199 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
200 	uno::Any 			aRet;
201 
202 	if( mpTheme )
203 	{
204 		if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
205 		{
206 			throw lang::IndexOutOfBoundsException();
207 		}
208 		else
209 		{
210 			const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
211 
212 			if( pObj )
213 				aRet = uno::makeAny( uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) ) );
214 		}
215 	}
216 
217 	return aRet;
218 }
219 
220 // ------------------------------------------------------------------------------
221 
getName()222 ::rtl::OUString SAL_CALL GalleryTheme::getName(  )
223 	throw (uno::RuntimeException)
224 {
225 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
226 	::rtl::OUString 	aRet;
227 
228 	if( mpTheme )
229 		aRet = mpTheme->GetName();
230 
231 	return aRet;
232 }
233 
234 // ------------------------------------------------------------------------------
235 
update()236 void SAL_CALL GalleryTheme::update(  )
237 	throw (uno::RuntimeException)
238 {
239 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
240 
241 	if( mpTheme )
242 	{
243 		const Link aDummyLink;
244 		mpTheme->Actualize( aDummyLink );
245 	}
246 }
247 
248 // ------------------------------------------------------------------------------
249 
insertURLByIndex(const::rtl::OUString & rURL,::sal_Int32 nIndex)250 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
251 	const ::rtl::OUString& rURL, ::sal_Int32 nIndex )
252 	throw (lang::WrappedTargetException, uno::RuntimeException)
253 {
254 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
255 	sal_Int32			nRet = -1;
256 
257 	if( mpTheme )
258 	{
259 		try
260 		{
261 			const INetURLObject aURL( rURL );
262 
263 			nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
264 
265 			if( ( aURL.GetProtocol() != INET_PROT_NOT_VALID ) && mpTheme->InsertURL( aURL, nIndex ) )
266 			{
267 				const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
268 
269 				if( pObj )
270 					nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
271 			}
272 		}
273 		catch( ... )
274 		{
275 		}
276 	}
277 
278 	return nRet;
279 }
280 
281 // ------------------------------------------------------------------------------
282 
insertGraphicByIndex(const uno::Reference<graphic::XGraphic> & rxGraphic,sal_Int32 nIndex)283 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
284 	const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
285 	throw (lang::WrappedTargetException, uno::RuntimeException)
286 {
287 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
288 	sal_Int32 			nRet = -1;
289 
290 	if( mpTheme )
291 	{
292 		try
293 		{
294 			const Graphic aGraphic( rxGraphic );
295 
296 			nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
297 
298 			if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
299 				nRet = nIndex;
300 		}
301 		catch( ... )
302 		{
303 		}
304 	}
305 
306 	return nRet;
307 }
308 
309 // ------------------------------------------------------------------------------
310 
insertDrawingByIndex(const uno::Reference<lang::XComponent> & Drawing,sal_Int32 nIndex)311 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
312 	const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
313 	throw (lang::WrappedTargetException, uno::RuntimeException)
314 {
315     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
316     sal_Int32           nRet = -1;
317 
318     if( mpTheme )
319     {
320         GalleryDrawingModel* pModel = GalleryDrawingModel::getImplementation( Drawing );
321 
322         if( pModel && pModel->GetDoc() && pModel->GetDoc()->ISA( FmFormModel ) )
323         {
324             nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
325 
326             if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
327                 nRet = nIndex;
328         }
329         else if (!pModel)
330         {
331             try
332             {
333                 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
334                 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
335                 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
336                 SvxDrawPage* pUnoPage = xPage.is() ? SvxDrawPage::getImplementation( xPage ) : NULL;
337                 SdrModel* pOrigModel = pUnoPage ? pUnoPage->GetSdrPage()->GetModel() : NULL;
338                 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : NULL;
339 
340                 if (pOrigPage && pOrigModel)
341                 {
342                     FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
343                     SdrPage* pNewPage = pOrigPage->Clone();
344                     pTmpModel->InsertPage(pNewPage, 0);
345 
346                     uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
347                     pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
348 
349                     nRet = insertDrawingByIndex( xDrawing, nIndex );
350                     return nRet;
351                 }
352             }
353             catch (...)
354             {
355             }
356         }
357     }
358 
359     return nRet;
360 }
361 
362 // ------------------------------------------------------------------------------
363 
removeByIndex(sal_Int32 nIndex)364 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
365 	throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
366 {
367 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
368 
369 	if( mpTheme )
370 	{
371 		if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
372 			throw lang::IndexOutOfBoundsException();
373 		else
374 			mpTheme->RemoveObject( nIndex );
375 	}
376 }
377 
378 // ------------------------------------------------------------------------------
379 
Notify(SfxBroadcaster &,const SfxHint & rHint)380 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
381 {
382 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
383 	const GalleryHint& 	rGalleryHint = static_cast< const GalleryHint& >( rHint );
384 
385 	switch( rGalleryHint.GetType() )
386 	{
387 		case( GALLERY_HINT_CLOSE_THEME ):
388 		{
389 			DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
390 
391 			implReleaseItems( NULL );
392 
393 			if( mpGallery && mpTheme )
394 			{
395 				mpGallery->ReleaseTheme( mpTheme, *this );
396 				mpTheme = NULL;
397 			}
398 		}
399 		break;
400 
401 		case( GALLERY_HINT_CLOSE_OBJECT ):
402 		{
403 			GalleryObject* pObj = reinterpret_cast< GalleryObject* >( rGalleryHint.GetData1() );
404 
405 			if( pObj )
406 				implReleaseItems( pObj );
407 		}
408 		break;
409 
410 		default:
411 		break;
412 	}
413 }
414 
415 // ------------------------------------------------------------------------------
416 
implReleaseItems(GalleryObject * pObj)417 void GalleryTheme::implReleaseItems( GalleryObject* pObj )
418 {
419 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
420 
421 	for( GalleryItemList::iterator aIter = maItemList.begin(); aIter != maItemList.end();  )
422 	{
423 		if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
424 		{
425 			(*aIter)->implSetInvalid();
426 			aIter = maItemList.erase( aIter );
427 		}
428 		else
429 			++aIter;
430 	}
431 }
432 
433 // ------------------------------------------------------------------------------
434 
implGetTheme() const435 ::GalleryTheme*	GalleryTheme::implGetTheme() const
436 {
437 	return mpTheme;
438 }
439 
440 // ------------------------------------------------------------------------------
441 
implRegisterGalleryItem(::unogallery::GalleryItem & rItem)442 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
443 {
444 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
445 
446 //	DBG_ASSERT( maItemList.find( &rItem ) == maItemList.end(), "Item already registered" );
447 	maItemList.push_back( &rItem );
448 }
449 
450 // ------------------------------------------------------------------------------
451 
implDeregisterGalleryItem(::unogallery::GalleryItem & rItem)452 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
453 {
454 	const ::vos::OGuard aGuard( Application::GetSolarMutex() );
455 
456 //	DBG_ASSERT( maItemList.find( &rItem ) != maItemList.end(), "Item is not registered" );
457 	maItemList.remove( &rItem );
458 }
459 
460 }
461