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
;
69 //////////////////////////////////////////////////////////////////////
71 String
GetDirectoryPathFromFileURL( const String
&rFileURL
)
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
);
85 rtl::OString
Win_GetShortPathName( const rtl::OUString
&rLongPathName
)
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
),
98 if (nShortLen
< nShortBufSize
) // conversion successful?
99 aRes
= rtl::OString( OU2ENC( rtl::OUString( aShortBuffer
, nShortLen
), osl_getThreadTextEncoding()) );
101 DBG_ERROR( "Win_GetShortPathName: buffer to short" );
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
;
119 rtl::OUString aFormatName
;
120 String aDicExtension
;
122 rtl::OUString aSystemDir
;
123 rtl::OUString aSystemPrefix
;
124 rtl::OUString aSystemSuffix
;
129 if (strcmp( pDicType
, "DICT" ) == 0)
131 aFormatName
= A2OU("DICT_SPELL");
132 aDicExtension
= String::CreateFromAscii( ".dic" );
134 aSystemDir
= A2OU( DICT_SYSTEM_DIR
);
135 aSystemSuffix
= aDicExtension
;
139 else if (strcmp( pDicType
, "HYPH" ) == 0)
141 aFormatName
= A2OU("DICT_HYPH");
142 aDicExtension
= String::CreateFromAscii( ".dic" );
144 aSystemDir
= A2OU( HYPH_SYSTEM_DIR
);
145 aSystemPrefix
= A2OU( "hyph_" );
146 aSystemSuffix
= aDicExtension
;
150 else if (strcmp( pDicType
, "THES" ) == 0)
152 aFormatName
= A2OU("DICT_THES");
153 aDicExtension
= String::CreateFromAscii( ".dat" );
155 aSystemDir
= A2OU( THES_SYSTEM_DIR
);
156 aSystemPrefix
= A2OU( "th_" );
157 aSystemSuffix
= A2OU( "_v2.dat" );
163 if (aFormatName
.getLength() == 0 || aDicExtension
.Len() == 0)
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
;
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
))
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())
190 rtl::OUString sRegion
;
192 sRegion
= sChunk
.copy( nIndex
, sChunk
.getLength() - nIndex
);
194 // Thus we first get the language of the dictionary
195 LanguageType nLang
= MsLangId::convertIsoNamesToLanguage(
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
);
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!" );
260 // language not yet added?
261 if (aNewStyleLanguages
.count( nLang
) == 0)
262 rNewStyleDics
.push_back( *aIt2
);
266 DBG_ERROR( "old style dictionary with no language found!" );
271 //////////////////////////////////////////////////////////////////////