Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / setup_native / source / win32 / customactions / sellang / sellang.cxx
blobe82560f4239add556878a38e7e443a7a8710a058
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /* Currently the "all" installer has a bit over 100 UI languages, and
21 * I doubt it will grow a lot over that.
23 #define MAX_LANGUAGES 200
25 #define WIN32_LEAN_AND_MEAN
26 #include <windows.h>
27 #include <msiquery.h>
28 #include <malloc.h>
30 #include <cassert>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <sal/macros.h>
36 #include <systools/win32/uwinapi.h>
37 #include <algorithm>
39 #include <spellchecker_selection.hxx>
41 static BOOL GetMsiPropA( MSIHANDLE hMSI, const char* pPropName, char** ppValue )
43 DWORD sz = 0;
44 if ( MsiGetPropertyA( hMSI, pPropName, const_cast<char *>(""), &sz ) == ERROR_MORE_DATA ) {
45 sz++;
46 DWORD nbytes = sz * sizeof( char );
47 char* buff = static_cast<char*>( malloc( nbytes ) );
48 assert(buff); // Don't handle OOM conditions
49 ZeroMemory( buff, nbytes );
50 MsiGetPropertyA( hMSI, pPropName, buff, &sz );
51 *ppValue = buff;
52 return ( strlen(buff) > 0 );
54 return FALSE;
57 static const char *
58 langid_to_string( LANGID langid )
60 /* Map from LANGID to string. The languages below are now in
61 * alphabetical order of codes as in
62 * l10ntools/source/ulfconv/msi-encodinglist.txt. Only the
63 * language part is returned in the string.
65 switch (PRIMARYLANGID (langid)) {
66 case LANG_AFRIKAANS: return "af";
67 case LANG_AMHARIC: return "am";
68 case LANG_ARABIC: return "ar";
69 case LANG_ASSAMESE: return "as";
70 case LANG_BELARUSIAN: return "be";
71 case LANG_BULGARIAN: return "bg";
72 case LANG_BENGALI: return "bn";
73 case LANG_BRETON: return "br";
74 case LANG_CATALAN: return "ca";
75 case LANG_CZECH: return "cs";
76 case LANG_WELSH: return "cy";
77 case LANG_DANISH: return "da";
78 case LANG_GERMAN: return "de";
79 case LANG_GREEK: return "el";
80 case LANG_ENGLISH: return "en";
81 case LANG_SPANISH: return "es";
82 case LANG_ESTONIAN: return "et";
83 case LANG_BASQUE: return "eu";
84 case LANG_FARSI: return "fa";
85 case LANG_FINNISH: return "fi";
86 case LANG_FAEROESE: return "fo";
87 case LANG_FRENCH: return "fr";
88 case LANG_IRISH: return "ga";
89 case LANG_GALICIAN: return "gl";
90 case LANG_GUJARATI: return "gu";
91 case LANG_HEBREW: return "he";
92 case LANG_HINDI: return "hi";
93 case LANG_HUNGARIAN: return "hu";
94 case LANG_ARMENIAN: return "hy";
95 case LANG_INDONESIAN: return "id";
96 case LANG_ICELANDIC: return "is";
97 case LANG_ITALIAN: return "it";
98 case LANG_JAPANESE: return "ja";
99 case LANG_GEORGIAN: return "ka";
100 case LANG_KHMER: return "km";
101 case LANG_KANNADA: return "kn";
102 case LANG_KOREAN: return "ko";
103 case LANG_KASHMIRI: return "ks";
104 case LANG_LAO: return "lo";
105 case LANG_LITHUANIAN: return "lt";
106 case LANG_LATVIAN: return "lv";
107 case LANG_MACEDONIAN: return "mk";
108 case LANG_MALAYALAM: return "ml";
109 case LANG_MONGOLIAN: return "mn";
110 case LANG_MARATHI: return "mr";
111 case LANG_MALAY: return "ms";
112 case LANG_MALTESE: return "mt";
113 case LANG_NEPALI: return "ne";
114 case LANG_DUTCH: return "nl";
115 case LANG_SOTHO: return "ns";
116 case LANG_ORIYA: return "or";
117 case LANG_PUNJABI: return "pa";
118 case LANG_POLISH: return "pl";
119 case LANG_PORTUGUESE: return "pt";
120 case LANG_ROMANSH: return "rm";
121 case LANG_ROMANIAN: return "ro";
122 case LANG_RUSSIAN: return "ru";
123 case LANG_KINYARWANDA: return "rw";
124 case LANG_SANSKRIT: return "sa";
125 case LANG_UPPER_SORBIAN: return "sb";
126 case LANG_SINDHI: return "sd";
127 case LANG_SLOVAK: return "sk";
128 case LANG_SLOVENIAN: return "sl";
129 case LANG_ALBANIAN: return "sq";
130 case LANG_SWEDISH: return "sv";
131 case LANG_SWAHILI: return "sw";
132 case LANG_TAMIL: return "ta";
133 case LANG_TELUGU: return "te";
134 case LANG_TAJIK: return "tg";
135 case LANG_THAI: return "th";
136 case LANG_TIGRIGNA: return "ti";
137 case LANG_TSWANA: return "tn";
138 case LANG_TURKISH: return "tr";
139 case LANG_TATAR: return "tt";
140 case LANG_UKRAINIAN: return "uk";
141 case LANG_URDU: return "ur";
142 case LANG_UZBEK: return "uz";
143 case LANG_VIETNAMESE: return "vi";
144 case LANG_XHOSA: return "xh";
145 case LANG_CHINESE: return "zh";
146 case LANG_ZULU: return "zu";
147 /* Special cases */
148 default:
149 switch (langid) {
150 case MAKELANGID(LANG_SERBIAN, 0x05): return "bs";
151 case MAKELANGID(LANG_SERBIAN, SUBLANG_DEFAULT): return "hr";
152 case MAKELANGID(LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL): return "nb";
153 case MAKELANGID(LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK): return "nn";
154 case MAKELANGID(LANG_SERBIAN, SUBLANG_SERBIAN_LATIN): return "sh";
155 case MAKELANGID(LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC): return "sr";
156 default: return nullptr;
161 /* Here we collect the UI languages present on the system;
162 * MAX_LANGUAGES is certainly enough for that
164 static const char *ui_langs[MAX_LANGUAGES];
165 static int num_ui_langs = 0;
167 static void add_ui_lang(char const * lang)
169 if (lang != nullptr && num_ui_langs != SAL_N_ELEMENTS(ui_langs)) {
170 ui_langs[num_ui_langs++] = lang;
174 static BOOL CALLBACK
175 enum_ui_lang_proc (LPTSTR language, LONG_PTR /* unused_lParam */)
177 long langid = strtol(language, nullptr, 16);
178 if (langid > 0xFFFF)
179 return TRUE;
180 add_ui_lang(langid_to_string(static_cast<LANGID>(langid)));
181 if (num_ui_langs == SAL_N_ELEMENTS(ui_langs) )
182 return FALSE;
183 return TRUE;
186 static BOOL
187 present_in_ui_langs(const char *lang)
189 for (int i = 0; i < num_ui_langs; i++)
191 if (strchr (lang, '_') != nullptr)
192 if (memcmp (ui_langs[i], lang, std::min(strlen(ui_langs[i]), strlen(lang))) == 0)
193 return TRUE;
194 if (strcmp (ui_langs[i], lang) == 0)
195 return TRUE;
197 return FALSE;
200 namespace {
202 /* TODO-BCP47: unlimit this, and if possible change from '_' to '-' separator
203 * to ease things. */
204 // XXX NOTE: the sizeof needs to follow what is accepted in
205 // setup_native/source/packinfo/spellchecker_selection.pl
206 struct InstallLocalized {
207 char lang[sizeof("lll_Ssss_CC_vvvvvvvv")];
208 bool install;
211 void addMatchingDictionaries(
212 char const * lang, InstallLocalized * dicts, int ndicts)
214 for (int i = 0; i != SAL_N_ELEMENTS(setup_native::languageDictionaries);
215 ++i)
217 if (strcmp(lang, setup_native::languageDictionaries[i].language) == 0) {
218 for (char const * const * p = setup_native::languageDictionaries[i].
219 dictionaries;
220 *p != nullptr; ++p)
222 for (int j = 0; j != ndicts; ++j) {
223 if (_stricmp(*p, dicts[j].lang) == 0) {
224 dicts[j].install = true;
225 break;
229 break;
236 extern "C" __declspec(dllexport) UINT __stdcall SelectLanguage( MSIHANDLE handle )
238 char feature[100];
239 MSIHANDLE database, view, record;
240 DWORD length;
241 int nlangs = 0;
242 InstallLocalized langs[MAX_LANGUAGES];
243 int ndicts = 0;
244 InstallLocalized dicts[MAX_LANGUAGES];
246 database = MsiGetActiveDatabase(handle);
248 if (MsiDatabaseOpenViewA(database, "SELECT Feature from Feature WHERE Feature_Parent = 'gm_Langpack_Languageroot'", &view) != ERROR_SUCCESS) {
249 MsiCloseHandle(database);
250 return ERROR_SUCCESS;
253 if (MsiViewExecute(view, NULL) != ERROR_SUCCESS) {
254 MsiCloseHandle(view);
255 MsiCloseHandle(database);
256 return ERROR_SUCCESS;
259 while (nlangs < MAX_LANGUAGES &&
260 MsiViewFetch(view, &record) == ERROR_SUCCESS) {
261 length = sizeof(feature);
262 if (MsiRecordGetStringA(record, 1, feature, &length) != ERROR_SUCCESS) {
263 MsiCloseHandle(record);
264 MsiCloseHandle(view);
265 MsiCloseHandle(database);
266 return ERROR_SUCCESS;
269 /* Keep track of what langpacks are included in this installer.
271 strcpy(langs[nlangs].lang, feature + strlen("gm_Langpack_r_"));
272 langs[nlangs].install = false;
273 ++nlangs;
275 MsiCloseHandle(record);
278 MsiCloseHandle(view);
280 /* Keep track of what dictionaries are included in this installer:
282 if (MsiDatabaseOpenViewA(
283 database,
284 ("SELECT Feature from Feature WHERE"
285 " Feature_Parent = 'gm_Dictionaries'"),
286 &view)
287 == ERROR_SUCCESS)
289 if (MsiViewExecute(view, NULL) == ERROR_SUCCESS) {
290 while (ndicts < MAX_LANGUAGES &&
291 MsiViewFetch(view, &record) == ERROR_SUCCESS)
293 length = sizeof(feature);
294 if (MsiRecordGetStringA(record, 1, feature, &length)
295 == ERROR_SUCCESS)
297 if (strncmp(
298 feature, "gm_r_ex_Dictionary_",
299 strlen("gm_r_ex_Dictionary_"))
300 == 0)
302 strcpy(
303 dicts[ndicts].lang,
304 feature + strlen("gm_r_ex_Dictionary_"));
305 dicts[ndicts].install = false;
306 ++ndicts;
309 MsiCloseHandle(record);
312 MsiCloseHandle(view);
315 /* Keep track of what UI languages are relevant, either the ones explicitly
316 * requested with the UI_LANGS property, or all available on the system:
318 char* pVal = nullptr;
319 if ( (GetMsiPropA( handle, "UI_LANGS", &pVal )) && pVal ) {
320 char *str_ptr;
321 str_ptr = strtok(pVal, ",");
322 for(; str_ptr != nullptr ;) {
323 add_ui_lang(str_ptr);
324 str_ptr = strtok(nullptr, ",");
326 } else {
327 add_ui_lang(langid_to_string(GetSystemDefaultUILanguage()));
328 add_ui_lang(langid_to_string(LANGIDFROMLCID(GetThreadLocale())));
329 //TODO: are the above two explicit additions necessary, or will
330 // those values always be included in the below EnumUILanguages
331 // anyway?
332 EnumUILanguagesA(enum_ui_lang_proc, 0, 0);
335 // If the set of langpacks that match any of the relevant UI languages is
336 // non-empty, select just those matching langpacks; otherwise, if an en_US
337 // langpack is included, select just that langpack (this happens if, e.g.,
338 // a multi-language en-US,de,es,fr,it,pt-BR installation set is installed on
339 // a Finnish Windows); otherwise, select all langpacks (this happens if,
340 // e.g., a single-language de installation set is installed on a Finnish
341 // Windows):
342 bool matches = false;
343 for (int i = 0; i < nlangs; i++) {
344 if (present_in_ui_langs(langs[i].lang)) {
345 langs[i].install = true;
346 matches = true;
349 if (!matches) {
350 for (int i = 0; i < nlangs; i++) {
351 if (strcmp(langs[i].lang, "en_US") == 0) {
352 langs[i].install = true;
353 matches = true;
354 break;
357 if (!matches) {
358 for (int i = 0; i < nlangs; i++) {
359 langs[i].install = true;
364 for (int i = 0; i < nlangs; i++) {
365 if (langs[i].install) {
366 addMatchingDictionaries(langs[i].lang, dicts, ndicts);
367 } else {
368 sprintf(feature, "gm_Langpack_r_%s", langs[i].lang);
369 MsiSetFeatureStateA(handle, feature, INSTALLSTATE_ABSENT);
373 // Select just those dictionaries that match any of the selected langpacks:
374 for (int i = 0; i != ndicts; ++i) {
375 if (!dicts[i].install) {
376 sprintf(feature, "gm_r_ex_Dictionary_%s", dicts[i].lang);
377 MsiSetFeatureStateA(handle, feature, INSTALLSTATE_ABSENT);
381 MsiCloseHandle(database);
383 return ERROR_SUCCESS;
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */