Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "resource/sharedresources.hxx"
21 : :
22 : : #include <comphelper/processfactory.hxx>
23 : : #include <comphelper/officeresourcebundle.hxx>
24 : :
25 : : #include <com/sun/star/beans/XPropertySet.hpp>
26 : : #include <com/sun/star/uno/XComponentContext.hpp>
27 : :
28 : : #include <tools/diagnose_ex.h>
29 : : #include <osl/diagnose.h>
30 : :
31 : : //........................................................................
32 : : namespace connectivity
33 : : {
34 : : //........................................................................
35 : :
36 : : /** === begin UNO using === **/
37 : : using ::com::sun::star::uno::Reference;
38 : : using ::com::sun::star::beans::XPropertySet;
39 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
40 : : using ::com::sun::star::uno::XComponentContext;
41 : : using ::com::sun::star::uno::Exception;
42 : : /** === end UNO using === **/
43 : :
44 : : //====================================================================
45 : : //= SharedResources_Impl
46 : : //====================================================================
47 : 5 : class SharedResources_Impl
48 : : {
49 : : private:
50 : : static SharedResources_Impl* s_pInstance;
51 : : static oslInterlockedCount s_nClients;
52 : :
53 : : private:
54 : : ::std::auto_ptr< ::comphelper::OfficeResourceBundle >
55 : : m_pResourceBundle;
56 : :
57 : : public:
58 : : static void registerClient();
59 : : static void revokeClient();
60 : :
61 : : static SharedResources_Impl&
62 : : getInstance();
63 : :
64 : : ::rtl::OUString getResourceString( ResourceId _nId );
65 : :
66 : : private:
67 : : SharedResources_Impl();
68 : :
69 : 104 : static ::osl::Mutex& getMutex()
70 : : {
71 [ + + ][ + - ]: 104 : static ::osl::Mutex s_aMutex;
[ + - ][ # # ]
72 : 104 : return s_aMutex;
73 : : }
74 : : };
75 : :
76 : : //--------------------------------------------------------------------
77 : : SharedResources_Impl* SharedResources_Impl::s_pInstance( NULL );
78 : : oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
79 : :
80 : : //--------------------------------------------------------------------
81 : 5 : SharedResources_Impl::SharedResources_Impl()
82 : : {
83 : : try
84 : : {
85 : : Reference< XPropertySet > xFactoryProps(
86 [ + - ][ + - ]: 5 : ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
87 : : Reference< XComponentContext > xContext(
88 [ + - ]: 5 : xFactoryProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
89 : : UNO_QUERY_THROW
90 [ + - ][ + - ]: 5 : );
[ + - ]
91 [ + - ][ # # ]: 5 : m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
[ + - ]
92 : : }
93 [ # # ]: 0 : catch( const Exception& )
94 : : {
95 : : DBG_UNHANDLED_EXCEPTION();
96 : : }
97 : 5 : }
98 : :
99 : : //--------------------------------------------------------------------
100 : 46 : ::rtl::OUString SharedResources_Impl::getResourceString( ResourceId _nId )
101 : : {
102 [ - + ]: 46 : if ( m_pResourceBundle.get() == NULL )
103 : : // this should never happen, but we gracefully ignore it. It has been reported
104 : : // in the constructor in non-product builds.
105 : 0 : return ::rtl::OUString();
106 : :
107 : 46 : return m_pResourceBundle->loadString( _nId );
108 : : }
109 : :
110 : : //--------------------------------------------------------------------
111 : 58 : void SharedResources_Impl::registerClient()
112 : : {
113 : 58 : osl_incrementInterlockedCount( &s_nClients );
114 : 58 : }
115 : :
116 : : //--------------------------------------------------------------------
117 : 58 : void SharedResources_Impl::revokeClient()
118 : : {
119 [ + - ][ + - ]: 58 : ::osl::MutexGuard aGuard( getMutex() );
120 [ + - ][ + + ]: 58 : if ( 0 == osl_decrementInterlockedCount( &s_nClients ) )
121 : : {
122 [ + + ][ + - ]: 15 : delete s_pInstance;
123 : 15 : s_pInstance = NULL;
124 [ + - ]: 58 : }
125 : 58 : }
126 : :
127 : : //--------------------------------------------------------------------
128 : 46 : SharedResources_Impl& SharedResources_Impl::getInstance()
129 : : {
130 [ + - ][ + - ]: 46 : ::osl::MutexGuard aGuard( getMutex() );
131 : : OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
132 : :
133 [ + + ]: 46 : if ( !s_pInstance )
134 [ + - ][ + - ]: 5 : s_pInstance = new SharedResources_Impl;
135 : :
136 [ + - ]: 46 : return *s_pInstance;
137 : : }
138 : :
139 : : //====================================================================
140 : : //= helpers
141 : : //====================================================================
142 : : namespace
143 : : {
144 : 0 : size_t lcl_substitute( ::rtl::OUString& _inout_rString,
145 : : const sal_Char* _pAsciiPattern, const ::rtl::OUString& _rReplace )
146 : : {
147 : 0 : size_t nOccurrences = 0;
148 : :
149 : 0 : ::rtl::OUString sPattern( ::rtl::OUString::createFromAscii( _pAsciiPattern ) );
150 : 0 : sal_Int32 nIndex = 0;
151 [ # # ]: 0 : while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
152 : : {
153 : 0 : ++nOccurrences;
154 : 0 : _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
155 : : }
156 : :
157 : 0 : return nOccurrences;
158 : : }
159 : : }
160 : :
161 : : //====================================================================
162 : : //= SharedResources
163 : : //====================================================================
164 : : //--------------------------------------------------------------------
165 : 58 : SharedResources::SharedResources()
166 : : {
167 : 58 : SharedResources_Impl::registerClient();
168 : 58 : }
169 : :
170 : : //--------------------------------------------------------------------
171 : 58 : SharedResources::~SharedResources()
172 : : {
173 : 58 : SharedResources_Impl::revokeClient();
174 : 58 : }
175 : :
176 : : //--------------------------------------------------------------------
177 : 46 : ::rtl::OUString SharedResources::getResourceString( ResourceId _nResId ) const
178 : : {
179 : 46 : return SharedResources_Impl::getInstance().getResourceString( _nResId );
180 : : }
181 : :
182 : : //--------------------------------------------------------------------
183 : 0 : ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
184 : : const sal_Char* _pAsciiPatternToReplace, const ::rtl::OUString& _rStringToSubstitute ) const
185 : : {
186 : 0 : ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
187 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) );
188 : 0 : return sString;
189 : : }
190 : :
191 : : //--------------------------------------------------------------------
192 : 0 : ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
193 : : const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
194 : : const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2 ) const
195 : : {
196 : 0 : ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
197 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
198 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
199 : 0 : return sString;
200 : : }
201 : :
202 : : //--------------------------------------------------------------------
203 : 0 : ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
204 : : const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
205 : : const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2,
206 : : const sal_Char* _pAsciiPatternToReplace3, const ::rtl::OUString& _rStringToSubstitute3 ) const
207 : : {
208 : 0 : ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
209 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
210 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
211 : 0 : OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) );
212 : 0 : return sString;
213 : : }
214 : : //--------------------------------------------------------------------
215 : 0 : ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
216 : : const ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > > _aStringToSubstitutes) const
217 : : {
218 [ # # ][ # # ]: 0 : ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
219 : 0 : ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aIter = _aStringToSubstitutes.begin();
220 : 0 : ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aEnd = _aStringToSubstitutes.end();
221 [ # # ]: 0 : for(;aIter != aEnd; ++aIter)
222 : 0 : OSL_VERIFY( lcl_substitute( sString, aIter->first, aIter->second ) );
223 : :
224 : 0 : return sString;
225 : : }
226 : :
227 : : //........................................................................
228 : : } // namespace connectivity
229 : : //........................................................................
230 : :
231 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|