Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / appl / imagemgr.cxx
blob08a945328d453cde55e45fe3a68f32d39849992c
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 <sfx2/imagemgr.hxx>
21 #include <com/sun/star/frame/XController.hpp>
22 #include <com/sun/star/ui/XImageManager.hpp>
23 #include <com/sun/star/frame/ModuleManager.hpp>
24 #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
25 #include <com/sun/star/ui/ImageType.hpp>
26 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
28 #include <tools/urlobj.hxx>
29 #include <svtools/imagemgr.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <rtl/ustring.hxx>
33 #include <sfx2/imgmgr.hxx>
34 #include <sfx2/app.hxx>
35 #include <sfx2/unoctitm.hxx>
36 #include <sfx2/dispatch.hxx>
37 #include <sfx2/msg.hxx>
38 #include <sfx2/msgpool.hxx>
39 #include <sfx2/viewfrm.hxx>
40 #include <sfx2/module.hxx>
41 #include <sfx2/objsh.hxx>
42 #include <sfx2/docfac.hxx>
44 #include <unordered_map>
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::frame;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::ui;
52 typedef std::unordered_map< OUString,
53 WeakReference< XImageManager >,
54 OUStringHash,
55 ::std::equal_to< OUString > > ModuleIdToImagegMgr;
58 Image SAL_CALL GetImage(
59 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
60 const OUString& aURL,
61 bool bBig
64 // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
65 if ( !rFrame.is() )
66 return Image();
68 INetURLObject aObj( aURL );
69 INetProtocol nProtocol = aObj.GetProtocol();
71 Reference < XController > xController;
72 Reference < XModel > xModel;
73 if ( rFrame.is() )
74 xController = rFrame->getController();
75 if ( xController.is() )
76 xModel = xController->getModel();
78 OUString aCommandURL( aURL );
79 if ( nProtocol == INetProtocol::Slot )
81 sal_uInt16 nId = ( sal_uInt16 ) aURL.copy(5).toInt32();
82 const SfxSlot* pSlot = 0;
83 if ( xModel.is() )
85 Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
86 Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
87 sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
88 if ( nHandle )
90 SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
91 SfxModule* pModule = pDoc->GetFactory().GetModule();
92 pSlot = pModule->GetSlotPool()->GetSlot( nId );
95 else
96 pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
98 if ( pSlot )
100 aCommandURL = ".uno:";
101 aCommandURL += OUString::createFromAscii( pSlot->GetUnoName() );
103 else
104 aCommandURL.clear();
107 Reference< XImageManager > xDocImgMgr;
108 if ( xModel.is() )
110 Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
111 if ( xSupplier.is() )
113 Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
114 xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
118 sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
119 ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
120 if ( bBig )
121 nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
123 if ( xDocImgMgr.is() )
125 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
126 Sequence< OUString > aImageCmdSeq( 1 );
127 aImageCmdSeq[0] = aCommandURL;
131 aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
132 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
133 Image aImage( xGraphic );
135 if ( !!aImage )
136 return aImage;
138 catch (const Exception&)
143 static WeakReference< XModuleManager2 > m_xModuleManager;
145 Reference< XModuleManager2 > xModuleManager = m_xModuleManager;
147 if ( !xModuleManager.is() )
149 xModuleManager = ModuleManager::create(::comphelper::getProcessComponentContext());
150 m_xModuleManager = xModuleManager;
155 if ( !aCommandURL.isEmpty() )
157 Reference< XImageManager > xModuleImageManager;
158 OUString aModuleId = xModuleManager->identify( rFrame );
160 static ModuleIdToImagegMgr m_aModuleIdToImageMgrMap;
162 ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
163 if ( pIter != m_aModuleIdToImageMgrMap.end() )
164 xModuleImageManager = pIter->second;
165 else
167 static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
169 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
171 if ( !xModuleCfgMgrSupplier.is() )
173 xModuleCfgMgrSupplier = theModuleUIConfigurationManagerSupplier::get(
174 ::comphelper::getProcessComponentContext() );
176 m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
179 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
180 xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
181 m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
184 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
185 Sequence< OUString > aImageCmdSeq( 1 );
186 aImageCmdSeq[0] = aCommandURL;
188 aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
190 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
191 Image aImage( xGraphic );
193 if ( !!aImage )
194 return aImage;
195 else if ( nProtocol != INetProtocol::Uno && nProtocol != INetProtocol::Slot )
196 return SvFileInformationManager::GetImageNoDefault( aObj, bBig );
199 catch (const Exception&)
203 return Image();
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */