1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "resource/sharedresources.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
29cdf0e10cSrcweir #include <comphelper/officeresourcebundle.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /** === begin UNO includes === **/
32cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
33cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
34cdf0e10cSrcweir /** === end UNO includes === **/
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <tools/diagnose_ex.h>
37cdf0e10cSrcweir #include <osl/diagnose.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //........................................................................
40cdf0e10cSrcweir namespace connectivity
41cdf0e10cSrcweir {
42cdf0e10cSrcweir //........................................................................
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	/** === begin UNO using === **/
45cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
46cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
47cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
48cdf0e10cSrcweir     using ::com::sun::star::uno::XComponentContext;
49cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
50cdf0e10cSrcweir 	/** === end UNO using === **/
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 	//====================================================================
53cdf0e10cSrcweir 	//= SharedResources_Impl
54cdf0e10cSrcweir 	//====================================================================
55cdf0e10cSrcweir     class SharedResources_Impl
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir     private:
58cdf0e10cSrcweir         static  SharedResources_Impl*   s_pInstance;
59cdf0e10cSrcweir         static  oslInterlockedCount     s_nClients;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     private:
62cdf0e10cSrcweir         ::std::auto_ptr< ::comphelper::OfficeResourceBundle >
63cdf0e10cSrcweir                                         m_pResourceBundle;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     public:
66cdf0e10cSrcweir         static void     registerClient();
67cdf0e10cSrcweir         static void     revokeClient();
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         static SharedResources_Impl&
70cdf0e10cSrcweir                         getInstance();
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         ::rtl::OUString getResourceString( ResourceId _nId );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     private:
75cdf0e10cSrcweir         SharedResources_Impl();
76cdf0e10cSrcweir 
getMutex()77cdf0e10cSrcweir         static ::osl::Mutex& getMutex()
78cdf0e10cSrcweir         {
79cdf0e10cSrcweir             static ::osl::Mutex s_aMutex;
80cdf0e10cSrcweir             return s_aMutex;
81cdf0e10cSrcweir         }
82cdf0e10cSrcweir     };
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	//--------------------------------------------------------------------
85cdf0e10cSrcweir     SharedResources_Impl*   SharedResources_Impl::s_pInstance( NULL );
86cdf0e10cSrcweir     oslInterlockedCount     SharedResources_Impl::s_nClients( 0 );
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	//--------------------------------------------------------------------
SharedResources_Impl()89cdf0e10cSrcweir     SharedResources_Impl::SharedResources_Impl()
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         try
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             Reference< XPropertySet > xFactoryProps(
94cdf0e10cSrcweir                 ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW );
95cdf0e10cSrcweir             Reference< XComponentContext > xContext(
96cdf0e10cSrcweir                 xFactoryProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) ),
97cdf0e10cSrcweir                 UNO_QUERY_THROW
98cdf0e10cSrcweir             );
99cdf0e10cSrcweir             m_pResourceBundle.reset( new ::comphelper::OfficeResourceBundle( xContext, "cnr" ) );
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir         catch( const Exception& )
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             DBG_UNHANDLED_EXCEPTION();
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     //--------------------------------------------------------------------
getResourceString(ResourceId _nId)108cdf0e10cSrcweir     ::rtl::OUString SharedResources_Impl::getResourceString( ResourceId _nId )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         if ( m_pResourceBundle.get() == NULL )
111cdf0e10cSrcweir             // this should never happen, but we gracefully ignore it. It has been reported
112cdf0e10cSrcweir             // in the constructor in non-product builds.
113cdf0e10cSrcweir             return ::rtl::OUString();
114cdf0e10cSrcweir 
115cdf0e10cSrcweir         return m_pResourceBundle->loadString( _nId );
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	//--------------------------------------------------------------------
registerClient()119cdf0e10cSrcweir     void SharedResources_Impl::registerClient()
120cdf0e10cSrcweir     {
121cdf0e10cSrcweir         osl_incrementInterlockedCount( &s_nClients );
122cdf0e10cSrcweir     }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	//--------------------------------------------------------------------
revokeClient()125cdf0e10cSrcweir     void SharedResources_Impl::revokeClient()
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
128cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &s_nClients ) )
129cdf0e10cSrcweir         {
130cdf0e10cSrcweir             delete s_pInstance;
131cdf0e10cSrcweir             s_pInstance = NULL;
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	//--------------------------------------------------------------------
getInstance()136cdf0e10cSrcweir     SharedResources_Impl& SharedResources_Impl::getInstance()
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         ::osl::MutexGuard aGuard( getMutex() );
139cdf0e10cSrcweir         OSL_ENSURE( s_nClients > 0, "SharedResources_Impl::getInstance: no active clients!" );
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         if ( !s_pInstance )
142cdf0e10cSrcweir             s_pInstance = new SharedResources_Impl;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         return *s_pInstance;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     //====================================================================
148cdf0e10cSrcweir 	//= helpers
149cdf0e10cSrcweir 	//====================================================================
150cdf0e10cSrcweir     namespace
151cdf0e10cSrcweir     {
lcl_substitute(::rtl::OUString & _inout_rString,const sal_Char * _pAsciiPattern,const::rtl::OUString & _rReplace)152cdf0e10cSrcweir         size_t lcl_substitute( ::rtl::OUString& _inout_rString,
153cdf0e10cSrcweir             const sal_Char* _pAsciiPattern, const ::rtl::OUString& _rReplace )
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             size_t nOccurences = 0;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             ::rtl::OUString sPattern( ::rtl::OUString::createFromAscii( _pAsciiPattern ) );
158cdf0e10cSrcweir             sal_Int32 nIndex = 0;
159cdf0e10cSrcweir             while ( ( nIndex = _inout_rString.indexOf( sPattern ) ) > -1 )
160cdf0e10cSrcweir             {
161cdf0e10cSrcweir                 ++nOccurences;
162cdf0e10cSrcweir                 _inout_rString = _inout_rString.replaceAt( nIndex, sPattern.getLength(), _rReplace );
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             return nOccurences;
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     //====================================================================
170cdf0e10cSrcweir 	//= SharedResources
171cdf0e10cSrcweir 	//====================================================================
172cdf0e10cSrcweir 	//--------------------------------------------------------------------
SharedResources()173cdf0e10cSrcweir     SharedResources::SharedResources()
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         SharedResources_Impl::registerClient();
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	//--------------------------------------------------------------------
~SharedResources()179cdf0e10cSrcweir     SharedResources::~SharedResources()
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         SharedResources_Impl::revokeClient();
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	//--------------------------------------------------------------------
getResourceString(ResourceId _nResId) const185cdf0e10cSrcweir     ::rtl::OUString SharedResources::getResourceString( ResourceId _nResId ) const
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         return SharedResources_Impl::getInstance().getResourceString( _nResId );
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	//--------------------------------------------------------------------
getResourceStringWithSubstitution(ResourceId _nResId,const sal_Char * _pAsciiPatternToReplace,const::rtl::OUString & _rStringToSubstitute) const191cdf0e10cSrcweir     ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
192cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace, const ::rtl::OUString& _rStringToSubstitute ) const
193cdf0e10cSrcweir     {
194cdf0e10cSrcweir         ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
195cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace, _rStringToSubstitute ) );
196cdf0e10cSrcweir         return sString;
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 	//--------------------------------------------------------------------
getResourceStringWithSubstitution(ResourceId _nResId,const sal_Char * _pAsciiPatternToReplace1,const::rtl::OUString & _rStringToSubstitute1,const sal_Char * _pAsciiPatternToReplace2,const::rtl::OUString & _rStringToSubstitute2) const200cdf0e10cSrcweir     ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
201cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
202cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2 ) const
203cdf0e10cSrcweir     {
204cdf0e10cSrcweir         ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
205cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
206cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
207cdf0e10cSrcweir         return sString;
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 	//--------------------------------------------------------------------
getResourceStringWithSubstitution(ResourceId _nResId,const sal_Char * _pAsciiPatternToReplace1,const::rtl::OUString & _rStringToSubstitute1,const sal_Char * _pAsciiPatternToReplace2,const::rtl::OUString & _rStringToSubstitute2,const sal_Char * _pAsciiPatternToReplace3,const::rtl::OUString & _rStringToSubstitute3) const211cdf0e10cSrcweir     ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
212cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace1, const ::rtl::OUString& _rStringToSubstitute1,
213cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace2, const ::rtl::OUString& _rStringToSubstitute2,
214cdf0e10cSrcweir                 const sal_Char* _pAsciiPatternToReplace3, const ::rtl::OUString& _rStringToSubstitute3 ) const
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
217cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace1, _rStringToSubstitute1 ) );
218cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace2, _rStringToSubstitute2 ) );
219cdf0e10cSrcweir         OSL_VERIFY( lcl_substitute( sString, _pAsciiPatternToReplace3, _rStringToSubstitute3 ) );
220cdf0e10cSrcweir         return sString;
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir     //--------------------------------------------------------------------
getResourceStringWithSubstitution(ResourceId _nResId,const::std::list<::std::pair<const sal_Char *,::rtl::OUString>> _aStringToSubstitutes) const223cdf0e10cSrcweir     ::rtl::OUString SharedResources::getResourceStringWithSubstitution( ResourceId _nResId,
224cdf0e10cSrcweir                     const ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > > _aStringToSubstitutes) const
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         ::rtl::OUString sString( SharedResources_Impl::getInstance().getResourceString( _nResId ) );
227cdf0e10cSrcweir         ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aIter = _aStringToSubstitutes.begin();
228cdf0e10cSrcweir         ::std::list< ::std::pair<const sal_Char* , ::rtl::OUString > >::const_iterator aEnd  = _aStringToSubstitutes.end();
229cdf0e10cSrcweir         for(;aIter != aEnd; ++aIter)
230cdf0e10cSrcweir             OSL_VERIFY( lcl_substitute( sString, aIter->first, aIter->second ) );
231cdf0e10cSrcweir 
232cdf0e10cSrcweir         return sString;
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir 
235cdf0e10cSrcweir //........................................................................
236cdf0e10cSrcweir } // namespace connectivity
237cdf0e10cSrcweir //........................................................................
238cdf0e10cSrcweir 
239