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 :
21 : #if defined(WNT)
22 : #include <windows.h>
23 : #endif
24 :
25 : #include <osl/thread.h>
26 : #include <osl/file.hxx>
27 : #include <tools/debug.hxx>
28 : #include <tools/urlobj.hxx>
29 : #include <i18npool/languagetag.hxx>
30 : #include <i18npool/mslangid.hxx>
31 : #include <unotools/lingucfg.hxx>
32 : #include <unotools/pathoptions.hxx>
33 : #include <rtl/ustring.hxx>
34 : #include <rtl/string.hxx>
35 : #include <rtl/tencinfo.h>
36 : #include <linguistic/misc.hxx>
37 :
38 : #include <set>
39 : #include <vector>
40 : #include <string.h>
41 :
42 : #include <lingutil.hxx>
43 :
44 : #include <sal/macros.h>
45 :
46 :
47 : using ::com::sun::star::lang::Locale;
48 : using namespace ::com::sun::star;
49 :
50 : #if defined(WNT)
51 : rtl::OString Win_GetShortPathName( const rtl::OUString &rLongPathName )
52 : {
53 : rtl::OString aRes;
54 :
55 : sal_Unicode aShortBuffer[1024] = {0};
56 : sal_Int32 nShortBufSize = SAL_N_ELEMENTS( aShortBuffer );
57 :
58 : // use the version of 'GetShortPathName' that can deal with Unicode...
59 : sal_Int32 nShortLen = GetShortPathNameW(
60 : reinterpret_cast<LPCWSTR>( rLongPathName.getStr() ),
61 : reinterpret_cast<LPWSTR>( aShortBuffer ),
62 : nShortBufSize );
63 :
64 : if (nShortLen < nShortBufSize) // conversion successful?
65 : aRes = rtl::OString( OU2ENC( rtl::OUString( aShortBuffer, nShortLen ), osl_getThreadTextEncoding()) );
66 : else
67 : OSL_FAIL( "Win_GetShortPathName: buffer to short" );
68 :
69 : return aRes;
70 : }
71 : #endif //defined(WNT)
72 :
73 : //////////////////////////////////////////////////////////////////////
74 :
75 : // build list of old style diuctionaries (not as extensions) to use.
76 : // User installed dictionaries (the ones residing in the user paths)
77 : // will get precedence over system installed ones for the same language.
78 0 : std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType )
79 : {
80 0 : std::vector< SvtLinguConfigDictionaryEntry > aRes;
81 :
82 0 : if (!pDicType)
83 : return aRes;
84 :
85 0 : rtl::OUString aFormatName;
86 0 : String aDicExtension;
87 : #ifdef SYSTEM_DICTS
88 0 : rtl::OUString aSystemDir;
89 0 : rtl::OUString aSystemPrefix;
90 0 : rtl::OUString aSystemSuffix;
91 : #endif
92 0 : if (strcmp( pDicType, "DICT" ) == 0)
93 : {
94 0 : aFormatName = A2OU("DICT_SPELL");
95 0 : aDicExtension = rtl::OUString( ".dic" );
96 : #ifdef SYSTEM_DICTS
97 0 : aSystemDir = A2OU( DICT_SYSTEM_DIR );
98 0 : aSystemSuffix = aDicExtension;
99 : #endif
100 : }
101 0 : else if (strcmp( pDicType, "HYPH" ) == 0)
102 : {
103 0 : aFormatName = A2OU("DICT_HYPH");
104 0 : aDicExtension = rtl::OUString( ".dic" );
105 : #ifdef SYSTEM_DICTS
106 0 : aSystemDir = A2OU( HYPH_SYSTEM_DIR );
107 0 : aSystemPrefix = A2OU( "hyph_" );
108 0 : aSystemSuffix = aDicExtension;
109 : #endif
110 : }
111 0 : else if (strcmp( pDicType, "THES" ) == 0)
112 : {
113 0 : aFormatName = A2OU("DICT_THES");
114 0 : aDicExtension = rtl::OUString( ".dat" );
115 : #ifdef SYSTEM_DICTS
116 0 : aSystemDir = A2OU( THES_SYSTEM_DIR );
117 0 : aSystemPrefix = A2OU( "th_" );
118 0 : aSystemSuffix = A2OU( "_v2.dat" );
119 : #endif
120 : }
121 :
122 :
123 0 : if (aFormatName.isEmpty() || aDicExtension.Len() == 0)
124 : return aRes;
125 :
126 : #ifdef SYSTEM_DICTS
127 0 : osl::Directory aSystemDicts(aSystemDir);
128 0 : if (aSystemDicts.open() == osl::FileBase::E_None)
129 : {
130 : // set of languages to remember the language where it is already
131 : // decided to make use of the dictionary.
132 0 : std::set< OUString > aDicLangInUse;
133 :
134 0 : osl::DirectoryItem aItem;
135 0 : osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL);
136 0 : while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None)
137 : {
138 0 : aItem.getFileStatus(aFileStatus);
139 0 : OUString sPath = aFileStatus.getFileURL();
140 0 : if (sPath.lastIndexOf(aSystemSuffix) == sPath.getLength()-aSystemSuffix.getLength())
141 : {
142 0 : sal_Int32 nStartIndex = sPath.lastIndexOf(sal_Unicode('/')) + 1;
143 0 : if (!sPath.match(aSystemPrefix, nStartIndex))
144 0 : continue;
145 0 : OUString sChunk = sPath.copy(nStartIndex + aSystemPrefix.getLength(),
146 0 : sPath.getLength() - aSystemSuffix.getLength() -
147 0 : nStartIndex - aSystemPrefix.getLength());
148 0 : if (sChunk.isEmpty())
149 0 : continue;
150 : // We prefer (now) to use language tags.
151 : // Avoid feeding in the older LANG_REGION scheme to the BCP47
152 : // ctor as that triggers use of liblangtag and initializes its
153 : // database which we do not want during startup. Convert
154 : // instead.
155 : sal_Int32 nPos;
156 0 : if (sChunk.indexOf('-') < 0 && ((nPos = sChunk.indexOf('_')) > 0))
157 0 : sChunk = sChunk.replaceAt( nPos, 1, OUString('-'));
158 0 : LanguageTag aLangTag(sChunk, true);
159 0 : if (!aLangTag.isValidBcp47())
160 0 : continue;
161 :
162 : // Thus we first get the language of the dictionary
163 0 : OUString aLocaleName(aLangTag.getBcp47());
164 :
165 0 : if (aDicLangInUse.count(aLocaleName) == 0)
166 : {
167 : // remember the new language in use
168 0 : aDicLangInUse.insert(aLocaleName);
169 :
170 : // add the dictionary to the resulting vector
171 0 : SvtLinguConfigDictionaryEntry aDicEntry;
172 0 : aDicEntry.aLocations.realloc(1);
173 0 : aDicEntry.aLocaleNames.realloc(1);
174 0 : aDicEntry.aLocations[0] = sPath;
175 0 : aDicEntry.aFormatName = aFormatName;
176 0 : aDicEntry.aLocaleNames[0] = aLocaleName;
177 0 : aRes.push_back( aDicEntry );
178 0 : }
179 : }
180 0 : }
181 : }
182 : #endif
183 :
184 0 : return aRes;
185 : }
186 :
187 :
188 0 : void MergeNewStyleDicsAndOldStyleDics(
189 : std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics,
190 : const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics )
191 : {
192 : // get list of languages supported by new style dictionaries
193 0 : std::set< LanguageType > aNewStyleLanguages;
194 0 : std::list< SvtLinguConfigDictionaryEntry >::const_iterator aIt;
195 0 : for (aIt = rNewStyleDics.begin() ; aIt != rNewStyleDics.end(); ++aIt)
196 : {
197 0 : const uno::Sequence< rtl::OUString > aLocaleNames( aIt->aLocaleNames );
198 0 : sal_Int32 nLocaleNames = aLocaleNames.getLength();
199 0 : for (sal_Int32 k = 0; k < nLocaleNames; ++k)
200 : {
201 0 : LanguageType nLang = LanguageTag( aLocaleNames[k] ).getLanguageType();
202 0 : aNewStyleLanguages.insert( nLang );
203 : }
204 0 : }
205 :
206 : // now check all old style dictionaries if they will add a not yet
207 : // added language. If so add them to the resulting vector
208 0 : std::vector< SvtLinguConfigDictionaryEntry >::const_iterator aIt2;
209 0 : for (aIt2 = rOldStyleDics.begin(); aIt2 != rOldStyleDics.end(); ++aIt2)
210 : {
211 0 : sal_Int32 nOldStyleDics = aIt2->aLocaleNames.getLength();
212 :
213 : // old style dics should only have one language listed...
214 : DBG_ASSERT( nOldStyleDics, "old style dictionary with more then one language found!");
215 0 : if (nOldStyleDics > 0)
216 : {
217 0 : LanguageType nLang = LanguageTag( aIt2->aLocaleNames[0] ).getLanguageType();
218 :
219 0 : if (nLang == LANGUAGE_DONTKNOW || nLang == LANGUAGE_NONE)
220 : {
221 : OSL_FAIL( "old style dictionary with invalid language found!" );
222 0 : continue;
223 : }
224 :
225 : // language not yet added?
226 0 : if (aNewStyleLanguages.count( nLang ) == 0)
227 0 : rNewStyleDics.push_back( *aIt2 );
228 : }
229 : else
230 : {
231 : OSL_FAIL( "old style dictionary with no language found!" );
232 : }
233 0 : }
234 0 : }
235 :
236 :
237 0 : rtl_TextEncoding getTextEncodingFromCharset(const sal_Char* pCharset)
238 : {
239 : // default result: used to indicate that we failed to get the proper encoding
240 0 : rtl_TextEncoding eRet = RTL_TEXTENCODING_DONTKNOW;
241 :
242 0 : if (pCharset)
243 : {
244 0 : eRet = rtl_getTextEncodingFromMimeCharset(pCharset);
245 0 : if (eRet == RTL_TEXTENCODING_DONTKNOW)
246 0 : eRet = rtl_getTextEncodingFromUnixCharset(pCharset);
247 0 : if (eRet == RTL_TEXTENCODING_DONTKNOW)
248 : {
249 0 : if (strcmp("ISCII-DEVANAGARI", pCharset) == 0)
250 0 : eRet = RTL_TEXTENCODING_ISCII_DEVANAGARI;
251 : }
252 : }
253 0 : return eRet;
254 : }
255 :
256 : //////////////////////////////////////////////////////////////////////
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|