bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / unogallery / unogaltheme.cxx
blobedbd37fd4c7ec35912146616578bc64e6e6fb2a3
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>
35 #include <cppuhelper/supportsservice.hxx>
37 using namespace ::com::sun::star;
39 namespace unogallery {
42 // - GalleryTheme -
45 GalleryTheme::GalleryTheme( const OUString& rThemeName )
47 mpGallery = ::Gallery::GetGalleryInstance();
48 mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : NULL );
50 if( mpGallery )
51 StartListening( *mpGallery );
56 GalleryTheme::~GalleryTheme()
58 const SolarMutexGuard aGuard;
60 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
62 implReleaseItems( NULL );
64 if( mpGallery )
66 EndListening( *mpGallery );
68 if( mpTheme )
69 mpGallery->ReleaseTheme( mpTheme, *this );
75 OUString GalleryTheme::getImplementationName_Static()
76 throw()
78 return OUString( "com.sun.star.comp.gallery.GalleryTheme" );
83 uno::Sequence< OUString > GalleryTheme::getSupportedServiceNames_Static()
84 throw()
86 uno::Sequence< OUString > aSeq( 1 );
88 aSeq.getArray()[ 0 ] = "com.sun.star.gallery.GalleryTheme";
90 return aSeq;
93 OUString SAL_CALL GalleryTheme::getImplementationName()
94 throw( uno::RuntimeException, std::exception )
96 return getImplementationName_Static();
99 sal_Bool SAL_CALL GalleryTheme::supportsService( const OUString& ServiceName )
100 throw( uno::RuntimeException, std::exception )
102 return cppu::supportsService( this, ServiceName );
105 uno::Sequence< OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
106 throw( uno::RuntimeException, std::exception )
108 return getSupportedServiceNames_Static();
111 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
112 throw(uno::RuntimeException, std::exception)
114 uno::Sequence< uno::Type > aTypes( 5 );
115 uno::Type* pTypes = aTypes.getArray();
117 *pTypes++ = cppu::UnoType<lang::XServiceInfo>::get();
118 *pTypes++ = cppu::UnoType<lang::XTypeProvider>::get();
119 *pTypes++ = cppu::UnoType<container::XElementAccess>::get();
120 *pTypes++ = cppu::UnoType<container::XIndexAccess>::get();
121 *pTypes++ = cppu::UnoType<gallery::XGalleryTheme>::get();
123 return aTypes;
126 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
127 throw(uno::RuntimeException, std::exception)
129 return css::uno::Sequence<sal_Int8>();
134 uno::Type SAL_CALL GalleryTheme::getElementType()
135 throw (uno::RuntimeException, std::exception)
137 return cppu::UnoType<gallery::XGalleryItem>::get();
142 sal_Bool SAL_CALL GalleryTheme::hasElements()
143 throw (uno::RuntimeException, std::exception)
145 const SolarMutexGuard aGuard;
147 return( ( mpTheme != NULL ) && ( mpTheme->GetObjectCount() > 0 ) );
152 sal_Int32 SAL_CALL GalleryTheme::getCount()
153 throw (uno::RuntimeException, std::exception)
155 const SolarMutexGuard aGuard;
157 return( mpTheme ? mpTheme->GetObjectCount() : 0 );
162 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
163 throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception)
165 const SolarMutexGuard aGuard;
166 uno::Any aRet;
168 if( mpTheme )
170 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
172 throw lang::IndexOutOfBoundsException();
174 else
176 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
178 if( pObj )
179 aRet = uno::makeAny( uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) ) );
183 return aRet;
188 OUString SAL_CALL GalleryTheme::getName( )
189 throw (uno::RuntimeException, std::exception)
191 const SolarMutexGuard aGuard;
192 OUString aRet;
194 if( mpTheme )
195 aRet = mpTheme->GetName();
197 return aRet;
202 void SAL_CALL GalleryTheme::update( )
203 throw (uno::RuntimeException, std::exception)
205 const SolarMutexGuard aGuard;
207 if( mpTheme )
209 const Link<> aDummyLink;
210 mpTheme->Actualize( aDummyLink );
216 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
217 const OUString& rURL, ::sal_Int32 nIndex )
218 throw (lang::WrappedTargetException, uno::RuntimeException, std::exception)
220 const SolarMutexGuard aGuard;
221 sal_Int32 nRet = -1;
223 if( mpTheme )
227 const INetURLObject aURL( rURL );
229 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
231 if( ( aURL.GetProtocol() != INetProtocol::NotValid ) && mpTheme->InsertURL( aURL, nIndex ) )
233 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
235 if( pObj )
236 nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
239 catch( ... )
244 return nRet;
249 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
250 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
251 throw (lang::WrappedTargetException, uno::RuntimeException, std::exception)
253 const SolarMutexGuard aGuard;
254 sal_Int32 nRet = -1;
256 if( mpTheme )
260 const Graphic aGraphic( rxGraphic );
262 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
264 if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
265 nRet = nIndex;
267 catch( ... )
272 return nRet;
277 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
278 const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
279 throw (lang::WrappedTargetException, uno::RuntimeException, std::exception)
281 const SolarMutexGuard aGuard;
282 sal_Int32 nRet = -1;
284 if( mpTheme )
286 GalleryDrawingModel* pModel = GalleryDrawingModel::getImplementation( Drawing );
288 if( pModel && pModel->GetDoc() && pModel->GetDoc()->ISA( FmFormModel ) )
290 // Here we're inserting something that's already a gallery theme drawing
291 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
293 if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
294 nRet = nIndex;
296 else if (!pModel)
298 // #i80184# Try to do the right thing and make a Gallery drawing out
299 // of an ordinary Drawing if possible.
302 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
303 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
304 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
305 SvxDrawPage* pUnoPage = xPage.is() ? SvxDrawPage::getImplementation( xPage ) : NULL;
306 SdrModel* pOrigModel = pUnoPage ? pUnoPage->GetSdrPage()->GetModel() : NULL;
307 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : NULL;
309 if (pOrigPage && pOrigModel)
311 FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
312 SdrPage* pNewPage = pOrigPage->Clone();
313 pTmpModel->InsertPage(pNewPage, 0);
315 uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
316 pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
318 nRet = insertDrawingByIndex( xDrawing, nIndex );
319 return nRet;
322 catch (...)
328 return nRet;
333 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
334 throw (lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
336 const SolarMutexGuard aGuard;
338 if( mpTheme )
340 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
341 throw lang::IndexOutOfBoundsException();
342 else
343 mpTheme->RemoveObject( nIndex );
349 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
351 const SolarMutexGuard aGuard;
352 const GalleryHint& rGalleryHint = static_cast< const GalleryHint& >( rHint );
354 switch( rGalleryHint.GetType() )
356 case( GalleryHintType::CLOSE_THEME ):
358 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
360 implReleaseItems( NULL );
362 if( mpGallery && mpTheme )
364 mpGallery->ReleaseTheme( mpTheme, *this );
365 mpTheme = NULL;
368 break;
370 case( GalleryHintType::CLOSE_OBJECT ):
372 GalleryObject* pObj = reinterpret_cast< GalleryObject* >( rGalleryHint.GetData1() );
374 if( pObj )
375 implReleaseItems( pObj );
377 break;
379 default:
380 break;
386 void GalleryTheme::implReleaseItems( GalleryObject* pObj )
388 const SolarMutexGuard aGuard;
390 for( GalleryItemList::iterator aIter = maItemList.begin(); aIter != maItemList.end(); )
392 if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
394 (*aIter)->implSetInvalid();
395 aIter = maItemList.erase( aIter );
397 else
398 ++aIter;
407 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
409 const SolarMutexGuard aGuard;
411 // DBG_ASSERT( maItemList.find( &rItem ) == maItemList.end(), "Item already registered" );
412 maItemList.push_back( &rItem );
417 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
419 const SolarMutexGuard aGuard;
421 // DBG_ASSERT( maItemList.find( &rItem ) != maItemList.end(), "Item is not registered" );
422 maItemList.remove( &rItem );
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */