xref: /aoo41x/main/sfx2/source/appl/imagemgr.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sfx2.hxx"
30 #include "sfx2/imagemgr.hxx"
31 #include <com/sun/star/frame/XController.hpp>
32 #include <com/sun/star/ui/XImageManager.hpp>
33 #include <com/sun/star/frame/XModuleManager.hpp>
34 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
35 #include <com/sun/star/ui/ImageType.hpp>
36 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
37 
38 #include <tools/urlobj.hxx>
39 #include <svtools/imagemgr.hxx>
40 #include <comphelper/processfactory.hxx>
41 #include <rtl/ustring.hxx>
42 #include <rtl/logfile.hxx>
43 
44 #include "sfx2/imgmgr.hxx"
45 #include <sfx2/app.hxx>
46 #include <sfx2/unoctitm.hxx>
47 #include <sfx2/dispatch.hxx>
48 #include <sfx2/msg.hxx>
49 #include <sfx2/msgpool.hxx>
50 #include <sfx2/viewfrm.hxx>
51 #include <sfx2/module.hxx>
52 #include <sfx2/objsh.hxx>
53 #include <sfx2/docfac.hxx>
54 
55 #include <hash_map>
56 
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::frame;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::util;
61 using namespace ::com::sun::star::ui;
62 using namespace ::com::sun::star::frame;
63 
64 typedef std::hash_map< ::rtl::OUString,
65                        WeakReference< XImageManager >,
66                        ::rtl::OUStringHash,
67                        ::std::equal_to< ::rtl::OUString > > ModuleIdToImagegMgr;
68 
69 static WeakReference< XModuleManager >                        m_xModuleManager;
70 static WeakReference< XModuleUIConfigurationManagerSupplier > m_xModuleCfgMgrSupplier;
71 static WeakReference< XURLTransformer >                       m_xURLTransformer;
72 static ModuleIdToImagegMgr                                    m_aModuleIdToImageMgrMap;
73 
74 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 )
75 {
76     // TODO/LATeR: shouldn't this become a method at SfxViewFrame?! That would save the UnoTunnel
77     if ( !rFrame.is() )
78         return Image();
79 
80     INetURLObject aObj( aURL );
81     INetProtocol  nProtocol = aObj.GetProtocol();
82 
83     Reference < XController > xController;
84     Reference < XModel > xModel;
85     if ( rFrame.is() )
86         xController = rFrame->getController();
87     if ( xController.is() )
88         xModel = xController->getModel();
89 
90     rtl::OUString aCommandURL( aURL );
91     if ( nProtocol == INET_PROT_SLOT )
92     {
93         /*
94         // Support old way to retrieve image via slot URL
95         Reference< XURLTransformer > xURLTransformer = m_xURLTransformer;
96         if ( !xURLTransformer.is() )
97         {
98             xURLTransformer = Reference< XURLTransformer >(
99                                 ::comphelper::getProcessServiceFactory()->createInstance(
100                                     rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer" )),
101                                 UNO_QUERY );
102             m_xURLTransformer = xURLTransformer;
103         }
104 
105         URL aTargetURL;
106         aTargetURL.Complete = aURL;
107         xURLTransformer->parseStrict( aTargetURL );
108         sal_uInt16 nId = ( sal_uInt16 ) aTargetURL.Path.toInt32();*/
109         sal_uInt16 nId = ( sal_uInt16 ) String(aURL).Copy(5).ToInt32();
110         const SfxSlot* pSlot = 0;
111         if ( xModel.is() )
112         {
113             Reference < XUnoTunnel > xObj( xModel, UNO_QUERY );
114             Sequence < sal_Int8 > aSeq( SvGlobalName( SFX_GLOBAL_CLASSID ).GetByteSequence() );
115             sal_Int64 nHandle = xObj.is() ? xObj->getSomething( aSeq ) : 0;
116             if ( nHandle )
117             {
118                 SfxObjectShell* pDoc = reinterpret_cast<SfxObjectShell*>(sal::static_int_cast<sal_IntPtr>( nHandle ));
119                 SfxModule* pModule = pDoc->GetFactory().GetModule();
120                 pSlot = pModule->GetSlotPool()->GetSlot( nId );
121             }
122         }
123         else
124             pSlot = SfxSlotPool::GetSlotPool().GetSlot( nId );
125 
126         if ( pSlot )
127         {
128             aCommandURL = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".uno:" ));
129             aCommandURL += rtl::OUString::createFromAscii( pSlot->GetUnoName() );
130         }
131         else
132             aCommandURL = rtl::OUString();
133     }
134 
135     Reference< XImageManager > xDocImgMgr;
136     if ( xModel.is() )
137     {
138         Reference< XUIConfigurationManagerSupplier > xSupplier( xModel, UNO_QUERY );
139         if ( xSupplier.is() )
140         {
141             Reference< XUIConfigurationManager > xDocUICfgMgr( xSupplier->getUIConfigurationManager(), UNO_QUERY );
142             xDocImgMgr = Reference< XImageManager >( xDocUICfgMgr->getImageManager(), UNO_QUERY );
143         }
144     }
145 
146     sal_Int16 nImageType( ::com::sun::star::ui::ImageType::COLOR_NORMAL|
147                             ::com::sun::star::ui::ImageType::SIZE_DEFAULT );
148     if ( bBig )
149         nImageType |= ::com::sun::star::ui::ImageType::SIZE_LARGE;
150     if ( bHiContrast )
151         nImageType |= ::com::sun::star::ui::ImageType::COLOR_HIGHCONTRAST;
152 
153     if ( xDocImgMgr.is() )
154     {
155         Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
156         Sequence< rtl::OUString > aImageCmdSeq( 1 );
157         aImageCmdSeq[0] = aCommandURL;
158 
159         try
160         {
161             aGraphicSeq = xDocImgMgr->getImages( nImageType, aImageCmdSeq );
162             Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
163             Image aImage( xGraphic );
164 
165             if ( !!aImage )
166                 return aImage;
167         }
168         catch ( Exception& )
169         {
170         }
171     }
172 
173     Reference< XModuleManager > xModuleManager = m_xModuleManager;
174 
175     if ( !xModuleManager.is() )
176     {
177         xModuleManager = Reference< XModuleManager >(
178                             ::comphelper::getProcessServiceFactory()->createInstance(
179                                 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
180                                     "com.sun.star.frame.ModuleManager" ))),
181                             UNO_QUERY );
182         m_xModuleManager = xModuleManager;
183     }
184 
185     try
186     {
187         if ( aCommandURL.getLength() > 0 )
188         {
189             Reference< XImageManager > xModuleImageManager;
190             rtl::OUString aModuleId = xModuleManager->identify( rFrame );
191             ModuleIdToImagegMgr::iterator pIter = m_aModuleIdToImageMgrMap.find( aModuleId );
192             if ( pIter != m_aModuleIdToImageMgrMap.end() )
193                 xModuleImageManager = pIter->second;
194             else
195             {
196                 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgMgrSupplier = m_xModuleCfgMgrSupplier;
197 
198                 if ( !xModuleCfgMgrSupplier.is() )
199                 {
200                     xModuleCfgMgrSupplier = Reference< XModuleUIConfigurationManagerSupplier >(
201                                                 ::comphelper::getProcessServiceFactory()->createInstance(
202                                                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
203                                                         "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
204                                                 UNO_QUERY );
205 
206                     m_xModuleCfgMgrSupplier = xModuleCfgMgrSupplier;
207                 }
208 
209                 Reference< XUIConfigurationManager > xUICfgMgr = xModuleCfgMgrSupplier->getUIConfigurationManager( aModuleId );
210                 xModuleImageManager = Reference< XImageManager >( xUICfgMgr->getImageManager(), UNO_QUERY );
211                 m_aModuleIdToImageMgrMap.insert( ModuleIdToImagegMgr::value_type( aModuleId, xModuleImageManager ));
212             }
213 
214             Sequence< Reference< ::com::sun::star::graphic::XGraphic > > aGraphicSeq;
215             Sequence< rtl::OUString > aImageCmdSeq( 1 );
216             aImageCmdSeq[0] = aCommandURL;
217 
218             aGraphicSeq = xModuleImageManager->getImages( nImageType, aImageCmdSeq );
219 
220             Reference< ::com::sun::star::graphic::XGraphic > xGraphic = aGraphicSeq[0];
221             Image aImage( xGraphic );
222 
223             if ( !!aImage )
224                 return aImage;
225             else if ( nProtocol != INET_PROT_UNO && nProtocol != INET_PROT_SLOT )
226                 return SvFileInformationManager::GetImageNoDefault( aObj, bBig, bHiContrast );
227         }
228     }
229     catch ( Exception& )
230     {
231     }
232 
233     return Image();
234 }
235