LibreOffice Module framework (master) 1
imagemanagerimpl.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "imagemanagerimpl.hxx"
21#include <utility>
25
26#include <properties.h>
27
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>
47#include <memory>
48#include <unordered_set>
49
50using ::com::sun::star::uno::Sequence;
51using ::com::sun::star::uno::XInterface;
52using ::com::sun::star::uno::RuntimeException;
53using ::com::sun::star::uno::UNO_QUERY;
54using ::com::sun::star::uno::Any;
55using ::com::sun::star::graphic::XGraphic;
56using namespace ::com::sun::star;
57using namespace ::com::sun::star::io;
58using namespace ::com::sun::star::embed;
59using namespace ::com::sun::star::lang;
60using namespace ::com::sun::star::container;
61using namespace ::com::sun::star::beans;
62using namespace ::com::sun::star::ui;
63using namespace ::cppu;
64
65const sal_Int16 MAX_IMAGETYPE_VALUE = css::ui::ImageType::SIZE_32;
66
67constexpr OUStringLiteral IMAGE_FOLDER = u"images";
68constexpr OUStringLiteral BITMAPS_FOLDER = u"Bitmaps";
69
71{
72 "sc_imagelist.xml",
73 "lc_imagelist.xml",
74 "xc_imagelist.xml"
75};
76
78{
79 "sc_userimages.png",
80 "lc_userimages.png",
81 "xc_userimages.png"
82};
83
84namespace framework
85{
86
88
89static std::mutex& getGlobalImageListMutex()
90{
91 static std::mutex mutex;
92 return mutex;
93}
94
95static GlobalImageList* getGlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext )
96{
97 std::unique_lock guard( getGlobalImageListMutex() );
98
99 if ( pGlobalImageList == nullptr )
100 pGlobalImageList = new GlobalImageList( rxContext );
101
102 return pGlobalImageList;
103}
104
105CmdImageList::CmdImageList( uno::Reference< uno::XComponentContext > rxContext, OUString aModuleIdentifier ) :
106 m_bInitialized(false),
107 m_aModuleIdentifier(std::move( aModuleIdentifier )),
108 m_xContext(std::move( rxContext ))
109{
110}
111
113{
114}
115
117{
118 if (m_bInitialized)
119 return;
120
121 const OUString aCommandImageList(UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDIMAGELIST);
122
123 Sequence<OUString> aCommandImageSeq;
124 uno::Reference<XNameAccess> xCommandDesc = frame::theUICommandDescription::get(m_xContext);
125
126 if (!m_aModuleIdentifier.isEmpty())
127 {
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
130 try
131 {
132 xCommandDesc->getByName(m_aModuleIdentifier) >>= xCommandDesc;
133 if (xCommandDesc.is())
134 xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq;
135 }
136 catch (const NoSuchElementException&)
137 {
138 // Module unknown we will work with an empty command image list!
139 return;
140 }
141 }
142
143 if (xCommandDesc.is())
144 {
145 try
146 {
147 xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq;
148 }
149 catch (const NoSuchElementException&)
150 {
151 }
152 catch (const WrappedTargetException&)
153 {
154 }
155 }
156
157 m_aResolver.registerCommands(aCommandImageSeq);
158
159 m_bInitialized = true;
160}
161
162
163Image CmdImageList::getImageFromCommandURL(vcl::ImageType nImageType, const OUString& rCommandURL)
164{
165 initialize();
166 return m_aResolver.getImageFromCommandURL(nImageType, rCommandURL);
167}
168
169bool CmdImageList::hasImage(vcl::ImageType /*nImageType*/, const OUString& rCommandURL)
170{
171 initialize();
172 return m_aResolver.hasImage(rCommandURL);
173}
174
176{
178}
179
180GlobalImageList::GlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext ) :
181 CmdImageList( rxContext, OUString() )
182{
183}
184
186{
187 std::unique_lock guard( getGlobalImageListMutex() );
188 // remove global pointer as we destroy the object now
189 pGlobalImageList = nullptr;
190}
191
192Image GlobalImageList::getImageFromCommandURL( vcl::ImageType nImageType, const OUString& rCommandURL )
193{
194 std::unique_lock guard( getGlobalImageListMutex() );
195 return CmdImageList::getImageFromCommandURL( nImageType, rCommandURL );
196}
197
198bool GlobalImageList::hasImage( vcl::ImageType nImageType, const OUString& rCommandURL )
199{
200 std::unique_lock guard( getGlobalImageListMutex() );
201 return CmdImageList::hasImage( nImageType, rCommandURL );
202}
203
204::std::vector< OUString >& GlobalImageList::getImageCommandNames()
205{
206 std::unique_lock guard( getGlobalImageListMutex() );
208}
209
210static bool implts_checkAndScaleGraphic( uno::Reference< XGraphic >& rOutGraphic, const uno::Reference< XGraphic >& rInGraphic, vcl::ImageType nImageType )
211{
212 if ( !rInGraphic.is() )
213 {
214 rOutGraphic = uno::Reference<graphic::XGraphic>();
215 return false;
216 }
217
218 static const o3tl::enumarray<vcl::ImageType, Size> BITMAP_SIZE =
219 {
220 Size(16, 16), Size(24, 24), Size(32, 32)
221 };
222
223 // Check size and scale it
224 Graphic aImage(rInGraphic);
225 if (BITMAP_SIZE[nImageType] != aImage.GetSizePixel())
226 {
227 BitmapEx aBitmap = aImage.GetBitmapEx();
228 aBitmap.Scale(BITMAP_SIZE[nImageType]);
229 aImage = Graphic(aBitmap);
230 rOutGraphic = aImage.GetXGraphic();
231 }
232 else
233 rOutGraphic = rInGraphic;
234
235 return true;
236}
237
238static vcl::ImageType implts_convertImageTypeToIndex( sal_Int16 nImageType )
239{
240 if (nImageType & css::ui::ImageType::SIZE_LARGE)
242 else if (nImageType & css::ui::ImageType::SIZE_32)
244 else
246}
247
249{
251 if ( !m_pUserImageList[nImageType] )
253
254 return m_pUserImageList[nImageType].get();
255}
256
258{
259 // Initialize the top-level structures with the storage data
260 if ( !m_xUserConfigStorage.is() )
261 return;
262
263 tools::Long nModes = m_bReadOnly ? ElementModes::READ : ElementModes::READWRITE;
264
265 try
266 {
268 nModes );
269 if ( m_xUserImageStorage.is() )
270 {
272 nModes );
273 }
274 }
275 catch ( const css::container::NoSuchElementException& )
276 {
277 }
278 catch ( const css::embed::InvalidStorageException& )
279 {
280 }
281 catch ( const css::lang::IllegalArgumentException& )
282 {
283 }
284 catch ( const css::io::IOException& )
285 {
286 }
287 catch ( const css::embed::StorageWrappedTargetException& )
288 {
289 }
290}
291
293 vcl::ImageType nImageType,
294 const uno::Reference< XStorage >& xUserImageStorage,
295 const uno::Reference< XStorage >& xUserBitmapsStorage )
296{
298
299 if ( xUserImageStorage.is() && xUserBitmapsStorage.is() )
300 {
301 try
302 {
303 uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( OUString::createFromAscii( IMAGELIST_XML_FILE[nImageType] ),
304 ElementModes::READ );
305 uno::Reference< XInputStream > xInputStream = xStream->getInputStream();
306
307 ImageItemDescriptorList aUserImageListInfo;
309 xInputStream,
310 aUserImageListInfo );
311 if ( !aUserImageListInfo.empty() )
312 {
313 sal_Int32 nCount = aUserImageListInfo.size();
314 std::vector< OUString > aUserImagesVector;
315 aUserImagesVector.reserve(nCount);
316 for ( sal_Int32 i=0; i < nCount; i++ )
317 {
318 const ImageItemDescriptor& rItem = aUserImageListInfo[i];
319 aUserImagesVector.push_back( rItem.aCommandURL );
320 }
321
322 uno::Reference< XStream > xBitmapStream = xUserBitmapsStorage->openStreamElement(
323 OUString::createFromAscii( BITMAP_FILE_NAMES[nImageType] ),
324 ElementModes::READ );
325
326 if ( xBitmapStream.is() )
327 {
328 BitmapEx aUserBitmap;
329 {
330 std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream ));
331 vcl::PngImageReader aPngReader( *pSvStream );
332 aUserBitmap = aPngReader.read();
333 }
334
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;
340 }
341 }
342 }
343 catch ( const css::container::NoSuchElementException& )
344 {
345 }
346 catch ( const css::embed::InvalidStorageException& )
347 {
348 }
349 catch ( const css::lang::IllegalArgumentException& )
350 {
351 }
352 catch ( const css::io::IOException& )
353 {
354 }
355 catch ( const css::embed::StorageWrappedTargetException& )
356 {
357 }
358 }
359
360 // Destroy old image list - create a new empty one
361 m_pUserImageList[nImageType].reset(new ImageList);
362}
363
365 vcl::ImageType nImageType,
366 const uno::Reference< XStorage >& xUserImageStorage,
367 const uno::Reference< XStorage >& xUserBitmapsStorage )
368{
370
371 if ( !m_bModified )
372 return false;
373
374 ImageList* pImageList = implts_getUserImageList( nImageType );
375 if ( pImageList->GetImageCount() > 0 )
376 {
377 ImageItemDescriptorList aUserImageListInfo;
378
379 for ( sal_uInt16 i=0; i < pImageList->GetImageCount(); i++ )
380 {
382 aItem.aCommandURL = pImageList->GetImageName( i );
383 aUserImageListInfo.push_back( aItem );
384 }
385
386 uno::Reference< XTransactedObject > xTransaction;
387 uno::Reference< XOutputStream > xOutputStream;
388 uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( OUString::createFromAscii( IMAGELIST_XML_FILE[nImageType] ),
389 ElementModes::WRITE|ElementModes::TRUNCATE );
390 if ( xStream.is() )
391 {
392 uno::Reference< XStream > xBitmapStream =
393 xUserBitmapsStorage->openStreamElement( OUString::createFromAscii( BITMAP_FILE_NAMES[nImageType] ),
394 ElementModes::WRITE|ElementModes::TRUNCATE );
395 if ( xBitmapStream.is() )
396 {
397 {
398 std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream ));
399 vcl::PngImageWriter aPngWriter( *pSvStream );
400 auto rBitmap = pImageList->GetAsHorizontalStrip();
401 aPngWriter.write( rBitmap );
402 }
403
404 // Commit user bitmaps storage
405 xTransaction.set( xUserBitmapsStorage, UNO_QUERY );
406 if ( xTransaction.is() )
407 xTransaction->commit();
408 }
409
410 xOutputStream = xStream->getOutputStream();
411 if ( xOutputStream.is() )
412 ImagesConfiguration::StoreImages( m_xContext, xOutputStream, aUserImageListInfo );
413
414 // Commit user image storage
415 xTransaction.set( xUserImageStorage, UNO_QUERY );
416 if ( xTransaction.is() )
417 xTransaction->commit();
418 }
419
420 return true;
421 }
422 else
423 {
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!
426 try
427 {
428 xUserImageStorage->removeElement( OUString::createFromAscii( IMAGELIST_XML_FILE[nImageType] ));
429 }
430 catch ( const css::container::NoSuchElementException& )
431 {
432 }
433
434 try
435 {
436 xUserBitmapsStorage->removeElement( OUString::createFromAscii( BITMAP_FILE_NAMES[nImageType] ));
437 }
438 catch ( const css::container::NoSuchElementException& )
439 {
440 }
441
442 uno::Reference< XTransactedObject > xTransaction;
443
444 // Commit user image storage
445 xTransaction.set( xUserImageStorage, UNO_QUERY );
446 if ( xTransaction.is() )
447 xTransaction->commit();
448
449 // Commit user bitmaps storage
450 xTransaction.set( xUserBitmapsStorage, UNO_QUERY );
451 if ( xTransaction.is() )
452 xTransaction->commit();
453
454 return true;
455 }
456
457 return false;
458}
459
461{
463
464 if ( !m_pGlobalImageList.is() )
466 return m_pGlobalImageList;
467}
468
470{
472
473 if ( !m_pDefaultImageList )
475
476 return m_pDefaultImageList.get();
477}
478
479ImageManagerImpl::ImageManagerImpl( uno::Reference< uno::XComponentContext > xContext, ::cppu::OWeakObject* pOwner, bool _bUseGlobal ) :
480 m_xContext(std::move( xContext ))
482 , m_aResourceString( "private:resource/images/moduleimages" )
483 , m_bUseGlobal(_bUseGlobal)
484 , m_bReadOnly( true )
485 , m_bInitialized( false )
486 , m_bModified( false )
487 , m_bDisposed( false )
488{
490 {
491 m_pUserImageList[n] = nullptr;
493 }
494}
495
497{
498 clear();
499}
500
502{
503 uno::Reference< uno::XInterface > xOwner(m_pOwner);
504 css::lang::EventObject aEvent( xOwner );
505 {
506 std::unique_lock aGuard(m_mutex);
508 }
509 {
510 std::unique_lock aGuard(m_mutex);
512 }
513
514 {
516 m_xUserConfigStorage.clear();
517 m_xUserImageStorage.clear();
518 m_xUserRootCommit.clear();
519 m_bModified = false;
520 m_bDisposed = true;
521
522 // delete user and default image list on dispose
523 for (auto& n : m_pUserImageList)
524 {
525 n.reset();
526 }
527 m_pDefaultImageList.reset();
528 }
529
530}
531void ImageManagerImpl::addEventListener( const uno::Reference< XEventListener >& xListener )
532{
533 {
535
536 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
537 if ( m_bDisposed )
538 throw DisposedException();
539 }
540
541 std::unique_lock aGuard(m_mutex);
542 m_aEventListeners.addInterface( aGuard, xListener );
543}
544
545void ImageManagerImpl::removeEventListener( const uno::Reference< XEventListener >& xListener )
546{
547 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
548 std::unique_lock aGuard(m_mutex);
549 m_aEventListeners.removeInterface( aGuard, xListener );
550}
551
552// XInitialization
553void ImageManagerImpl::initialize( const Sequence< Any >& aArguments )
554{
556
557 if ( m_bInitialized )
558 return;
559
560 for ( const Any& rArg : aArguments )
561 {
562 PropertyValue aPropValue;
563 if ( rArg >>= aPropValue )
564 {
565 if ( aPropValue.Name == "UserConfigStorage" )
566 {
567 aPropValue.Value >>= m_xUserConfigStorage;
568 }
569 else if ( aPropValue.Name == "ModuleIdentifier" )
570 {
571 aPropValue.Value >>= m_aModuleIdentifier;
572 }
573 else if ( aPropValue.Name == "UserRootCommit" )
574 {
575 aPropValue.Value >>= m_xUserRootCommit;
576 }
577 }
578 }
579
580 if ( m_xUserConfigStorage.is() )
581 {
582 uno::Reference< XPropertySet > xPropSet( m_xUserConfigStorage, UNO_QUERY );
583 if ( xPropSet.is() )
584 {
585 tools::Long nOpenMode = 0;
586 if ( xPropSet->getPropertyValue("OpenMode") >>= nOpenMode )
587 m_bReadOnly = !( nOpenMode & ElementModes::WRITE );
588 }
589 }
590
592
593 m_bInitialized = true;
594}
595
596// XImageManagerImpl
598{
600
601 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
602 if ( m_bDisposed )
603 throw DisposedException();
604
605 std::vector< OUString > aUserImageNames;
606
608 {
609 aUserImageNames.clear();
610 ImageList* pImageList = implts_getUserImageList(i);
611 pImageList->GetImageNames( aUserImageNames );
612
613 Sequence< OUString > aRemoveList( comphelper::containerToSequence(aUserImageNames) );
614
615 // Remove images
616 removeImages( sal_Int16( i ), aRemoveList );
618 }
619
620 m_bModified = true;
621}
622
623Sequence< OUString > ImageManagerImpl::getAllImageNames( ::sal_Int16 nImageType )
624{
626
627 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
628 if ( m_bDisposed )
629 throw DisposedException();
630
631 std::unordered_set< OUString > aImageCmdNames;
632
634
635 sal_uInt32 i( 0 );
636 if ( m_bUseGlobal )
637 {
639
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] );
644
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] );
649 }
650
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] );
657
658 return comphelper::containerToSequence( aImageCmdNames );
659}
660
661bool ImageManagerImpl::hasImage( ::sal_Int16 nImageType, const OUString& aCommandURL )
662{
664
665 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
666 if ( m_bDisposed )
667 throw DisposedException();
668
669 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
670 throw IllegalArgumentException();
671
673 if ( m_bUseGlobal && implts_getGlobalImageList()->hasImage( nIndex, aCommandURL ))
674 return true;
675 else
676 {
677 if ( m_bUseGlobal && implts_getDefaultImageList()->hasImage( nIndex, aCommandURL ))
678 return true;
679 else
680 {
681 // User layer
683 if ( pImageList )
684 return ( pImageList->GetImagePos( aCommandURL ) != IMAGELIST_IMAGE_NOTFOUND );
685 }
686 }
687
688 return false;
689}
690
691namespace
692{
693 css::uno::Reference< css::graphic::XGraphic > GetXGraphic(const Image &rImage)
694 {
695 return Graphic(rImage).GetXGraphic();
696 }
697}
698
699Sequence< uno::Reference< XGraphic > > ImageManagerImpl::getImages(
700 ::sal_Int16 nImageType,
701 const Sequence< OUString >& aCommandURLSequence )
702{
704
705 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
706 if ( m_bDisposed )
707 throw DisposedException();
708
709 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
710 throw IllegalArgumentException();
711
712 Sequence< uno::Reference< XGraphic > > aGraphSeq( aCommandURLSequence.getLength() );
713
715 rtl::Reference< GlobalImageList > rGlobalImageList;
716 CmdImageList* pDefaultImageList = nullptr;
717 if ( m_bUseGlobal )
718 {
719 rGlobalImageList = implts_getGlobalImageList();
720 pDefaultImageList = implts_getDefaultImageList();
721 }
722 ImageList* pUserImageList = implts_getUserImageList(nIndex);
723
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 )
731 {
732 Image aImage = pUserImageList->GetImage( rURL );
733 if ( !aImage && m_bUseGlobal )
734 {
735 aImage = pDefaultImageList->getImageFromCommandURL( nIndex, rURL );
736 if ( !aImage )
737 aImage = rGlobalImageList->getImageFromCommandURL( nIndex, rURL );
738 }
739
740 aGraphSeqRange[n++] = GetXGraphic(aImage);
741 }
742
743 return aGraphSeq;
744}
745
747 ::sal_Int16 nImageType,
748 const Sequence< OUString >& aCommandURLSequence,
749 const Sequence< uno::Reference< XGraphic > >& aGraphicsSequence )
750{
751 rtl::Reference<GraphicNameAccess> pInsertedImages;
752 rtl::Reference<GraphicNameAccess> pReplacedImages;
753
754 {
756
757 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
758 if ( m_bDisposed )
759 throw DisposedException();
760
761 if (( aCommandURLSequence.getLength() != aGraphicsSequence.getLength() ) ||
762 (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE )))
763 throw IllegalArgumentException();
764
765 if ( m_bReadOnly )
766 throw IllegalAccessException();
767
770
771 uno::Reference< XGraphic > xGraphic;
772 for ( sal_Int32 i = 0; i < aCommandURLSequence.getLength(); i++ )
773 {
774 // Check size and scale. If we don't have any graphics ignore it
775 if ( !implts_checkAndScaleGraphic( xGraphic, aGraphicsSequence[i], nIndex ))
776 continue;
777
778 sal_uInt16 nPos = pImageList->GetImagePos( aCommandURLSequence[i] );
780 {
781 pImageList->AddImage(aCommandURLSequence[i], Image(xGraphic));
782 if ( !pInsertedImages )
783 pInsertedImages = new GraphicNameAccess();
784 pInsertedImages->addElement( aCommandURLSequence[i], xGraphic );
785 }
786 else
787 {
788 pImageList->ReplaceImage(aCommandURLSequence[i], Image(xGraphic));
789 if ( !pReplacedImages )
790 pReplacedImages = new GraphicNameAccess();
791 pReplacedImages->addElement( aCommandURLSequence[i], xGraphic );
792 }
793 }
794
795 if (( pInsertedImages != nullptr ) || ( pReplacedImages != nullptr ))
796 {
797 m_bModified = true;
799 }
800 }
801
802 uno::Reference< uno::XInterface > xOwner(m_pOwner);
803 // Notify listeners
804 if ( pInsertedImages != nullptr )
805 {
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);
813 }
814 if ( pReplacedImages != nullptr )
815 {
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);
824 }
825}
826
827void ImageManagerImpl::removeImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence )
828{
830 rtl::Reference<GraphicNameAccess> pReplacedImages;
831
832 {
834
835 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
836 if ( m_bDisposed )
837 throw DisposedException();
838
839 if (( nImageType < 0 ) || ( nImageType > MAX_IMAGETYPE_VALUE ))
840 throw IllegalArgumentException();
841
842 if ( m_bReadOnly )
843 throw IllegalAccessException();
844
846 rtl::Reference< GlobalImageList > rGlobalImageList;
847 CmdImageList* pDefaultImageList = nullptr;
848 if ( m_bUseGlobal )
849 {
850 rGlobalImageList = implts_getGlobalImageList();
851 pDefaultImageList = implts_getDefaultImageList();
852 }
854 uno::Reference<XGraphic> xEmptyGraphic;
855
856 for ( const OUString& rURL : aCommandURLSequence )
857 {
858 sal_uInt16 nPos = pImageList->GetImagePos( rURL );
860 {
861 sal_uInt16 nId = pImageList->GetImageId( nPos );
862 pImageList->RemoveImage( nId );
863
864 if ( m_bUseGlobal )
865 {
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 )
872 {
873 if ( !pRemovedImages )
874 pRemovedImages = new GraphicNameAccess();
875 pRemovedImages->addElement( rURL, xEmptyGraphic );
876 }
877 else
878 {
879 if ( !pReplacedImages )
880 pReplacedImages = new GraphicNameAccess();
881 pReplacedImages->addElement(rURL, GetXGraphic(aNewImage));
882 }
883 } // if ( m_bUseGlobal )
884 else
885 {
886 if ( !pRemovedImages )
887 pRemovedImages = new GraphicNameAccess();
888 pRemovedImages->addElement( rURL, xEmptyGraphic );
889 }
890 }
891 }
892
893 if (( pReplacedImages != nullptr ) || ( pRemovedImages != nullptr ))
894 {
895 m_bModified = true;
897 }
898 }
899
900 // Notify listeners
901 uno::Reference< uno::XInterface > xOwner(m_pOwner);
902 if ( pRemovedImages != nullptr )
903 {
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);
911 }
912 if ( pReplacedImages != nullptr )
913 {
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);
922 }
923}
924
925void ImageManagerImpl::insertImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence, const Sequence< uno::Reference< XGraphic > >& aGraphicSequence )
926{
927 replaceImages(nImageType,aCommandURLSequence,aGraphicSequence);
928}
929
930// XUIConfigurationPersistence
932{
934
935 if ( m_bDisposed )
936 throw DisposedException();
937
938 CommandMap aOldUserCmdImageSet;
939 std::vector< OUString > aNewUserCmdImageSet;
940
941 if ( !m_bModified )
942 return;
943
945 {
947 {
948 std::vector< OUString > aOldUserCmdImageVector;
949 ImageList* pImageList = implts_getUserImageList(i);
950 pImageList->GetImageNames( aOldUserCmdImageVector );
951
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 );
957
958 // Attention: This can make the old image list pointer invalid!
960 pImageList = implts_getUserImageList(i);
961 pImageList->GetImageNames( aNewUserCmdImageSet );
962
963 rtl::Reference<GraphicNameAccess> pInsertedImages;
964 rtl::Reference<GraphicNameAccess> pReplacedImages;
966
967 for (auto const& newUserCmdImage : aNewUserCmdImageSet)
968 {
969 CommandMap::iterator pIter = aOldUserCmdImageSet.find(newUserCmdImage);
970 if ( pIter != aOldUserCmdImageSet.end() )
971 {
972 pIter->second = true; // mark entry as replaced
973 if ( !pReplacedImages )
974 pReplacedImages = new GraphicNameAccess();
975 pReplacedImages->addElement( newUserCmdImage,
976 GetXGraphic(pImageList->GetImage(newUserCmdImage)) );
977 }
978 else
979 {
980 if ( !pInsertedImages )
981 pInsertedImages = new GraphicNameAccess();
982 pInsertedImages->addElement( newUserCmdImage,
983 GetXGraphic(pImageList->GetImage(newUserCmdImage)) );
984 }
985 }
986
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 )
993 {
994 rGlobalImageList = implts_getGlobalImageList();
995 pDefaultImageList = implts_getDefaultImageList();
996 }
997 uno::Reference<XGraphic> xEmptyGraphic;
998 for (auto const& oldUserCmdImage : aOldUserCmdImageSet)
999 {
1000 if ( !oldUserCmdImage.second )
1001 {
1002 if ( m_bUseGlobal )
1003 {
1004 Image aImage = pDefaultImageList->getImageFromCommandURL( i, oldUserCmdImage.first );
1005 if ( !aImage )
1006 aImage = rGlobalImageList->getImageFromCommandURL( i, oldUserCmdImage.first );
1007
1008 if ( !aImage )
1009 {
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 );
1014 }
1015 else
1016 {
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));
1021 }
1022 } // if ( m_bUseGlobal )
1023 else
1024 {
1025 // No image in the user image list => remove user image
1026 if ( !pRemovedImages )
1027 pRemovedImages = new GraphicNameAccess();
1028 pRemovedImages->addElement( oldUserCmdImage.first, xEmptyGraphic );
1029 }
1030 }
1031 }
1032
1033 aGuard.clear();
1034
1035 // Now notify our listeners. Unlock mutex to prevent deadlocks
1036 uno::Reference< uno::XInterface > xOwner(m_pOwner);
1037 if ( pInsertedImages != nullptr )
1038 {
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 );
1046 }
1047 if ( pReplacedImages != nullptr )
1048 {
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 );
1057 }
1058 if ( pRemovedImages != nullptr )
1059 {
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 );
1067 }
1068
1069 aGuard.reset();
1070 }
1071 }
1072}
1073
1075{
1077
1078 if ( m_bDisposed )
1079 throw DisposedException();
1080
1081 if ( !m_bModified )
1082 return;
1083
1084 bool bWritten( false );
1086 {
1088 if ( bSuccess )
1089 bWritten = true;
1090 m_bUserImageListModified[i] = false;
1091 }
1092
1093 if ( bWritten &&
1095 {
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();
1101 }
1102
1103 m_bModified = false;
1104}
1105
1106void ImageManagerImpl::storeToStorage( const uno::Reference< XStorage >& Storage )
1107{
1109
1110 if ( m_bDisposed )
1111 throw DisposedException();
1112
1113 if ( !(m_bModified && Storage.is()) )
1114 return;
1115
1116 tools::Long nModes = ElementModes::READWRITE;
1117
1118 uno::Reference< XStorage > xUserImageStorage = Storage->openStorageElement( IMAGE_FOLDER,
1119 nModes );
1120 if ( !xUserImageStorage.is() )
1121 return;
1122
1123 uno::Reference< XStorage > xUserBitmapsStorage = xUserImageStorage->openStorageElement( BITMAPS_FOLDER,
1124 nModes );
1126 {
1128 implts_storeUserImages( i, xUserImageStorage, xUserBitmapsStorage );
1129 }
1130
1131 uno::Reference< XTransactedObject > xTransaction( Storage, UNO_QUERY );
1132 if ( xTransaction.is() )
1133 xTransaction->commit();
1134}
1135
1137{
1139 return m_bModified;
1140}
1141
1143{
1145 return m_bReadOnly;
1146}
1147// XUIConfiguration
1148void ImageManagerImpl::addConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener )
1149{
1150 {
1152
1153 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
1154 if ( m_bDisposed )
1155 throw DisposedException();
1156 }
1157
1158 std::unique_lock aGuard(m_mutex);
1159 m_aConfigListeners.addInterface( aGuard, xListener );
1160}
1161
1162void ImageManagerImpl::removeConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener )
1163{
1164 /* SAFE AREA ----------------------------------------------------------------------------------------------- */
1165 std::unique_lock aGuard(m_mutex);
1166 m_aConfigListeners.removeInterface( aGuard, xListener );
1167}
1168
1169void ImageManagerImpl::implts_notifyContainerListener( const ConfigurationEvent& aEvent, NotifyOp eOp )
1170{
1171 std::unique_lock aGuard(m_mutex);
1173 aGuard.unlock();
1174 while ( pIterator.hasMoreElements() )
1175 {
1176 try
1177 {
1178 switch ( eOp )
1179 {
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;
1189 }
1190 }
1191 catch( const css::uno::RuntimeException& )
1192 {
1193 aGuard.lock();
1194 pIterator.remove(aGuard);
1195 aGuard.unlock();
1196 }
1197 }
1198}
1200{
1202
1203 for (auto & n : m_pUserImageList)
1204 {
1205 n.reset();
1206 }
1207}
1208} // namespace framework
1209
1210/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
AnyEventRef aEvent
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
void AddImage(const OUString &rImageName, const Image &rImage)
Definition: ImageList.cxx:97
void ReplaceImage(const OUString &rImageName, const Image &rImage)
Definition: ImageList.cxx:104
sal_uInt16 GetImagePos(std::u16string_view rImageName) const
Definition: ImageList.cxx:142
Image GetImage(const OUString &rImageName) const
Definition: ImageList.cxx:129
sal_uInt16 GetImageId(sal_uInt16 nPos) const
Definition: ImageList.cxx:156
const OUString & GetImageName(sal_uInt16 nPos) const
Definition: ImageList.cxx:161
BitmapEx GetAsHorizontalStrip() const
Definition: ImageList.cxx:42
void GetImageNames(::std::vector< OUString > &rNames) const
Definition: ImageList.cxx:166
void RemoveImage(sal_uInt16 nId)
Definition: ImageList.cxx:117
sal_uInt16 GetImageCount() const
Definition: ImageList.cxx:137
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void disposeAndClear(::std::unique_lock<::std::mutex > &rGuard, const css::lang::EventObject &rEvt)
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
css::uno::Reference< ListenerT > const & next()
void remove(::std::unique_lock<::std::mutex > &rGuard)
css::uno::Reference< css::uno::XComponentContext > m_xContext
CmdImageList(css::uno::Reference< css::uno::XComponentContext > xContext, OUString aModuleIdentifier)
virtual std::vector< OUString > & getImageCommandNames()
vcl::CommandImageResolver m_aResolver
virtual Image getImageFromCommandURL(vcl::ImageType nImageType, const OUString &rCommandURL)
virtual bool hasImage(vcl::ImageType nImageType, const OUString &rCommandURL)
virtual ::std::vector< OUString > & getImageCommandNames() override
virtual ~GlobalImageList() override
GlobalImageList(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual Image getImageFromCommandURL(vcl::ImageType nImageType, const OUString &rCommandURL) override
virtual bool hasImage(vcl::ImageType nImageType, const OUString &rCommandURL) override
css::uno::Sequence< OUString > getAllImageNames(::sal_Int16 nImageType)
void implts_notifyContainerListener(const css::ui::ConfigurationEvent &aEvent, NotifyOp eOp)
o3tl::enumarray< vcl::ImageType, std::unique_ptr< ImageList > > m_pUserImageList
ImageManagerImpl(css::uno::Reference< css::uno::XComponentContext > xContext,::cppu::OWeakObject *pOwner, bool _bUseGlobal)
::cppu::OWeakObject * m_pOwner
css::uno::Reference< css::embed::XStorage > m_xUserConfigStorage
CmdImageList * implts_getDefaultImageList()
void storeToStorage(const css::uno::Reference< css::embed::XStorage > &Storage)
o3tl::enumarray< vcl::ImageType, bool > m_bUserImageListModified
std::unique_ptr< CmdImageList > m_pDefaultImageList
void insertImages(::sal_Int16 nImageType, const css::uno::Sequence< OUString > &aCommandURLSequence, const css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > &aGraphicSequence)
rtl::Reference< GlobalImageList > m_pGlobalImageList
bool hasImage(::sal_Int16 nImageType, const OUString &aCommandURL)
const rtl::Reference< GlobalImageList > & implts_getGlobalImageList()
css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > getImages(::sal_Int16 nImageType, const css::uno::Sequence< OUString > &aCommandURLSequence)
comphelper::OInterfaceContainerHelper4< css::ui::XUIConfigurationListener > m_aConfigListeners
css::uno::Reference< css::embed::XStorage > m_xUserBitmapsStorage
css::uno::Reference< css::embed::XStorage > m_xUserImageStorage
void addEventListener(const css::uno::Reference< css::lang::XEventListener > &xListener)
void initialize(const css::uno::Sequence< css::uno::Any > &aArguments)
void removeImages(::sal_Int16 nImageType, const css::uno::Sequence< OUString > &aResourceURLSequence)
void replaceImages(::sal_Int16 nImageType, const css::uno::Sequence< OUString > &aCommandURLSequence, const css::uno::Sequence< css::uno::Reference< css::graphic::XGraphic > > &aGraphicsSequence)
css::uno::Reference< css::embed::XTransactedObject > m_xUserRootCommit
void removeConfigurationListener(const css::uno::Reference< css::ui::XUIConfigurationListener > &Listener)
bool implts_storeUserImages(vcl::ImageType nImageType, const css::uno::Reference< css::embed::XStorage > &xUserImageStorage, const css::uno::Reference< css::embed::XStorage > &xUserBitmapsStorage)
ImageList * implts_getUserImageList(vcl::ImageType nImageType)
void implts_loadUserImages(vcl::ImageType nImageType, const css::uno::Reference< css::embed::XStorage > &xUserImageStorage, const css::uno::Reference< css::embed::XStorage > &xUserBitmapsStorage)
void removeEventListener(const css::uno::Reference< css::lang::XEventListener > &aListener)
comphelper::OInterfaceContainerHelper4< css::lang::XEventListener > m_aEventListeners
void addConfigurationListener(const css::uno::Reference< css::ui::XUIConfigurationListener > &Listener)
css::uno::Reference< css::uno::XComponentContext > m_xContext
static bool LoadImages(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::io::XInputStream > &rInputStream, ImageItemDescriptorList &rItems)
static bool StoreImages(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::io::XOutputStream > &rOutputStream, const ImageItemDescriptorList &rItems)
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
bool hasImage(const OUString &rCommandURL)
Image getImageFromCommandURL(ImageType nImageType, const OUString &rCommandURL)
std::vector< OUString > & getCommandNames()
void registerCommands(const css::uno::Sequence< OUString > &aCommandSequence)
bool read(BitmapEx &rBitmap)
bool write(const BitmapEx &rBitmap)
int nCount
float u
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool m_bDisposed
bool m_bReadOnly
#define IMAGELIST_IMAGE_NOTFOUND
constexpr OUStringLiteral BITMAPS_FOLDER
const o3tl::enumarray< vcl::ImageType, const char * > BITMAP_FILE_NAMES
const o3tl::enumarray< vcl::ImageType, const char * > IMAGELIST_XML_FILE
constexpr OUStringLiteral IMAGE_FOLDER
const sal_Int16 MAX_IMAGETYPE_VALUE
Sequence< PropertyValue > aArguments
sal_Int32 nIndex
sal_Int64 n
SvLinkSource * pOwner
sal_uInt16 nPos
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
std::unordered_map< OUString, bool > CommandMap
Definition: imagetype.hxx:31
constexpr OUStringLiteral UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDIMAGELIST
properties for "UICommandDescription" class
Definition: properties.h:65
std::vector< ImageItemDescriptor > ImageItemDescriptorList
static bool implts_checkAndScaleGraphic(uno::Reference< XGraphic > &rOutGraphic, const uno::Reference< XGraphic > &rInGraphic, vcl::ImageType nImageType)
static GlobalImageList * getGlobalImageList(const uno::Reference< uno::XComponentContext > &rxContext)
static std::mutex & getGlobalImageListMutex()
static GlobalImageList * pGlobalImageList
static vcl::ImageType implts_convertImageTypeToIndex(sal_Int16 nImageType)
int i
long Long
sal_Int16 nId
ContentProvider * m_pOwner
std::mutex mutex