/src/libreoffice/framework/source/uiconfiguration/imagemanagerimpl.cxx
Line | Count | Source |
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> |
22 | | #include <xml/imagesconfiguration.hxx> |
23 | | #include <uiconfiguration/imagetype.hxx> |
24 | | #include <uiconfiguration/graphicnameaccess.hxx> |
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 <tools/stream.hxx> |
41 | | #include <vcl/bitmap.hxx> |
42 | | #include <vcl/graph.hxx> |
43 | | #include <vcl/svapp.hxx> |
44 | | #include <o3tl/enumrange.hxx> |
45 | | #include <comphelper/sequence.hxx> |
46 | | #include <unotools/ucbstreamhelper.hxx> |
47 | | #include <vcl/filter/PngImageReader.hxx> |
48 | | #include <vcl/filter/PngImageWriter.hxx> |
49 | | #include <memory> |
50 | | #include <unordered_set> |
51 | | |
52 | | using ::com::sun::star::uno::Sequence; |
53 | | using ::com::sun::star::uno::XInterface; |
54 | | using ::com::sun::star::uno::RuntimeException; |
55 | | using ::com::sun::star::uno::UNO_QUERY; |
56 | | using ::com::sun::star::uno::Any; |
57 | | using ::com::sun::star::graphic::XGraphic; |
58 | | using namespace ::com::sun::star; |
59 | | using namespace ::com::sun::star::io; |
60 | | using namespace ::com::sun::star::embed; |
61 | | using namespace ::com::sun::star::lang; |
62 | | using namespace ::com::sun::star::container; |
63 | | using namespace ::com::sun::star::beans; |
64 | | using namespace ::com::sun::star::ui; |
65 | | using namespace ::cppu; |
66 | | |
67 | | constexpr OUString IMAGE_FOLDER = u"images"_ustr; |
68 | | constexpr OUString BITMAPS_FOLDER = u"Bitmaps"_ustr; |
69 | | |
70 | | constexpr o3tl::enumarray<vcl::ImageType, OUString> IMAGELIST_XML_FILE |
71 | | { |
72 | | u"sc_imagelist.xml"_ustr, |
73 | | u"lc_imagelist.xml"_ustr, |
74 | | u"xc_imagelist.xml"_ustr |
75 | | }; |
76 | | |
77 | | constexpr o3tl::enumarray<vcl::ImageType, OUString> BITMAP_FILE_NAMES |
78 | | { |
79 | | u"sc_userimages.png"_ustr, |
80 | | u"lc_userimages.png"_ustr, |
81 | | u"xc_userimages.png"_ustr |
82 | | }; |
83 | | |
84 | | namespace framework |
85 | | { |
86 | | namespace |
87 | | { |
88 | | using ImageIndex = std::tuple<vcl::ImageType, vcl::ImageWritingDirection>; |
89 | | } |
90 | | |
91 | | static GlobalImageList* pGlobalImageList = nullptr; |
92 | | |
93 | | static std::mutex& getGlobalImageListMutex() |
94 | 0 | { |
95 | 0 | static std::mutex mutex; |
96 | 0 | return mutex; |
97 | 0 | } |
98 | | |
99 | | static GlobalImageList* getGlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext ) |
100 | 0 | { |
101 | 0 | std::unique_lock guard( getGlobalImageListMutex() ); |
102 | |
|
103 | 0 | if ( pGlobalImageList == nullptr ) |
104 | 0 | pGlobalImageList = new GlobalImageList( rxContext ); |
105 | |
|
106 | 0 | return pGlobalImageList; |
107 | 0 | } |
108 | | |
109 | | CmdImageList::CmdImageList( uno::Reference< uno::XComponentContext > rxContext, OUString aModuleIdentifier ) : |
110 | 0 | m_bInitialized(false), |
111 | 0 | m_aModuleIdentifier(std::move( aModuleIdentifier )), |
112 | 0 | m_xContext(std::move( rxContext )) |
113 | 0 | { |
114 | 0 | } |
115 | | |
116 | | CmdImageList::~CmdImageList() |
117 | 0 | { |
118 | 0 | } |
119 | | |
120 | | void CmdImageList::initialize() |
121 | 0 | { |
122 | 0 | if (m_bInitialized) |
123 | 0 | return; |
124 | | |
125 | 0 | const OUString aCommandImageList(UICOMMANDDESCRIPTION_NAMEACCESS_COMMANDIMAGELIST); |
126 | |
|
127 | 0 | Sequence<OUString> aCommandImageSeq; |
128 | 0 | uno::Reference<XNameAccess> xCommandDesc = frame::theUICommandDescription::get(m_xContext); |
129 | |
|
130 | 0 | if (!m_aModuleIdentifier.isEmpty()) |
131 | 0 | { |
132 | | // If we have a module identifier - use to retrieve the command image name list from it. |
133 | | // Otherwise we will use the global command image list |
134 | 0 | try |
135 | 0 | { |
136 | 0 | xCommandDesc->getByName(m_aModuleIdentifier) >>= xCommandDesc; |
137 | 0 | if (xCommandDesc.is()) |
138 | 0 | xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq; |
139 | 0 | } |
140 | 0 | catch (const NoSuchElementException&) |
141 | 0 | { |
142 | | // Module unknown we will work with an empty command image list! |
143 | 0 | return; |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | 0 | if (xCommandDesc.is()) |
148 | 0 | { |
149 | 0 | try |
150 | 0 | { |
151 | 0 | xCommandDesc->getByName(aCommandImageList) >>= aCommandImageSeq; |
152 | 0 | } |
153 | 0 | catch (const NoSuchElementException&) |
154 | 0 | { |
155 | 0 | } |
156 | 0 | catch (const WrappedTargetException&) |
157 | 0 | { |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | 0 | m_aResolver.registerCommands(aCommandImageSeq); |
162 | |
|
163 | 0 | m_bInitialized = true; |
164 | 0 | } |
165 | | |
166 | | Image CmdImageList::getImageFromCommandURL(vcl::ImageType nImageType, |
167 | | vcl::ImageWritingDirection nImageDir, |
168 | | const OUString& rCommandURL) |
169 | 0 | { |
170 | 0 | initialize(); |
171 | 0 | return m_aResolver.getImageFromCommandURL(nImageType, nImageDir, rCommandURL); |
172 | 0 | } |
173 | | |
174 | | bool CmdImageList::hasImage(vcl::ImageType /*nImageType*/, vcl::ImageWritingDirection /*nImageDir*/, |
175 | | const OUString& rCommandURL) |
176 | 0 | { |
177 | 0 | initialize(); |
178 | 0 | return m_aResolver.hasImage(rCommandURL); |
179 | 0 | } |
180 | | |
181 | | std::vector<OUString>& CmdImageList::getImageCommandNames() |
182 | 0 | { |
183 | 0 | return m_aResolver.getCommandNames(); |
184 | 0 | } |
185 | | |
186 | | GlobalImageList::GlobalImageList( const uno::Reference< uno::XComponentContext >& rxContext ) : |
187 | 0 | CmdImageList( rxContext, OUString() ) |
188 | 0 | { |
189 | 0 | } |
190 | | |
191 | | GlobalImageList::~GlobalImageList() |
192 | 0 | { |
193 | 0 | std::unique_lock guard( getGlobalImageListMutex() ); |
194 | | // remove global pointer as we destroy the object now |
195 | 0 | pGlobalImageList = nullptr; |
196 | 0 | } |
197 | | |
198 | | Image GlobalImageList::getImageFromCommandURL(vcl::ImageType nImageType, |
199 | | vcl::ImageWritingDirection nImageDir, |
200 | | const OUString& rCommandURL) |
201 | 0 | { |
202 | 0 | std::unique_lock guard(getGlobalImageListMutex()); |
203 | 0 | return CmdImageList::getImageFromCommandURL(nImageType, nImageDir, rCommandURL); |
204 | 0 | } |
205 | | |
206 | | bool GlobalImageList::hasImage(vcl::ImageType nImageType, vcl::ImageWritingDirection nImageDir, |
207 | | const OUString& rCommandURL) |
208 | 0 | { |
209 | 0 | std::unique_lock guard(getGlobalImageListMutex()); |
210 | 0 | return CmdImageList::hasImage(nImageType, nImageDir, rCommandURL); |
211 | 0 | } |
212 | | |
213 | | ::std::vector< OUString >& GlobalImageList::getImageCommandNames() |
214 | 0 | { |
215 | 0 | std::unique_lock guard( getGlobalImageListMutex() ); |
216 | 0 | return CmdImageList::getImageCommandNames(); |
217 | 0 | } |
218 | | |
219 | | static bool implts_checkAndScaleGraphic( uno::Reference< XGraphic >& rOutGraphic, const uno::Reference< XGraphic >& rInGraphic, vcl::ImageType nImageType ) |
220 | 0 | { |
221 | 0 | if ( !rInGraphic.is() ) |
222 | 0 | { |
223 | 0 | rOutGraphic = uno::Reference<graphic::XGraphic>(); |
224 | 0 | return false; |
225 | 0 | } |
226 | | |
227 | 0 | static const o3tl::enumarray<vcl::ImageType, Size> BITMAP_SIZE = |
228 | 0 | { |
229 | 0 | Size(16, 16), Size(24, 24), Size(32, 32) |
230 | 0 | }; |
231 | | |
232 | | // Check size and scale it |
233 | 0 | Graphic aImage(rInGraphic); |
234 | 0 | if (BITMAP_SIZE[nImageType] != aImage.GetSizePixel()) |
235 | 0 | { |
236 | 0 | Bitmap aBitmap = aImage.GetBitmap(); |
237 | 0 | aBitmap.Scale(BITMAP_SIZE[nImageType]); |
238 | 0 | aImage = Graphic(aBitmap); |
239 | 0 | rOutGraphic = aImage.GetXGraphic(); |
240 | 0 | } |
241 | 0 | else |
242 | 0 | rOutGraphic = rInGraphic; |
243 | |
|
244 | 0 | return true; |
245 | 0 | } |
246 | | |
247 | | static std::optional<ImageIndex> implts_convertImageTypeToIndex(sal_Int16 nImageType) |
248 | 0 | { |
249 | 0 | sal_Int16 nSanitize = 0; |
250 | |
|
251 | 0 | vcl::ImageType nType = vcl::ImageType::Size16; |
252 | 0 | vcl::ImageWritingDirection nDir = vcl::ImageWritingDirection::DontCare; |
253 | |
|
254 | 0 | if (nImageType & css::ui::ImageType::SIZE_LARGE) |
255 | 0 | { |
256 | 0 | nType = vcl::ImageType::Size26; |
257 | 0 | nSanitize |= css::ui::ImageType::SIZE_LARGE; |
258 | 0 | } |
259 | 0 | else if (nImageType & css::ui::ImageType::SIZE_32) |
260 | 0 | { |
261 | 0 | nType = vcl::ImageType::Size32; |
262 | 0 | nSanitize |= css::ui::ImageType::SIZE_32; |
263 | 0 | } |
264 | |
|
265 | 0 | if (nImageType & css::ui::ImageType::DIR_RL_TB) |
266 | 0 | { |
267 | 0 | nDir = vcl::ImageWritingDirection::RightLeftTopBottom; |
268 | 0 | nSanitize |= css::ui::ImageType::DIR_RL_TB; |
269 | 0 | } |
270 | |
|
271 | 0 | if (nSanitize != nImageType) |
272 | 0 | { |
273 | 0 | return std::nullopt; |
274 | 0 | } |
275 | | |
276 | 0 | return ImageIndex{ nType, nDir }; |
277 | 0 | } |
278 | | |
279 | | ImageList* ImageManagerImpl::implts_getUserImageList( vcl::ImageType nImageType ) |
280 | 0 | { |
281 | 0 | SolarMutexGuard g; |
282 | 0 | if ( !m_pUserImageList[nImageType] ) |
283 | 0 | implts_loadUserImages( nImageType, m_xUserImageStorage, m_xUserBitmapsStorage ); |
284 | |
|
285 | 0 | return m_pUserImageList[nImageType].get(); |
286 | 0 | } |
287 | | |
288 | | void ImageManagerImpl::implts_initialize() |
289 | 0 | { |
290 | | // Initialize the top-level structures with the storage data |
291 | 0 | if ( !m_xUserConfigStorage.is() ) |
292 | 0 | return; |
293 | | |
294 | 0 | try |
295 | 0 | { |
296 | 0 | m_xUserImageStorage = m_xUserConfigStorage->openStorageElement( IMAGE_FOLDER, |
297 | 0 | ElementModes::READ ); |
298 | 0 | if ( m_xUserImageStorage.is() ) |
299 | 0 | { |
300 | 0 | m_xUserBitmapsStorage = m_xUserImageStorage->openStorageElement( BITMAPS_FOLDER, |
301 | 0 | ElementModes::READ ); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | catch ( const css::container::NoSuchElementException& ) |
305 | 0 | { |
306 | 0 | } |
307 | 0 | catch ( const css::embed::InvalidStorageException& ) |
308 | 0 | { |
309 | 0 | } |
310 | 0 | catch ( const css::lang::IllegalArgumentException& ) |
311 | 0 | { |
312 | 0 | } |
313 | 0 | catch ( const css::io::IOException& ) |
314 | 0 | { |
315 | 0 | } |
316 | 0 | catch ( const css::embed::StorageWrappedTargetException& ) |
317 | 0 | { |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | | void ImageManagerImpl::implts_loadUserImages( |
322 | | vcl::ImageType nImageType, |
323 | | const uno::Reference< XStorage >& xUserImageStorage, |
324 | | const uno::Reference< XStorage >& xUserBitmapsStorage ) |
325 | 0 | { |
326 | 0 | SolarMutexGuard g; |
327 | |
|
328 | 0 | if ( xUserImageStorage.is() && xUserBitmapsStorage.is() ) |
329 | 0 | { |
330 | 0 | try |
331 | 0 | { |
332 | 0 | uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( IMAGELIST_XML_FILE[nImageType], |
333 | 0 | ElementModes::READ ); |
334 | 0 | uno::Reference< XInputStream > xInputStream = xStream->getInputStream(); |
335 | |
|
336 | 0 | ImageItemDescriptorList aUserImageListInfo; |
337 | 0 | ImagesConfiguration::LoadImages( m_xContext, |
338 | 0 | xInputStream, |
339 | 0 | aUserImageListInfo ); |
340 | 0 | if (!aUserImageListInfo.aImageItemDescriptors.empty()) |
341 | 0 | { |
342 | 0 | sal_Int32 nCount = aUserImageListInfo.aImageItemDescriptors.size(); |
343 | 0 | std::vector< OUString > aUserImagesVector; |
344 | 0 | aUserImagesVector.reserve(nCount); |
345 | 0 | for ( sal_Int32 i=0; i < nCount; i++ ) |
346 | 0 | { |
347 | 0 | const ImageItemDescriptor& rItem = aUserImageListInfo.aImageItemDescriptors[i]; |
348 | 0 | aUserImagesVector.push_back( rItem.aCommandURL ); |
349 | 0 | } |
350 | |
|
351 | 0 | uno::Reference< XStream > xBitmapStream = xUserBitmapsStorage->openStreamElement( |
352 | 0 | BITMAP_FILE_NAMES[nImageType], |
353 | 0 | ElementModes::READ ); |
354 | |
|
355 | 0 | if ( xBitmapStream.is() ) |
356 | 0 | { |
357 | 0 | Bitmap aUserBitmap; |
358 | 0 | { |
359 | 0 | std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream )); |
360 | 0 | vcl::PngImageReader aPngReader( *pSvStream ); |
361 | 0 | aUserBitmap = aPngReader.read(); |
362 | 0 | } |
363 | | |
364 | | // Delete old image list and create a new one from the read bitmap |
365 | 0 | m_pUserImageList[nImageType].reset(new ImageList()); |
366 | 0 | m_pUserImageList[nImageType]->InsertFromHorizontalStrip |
367 | 0 | ( aUserBitmap, aUserImagesVector ); |
368 | 0 | return; |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | 0 | catch ( const css::container::NoSuchElementException& ) |
373 | 0 | { |
374 | 0 | } |
375 | 0 | catch ( const css::embed::InvalidStorageException& ) |
376 | 0 | { |
377 | 0 | } |
378 | 0 | catch ( const css::lang::IllegalArgumentException& ) |
379 | 0 | { |
380 | 0 | } |
381 | 0 | catch ( const css::io::IOException& ) |
382 | 0 | { |
383 | 0 | } |
384 | 0 | catch ( const css::embed::StorageWrappedTargetException& ) |
385 | 0 | { |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | // Destroy old image list - create a new empty one |
390 | 0 | m_pUserImageList[nImageType].reset(new ImageList); |
391 | 0 | } |
392 | | |
393 | | bool ImageManagerImpl::implts_storeUserImages( |
394 | | vcl::ImageType nImageType, |
395 | | const uno::Reference< XStorage >& xUserImageStorage, |
396 | | const uno::Reference< XStorage >& xUserBitmapsStorage ) |
397 | 0 | { |
398 | 0 | SolarMutexGuard g; |
399 | |
|
400 | 0 | if ( !m_bModified ) |
401 | 0 | return false; |
402 | | |
403 | 0 | ImageList* pImageList = implts_getUserImageList( nImageType ); |
404 | 0 | if ( pImageList->GetImageCount() > 0 ) |
405 | 0 | { |
406 | 0 | ImageItemDescriptorList aUserImageListInfo; |
407 | |
|
408 | 0 | for ( sal_uInt16 i=0; i < pImageList->GetImageCount(); i++ ) |
409 | 0 | { |
410 | 0 | ImageItemDescriptor aItem; |
411 | 0 | aItem.nIndex = i; |
412 | 0 | aItem.aCommandURL = pImageList->GetImageName(i); |
413 | 0 | aUserImageListInfo.aImageItemDescriptors.push_back( aItem ); |
414 | 0 | } |
415 | 0 | aUserImageListInfo.aURL = "Bitmaps/" + BITMAP_FILE_NAMES[nImageType]; |
416 | |
|
417 | 0 | uno::Reference< XTransactedObject > xTransaction; |
418 | 0 | uno::Reference< XOutputStream > xOutputStream; |
419 | 0 | uno::Reference< XStream > xStream = xUserImageStorage->openStreamElement( IMAGELIST_XML_FILE[nImageType], |
420 | 0 | ElementModes::WRITE|ElementModes::TRUNCATE ); |
421 | 0 | if ( xStream.is() ) |
422 | 0 | { |
423 | 0 | uno::Reference< XStream > xBitmapStream = |
424 | 0 | xUserBitmapsStorage->openStreamElement( BITMAP_FILE_NAMES[nImageType], |
425 | 0 | ElementModes::WRITE|ElementModes::TRUNCATE ); |
426 | 0 | if ( xBitmapStream.is() ) |
427 | 0 | { |
428 | 0 | { |
429 | 0 | std::unique_ptr<SvStream> pSvStream(utl::UcbStreamHelper::CreateStream( xBitmapStream )); |
430 | 0 | vcl::PngImageWriter aPngWriter( *pSvStream ); |
431 | 0 | auto rBitmap = pImageList->GetAsHorizontalStrip(); |
432 | 0 | aPngWriter.write( rBitmap ); |
433 | 0 | } |
434 | | |
435 | | // Commit user bitmaps storage |
436 | 0 | xTransaction.set( xUserBitmapsStorage, UNO_QUERY ); |
437 | 0 | if ( xTransaction.is() ) |
438 | 0 | xTransaction->commit(); |
439 | 0 | } |
440 | |
|
441 | 0 | xOutputStream = xStream->getOutputStream(); |
442 | 0 | if ( xOutputStream.is() ) |
443 | 0 | ImagesConfiguration::StoreImages( m_xContext, xOutputStream, aUserImageListInfo ); |
444 | | |
445 | | // Commit user image storage |
446 | 0 | xTransaction.set( xUserImageStorage, UNO_QUERY ); |
447 | 0 | if ( xTransaction.is() ) |
448 | 0 | xTransaction->commit(); |
449 | 0 | } |
450 | |
|
451 | 0 | return true; |
452 | 0 | } |
453 | 0 | else |
454 | 0 | { |
455 | | // Remove the streams from the storage, if we have no data. We have to catch |
456 | | // the NoSuchElementException as it can be possible that there is no stream at all! |
457 | 0 | try |
458 | 0 | { |
459 | 0 | xUserImageStorage->removeElement( IMAGELIST_XML_FILE[nImageType] ); |
460 | 0 | } |
461 | 0 | catch ( const css::container::NoSuchElementException& ) |
462 | 0 | { |
463 | 0 | } |
464 | |
|
465 | 0 | try |
466 | 0 | { |
467 | 0 | xUserBitmapsStorage->removeElement( BITMAP_FILE_NAMES[nImageType] ); |
468 | 0 | } |
469 | 0 | catch ( const css::container::NoSuchElementException& ) |
470 | 0 | { |
471 | 0 | } |
472 | |
|
473 | 0 | uno::Reference< XTransactedObject > xTransaction; |
474 | | |
475 | | // Commit user image storage |
476 | 0 | xTransaction.set( xUserImageStorage, UNO_QUERY ); |
477 | 0 | if ( xTransaction.is() ) |
478 | 0 | xTransaction->commit(); |
479 | | |
480 | | // Commit user bitmaps storage |
481 | 0 | xTransaction.set( xUserBitmapsStorage, UNO_QUERY ); |
482 | 0 | if ( xTransaction.is() ) |
483 | 0 | xTransaction->commit(); |
484 | |
|
485 | 0 | return true; |
486 | 0 | } |
487 | | |
488 | 0 | return false; |
489 | 0 | } |
490 | | |
491 | | const rtl::Reference< GlobalImageList >& ImageManagerImpl::implts_getGlobalImageList() |
492 | 0 | { |
493 | 0 | SolarMutexGuard g; |
494 | |
|
495 | 0 | if ( !m_pGlobalImageList.is() ) |
496 | 0 | m_pGlobalImageList = getGlobalImageList( m_xContext ); |
497 | 0 | return m_pGlobalImageList; |
498 | 0 | } |
499 | | |
500 | | CmdImageList* ImageManagerImpl::implts_getDefaultImageList() |
501 | 0 | { |
502 | 0 | SolarMutexGuard g; |
503 | |
|
504 | 0 | if ( !m_pDefaultImageList ) |
505 | 0 | m_pDefaultImageList.reset(new CmdImageList( m_xContext, m_aModuleIdentifier )); |
506 | |
|
507 | 0 | return m_pDefaultImageList.get(); |
508 | 0 | } |
509 | | |
510 | | ImageManagerImpl::ImageManagerImpl( uno::Reference< uno::XComponentContext > xContext, ::cppu::OWeakObject* pOwner, bool _bUseGlobal ) : |
511 | 0 | m_xContext(std::move( xContext )) |
512 | 0 | , m_pOwner(pOwner) |
513 | 0 | , m_aResourceString( u"private:resource/images/moduleimages"_ustr ) |
514 | 0 | , m_bUseGlobal(_bUseGlobal) |
515 | 0 | , m_bReadOnly( true ) |
516 | 0 | , m_bInitialized( false ) |
517 | 0 | , m_bModified( false ) |
518 | 0 | , m_bDisposed( false ) |
519 | 0 | , m_bShouldReloadRWOnStore( false ) |
520 | 0 | { |
521 | 0 | for ( vcl::ImageType n : o3tl::enumrange<vcl::ImageType>() ) |
522 | 0 | { |
523 | 0 | m_pUserImageList[n] = nullptr; |
524 | 0 | m_bUserImageListModified[n] = false; |
525 | 0 | } |
526 | 0 | } |
527 | | |
528 | | ImageManagerImpl::~ImageManagerImpl() |
529 | 0 | { |
530 | 0 | clear(); |
531 | 0 | } |
532 | | |
533 | | void ImageManagerImpl::dispose() |
534 | 0 | { |
535 | 0 | uno::Reference< uno::XInterface > xOwner(m_pOwner); |
536 | 0 | css::lang::EventObject aEvent( xOwner ); |
537 | 0 | { |
538 | 0 | std::unique_lock aGuard(m_mutex); |
539 | 0 | m_aEventListeners.disposeAndClear( aGuard, aEvent ); |
540 | 0 | } |
541 | 0 | { |
542 | 0 | std::unique_lock aGuard(m_mutex); |
543 | 0 | m_aConfigListeners.disposeAndClear( aGuard, aEvent ); |
544 | 0 | } |
545 | |
|
546 | 0 | { |
547 | 0 | SolarMutexGuard g; |
548 | 0 | m_xUserConfigStorage.clear(); |
549 | 0 | m_xUserImageStorage.clear(); |
550 | 0 | m_xUserRootCommit.clear(); |
551 | 0 | m_bModified = false; |
552 | 0 | m_bDisposed = true; |
553 | | |
554 | | // delete user and default image list on dispose |
555 | 0 | for (auto& n : m_pUserImageList) |
556 | 0 | { |
557 | 0 | n.reset(); |
558 | 0 | } |
559 | 0 | m_pDefaultImageList.reset(); |
560 | 0 | } |
561 | |
|
562 | 0 | } |
563 | | void ImageManagerImpl::addEventListener( const uno::Reference< XEventListener >& xListener ) |
564 | 0 | { |
565 | 0 | { |
566 | 0 | SolarMutexGuard g; |
567 | | |
568 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
569 | 0 | if ( m_bDisposed ) |
570 | 0 | throw DisposedException(); |
571 | 0 | } |
572 | | |
573 | 0 | std::unique_lock aGuard(m_mutex); |
574 | 0 | m_aEventListeners.addInterface( aGuard, xListener ); |
575 | 0 | } |
576 | | |
577 | | void ImageManagerImpl::removeEventListener( const uno::Reference< XEventListener >& xListener ) |
578 | 0 | { |
579 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
580 | 0 | std::unique_lock aGuard(m_mutex); |
581 | 0 | m_aEventListeners.removeInterface( aGuard, xListener ); |
582 | 0 | } |
583 | | |
584 | | // XInitialization |
585 | | void ImageManagerImpl::initialize( const Sequence< Any >& aArguments ) |
586 | 0 | { |
587 | 0 | SolarMutexGuard g; |
588 | |
|
589 | 0 | if ( m_bInitialized ) |
590 | 0 | return; |
591 | | |
592 | 0 | for ( const Any& rArg : aArguments ) |
593 | 0 | { |
594 | 0 | PropertyValue aPropValue; |
595 | 0 | if ( rArg >>= aPropValue ) |
596 | 0 | { |
597 | 0 | if ( aPropValue.Name == "UserConfigStorage" ) |
598 | 0 | { |
599 | 0 | aPropValue.Value >>= m_xUserConfigStorage; |
600 | 0 | } |
601 | 0 | else if ( aPropValue.Name == "ModuleIdentifier" ) |
602 | 0 | { |
603 | 0 | aPropValue.Value >>= m_aModuleIdentifier; |
604 | 0 | } |
605 | 0 | else if ( aPropValue.Name == "UserRootCommit" ) |
606 | 0 | { |
607 | 0 | aPropValue.Value >>= m_xUserRootCommit; |
608 | 0 | } |
609 | 0 | } |
610 | 0 | } |
611 | |
|
612 | 0 | if ( m_xUserConfigStorage.is() ) |
613 | 0 | { |
614 | 0 | uno::Reference< XPropertySet > xPropSet( m_xUserConfigStorage, UNO_QUERY ); |
615 | 0 | if ( xPropSet.is() ) |
616 | 0 | { |
617 | 0 | tools::Long nOpenMode = 0; |
618 | 0 | if ( xPropSet->getPropertyValue(u"OpenMode"_ustr) >>= nOpenMode ) { |
619 | 0 | m_bReadOnly = !( nOpenMode & ElementModes::WRITE ); |
620 | 0 | m_bShouldReloadRWOnStore = !m_bReadOnly; |
621 | 0 | } |
622 | 0 | } |
623 | 0 | } |
624 | |
|
625 | 0 | implts_initialize(); |
626 | |
|
627 | 0 | m_bInitialized = true; |
628 | 0 | } |
629 | | |
630 | | // XImageManagerImpl |
631 | | void ImageManagerImpl::reset() |
632 | 0 | { |
633 | 0 | SolarMutexGuard g; |
634 | | |
635 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
636 | 0 | if ( m_bDisposed ) |
637 | 0 | throw DisposedException(); |
638 | | |
639 | 0 | std::vector< OUString > aUserImageNames; |
640 | |
|
641 | 0 | for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() ) |
642 | 0 | { |
643 | 0 | aUserImageNames.clear(); |
644 | 0 | ImageList* pImageList = implts_getUserImageList(i); |
645 | 0 | pImageList->GetImageNames( aUserImageNames ); |
646 | |
|
647 | 0 | Sequence< OUString > aRemoveList( comphelper::containerToSequence(aUserImageNames) ); |
648 | | |
649 | | // Remove images |
650 | 0 | removeImages( sal_Int16( i ), aRemoveList ); |
651 | 0 | m_bUserImageListModified[i] = true; |
652 | 0 | } |
653 | |
|
654 | 0 | m_bModified = true; |
655 | 0 | } |
656 | | |
657 | | Sequence< OUString > ImageManagerImpl::getAllImageNames( ::sal_Int16 nImageType ) |
658 | 0 | { |
659 | 0 | SolarMutexGuard g; |
660 | | |
661 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
662 | 0 | if ( m_bDisposed ) |
663 | 0 | throw DisposedException(); |
664 | | |
665 | 0 | std::unordered_set< OUString > aImageCmdNames; |
666 | |
|
667 | 0 | auto nIndex = implts_convertImageTypeToIndex(nImageType); |
668 | 0 | if (!nIndex.has_value()) |
669 | 0 | throw IllegalArgumentException(); |
670 | | |
671 | 0 | sal_uInt32 i( 0 ); |
672 | 0 | if ( m_bUseGlobal ) |
673 | 0 | { |
674 | 0 | rtl::Reference< GlobalImageList > rGlobalImageList = implts_getGlobalImageList(); |
675 | |
|
676 | 0 | const std::vector< OUString >& rGlobalImageNameVector = rGlobalImageList->getImageCommandNames(); |
677 | 0 | const sal_uInt32 nGlobalCount = rGlobalImageNameVector.size(); |
678 | 0 | for ( i = 0; i < nGlobalCount; i++ ) |
679 | 0 | aImageCmdNames.insert( rGlobalImageNameVector[i] ); |
680 | |
|
681 | 0 | const std::vector< OUString >& rModuleImageNameVector = implts_getDefaultImageList()->getImageCommandNames(); |
682 | 0 | const sal_uInt32 nModuleCount = rModuleImageNameVector.size(); |
683 | 0 | for ( i = 0; i < nModuleCount; i++ ) |
684 | 0 | aImageCmdNames.insert( rModuleImageNameVector[i] ); |
685 | 0 | } |
686 | |
|
687 | 0 | ImageList* pImageList = implts_getUserImageList(std::get<0>(*nIndex)); |
688 | 0 | std::vector< OUString > rUserImageNames; |
689 | 0 | pImageList->GetImageNames( rUserImageNames ); |
690 | 0 | const sal_uInt32 nUserCount = rUserImageNames.size(); |
691 | 0 | for ( i = 0; i < nUserCount; i++ ) |
692 | 0 | aImageCmdNames.insert( rUserImageNames[i] ); |
693 | |
|
694 | 0 | return comphelper::containerToSequence( aImageCmdNames ); |
695 | 0 | } |
696 | | |
697 | | bool ImageManagerImpl::hasImage( ::sal_Int16 nImageType, const OUString& aCommandURL ) |
698 | 0 | { |
699 | 0 | SolarMutexGuard g; |
700 | | |
701 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
702 | 0 | if ( m_bDisposed ) |
703 | 0 | throw DisposedException(); |
704 | | |
705 | 0 | auto nIndex = implts_convertImageTypeToIndex(nImageType); |
706 | 0 | if (!nIndex.has_value()) |
707 | 0 | throw IllegalArgumentException(); |
708 | | |
709 | 0 | if (m_bUseGlobal |
710 | 0 | && implts_getGlobalImageList()->hasImage(std::get<0>(*nIndex), std::get<1>(*nIndex), |
711 | 0 | aCommandURL)) |
712 | 0 | return true; |
713 | 0 | else |
714 | 0 | { |
715 | 0 | if (m_bUseGlobal |
716 | 0 | && implts_getDefaultImageList()->hasImage(std::get<0>(*nIndex), std::get<1>(*nIndex), |
717 | 0 | aCommandURL)) |
718 | 0 | return true; |
719 | 0 | else |
720 | 0 | { |
721 | | // User layer |
722 | 0 | ImageList* pImageList = implts_getUserImageList(std::get<0>(*nIndex)); |
723 | 0 | if ( pImageList ) |
724 | 0 | return ( pImageList->GetImagePos( aCommandURL ) != IMAGELIST_IMAGE_NOTFOUND ); |
725 | 0 | } |
726 | 0 | } |
727 | | |
728 | 0 | return false; |
729 | 0 | } |
730 | | |
731 | | namespace |
732 | | { |
733 | | css::uno::Reference< css::graphic::XGraphic > GetXGraphic(const Image &rImage) |
734 | 0 | { |
735 | 0 | return Graphic(rImage).GetXGraphic(); |
736 | 0 | } |
737 | | } |
738 | | |
739 | | Sequence< uno::Reference< XGraphic > > ImageManagerImpl::getImages( |
740 | | ::sal_Int16 nImageType, |
741 | | const Sequence< OUString >& aCommandURLSequence ) |
742 | 0 | { |
743 | 0 | SolarMutexGuard g; |
744 | | |
745 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
746 | 0 | if ( m_bDisposed ) |
747 | 0 | throw DisposedException(); |
748 | | |
749 | 0 | auto nIndex = implts_convertImageTypeToIndex(nImageType); |
750 | 0 | if (!nIndex.has_value()) |
751 | 0 | throw IllegalArgumentException(); |
752 | | |
753 | 0 | Sequence< uno::Reference< XGraphic > > aGraphSeq( aCommandURLSequence.getLength() ); |
754 | |
|
755 | 0 | rtl::Reference< GlobalImageList > rGlobalImageList; |
756 | 0 | CmdImageList* pDefaultImageList = nullptr; |
757 | 0 | if ( m_bUseGlobal ) |
758 | 0 | { |
759 | 0 | rGlobalImageList = implts_getGlobalImageList(); |
760 | 0 | pDefaultImageList = implts_getDefaultImageList(); |
761 | 0 | } |
762 | 0 | ImageList* pUserImageList = implts_getUserImageList(std::get<0>(*nIndex)); |
763 | | |
764 | | // We have to search our image list in the following order: |
765 | | // 1. user image list (read/write) |
766 | | // 2. module image list (read) |
767 | | // 3. global image list (read) |
768 | 0 | auto aGraphSeqRange = asNonConstRange(aGraphSeq); |
769 | 0 | sal_Int32 n = 0; |
770 | 0 | for ( const OUString& rURL : aCommandURLSequence ) |
771 | 0 | { |
772 | 0 | Image aImage = pUserImageList->GetImage( rURL ); |
773 | 0 | if ( !aImage && m_bUseGlobal ) |
774 | 0 | { |
775 | 0 | aImage = pDefaultImageList->getImageFromCommandURL(std::get<0>(*nIndex), |
776 | 0 | std::get<1>(*nIndex), rURL); |
777 | 0 | if (!aImage) |
778 | 0 | aImage = rGlobalImageList->getImageFromCommandURL(std::get<0>(*nIndex), |
779 | 0 | std::get<1>(*nIndex), rURL); |
780 | 0 | } |
781 | | |
782 | | // tdf#70102: Writing direction specializations are always optional. Suppress |
783 | | // missing image file warnings on load. |
784 | 0 | aImage.SetOptional(std::get<1>(*nIndex) != vcl::ImageWritingDirection::DontCare); |
785 | |
|
786 | 0 | aGraphSeqRange[n++] = GetXGraphic(aImage); |
787 | 0 | } |
788 | |
|
789 | 0 | return aGraphSeq; |
790 | 0 | } |
791 | | |
792 | | void ImageManagerImpl::replaceImages( |
793 | | ::sal_Int16 nImageType, |
794 | | const Sequence< OUString >& aCommandURLSequence, |
795 | | const Sequence< uno::Reference< XGraphic > >& aGraphicsSequence ) |
796 | 0 | { |
797 | 0 | rtl::Reference<GraphicNameAccess> pInsertedImages; |
798 | 0 | rtl::Reference<GraphicNameAccess> pReplacedImages; |
799 | |
|
800 | 0 | { |
801 | 0 | SolarMutexGuard g; |
802 | | |
803 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
804 | 0 | if ( m_bDisposed ) |
805 | 0 | throw DisposedException(); |
806 | | |
807 | 0 | auto nIndex = implts_convertImageTypeToIndex(nImageType); |
808 | 0 | if ((aCommandURLSequence.getLength() != aGraphicsSequence.getLength()) |
809 | 0 | || (!nIndex.has_value())) |
810 | 0 | throw IllegalArgumentException(); |
811 | | |
812 | 0 | if ( m_bReadOnly ) |
813 | 0 | throw IllegalAccessException(); |
814 | | |
815 | 0 | ImageList* pImageList = implts_getUserImageList(std::get<0>(*nIndex)); |
816 | |
|
817 | 0 | uno::Reference< XGraphic > xGraphic; |
818 | 0 | for ( sal_Int32 i = 0; i < aCommandURLSequence.getLength(); i++ ) |
819 | 0 | { |
820 | | // Check size and scale. If we don't have any graphics ignore it |
821 | 0 | if (!implts_checkAndScaleGraphic(xGraphic, aGraphicsSequence[i], std::get<0>(*nIndex))) |
822 | 0 | continue; |
823 | | |
824 | 0 | sal_uInt16 nPos = pImageList->GetImagePos( aCommandURLSequence[i] ); |
825 | 0 | if ( nPos == IMAGELIST_IMAGE_NOTFOUND ) |
826 | 0 | { |
827 | 0 | pImageList->AddImage(aCommandURLSequence[i], Image(xGraphic)); |
828 | 0 | if ( !pInsertedImages ) |
829 | 0 | pInsertedImages = new GraphicNameAccess(); |
830 | 0 | pInsertedImages->addElement( aCommandURLSequence[i], xGraphic ); |
831 | 0 | } |
832 | 0 | else |
833 | 0 | { |
834 | 0 | pImageList->ReplaceImage(aCommandURLSequence[i], Image(xGraphic)); |
835 | 0 | if ( !pReplacedImages ) |
836 | 0 | pReplacedImages = new GraphicNameAccess(); |
837 | 0 | pReplacedImages->addElement( aCommandURLSequence[i], xGraphic ); |
838 | 0 | } |
839 | 0 | } |
840 | |
|
841 | 0 | if (( pInsertedImages != nullptr ) || ( pReplacedImages != nullptr )) |
842 | 0 | { |
843 | 0 | m_bModified = true; |
844 | 0 | m_bUserImageListModified[std::get<0>(*nIndex)] = true; |
845 | 0 | } |
846 | 0 | } |
847 | | |
848 | 0 | uno::Reference< uno::XInterface > xOwner(m_pOwner); |
849 | | // Notify listeners |
850 | 0 | if ( pInsertedImages != nullptr ) |
851 | 0 | { |
852 | 0 | ConfigurationEvent aInsertEvent; |
853 | 0 | aInsertEvent.aInfo <<= nImageType; |
854 | 0 | aInsertEvent.Accessor <<= xOwner; |
855 | 0 | aInsertEvent.Source = xOwner; |
856 | 0 | aInsertEvent.ResourceURL = m_aResourceString; |
857 | 0 | aInsertEvent.Element <<= uno::Reference< XNameAccess >(pInsertedImages); |
858 | 0 | implts_notifyContainerListener( aInsertEvent, NotifyOp_Insert ); |
859 | 0 | } |
860 | 0 | if ( pReplacedImages != nullptr ) |
861 | 0 | { |
862 | 0 | ConfigurationEvent aReplaceEvent; |
863 | 0 | aReplaceEvent.aInfo <<= nImageType; |
864 | 0 | aReplaceEvent.Accessor <<= xOwner; |
865 | 0 | aReplaceEvent.Source = std::move(xOwner); |
866 | 0 | aReplaceEvent.ResourceURL = m_aResourceString; |
867 | 0 | aReplaceEvent.ReplacedElement = Any(); |
868 | 0 | aReplaceEvent.Element <<= uno::Reference< XNameAccess >(pReplacedImages); |
869 | 0 | implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace ); |
870 | 0 | } |
871 | 0 | } |
872 | | |
873 | | void ImageManagerImpl::removeImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence ) |
874 | 0 | { |
875 | 0 | rtl::Reference<GraphicNameAccess> pRemovedImages; |
876 | 0 | rtl::Reference<GraphicNameAccess> pReplacedImages; |
877 | |
|
878 | 0 | { |
879 | 0 | SolarMutexGuard g; |
880 | | |
881 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
882 | 0 | if ( m_bDisposed ) |
883 | 0 | throw DisposedException(); |
884 | | |
885 | 0 | auto nIndex = implts_convertImageTypeToIndex(nImageType); |
886 | 0 | if (!nIndex.has_value()) |
887 | 0 | throw IllegalArgumentException(); |
888 | | |
889 | 0 | if ( m_bReadOnly ) |
890 | 0 | throw IllegalAccessException(); |
891 | | |
892 | 0 | rtl::Reference< GlobalImageList > rGlobalImageList; |
893 | 0 | CmdImageList* pDefaultImageList = nullptr; |
894 | 0 | if ( m_bUseGlobal ) |
895 | 0 | { |
896 | 0 | rGlobalImageList = implts_getGlobalImageList(); |
897 | 0 | pDefaultImageList = implts_getDefaultImageList(); |
898 | 0 | } |
899 | 0 | ImageList* pImageList = implts_getUserImageList(std::get<0>(*nIndex)); |
900 | 0 | uno::Reference<XGraphic> xEmptyGraphic; |
901 | |
|
902 | 0 | for ( const OUString& rURL : aCommandURLSequence ) |
903 | 0 | { |
904 | 0 | sal_uInt16 nPos = pImageList->GetImagePos( rURL ); |
905 | 0 | if ( nPos != IMAGELIST_IMAGE_NOTFOUND ) |
906 | 0 | { |
907 | 0 | sal_uInt16 nId = pImageList->GetImageId( nPos ); |
908 | 0 | pImageList->RemoveImage( nId ); |
909 | |
|
910 | 0 | if ( m_bUseGlobal ) |
911 | 0 | { |
912 | | // Check, if we have an image in our module/global image list. If we find one => |
913 | | // this is a replace instead of a remove operation! |
914 | 0 | Image aNewImage = pDefaultImageList->getImageFromCommandURL( |
915 | 0 | std::get<0>(*nIndex), std::get<1>(*nIndex), rURL); |
916 | 0 | if (!aNewImage) |
917 | 0 | aNewImage = rGlobalImageList->getImageFromCommandURL( |
918 | 0 | std::get<0>(*nIndex), std::get<1>(*nIndex), rURL); |
919 | 0 | if ( !aNewImage ) |
920 | 0 | { |
921 | 0 | if ( !pRemovedImages ) |
922 | 0 | pRemovedImages = new GraphicNameAccess(); |
923 | 0 | pRemovedImages->addElement( rURL, xEmptyGraphic ); |
924 | 0 | } |
925 | 0 | else |
926 | 0 | { |
927 | 0 | if ( !pReplacedImages ) |
928 | 0 | pReplacedImages = new GraphicNameAccess(); |
929 | 0 | pReplacedImages->addElement(rURL, GetXGraphic(aNewImage)); |
930 | 0 | } |
931 | 0 | } // if ( m_bUseGlobal ) |
932 | 0 | else |
933 | 0 | { |
934 | 0 | if ( !pRemovedImages ) |
935 | 0 | pRemovedImages = new GraphicNameAccess(); |
936 | 0 | pRemovedImages->addElement( rURL, xEmptyGraphic ); |
937 | 0 | } |
938 | 0 | } |
939 | 0 | } |
940 | |
|
941 | 0 | if (( pReplacedImages != nullptr ) || ( pRemovedImages != nullptr )) |
942 | 0 | { |
943 | 0 | m_bModified = true; |
944 | 0 | m_bUserImageListModified[std::get<0>(*nIndex)] = true; |
945 | 0 | } |
946 | 0 | } |
947 | | |
948 | | // Notify listeners |
949 | 0 | uno::Reference< uno::XInterface > xOwner(m_pOwner); |
950 | 0 | if ( pRemovedImages != nullptr ) |
951 | 0 | { |
952 | 0 | ConfigurationEvent aRemoveEvent; |
953 | 0 | aRemoveEvent.aInfo <<= nImageType; |
954 | 0 | aRemoveEvent.Accessor <<= xOwner; |
955 | 0 | aRemoveEvent.Source = xOwner; |
956 | 0 | aRemoveEvent.ResourceURL = m_aResourceString; |
957 | 0 | aRemoveEvent.Element <<= uno::Reference< XNameAccess >(pRemovedImages); |
958 | 0 | implts_notifyContainerListener( aRemoveEvent, NotifyOp_Remove ); |
959 | 0 | } |
960 | 0 | if ( pReplacedImages != nullptr ) |
961 | 0 | { |
962 | 0 | ConfigurationEvent aReplaceEvent; |
963 | 0 | aReplaceEvent.aInfo <<= nImageType; |
964 | 0 | aReplaceEvent.Accessor <<= xOwner; |
965 | 0 | aReplaceEvent.Source = std::move(xOwner); |
966 | 0 | aReplaceEvent.ResourceURL = m_aResourceString; |
967 | 0 | aReplaceEvent.ReplacedElement = Any(); |
968 | 0 | aReplaceEvent.Element <<= uno::Reference< XNameAccess >(pReplacedImages); |
969 | 0 | implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace ); |
970 | 0 | } |
971 | 0 | } |
972 | | |
973 | | void ImageManagerImpl::insertImages( ::sal_Int16 nImageType, const Sequence< OUString >& aCommandURLSequence, const Sequence< uno::Reference< XGraphic > >& aGraphicSequence ) |
974 | 0 | { |
975 | 0 | replaceImages(nImageType,aCommandURLSequence,aGraphicSequence); |
976 | 0 | } |
977 | | |
978 | | // XUIConfigurationPersistence |
979 | | void ImageManagerImpl::reload() |
980 | 0 | { |
981 | 0 | SolarMutexResettableGuard aGuard; |
982 | |
|
983 | 0 | if ( m_bDisposed ) |
984 | 0 | throw DisposedException(); |
985 | | |
986 | 0 | CommandMap aOldUserCmdImageSet; |
987 | 0 | std::vector< OUString > aNewUserCmdImageSet; |
988 | |
|
989 | 0 | if ( !m_bModified ) |
990 | 0 | return; |
991 | | |
992 | 0 | for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() ) |
993 | 0 | { |
994 | 0 | if ( !m_bDisposed && m_bUserImageListModified[i] ) |
995 | 0 | { |
996 | 0 | std::vector< OUString > aOldUserCmdImageVector; |
997 | 0 | ImageList* pImageList = implts_getUserImageList(i); |
998 | 0 | pImageList->GetImageNames( aOldUserCmdImageVector ); |
999 | | |
1000 | | // Fill hash map to speed up search afterwards |
1001 | 0 | sal_uInt32 j( 0 ); |
1002 | 0 | const sal_uInt32 nOldCount = aOldUserCmdImageVector.size(); |
1003 | 0 | for ( j = 0; j < nOldCount; j++ ) |
1004 | 0 | aOldUserCmdImageSet.emplace( aOldUserCmdImageVector[j], false ); |
1005 | | |
1006 | | // Attention: This can make the old image list pointer invalid! |
1007 | 0 | implts_loadUserImages( i, m_xUserImageStorage, m_xUserBitmapsStorage ); |
1008 | 0 | pImageList = implts_getUserImageList(i); |
1009 | 0 | pImageList->GetImageNames( aNewUserCmdImageSet ); |
1010 | |
|
1011 | 0 | rtl::Reference<GraphicNameAccess> pInsertedImages; |
1012 | 0 | rtl::Reference<GraphicNameAccess> pReplacedImages; |
1013 | 0 | rtl::Reference<GraphicNameAccess> pRemovedImages; |
1014 | |
|
1015 | 0 | for (auto const& newUserCmdImage : aNewUserCmdImageSet) |
1016 | 0 | { |
1017 | 0 | CommandMap::iterator pIter = aOldUserCmdImageSet.find(newUserCmdImage); |
1018 | 0 | if ( pIter != aOldUserCmdImageSet.end() ) |
1019 | 0 | { |
1020 | 0 | pIter->second = true; // mark entry as replaced |
1021 | 0 | if ( !pReplacedImages ) |
1022 | 0 | pReplacedImages = new GraphicNameAccess(); |
1023 | 0 | pReplacedImages->addElement( newUserCmdImage, |
1024 | 0 | GetXGraphic(pImageList->GetImage(newUserCmdImage)) ); |
1025 | 0 | } |
1026 | 0 | else |
1027 | 0 | { |
1028 | 0 | if ( !pInsertedImages ) |
1029 | 0 | pInsertedImages = new GraphicNameAccess(); |
1030 | 0 | pInsertedImages->addElement( newUserCmdImage, |
1031 | 0 | GetXGraphic(pImageList->GetImage(newUserCmdImage)) ); |
1032 | 0 | } |
1033 | 0 | } |
1034 | | |
1035 | | // Search map for unmarked entries => they have been removed from the user list |
1036 | | // through this reload operation. |
1037 | | // We have to search the module and global image list! |
1038 | 0 | rtl::Reference< GlobalImageList > rGlobalImageList; |
1039 | 0 | CmdImageList* pDefaultImageList = nullptr; |
1040 | 0 | if ( m_bUseGlobal ) |
1041 | 0 | { |
1042 | 0 | rGlobalImageList = implts_getGlobalImageList(); |
1043 | 0 | pDefaultImageList = implts_getDefaultImageList(); |
1044 | 0 | } |
1045 | 0 | uno::Reference<XGraphic> xEmptyGraphic; |
1046 | 0 | for (auto const& oldUserCmdImage : aOldUserCmdImageSet) |
1047 | 0 | { |
1048 | 0 | if ( !oldUserCmdImage.second ) |
1049 | 0 | { |
1050 | 0 | if ( m_bUseGlobal ) |
1051 | 0 | { |
1052 | 0 | Image aImage = pDefaultImageList->getImageFromCommandURL( |
1053 | 0 | i, vcl::ImageWritingDirection::DontCare, oldUserCmdImage.first); |
1054 | 0 | if (!aImage) |
1055 | 0 | aImage = rGlobalImageList->getImageFromCommandURL( |
1056 | 0 | i, vcl::ImageWritingDirection::DontCare, oldUserCmdImage.first); |
1057 | |
|
1058 | 0 | if ( !aImage ) |
1059 | 0 | { |
1060 | | // No image in the module/global image list => remove user image |
1061 | 0 | if ( !pRemovedImages ) |
1062 | 0 | pRemovedImages = new GraphicNameAccess(); |
1063 | 0 | pRemovedImages->addElement( oldUserCmdImage.first, xEmptyGraphic ); |
1064 | 0 | } |
1065 | 0 | else |
1066 | 0 | { |
1067 | | // Image has been found in the module/global image list => replace user image |
1068 | 0 | if ( !pReplacedImages ) |
1069 | 0 | pReplacedImages = new GraphicNameAccess(); |
1070 | 0 | pReplacedImages->addElement(oldUserCmdImage.first, GetXGraphic(aImage)); |
1071 | 0 | } |
1072 | 0 | } // if ( m_bUseGlobal ) |
1073 | 0 | else |
1074 | 0 | { |
1075 | | // No image in the user image list => remove user image |
1076 | 0 | if ( !pRemovedImages ) |
1077 | 0 | pRemovedImages = new GraphicNameAccess(); |
1078 | 0 | pRemovedImages->addElement( oldUserCmdImage.first, xEmptyGraphic ); |
1079 | 0 | } |
1080 | 0 | } |
1081 | 0 | } |
1082 | |
|
1083 | 0 | aGuard.clear(); |
1084 | | |
1085 | | // Now notify our listeners. Unlock mutex to prevent deadlocks |
1086 | 0 | uno::Reference< uno::XInterface > xOwner(m_pOwner); |
1087 | 0 | if ( pInsertedImages != nullptr ) |
1088 | 0 | { |
1089 | 0 | ConfigurationEvent aInsertEvent; |
1090 | 0 | aInsertEvent.aInfo <<=static_cast<sal_uInt16>(i); |
1091 | 0 | aInsertEvent.Accessor <<= xOwner; |
1092 | 0 | aInsertEvent.Source = xOwner; |
1093 | 0 | aInsertEvent.ResourceURL = m_aResourceString; |
1094 | 0 | aInsertEvent.Element <<= uno::Reference< XNameAccess >( pInsertedImages ); |
1095 | 0 | implts_notifyContainerListener( aInsertEvent, NotifyOp_Insert ); |
1096 | 0 | } |
1097 | 0 | if ( pReplacedImages != nullptr ) |
1098 | 0 | { |
1099 | 0 | ConfigurationEvent aReplaceEvent; |
1100 | 0 | aReplaceEvent.aInfo <<= static_cast<sal_uInt16>(i); |
1101 | 0 | aReplaceEvent.Accessor <<= xOwner; |
1102 | 0 | aReplaceEvent.Source = xOwner; |
1103 | 0 | aReplaceEvent.ResourceURL = m_aResourceString; |
1104 | 0 | aReplaceEvent.ReplacedElement = Any(); |
1105 | 0 | aReplaceEvent.Element <<= uno::Reference< XNameAccess >( pReplacedImages ); |
1106 | 0 | implts_notifyContainerListener( aReplaceEvent, NotifyOp_Replace ); |
1107 | 0 | } |
1108 | 0 | if ( pRemovedImages != nullptr ) |
1109 | 0 | { |
1110 | 0 | ConfigurationEvent aRemoveEvent; |
1111 | 0 | aRemoveEvent.aInfo <<= static_cast<sal_uInt16>(i); |
1112 | 0 | aRemoveEvent.Accessor <<= xOwner; |
1113 | 0 | aRemoveEvent.Source = std::move(xOwner); |
1114 | 0 | aRemoveEvent.ResourceURL = m_aResourceString; |
1115 | 0 | aRemoveEvent.Element <<= uno::Reference< XNameAccess >( pRemovedImages ); |
1116 | 0 | implts_notifyContainerListener( aRemoveEvent, NotifyOp_Remove ); |
1117 | 0 | } |
1118 | |
|
1119 | 0 | aGuard.reset(); |
1120 | 0 | } |
1121 | 0 | } |
1122 | 0 | } |
1123 | | |
1124 | | void ImageManagerImpl::store() |
1125 | 0 | { |
1126 | 0 | SolarMutexGuard g; |
1127 | |
|
1128 | 0 | if ( m_bDisposed ) |
1129 | 0 | throw DisposedException(); |
1130 | | |
1131 | 0 | if ( !m_bModified ) |
1132 | 0 | return; |
1133 | | |
1134 | 0 | if ( m_bShouldReloadRWOnStore ) { |
1135 | 0 | m_bShouldReloadRWOnStore = false; |
1136 | |
|
1137 | 0 | m_xUserBitmapsStorage.clear(); |
1138 | 0 | m_xUserImageStorage.clear(); |
1139 | |
|
1140 | 0 | try |
1141 | 0 | { |
1142 | 0 | uno::Reference< XStorage > xUserImageStorage = |
1143 | 0 | m_xUserConfigStorage->openStorageElement( IMAGE_FOLDER, ElementModes::READWRITE ); |
1144 | 0 | if ( !xUserImageStorage.is() ) |
1145 | 0 | throw css::uno::Exception(); |
1146 | | |
1147 | 0 | uno::Reference< XStorage > xUserBitmapsStorage = |
1148 | 0 | xUserImageStorage->openStorageElement( BITMAPS_FOLDER, ElementModes::READWRITE ); |
1149 | 0 | if ( !xUserBitmapsStorage.is() ) |
1150 | 0 | throw css::uno::Exception(); |
1151 | | |
1152 | 0 | m_xUserImageStorage = std::move( xUserImageStorage ); |
1153 | 0 | m_xUserBitmapsStorage = std::move( xUserBitmapsStorage ); |
1154 | 0 | } catch ( const css::uno::Exception& ) |
1155 | 0 | { |
1156 | 0 | try |
1157 | 0 | { |
1158 | 0 | uno::Reference< XStorage > xUserImageStorage = |
1159 | 0 | m_xUserConfigStorage->openStorageElement( IMAGE_FOLDER, ElementModes::READ ); |
1160 | 0 | if ( !xUserImageStorage.is() ) |
1161 | 0 | return; |
1162 | | |
1163 | 0 | uno::Reference< XStorage > xUserBitmapsStorage = |
1164 | 0 | xUserImageStorage->openStorageElement( BITMAPS_FOLDER, ElementModes::READ ); |
1165 | 0 | if ( !xUserBitmapsStorage.is() ) |
1166 | 0 | return; |
1167 | | |
1168 | 0 | m_xUserImageStorage = std::move( xUserImageStorage ); |
1169 | 0 | m_xUserBitmapsStorage = std::move( xUserBitmapsStorage ); |
1170 | 0 | } catch ( const css::uno::Exception& ) |
1171 | 0 | { |
1172 | 0 | } |
1173 | | |
1174 | 0 | return; |
1175 | 0 | } |
1176 | 0 | } |
1177 | | |
1178 | 0 | bool bWritten( false ); |
1179 | 0 | for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() ) |
1180 | 0 | { |
1181 | 0 | bool bSuccess = implts_storeUserImages(i, m_xUserImageStorage, m_xUserBitmapsStorage ); |
1182 | 0 | if ( bSuccess ) |
1183 | 0 | bWritten = true; |
1184 | 0 | m_bUserImageListModified[i] = false; |
1185 | 0 | } |
1186 | |
|
1187 | 0 | if ( bWritten && |
1188 | 0 | m_xUserConfigStorage.is() ) |
1189 | 0 | { |
1190 | 0 | uno::Reference< XTransactedObject > xUserConfigStorageCommit( m_xUserConfigStorage, UNO_QUERY ); |
1191 | 0 | if ( xUserConfigStorageCommit.is() ) |
1192 | 0 | xUserConfigStorageCommit->commit(); |
1193 | 0 | if ( m_xUserRootCommit.is() ) |
1194 | 0 | m_xUserRootCommit->commit(); |
1195 | 0 | } |
1196 | |
|
1197 | 0 | m_bModified = false; |
1198 | 0 | } |
1199 | | |
1200 | | void ImageManagerImpl::storeToStorage( const uno::Reference< XStorage >& Storage ) |
1201 | 0 | { |
1202 | 0 | SolarMutexGuard g; |
1203 | |
|
1204 | 0 | if ( m_bDisposed ) |
1205 | 0 | throw DisposedException(); |
1206 | | |
1207 | 0 | if ( !(m_bModified && Storage.is()) ) |
1208 | 0 | return; |
1209 | | |
1210 | 0 | tools::Long nModes = ElementModes::READWRITE; |
1211 | |
|
1212 | 0 | uno::Reference< XStorage > xUserImageStorage = Storage->openStorageElement( IMAGE_FOLDER, |
1213 | 0 | nModes ); |
1214 | 0 | if ( !xUserImageStorage.is() ) |
1215 | 0 | return; |
1216 | | |
1217 | 0 | uno::Reference< XStorage > xUserBitmapsStorage = xUserImageStorage->openStorageElement( BITMAPS_FOLDER, |
1218 | 0 | nModes ); |
1219 | 0 | for ( vcl::ImageType i : o3tl::enumrange<vcl::ImageType>() ) |
1220 | 0 | { |
1221 | 0 | implts_getUserImageList(i); |
1222 | 0 | implts_storeUserImages( i, xUserImageStorage, xUserBitmapsStorage ); |
1223 | 0 | } |
1224 | |
|
1225 | 0 | uno::Reference< XTransactedObject > xTransaction( Storage, UNO_QUERY ); |
1226 | 0 | if ( xTransaction.is() ) |
1227 | 0 | xTransaction->commit(); |
1228 | 0 | } |
1229 | | |
1230 | | bool ImageManagerImpl::isModified() const |
1231 | 0 | { |
1232 | 0 | SolarMutexGuard g; |
1233 | 0 | return m_bModified; |
1234 | 0 | } |
1235 | | |
1236 | | bool ImageManagerImpl::isReadOnly() const |
1237 | 0 | { |
1238 | 0 | SolarMutexGuard g; |
1239 | 0 | return m_bReadOnly; |
1240 | 0 | } |
1241 | | // XUIConfiguration |
1242 | | void ImageManagerImpl::addConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener ) |
1243 | 0 | { |
1244 | 0 | { |
1245 | 0 | SolarMutexGuard g; |
1246 | | |
1247 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
1248 | 0 | if ( m_bDisposed ) |
1249 | 0 | throw DisposedException(); |
1250 | 0 | } |
1251 | | |
1252 | 0 | std::unique_lock aGuard(m_mutex); |
1253 | 0 | m_aConfigListeners.addInterface( aGuard, xListener ); |
1254 | 0 | } |
1255 | | |
1256 | | void ImageManagerImpl::removeConfigurationListener( const uno::Reference< css::ui::XUIConfigurationListener >& xListener ) |
1257 | 0 | { |
1258 | | /* SAFE AREA ----------------------------------------------------------------------------------------------- */ |
1259 | 0 | std::unique_lock aGuard(m_mutex); |
1260 | 0 | m_aConfigListeners.removeInterface( aGuard, xListener ); |
1261 | 0 | } |
1262 | | |
1263 | | void ImageManagerImpl::implts_notifyContainerListener( const ConfigurationEvent& aEvent, NotifyOp eOp ) |
1264 | 0 | { |
1265 | 0 | std::unique_lock aGuard(m_mutex); |
1266 | 0 | comphelper::OInterfaceIteratorHelper4 pIterator( aGuard, m_aConfigListeners ); |
1267 | 0 | aGuard.unlock(); |
1268 | 0 | while ( pIterator.hasMoreElements() ) |
1269 | 0 | { |
1270 | 0 | try |
1271 | 0 | { |
1272 | 0 | switch ( eOp ) |
1273 | 0 | { |
1274 | 0 | case NotifyOp_Replace: |
1275 | 0 | pIterator.next()->elementReplaced( aEvent ); |
1276 | 0 | break; |
1277 | 0 | case NotifyOp_Insert: |
1278 | 0 | pIterator.next()->elementInserted( aEvent ); |
1279 | 0 | break; |
1280 | 0 | case NotifyOp_Remove: |
1281 | 0 | pIterator.next()->elementRemoved( aEvent ); |
1282 | 0 | break; |
1283 | 0 | } |
1284 | 0 | } |
1285 | 0 | catch( const css::uno::RuntimeException& ) |
1286 | 0 | { |
1287 | 0 | aGuard.lock(); |
1288 | 0 | pIterator.remove(aGuard); |
1289 | 0 | aGuard.unlock(); |
1290 | 0 | } |
1291 | 0 | } |
1292 | 0 | } |
1293 | | void ImageManagerImpl::clear() |
1294 | 0 | { |
1295 | 0 | SolarMutexGuard g; |
1296 | |
|
1297 | 0 | for (auto & n : m_pUserImageList) |
1298 | 0 | { |
1299 | 0 | n.reset(); |
1300 | 0 | } |
1301 | 0 | } |
1302 | | } // namespace framework |
1303 | | |
1304 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |