diff options
author | Jookia <166291@gmail.com> | 2016-10-08 19:04:38 +1100 |
---|---|---|
committer | jan iversen <jani@documentfoundation.org> | 2016-10-10 20:09:41 +0000 |
commit | 8e8afc358b7537d493b478b429e1711c6ab46bdc (patch) | |
tree | 5ebd4c7addd2e24b0e1c0ed32a577ab0daef6664 /lingucomponent | |
parent | e0d442b1995082fd28614d51b85cb4248ba8189d (diff) |
Search for old style dictionaries in DICPATH
When searching in system directories for old style dictionaries, also
look in the DICPATH environment variable much like the Hunspell
application does. This gives a lot more flexibility for users and
packagers in finding dictionaries at runtime.
The patch is simple, it just moves a block of code from
GetOldStyleDics that handles searching a directory to a new function,
GetOldStyleDicsInDir. Then if DICPATH is set, its directories are
passed to the new function. Original system directories are also
passed so dictionaries in system-wide directories are found.
Change-Id: I56ac66539495f03f41376b533ca19c6c8d615ec3
Reviewed-on: https://gerrit.libreoffice.org/29543
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: jan iversen <jani@documentfoundation.org>
Diffstat (limited to 'lingucomponent')
-rw-r--r-- | lingucomponent/source/lingutil/lingutil.cxx | 159 |
1 files changed, 105 insertions, 54 deletions
diff --git a/lingucomponent/source/lingutil/lingutil.cxx b/lingucomponent/source/lingutil/lingutil.cxx index 707f0a91f292..fd7151fc7b07 100644 --- a/lingucomponent/source/lingutil/lingutil.cxx +++ b/lingucomponent/source/lingutil/lingutil.cxx @@ -23,8 +23,10 @@ #include <osl/thread.h> #include <osl/file.hxx> +#include <osl/process.h> #include <tools/debug.hxx> #include <tools/urlobj.hxx> +#include <tools/getprocessworkingdir.hxx> #include <i18nlangtag/languagetag.hxx> #include <i18nlangtag/mslangid.hxx> #include <unotools/lingucfg.hxx> @@ -53,64 +55,17 @@ OString Win_AddLongPathPrefix( const OString &rPathName ) } #endif //defined(WNT) -// build list of old style dictionaries (not as extensions) to use. -// User installed dictionaries (the ones residing in the user paths) -// will get precedence over system installed ones for the same language. -std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType ) -{ - std::vector< SvtLinguConfigDictionaryEntry > aRes; - - if (!pDicType) - return aRes; - - OUString aFormatName; - OUString aDicExtension; -#ifdef SYSTEM_DICTS - OUString aSystemDir; - OUString aSystemPrefix; - OUString aSystemSuffix; -#endif - if (strcmp( pDicType, "DICT" ) == 0) - { - aFormatName = "DICT_SPELL"; - aDicExtension = ".dic"; -#ifdef SYSTEM_DICTS - aSystemDir = DICT_SYSTEM_DIR; - aSystemSuffix = aDicExtension; -#endif - } - else if (strcmp( pDicType, "HYPH" ) == 0) - { - aFormatName = "DICT_HYPH"; - aDicExtension = ".dic"; -#ifdef SYSTEM_DICTS - aSystemDir = HYPH_SYSTEM_DIR; - aSystemPrefix = "hyph_"; - aSystemSuffix = aDicExtension; -#endif - } - else if (strcmp( pDicType, "THES" ) == 0) - { - aFormatName = "DICT_THES"; - aDicExtension = ".dat"; -#ifdef SYSTEM_DICTS - aSystemDir = THES_SYSTEM_DIR; - aSystemPrefix = "th_"; - aSystemSuffix = "_v2.dat"; -#endif - } - - if (aFormatName.isEmpty() || aDicExtension.isEmpty()) - return aRes; - #ifdef SYSTEM_DICTS +// find old style dictionaries in system directories +void GetOldStyleDicsInDir( + OUString& aSystemDir, OUString& aFormatName, + OUString& aSystemSuffix, OUString& aSystemPrefix, + std::set< OUString >& aDicLangInUse, + std::vector< SvtLinguConfigDictionaryEntry >& aRes ) +{ osl::Directory aSystemDicts(aSystemDir); if (aSystemDicts.open() == osl::FileBase::E_None) { - // set of languages to remember the language where it is already - // decided to make use of the dictionary. - std::set< OUString > aDicLangInUse; - osl::DirectoryItem aItem; osl::FileStatus aFileStatus(osl_FileStatus_Mask_FileURL); while (aSystemDicts.getNextItem(aItem) == osl::FileBase::E_None) @@ -170,6 +125,102 @@ std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicTy } } } +} +#endif + +// build list of old style dictionaries (not as extensions) to use. +// User installed dictionaries (the ones residing in the user paths) +// will get precedence over system installed ones for the same language. +std::vector< SvtLinguConfigDictionaryEntry > GetOldStyleDics( const char *pDicType ) +{ + std::vector< SvtLinguConfigDictionaryEntry > aRes; + + if (!pDicType) + return aRes; + + OUString aFormatName; + OUString aDicExtension; +#ifdef SYSTEM_DICTS + OUString aSystemDir; + OUString aSystemPrefix; + OUString aSystemSuffix; +#endif + if (strcmp( pDicType, "DICT" ) == 0) + { + aFormatName = "DICT_SPELL"; + aDicExtension = ".dic"; +#ifdef SYSTEM_DICTS + aSystemDir = DICT_SYSTEM_DIR; + aSystemSuffix = aDicExtension; +#endif + } + else if (strcmp( pDicType, "HYPH" ) == 0) + { + aFormatName = "DICT_HYPH"; + aDicExtension = ".dic"; +#ifdef SYSTEM_DICTS + aSystemDir = HYPH_SYSTEM_DIR; + aSystemPrefix = "hyph_"; + aSystemSuffix = aDicExtension; +#endif + } + else if (strcmp( pDicType, "THES" ) == 0) + { + aFormatName = "DICT_THES"; + aDicExtension = ".dat"; +#ifdef SYSTEM_DICTS + aSystemDir = THES_SYSTEM_DIR; + aSystemPrefix = "th_"; + aSystemSuffix = "_v2.dat"; +#endif + } + + if (aFormatName.isEmpty() || aDicExtension.isEmpty()) + return aRes; + +#ifdef SYSTEM_DICTS + // set of languages to remember the language where it is already + // decided to make use of the dictionary. + std::set< OUString > aDicLangInUse; + + // follow the hunspell tool's example and check DICPATH for preferred dictionaries + rtl_uString * pSearchPath = nullptr; + osl_getEnvironment(OUString("DICPATH").pData, &pSearchPath); + + if (pSearchPath) + { + OUString aSearchPath(pSearchPath); + rtl_uString_release(pSearchPath); + + sal_Int32 nIndex = 0; + do + { + OUString aSystem = aSearchPath.getToken(0, ':', nIndex); + OUString aCWD; + OUString aRelative; + OUString aAbsolute; + + if (!tools::getProcessWorkingDir(aCWD)) + continue; + if (osl::FileBase::getFileURLFromSystemPath(aSystem, aRelative) + != osl::FileBase::E_None) + continue; + if (osl::FileBase::getAbsoluteFileURL(aCWD, aRelative, aAbsolute) + != osl::FileBase::E_None) + continue; + + // GetOldStyleDicsInDir will make sure the dictionary is the right + // type based on its prefix, that way hyphen, mythes and regular + // dictionaries can live in one directory + GetOldStyleDicsInDir(aAbsolute, aFormatName, aSystemSuffix, + aSystemPrefix, aDicLangInUse, aRes); + } + while (nIndex != -1); + } + + // load system directories last so that DICPATH prevails + GetOldStyleDicsInDir(aSystemDir, aFormatName, aSystemSuffix, aSystemPrefix, + aDicLangInUse, aRes); #endif return aRes; |