Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / resource / sharedresources.cxx
blobea28d480166885e2fdc4bced05fc1e6c39981294
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;
40 //= SharedResources_Impl
42 class SharedResources_Impl
44 private:
45 static SharedResources_Impl* s_pInstance;
46 static oslInterlockedCount s_nClients;
48 private:
49 ::std::auto_ptr< ::comphelper::OfficeResourceBundle >
50 m_pResourceBundle;
52 public:
53 static void registerClient();
54 static void revokeClient();
56 static SharedResources_Impl&
57 getInstance();
59 OUString getResourceString( ResourceId _nId );
61 private:
62 SharedResources_Impl();
64 static ::osl::Mutex& getMutex()
66 static ::osl::Mutex s_aMutex;
67 return s_aMutex;
72 SharedResources_Impl* SharedResources_Impl::s_pInstance( NULL );
73 oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
76 SharedResources_Impl::SharedResources_Impl()
78 try
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.
96 return OUString();
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 ) )
113 delete s_pInstance;
114 s_pInstance = NULL;
119 SharedResources_Impl& SharedResources_Impl::getInstance()
121 ::osl::MutexGuard aGuard( getMutex() );
122 OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
124 if ( !s_pInstance )
125 s_pInstance = new SharedResources_Impl;
127 return *s_pInstance;
131 //= helpers
133 namespace
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 )
144 ++nOccurrences;
145 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
148 return nOccurrences;
153 //= SharedResources
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 ) );
179 return sString;
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 ) );
190 return sString;
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 ) );
203 return sString;
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 ) );
215 return sString;
219 } // namespace connectivity
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */