xref: /trunk/main/svx/source/unogallery/unogaltheme.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
105     return getImplementationName_Static();
106 }
107 
108 // ------------------------------------------------------------------------------
109 
supportsService(const::rtl::OUString & ServiceName)110 sal_Bool SAL_CALL GalleryTheme::supportsService( const ::rtl::OUString& ServiceName )
111 {
112     uno::Sequence< ::rtl::OUString >    aSNL( getSupportedServiceNames() );
113     const ::rtl::OUString*              pArray = aSNL.getConstArray();
114 
115     for( int i = 0; i < aSNL.getLength(); i++ )
116         if( pArray[i] == ServiceName )
117             return true;
118 
119     return false;
120 }
121 
122 // ------------------------------------------------------------------------------
123 
getSupportedServiceNames()124 uno::Sequence< ::rtl::OUString > SAL_CALL GalleryTheme::getSupportedServiceNames()
125 {
126     return getSupportedServiceNames_Static();
127 }
128 
129 // ------------------------------------------------------------------------------
130 
getTypes()131 uno::Sequence< uno::Type > SAL_CALL GalleryTheme::getTypes()
132 {
133     uno::Sequence< uno::Type >  aTypes( 5 );
134     uno::Type*                  pTypes = aTypes.getArray();
135 
136     *pTypes++ = ::getCppuType((const uno::Reference< lang::XServiceInfo>*)0);
137     *pTypes++ = ::getCppuType((const uno::Reference< lang::XTypeProvider>*)0);
138     *pTypes++ = ::getCppuType((const uno::Reference< container::XElementAccess>*)0);
139     *pTypes++ = ::getCppuType((const uno::Reference< container::XIndexAccess>*)0);
140     *pTypes++ = ::getCppuType((const uno::Reference< gallery::XGalleryTheme>*)0);
141 
142     return aTypes;
143 }
144 
145 // ------------------------------------------------------------------------------
146 
getImplementationId()147 uno::Sequence< sal_Int8 > SAL_CALL GalleryTheme::getImplementationId()
148 {
149     const vos::OGuard                   aGuard( Application::GetSolarMutex() );
150     static uno::Sequence< sal_Int8 >    aId;
151 
152     if( aId.getLength() == 0 )
153     {
154         aId.realloc( 16 );
155         rtl_createUuid( reinterpret_cast< sal_uInt8* >( aId.getArray() ), 0, sal_True );
156     }
157 
158     return aId;
159 }
160 
161 // ------------------------------------------------------------------------------
162 
getElementType()163 uno::Type SAL_CALL GalleryTheme::getElementType()
164 {
165     return ::getCppuType( (const uno::Reference< gallery::XGalleryItem >*) 0);
166 }
167 
168 // ------------------------------------------------------------------------------
169 
hasElements()170 sal_Bool SAL_CALL GalleryTheme::hasElements()
171 {
172     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
173 
174     return( ( mpTheme != NULL ) && ( mpTheme->GetObjectCount() > 0 ) );
175 }
176 
177 // ------------------------------------------------------------------------------
178 
getCount()179 sal_Int32 SAL_CALL GalleryTheme::getCount()
180 {
181     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
182 
183     return( mpTheme ? mpTheme->GetObjectCount() : 0 );
184 }
185 
186 // ------------------------------------------------------------------------------
187 
getByIndex(::sal_Int32 nIndex)188 uno::Any SAL_CALL GalleryTheme::getByIndex( ::sal_Int32 nIndex )
189 {
190     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
191     uno::Any            aRet;
192 
193     if( mpTheme )
194     {
195         if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
196         {
197             throw lang::IndexOutOfBoundsException();
198         }
199         else
200         {
201             const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( nIndex );
202 
203             if( pObj )
204                 aRet = uno::makeAny( uno::Reference< gallery::XGalleryItem >( new GalleryItem( *this, *pObj ) ) );
205         }
206     }
207 
208     return aRet;
209 }
210 
211 // ------------------------------------------------------------------------------
212 
getName()213 ::rtl::OUString SAL_CALL GalleryTheme::getName(  )
214 {
215     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
216     ::rtl::OUString     aRet;
217 
218     if( mpTheme )
219         aRet = mpTheme->GetName();
220 
221     return aRet;
222 }
223 
224 // ------------------------------------------------------------------------------
225 
update()226 void SAL_CALL GalleryTheme::update(  )
227 {
228     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
229 
230     if( mpTheme )
231     {
232         const Link aDummyLink;
233         mpTheme->Actualize( aDummyLink );
234     }
235 }
236 
237 // ------------------------------------------------------------------------------
238 
insertURLByIndex(const::rtl::OUString & rURL,::sal_Int32 nIndex)239 ::sal_Int32 SAL_CALL GalleryTheme::insertURLByIndex(
240     const ::rtl::OUString& rURL, ::sal_Int32 nIndex )
241 {
242     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
243     sal_Int32           nRet = -1;
244 
245     if( mpTheme )
246     {
247         try
248         {
249             const INetURLObject aURL( rURL );
250 
251             nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
252 
253             if( ( aURL.GetProtocol() != INET_PROT_NOT_VALID ) && mpTheme->InsertURL( aURL, nIndex ) )
254             {
255                 const GalleryObject* pObj = mpTheme->ImplGetGalleryObject( aURL );
256 
257                 if( pObj )
258                     nRet = mpTheme->ImplGetGalleryObjectPos( pObj );
259             }
260         }
261         catch( ... )
262         {
263         }
264     }
265 
266     return nRet;
267 }
268 
269 // ------------------------------------------------------------------------------
270 
insertGraphicByIndex(const uno::Reference<graphic::XGraphic> & rxGraphic,sal_Int32 nIndex)271 ::sal_Int32 SAL_CALL GalleryTheme::insertGraphicByIndex(
272     const uno::Reference< graphic::XGraphic >& rxGraphic, sal_Int32 nIndex )
273 {
274     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
275     sal_Int32           nRet = -1;
276 
277     if( mpTheme )
278     {
279         try
280         {
281             const Graphic aGraphic( rxGraphic );
282 
283             nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
284 
285             if( mpTheme->InsertGraphic( aGraphic, nIndex ) )
286                 nRet = nIndex;
287         }
288         catch( ... )
289         {
290         }
291     }
292 
293     return nRet;
294 }
295 
296 // ------------------------------------------------------------------------------
297 
insertDrawingByIndex(const uno::Reference<lang::XComponent> & Drawing,sal_Int32 nIndex)298 ::sal_Int32 SAL_CALL GalleryTheme::insertDrawingByIndex(
299     const uno::Reference< lang::XComponent >& Drawing, sal_Int32 nIndex )
300 {
301     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
302     sal_Int32           nRet = -1;
303 
304     if( mpTheme )
305     {
306         GalleryDrawingModel* pModel = GalleryDrawingModel::getImplementation( Drawing );
307 
308         if( pModel && pModel->GetDoc() && pModel->GetDoc()->ISA( FmFormModel ) )
309         {
310             nIndex = ::std::max( ::std::min( nIndex, getCount() ), sal_Int32( 0 ) );
311 
312             if( mpTheme->InsertModel( *static_cast< FmFormModel* >( pModel->GetDoc() ), nIndex ) )
313                 nRet = nIndex;
314         }
315         else if (!pModel)
316         {
317             try
318             {
319                 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSupplier( Drawing, uno::UNO_QUERY_THROW );
320                 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), uno::UNO_QUERY_THROW );
321                 uno::Reference< drawing::XDrawPage > xPage( xDrawPages->getByIndex( 0 ), uno::UNO_QUERY_THROW );
322                 SvxDrawPage* pUnoPage = xPage.is() ? SvxDrawPage::getImplementation( xPage ) : NULL;
323                 SdrModel* pOrigModel = pUnoPage ? pUnoPage->GetSdrPage()->GetModel() : NULL;
324                 SdrPage* pOrigPage = pUnoPage ? pUnoPage->GetSdrPage() : NULL;
325 
326                 if (pOrigPage && pOrigModel)
327                 {
328                     FmFormModel* pTmpModel = new FmFormModel(&pOrigModel->GetItemPool());
329                     SdrPage* pNewPage = pOrigPage->Clone();
330                     pTmpModel->InsertPage(pNewPage, 0);
331 
332                     uno::Reference< lang::XComponent > xDrawing( new GalleryDrawingModel( pTmpModel ) );
333                     pTmpModel->setUnoModel( uno::Reference< uno::XInterface >::query( xDrawing ) );
334 
335                     nRet = insertDrawingByIndex( xDrawing, nIndex );
336                     return nRet;
337                 }
338             }
339             catch (...)
340             {
341             }
342         }
343     }
344 
345     return nRet;
346 }
347 
348 // ------------------------------------------------------------------------------
349 
removeByIndex(sal_Int32 nIndex)350 void SAL_CALL GalleryTheme::removeByIndex( sal_Int32 nIndex )
351 {
352     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
353 
354     if( mpTheme )
355     {
356         if( ( nIndex < 0 ) || ( nIndex >= getCount() ) )
357             throw lang::IndexOutOfBoundsException();
358         else
359             mpTheme->RemoveObject( nIndex );
360     }
361 }
362 
363 // ------------------------------------------------------------------------------
364 
Notify(SfxBroadcaster &,const SfxHint & rHint)365 void GalleryTheme::Notify( SfxBroadcaster&, const SfxHint& rHint )
366 {
367     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
368     const GalleryHint&  rGalleryHint = static_cast< const GalleryHint& >( rHint );
369 
370     switch( rGalleryHint.GetType() )
371     {
372         case( GALLERY_HINT_CLOSE_THEME ):
373         {
374             DBG_ASSERT( !mpTheme || mpGallery, "Theme is living without Gallery" );
375 
376             implReleaseItems( NULL );
377 
378             if( mpGallery && mpTheme )
379             {
380                 mpGallery->ReleaseTheme( mpTheme, *this );
381                 mpTheme = NULL;
382             }
383         }
384         break;
385 
386         case( GALLERY_HINT_CLOSE_OBJECT ):
387         {
388             GalleryObject* pObj = reinterpret_cast< GalleryObject* >( rGalleryHint.GetData1() );
389 
390             if( pObj )
391                 implReleaseItems( pObj );
392         }
393         break;
394 
395         default:
396         break;
397     }
398 }
399 
400 // ------------------------------------------------------------------------------
401 
implReleaseItems(GalleryObject * pObj)402 void GalleryTheme::implReleaseItems( GalleryObject* pObj )
403 {
404     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
405 
406     for( GalleryItemList::iterator aIter = maItemList.begin(); aIter != maItemList.end();  )
407     {
408         if( !pObj || ( (*aIter)->implGetObject() == pObj ) )
409         {
410             (*aIter)->implSetInvalid();
411             aIter = maItemList.erase( aIter );
412         }
413         else
414             ++aIter;
415     }
416 }
417 
418 // ------------------------------------------------------------------------------
419 
implGetTheme() const420 ::GalleryTheme* GalleryTheme::implGetTheme() const
421 {
422     return mpTheme;
423 }
424 
425 // ------------------------------------------------------------------------------
426 
implRegisterGalleryItem(::unogallery::GalleryItem & rItem)427 void GalleryTheme::implRegisterGalleryItem( ::unogallery::GalleryItem& rItem )
428 {
429     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
430 
431 //  DBG_ASSERT( maItemList.find( &rItem ) == maItemList.end(), "Item already registered" );
432     maItemList.push_back( &rItem );
433 }
434 
435 // ------------------------------------------------------------------------------
436 
implDeregisterGalleryItem(::unogallery::GalleryItem & rItem)437 void GalleryTheme::implDeregisterGalleryItem( ::unogallery::GalleryItem& rItem )
438 {
439     const ::vos::OGuard aGuard( Application::GetSolarMutex() );
440 
441 //  DBG_ASSERT( maItemList.find( &rItem ) != maItemList.end(), "Item is not registered" );
442     maItemList.remove( &rItem );
443 }
444 
445 }
446