Bump version to 6.4-15
[LibreOffice.git] / svx / source / unogallery / unogaltheme.cxx
blobef6d7c83c4092df001bb8b7f24addbda05a787d2
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 <vcl/svapp.hxx>
32 #include <unotools/pathoptions.hxx>
33 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
34 #include <cppuhelper/supportsservice.hxx>
36 using namespace ::com::sun::star;
38 namespace unogallery {
41 GalleryTheme::GalleryTheme( const OUString& rThemeName )
43 mpGallery = ::Gallery::GetGalleryInstance();
44 mpTheme = ( mpGallery ? mpGallery->AcquireTheme( rThemeName, *this ) : nullptr );
46 if( mpGallery )
47 StartListening( *mpGallery );
51 GalleryTheme::~GalleryTheme()
53 const SolarMutexGuard aGuard;
55 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
57 implReleaseItems( nullptr );
59 if( mpGallery )
61 EndListening( *mpGallery );
63 if( mpTheme )
64 mpGallery->ReleaseTheme( mpTheme, *this );
69 OUString SAL_CALL GalleryTheme::getImplementationName()
71 return "com.sun.star.comp.gallery.GalleryTheme";
74 sal_Bool SAL_CALL GalleryTheme::supportsService( const OUString& ServiceName )
76 return cppu::supportsService( this, ServiceName );
79 uno::Sequence< OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
81 return { "com.sun.star.gallery.GalleryTheme" };
84 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
86 static const uno::Sequence aTypes {
87 cppu::UnoType<lang::XServiceInfo>::get(),
88 cppu::UnoType<lang::XTypeProvider>::get(),
89 cppu::UnoType<container::XElementAccess>::get(),
90 cppu::UnoType<container::XIndexAccess>::get(),
91 cppu::UnoType<gallery::XGalleryTheme>::get(),
93 return aTypes;
96 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
98 return css::uno::Sequence<sal_Int8>();
102 uno::Type SAL_CALL GalleryTheme::getElementType()
104 return cppu::UnoType<gallery::XGalleryItem>::get();
108 sal_Bool SAL_CALL GalleryTheme::hasElements()
110 const SolarMutexGuard aGuard;
112 return( ( mpTheme != nullptr ) && ( mpTheme->GetObjectCount() > 0 ) );
116 sal_Int32 SAL_CALL GalleryTheme::getCount()
118 const SolarMutexGuard aGuard;
120 return( mpTheme ? mpTheme->GetObjectCount() : 0 );
124 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
126 const SolarMutexGuard aGuard;
127 uno::Any aRet;
129 if( mpTheme )
131 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
133 throw lang::IndexOutOfBoundsException();
135 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
137 if( pObj )
138 aRet <<= uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) );
141 return aRet;
145 OUString SAL_CALL GalleryTheme::getName( )
147 const SolarMutexGuard aGuard;
148 OUString aRet;
150 if( mpTheme )
151 aRet = mpTheme->GetName();
153 return aRet;
157 void SAL_CALL GalleryTheme::update( )
159 const SolarMutexGuard aGuard;
161 if( mpTheme )
163 const Link<const INetURLObject&, void> aDummyLink;
164 mpTheme->Actualize( aDummyLink );
169 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
170 const OUString& rURL, ::sal_Int32 nIndex )
172 const SolarMutexGuard aGuard;
173 sal_Int32 nRet = -1;
175 if( mpTheme )
179 const INetURLObject aURL( rURL );
181 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
183 if( ( aURL.GetProtocol() != INetProtocol::NotValid ) && mpTheme->InsertURL( aURL, nIndex ) )
185 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
187 if( pObj )
188 nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
191 catch( ... )
196 return nRet;
200 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
201 const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
203 const SolarMutexGuard aGuard;
204 sal_Int32 nRet = -1;
206 if( mpTheme )
210 const Graphic aGraphic( rxGraphic );
212 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
214 if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
215 nRet = nIndex;
217 catch( ... )
222 return nRet;
226 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
227 const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
229 const SolarMutexGuard aGuard;
230 sal_Int32 nRet = -1;
232 if( mpTheme )
234 GalleryDrawingModel* pModel = comphelper::getUnoTunnelImplementation<GalleryDrawingModel>( Drawing );
236 if( pModel && dynamic_cast<const FmFormModel*>(pModel->GetDoc()) )
238 // Here we're inserting something that's already a gallery theme drawing
239 nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
241 if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
242 nRet = nIndex;
244 else if (!pModel)
246 // #i80184# Try to do the right thing and make a Gallery drawing out
247 // of an ordinary Drawing if possible.
250 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
251 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_SET_THROW );
252 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
253 SvxDrawPage* pUnoPage = xPage.is() ? comphelper::getUnoTunnelImplementation<SvxDrawPage>( xPage ) : nullptr;
254 SdrModel* pOrigModel = pUnoPage ? &pUnoPage->GetSdrPage()->getSdrModelFromSdrPage() : nullptr;
255 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : nullptr;
257 if (pOrigPage && pOrigModel)
259 FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
260 // Clone to new target SdrModel
261 SdrPage* pNewPage(pOrigPage->CloneSdrPage(*pTmpModel));
262 pTmpModel->InsertPage(pNewPage, 0);
264 uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
265 pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
267 nRet = insertDrawingByIndex( xDrawing, nIndex );
268 return nRet;
271 catch (...)
277 return nRet;
281 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
283 const SolarMutexGuard aGuard;
285 if( mpTheme )
287 if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
288 throw lang::IndexOutOfBoundsException();
289 mpTheme->RemoveObject( nIndex );
294 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
296 const SolarMutexGuard aGuard;
297 const GalleryHint& rGalleryHint = static_cast< const GalleryHint& >( rHint );
299 switch( rGalleryHint.GetType() )
301 case GalleryHintType::CLOSE_THEME:
303 DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
305 implReleaseItems( nullptr );
307 if( mpGallery && mpTheme )
309 mpGallery->ReleaseTheme( mpTheme, *this );
310 mpTheme = nullptr;
313 break;
315 case GalleryHintType::CLOSE_OBJECT:
317 GalleryObject* pObj = static_cast< GalleryObject* >( rGalleryHint.GetData1() );
319 if( pObj )
320 implReleaseItems( pObj );
322 break;
324 default:
325 break;
330 void GalleryTheme::implReleaseItems( GalleryObject const * pObj )
332 const SolarMutexGuard aGuard;
334 for( GalleryItemVector::iterator aIter = maItemVector.begin(); aIter != maItemVector.end(); )
336 if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
338 (*aIter)->implSetInvalid();
339 aIter = maItemVector.erase( aIter );
341 else
342 ++aIter;
347 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
349 const SolarMutexGuard aGuard;
351 maItemVector.push_back( &rItem );
355 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
357 const SolarMutexGuard aGuard;
359 maItemVector.erase(std::remove(maItemVector.begin(), maItemVector.end(), &rItem), maItemVector.end());
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */