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