xref: /trunk/main/desktop/source/deployment/misc/dp_resource.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_desktop.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "dp_misc.h"
32*cdf0e10cSrcweir #include "dp_resource.h"
33*cdf0e10cSrcweir #include "osl/module.hxx"
34*cdf0e10cSrcweir #include "osl/mutex.hxx"
35*cdf0e10cSrcweir #include "rtl/ustring.h"
36*cdf0e10cSrcweir #include "cppuhelper/implbase1.hxx"
37*cdf0e10cSrcweir #include "unotools/configmgr.hxx"
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::com::sun::star;
41*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace dp_misc {
45*cdf0e10cSrcweir namespace {
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir struct OfficeLocale :
48*cdf0e10cSrcweir         public rtl::StaticWithInit<const OUString, OfficeLocale> {
49*cdf0e10cSrcweir     const OUString operator () () {
50*cdf0e10cSrcweir         OUString slang;
51*cdf0e10cSrcweir         if (! (::utl::ConfigManager::GetDirectConfigProperty(
52*cdf0e10cSrcweir                    ::utl::ConfigManager::LOCALE ) >>= slang))
53*cdf0e10cSrcweir             throw RuntimeException( OUSTR("Cannot determine language!"), 0 );
54*cdf0e10cSrcweir         //fallback, the locale is currently only set when the user starts the
55*cdf0e10cSrcweir         //office for the first time.
56*cdf0e10cSrcweir         if (slang.getLength() == 0)
57*cdf0e10cSrcweir             slang =  rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
58*cdf0e10cSrcweir         return slang;
59*cdf0e10cSrcweir     }
60*cdf0e10cSrcweir };
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir struct DeploymentResMgr : public rtl::StaticWithInit<
63*cdf0e10cSrcweir     ResMgr *, DeploymentResMgr> {
64*cdf0e10cSrcweir     ResMgr * operator () () {
65*cdf0e10cSrcweir         return ResMgr::CreateResMgr( "deployment", getOfficeLocale() );
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir };
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir osl::Mutex s_mutex;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir } // anon namespace
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir //==============================================================================
74*cdf0e10cSrcweir ResId getResId( sal_uInt16 id )
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir     const osl::MutexGuard guard( s_mutex );
77*cdf0e10cSrcweir     return ResId( id, *DeploymentResMgr::get() );
78*cdf0e10cSrcweir }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //==============================================================================
81*cdf0e10cSrcweir String getResourceString( sal_uInt16 id )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     const osl::MutexGuard guard( s_mutex );
84*cdf0e10cSrcweir     String ret( ResId( id, *DeploymentResMgr::get() ) );
85*cdf0e10cSrcweir     if (ret.SearchAscii( "%PRODUCTNAME" ) != STRING_NOTFOUND) {
86*cdf0e10cSrcweir         static String s_brandName;
87*cdf0e10cSrcweir         if (s_brandName.Len() == 0) {
88*cdf0e10cSrcweir             OUString brandName(
89*cdf0e10cSrcweir                 ::utl::ConfigManager::GetDirectConfigProperty(
90*cdf0e10cSrcweir                     ::utl::ConfigManager::PRODUCTNAME ).get<OUString>() );
91*cdf0e10cSrcweir             s_brandName = brandName;
92*cdf0e10cSrcweir         }
93*cdf0e10cSrcweir         ret.SearchAndReplaceAllAscii( "%PRODUCTNAME", s_brandName );
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     return ret;
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir //throws an Exception on failure
99*cdf0e10cSrcweir //primary subtag 2 or three letters(A-Z, a-z), i or x
100*cdf0e10cSrcweir void checkPrimarySubtag(::rtl::OUString const & tag)
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     sal_Int32 len = tag.getLength();
103*cdf0e10cSrcweir     sal_Unicode const * arLang = tag.getStr();
104*cdf0e10cSrcweir     if (len < 1 || len > 3)
105*cdf0e10cSrcweir         throw Exception(OUSTR("Invalid language string."), 0);
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     if (len == 1
108*cdf0e10cSrcweir         && (arLang[0] != 'i' && arLang[0] != 'x'))
109*cdf0e10cSrcweir         throw Exception(OUSTR("Invalid language string."), 0);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     if (len == 2 || len == 3)
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         for (sal_Int32 i = 0; i < len; i++)
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             if ( !((arLang[i] >= 'A' && arLang[i] <= 'Z')
116*cdf0e10cSrcweir                 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
117*cdf0e10cSrcweir             {
118*cdf0e10cSrcweir                 throw Exception(OUSTR("Invalid language string."), 0);
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir //throws an Exception on failure
125*cdf0e10cSrcweir //second subtag 2 letter country code or 3-8 letter other code(A-Z, a-z, 0-9)
126*cdf0e10cSrcweir void checkSecondSubtag(::rtl::OUString const & tag, bool & bIsCountry)
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir     sal_Int32 len = tag.getLength();
129*cdf0e10cSrcweir     sal_Unicode const * arLang = tag.getStr();
130*cdf0e10cSrcweir     if (len < 2 || len > 8)
131*cdf0e10cSrcweir         throw Exception(OUSTR("Invalid language string."), 0);
132*cdf0e10cSrcweir     //country code
133*cdf0e10cSrcweir     bIsCountry = false;
134*cdf0e10cSrcweir     if (len == 2)
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir         for (sal_Int32 i = 0; i < 2; i++)
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
139*cdf0e10cSrcweir                 || (arLang[i] >= 'a' && arLang[i] <= 'z')))
140*cdf0e10cSrcweir             {
141*cdf0e10cSrcweir                 throw Exception(OUSTR("Invalid language string."), 0);
142*cdf0e10cSrcweir             }
143*cdf0e10cSrcweir         }
144*cdf0e10cSrcweir         bIsCountry = true;
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     if (len > 2)
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         for (sal_Int32 i = 0; i < len; i++)
150*cdf0e10cSrcweir         {
151*cdf0e10cSrcweir             if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
152*cdf0e10cSrcweir                 || (arLang[i] >= 'a' && arLang[i] <= 'z')
153*cdf0e10cSrcweir                 || (arLang[i] >= '0' && arLang[i] <= '9') ))
154*cdf0e10cSrcweir             {
155*cdf0e10cSrcweir                 throw Exception(OUSTR("Invalid language string."), 0);
156*cdf0e10cSrcweir             }
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir void checkThirdSubtag(::rtl::OUString const & tag)
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     sal_Int32 len = tag.getLength();
164*cdf0e10cSrcweir     sal_Unicode const * arLang = tag.getStr();
165*cdf0e10cSrcweir     if (len < 1 || len > 8)
166*cdf0e10cSrcweir         throw Exception(OUSTR("Invalid language string."), 0);
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     for (sal_Int32 i = 0; i < len; i++)
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         if (!( (arLang[i] >= 'A' && arLang[i] <= 'Z')
171*cdf0e10cSrcweir             || (arLang[i] >= 'a' && arLang[i] <= 'z')
172*cdf0e10cSrcweir             || (arLang[i] >= '0' && arLang[i] <= '9') ))
173*cdf0e10cSrcweir         {
174*cdf0e10cSrcweir             throw Exception(OUSTR("Invalid language string."), 0);
175*cdf0e10cSrcweir         }
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir //=============================================================================
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir //We parse the string acording to RFC 3066
182*cdf0e10cSrcweir //We only use the primary sub-tag and two subtags. That is lang-country-variant
183*cdf0e10cSrcweir //We do some simple tests if the string is correct. Actually this should do a
184*cdf0e10cSrcweir //validating parser
185*cdf0e10cSrcweir //We may have the case that there is no country tag, for example en-welsh
186*cdf0e10cSrcweir ::com::sun::star::lang::Locale toLocale( ::rtl::OUString const & slang )
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir     OUString _sLang = slang.trim();
189*cdf0e10cSrcweir     ::com::sun::star::lang::Locale locale;
190*cdf0e10cSrcweir     sal_Int32 nIndex = 0;
191*cdf0e10cSrcweir     OUString lang = _sLang.getToken( 0, '-', nIndex );
192*cdf0e10cSrcweir     checkPrimarySubtag(lang);
193*cdf0e10cSrcweir     locale.Language = lang;
194*cdf0e10cSrcweir     OUString country = _sLang.getToken( 0, '-', nIndex );
195*cdf0e10cSrcweir     if (country.getLength() > 0)
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         bool bIsCountry = false;
198*cdf0e10cSrcweir         checkSecondSubtag(country, bIsCountry);
199*cdf0e10cSrcweir         if (bIsCountry)
200*cdf0e10cSrcweir         {
201*cdf0e10cSrcweir             locale.Country = country;
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir         else
204*cdf0e10cSrcweir         {
205*cdf0e10cSrcweir              locale.Variant = country;
206*cdf0e10cSrcweir         }
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir     if (locale.Variant.getLength() == 0)
209*cdf0e10cSrcweir     {
210*cdf0e10cSrcweir         OUString variant = _sLang.getToken( 0, '-', nIndex );
211*cdf0e10cSrcweir         if (variant.getLength() > 0)
212*cdf0e10cSrcweir         {
213*cdf0e10cSrcweir             checkThirdSubtag(variant);
214*cdf0e10cSrcweir             locale.Variant = variant;
215*cdf0e10cSrcweir         }
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     return locale;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir //==============================================================================
222*cdf0e10cSrcweir lang::Locale getOfficeLocale()
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     return toLocale(OfficeLocale::get());
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir ::rtl::OUString getOfficeLocaleString()
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir     return OfficeLocale::get();
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234