Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #if defined(WNT)
21 : #include <windows.h>
22 : #endif
23 :
24 : #include <osl/thread.h>
25 : #include <osl/file.hxx>
26 : #include <tools/debug.hxx>
27 : #include <tools/urlobj.hxx>
28 : #include <i18nlangtag/languagetag.hxx>
29 : #include <i18nlangtag/mslangid.hxx>
30 : #include <unotools/lingucfg.hxx>
31 : #include <unotools/pathoptions.hxx>
32 : #include <rtl/ustring.hxx>
33 : #include <rtl/string.hxx>
34 : #include <rtl/tencinfo.h>
35 : #include <linguistic/misc.hxx>
36 :
37 : #include <set>
38 : #include <vector>
39 : #include <string.h>
40 :
41 : #include <lingutil.hxx>
42 :
43 : #include <sal/macros.h>
44 :
45 : using ::com::sun::star::lang::Locale;
46 : using namespace ::com::sun::star;
47 :
48 : #if defined(WNT)
49 : OString Win_AddLongPathPrefix( const OString &rPathName )
50 : {
51 : #define WIN32_LONG_PATH_PREFIX "\\\\?\\"
52 : if (!rPathName.match(WIN32_LONG_PATH_PREFIX)) return WIN32_LONG_PATH_PREFIX + rPathName;
53 : return rPathName;
54 : }
55 : #endif //defined(WNT)
56 :
57 : // build list of old style diuctionaries (not as extensions) to use.
58 : // User installed dictionaries (the ones residing in the user paths)
59 : // will get precedence over system installed ones for the same language.
60 132 : std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType )
61 : {
62 132 : std::vector< SvtLinguConfigDictionaryEntry > aRes;
63 :
64 132 : if (!pDicType)
65 0 : return aRes;
66 :
67 264 : OUString aFormatName;
68 264 : OUString aDicExtension;
69 : #ifdef SYSTEM_DICTS
70 264 : OUString aSystemDir;
71 264 : OUString aSystemPrefix;
72 264 : OUString aSystemSuffix;
73 : #endif
74 132 : if (strcmp( pDicType, "DICT" ) == 0)
75 : {
76 44 : aFormatName = "DICT_SPELL";
77 44 : aDicExtension = ".dic";
78 : #ifdef SYSTEM_DICTS
79 44 : aSystemDir = DICT_SYSTEM_DIR;
80 44 : aSystemSuffix = aDicExtension;
81 : #endif
82 : }
83 88 : else if (strcmp( pDicType, "HYPH" ) == 0)
84 : {
85 44 : aFormatName = "DICT_HYPH";
86 44 : aDicExtension = ".dic";
87 : #ifdef SYSTEM_DICTS
88 44 : aSystemDir = HYPH_SYSTEM_DIR;
89 44 : aSystemPrefix = "hyph_";
90 44 : aSystemSuffix = aDicExtension;
91 : #endif
92 : }
93 44 : else if (strcmp( pDicType, "THES" ) == 0)
94 : {
95 44 : aFormatName = "DICT_THES";
96 44 : aDicExtension = ".dat";
97 : #ifdef SYSTEM_DICTS
98 44 : aSystemDir = THES_SYSTEM_DIR;
99 44 : aSystemPrefix = "th_";
100 44 : aSystemSuffix = "_v2.dat";
101 : #endif
102 : }
103 :
104 132 : if (aFormatName.isEmpty() || aDicExtension.isEmpty())
105 0 : return aRes;
106 :
107 : #ifdef SYSTEM_DICTS
108 264 : osl::Directory aSystemDicts(aSystemDir);
109 132 : if (aSystemDicts.open() == osl::FileBase::E_None)
110 : {
111 : // set of languages to remember the language where it is already
112 : // decided to make use of the dictionary.
113 132 : std::set< OUString > aDicLangInUse;
114 :
115 264 : osl::DirectoryItem aItem;
116 264 : osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL);
117 5412 : while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None)
118 : {
119 5148 : aItem.getFileStatus(aFileStatus);
120 5148 : OUString sPath = aFileStatus.getFileURL();
121 5148 : if (sPath.endsWith(aSystemSuffix))
122 : {
123 3080 : sal_Int32 nStartIndex = sPath.lastIndexOf('/') + 1;
124 3080 : if (!sPath.match(aSystemPrefix, nStartIndex))
125 0 : continue;
126 3080 : OUString sChunk = sPath.copy(nStartIndex + aSystemPrefix.getLength(),
127 3080 : sPath.getLength() - aSystemSuffix.getLength() -
128 6160 : nStartIndex - aSystemPrefix.getLength());
129 3080 : if (sChunk.isEmpty())
130 0 : continue;
131 :
132 : // We prefer (now) to use language tags.
133 : // Avoid feeding in the older LANG_REGION scheme to the BCP47
134 : // ctor as that triggers use of liblangtag and initializes its
135 : // database which we do not want during startup. Convert
136 : // instead.
137 3080 : sChunk = sChunk.replace( '_', '-');
138 :
139 : // There's a known exception to the rule, the dreaded
140 : // hu_HU_u8.dic of the myspell-hu package, see
141 : // http://packages.debian.org/search?arch=any&searchon=contents&keywords=hu_HU_u8.dic
142 : // This was ignored because unknown in the old implementation,
143 : // truncate to the known locale and either insert because hu_HU
144 : // wasn't encountered yet, or skip because it was. It doesn't
145 : // really matter because the proper new-style hu_HU dictionary
146 : // will take precedence anyway if installed with a Hungarian
147 : // languagepack. Again, this is only to not pull in all
148 : // liblangtag and stuff during startup, the result would be
149 : // !isValidBcp47() and the dictionary ignored.
150 3080 : if (sChunk == "hu-HU-u8")
151 0 : sChunk = "hu-HU";
152 :
153 6160 : LanguageTag aLangTag(sChunk, true);
154 3080 : if (!aLangTag.isValidBcp47())
155 0 : continue;
156 :
157 : // Thus we first get the language of the dictionary
158 6160 : OUString aLocaleName(aLangTag.getBcp47());
159 :
160 3080 : if (aDicLangInUse.insert(aLocaleName).second)
161 : {
162 : // add the dictionary to the resulting vector
163 3080 : SvtLinguConfigDictionaryEntry aDicEntry;
164 3080 : aDicEntry.aLocations.realloc(1);
165 3080 : aDicEntry.aLocaleNames.realloc(1);
166 3080 : aDicEntry.aLocations[0] = sPath;
167 3080 : aDicEntry.aFormatName = aFormatName;
168 3080 : aDicEntry.aLocaleNames[0] = aLocaleName;
169 3080 : aRes.push_back( aDicEntry );
170 3080 : }
171 : }
172 5280 : }
173 : }
174 : #endif
175 :
176 132 : return aRes;
177 : }
178 :
179 132 : void MergeNewStyleDicsAndOldStyleDics(
180 : std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics,
181 : const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics )
182 : {
183 : // get list of languages supported by new style dictionaries
184 132 : std::set< OUString > aNewStyleLanguages;
185 132 : std::list< SvtLinguConfigDictionaryEntry >::const_iterator aIt;
186 132 : for (aIt = rNewStyleDics.begin() ; aIt != rNewStyleDics.end(); ++aIt)
187 : {
188 0 : const uno::Sequence< OUString > aLocaleNames( aIt->aLocaleNames );
189 0 : sal_Int32 nLocaleNames = aLocaleNames.getLength();
190 0 : for (sal_Int32 k = 0; k < nLocaleNames; ++k)
191 : {
192 0 : aNewStyleLanguages.insert( aLocaleNames[k] );
193 : }
194 0 : }
195 :
196 : // now check all old style dictionaries if they will add a not yet
197 : // added language. If so add them to the resulting vector
198 132 : std::vector< SvtLinguConfigDictionaryEntry >::const_iterator aIt2;
199 3212 : for (aIt2 = rOldStyleDics.begin(); aIt2 != rOldStyleDics.end(); ++aIt2)
200 : {
201 3080 : sal_Int32 nOldStyleDics = aIt2->aLocaleNames.getLength();
202 :
203 : // old style dics should only have one language listed...
204 : DBG_ASSERT( nOldStyleDics, "old style dictionary with more than one language found!");
205 3080 : if (nOldStyleDics > 0)
206 : {
207 3080 : if (linguistic::LinguIsUnspecified( aIt2->aLocaleNames[0]))
208 : {
209 : OSL_FAIL( "old style dictionary with invalid language found!" );
210 0 : continue;
211 : }
212 :
213 : // language not yet added?
214 3080 : if (aNewStyleLanguages.find( aIt2->aLocaleNames[0] ) == aNewStyleLanguages.end())
215 3080 : rNewStyleDics.push_back( *aIt2 );
216 : }
217 : else
218 : {
219 : OSL_FAIL( "old style dictionary with no language found!" );
220 : }
221 132 : }
222 132 : }
223 :
224 42 : rtl_TextEncoding getTextEncodingFromCharset(const sal_Char* pCharset)
225 : {
226 : // default result: used to indicate that we failed to get the proper encoding
227 42 : rtl_TextEncoding eRet = RTL_TEXTENCODING_DONTKNOW;
228 :
229 42 : if (pCharset)
230 : {
231 42 : eRet = rtl_getTextEncodingFromMimeCharset(pCharset);
232 42 : if (eRet == RTL_TEXTENCODING_DONTKNOW)
233 0 : eRet = rtl_getTextEncodingFromUnixCharset(pCharset);
234 42 : if (eRet == RTL_TEXTENCODING_DONTKNOW)
235 : {
236 0 : if (strcmp("ISCII-DEVANAGARI", pCharset) == 0)
237 0 : eRet = RTL_TEXTENCODING_ISCII_DEVANAGARI;
238 : }
239 : }
240 42 : return eRet;
241 : }
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|