bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / resource / sharedresources.cxx
blobe69cfa669cf2a45bc12006694f80fef7dd9e3446
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
39 class SharedResources_Impl
41 private:
42 static SharedResources_Impl* s_pInstance;
43 static oslInterlockedCount s_nClients;
45 private:
46 ::std::unique_ptr< ::comphelper::OfficeResourceBundle >
47 m_pResourceBundle;
49 public:
50 static void registerClient();
51 static void revokeClient();
53 static SharedResources_Impl&
54 getInstance();
56 OUString getResourceString( ResourceId _nId );
58 private:
59 SharedResources_Impl();
61 static ::osl::Mutex& getMutex()
63 static ::osl::Mutex s_aMutex;
64 return s_aMutex;
69 SharedResources_Impl* SharedResources_Impl::s_pInstance( NULL );
70 oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
73 SharedResources_Impl::SharedResources_Impl()
75 try
77 Reference< XComponentContext > xContext(
78 comphelper::getProcessComponentContext() );
79 m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
81 catch( const Exception& )
83 DBG_UNHANDLED_EXCEPTION();
88 OUString SharedResources_Impl::getResourceString( ResourceId _nId )
90 if ( m_pResourceBundle.get() == NULL )
91 // this should never happen, but we gracefully ignore it. It has been reported
92 // in the constructor in non-product builds.
93 return OUString();
95 return m_pResourceBundle->loadString( _nId );
99 void SharedResources_Impl::registerClient()
101 osl_atomic_increment( &s_nClients );
105 void SharedResources_Impl::revokeClient()
107 ::osl::MutexGuard aGuard( getMutex() );
108 if ( 0 == osl_atomic_decrement( &s_nClients ) )
110 delete s_pInstance;
111 s_pInstance = NULL;
116 SharedResources_Impl& SharedResources_Impl::getInstance()
118 ::osl::MutexGuard aGuard( getMutex() );
119 OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
121 if ( !s_pInstance )
122 s_pInstance = new SharedResources_Impl;
124 return *s_pInstance;
127 namespace
129 size_t lcl_substitute( OUString& _inout_rString,
130 const sal_Char* _pAsciiPattern, const OUString& _rReplace )
132 size_t nOccurrences = 0;
134 OUString sPattern( OUString::createFromAscii( _pAsciiPattern ) );
135 sal_Int32 nIndex = 0;
136 while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
138 ++nOccurrences;
139 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
142 return nOccurrences;
146 SharedResources::SharedResources()
148 SharedResources_Impl::registerClient();
152 SharedResources::~SharedResources()
154 SharedResources_Impl::revokeClient();
158 OUString SharedResources::getResourceString( ResourceId _nResId ) const
160 return SharedResources_Impl::getInstance().getResourceString( _nResId );
164 OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
165 const sal_Char* _pAsciiPatternToReplace, const OUString& _rStringToSubstitute ) const
167 OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
168 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) );
169 return sString;
173 OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
174 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
175 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2 ) const
177 OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
178 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
179 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
180 return sString;
184 OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
185 const sal_Char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
186 const sal_Char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2,
187 const sal_Char* _pAsciiPatternToReplace3, const OUString& _rStringToSubstitute3 ) const
189 OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
190 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
191 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
192 OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) );
193 return sString;
196 OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
197 const ::std::list< ::std::pair<const sal_Char* , OUString > >& _rStringToSubstitutes) const
199 OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
200 ::std::list< ::std::pair<const sal_Char* , OUString > >::const_iterator aIter = _rStringToSubstitutes.begin();
201 ::std::list< ::std::pair<const sal_Char* , OUString > >::const_iterator aEnd = _rStringToSubstitutes.end();
202 for(;aIter != aEnd; ++aIter)
203 OSL_VERIFY( lcl_substitute( sString, aIter->first, aIter->second ) );
205 return sString;
209 } // namespace connectivity
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */