Line data Source code
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 <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>
27 :
28 : #include <tools/urlobj.hxx>
29 : #include <svtools/imagemgr.hxx>
30 : #include <comphelper/processfactory.hxx>
31 : #include <rtl/ustring.hxx>
32 :
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>
43 :
44 : #include <boost/unordered_map.hpp>
45 :
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;
51 :
52 : typedef boost::unordered_map< OUString,
53 : WeakReference< XImageManager >,
54 : OUStringHash,
55 : ::std::equal_to< OUString > > ModuleIdToImagegMgr;
56 :
57 :
58 45216 : Image SAL_CALL GetImage(
59 : const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame,
60 : const OUString& aURL,
61 : bool bBig
62 : )
63 : {
64 : // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
65 45216 : if ( !rFrame.is() )
66 0 : return Image();
67 :
68 45216 : INetURLObject aObj( aURL );
69 45216 : INetProtocol nProtocol = aObj.GetProtocol();
70 :
71 90432 : Reference < XController > xController;
72 90432 : Reference < XModel > xModel;
73 45216 : if ( rFrame.is() )
74 45216 : xController = rFrame->getController();
75 45216 : if ( xController.is() )
76 45216 : xModel = xController->getModel();
77 :
78 90432 : OUString aCommandURL( aURL );
79 45216 : if ( nProtocol == INET_PROT_SLOT )
80 : {
81 3306 : sal_uInt16 nId = ( sal_uInt16 ) aURL.copy(5).toInt32();
82 3306 : const SfxSlot* pSlot = 0;
83 3306 : if ( xModel.is() )
84 : {
85 3306 : Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
86 6612 : Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
87 3306 : sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
88 3306 : if ( nHandle )
89 : {
90 3234 : SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
91 3234 : SfxModule* pModule = pDoc->GetFactory().GetModule();
92 3234 : pSlot = pModule->GetSlotPool()->GetSlot( nId );
93 3306 : }
94 : }
95 : else
96 0 : pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
97 :
98 3306 : if ( pSlot )
99 : {
100 3234 : aCommandURL = ".uno:";
101 3234 : aCommandURL += OUString::createFromAscii( pSlot->GetUnoName() );
102 : }
103 : else
104 72 : aCommandURL = OUString();
105 : }
106 :
107 90432 : Reference< XImageManager > xDocImgMgr;
108 45216 : if ( xModel.is() )
109 : {
110 45216 : Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
111 45216 : if ( xSupplier.is() )
112 : {
113 44332 : Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
114 44332 : xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
115 45216 : }
116 : }
117 :
118 : sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
119 45216 : ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
120 45216 : if ( bBig )
121 0 : nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
122 :
123 45216 : if ( xDocImgMgr.is() )
124 : {
125 44332 : Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
126 88664 : Sequence< OUString > aImageCmdSeq( 1 );
127 44332 : aImageCmdSeq[0] = aCommandURL;
128 :
129 : try
130 : {
131 44332 : aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
132 44332 : Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
133 88664 : Image aImage( xGraphic );
134 :
135 44332 : if ( !!aImage )
136 44332 : return aImage;
137 : }
138 0 : catch (const Exception&)
139 : {
140 44332 : }
141 : }
142 :
143 45216 : static WeakReference< XModuleManager2 > m_xModuleManager;
144 :
145 90432 : Reference< XModuleManager2 > xModuleManager = m_xModuleManager;
146 :
147 45216 : if ( !xModuleManager.is() )
148 : {
149 66 : xModuleManager = ModuleManager::create(::comphelper::getProcessComponentContext());
150 66 : m_xModuleManager = xModuleManager;
151 : }
152 :
153 : try
154 : {
155 45216 : if ( !aCommandURL.isEmpty() )
156 : {
157 45144 : Reference< XImageManager > xModuleImageManager;
158 45212 : OUString aModuleId = xModuleManager->identify( rFrame );
159 :
160 45144 : static ModuleIdToImagegMgr m_aModuleIdToImageMgrMap;
161 :
162 45144 : ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
163 45144 : if ( pIter != m_aModuleIdToImageMgrMap.end() )
164 45048 : xModuleImageManager = pIter->second;
165 : else
166 : {
167 96 : static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
168 :
169 96 : Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
170 :
171 96 : if ( !xModuleCfgMgrSupplier.is() )
172 : {
173 132 : xModuleCfgMgrSupplier = theModuleUIConfigurationManagerSupplier::get(
174 66 : ::comphelper::getProcessComponentContext() );
175 :
176 66 : m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
177 : }
178 :
179 192 : Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
180 96 : xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
181 192 : m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
182 : }
183 :
184 45212 : Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
185 45212 : Sequence< OUString > aImageCmdSeq( 1 );
186 45144 : aImageCmdSeq[0] = aCommandURL;
187 :
188 45144 : aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
189 :
190 45212 : Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
191 45212 : Image aImage( xGraphic );
192 :
193 45144 : if ( !!aImage )
194 9214 : return aImage;
195 35930 : else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
196 35930 : return SvFileInformationManager::GetImageNoDefault( aObj, bBig );
197 : }
198 : }
199 0 : catch (const Exception&)
200 : {
201 : }
202 :
203 45356 : return Image();
204 951 : }
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|