1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "resource/sharedresources.hxx"
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/officeresourcebundle.hxx>
25 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <tools/diagnose_ex.h>
28 #include <osl/diagnose.h>
31 namespace connectivity
35 using ::com::sun::star::uno::Reference
;
36 using ::com::sun::star::uno::XComponentContext
;
37 using ::com::sun::star::uno::Exception
;
40 //= SharedResources_Impl
42 class SharedResources_Impl
45 static SharedResources_Impl
* s_pInstance
;
46 static oslInterlockedCount s_nClients
;
49 ::std::auto_ptr
< ::comphelper::OfficeResourceBundle
>
53 static void registerClient();
54 static void revokeClient();
56 static SharedResources_Impl
&
59 OUString
getResourceString( ResourceId _nId
);
62 SharedResources_Impl();
64 static ::osl::Mutex
& getMutex()
66 static ::osl::Mutex s_aMutex
;
72 SharedResources_Impl
* SharedResources_Impl::s_pInstance( NULL
);
73 oslInterlockedCount
SharedResources_Impl::s_nClients( 0 );
76 SharedResources_Impl::SharedResources_Impl()
80 Reference
< XComponentContext
> xContext(
81 comphelper::getProcessComponentContext() );
82 m_pResourceBundle
.reset( new ::comphelper::OfficeResourceBundle( xContext
, "cnr" ) );
84 catch( const Exception
& )
86 DBG_UNHANDLED_EXCEPTION();
91 OUString
SharedResources_Impl::getResourceString( ResourceId _nId
)
93 if ( m_pResourceBundle
.get() == NULL
)
94 // this should never happen, but we gracefully ignore it. It has been reported
95 // in the constructor in non-product builds.
98 return m_pResourceBundle
->loadString( _nId
);
102 void SharedResources_Impl::registerClient()
104 osl_atomic_increment( &s_nClients
);
108 void SharedResources_Impl::revokeClient()
110 ::osl::MutexGuard
aGuard( getMutex() );
111 if ( 0 == osl_atomic_decrement( &s_nClients
) )
119 SharedResources_Impl
& SharedResources_Impl::getInstance()
121 ::osl::MutexGuard
aGuard( getMutex() );
122 OSL_ENSURE( s_nClients
> 0, "SharedResources_Impl::getInstance: no active clients!" );
125 s_pInstance
= new SharedResources_Impl
;
135 size_t lcl_substitute( OUString
& _inout_rString
,
136 const sal_Char
* _pAsciiPattern
, const OUString
& _rReplace
)
138 size_t nOccurrences
= 0;
140 OUString
sPattern( OUString::createFromAscii( _pAsciiPattern
) );
141 sal_Int32 nIndex
= 0;
142 while ( ( nIndex
= _inout_rString
.indexOf( sPattern
) ) > -1 )
145 _inout_rString
= _inout_rString
.replaceAt( nIndex
, sPattern
.getLength(), _rReplace
);
156 SharedResources::SharedResources()
158 SharedResources_Impl::registerClient();
162 SharedResources::~SharedResources()
164 SharedResources_Impl::revokeClient();
168 OUString
SharedResources::getResourceString( ResourceId _nResId
) const
170 return SharedResources_Impl::getInstance().getResourceString( _nResId
);
174 OUString
SharedResources::getResourceStringWithSubstitution( ResourceId _nResId
,
175 const sal_Char
* _pAsciiPatternToReplace
, const OUString
& _rStringToSubstitute
) const
177 OUString
sString( SharedResources_Impl::getInstance().getResourceString( _nResId
) );
178 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace
, _rStringToSubstitute
) );
183 OUString
SharedResources::getResourceStringWithSubstitution( ResourceId _nResId
,
184 const sal_Char
* _pAsciiPatternToReplace1
, const OUString
& _rStringToSubstitute1
,
185 const sal_Char
* _pAsciiPatternToReplace2
, const OUString
& _rStringToSubstitute2
) const
187 OUString
sString( SharedResources_Impl::getInstance().getResourceString( _nResId
) );
188 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace1
, _rStringToSubstitute1
) );
189 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace2
, _rStringToSubstitute2
) );
194 OUString
SharedResources::getResourceStringWithSubstitution( ResourceId _nResId
,
195 const sal_Char
* _pAsciiPatternToReplace1
, const OUString
& _rStringToSubstitute1
,
196 const sal_Char
* _pAsciiPatternToReplace2
, const OUString
& _rStringToSubstitute2
,
197 const sal_Char
* _pAsciiPatternToReplace3
, const OUString
& _rStringToSubstitute3
) const
199 OUString
sString( SharedResources_Impl::getInstance().getResourceString( _nResId
) );
200 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace1
, _rStringToSubstitute1
) );
201 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace2
, _rStringToSubstitute2
) );
202 OSL_VERIFY( lcl_substitute( sString
, _pAsciiPatternToReplace3
, _rStringToSubstitute3
) );
206 OUString
SharedResources::getResourceStringWithSubstitution( ResourceId _nResId
,
207 const ::std::list
< ::std::pair
<const sal_Char
* , OUString
> > _aStringToSubstitutes
) const
209 OUString
sString( SharedResources_Impl::getInstance().getResourceString( _nResId
) );
210 ::std::list
< ::std::pair
<const sal_Char
* , OUString
> >::const_iterator aIter
= _aStringToSubstitutes
.begin();
211 ::std::list
< ::std::pair
<const sal_Char
* , OUString
> >::const_iterator aEnd
= _aStringToSubstitutes
.end();
212 for(;aIter
!= aEnd
; ++aIter
)
213 OSL_VERIFY( lcl_substitute( sString
, aIter
->first
, aIter
->second
) );
219 } // namespace connectivity
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */