xref: /aoo42x/main/sfx2/source/appl/imagemgr.cxx (revision d119d52d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sfx2.hxx"
26 #include "sfx2/imagemgr.hxx"
27 #include <com/sun/star/frame/XController.hpp>
28 #include <com/sun/star/ui/XImageManager.hpp>
29 #include <com/sun/star/frame/XModuleManager.hpp>
30 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
31 #include <com/sun/star/ui/ImageType.hpp>
32 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
33 
34 #include <tools/urlobj.hxx>
35 #include <svtools/imagemgr.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <rtl/ustring.hxx>
38 #include <rtl/logfile.hxx>
39 
40 #include "sfx2/imgmgr.hxx"
41 #include <sfx2/app.hxx>
42 #include <sfx2/unoctitm.hxx>
43 #include <sfx2/dispatch.hxx>
44 #include <sfx2/msg.hxx>
45 #include <sfx2/msgpool.hxx>
46 #include <sfx2/viewfrm.hxx>
47 #include <sfx2/module.hxx>
48 #include <sfx2/objsh.hxx>
49 #include <sfx2/docfac.hxx>
50 
51 #include <hash_map>
52 
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::frame;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::util;
57 using namespace ::com::sun::star::ui;
58 using namespace ::com::sun::star::frame;
59 
60 typedef std::hash_map< ::rtl::OUString,
61                        WeakReference< XImageManager >,
62                        ::rtl::OUStringHash,
63                        ::std::equal_to< ::rtl::OUString > > ModuleIdToImagegMgr;
64 
65 static WeakReference< XModuleManager >                        m_xModuleManager;
66 static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
67 static WeakReference< XURLTransformer >                       m_xURLTransformer;
68 static ModuleIdToImagegMgr                                    m_aModuleIdToImageMgrMap;
69 
GetImage(const::com::sun::star::uno::Reference<::com::sun::star::frame::XFrame> & rFrame,const::rtl::OUString & aURL,sal_Bool bBig,sal_Bool bHiContrast)70 Image SAL_CALL GetImage( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rFrame, const ::rtl::OUString& aURL, sal_Bool bBig, sal_Bool bHiContrast )
71 {
72     // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
73     if ( !rFrame.is() )
74         return Image();
75 
76     INetURLObject aObj( aURL );
77     INetProtocol  nProtocol = aObj.GetProtocol();
78 
79     Reference < XController > xController;
80     Reference < XModel > xModel;
81     if ( rFrame.is() )
82         xController = rFrame->getController();
83     if ( xController.is() )
84         xModel = xController->getModel();
85 
86     rtl::OUString aCommandURL( aURL );
87     if ( nProtocol == INET_PROT_SLOT )
88     {
89         /*
90         // Support old way to retrieve image via slot URL
91         Reference< XURLTransformer > xURLTransformer = m_xURLTransformer;
92         if ( !xURLTransformer.is() )
93         {
94             xURLTransformer = Reference< XURLTransformer >(
95                                 ::comphelper::getProcessServiceFactory()->createInstance(
96                                     rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )),
97                                 UNO_QUERY );
98             m_xURLTransformer = xURLTransformer;
99         }
100 
101         URL aTargetURL;
102         aTargetURL.Complete = aURL;
103         xURLTransformer->parseStrict( aTargetURL );
104         sal_uInt16 nId = ( sal_uInt16 ) aTargetURL.Path.toInt32();*/
105         sal_uInt16 nId = ( sal_uInt16 ) String(aURL).Copy(5).ToInt32();
106         const SfxSlot* pSlot = 0;
107         if ( xModel.is() )
108         {
109             Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
110             Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
111             sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
112             if ( nHandle )
113             {
114                 SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
115                 SfxModule* pModule = pDoc->GetFactory().GetModule();
116                 pSlot = pModule->GetSlotPool()->GetSlot( nId );
117             }
118         }
119         else
120             pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
121 
122         if ( pSlot )
123         {
124             aCommandURL = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
125             aCommandURL += rtl::OUString::createFromAscii( pSlot->GetUnoName() );
126         }
127         else
128             aCommandURL = rtl::OUString();
129     }
130 
131     Reference< XImageManager > xDocImgMgr;
132     if ( xModel.is() )
133     {
134         Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
135         if ( xSupplier.is() )
136         {
137             Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
138             xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
139         }
140     }
141 
142     sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
143                             ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
144     if ( bBig )
145         nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
146     if ( bHiContrast )
147         nImageType |= ::com::sun::star::ui::ImageType::COLOR_HIGHCONTRAST;
148 
149     if ( xDocImgMgr.is() )
150     {
151         Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
152         Sequence< rtl::OUString > aImageCmdSeq( 1 );
153         aImageCmdSeq[0] = aCommandURL;
154 
155         try
156         {
157             aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
158             Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
159             Image aImage( xGraphic );
160 
161             if ( !!aImage )
162                 return aImage;
163         }
164         catch ( Exception& )
165         {
166         }
167     }
168 
169     Reference< XModuleManager > xModuleManager = m_xModuleManager;
170 
171     if ( !xModuleManager.is() )
172     {
173         xModuleManager = Reference< XModuleManager >(
174                             ::comphelper::getProcessServiceFactory()->createInstance(
175                                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
176                                     "com.sun.star.frame.ModuleManager" ))),
177                             UNO_QUERY );
178         m_xModuleManager = xModuleManager;
179     }
180 
181     try
182     {
183         if ( aCommandURL.getLength() > 0 )
184         {
185             Reference< XImageManager > xModuleImageManager;
186             rtl::OUString aModuleId = xModuleManager->identify( rFrame );
187             ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
188             if ( pIter != m_aModuleIdToImageMgrMap.end() )
189                 xModuleImageManager = pIter->second;
190             else
191             {
192                 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
193 
194                 if ( !xModuleCfgMgrSupplier.is() )
195                 {
196                     xModuleCfgMgrSupplier = Reference< XModuleUIConfigurationManagerSupplier >(
197                                                 ::comphelper::getProcessServiceFactory()->createInstance(
198                                                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
199                                                         "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
200                                                 UNO_QUERY );
201 
202                     m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
203                 }
204 
205                 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
206                 xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
207                 m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
208             }
209 
210             Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
211             Sequence< rtl::OUString > aImageCmdSeq( 1 );
212             aImageCmdSeq[0] = aCommandURL;
213 
214             aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
215 
216             Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
217             Image aImage( xGraphic );
218 
219             if ( !!aImage )
220                 return aImage;
221             else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
222                 return SvFileInformationManager::GetImageNoDefault( aObj, bBig, bHiContrast );
223         }
224     }
225     catch ( Exception& )
226     {
227     }
228 
229     return Image();
230 }
231