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_toolkit.hxx"
26 #include <toolkit/helper/tkresmgr.hxx>
27 #include <tools/simplerm.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/componentcontext.hxx>
30 #include <comphelper/namedvaluecollection.hxx>
31 #include <com/sun/star/graphic/XGraphicProvider.hpp>
32 #ifndef _TOOLS_RESMGR_HXX_
33 #include <tools/resmgr.hxx>
34 #endif
35 #include <tools/diagnose_ex.h>
36
37 #include <vcl/svapp.hxx>
38
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::graphic::XGraphic;
41 using ::com::sun::star::graphic::XGraphicProvider;
42 using namespace ::com::sun::star;
43 // -----------------------------------------------------------------------------
44 // TkResMgr
45 // -----------------------------------------------------------------------------
46
47 SimpleResMgr* TkResMgr::m_pSimpleResMgr = NULL;
48 ResMgr* TkResMgr::m_pResMgr = NULL;
49
50 // -----------------------------------------------------------------------------
51
~EnsureDelete()52 TkResMgr::EnsureDelete::~EnsureDelete()
53 {
54 delete TkResMgr::m_pSimpleResMgr;
55 // delete TkResMgr::m_pResMgr;
56 }
57
58 // -----------------------------------------------------------------------------
59
ensureImplExists()60 void TkResMgr::ensureImplExists()
61 {
62 if (m_pSimpleResMgr)
63 return;
64
65 ::com::sun::star::lang::Locale aLocale = Application::GetSettings().GetUILocale();
66
67 ByteString sResMgrName( "tk" );
68
69 m_pSimpleResMgr = SimpleResMgr::Create( sResMgrName.GetBuffer(), aLocale );
70 m_pResMgr = ResMgr::CreateResMgr( sResMgrName.GetBuffer() );
71
72 if (m_pSimpleResMgr)
73 {
74 // now that we have a impl class, make sure it's deleted on unloading the library
75 static TkResMgr::EnsureDelete s_aDeleteTheImplClass;
76 }
77 }
78
79 // -----------------------------------------------------------------------------
loadString(sal_uInt16 nResId)80 ::rtl::OUString TkResMgr::loadString( sal_uInt16 nResId )
81 {
82 ::rtl::OUString sReturn;
83
84 ensureImplExists();
85 if ( m_pSimpleResMgr )
86 sReturn = m_pSimpleResMgr->ReadString( nResId );
87
88 return sReturn;
89 }
90
91 // -----------------------------------------------------------------------------
loadImage(sal_uInt16 nResId)92 Image TkResMgr::loadImage( sal_uInt16 nResId )
93 {
94 Image aReturn;
95
96 ensureImplExists();
97 if ( m_pResMgr )
98 aReturn = Image( ResId( nResId, *m_pResMgr ) );
99
100 return aReturn;
101 }
102
103 // -----------------------------------------------------------------------------
getImageFromURL(const::rtl::OUString & i_rImageURL)104 Image TkResMgr::getImageFromURL( const ::rtl::OUString& i_rImageURL )
105 {
106 if ( !i_rImageURL.getLength() )
107 return Image();
108
109 try
110 {
111 ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
112 Reference< XGraphicProvider > xProvider;
113 if ( aContext.createComponent( "com.sun.star.graphic.GraphicProvider", xProvider ) )
114 {
115 ::comphelper::NamedValueCollection aMediaProperties;
116 aMediaProperties.put( "URL", i_rImageURL );
117 Reference< XGraphic > xGraphic = xProvider->queryGraphic( aMediaProperties.getPropertyValues() );
118 return Image( xGraphic );
119 }
120 }
121 catch( const uno::Exception& )
122 {
123 DBG_UNHANDLED_EXCEPTION();
124 }
125 return Image();
126 }
127
128