LibreOffice Module connectivity (master) 1
sharedresources.cxx
Go to the documentation of this file.
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
21
22#include <unotools/resmgr.hxx>
23#include <osl/diagnose.h>
24#include <osl/mutex.hxx>
25#include <sal/log.hxx>
26
27namespace connectivity
28{
29 namespace {
30
31 class SharedResources_Impl
32 {
33 private:
34 static SharedResources_Impl* s_pInstance;
35 static oslInterlockedCount s_nClients;
36
37 private:
38 std::locale m_aLocale;
39
40 public:
41 static void registerClient();
42 static void revokeClient();
43
44 static SharedResources_Impl&
45 getInstance();
46
47 OUString getResourceString(TranslateId pId) const;
48
49 private:
50 SharedResources_Impl();
51
52 static ::osl::Mutex& getMutex()
53 {
54 static ::osl::Mutex s_aMutex;
55 return s_aMutex;
56 }
57 };
58
59 }
60
61 SharedResources_Impl* SharedResources_Impl::s_pInstance( nullptr );
62 oslInterlockedCount SharedResources_Impl::s_nClients( 0 );
63
64 SharedResources_Impl::SharedResources_Impl()
65 : m_aLocale(Translate::Create("cnr"))
66 {
67 }
68
69 OUString SharedResources_Impl::getResourceString(TranslateId pId) const
70 {
71 return Translate::get(pId, m_aLocale);
72 }
73
74 void SharedResources_Impl::registerClient()
75 {
76 osl_atomic_increment( &s_nClients );
77 }
78
79 void SharedResources_Impl::revokeClient()
80 {
81 ::osl::MutexGuard aGuard( getMutex() );
82 if ( 0 == osl_atomic_decrement( &s_nClients ) )
83 {
84 delete s_pInstance;
85 s_pInstance = nullptr;
86 }
87 }
88
89
90 SharedResources_Impl& SharedResources_Impl::getInstance()
91 {
92 ::osl::MutexGuard aGuard( getMutex() );
93 OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
94
95 if ( !s_pInstance )
96 s_pInstance = new SharedResources_Impl;
97
98 return *s_pInstance;
99 }
100
101 namespace
102 {
103 bool lcl_substitute( OUString& _inout_rString,
104 const char* _pAsciiPattern, std::u16string_view _rReplace )
105 {
106 OUString oldString = _inout_rString;
107 OUString sPattern( OUString::createFromAscii( _pAsciiPattern ) );
108 _inout_rString = _inout_rString.replaceAll(sPattern, _rReplace);
109 return oldString != _inout_rString;
110 }
111 }
112
113 SharedResources::SharedResources()
114 {
115 SharedResources_Impl::registerClient();
116 }
117
118
119 SharedResources::~SharedResources()
120 {
121 SharedResources_Impl::revokeClient();
122 }
123
124
125 OUString SharedResources::getResourceString(TranslateId pResId) const
126 {
127 return SharedResources_Impl::getInstance().getResourceString(pResId);
128 }
129
130
131 OUString SharedResources::getResourceStringWithSubstitution(TranslateId pResId,
132 const char* _pAsciiPatternToReplace, const OUString& _rStringToSubstitute ) const
133 {
134 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
135 if ( !lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) )
136 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace << " with " << _rStringToSubstitute);
137 return sString;
138 }
139
140
141 OUString SharedResources::getResourceStringWithSubstitution(TranslateId pResId,
142 const char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
143 const char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2 ) const
144 {
145 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
146 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
147 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1 << " with " << _rStringToSubstitute1);
148 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
149 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2 << " with " << _rStringToSubstitute2);
150 return sString;
151 }
152
153
154 OUString SharedResources::getResourceStringWithSubstitution(TranslateId pResId,
155 const char* _pAsciiPatternToReplace1, const OUString& _rStringToSubstitute1,
156 const char* _pAsciiPatternToReplace2, const OUString& _rStringToSubstitute2,
157 const char* _pAsciiPatternToReplace3, const OUString& _rStringToSubstitute3 ) const
158 {
159 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
160 if( !lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) )
161 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace1 << " with " << _rStringToSubstitute1);
162 if( !lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) )
163 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace2 << " with " << _rStringToSubstitute2);
164 if( !lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) )
165 SAL_WARN("connectivity.resource", "Unable to substitute " << _pAsciiPatternToReplace3 << " with " << _rStringToSubstitute3);
166 return sString;
167 }
168
169 OUString SharedResources::getResourceStringWithSubstitution(TranslateId pResId,
170 const std::vector< std::pair<const char* , OUString > >& _rStringToSubstitutes) const
171 {
172 OUString sString( SharedResources_Impl::getInstance().getResourceString(pResId) );
173 for(const auto& [rPattern, rReplace] : _rStringToSubstitutes)
174 if( !lcl_substitute( sString, rPattern, rReplace ) )
175 SAL_WARN("connectivity.resource", "Unable to substitute " << rPattern << " with " << rReplace);
176
177 return sString;
178 }
179
180
181} // namespace connectivity
182
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_WARN(area, stream)
OUString get(TranslateId sContextAndId, const std::locale &loc)
::osl::Mutex & getMutex()
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
static oslInterlockedCount s_nClients
static SharedResources_Impl * s_pInstance
std::locale m_aLocale