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 <unotools/resmgr.hxx>
23 #include <osl/diagnose.h>
24 #include <osl/mutex.hxx>
25 #include <sal/log.hxx>
27 namespace connectivity
31 class SharedResources_Impl
34 static SharedResources_Impl
* s_pInstance
;
35 static oslInterlockedCount s_nClients
;
38 std::locale m_aLocale
;
41 static void registerClient();
42 static void revokeClient();
44 static SharedResources_Impl
&
47 OUString
getResourceString(const char* pId
);
50 SharedResources_Impl();
52 static ::osl::Mutex
& getMutex()
54 static ::osl::Mutex s_aMutex
;
61 SharedResources_Impl
* SharedResources_Impl::s_pInstance( nullptr );
62 oslInterlockedCount
SharedResources_Impl::s_nClients( 0 );
64 SharedResources_Impl::SharedResources_Impl()
65 : m_aLocale(Translate::Create("cnr"))
69 OUString
SharedResources_Impl::getResourceString(const char* pId
)
71 return Translate::get(pId
, m_aLocale
);
74 void SharedResources_Impl::registerClient()
76 osl_atomic_increment( &s_nClients
);
79 void SharedResources_Impl::revokeClient()
81 ::osl::MutexGuard
aGuard( getMutex() );
82 if ( 0 == osl_atomic_decrement( &s_nClients
) )
85 s_pInstance
= nullptr;
90 SharedResources_Impl
& SharedResources_Impl::getInstance()
92 ::osl::MutexGuard
aGuard( getMutex() );
93 OSL_ENSURE( s_nClients
> 0, "SharedResources_Impl::getInstance: no active clients!" );
96 s_pInstance
= new SharedResources_Impl
;
103 size_t lcl_substitute( OUString
& _inout_rString
,
104 const char* _pAsciiPattern
, const OUString
& _rReplace
)
106 size_t nOccurrences
= 0;
108 OUString
sPattern( OUString::createFromAscii( _pAsciiPattern
) );
109 sal_Int32 nIndex
= 0;
110 while ( ( nIndex
= _inout_rString
.indexOf( sPattern
) ) > -1 )
113 _inout_rString
= _inout_rString
.replaceAt( nIndex
, sPattern
.getLength(), _rReplace
);
120 SharedResources::SharedResources()
122 SharedResources_Impl::registerClient();
126 SharedResources::~SharedResources()
128 SharedResources_Impl::revokeClient();
132 OUString
SharedResources::getResourceString(const char* pResId
) const
134 return SharedResources_Impl::getInstance().getResourceString(pResId
);
138 OUString
SharedResources::getResourceStringWithSubstitution(const char* pResId
,
139 const char* _pAsciiPatternToReplace
, const OUString
& _rStringToSubstitute
) const
141 OUString
sString( SharedResources_Impl::getInstance().getResourceString(pResId
) );
142 if ( !lcl_substitute( sString
, _pAsciiPatternToReplace
, _rStringToSubstitute
) )
143 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace
<< " with " << _rStringToSubstitute
);
148 OUString
SharedResources::getResourceStringWithSubstitution(const char* pResId
,
149 const char* _pAsciiPatternToReplace1
, const OUString
& _rStringToSubstitute1
,
150 const char* _pAsciiPatternToReplace2
, const OUString
& _rStringToSubstitute2
) const
152 OUString
sString( SharedResources_Impl::getInstance().getResourceString(pResId
) );
153 if( !lcl_substitute( sString
, _pAsciiPatternToReplace1
, _rStringToSubstitute1
) )
154 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1
<< " with " << _rStringToSubstitute1
);
155 if( !lcl_substitute( sString
, _pAsciiPatternToReplace2
, _rStringToSubstitute2
) )
156 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2
<< " with " << _rStringToSubstitute2
);
161 OUString
SharedResources::getResourceStringWithSubstitution(const char* pResId
,
162 const char* _pAsciiPatternToReplace1
, const OUString
& _rStringToSubstitute1
,
163 const char* _pAsciiPatternToReplace2
, const OUString
& _rStringToSubstitute2
,
164 const char* _pAsciiPatternToReplace3
, const OUString
& _rStringToSubstitute3
) const
166 OUString
sString( SharedResources_Impl::getInstance().getResourceString(pResId
) );
167 if( !lcl_substitute( sString
, _pAsciiPatternToReplace1
, _rStringToSubstitute1
) )
168 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1
<< " with " << _rStringToSubstitute1
);
169 if( !lcl_substitute( sString
, _pAsciiPatternToReplace2
, _rStringToSubstitute2
) )
170 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2
<< " with " << _rStringToSubstitute2
);
171 if( !lcl_substitute( sString
, _pAsciiPatternToReplace3
, _rStringToSubstitute3
) )
172 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace3
<< " with " << _rStringToSubstitute3
);
176 OUString
SharedResources::getResourceStringWithSubstitution(const char* pResId
,
177 const std::vector
< std::pair
<const char* , OUString
> >& _rStringToSubstitutes
) const
179 OUString
sString( SharedResources_Impl::getInstance().getResourceString(pResId
) );
180 for(const auto& [rPattern
, rReplace
] : _rStringToSubstitutes
)
181 if( !lcl_substitute( sString
, rPattern
, rReplace
) )
182 SAL_WARN("connectivity.resource", "Unable to substitute " << rPattern
<< " with " << rReplace
);
188 } // namespace connectivity
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */