1*2722ceddSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2722ceddSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2722ceddSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2722ceddSAndrew Rist * distributed with this work for additional information
6*2722ceddSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2722ceddSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2722ceddSAndrew Rist * "License"); you may not use this file except in compliance
9*2722ceddSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*2722ceddSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*2722ceddSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2722ceddSAndrew Rist * software distributed under the License is distributed on an
15*2722ceddSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2722ceddSAndrew Rist * KIND, either express or implied. See the License for the
17*2722ceddSAndrew Rist * specific language governing permissions and limitations
18*2722ceddSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*2722ceddSAndrew Rist *************************************************************/
21*2722ceddSAndrew Rist
22*2722ceddSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_desktop.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "dp_misc.h"
28cdf0e10cSrcweir #include "dp_resource.h"
29cdf0e10cSrcweir #include "osl/module.hxx"
30cdf0e10cSrcweir #include "osl/mutex.hxx"
31cdf0e10cSrcweir #include "rtl/ustring.h"
32cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
33cdf0e10cSrcweir #include "unotools/configmgr.hxx"
34cdf0e10cSrcweir
35cdf0e10cSrcweir
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir using namespace ::com::sun::star::uno;
38cdf0e10cSrcweir using ::rtl::OUString;
39cdf0e10cSrcweir
40cdf0e10cSrcweir namespace dp_misc {
41cdf0e10cSrcweir namespace {
42cdf0e10cSrcweir
43cdf0e10cSrcweir struct OfficeLocale :
44cdf0e10cSrcweir public rtl::StaticWithInit<const OUString, OfficeLocale> {
operator ()dp_misc::__anon6239d44e0111::OfficeLocale45cdf0e10cSrcweir const OUString operator () () {
46cdf0e10cSrcweir OUString slang;
47cdf0e10cSrcweir if (! (::utl::ConfigManager::GetDirectConfigProperty(
48cdf0e10cSrcweir ::utl::ConfigManager::LOCALE ) >>= slang))
49cdf0e10cSrcweir throw RuntimeException( OUSTR("Cannot determine language!"), 0 );
50cdf0e10cSrcweir //fallback, the locale is currently only set when the user starts the
51cdf0e10cSrcweir //office for the first time.
52cdf0e10cSrcweir if (slang.getLength() == 0)
53cdf0e10cSrcweir slang = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
54cdf0e10cSrcweir return slang;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir };
57cdf0e10cSrcweir
58cdf0e10cSrcweir struct DeploymentResMgr : public rtl::StaticWithInit<
59cdf0e10cSrcweir ResMgr *, DeploymentResMgr> {
operator ()dp_misc::__anon6239d44e0111::DeploymentResMgr60cdf0e10cSrcweir ResMgr * operator () () {
61cdf0e10cSrcweir return ResMgr::CreateResMgr( "deployment", getOfficeLocale() );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir };
64cdf0e10cSrcweir
65cdf0e10cSrcweir osl::Mutex s_mutex;
66cdf0e10cSrcweir
67cdf0e10cSrcweir } // anon namespace
68cdf0e10cSrcweir
69cdf0e10cSrcweir //==============================================================================
getResId(sal_uInt16 id)70cdf0e10cSrcweir ResId getResId( sal_uInt16 id )
71cdf0e10cSrcweir {
72cdf0e10cSrcweir const osl::MutexGuard guard( s_mutex );
73cdf0e10cSrcweir return ResId( id, *DeploymentResMgr::get() );
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir //==============================================================================
getResourceString(sal_uInt16 id)77cdf0e10cSrcweir String getResourceString( sal_uInt16 id )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir const osl::MutexGuard guard( s_mutex );
80cdf0e10cSrcweir String ret( ResId( id, *DeploymentResMgr::get() ) );
81cdf0e10cSrcweir if (ret.SearchAscii( "%PRODUCTNAME" ) != STRING_NOTFOUND) {
82cdf0e10cSrcweir static String s_brandName;
83cdf0e10cSrcweir if (s_brandName.Len() == 0) {
84cdf0e10cSrcweir OUString brandName(
85cdf0e10cSrcweir ::utl::ConfigManager::GetDirectConfigProperty(
86cdf0e10cSrcweir ::utl::ConfigManager::PRODUCTNAME ).get<OUString>() );
87cdf0e10cSrcweir s_brandName = brandName;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir ret.SearchAndReplaceAllAscii( "%PRODUCTNAME", s_brandName );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir return ret;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir
94cdf0e10cSrcweir //throws an Exception on failure
95cdf0e10cSrcweir //primary subtag 2 or three letters(A-Z, a-z), i or x
checkPrimarySubtag(::rtl::OUString const & tag)96cdf0e10cSrcweir void checkPrimarySubtag(::rtl::OUString const & tag)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir sal_Int32 len = tag.getLength();
99cdf0e10cSrcweir sal_Unicode const * arLang = tag.getStr();
100cdf0e10cSrcweir if (len < 1 || len > 3)
101cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
102cdf0e10cSrcweir
103cdf0e10cSrcweir if (len == 1
104cdf0e10cSrcweir && (arLang[0] != 'i' && arLang[0] != 'x'))
105cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
106cdf0e10cSrcweir
107cdf0e10cSrcweir if (len == 2 || len == 3)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir for (sal_Int32 i = 0; i < len; i++)
110cdf0e10cSrcweir {
111cdf0e10cSrcweir if ( !((arLang[i] >= 'A' && arLang[i] <= 'Z')
112cdf0e10cSrcweir || (arLang[i] >= 'a' && arLang[i] <= 'z')))
113cdf0e10cSrcweir {
114cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
115cdf0e10cSrcweir }
116cdf0e10cSrcweir }
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir //throws an Exception on failure
121cdf0e10cSrcweir //second subtag 2 letter country code or 3-8 letter other code(A-Z, a-z, 0-9)
checkSecondSubtag(::rtl::OUString const & tag,bool & bIsCountry)122cdf0e10cSrcweir void checkSecondSubtag(::rtl::OUString const & tag, bool & bIsCountry)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir sal_Int32 len = tag.getLength();
125cdf0e10cSrcweir sal_Unicode const * arLang = tag.getStr();
126cdf0e10cSrcweir if (len < 2 || len > 8)
127cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
128cdf0e10cSrcweir //country code
129cdf0e10cSrcweir bIsCountry = false;
130cdf0e10cSrcweir if (len == 2)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir for (sal_Int32 i = 0; i < 2; i++)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
135cdf0e10cSrcweir || (arLang[i] >= 'a' && arLang[i] <= 'z')))
136cdf0e10cSrcweir {
137cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
138cdf0e10cSrcweir }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir bIsCountry = true;
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
143cdf0e10cSrcweir if (len > 2)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir for (sal_Int32 i = 0; i < len; i++)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
148cdf0e10cSrcweir || (arLang[i] >= 'a' && arLang[i] <= 'z')
149cdf0e10cSrcweir || (arLang[i] >= '0' && arLang[i] <= '9') ))
150cdf0e10cSrcweir {
151cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
152cdf0e10cSrcweir }
153cdf0e10cSrcweir }
154cdf0e10cSrcweir }
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
checkThirdSubtag(::rtl::OUString const & tag)157cdf0e10cSrcweir void checkThirdSubtag(::rtl::OUString const & tag)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir sal_Int32 len = tag.getLength();
160cdf0e10cSrcweir sal_Unicode const * arLang = tag.getStr();
161cdf0e10cSrcweir if (len < 1 || len > 8)
162cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
163cdf0e10cSrcweir
164cdf0e10cSrcweir for (sal_Int32 i = 0; i < len; i++)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
167cdf0e10cSrcweir || (arLang[i] >= 'a' && arLang[i] <= 'z')
168cdf0e10cSrcweir || (arLang[i] >= '0' && arLang[i] <= '9') ))
169cdf0e10cSrcweir {
170cdf0e10cSrcweir throw Exception(OUSTR("Invalid language string."), 0);
171cdf0e10cSrcweir }
172cdf0e10cSrcweir }
173cdf0e10cSrcweir }
174cdf0e10cSrcweir
175cdf0e10cSrcweir //=============================================================================
176cdf0e10cSrcweir
177cdf0e10cSrcweir //We parse the string acording to RFC 3066
178cdf0e10cSrcweir //We only use the primary sub-tag and two subtags. That is lang-country-variant
179cdf0e10cSrcweir //We do some simple tests if the string is correct. Actually this should do a
180cdf0e10cSrcweir //validating parser
181cdf0e10cSrcweir //We may have the case that there is no country tag, for example en-welsh
toLocale(::rtl::OUString const & slang)182cdf0e10cSrcweir ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir OUString _sLang = slang.trim();
185cdf0e10cSrcweir ::com::sun::star::lang::Locale locale;
186cdf0e10cSrcweir sal_Int32 nIndex = 0;
187cdf0e10cSrcweir OUString lang = _sLang.getToken( 0, '-', nIndex );
188cdf0e10cSrcweir checkPrimarySubtag(lang);
189cdf0e10cSrcweir locale.Language = lang;
190cdf0e10cSrcweir OUString country = _sLang.getToken( 0, '-', nIndex );
191cdf0e10cSrcweir if (country.getLength() > 0)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir bool bIsCountry = false;
194cdf0e10cSrcweir checkSecondSubtag(country, bIsCountry);
195cdf0e10cSrcweir if (bIsCountry)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir locale.Country = country;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir else
200cdf0e10cSrcweir {
201cdf0e10cSrcweir locale.Variant = country;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir }
204cdf0e10cSrcweir if (locale.Variant.getLength() == 0)
205cdf0e10cSrcweir {
206cdf0e10cSrcweir OUString variant = _sLang.getToken( 0, '-', nIndex );
207cdf0e10cSrcweir if (variant.getLength() > 0)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir checkThirdSubtag(variant);
210cdf0e10cSrcweir locale.Variant = variant;
211cdf0e10cSrcweir }
212cdf0e10cSrcweir }
213cdf0e10cSrcweir
214cdf0e10cSrcweir return locale;
215cdf0e10cSrcweir }
216cdf0e10cSrcweir
217cdf0e10cSrcweir //==============================================================================
getOfficeLocale()218cdf0e10cSrcweir lang::Locale getOfficeLocale()
219cdf0e10cSrcweir {
220cdf0e10cSrcweir return toLocale(OfficeLocale::get());
221cdf0e10cSrcweir }
222cdf0e10cSrcweir
getOfficeLocaleString()223cdf0e10cSrcweir ::rtl::OUString getOfficeLocaleString()
224cdf0e10cSrcweir {
225cdf0e10cSrcweir return OfficeLocale::get();
226cdf0e10cSrcweir }
227cdf0e10cSrcweir
228cdf0e10cSrcweir }
229cdf0e10cSrcweir
230