update credits
[LibreOffice.git] / svx / source / unogallery / unogaltheme.cxx
blobeb03c477f36167fb8b947db100fed1e7ef246c94
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
22 #include "unogaltheme.hxx"
23 #include "unogalitem.hxx"
24 #include "svx/galtheme.hxx"
25 #include "svx/gallery1.hxx"
26 #include "svx/galmisc.hxx"
27 #include <svx/fmmodel.hxx>
28 #include <svx/svdpage.hxx>
29 #include <svx/unopage.hxx>
30 #include <svl/itempool.hxx>
31 #include <osl/mutex.hxx>
32 #include <vcl/svapp.hxx>
33 #include <unotools/pathoptions.hxx>
34 #include <comphelper/servicehelper.hxx>
36 using namespace ::com::sun::star;
38 namespace unogallery {
40 // -----------------
41 // - GalleryTheme -
42 // -----------------
44 GalleryTheme::GalleryTheme( const OUString& rThemeName )
46 mpGallery = ::Gallery::GetGalleryInstance();
47 mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : NULL );
49 if( mpGallery )
50 StartListening( *mpGallery );
53 // ------------------------------------------------------------------------------
55 GalleryTheme::~GalleryTheme()
57 const SolarMutexGuard aGuard;
59 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
61 implReleaseItems( NULL );
63 if( mpGallery )
65 EndListening( *mpGallery );
67 if( mpTheme )
68 mpGallery->ReleaseTheme( mpTheme, *this );
72 // ------------------------------------------------------------------------------
74 OUString GalleryTheme::getImplementationName_Static()
75 throw()
77 return OUString( "com.sun.star.comp.gallery.GalleryTheme" );
80 // ------------------------------------------------------------------------------
82 uno::Sequence< OUString > GalleryTheme::getSupportedServiceNames_Static()
83 throw()
85 uno::Sequence< OUString > aSeq( 1 );
87 aSeq.getArray()[ 0 ] = OUString( "com.sun.star.gallery.GalleryTheme" );
89 return aSeq;
92 // ------------------------------------------------------------------------------
94 OUString SAL_CALL GalleryTheme::getImplementationName()
95 throw( uno::RuntimeException )
97 return getImplementationName_Static();
100 // ------------------------------------------------------------------------------
102 sal_Bool SAL_CALL GalleryTheme::supportsService( const OUString& ServiceName )
103 throw( uno::RuntimeException )
105 uno::Sequence< OUString > aSNL( getSupportedServiceNames() );
106 const OUString* pArray = aSNL.getConstArray();
108 for( int i = 0; i < aSNL.getLength(); i++ )
109 if( pArray[i] == ServiceName )
110 return true;
112 return false;
115 // ------------------------------------------------------------------------------
117 uno::Sequence< OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
118 throw( uno::RuntimeException )
120 return getSupportedServiceNames_Static();
123 // ------------------------------------------------------------------------------
125 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
126 throw(uno::RuntimeException)
128 uno::Sequence< uno::Type > aTypes( 5 );
129 uno::Type* pTypes = aTypes.getArray();
131 *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
132 *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
133 *pTypes++ = ::getCppuType((const uno::Reference< container::XElementAccess>*)0);
134 *pTypes++ = ::getCppuType((const uno::Reference< container::XIndexAccess>*)0);
135 *pTypes++ = ::getCppuType((const uno::Reference< gallery::XGalleryTheme>*)0);
137 return aTypes;
140 namespace
142 class theGalleryThemeImplementationId : public rtl::Static< UnoTunnelIdInit, theGalleryThemeImplementationId > {};
145 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
146 throw(uno::RuntimeException)
148 return theGalleryThemeImplementationId::get().getSeq();
151 // ------------------------------------------------------------------------------
153 uno::Type SAL_CALL GalleryTheme::getElementType()
154 throw (uno::RuntimeException)
156 return ::getCppuType( (const uno::Reference< gallery::XGalleryItem >*) 0);
159 // ------------------------------------------------------------------------------
161 sal_Bool SAL_CALL GalleryTheme::hasElements()
162 throw (uno::RuntimeException)
164 const SolarMutexGuard aGuard;
166 return( ( mpTheme != NULL ) && ( mpTheme->GetObjectCount() > 0 ) );
169 // ------------------------------------------------------------------------------
171 sal_Int32 SAL_CALL GalleryTheme::getCount()
172 throw (uno::RuntimeException)
174 const SolarMutexGuard aGuard;
176 return( mpTheme ? mpTheme->GetObjectCount() : 0 );
179 // ------------------------------------------------------------------------------
181 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
182 throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
184 const SolarMutexGuard aGuard;
185 uno::Any aRet;
187 if( mpTheme )
189 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
191 throw lang::IndexOutOfBoundsException();
193 else
195 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
197 if( pObj )
198 aRet = uno::makeAny( uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) ) );
202 return aRet;
205 // ------------------------------------------------------------------------------
207 OUString SAL_CALL GalleryTheme::getName( )
208 throw (uno::RuntimeException)
210 const SolarMutexGuard aGuard;
211 OUString aRet;
213 if( mpTheme )
214 aRet = mpTheme->GetName();
216 return aRet;
219 // ------------------------------------------------------------------------------
221 void SAL_CALL GalleryTheme::update( )
222 throw (uno::RuntimeException)
224 const SolarMutexGuard aGuard;
226 if( mpTheme )
228 const Link aDummyLink;
229 mpTheme->Actualize( aDummyLink );
233 // ------------------------------------------------------------------------------
235 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
236 const OUString& rURL, ::sal_Int32 nIndex )
237 throw (lang::WrappedTargetException, uno::RuntimeException)
239 const SolarMutexGuard aGuard;
240 sal_Int32 nRet = -1;
242 if( mpTheme )
246 const INetURLObject aURL( rURL );
248 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
250 if( ( aURL.GetProtocol() != INET_PROT_NOT_VALID ) && mpTheme->InsertURL( aURL, nIndex ) )
252 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
254 if( pObj )
255 nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
258 catch( ... )
263 return nRet;
266 // ------------------------------------------------------------------------------
268 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
269 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
270 throw (lang::WrappedTargetException, uno::RuntimeException)
272 const SolarMutexGuard aGuard;
273 sal_Int32 nRet = -1;
275 if( mpTheme )
279 const Graphic aGraphic( rxGraphic );
281 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
283 if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
284 nRet = nIndex;
286 catch( ... )
291 return nRet;
294 // ------------------------------------------------------------------------------
296 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
297 const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
298 throw (lang::WrappedTargetException, uno::RuntimeException)
300 const SolarMutexGuard aGuard;
301 sal_Int32 nRet = -1;
303 if( mpTheme )
305 GalleryDrawingModel* pModel = GalleryDrawingModel::getImplementation( Drawing );
307 if( pModel && pModel->GetDoc() && pModel->GetDoc()->ISA( FmFormModel ) )
309 // Here we're inserting something that's already a gallery theme drawing
310 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
312 if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
313 nRet = nIndex;
315 else if (!pModel)
317 // #i80184# Try to do the right thing and make a Gallery drawing out
318 // of an ordinary Drawing if possible.
321 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
322 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
323 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
324 SvxDrawPage* pUnoPage = xPage.is() ? SvxDrawPage::getImplementation( xPage ) : NULL;
325 SdrModel* pOrigModel = pUnoPage ? pUnoPage->GetSdrPage()->GetModel() : NULL;
326 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : NULL;
328 if (pOrigPage && pOrigModel)
330 FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
331 SdrPage* pNewPage = pOrigPage->Clone();
332 pTmpModel->InsertPage(pNewPage, 0);
334 uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
335 pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
337 nRet = insertDrawingByIndex( xDrawing, nIndex );
338 return nRet;
341 catch (...)
347 return nRet;
350 // ------------------------------------------------------------------------------
352 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
353 throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
355 const SolarMutexGuard aGuard;
357 if( mpTheme )
359 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
360 throw lang::IndexOutOfBoundsException();
361 else
362 mpTheme->RemoveObject( nIndex );
366 // ------------------------------------------------------------------------------
368 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
370 const SolarMutexGuard aGuard;
371 const GalleryHint& rGalleryHint = static_cast< const GalleryHint& >( rHint );
373 switch( rGalleryHint.GetType() )
375 case( GALLERY_HINT_CLOSE_THEME ):
377 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
379 implReleaseItems( NULL );
381 if( mpGallery && mpTheme )
383 mpGallery->ReleaseTheme( mpTheme, *this );
384 mpTheme = NULL;
387 break;
389 case( GALLERY_HINT_CLOSE_OBJECT ):
391 GalleryObject* pObj = reinterpret_cast< GalleryObject* >( rGalleryHint.GetData1() );
393 if( pObj )
394 implReleaseItems( pObj );
396 break;
398 default:
399 break;
403 // ------------------------------------------------------------------------------
405 void GalleryTheme::implReleaseItems( GalleryObject* pObj )
407 const SolarMutexGuard aGuard;
409 for( GalleryItemList::iterator aIter = maItemList.begin(); aIter != maItemList.end(); )
411 if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
413 (*aIter)->implSetInvalid();
414 aIter = maItemList.erase( aIter );
416 else
417 ++aIter;
421 // ------------------------------------------------------------------------------
423 ::GalleryTheme* GalleryTheme::implGetTheme() const
425 return mpTheme;
428 // ------------------------------------------------------------------------------
430 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
432 const SolarMutexGuard aGuard;
434 // DBG_ASSERT( maItemList.find( &rItem ) == maItemList.end(), "Item already registered" );
435 maItemList.push_back( &rItem );
438 // ------------------------------------------------------------------------------
440 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
442 const SolarMutexGuard aGuard;
444 // DBG_ASSERT( maItemList.find( &rItem ) != maItemList.end(), "Item is not registered" );
445 maItemList.remove( &rItem );
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */