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