1*3b8558fdSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*3b8558fdSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*3b8558fdSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*3b8558fdSAndrew Rist * distributed with this work for additional information
6*3b8558fdSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*3b8558fdSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*3b8558fdSAndrew Rist * "License"); you may not use this file except in compliance
9*3b8558fdSAndrew Rist * with the License. You may obtain a copy of the License at
10*3b8558fdSAndrew Rist *
11*3b8558fdSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*3b8558fdSAndrew Rist *
13*3b8558fdSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*3b8558fdSAndrew Rist * software distributed under the License is distributed on an
15*3b8558fdSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3b8558fdSAndrew Rist * KIND, either express or implied. See the License for the
17*3b8558fdSAndrew Rist * specific language governing permissions and limitations
18*3b8558fdSAndrew Rist * under the License.
19*3b8558fdSAndrew Rist *
20*3b8558fdSAndrew Rist *************************************************************/
21*3b8558fdSAndrew Rist
22*3b8558fdSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_linguistic.hxx"
26cdf0e10cSrcweir #include <tools/string.hxx>
27cdf0e10cSrcweir #include <tools/fsys.hxx>
28cdf0e10cSrcweir #include <tools/urlobj.hxx>
29cdf0e10cSrcweir #include <ucbhelper/content.hxx>
30cdf0e10cSrcweir #include <tools/debug.hxx>
31cdf0e10cSrcweir #include <unotools/pathoptions.hxx>
32cdf0e10cSrcweir #include <unotools/processfactory.hxx>
33cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
34cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
35cdf0e10cSrcweir #include <unotools/ucbhelper.hxx>
36cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
37cdf0e10cSrcweir #include <com/sun/star/beans/XFastPropertySet.hpp>
38cdf0e10cSrcweir
39cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValues.hpp>
40cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
41cdf0e10cSrcweir #include <com/sun/star/uno/Reference.h>
42cdf0e10cSrcweir
43cdf0e10cSrcweir #include "linguistic/misc.hxx"
44cdf0e10cSrcweir
45cdf0e10cSrcweir using namespace com::sun::star;
46cdf0e10cSrcweir
47cdf0e10cSrcweir namespace linguistic
48cdf0e10cSrcweir {
49cdf0e10cSrcweir
50cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
51cdf0e10cSrcweir
FileExists(const String & rMainURL)52cdf0e10cSrcweir sal_Bool FileExists( const String &rMainURL )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir sal_Bool bExists = sal_False;
55cdf0e10cSrcweir if (rMainURL.Len())
56cdf0e10cSrcweir {
57cdf0e10cSrcweir try
58cdf0e10cSrcweir {
59cdf0e10cSrcweir ::ucbhelper::Content aContent( rMainURL,
60cdf0e10cSrcweir uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >());
61cdf0e10cSrcweir bExists = aContent.isDocument();
62cdf0e10cSrcweir }
63cdf0e10cSrcweir catch (uno::Exception &)
64cdf0e10cSrcweir {
65cdf0e10cSrcweir }
66cdf0e10cSrcweir }
67cdf0e10cSrcweir return bExists;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir
71cdf0e10cSrcweir #ifdef TL_OUTDATED
72cdf0e10cSrcweir
GetFileURL(SvtPathOptions::Pathes ePath,const String & rFileName)73cdf0e10cSrcweir String GetFileURL( SvtPathOptions::Pathes ePath, const String &rFileName )
74cdf0e10cSrcweir {
75cdf0e10cSrcweir String aURL;
76cdf0e10cSrcweir if (rFileName.Len())
77cdf0e10cSrcweir {
78cdf0e10cSrcweir INetURLObject aURLObj;
79cdf0e10cSrcweir aURLObj.SetSmartProtocol( INET_PROT_FILE );
80cdf0e10cSrcweir aURLObj.SetSmartURL( GetModulePath(ePath) );
81cdf0e10cSrcweir aURLObj.Append( rFileName );
82cdf0e10cSrcweir if (aURLObj.HasError())
83cdf0e10cSrcweir {
84cdf0e10cSrcweir DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
85cdf0e10cSrcweir }
86cdf0e10cSrcweir aURL = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir return aURL;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir
GetModulePath(SvtPathOptions::Pathes ePath,sal_Bool bAddAccessDelim)92cdf0e10cSrcweir String GetModulePath( SvtPathOptions::Pathes ePath, sal_Bool bAddAccessDelim )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir String aRes;
95cdf0e10cSrcweir
96cdf0e10cSrcweir SvtPathOptions aPathOpt;
97cdf0e10cSrcweir switch (ePath)
98cdf0e10cSrcweir {
99cdf0e10cSrcweir case SvtPathOptions::PATH_MODULE :
100cdf0e10cSrcweir aRes = aPathOpt.GetModulePath();
101cdf0e10cSrcweir break;
102cdf0e10cSrcweir case SvtPathOptions::PATH_LINGUISTIC :
103cdf0e10cSrcweir {
104cdf0e10cSrcweir String aTmp( aPathOpt.GetLinguisticPath() );
105cdf0e10cSrcweir utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp, aRes );
106cdf0e10cSrcweir break;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir /*
109cdf0e10cSrcweir case SvtPathOptions::PATH_USERDICTIONARY :
110cdf0e10cSrcweir {
111cdf0e10cSrcweir String aTmp( aPathOpt.GetUserDictionaryPath() );
112cdf0e10cSrcweir utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp, aRes );
113cdf0e10cSrcweir break;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir */
116cdf0e10cSrcweir default:
117cdf0e10cSrcweir DBG_ASSERT( 0, "unexpected argument (path)" );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir if (bAddAccessDelim && aRes.Len())
120cdf0e10cSrcweir {
121cdf0e10cSrcweir #ifdef WNT
122cdf0e10cSrcweir aRes += '\\';
123cdf0e10cSrcweir #else
124cdf0e10cSrcweir aRes += '/';
125cdf0e10cSrcweir #endif
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir return aRes;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
131cdf0e10cSrcweir #endif
132cdf0e10cSrcweir
133cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
134cdf0e10cSrcweir
StripTrailingChars(rtl::OUString & rTxt,sal_Unicode cChar)135cdf0e10cSrcweir rtl::OUString StripTrailingChars( rtl::OUString &rTxt, sal_Unicode cChar )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir sal_Int32 nTrailing = 0;
138cdf0e10cSrcweir sal_Int32 nTxtLen = rTxt.getLength();
139cdf0e10cSrcweir sal_Int32 nIdx = nTxtLen - 1;
140cdf0e10cSrcweir while (nIdx >= 0 && rTxt[ nIdx-- ] == cChar)
141cdf0e10cSrcweir ++nTrailing;
142cdf0e10cSrcweir
143cdf0e10cSrcweir rtl::OUString aRes( rTxt.copy( nTxtLen - nTrailing ) );
144cdf0e10cSrcweir rTxt = rTxt.copy( 0, nTxtLen - nTrailing );
145cdf0e10cSrcweir return aRes;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir
148cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////////
149cdf0e10cSrcweir
GetMultiPaths_Impl(const rtl::OUString & rPathPrefix,sal_Int16 nPathFlags)150cdf0e10cSrcweir static uno::Sequence< rtl::OUString > GetMultiPaths_Impl(
151cdf0e10cSrcweir const rtl::OUString &rPathPrefix,
152cdf0e10cSrcweir sal_Int16 nPathFlags )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir uno::Sequence< rtl::OUString > aRes;
155cdf0e10cSrcweir uno::Sequence< rtl::OUString > aInternalPaths;
156cdf0e10cSrcweir uno::Sequence< rtl::OUString > aUserPaths;
157cdf0e10cSrcweir rtl::OUString aWritablePath;
158cdf0e10cSrcweir
159cdf0e10cSrcweir bool bSuccess = true;
160cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > xMgr( utl::getProcessServiceFactory() );
161cdf0e10cSrcweir if (xMgr.is())
162cdf0e10cSrcweir {
163cdf0e10cSrcweir try
164cdf0e10cSrcweir {
165cdf0e10cSrcweir String aInternal( rPathPrefix );
166cdf0e10cSrcweir String aUser( rPathPrefix );
167cdf0e10cSrcweir String aWriteable( rPathPrefix );
168cdf0e10cSrcweir aInternal .AppendAscii( "_internal" );
169cdf0e10cSrcweir aUser .AppendAscii( "_user" );
170cdf0e10cSrcweir aWriteable.AppendAscii( "_writable" );
171cdf0e10cSrcweir
172cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPathSettings( xMgr->createInstance(
173cdf0e10cSrcweir A2OU( "com.sun.star.util.PathSettings" ) ), uno::UNO_QUERY_THROW );
174cdf0e10cSrcweir xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
175cdf0e10cSrcweir xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
176cdf0e10cSrcweir xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir catch (uno::Exception &)
179cdf0e10cSrcweir {
180cdf0e10cSrcweir bSuccess = false;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir }
183cdf0e10cSrcweir if (bSuccess)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir // build resulting sequence by adding the pathes in the following order:
186cdf0e10cSrcweir // 1. writable path
187cdf0e10cSrcweir // 2. all user pathes
188cdf0e10cSrcweir // 3. all internal pathes
189cdf0e10cSrcweir sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
190cdf0e10cSrcweir if (aWritablePath.getLength() > 0)
191cdf0e10cSrcweir ++nMaxEntries;
192cdf0e10cSrcweir aRes.realloc( nMaxEntries );
193cdf0e10cSrcweir rtl::OUString *pRes = aRes.getArray();
194cdf0e10cSrcweir sal_Int32 nCount = 0; // number of actually added entries
195cdf0e10cSrcweir if ((nPathFlags & PATH_FLAG_WRITABLE) && aWritablePath.getLength() != 0)
196cdf0e10cSrcweir pRes[ nCount++ ] = aWritablePath;
197cdf0e10cSrcweir for (int i = 0; i < 2; ++i)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir const uno::Sequence< rtl::OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
200cdf0e10cSrcweir const rtl::OUString *pPathSeq = rPathSeq.getConstArray();
201cdf0e10cSrcweir for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & PATH_FLAG_USER);
204cdf0e10cSrcweir const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & PATH_FLAG_INTERNAL);
205cdf0e10cSrcweir if ((bAddUser || bAddInternal) && pPathSeq[k].getLength() > 0)
206cdf0e10cSrcweir pRes[ nCount++ ] = pPathSeq[k];
207cdf0e10cSrcweir }
208cdf0e10cSrcweir }
209cdf0e10cSrcweir aRes.realloc( nCount );
210cdf0e10cSrcweir }
211cdf0e10cSrcweir
212cdf0e10cSrcweir return aRes;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
GetDictionaryWriteablePath()215cdf0e10cSrcweir rtl::OUString GetDictionaryWriteablePath()
216cdf0e10cSrcweir {
217cdf0e10cSrcweir uno::Sequence< rtl::OUString > aPaths( GetMultiPaths_Impl( A2OU("Dictionary"), PATH_FLAG_WRITABLE ) );
218cdf0e10cSrcweir DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
219cdf0e10cSrcweir String aRes;
220cdf0e10cSrcweir if (aPaths.getLength() > 0)
221cdf0e10cSrcweir aRes = aPaths[0];
222cdf0e10cSrcweir return aRes;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir
GetDictionaryPaths(sal_Int16 nPathFlags)225cdf0e10cSrcweir uno::Sequence< rtl::OUString > GetDictionaryPaths( sal_Int16 nPathFlags )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir return GetMultiPaths_Impl( A2OU("Dictionary"), nPathFlags );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir
GetLinguisticPaths(sal_Int16 nPathFlags)230cdf0e10cSrcweir uno::Sequence< rtl::OUString > GetLinguisticPaths( sal_Int16 nPathFlags )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir return GetMultiPaths_Impl( A2OU("Linguistic"), nPathFlags );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir
GetWritableDictionaryURL(const String & rDicName)235cdf0e10cSrcweir String GetWritableDictionaryURL( const String &rDicName )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir // new user writable dictionaries should be created in the 'writable' path
238cdf0e10cSrcweir String aDirName( GetDictionaryWriteablePath() );
239cdf0e10cSrcweir
240cdf0e10cSrcweir // build URL to use for a new (persistent) dictionary
241cdf0e10cSrcweir INetURLObject aURLObj;
242cdf0e10cSrcweir aURLObj.SetSmartProtocol( INET_PROT_FILE );
243cdf0e10cSrcweir aURLObj.SetSmartURL( aDirName );
244cdf0e10cSrcweir DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
245cdf0e10cSrcweir aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
246cdf0e10cSrcweir DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
247cdf0e10cSrcweir
248cdf0e10cSrcweir // NO_DECODE preserves the escape sequences that might be included in aDirName
249cdf0e10cSrcweir // depending on the characters used in the path string. (Needed when comparing
250cdf0e10cSrcweir // the dictionary URL with GetDictionaryWriteablePath in DicList::createDictionary.)
251cdf0e10cSrcweir return aURLObj.GetMainURL( INetURLObject::NO_DECODE );
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir
SearchFileInPaths(const String & rFile,const uno::Sequence<rtl::OUString> & rPaths)255cdf0e10cSrcweir String SearchFileInPaths(
256cdf0e10cSrcweir const String &rFile,
257cdf0e10cSrcweir const uno::Sequence< rtl::OUString > &rPaths )
258cdf0e10cSrcweir {
259cdf0e10cSrcweir //!! see also SvtPathOptions::SearchFile for the riginal code
260cdf0e10cSrcweir
261cdf0e10cSrcweir String aRes;
262cdf0e10cSrcweir
263cdf0e10cSrcweir // check in all paths...
264cdf0e10cSrcweir const sal_Int32 nPaths = rPaths.getLength();
265cdf0e10cSrcweir for (sal_Int32 k = 0; k < nPaths; ++k)
266cdf0e10cSrcweir {
267cdf0e10cSrcweir sal_Bool bIsURL = sal_True;
268cdf0e10cSrcweir INetURLObject aObj( rPaths[k] );
269cdf0e10cSrcweir if ( aObj.HasError() )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir bIsURL = sal_False;
272cdf0e10cSrcweir String aURL;
273cdf0e10cSrcweir if ( utl::LocalFileHelper::ConvertPhysicalNameToURL( rPaths[k], aURL ) )
274cdf0e10cSrcweir aObj.SetURL( aURL );
275cdf0e10cSrcweir }
276cdf0e10cSrcweir
277cdf0e10cSrcweir xub_StrLen i, nCount = rFile.GetTokenCount( '/' );
278cdf0e10cSrcweir for ( i = 0; i < nCount; ++i )
279cdf0e10cSrcweir aObj.insertName( rFile.GetToken( i, '/' ) );
280cdf0e10cSrcweir bool bRet = ::utl::UCBContentHelper::Exists( aObj.GetMainURL( INetURLObject::NO_DECODE ) );
281cdf0e10cSrcweir
282cdf0e10cSrcweir if ( bRet )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir if ( !bIsURL )
285cdf0e10cSrcweir ::utl::LocalFileHelper::ConvertURLToPhysicalName(
286cdf0e10cSrcweir aObj.GetMainURL( INetURLObject::NO_DECODE ), aRes );
287cdf0e10cSrcweir else
288cdf0e10cSrcweir aRes = aObj.GetMainURL( INetURLObject::NO_DECODE );
289cdf0e10cSrcweir break;
290cdf0e10cSrcweir }
291cdf0e10cSrcweir }
292cdf0e10cSrcweir
293cdf0e10cSrcweir return aRes;
294cdf0e10cSrcweir }
295cdf0e10cSrcweir
296cdf0e10cSrcweir
297cdf0e10cSrcweir } // namespace linguistic
298cdf0e10cSrcweir
299cdf0e10cSrcweir
300