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 0 : 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 0 : if ( !rFrame.is() )
66 0 : return Image();
67 :
68 0 : INetURLObject aObj( aURL );
69 0 : INetProtocol nProtocol = aObj.GetProtocol();
70 :
71 0 : Reference < XController > xController;
72 0 : Reference < XModel > xModel;
73 0 : if ( rFrame.is() )
74 0 : xController = rFrame->getController();
75 0 : if ( xController.is() )
76 0 : xModel = xController->getModel();
77 :
78 0 : OUString aCommandURL( aURL );
79 0 : if ( nProtocol == INET_PROT_SLOT )
80 : {
81 0 : sal_uInt16 nId = ( sal_uInt16 ) aURL.copy(5).toInt32();
82 0 : const SfxSlot* pSlot = 0;
83 0 : if ( xModel.is() )
84 : {
85 0 : Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
86 0 : Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
87 0 : sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
88 0 : if ( nHandle )
89 : {
90 0 : SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
91 0 : SfxModule* pModule = pDoc->GetFactory().GetModule();
92 0 : pSlot = pModule->GetSlotPool()->GetSlot( nId );
93 0 : }
94 : }
95 : else
96 0 : pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
97 :
98 0 : if ( pSlot )
99 : {
100 0 : aCommandURL = ".uno:";
101 0 : aCommandURL += OUString::createFromAscii( pSlot->GetUnoName() );
102 : }
103 : else
104 0 : aCommandURL = OUString();
105 : }
106 :
107 0 : Reference< XImageManager > xDocImgMgr;
108 0 : if ( xModel.is() )
109 : {
110 0 : Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
111 0 : if ( xSupplier.is() )
112 : {
113 0 : Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
114 0 : xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
115 0 : }
116 : }
117 :
118 : sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
119 0 : ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
120 0 : if ( bBig )
121 0 : nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
122 :
123 0 : if ( xDocImgMgr.is() )
124 : {
125 0 : Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
126 0 : Sequence< OUString > aImageCmdSeq( 1 );
127 0 : aImageCmdSeq[0] = aCommandURL;
128 :
129 : try
130 : {
131 0 : aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
132 0 : Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
133 0 : Image aImage( xGraphic );
134 :
135 0 : if ( !!aImage )
136 0 : return aImage;
137 : }
138 0 : catch (const Exception&)
139 : {
140 0 : }
141 : }
142 :
143 0 : static WeakReference< XModuleManager2 > m_xModuleManager;
144 :
145 0 : Reference< XModuleManager2 > xModuleManager = m_xModuleManager;
146 :
147 0 : if ( !xModuleManager.is() )
148 : {
149 0 : xModuleManager = ModuleManager::create(::comphelper::getProcessComponentContext());
150 0 : m_xModuleManager = xModuleManager;
151 : }
152 :
153 : try
154 : {
155 0 : if ( !aCommandURL.isEmpty() )
156 : {
157 0 : Reference< XImageManager > xModuleImageManager;
158 0 : OUString aModuleId = xModuleManager->identify( rFrame );
159 :
160 0 : static ModuleIdToImagegMgr m_aModuleIdToImageMgrMap;
161 :
162 0 : ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
163 0 : if ( pIter != m_aModuleIdToImageMgrMap.end() )
164 0 : xModuleImageManager = pIter->second;
165 : else
166 : {
167 0 : static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
168 :
169 0 : Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
170 :
171 0 : if ( !xModuleCfgMgrSupplier.is() )
172 : {
173 0 : xModuleCfgMgrSupplier = theModuleUIConfigurationManagerSupplier::get(
174 0 : ::comphelper::getProcessComponentContext() );
175 :
176 0 : m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
177 : }
178 :
179 0 : Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
180 0 : xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
181 0 : m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
182 : }
183 :
184 0 : Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
185 0 : Sequence< OUString > aImageCmdSeq( 1 );
186 0 : aImageCmdSeq[0] = aCommandURL;
187 :
188 0 : aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
189 :
190 0 : Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
191 0 : Image aImage( xGraphic );
192 :
193 0 : if ( !!aImage )
194 0 : return aImage;
195 0 : else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
196 0 : return SvFileInformationManager::GetImageNoDefault( aObj, bBig );
197 : }
198 : }
199 0 : catch (const Exception&)
200 : {
201 : }
202 :
203 0 : return Image();
204 : }
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|