File: langhelp.cxx

package info (click to toggle)
libreoffice 1:6.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 2,699,584 kB
  • sloc: cpp: 4,078,558; xml: 324,485; java: 281,935; python: 48,236; ansic: 36,476; perl: 32,383; sh: 13,671; yacc: 10,820; makefile: 9,085; cs: 6,600; lex: 2,184; objc: 1,904; awk: 978; pascal: 949; asm: 866; php: 79; csh: 20; sed: 5
file content (162 lines) | stat: -rw-r--r-- 6,241 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/sequence.hxx>
#include <i18nlangtag/mslangid.hxx>
#include <o3tl/string_view.hxx>
#include <officecfg/Office/Common.hxx>
#include <officecfg/System.hxx>
#include <org/freedesktop/PackageKit/SyncDbusSessionHelper.hpp>
#include <rtl/ustring.hxx>
#include <svtools/langhelp.hxx>
#include <vcl/idle.hxx>
#include <vcl/svapp.hxx>
#include <vcl/sysdata.hxx>
#include <vcl/settings.hxx>
#include <vcl/window.hxx>
#include <config_langs.h>
#include <config_vendor.h>

void localizeWebserviceURI( OUString& rURI )
{
    OUString aLang = Application::GetSettings().GetUILanguageTag().getLanguage();
    if ( aLang.equalsIgnoreAsciiCase("pt")
         && Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("br") )
    {
        aLang = "pt-br";
    }
    if ( aLang.equalsIgnoreAsciiCase("zh") )
    {
        if ( Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("cn") )
            aLang = "zh-cn";
        if ( Application::GetSettings().GetUILanguageTag().getCountry().equalsIgnoreAsciiCase("tw") )
            aLang = "zh-tw";
    }

    rURI += aLang;
}

OUString getInstalledLocaleForLanguage(css::uno::Sequence<OUString> const & installed, OUString const & locale)
{
    if (locale.isEmpty())
        return OUString();  // do not attempt to resolve anything

    for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
        if (installed[i] == locale) {
            return installed[i];
        }
    }
    std::vector<OUString> fallbacks(LanguageTag(locale).getFallbackStrings(false));
    for (OUString & rf : fallbacks) {
        for (sal_Int32 i = 0; i != installed.getLength(); ++i) {
            if (installed[i] == rf) {
                return installed[i];
            }
        }
    }
    return OUString();
}

static std::unique_ptr<Idle> xLangpackInstaller;

class InstallLangpack : public Idle
{
    std::vector<OUString> m_aPackages;
public:
    explicit InstallLangpack(const std::vector<OUString>& rPackages)
        : Idle("install langpack")
        , m_aPackages(rPackages)
    {
        SetPriority(TaskPriority::LOWEST);
    }

    virtual void Invoke() override
    {
        vcl::Window* pTopWindow = Application::GetActiveTopWindow();
        if (!pTopWindow)
            pTopWindow = Application::GetFirstTopLevelWindow();
        if (!pTopWindow)
        {
            Start();
            return;
        }
        try
        {
            using namespace org::freedesktop::PackageKit;
            css::uno::Reference<XSyncDbusSessionHelper> xSyncDbusSessionHelper(SyncDbusSessionHelper::create(comphelper::getProcessComponentContext()));
            xSyncDbusSessionHelper->InstallPackageNames(comphelper::containerToSequence(m_aPackages), OUString());
        }
        catch (const css::uno::Exception& e)
        {
            SAL_INFO("svl", "trying to install a LibreOffice langpack, caught " << e);
        }
        xLangpackInstaller.reset();
    }
};

OUString getInstalledLocaleForSystemUILanguage(const css::uno::Sequence<OUString>& rLocaleElementNames, bool bRequestInstallIfMissing, const OUString& rPreferredLocale)
{
    OUString wantedLocale(rPreferredLocale);
    if (wantedLocale.isEmpty())
        wantedLocale = officecfg::System::L10N::UILocale::get();

    OUString locale = getInstalledLocaleForLanguage(rLocaleElementNames, wantedLocale);
    if (bRequestInstallIfMissing && locale.isEmpty() && !wantedLocale.isEmpty() && !Application::IsHeadlessModeEnabled() &&
        officecfg::Office::Common::PackageKit::EnableLangpackInstallation::get())
    {
        LanguageTag aWantedTag(wantedLocale);
        if (aWantedTag.getLanguage() != "en")
        {
            // Get the list of langpacks that this build was configured to include
            std::vector<OUString> aPackages;
            OUString const sAvailableLocales(WITH_LANG);
            std::vector<OUString> aAvailable;
            sal_Int32 nIndex = 0;
            do
            {
                aAvailable.emplace_back(sAvailableLocales.getToken(0, ' ', nIndex));
            }
            while (nIndex >= 0);
            // See which one matches the desired ui locale
            OUString install = getInstalledLocaleForLanguage(comphelper::containerToSequence(aAvailable), wantedLocale);
            if (!install.isEmpty() && install != "en-US")
            {
                o3tl::string_view sVendor(OOO_VENDOR);
                if (sVendor == "Red Hat, Inc." || sVendor == "The Fedora Project")
                {
                    // langpack is the typical Fedora/RHEL naming convention
                    LanguageType eType = aWantedTag.getLanguageType();
                    if (MsLangId::isSimplifiedChinese(eType))
                        aPackages.emplace_back("libreoffice-langpack-zh-Hans");
                    else if (MsLangId::isTraditionalChinese(eType))
                        aPackages.emplace_back("libreoffice-langpack-zh-Hant");
                    else
                        aPackages.emplace_back("libreoffice-langpack-" + install);
                }
                else if (sVendor == "The Document Foundation/Debian" || sVendor == "The Document Foundation, Debian and Ubuntu")
                {
                    // l10n is the typical Debian/Ubuntu naming convention
                    aPackages.emplace_back("libreoffice-l10n-" + install);
                }
            }
            if (!aPackages.empty())
            {
                xLangpackInstaller.reset(new InstallLangpack(aPackages));
                xLangpackInstaller->Start();
            }
        }
    }
    if (locale.isEmpty())
        locale = getInstalledLocaleForLanguage(rLocaleElementNames, "en-US");
    if (locale.isEmpty() && rLocaleElementNames.hasElements())
        locale = rLocaleElementNames[0];
    return locale;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */