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