Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / appl / imagemgr.cxx
blobcf62e9e7770c66881e286075a8000bdc66d18c32
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sfx2/imagemgr.hxx"
30 #include <com/sun/star/frame/XController.hpp>
31 #include <com/sun/star/ui/XImageManager.hpp>
32 #include <com/sun/star/frame/XModuleManager.hpp>
33 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
34 #include <com/sun/star/ui/ImageType.hpp>
35 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
37 #include <tools/urlobj.hxx>
38 #include <svtools/imagemgr.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <rtl/ustring.hxx>
41 #include <rtl/logfile.hxx>
43 #include "sfx2/imgmgr.hxx"
44 #include <sfx2/app.hxx>
45 #include <sfx2/unoctitm.hxx>
46 #include <sfx2/dispatch.hxx>
47 #include <sfx2/msg.hxx>
48 #include <sfx2/msgpool.hxx>
49 #include <sfx2/viewfrm.hxx>
50 #include <sfx2/module.hxx>
51 #include <sfx2/objsh.hxx>
52 #include <sfx2/docfac.hxx>
54 #include <boost/unordered_map.hpp>
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::frame;
58 using namespace ::com::sun::star::lang;
59 using namespace ::com::sun::star::util;
60 using namespace ::com::sun::star::ui;
61 using namespace ::com::sun::star::frame;
63 typedef boost::unordered_map< ::rtl::OUString,
64 WeakReference< XImageManager >,
65 ::rtl::OUStringHash,
66 ::std::equal_to< ::rtl::OUString > > ModuleIdToImagegMgr;
69 Image SAL_CALL GetImage(
70 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
71 const ::rtl::OUString& aURL,
72 bool bBig
75 // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
76 if ( !rFrame.is() )
77 return Image();
79 INetURLObject aObj( aURL );
80 INetProtocol nProtocol = aObj.GetProtocol();
82 Reference < XController > xController;
83 Reference < XModel > xModel;
84 if ( rFrame.is() )
85 xController = rFrame->getController();
86 if ( xController.is() )
87 xModel = xController->getModel();
89 rtl::OUString aCommandURL( aURL );
90 if ( nProtocol == INET_PROT_SLOT )
92 sal_uInt16 nId = ( sal_uInt16 ) String(aURL).Copy(5).ToInt32();
93 const SfxSlot* pSlot = 0;
94 if ( xModel.is() )
96 Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
97 Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
98 sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
99 if ( nHandle )
101 SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
102 SfxModule* pModule = pDoc->GetFactory().GetModule();
103 pSlot = pModule->GetSlotPool()->GetSlot( nId );
106 else
107 pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
109 if ( pSlot )
111 aCommandURL = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
112 aCommandURL += rtl::OUString::createFromAscii( pSlot->GetUnoName() );
114 else
115 aCommandURL = rtl::OUString();
118 Reference< XImageManager > xDocImgMgr;
119 if ( xModel.is() )
121 Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
122 if ( xSupplier.is() )
124 Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
125 xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
129 sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
130 ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
131 if ( bBig )
132 nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
134 if ( xDocImgMgr.is() )
136 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
137 Sequence< rtl::OUString > aImageCmdSeq( 1 );
138 aImageCmdSeq[0] = aCommandURL;
142 aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
143 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
144 Image aImage( xGraphic );
146 if ( !!aImage )
147 return aImage;
149 catch (const Exception&)
154 static WeakReference< XModuleManager > m_xModuleManager;
156 Reference< XModuleManager > xModuleManager = m_xModuleManager;
158 if ( !xModuleManager.is() )
160 xModuleManager = Reference< XModuleManager >(
161 ::comphelper::getProcessServiceFactory()->createInstance(
162 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
163 "com.sun.star.frame.ModuleManager" ))),
164 UNO_QUERY );
165 m_xModuleManager = xModuleManager;
170 if ( !aCommandURL.isEmpty() )
172 Reference< XImageManager > xModuleImageManager;
173 rtl::OUString aModuleId = xModuleManager->identify( rFrame );
175 static ModuleIdToImagegMgr m_aModuleIdToImageMgrMap;
177 ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
178 if ( pIter != m_aModuleIdToImageMgrMap.end() )
179 xModuleImageManager = pIter->second;
180 else
182 static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
184 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
186 if ( !xModuleCfgMgrSupplier.is() )
188 xModuleCfgMgrSupplier = Reference< XModuleUIConfigurationManagerSupplier >(
189 ::comphelper::getProcessServiceFactory()->createInstance(
190 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
191 "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
192 UNO_QUERY );
194 m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
197 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
198 xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
199 m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
202 Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
203 Sequence< rtl::OUString > aImageCmdSeq( 1 );
204 aImageCmdSeq[0] = aCommandURL;
206 aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
208 Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
209 Image aImage( xGraphic );
211 if ( !!aImage )
212 return aImage;
213 else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
214 return SvFileInformationManager::GetImageNoDefault( aObj, bBig );
217 catch (const Exception&)
221 return Image();
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */