summaryrefslogtreecommitdiff
path: root/desktop/source/app/langselect.cxx
blob: aa52874bb784bcd82131a747f46250178d5c2ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*************************************************************************
 *
 *  $RCSfile: langselect.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: hjs $ $Date: 2004-06-25 12:24:45 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include "app.hxx"
#include "langselect.hxx"
#include "langselect.hrc"
#include <stdio.h>

#ifndef _RTL_STRING_HXX
#include <rtl/string.hxx>
#endif
#ifndef _SVTOOLS_PATHOPTIONS_HXX
#include <svtools/pathoptions.hxx>
#endif
#ifndef _TOOLS_RESID_HXX
#include <tools/resid.hxx>
#endif
#ifndef _TOOLS_ISOLANG_HXX
#include <tools/isolang.hxx>
#endif
#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
#include <comphelper/processfactory.hxx>
#endif
#ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
#include <com/sun/star/container/XNameAccess.hpp>
#endif
#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/lang/XLocalizable.hpp>
#include <com/sun/star/lang/Locale.hpp>
#include <rtl/locale.hxx>
#include <osl/process.h>

using namespace rtl;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::container;
using namespace com::sun::star::beans;

namespace desktop {

LanguageSelectionDialog::LanguageSelectionDialog(ResMgr *pResMgr) :
    ModalDialog(NULL,ResId(DLG_LANGSELECT, pResMgr)),
    m_aText(this, ResId(TXT_DLG_LANGSELECT, pResMgr)),
    m_aListBox(this, ResId(LST_DLG_LANGSELECT, pResMgr)),
    m_aButton(this, ResId(BTN_DLG_LANGSELECT_OK, pResMgr))
{    
    FreeResource();
}

IsoList LanguageSelection::m_lLanguages;

// execute the language selection 
// display a dialog if more than one language is installed
// XXX this is a temporary solution
static sal_Bool bFoundLanguage = sal_False;
//static LanguageType aFoundLanguageType = LANGUAGE_DONTKNOW;
static OUString aFoundLanguage;

Locale LanguageSelection::IsoStringToLocale(const OUString& str)
{
    Locale l;
    sal_Int32 index=0;
    l.Language = str.getToken(0, '-', index);
    if (index >= 0) l.Country = str.getToken(0, '-', index);
    if (index >= 0) l.Variant = str.getToken(0, '-', index);
    return l;
}   

void LanguageSelection::prepareLanguage()
{
    // XXX make everything works without assertions in first run
    // in multi-language installations, some things might fail in first run
    if (getUserLanguage().getLength() > 0) return;
    IsoList l = getInstalledIsoLanguages();
    if (l.size() >= 1)
    {
        // throw any away existing default config
        OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
        Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
        Reference< XLocalizable > theConfigProvider(
            theMSF->createInstance( sConfigSrvc ),UNO_QUERY );
        OSL_ENSURE(theConfigProvider.is(), "cannot localize config manager.");
        if (theConfigProvider.is())
        {
            OUString aLocaleString = *l.begin();
            Locale loc = LanguageSelection::IsoStringToLocale(aLocaleString);
            theConfigProvider->setLocale(loc);
        }
    }
}

OUString LanguageSelection::getLanguageString()
{

    if (bFoundLanguage) 
        return aFoundLanguage;

    // check whether there was aleady a language selected by the user
    OUString aUserLanguage = getUserLanguage();
    if (aUserLanguage.getLength() > 0 )
    {
       bFoundLanguage = sal_True;
        aFoundLanguage = aUserLanguage;
        return aFoundLanguage;
    }

    // fill list
    if (m_lLanguages.size() < 1) 
        m_lLanguages = getInstalledIsoLanguages();
    
    if (m_lLanguages.size() > 1) {
        // are there multiple languages installed?
        // get resource
        rtl::OString aMgrName = OString("langselect") + OString::valueOf((sal_Int32)SUPD, 10);
        ::com::sun::star::lang::Locale aLocale;
        ResMgr* pResMgr = ResMgr::SearchCreateResMgr( aMgrName, aLocale );
        LanguageSelectionDialog lsd(pResMgr);
        StrList languages(getLanguageStrings(m_lLanguages));    
        for (StrList::iterator str_iter = languages.begin(); str_iter != languages.end(); str_iter++)
        {
            lsd.m_aListBox.InsertEntry(*str_iter);
        }

        lsd.Execute();
        short nSelected = lsd.m_aListBox.GetSelectEntryPos();
        IsoList::const_iterator i = m_lLanguages.begin();
        for (sal_Int32 n=0; n<nSelected; n++) i++;    
        bFoundLanguage = sal_True;
        aFoundLanguage = *i;
        return aFoundLanguage;
    } else {
        // if there is only one language, use it
        if (m_lLanguages.size() == 1) {
            bFoundLanguage = sal_True;
            aFoundLanguage = *(m_lLanguages.begin());
            return aFoundLanguage;
        } else {
            // last resort
            // don't save
            // what's this ?????
//            return (LanguageType) SvtPathOptions().SubstituteVariable(
//                String::CreateFromAscii("$(langid)")).ToInt32();
            ::com::sun::star::lang::Locale aLocale;
            OUString aLocString( aLocale.Language );
            if ( aLocale.Country.getLength() != 0 )
            {
                aLocString += OUString::createFromAscii("-")
                                + aLocale.Country;
                if ( aLocale.Variant.getLength() != 0 )
                {
                    aLocString += OUString::createFromAscii("-")
                                    + aLocale.Variant;
                }                                
            }                                
            return aLocString;
        }
    }
}


// Get the localized selection strings for the list of languages
StrList LanguageSelection::getLanguageStrings(const IsoList& langLst) 
{
    StrList aList;
    rtl::OString aMgrName = OString("langselect") + OString::valueOf((sal_Int32)SUPD, 10);
    for (IsoList::const_iterator lang_iter = langLst.begin(); lang_iter != langLst.end(); lang_iter++)
    {
        rtl::OUString lang = static_cast<OUString>(*lang_iter);
        ::com::sun::star::lang::Locale aLocale = LanguageSelection::IsoStringToLocale(lang);
        ResMgr* pResMgr = ResMgr::SearchCreateResMgr( aMgrName, aLocale );
        if (pResMgr != NULL) {
            String aString(ResId(STR_LANGSELECT, pResMgr));
            aList.push_back(aString);
            delete pResMgr;
        }
    }
    return aList;
}

// get a language choosen by the user
OUString LanguageSelection::getUserLanguage()
{
    OUString aLanguage;
    try{

        OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
        OUString sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess");
        OUString sConfigURL = OUString::createFromAscii("org.openoffice.Setup/L10N/");    
        OUString sLocales = OUString::createFromAscii("ooLocale");    

        // get configuration provider
        Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
        Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory > (
                theMSF->createInstance( sConfigSrvc ),UNO_QUERY );
        // check provider
        if (!theConfigProvider.is()) return aLanguage;

        // access the provider
        Sequence< Any > theArgs(1);
        theArgs[ 0 ] <<= sConfigURL;
        Reference< XNameAccess > theNameAccess = Reference< XNameAccess > (
                theConfigProvider->createInstanceWithArguments(
                sAccessSrvc, theArgs ), UNO_QUERY );
        // check access
        if (!theNameAccess.is()) return aLanguage;
        // run query
        Any aResult = theNameAccess->getByName( sLocales );
        OUString aLangString;
        if (aResult >>= aLangString) 
        {
            aLanguage = aLangString;
        } 
    } catch (com::sun::star::uno::RuntimeException)
    {
        // didn't work - return dontknow
        return aLanguage;
    }
    return aLanguage;
}    

// get a list with the languages that are installed
IsoList LanguageSelection::getInstalledIsoLanguages()
{
    IsoList aList;
    // read language list from org.openoffice.Setup/Office/ooSetupLocales    
    try{
        OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
        OUString sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess");
        OUString sConfigURL = OUString::createFromAscii("org.openoffice.Setup/Office/");    
        OUString sLocales = OUString::createFromAscii("ooSetupLocales");    

        // get configuration provider
        Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
        Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory > (
                theMSF->createInstance( sConfigSrvc ),UNO_QUERY );
        // check provider
        if (!theConfigProvider.is()) return aList;

        // access the provider
        Sequence< Any > theArgs(1);
        theArgs[ 0 ] <<= sConfigURL;
        Reference< XNameAccess > theNameAccess = Reference< XNameAccess > (
                theConfigProvider->createInstanceWithArguments(
                sAccessSrvc, theArgs ), UNO_QUERY );
        //check access
        if (!theNameAccess.is()) return aList;

        Any aResult = theNameAccess->getByName( sLocales );
        Sequence< OUString > aLangSeq;

        // unpack result from Any type
        if (aResult >>= aLangSeq) 
        {
            for (int i=0; i<aLangSeq.getLength(); i++)
                aList.push_back(aLangSeq[i]);
        } 
    } catch (com::sun::star::uno::RuntimeException)
    {
        // didn't work - return empty list
    }
    return aList;
}

/*
// get a list with the languages that are installed
LangList LanguageSelection::getInstalledLanguages()
{
    LangList aList;
    // read language list from org.openoffice.Setup/Office/ooSetupLocales    
    try{
        OUString sConfigSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider");
        OUString sAccessSrvc = OUString::createFromAscii("com.sun.star.configuration.ConfigurationAccess");
        OUString sConfigURL = OUString::createFromAscii("org.openoffice.Setup/Office/");    
        OUString sLocales = OUString::createFromAscii("ooSetupLocales");    

        // get configuration provider
        Reference< XMultiServiceFactory > theMSF = comphelper::getProcessServiceFactory();
        Reference< XMultiServiceFactory > theConfigProvider = Reference< XMultiServiceFactory > (
                theMSF->createInstance( sConfigSrvc ),UNO_QUERY );
        // check provider
        if (!theConfigProvider.is()) return aList;

        // access the provider
        Sequence< Any > theArgs(1);
        theArgs[ 0 ] <<= sConfigURL;
        Reference< XNameAccess > theNameAccess = Reference< XNameAccess > (
                theConfigProvider->createInstanceWithArguments(
                sAccessSrvc, theArgs ), UNO_QUERY );
        //check access
        if (!theNameAccess.is()) return aList;

        Any aResult = theNameAccess->getByName( sLocales );
        Sequence< OUString > aLangSeq;

        // unpack result from Any type
        if (aResult >>= aLangSeq) 
        {
            for (int i=0; i<aLangSeq.getLength(); i++)
                aList.push_back(ConvertIsoStringToLanguage(aLangSeq[i]));
        } 
    } catch (com::sun::star::uno::RuntimeException)
    {
        // didn't work - return empty list
    }
    return aList;
}
*/
} // namespace desktop