1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lingutil.cxx,v $
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"
35 #include <tools/prewin.h>
43 #include <tools/postwin.h>
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>
61 #include <lingutil.hxx>
62 #include <dictmgr.hxx>
65 using ::com::sun::star::lang::Locale
;
66 using namespace ::com::sun::star
;
68 //////////////////////////////////////////////////////////////////////
70 String
GetDirectoryPathFromFileURL( const String
&rFileURL
)
73 INetURLObject aURLObj
;
74 aURLObj
.SetSmartProtocol( INET_PROT_FILE
);
75 aURLObj
.SetSmartURL( rFileURL
);
76 aURLObj
.removeSegment();
77 DBG_ASSERT( !aURLObj
.HasError(), "invalid URL" );
78 String aRes
= aURLObj
.GetMainURL( INetURLObject::DECODE_TO_IURI
);
83 rtl::OString
Win_GetShortPathName( const rtl::OUString
&rLongPathName
)
90 sal_Unicode aShortBuffer
[1024] = {0};
91 sal_Int32 nShortBufSize
= sizeof( aShortBuffer
) / sizeof( aShortBuffer
[0] );
93 // use the version of 'GetShortPathName' that can deal with Unicode...
94 sal_Int32 nShortLen
= GetShortPathNameW(
95 reinterpret_cast<LPCWSTR
>( rLongPathName
.getStr() ),
96 reinterpret_cast<LPWSTR
>( aShortBuffer
),
99 if (nShortLen
< nShortBufSize
) // conversion successful?
100 aRes
= rtl::OString( OU2ENC( rtl::OUString( aShortBuffer
, nShortLen
), osl_getThreadTextEncoding()) );
102 DBG_ERROR( "Win_GetShortPathName: buffer to short" );
104 DBG_ERROR( "Win_GetShortPathName: functions should nor be called in non-Windows builds" );
105 #endif //defined(WNT)
110 //////////////////////////////////////////////////////////////////////
112 // build list of old style diuctionaries (not as extensions) to use.
113 // User installed dictionaries (the ones residing in the user paths)
114 // will get precedence over system installed ones for the same language.
115 std::vector
< SvtLinguConfigDictionaryEntry
> GetOldStyleDics( const char *pDicType
)
117 std::vector
< SvtLinguConfigDictionaryEntry
> aRes
;
122 rtl::OUString aFormatName
;
123 String aDicExtension
;
125 rtl::OUString aSystemDir
;
126 rtl::OUString aSystemPrefix
;
127 rtl::OUString aSystemSuffix
;
132 if (strcmp( pDicType
, "DICT" ) == 0)
134 aFormatName
= A2OU("DICT_SPELL");
135 aDicExtension
= String::CreateFromAscii( ".dic" );
137 aSystemDir
= A2OU( DICT_SYSTEM_DIR
);
138 aSystemSuffix
= aDicExtension
;
142 else if (strcmp( pDicType
, "HYPH" ) == 0)
144 aFormatName
= A2OU("DICT_HYPH");
145 aDicExtension
= String::CreateFromAscii( ".dic" );
147 aSystemDir
= A2OU( HYPH_SYSTEM_DIR
);
148 aSystemPrefix
= A2OU( "hyph_" );
149 aSystemSuffix
= aDicExtension
;
153 else if (strcmp( pDicType
, "THES" ) == 0)
155 aFormatName
= A2OU("DICT_THES");
156 aDicExtension
= String::CreateFromAscii( ".dat" );
158 aSystemDir
= A2OU( THES_SYSTEM_DIR
);
159 aSystemPrefix
= A2OU( "th_" );
160 aSystemSuffix
= A2OU( "_v2.dat" );
166 if (aFormatName
.getLength() == 0 || aDicExtension
.Len() == 0)
169 // set of languages to remember the language where it is already
170 // decided to make use of the dictionary.
171 std::set
< LanguageType
> aDicLangInUse
;
174 osl::Directory
aSystemDicts(aSystemDir
);
175 if (aSystemDicts
.open() == osl::FileBase::E_None
)
177 osl::DirectoryItem aItem
;
178 osl::FileStatus
aFileStatus(FileStatusMask_FileURL
);
179 while (aSystemDicts
.getNextItem(aItem
) == osl::FileBase::E_None
)
181 aItem
.getFileStatus(aFileStatus
);
182 rtl::OUString sPath
= aFileStatus
.getFileURL();
183 if (sPath
.lastIndexOf(aSystemSuffix
) == sPath
.getLength()-aSystemSuffix
.getLength())
185 sal_Int32 nStartIndex
= sPath
.lastIndexOf(sal_Unicode('/')) + 1;
186 if (!sPath
.match(aSystemPrefix
, nStartIndex
))
188 rtl::OUString sChunk
= sPath
.copy(0, sPath
.getLength() - aSystemSuffix
.getLength());
189 sal_Int32 nIndex
= nStartIndex
+ aSystemPrefix
.getLength();
190 rtl::OUString sLang
= sChunk
.getToken( 0, '_', nIndex
);
191 if (!sLang
.getLength())
193 rtl::OUString sRegion
;
195 sRegion
= sChunk
.copy( nIndex
, sChunk
.getLength() - nIndex
);
197 // Thus we first get the language of the dictionary
198 LanguageType nLang
= MsLangId::convertIsoNamesToLanguage(
201 if (aDicLangInUse
.count( nLang
) == 0)
203 // remember the new language in use
204 aDicLangInUse
.insert( nLang
);
206 // add the dictionary to the resulting vector
207 SvtLinguConfigDictionaryEntry aDicEntry
;
208 aDicEntry
.aLocations
.realloc(1);
209 aDicEntry
.aLocaleNames
.realloc(1);
210 rtl::OUString
aLocaleName( MsLangId::convertLanguageToIsoString( nLang
) );
211 aDicEntry
.aLocations
[0] = sPath
;
212 aDicEntry
.aFormatName
= aFormatName
;
213 aDicEntry
.aLocaleNames
[0] = aLocaleName
;
214 aRes
.push_back( aDicEntry
);
226 void MergeNewStyleDicsAndOldStyleDics(
227 std::list
< SvtLinguConfigDictionaryEntry
> &rNewStyleDics
,
228 const std::vector
< SvtLinguConfigDictionaryEntry
> &rOldStyleDics
)
230 // get list of languages supported by new style dictionaries
231 std::set
< LanguageType
> aNewStyleLanguages
;
232 std::list
< SvtLinguConfigDictionaryEntry
>::const_iterator aIt
;
233 for (aIt
= rNewStyleDics
.begin() ; aIt
!= rNewStyleDics
.end(); ++aIt
)
235 const uno::Sequence
< rtl::OUString
> aLocaleNames( aIt
->aLocaleNames
);
236 sal_Int32 nLocaleNames
= aLocaleNames
.getLength();
237 for (sal_Int32 k
= 0; k
< nLocaleNames
; ++k
)
239 LanguageType nLang
= MsLangId::convertIsoStringToLanguage( aLocaleNames
[k
] );
240 aNewStyleLanguages
.insert( nLang
);
244 // now check all old style dictionaries if they will add a not yet
245 // added language. If so add them to the resulting vector
246 std::vector
< SvtLinguConfigDictionaryEntry
>::const_iterator aIt2
;
247 for (aIt2
= rOldStyleDics
.begin(); aIt2
!= rOldStyleDics
.end(); ++aIt2
)
249 sal_Int32 nOldStyleDics
= aIt2
->aLocaleNames
.getLength();
251 // old style dics should only have one language listed...
252 DBG_ASSERT( nOldStyleDics
, "old style dictionary with more then one language found!");
253 if (nOldStyleDics
> 0)
255 LanguageType nLang
= MsLangId::convertIsoStringToLanguage( aIt2
->aLocaleNames
[0] );
257 if (nLang
== LANGUAGE_DONTKNOW
|| nLang
== LANGUAGE_NONE
)
259 DBG_ERROR( "old style dictionary with invalid language found!" );
263 // language not yet added?
264 if (aNewStyleLanguages
.count( nLang
) == 0)
265 rNewStyleDics
.push_back( *aIt2
);
269 DBG_ERROR( "old style dictionary with no language found!" );
274 //////////////////////////////////////////////////////////////////////