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