Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / svx / source / unogallery / unogaltheme.cxx
blob2eb0894430e7a6bbb178faddf0dc31d8431ebc8b
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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
35 #include <comphelper/servicehelper.hxx>
36 #include <cppuhelper/supportsservice.hxx>
38 using namespace ::com::sun::star;
40 namespace unogallery {
43 GalleryTheme::GalleryTheme( const OUString& rThemeName )
45 mpGallery = ::Gallery::GetGalleryInstance();
46 mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : nullptr );
48 if( mpGallery )
49 StartListening( *mpGallery );
53 GalleryTheme::~GalleryTheme()
55 const SolarMutexGuard aGuard;
57 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
59 implReleaseItems( nullptr );
61 if( mpGallery )
63 EndListening( *mpGallery );
65 if( mpTheme )
66 mpGallery->ReleaseTheme( mpTheme, *this );
71 OUString SAL_CALL GalleryTheme::getImplementationName()
73 return OUString( "com.sun.star.comp.gallery.GalleryTheme" );
76 sal_Bool SAL_CALL GalleryTheme::supportsService( const OUString& ServiceName )
78 return cppu::supportsService( this, ServiceName );
81 uno::Sequence< OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
83 return { "com.sun.star.gallery.GalleryTheme" };
86 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
88 uno::Sequence< uno::Type > aTypes( 5 );
89 uno::Type* pTypes = aTypes.getArray();
91 *pTypes++ = cppu::UnoType<lang::XServiceInfo>::get();
92 *pTypes++ = cppu::UnoType<lang::XTypeProvider>::get();
93 *pTypes++ = cppu::UnoType<container::XElementAccess>::get();
94 *pTypes++ = cppu::UnoType<container::XIndexAccess>::get();
95 *pTypes++ = cppu::UnoType<gallery::XGalleryTheme>::get();
97 return aTypes;
100 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
102 return css::uno::Sequence<sal_Int8>();
106 uno::Type SAL_CALL GalleryTheme::getElementType()
108 return cppu::UnoType<gallery::XGalleryItem>::get();
112 sal_Bool SAL_CALL GalleryTheme::hasElements()
114 const SolarMutexGuard aGuard;
116 return( ( mpTheme != nullptr ) && ( mpTheme->GetObjectCount() > 0 ) );
120 sal_Int32 SAL_CALL GalleryTheme::getCount()
122 const SolarMutexGuard aGuard;
124 return( mpTheme ? mpTheme->GetObjectCount() : 0 );
128 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
130 const SolarMutexGuard aGuard;
131 uno::Any aRet;
133 if( mpTheme )
135 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
137 throw lang::IndexOutOfBoundsException();
139 else
141 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
143 if( pObj )
144 aRet <<= uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) );
148 return aRet;
152 OUString SAL_CALL GalleryTheme::getName( )
154 const SolarMutexGuard aGuard;
155 OUString aRet;
157 if( mpTheme )
158 aRet = mpTheme->GetName();
160 return aRet;
164 void SAL_CALL GalleryTheme::update( )
166 const SolarMutexGuard aGuard;
168 if( mpTheme )
170 const Link<const INetURLObject&, void> aDummyLink;
171 mpTheme->Actualize( aDummyLink );
176 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
177 const OUString& rURL, ::sal_Int32 nIndex )
179 const SolarMutexGuard aGuard;
180 sal_Int32 nRet = -1;
182 if( mpTheme )
186 const INetURLObject aURL( rURL );
188 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
190 if( ( aURL.GetProtocol() != INetProtocol::NotValid ) && mpTheme->InsertURL( aURL, nIndex ) )
192 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
194 if( pObj )
195 nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
198 catch( ... )
203 return nRet;
207 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
208 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
210 const SolarMutexGuard aGuard;
211 sal_Int32 nRet = -1;
213 if( mpTheme )
217 const Graphic aGraphic( rxGraphic );
219 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
221 if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
222 nRet = nIndex;
224 catch( ... )
229 return nRet;
233 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
234 const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
236 const SolarMutexGuard aGuard;
237 sal_Int32 nRet = -1;
239 if( mpTheme )
241 GalleryDrawingModel* pModel = GalleryDrawingModel::getImplementation( Drawing );
243 if( pModel && pModel->GetDoc() && dynamic_cast<const FmFormModel*>(pModel->GetDoc()) != nullptr )
245 // Here we're inserting something that's already a gallery theme drawing
246 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
248 if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
249 nRet = nIndex;
251 else if (!pModel)
253 // #i80184# Try to do the right thing and make a Gallery drawing out
254 // of an ordinary Drawing if possible.
257 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
258 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
259 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
260 SvxDrawPage* pUnoPage = xPage.is() ? SvxDrawPage::getImplementation( xPage ) : nullptr;
261 SdrModel* pOrigModel = pUnoPage ? pUnoPage->GetSdrPage()->GetModel() : nullptr;
262 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : nullptr;
264 if (pOrigPage && pOrigModel)
266 FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
267 SdrPage* pNewPage = pOrigPage->Clone();
268 pTmpModel->InsertPage(pNewPage, 0);
270 uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
271 pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
273 nRet = insertDrawingByIndex( xDrawing, nIndex );
274 return nRet;
277 catch (...)
283 return nRet;
287 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
289 const SolarMutexGuard aGuard;
291 if( mpTheme )
293 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
294 throw lang::IndexOutOfBoundsException();
295 else
296 mpTheme->RemoveObject( nIndex );
301 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
303 const SolarMutexGuard aGuard;
304 const GalleryHint& rGalleryHint = static_cast< const GalleryHint& >( rHint );
306 switch( rGalleryHint.GetType() )
308 case( GalleryHintType::CLOSE_THEME ):
310 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
312 implReleaseItems( nullptr );
314 if( mpGallery && mpTheme )
316 mpGallery->ReleaseTheme( mpTheme, *this );
317 mpTheme = nullptr;
320 break;
322 case( GalleryHintType::CLOSE_OBJECT ):
324 GalleryObject* pObj = reinterpret_cast< GalleryObject* >( rGalleryHint.GetData1() );
326 if( pObj )
327 implReleaseItems( pObj );
329 break;
331 default:
332 break;
337 void GalleryTheme::implReleaseItems( GalleryObject* pObj )
339 const SolarMutexGuard aGuard;
341 for( GalleryItemList::iterator aIter = maItemList.begin(); aIter != maItemList.end(); )
343 if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
345 (*aIter)->implSetInvalid();
346 aIter = maItemList.erase( aIter );
348 else
349 ++aIter;
354 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
356 const SolarMutexGuard aGuard;
358 // DBG_ASSERT( maItemList.find( &rItem ) == maItemList.end(), "Item already registered" );
359 maItemList.push_back( &rItem );
363 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
365 const SolarMutexGuard aGuard;
367 // DBG_ASSERT( maItemList.find( &rItem ) != maItemList.end(), "Item is not registered" );
368 maItemList.remove( &rItem );
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */