sw/qa: warning C6011: Dereferencing NULL pointer
[LibreOffice.git] / framework / source / uiconfiguration / imagemanagerimpl.cxx
blob31deab1fc15ce6bfe2d7c29eb13e8d25cf5cf9e6
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 "imagemanagerimpl.hxx"
21 #include <utility>
22 #include <xml/imagesconfiguration.hxx>
23 #include <uiconfiguration/imagetype.hxx>
24 #include <uiconfiguration/graphicnameaccess.hxx>
26 #include <properties.h>
28 #include <com/sun/star/frame/theUICommandDescription.hpp>
29 #include <com/sun/star/ui/ConfigurationEvent.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/lang/IllegalAccessException.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/embed/ElementModes.hpp>
35 #include <com/sun/star/embed/InvalidStorageException.hpp>
36 #include <com/sun/star/embed/StorageWrappedTargetException.hpp>
37 #include <com/sun/star/io/IOException.hpp>
38 #include <com/sun/star/io/XStream.hpp>
39 #include <com/sun/star/ui/ImageType.hpp>
40 #include <vcl/graph.hxx>
41 #include <vcl/svapp.hxx>
42 #include <o3tl/enumrange.hxx>
43 #include <comphelper/sequence.hxx>
44 #include <unotools/ucbstreamhelper.hxx>
45 #include <vcl/filter/PngImageReader.hxx>
46 #include <vcl/filter/PngImageWriter.hxx>
47 #include <memory>
48 #include <unordered_set>
50 using ::com::sun::star::uno::Sequence;
51 using ::com::sun::star::uno::XInterface;
52 using ::com::sun::star::uno::RuntimeException;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::graphic::XGraphic;
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::io;
58 using namespace ::com::sun::star::embed;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::container;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::ui;
63 using namespace ::cppu;
65 const sal_Int16 MAX_IMAGETYPE_VALUE = css::ui::ImageType::SIZE_32;
67 constexpr OUString IMAGE_FOLDER = u"images"_ustr;
68 constexpr OUString BITMAPS_FOLDER = u"Bitmaps"_ustr;
70 constexpr o3tl::enumarray<vcl::ImageType, OUString> IMAGELIST_XML_FILE
72 u"sc_imagelist.xml"_ustr,
73 u"lc_imagelist.xml"_ustr,
74 u"xc_imagelist.xml"_ustr
77 constexpr o3tl::enumarray<vcl::ImageType, OUString> BITMAP_FILE_NAMES
79 u"sc_userimages.png"_ustr,
80 u"lc_userimages.png"_ustr,
81 u"xc_userimages.png"_ustr
84 namespace framework
87 static GlobalImageList* pGlobalImageList = nullptr;
89 static std::mutex& getGlobalImageListMutex()
91 static std::mutex mutex;
92 return mutex;
95 static GlobalImageList* getGlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext )
97 std::unique_lock guard( getGlobalImageListMutex() );
99 if ( pGlobalImageList == nullptr )
100 pGlobalImageList = new GlobalImageList( rxContext );
102 return pGlobalImageList;
105 CmdImageList::CmdImageList( uno::Reference< uno::XComponentContext > rxContext, OUString aModuleIdentifier ) :
106 m_bInitialized(false),
107 m_aModuleIdentifier(std::move( aModuleIdentifier )),
108 m_xContext(std::move( rxContext ))
112 CmdImageList::~CmdImageList()
116 void CmdImageList::initialize()
118 if (m_bInitialized)
119 return;
121 const OUString aCommandImageList(UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDIMAGELIST);
123 Sequence<OUString> aCommandImageSeq;
124 uno::Reference<XNameAccess> xCommandDesc = frame::theUICommandDescription::get(m_xContext);
126 if (!m_aModuleIdentifier.isEmpty())
128 // If we have a module identifier - use to retrieve the command image name list from it.
129 // Otherwise we will use the global command image list
132 xCommandDesc->getByName(m_aModuleIdentifier) >>= xCommandDesc;
133 if (xCommandDesc.is())
134 xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq;
136 catch (const NoSuchElementException&)
138 // Module unknown we will work with an empty command image list!
139 return;
143 if (xCommandDesc.is())
147 xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq;
149 catch (const NoSuchElementException&)
152 catch (const WrappedTargetException&)
157 m_aResolver.registerCommands(aCommandImageSeq);
159 m_bInitialized = true;
163 Image CmdImageList::getImageFromCommandURL(vcl::ImageType nImageType, const OUString& rCommandURL)
165 initialize();
166 return m_aResolver.getImageFromCommandURL(nImageType, rCommandURL);
169 bool CmdImageList::hasImage(vcl::ImageType /*nImageType*/, const OUString& rCommandURL)
171 initialize();
172 return m_aResolver.hasImage(rCommandURL);
175 std::vector<OUString>& CmdImageList::getImageCommandNames()
177 return m_aResolver.getCommandNames();
180 GlobalImageList::GlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext ) :
181 CmdImageList( rxContext, OUString() )
185 GlobalImageList::~GlobalImageList()
187 std::unique_lock guard( getGlobalImageListMutex() );
188 // remove global pointer as we destroy the object now
189 pGlobalImageList = nullptr;
192 Image GlobalImageList::getImageFromCommandURL( vcl::ImageType nImageType, const OUString& rCommandURL )
194 std::unique_lock guard( getGlobalImageListMutex() );
195 return CmdImageList::getImageFromCommandURL( nImageType, rCommandURL );
198 bool GlobalImageList::hasImage( vcl::ImageType nImageType, const OUString& rCommandURL )
200 std::unique_lock guard( getGlobalImageListMutex() );
201 return CmdImageList::hasImage( nImageType, rCommandURL );
204 ::std::vector< OUString >& GlobalImageList::getImageCommandNames()
206 std::unique_lock guard( getGlobalImageListMutex() );
207 return CmdImageList::getImageCommandNames();
210 static bool implts_checkAndScaleGraphic( uno::Reference< XGraphic >& rOutGraphic, const uno::Reference< XGraphic >& rInGraphic, vcl::ImageType nImageType )
212 if ( !rInGraphic.is() )
214 rOutGraphic = uno::Reference<graphic::XGraphic>();
215 return false;
218 static const o3tl::enumarray<vcl::ImageType, Size> BITMAP_SIZE =
220 Size(16, 16), Size(24, 24), Size(32, 32)
223 // Check size and scale it
224 Graphic aImage(rInGraphic);
225 if (BITMAP_SIZE[nImageType] != aImage.GetSizePixel())
227 BitmapEx aBitmap = aImage.GetBitmapEx();
228 aBitmap.Scale(BITMAP_SIZE[nImageType]);
229 aImage = Graphic(aBitmap);
230 rOutGraphic = aImage.GetXGraphic();
232 else
233 rOutGraphic = rInGraphic;
235 return true;
238 static vcl::ImageType implts_convertImageTypeToIndex( sal_Int16 nImageType )
240 if (nImageType & css::ui::ImageType::SIZE_LARGE)
241 return vcl::ImageType::Size26;
242 else if (nImageType & css::ui::ImageType::SIZE_32)
243 return vcl::ImageType::Size32;
244 else
245 return vcl::ImageType::Size16;
248 ImageList* ImageManagerImpl::implts_getUserImageList( vcl::ImageType nImageType )
250 SolarMutexGuard g;
251 if ( !m_pUserImageList[nImageType] )
252 implts_loadUserImages( nImageType, m_xUserImageStorage, m_xUserBitmapsStorage );
254 return m_pUserImageList[nImageType].get();
257 void ImageManagerImpl::implts_initialize()
259 // Initialize the top-level structures with the storage data
260 if ( !m_xUserConfigStorage.is() )
261 return;
263 tools::Long nModes = m_bReadOnly ? ElementModes::READ : ElementModes::READWRITE;
267 m_xUserImageStorage = m_xUserConfigStorage->openStorageElement( IMAGE_FOLDER,
268 nModes );
269 if ( m_xUserImageStorage.is() )
271 m_xUserBitmapsStorage = m_xUserImageStorage->openStorageElement( BITMAPS_FOLDER,
272 nModes );
275 catch ( const css::container::NoSuchElementException& )
278 catch ( const css::embed::InvalidStorageException& )
281 catch ( const css::lang::IllegalArgumentException& )
284 catch ( const css::io::IOException& )
287 catch ( const css::embed::StorageWrappedTargetException& )
292 void ImageManagerImpl::implts_loadUserImages(
293 vcl::ImageType nImageType,
294 const uno::Reference< XStorage >& xUserImageStorage,
295 const uno::Reference< XStorage >& xUserBitmapsStorage )
297 SolarMutexGuard g;
299 if ( xUserImageStorage.is() && xUserBitmapsStorage.is() )
303 uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( IMAGELIST_XML_FILE[nImageType],
304 ElementModes::READ );
305 uno::Reference< XInputStream > xInputStream = xStream->getInputStream();
307 ImageItemDescriptorList aUserImageListInfo;
308 ImagesConfiguration::LoadImages( m_xContext,
309 xInputStream,
310 aUserImageListInfo );
311 if ( !aUserImageListInfo.empty() )
313 sal_Int32 nCount = aUserImageListInfo.size();
314 std::vector< OUString > aUserImagesVector;
315 aUserImagesVector.reserve(nCount);
316 for ( sal_Int32 i=0; i < nCount; i++ )
318 const ImageItemDescriptor& rItem = aUserImageListInfo[i];
319 aUserImagesVector.push_back( rItem.aCommandURL );
322 uno::Reference< XStream > xBitmapStream = xUserBitmapsStorage->openStreamElement(
323 BITMAP_FILE_NAMES[nImageType],
324 ElementModes::READ );
326 if ( xBitmapStream.is() )
328 BitmapEx aUserBitmap;
330 std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream ));
331 vcl::PngImageReader aPngReader( *pSvStream );
332 aUserBitmap = aPngReader.read();
335 // Delete old image list and create a new one from the read bitmap
336 m_pUserImageList[nImageType].reset(new ImageList());
337 m_pUserImageList[nImageType]->InsertFromHorizontalStrip
338 ( aUserBitmap, aUserImagesVector );
339 return;
343 catch ( const css::container::NoSuchElementException& )
346 catch ( const css::embed::InvalidStorageException& )
349 catch ( const css::lang::IllegalArgumentException& )
352 catch ( const css::io::IOException& )
355 catch ( const css::embed::StorageWrappedTargetException& )
360 // Destroy old image list - create a new empty one
361 m_pUserImageList[nImageType].reset(new ImageList);
364 bool ImageManagerImpl::implts_storeUserImages(
365 vcl::ImageType nImageType,
366 const uno::Reference< XStorage >& xUserImageStorage,
367 const uno::Reference< XStorage >& xUserBitmapsStorage )
369 SolarMutexGuard g;
371 if ( !m_bModified )
372 return false;
374 ImageList* pImageList = implts_getUserImageList( nImageType );
375 if ( pImageList->GetImageCount() > 0 )
377 ImageItemDescriptorList aUserImageListInfo;
379 for ( sal_uInt16 i=0; i < pImageList->GetImageCount(); i++ )
381 ImageItemDescriptor aItem;
382 aItem.aCommandURL = pImageList->GetImageName( i );
383 aUserImageListInfo.push_back( aItem );
386 uno::Reference< XTransactedObject > xTransaction;
387 uno::Reference< XOutputStream > xOutputStream;
388 uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( IMAGELIST_XML_FILE[nImageType],
389 ElementModes::WRITE|ElementModes::TRUNCATE );
390 if ( xStream.is() )
392 uno::Reference< XStream > xBitmapStream =
393 xUserBitmapsStorage->openStreamElement( BITMAP_FILE_NAMES[nImageType],
394 ElementModes::WRITE|ElementModes::TRUNCATE );
395 if ( xBitmapStream.is() )
398 std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream ));
399 vcl::PngImageWriter aPngWriter( *pSvStream );
400 auto rBitmap = pImageList->GetAsHorizontalStrip();
401 aPngWriter.write( rBitmap );
404 // Commit user bitmaps storage
405 xTransaction.set( xUserBitmapsStorage, UNO_QUERY );
406 if ( xTransaction.is() )
407 xTransaction->commit();
410 xOutputStream = xStream->getOutputStream();
411 if ( xOutputStream.is() )
412 ImagesConfiguration::StoreImages( m_xContext, xOutputStream, aUserImageListInfo );
414 // Commit user image storage
415 xTransaction.set( xUserImageStorage, UNO_QUERY );
416 if ( xTransaction.is() )
417 xTransaction->commit();
420 return true;
422 else
424 // Remove the streams from the storage, if we have no data. We have to catch
425 // the NoSuchElementException as it can be possible that there is no stream at all!
428 xUserImageStorage->removeElement( IMAGELIST_XML_FILE[nImageType] );
430 catch ( const css::container::NoSuchElementException& )
436 xUserBitmapsStorage->removeElement( BITMAP_FILE_NAMES[nImageType] );
438 catch ( const css::container::NoSuchElementException& )
442 uno::Reference< XTransactedObject > xTransaction;
444 // Commit user image storage
445 xTransaction.set( xUserImageStorage, UNO_QUERY );
446 if ( xTransaction.is() )
447 xTransaction->commit();
449 // Commit user bitmaps storage
450 xTransaction.set( xUserBitmapsStorage, UNO_QUERY );
451 if ( xTransaction.is() )
452 xTransaction->commit();
454 return true;
457 return false;
460 const rtl::Reference< GlobalImageList >& ImageManagerImpl::implts_getGlobalImageList()
462 SolarMutexGuard g;
464 if ( !m_pGlobalImageList.is() )
465 m_pGlobalImageList = getGlobalImageList( m_xContext );
466 return m_pGlobalImageList;
469 CmdImageList* ImageManagerImpl::implts_getDefaultImageList()
471 SolarMutexGuard g;
473 if ( !m_pDefaultImageList )
474 m_pDefaultImageList.reset(new CmdImageList( m_xContext, m_aModuleIdentifier ));
476 return m_pDefaultImageList.get();
479 ImageManagerImpl::ImageManagerImpl( uno::Reference< uno::XComponentContext > xContext, ::cppu::OWeakObject* pOwner, bool _bUseGlobal ) :
480 m_xContext(std::move( xContext ))
481 , m_pOwner(pOwner)
482 , m_aResourceString( u"private:resource/images/moduleimages"_ustr )
483 , m_bUseGlobal(_bUseGlobal)
484 , m_bReadOnly( true )
485 , m_bInitialized( false )
486 , m_bModified( false )
487 , m_bDisposed( false )
489 for ( vcl::ImageType n : o3tl::enumrange<vcl::ImageType>() )
491 m_pUserImageList[n] = nullptr;
492 m_bUserImageListModified[n] = false;
496 ImageManagerImpl::~ImageManagerImpl()
498 clear();
501 void ImageManagerImpl::dispose()
503 uno::Reference< uno::XInterface > xOwner(m_pOwner);
504 css::lang::EventObject aEvent( xOwner );
506 std::unique_lock aGuard(m_mutex);
507 m_aEventListeners.disposeAndClear( aGuard, aEvent );
510 std::unique_lock aGuard(m_mutex);
511 m_aConfigListeners.disposeAndClear( aGuard, aEvent );
515 SolarMutexGuard g;
516 m_xUserConfigStorage.clear();
517 m_xUserImageStorage.clear();
518 m_xUserRootCommit.clear();
519 m_bModified = false;
520 m_bDisposed = true;
522 // delete user and default image list on dispose
523 for (auto& n : m_pUserImageList)
525 n.reset();
527 m_pDefaultImageList.reset();
531 void ImageManagerImpl::addEventListener( const uno::Reference< XEventListener >& xListener )
534 SolarMutexGuard g;
536 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
537 if ( m_bDisposed )
538 throw DisposedException();
541 std::unique_lock aGuard(m_mutex);
542 m_aEventListeners.addInterface( aGuard, xListener );
545 void ImageManagerImpl::removeEventListener( const uno::Reference< XEventListener >& xListener )
547 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
548 std::unique_lock aGuard(m_mutex);
549 m_aEventListeners.removeInterface( aGuard, xListener );
552 // XInitialization
553 void ImageManagerImpl::initialize( const Sequence< Any >& aArguments )
555 SolarMutexGuard g;
557 if ( m_bInitialized )
558 return;
560 for ( const Any& rArg : aArguments )
562 PropertyValue aPropValue;
563 if ( rArg >>= aPropValue )
565 if ( aPropValue.Name == "UserConfigStorage" )
567 aPropValue.Value >>= m_xUserConfigStorage;
569 else if ( aPropValue.Name == "ModuleIdentifier" )
571 aPropValue.Value >>= m_aModuleIdentifier;
573 else if ( aPropValue.Name == "UserRootCommit" )
575 aPropValue.Value >>= m_xUserRootCommit;
580 if ( m_xUserConfigStorage.is() )
582 uno::Reference< XPropertySet > xPropSet( m_xUserConfigStorage, UNO_QUERY );
583 if ( xPropSet.is() )
585 tools::Long nOpenMode = 0;
586 if ( xPropSet->getPropertyValue(u"OpenMode"_ustr) >>= nOpenMode )
587 m_bReadOnly = !( nOpenMode & ElementModes::WRITE );
591 implts_initialize();
593 m_bInitialized = true;
596 // XImageManagerImpl
597 void ImageManagerImpl::reset()
599 SolarMutexGuard g;
601 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
602 if ( m_bDisposed )
603 throw DisposedException();
605 std::vector< OUString > aUserImageNames;
607 for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() )
609 aUserImageNames.clear();
610 ImageList* pImageList = implts_getUserImageList(i);
611 pImageList->GetImageNames( aUserImageNames );
613 Sequence< OUString > aRemoveList( comphelper::containerToSequence(aUserImageNames) );
615 // Remove images
616 removeImages( sal_Int16( i ), aRemoveList );
617 m_bUserImageListModified[i] = true;
620 m_bModified = true;
623 Sequence< OUString > ImageManagerImpl::getAllImageNames( ::sal_Int16 nImageType )
625 SolarMutexGuard g;
627 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
628 if ( m_bDisposed )
629 throw DisposedException();
631 std::unordered_set< OUString > aImageCmdNames;
633 vcl::ImageType nIndex = implts_convertImageTypeToIndex( nImageType );
635 sal_uInt32 i( 0 );
636 if ( m_bUseGlobal )
638 rtl::Reference< GlobalImageList > rGlobalImageList = implts_getGlobalImageList();
640 const std::vector< OUString >& rGlobalImageNameVector = rGlobalImageList->getImageCommandNames();
641 const sal_uInt32 nGlobalCount = rGlobalImageNameVector.size();
642 for ( i = 0; i < nGlobalCount; i++ )
643 aImageCmdNames.insert( rGlobalImageNameVector[i] );
645 const std::vector< OUString >& rModuleImageNameVector = implts_getDefaultImageList()->getImageCommandNames();
646 const sal_uInt32 nModuleCount = rModuleImageNameVector.size();
647 for ( i = 0; i < nModuleCount; i++ )
648 aImageCmdNames.insert( rModuleImageNameVector[i] );
651 ImageList* pImageList = implts_getUserImageList(nIndex);
652 std::vector< OUString > rUserImageNames;
653 pImageList->GetImageNames( rUserImageNames );
654 const sal_uInt32 nUserCount = rUserImageNames.size();
655 for ( i = 0; i < nUserCount; i++ )
656 aImageCmdNames.insert( rUserImageNames[i] );
658 return comphelper::containerToSequence( aImageCmdNames );
661 bool ImageManagerImpl::hasImage( ::sal_Int16 nImageType, const OUString& aCommandURL )
663 SolarMutexGuard g;
665 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
666 if ( m_bDisposed )
667 throw DisposedException();
669 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
670 throw IllegalArgumentException();
672 vcl::ImageType nIndex = implts_convertImageTypeToIndex( nImageType );
673 if ( m_bUseGlobal && implts_getGlobalImageList()->hasImage( nIndex, aCommandURL ))
674 return true;
675 else
677 if ( m_bUseGlobal && implts_getDefaultImageList()->hasImage( nIndex, aCommandURL ))
678 return true;
679 else
681 // User layer
682 ImageList* pImageList = implts_getUserImageList(nIndex);
683 if ( pImageList )
684 return ( pImageList->GetImagePos( aCommandURL ) != IMAGELIST_IMAGE_NOTFOUND );
688 return false;
691 namespace
693 css::uno::Reference< css::graphic::XGraphic > GetXGraphic(const Image &rImage)
695 return Graphic(rImage).GetXGraphic();
699 Sequence< uno::Reference< XGraphic > > ImageManagerImpl::getImages(
700 ::sal_Int16 nImageType,
701 const Sequence< OUString >& aCommandURLSequence )
703 SolarMutexGuard g;
705 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
706 if ( m_bDisposed )
707 throw DisposedException();
709 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
710 throw IllegalArgumentException();
712 Sequence< uno::Reference< XGraphic > > aGraphSeq( aCommandURLSequence.getLength() );
714 vcl::ImageType nIndex = implts_convertImageTypeToIndex( nImageType );
715 rtl::Reference< GlobalImageList > rGlobalImageList;
716 CmdImageList* pDefaultImageList = nullptr;
717 if ( m_bUseGlobal )
719 rGlobalImageList = implts_getGlobalImageList();
720 pDefaultImageList = implts_getDefaultImageList();
722 ImageList* pUserImageList = implts_getUserImageList(nIndex);
724 // We have to search our image list in the following order:
725 // 1. user image list (read/write)
726 // 2. module image list (read)
727 // 3. global image list (read)
728 auto aGraphSeqRange = asNonConstRange(aGraphSeq);
729 sal_Int32 n = 0;
730 for ( const OUString& rURL : aCommandURLSequence )
732 Image aImage = pUserImageList->GetImage( rURL );
733 if ( !aImage && m_bUseGlobal )
735 aImage = pDefaultImageList->getImageFromCommandURL( nIndex, rURL );
736 if ( !aImage )
737 aImage = rGlobalImageList->getImageFromCommandURL( nIndex, rURL );
740 aGraphSeqRange[n++] = GetXGraphic(aImage);
743 return aGraphSeq;
746 void ImageManagerImpl::replaceImages(
747 ::sal_Int16 nImageType,
748 const Sequence< OUString >& aCommandURLSequence,
749 const Sequence< uno::Reference< XGraphic > >& aGraphicsSequence )
751 rtl::Reference<GraphicNameAccess> pInsertedImages;
752 rtl::Reference<GraphicNameAccess> pReplacedImages;
755 SolarMutexGuard g;
757 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
758 if ( m_bDisposed )
759 throw DisposedException();
761 if (( aCommandURLSequence.getLength() != aGraphicsSequence.getLength() ) ||
762 (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE )))
763 throw IllegalArgumentException();
765 if ( m_bReadOnly )
766 throw IllegalAccessException();
768 vcl::ImageType nIndex = implts_convertImageTypeToIndex( nImageType );
769 ImageList* pImageList = implts_getUserImageList(nIndex);
771 uno::Reference< XGraphic > xGraphic;
772 for ( sal_Int32 i = 0; i < aCommandURLSequence.getLength(); i++ )
774 // Check size and scale. If we don't have any graphics ignore it
775 if ( !implts_checkAndScaleGraphic( xGraphic, aGraphicsSequence[i], nIndex ))
776 continue;
778 sal_uInt16 nPos = pImageList->GetImagePos( aCommandURLSequence[i] );
779 if ( nPos == IMAGELIST_IMAGE_NOTFOUND )
781 pImageList->AddImage(aCommandURLSequence[i], Image(xGraphic));
782 if ( !pInsertedImages )
783 pInsertedImages = new GraphicNameAccess();
784 pInsertedImages->addElement( aCommandURLSequence[i], xGraphic );
786 else
788 pImageList->ReplaceImage(aCommandURLSequence[i], Image(xGraphic));
789 if ( !pReplacedImages )
790 pReplacedImages = new GraphicNameAccess();
791 pReplacedImages->addElement( aCommandURLSequence[i], xGraphic );
795 if (( pInsertedImages != nullptr ) || ( pReplacedImages != nullptr ))
797 m_bModified = true;
798 m_bUserImageListModified[nIndex] = true;
802 uno::Reference< uno::XInterface > xOwner(m_pOwner);
803 // Notify listeners
804 if ( pInsertedImages != nullptr )
806 ConfigurationEvent aInsertEvent;
807 aInsertEvent.aInfo <<= nImageType;
808 aInsertEvent.Accessor <<= xOwner;
809 aInsertEvent.Source = xOwner;
810 aInsertEvent.ResourceURL = m_aResourceString;
811 aInsertEvent.Element <<= uno::Reference< XNameAccess >(pInsertedImages);
812 implts_notifyContainerListener( aInsertEvent, NotifyOp_Insert );
814 if ( pReplacedImages != nullptr )
816 ConfigurationEvent aReplaceEvent;
817 aReplaceEvent.aInfo <<= nImageType;
818 aReplaceEvent.Accessor <<= xOwner;
819 aReplaceEvent.Source = xOwner;
820 aReplaceEvent.ResourceURL = m_aResourceString;
821 aReplaceEvent.ReplacedElement = Any();
822 aReplaceEvent.Element <<= uno::Reference< XNameAccess >(pReplacedImages);
823 implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace );
827 void ImageManagerImpl::removeImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence )
829 rtl::Reference<GraphicNameAccess> pRemovedImages;
830 rtl::Reference<GraphicNameAccess> pReplacedImages;
833 SolarMutexGuard g;
835 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
836 if ( m_bDisposed )
837 throw DisposedException();
839 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
840 throw IllegalArgumentException();
842 if ( m_bReadOnly )
843 throw IllegalAccessException();
845 vcl::ImageType nIndex = implts_convertImageTypeToIndex( nImageType );
846 rtl::Reference< GlobalImageList > rGlobalImageList;
847 CmdImageList* pDefaultImageList = nullptr;
848 if ( m_bUseGlobal )
850 rGlobalImageList = implts_getGlobalImageList();
851 pDefaultImageList = implts_getDefaultImageList();
853 ImageList* pImageList = implts_getUserImageList(nIndex);
854 uno::Reference<XGraphic> xEmptyGraphic;
856 for ( const OUString& rURL : aCommandURLSequence )
858 sal_uInt16 nPos = pImageList->GetImagePos( rURL );
859 if ( nPos != IMAGELIST_IMAGE_NOTFOUND )
861 sal_uInt16 nId = pImageList->GetImageId( nPos );
862 pImageList->RemoveImage( nId );
864 if ( m_bUseGlobal )
866 // Check, if we have an image in our module/global image list. If we find one =>
867 // this is a replace instead of a remove operation!
868 Image aNewImage = pDefaultImageList->getImageFromCommandURL( nIndex, rURL );
869 if ( !aNewImage )
870 aNewImage = rGlobalImageList->getImageFromCommandURL( nIndex, rURL );
871 if ( !aNewImage )
873 if ( !pRemovedImages )
874 pRemovedImages = new GraphicNameAccess();
875 pRemovedImages->addElement( rURL, xEmptyGraphic );
877 else
879 if ( !pReplacedImages )
880 pReplacedImages = new GraphicNameAccess();
881 pReplacedImages->addElement(rURL, GetXGraphic(aNewImage));
883 } // if ( m_bUseGlobal )
884 else
886 if ( !pRemovedImages )
887 pRemovedImages = new GraphicNameAccess();
888 pRemovedImages->addElement( rURL, xEmptyGraphic );
893 if (( pReplacedImages != nullptr ) || ( pRemovedImages != nullptr ))
895 m_bModified = true;
896 m_bUserImageListModified[nIndex] = true;
900 // Notify listeners
901 uno::Reference< uno::XInterface > xOwner(m_pOwner);
902 if ( pRemovedImages != nullptr )
904 ConfigurationEvent aRemoveEvent;
905 aRemoveEvent.aInfo <<= nImageType;
906 aRemoveEvent.Accessor <<= xOwner;
907 aRemoveEvent.Source = xOwner;
908 aRemoveEvent.ResourceURL = m_aResourceString;
909 aRemoveEvent.Element <<= uno::Reference< XNameAccess >(pRemovedImages);
910 implts_notifyContainerListener( aRemoveEvent, NotifyOp_Remove );
912 if ( pReplacedImages != nullptr )
914 ConfigurationEvent aReplaceEvent;
915 aReplaceEvent.aInfo <<= nImageType;
916 aReplaceEvent.Accessor <<= xOwner;
917 aReplaceEvent.Source = xOwner;
918 aReplaceEvent.ResourceURL = m_aResourceString;
919 aReplaceEvent.ReplacedElement = Any();
920 aReplaceEvent.Element <<= uno::Reference< XNameAccess >(pReplacedImages);
921 implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace );
925 void ImageManagerImpl::insertImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence, const Sequence< uno::Reference< XGraphic > >& aGraphicSequence )
927 replaceImages(nImageType,aCommandURLSequence,aGraphicSequence);
930 // XUIConfigurationPersistence
931 void ImageManagerImpl::reload()
933 SolarMutexResettableGuard aGuard;
935 if ( m_bDisposed )
936 throw DisposedException();
938 CommandMap aOldUserCmdImageSet;
939 std::vector< OUString > aNewUserCmdImageSet;
941 if ( !m_bModified )
942 return;
944 for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() )
946 if ( !m_bDisposed && m_bUserImageListModified[i] )
948 std::vector< OUString > aOldUserCmdImageVector;
949 ImageList* pImageList = implts_getUserImageList(i);
950 pImageList->GetImageNames( aOldUserCmdImageVector );
952 // Fill hash map to speed up search afterwards
953 sal_uInt32 j( 0 );
954 const sal_uInt32 nOldCount = aOldUserCmdImageVector.size();
955 for ( j = 0; j < nOldCount; j++ )
956 aOldUserCmdImageSet.emplace( aOldUserCmdImageVector[j], false );
958 // Attention: This can make the old image list pointer invalid!
959 implts_loadUserImages( i, m_xUserImageStorage, m_xUserBitmapsStorage );
960 pImageList = implts_getUserImageList(i);
961 pImageList->GetImageNames( aNewUserCmdImageSet );
963 rtl::Reference<GraphicNameAccess> pInsertedImages;
964 rtl::Reference<GraphicNameAccess> pReplacedImages;
965 rtl::Reference<GraphicNameAccess> pRemovedImages;
967 for (auto const& newUserCmdImage : aNewUserCmdImageSet)
969 CommandMap::iterator pIter = aOldUserCmdImageSet.find(newUserCmdImage);
970 if ( pIter != aOldUserCmdImageSet.end() )
972 pIter->second = true; // mark entry as replaced
973 if ( !pReplacedImages )
974 pReplacedImages = new GraphicNameAccess();
975 pReplacedImages->addElement( newUserCmdImage,
976 GetXGraphic(pImageList->GetImage(newUserCmdImage)) );
978 else
980 if ( !pInsertedImages )
981 pInsertedImages = new GraphicNameAccess();
982 pInsertedImages->addElement( newUserCmdImage,
983 GetXGraphic(pImageList->GetImage(newUserCmdImage)) );
987 // Search map for unmarked entries => they have been removed from the user list
988 // through this reload operation.
989 // We have to search the module and global image list!
990 rtl::Reference< GlobalImageList > rGlobalImageList;
991 CmdImageList* pDefaultImageList = nullptr;
992 if ( m_bUseGlobal )
994 rGlobalImageList = implts_getGlobalImageList();
995 pDefaultImageList = implts_getDefaultImageList();
997 uno::Reference<XGraphic> xEmptyGraphic;
998 for (auto const& oldUserCmdImage : aOldUserCmdImageSet)
1000 if ( !oldUserCmdImage.second )
1002 if ( m_bUseGlobal )
1004 Image aImage = pDefaultImageList->getImageFromCommandURL( i, oldUserCmdImage.first );
1005 if ( !aImage )
1006 aImage = rGlobalImageList->getImageFromCommandURL( i, oldUserCmdImage.first );
1008 if ( !aImage )
1010 // No image in the module/global image list => remove user image
1011 if ( !pRemovedImages )
1012 pRemovedImages = new GraphicNameAccess();
1013 pRemovedImages->addElement( oldUserCmdImage.first, xEmptyGraphic );
1015 else
1017 // Image has been found in the module/global image list => replace user image
1018 if ( !pReplacedImages )
1019 pReplacedImages = new GraphicNameAccess();
1020 pReplacedImages->addElement(oldUserCmdImage.first, GetXGraphic(aImage));
1022 } // if ( m_bUseGlobal )
1023 else
1025 // No image in the user image list => remove user image
1026 if ( !pRemovedImages )
1027 pRemovedImages = new GraphicNameAccess();
1028 pRemovedImages->addElement( oldUserCmdImage.first, xEmptyGraphic );
1033 aGuard.clear();
1035 // Now notify our listeners. Unlock mutex to prevent deadlocks
1036 uno::Reference< uno::XInterface > xOwner(m_pOwner);
1037 if ( pInsertedImages != nullptr )
1039 ConfigurationEvent aInsertEvent;
1040 aInsertEvent.aInfo <<=static_cast<sal_uInt16>(i);
1041 aInsertEvent.Accessor <<= xOwner;
1042 aInsertEvent.Source = xOwner;
1043 aInsertEvent.ResourceURL = m_aResourceString;
1044 aInsertEvent.Element <<= uno::Reference< XNameAccess >( pInsertedImages );
1045 implts_notifyContainerListener( aInsertEvent, NotifyOp_Insert );
1047 if ( pReplacedImages != nullptr )
1049 ConfigurationEvent aReplaceEvent;
1050 aReplaceEvent.aInfo <<= static_cast<sal_uInt16>(i);
1051 aReplaceEvent.Accessor <<= xOwner;
1052 aReplaceEvent.Source = xOwner;
1053 aReplaceEvent.ResourceURL = m_aResourceString;
1054 aReplaceEvent.ReplacedElement = Any();
1055 aReplaceEvent.Element <<= uno::Reference< XNameAccess >( pReplacedImages );
1056 implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace );
1058 if ( pRemovedImages != nullptr )
1060 ConfigurationEvent aRemoveEvent;
1061 aRemoveEvent.aInfo <<= static_cast<sal_uInt16>(i);
1062 aRemoveEvent.Accessor <<= xOwner;
1063 aRemoveEvent.Source = xOwner;
1064 aRemoveEvent.ResourceURL = m_aResourceString;
1065 aRemoveEvent.Element <<= uno::Reference< XNameAccess >( pRemovedImages );
1066 implts_notifyContainerListener( aRemoveEvent, NotifyOp_Remove );
1069 aGuard.reset();
1074 void ImageManagerImpl::store()
1076 SolarMutexGuard g;
1078 if ( m_bDisposed )
1079 throw DisposedException();
1081 if ( !m_bModified )
1082 return;
1084 bool bWritten( false );
1085 for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() )
1087 bool bSuccess = implts_storeUserImages(i, m_xUserImageStorage, m_xUserBitmapsStorage );
1088 if ( bSuccess )
1089 bWritten = true;
1090 m_bUserImageListModified[i] = false;
1093 if ( bWritten &&
1094 m_xUserConfigStorage.is() )
1096 uno::Reference< XTransactedObject > xUserConfigStorageCommit( m_xUserConfigStorage, UNO_QUERY );
1097 if ( xUserConfigStorageCommit.is() )
1098 xUserConfigStorageCommit->commit();
1099 if ( m_xUserRootCommit.is() )
1100 m_xUserRootCommit->commit();
1103 m_bModified = false;
1106 void ImageManagerImpl::storeToStorage( const uno::Reference< XStorage >& Storage )
1108 SolarMutexGuard g;
1110 if ( m_bDisposed )
1111 throw DisposedException();
1113 if ( !(m_bModified && Storage.is()) )
1114 return;
1116 tools::Long nModes = ElementModes::READWRITE;
1118 uno::Reference< XStorage > xUserImageStorage = Storage->openStorageElement( IMAGE_FOLDER,
1119 nModes );
1120 if ( !xUserImageStorage.is() )
1121 return;
1123 uno::Reference< XStorage > xUserBitmapsStorage = xUserImageStorage->openStorageElement( BITMAPS_FOLDER,
1124 nModes );
1125 for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() )
1127 implts_getUserImageList(i);
1128 implts_storeUserImages( i, xUserImageStorage, xUserBitmapsStorage );
1131 uno::Reference< XTransactedObject > xTransaction( Storage, UNO_QUERY );
1132 if ( xTransaction.is() )
1133 xTransaction->commit();
1136 bool ImageManagerImpl::isModified() const
1138 SolarMutexGuard g;
1139 return m_bModified;
1142 bool ImageManagerImpl::isReadOnly() const
1144 SolarMutexGuard g;
1145 return m_bReadOnly;
1147 // XUIConfiguration
1148 void ImageManagerImpl::addConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener )
1151 SolarMutexGuard g;
1153 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
1154 if ( m_bDisposed )
1155 throw DisposedException();
1158 std::unique_lock aGuard(m_mutex);
1159 m_aConfigListeners.addInterface( aGuard, xListener );
1162 void ImageManagerImpl::removeConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener )
1164 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
1165 std::unique_lock aGuard(m_mutex);
1166 m_aConfigListeners.removeInterface( aGuard, xListener );
1169 void ImageManagerImpl::implts_notifyContainerListener( const ConfigurationEvent& aEvent, NotifyOp eOp )
1171 std::unique_lock aGuard(m_mutex);
1172 comphelper::OInterfaceIteratorHelper4 pIterator( aGuard, m_aConfigListeners );
1173 aGuard.unlock();
1174 while ( pIterator.hasMoreElements() )
1178 switch ( eOp )
1180 case NotifyOp_Replace:
1181 pIterator.next()->elementReplaced( aEvent );
1182 break;
1183 case NotifyOp_Insert:
1184 pIterator.next()->elementInserted( aEvent );
1185 break;
1186 case NotifyOp_Remove:
1187 pIterator.next()->elementRemoved( aEvent );
1188 break;
1191 catch( const css::uno::RuntimeException& )
1193 aGuard.lock();
1194 pIterator.remove(aGuard);
1195 aGuard.unlock();
1199 void ImageManagerImpl::clear()
1201 SolarMutexGuard g;
1203 for (auto & n : m_pUserImageList)
1205 n.reset();
1208 } // namespace framework
1210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */