Update ooo320-m1
[ooovba.git] / lingucomponent / source / lingutil / lingutil.cxx
blob26a495b62436cd9978ea274a4386597c02cdaf49
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lingutil.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_lingucomponent.hxx"
34 #if defined(WNT)
35 #include <tools/prewin.h>
36 #endif
38 #if defined(WNT)
39 #include <Windows.h>
40 #endif
42 #if defined(WNT)
43 #include <tools/postwin.h>
44 #endif
47 #include <osl/thread.h>
48 #include <osl/file.hxx>
49 #include <tools/debug.hxx>
50 #include <tools/urlobj.hxx>
51 #include <i18npool/mslangid.hxx>
52 #include <svtools/lingucfg.hxx>
53 #include <svtools/pathoptions.hxx>
54 #include <rtl/ustring.hxx>
55 #include <rtl/string.hxx>
56 #include <linguistic/misc.hxx>
58 #include <set>
59 #include <vector>
61 #include <lingutil.hxx>
62 #include <dictmgr.hxx>
65 using ::com::sun::star::lang::Locale;
66 using namespace ::com::sun::star;
68 #if 0
69 //////////////////////////////////////////////////////////////////////
71 String GetDirectoryPathFromFileURL( const String &rFileURL )
73 // get file URL
74 INetURLObject aURLObj;
75 aURLObj.SetSmartProtocol( INET_PROT_FILE );
76 aURLObj.SetSmartURL( rFileURL );
77 aURLObj.removeSegment();
78 DBG_ASSERT( !aURLObj.HasError(), "invalid URL" );
79 String aRes = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
80 return aRes;
82 #endif
84 #if defined(WNT)
85 rtl::OString Win_GetShortPathName( const rtl::OUString &rLongPathName )
87 rtl::OString aRes;
89 sal_Unicode aShortBuffer[1024] = {0};
90 sal_Int32 nShortBufSize = sizeof( aShortBuffer ) / sizeof( aShortBuffer[0] );
92 // use the version of 'GetShortPathName' that can deal with Unicode...
93 sal_Int32 nShortLen = GetShortPathNameW(
94 reinterpret_cast<LPCWSTR>( rLongPathName.getStr() ),
95 reinterpret_cast<LPWSTR>( aShortBuffer ),
96 nShortBufSize );
98 if (nShortLen < nShortBufSize) // conversion successful?
99 aRes = rtl::OString( OU2ENC( rtl::OUString( aShortBuffer, nShortLen ), osl_getThreadTextEncoding()) );
100 else
101 DBG_ERROR( "Win_GetShortPathName: buffer to short" );
103 return aRes;
105 #endif //defined(WNT)
107 //////////////////////////////////////////////////////////////////////
109 // build list of old style diuctionaries (not as extensions) to use.
110 // User installed dictionaries (the ones residing in the user paths)
111 // will get precedence over system installed ones for the same language.
112 std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType )
114 std::vector< SvtLinguConfigDictionaryEntry > aRes;
116 if (!pDicType)
117 return aRes;
119 rtl::OUString aFormatName;
120 String aDicExtension;
121 #ifdef SYSTEM_DICTS
122 rtl::OUString aSystemDir;
123 rtl::OUString aSystemPrefix;
124 rtl::OUString aSystemSuffix;
125 #endif
126 bool bSpell = false;
127 bool bHyph = false;
128 bool bThes = false;
129 if (strcmp( pDicType, "DICT" ) == 0)
131 aFormatName = A2OU("DICT_SPELL");
132 aDicExtension = String::CreateFromAscii( ".dic" );
133 #ifdef SYSTEM_DICTS
134 aSystemDir = A2OU( DICT_SYSTEM_DIR );
135 aSystemSuffix = aDicExtension;
136 #endif
137 bSpell = true;
139 else if (strcmp( pDicType, "HYPH" ) == 0)
141 aFormatName = A2OU("DICT_HYPH");
142 aDicExtension = String::CreateFromAscii( ".dic" );
143 #ifdef SYSTEM_DICTS
144 aSystemDir = A2OU( HYPH_SYSTEM_DIR );
145 aSystemPrefix = A2OU( "hyph_" );
146 aSystemSuffix = aDicExtension;
147 #endif
148 bHyph = true;
150 else if (strcmp( pDicType, "THES" ) == 0)
152 aFormatName = A2OU("DICT_THES");
153 aDicExtension = String::CreateFromAscii( ".dat" );
154 #ifdef SYSTEM_DICTS
155 aSystemDir = A2OU( THES_SYSTEM_DIR );
156 aSystemPrefix = A2OU( "th_" );
157 aSystemSuffix = A2OU( "_v2.dat" );
158 #endif
159 bThes = true;
163 if (aFormatName.getLength() == 0 || aDicExtension.Len() == 0)
164 return aRes;
166 // set of languages to remember the language where it is already
167 // decided to make use of the dictionary.
168 std::set< LanguageType > aDicLangInUse;
170 #ifdef SYSTEM_DICTS
171 osl::Directory aSystemDicts(aSystemDir);
172 if (aSystemDicts.open() == osl::FileBase::E_None)
174 osl::DirectoryItem aItem;
175 osl::FileStatus aFileStatus(FileStatusMask_FileURL);
176 while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None)
178 aItem.getFileStatus(aFileStatus);
179 rtl::OUString sPath = aFileStatus.getFileURL();
180 if (sPath.lastIndexOf(aSystemSuffix) == sPath.getLength()-aSystemSuffix.getLength())
182 sal_Int32 nStartIndex = sPath.lastIndexOf(sal_Unicode('/')) + 1;
183 if (!sPath.match(aSystemPrefix, nStartIndex))
184 continue;
185 rtl::OUString sChunk = sPath.copy(0, sPath.getLength() - aSystemSuffix.getLength());
186 sal_Int32 nIndex = nStartIndex + aSystemPrefix.getLength();
187 rtl::OUString sLang = sChunk.getToken( 0, '_', nIndex );
188 if (!sLang.getLength())
189 continue;
190 rtl::OUString sRegion;
191 if (nIndex != -1)
192 sRegion = sChunk.copy( nIndex, sChunk.getLength() - nIndex );
194 // Thus we first get the language of the dictionary
195 LanguageType nLang = MsLangId::convertIsoNamesToLanguage(
196 sLang, sRegion );
198 if (aDicLangInUse.count( nLang ) == 0)
200 // remember the new language in use
201 aDicLangInUse.insert( nLang );
203 // add the dictionary to the resulting vector
204 SvtLinguConfigDictionaryEntry aDicEntry;
205 aDicEntry.aLocations.realloc(1);
206 aDicEntry.aLocaleNames.realloc(1);
207 rtl::OUString aLocaleName( MsLangId::convertLanguageToIsoString( nLang ) );
208 aDicEntry.aLocations[0] = sPath;
209 aDicEntry.aFormatName = aFormatName;
210 aDicEntry.aLocaleNames[0] = aLocaleName;
211 aRes.push_back( aDicEntry );
217 #endif
219 return aRes;
223 void MergeNewStyleDicsAndOldStyleDics(
224 std::list< SvtLinguConfigDictionaryEntry > &rNewStyleDics,
225 const std::vector< SvtLinguConfigDictionaryEntry > &rOldStyleDics )
227 // get list of languages supported by new style dictionaries
228 std::set< LanguageType > aNewStyleLanguages;
229 std::list< SvtLinguConfigDictionaryEntry >::const_iterator aIt;
230 for (aIt = rNewStyleDics.begin() ; aIt != rNewStyleDics.end(); ++aIt)
232 const uno::Sequence< rtl::OUString > aLocaleNames( aIt->aLocaleNames );
233 sal_Int32 nLocaleNames = aLocaleNames.getLength();
234 for (sal_Int32 k = 0; k < nLocaleNames; ++k)
236 LanguageType nLang = MsLangId::convertIsoStringToLanguage( aLocaleNames[k] );
237 aNewStyleLanguages.insert( nLang );
241 // now check all old style dictionaries if they will add a not yet
242 // added language. If so add them to the resulting vector
243 std::vector< SvtLinguConfigDictionaryEntry >::const_iterator aIt2;
244 for (aIt2 = rOldStyleDics.begin(); aIt2 != rOldStyleDics.end(); ++aIt2)
246 sal_Int32 nOldStyleDics = aIt2->aLocaleNames.getLength();
248 // old style dics should only have one language listed...
249 DBG_ASSERT( nOldStyleDics, "old style dictionary with more then one language found!");
250 if (nOldStyleDics > 0)
252 LanguageType nLang = MsLangId::convertIsoStringToLanguage( aIt2->aLocaleNames[0] );
254 if (nLang == LANGUAGE_DONTKNOW || nLang == LANGUAGE_NONE)
256 DBG_ERROR( "old style dictionary with invalid language found!" );
257 continue;
260 // language not yet added?
261 if (aNewStyleLanguages.count( nLang ) == 0)
262 rNewStyleDics.push_back( *aIt2 );
264 else
266 DBG_ERROR( "old style dictionary with no language found!" );
271 //////////////////////////////////////////////////////////////////////