merge the formfield patch from ooo-build
[ooovba.git] / linguistic / source / misc2.cxx
blob297366322f8bafd17b76fcc7dba0ece8e607f8b4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: misc2.cxx,v $
10 * $Revision: 1.3 $
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_linguistic.hxx"
33 #include <tools/string.hxx>
34 #include <tools/fsys.hxx>
35 #include <tools/urlobj.hxx>
36 #include <ucbhelper/content.hxx>
37 #include <tools/debug.hxx>
38 #include <svtools/pathoptions.hxx>
39 #include <unotools/processfactory.hxx>
40 #include <unotools/localfilehelper.hxx>
41 #include <unotools/localedatawrapper.hxx>
42 #include <unotools/ucbhelper.hxx>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <com/sun/star/beans/XFastPropertySet.hpp>
46 #include <com/sun/star/beans/PropertyValues.hpp>
47 #include <com/sun/star/uno/Sequence.hxx>
48 #include <com/sun/star/uno/Reference.h>
50 #include "misc.hxx"
52 using namespace com::sun::star;
54 namespace linguistic
57 ///////////////////////////////////////////////////////////////////////////
59 BOOL FileExists( const String &rMainURL )
61 BOOL bExists = FALSE;
62 if (rMainURL.Len())
64 try
66 ::ucbhelper::Content aContent( rMainURL,
67 uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >());
68 bExists = aContent.isDocument();
70 catch (uno::Exception &)
74 return bExists;
78 #ifdef TL_OUTDATED
80 String GetFileURL( SvtPathOptions::Pathes ePath, const String &rFileName )
82 String aURL;
83 if (rFileName.Len())
85 INetURLObject aURLObj;
86 aURLObj.SetSmartProtocol( INET_PROT_FILE );
87 aURLObj.SetSmartURL( GetModulePath(ePath) );
88 aURLObj.Append( rFileName );
89 if (aURLObj.HasError())
91 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
93 aURL = aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
95 return aURL;
99 String GetModulePath( SvtPathOptions::Pathes ePath, BOOL bAddAccessDelim )
101 String aRes;
103 SvtPathOptions aPathOpt;
104 switch (ePath)
106 case SvtPathOptions::PATH_MODULE :
107 aRes = aPathOpt.GetModulePath();
108 break;
109 case SvtPathOptions::PATH_LINGUISTIC :
111 String aTmp( aPathOpt.GetLinguisticPath() );
112 utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp, aRes );
113 break;
116 case SvtPathOptions::PATH_USERDICTIONARY :
118 String aTmp( aPathOpt.GetUserDictionaryPath() );
119 utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp, aRes );
120 break;
123 default:
124 DBG_ASSERT( 0, "unexpected argument (path)" );
126 if (bAddAccessDelim && aRes.Len())
128 #ifdef WNT
129 aRes += '\\';
130 #else
131 aRes += '/';
132 #endif
135 return aRes;
138 #endif
140 ///////////////////////////////////////////////////////////////////////////
142 rtl::OUString StripTrailingChars( rtl::OUString &rTxt, sal_Unicode cChar )
144 sal_Int32 nTrailing = 0;
145 sal_Int32 nTxtLen = rTxt.getLength();
146 sal_Int32 nIdx = nTxtLen - 1;
147 while (nIdx >= 0 && rTxt[ nIdx-- ] == cChar)
148 ++nTrailing;
150 rtl::OUString aRes( rTxt.copy( nTxtLen - nTrailing ) );
151 rTxt = rTxt.copy( 0, nTxtLen - nTrailing );
152 return aRes;
155 ///////////////////////////////////////////////////////////////////////////
157 static uno::Sequence< rtl::OUString > GetMultiPaths_Impl(
158 const rtl::OUString &rPathPrefix,
159 sal_Int16 nPathFlags )
161 uno::Sequence< rtl::OUString > aRes;
162 uno::Sequence< rtl::OUString > aInternalPaths;
163 uno::Sequence< rtl::OUString > aUserPaths;
164 rtl::OUString aWritablePath;
166 bool bSuccess = true;
167 uno::Reference< lang::XMultiServiceFactory > xMgr( utl::getProcessServiceFactory() );
168 if (xMgr.is())
172 String aInternal( rPathPrefix );
173 String aUser( rPathPrefix );
174 String aWriteable( rPathPrefix );
175 aInternal .AppendAscii( "_internal" );
176 aUser .AppendAscii( "_user" );
177 aWriteable.AppendAscii( "_writable" );
179 uno::Reference< beans::XPropertySet > xPathSettings( xMgr->createInstance(
180 A2OU( "com.sun.star.util.PathSettings" ) ), uno::UNO_QUERY_THROW );
181 xPathSettings->getPropertyValue( aInternal ) >>= aInternalPaths;
182 xPathSettings->getPropertyValue( aUser ) >>= aUserPaths;
183 xPathSettings->getPropertyValue( aWriteable ) >>= aWritablePath;
185 catch (uno::Exception &)
187 bSuccess = false;
190 if (bSuccess)
192 // build resulting sequence by adding the pathes in the following order:
193 // 1. writable path
194 // 2. all user pathes
195 // 3. all internal pathes
196 sal_Int32 nMaxEntries = aInternalPaths.getLength() + aUserPaths.getLength();
197 if (aWritablePath.getLength() > 0)
198 ++nMaxEntries;
199 aRes.realloc( nMaxEntries );
200 rtl::OUString *pRes = aRes.getArray();
201 sal_Int32 nCount = 0; // number of actually added entries
202 if ((nPathFlags & PATH_FLAG_WRITABLE) && aWritablePath.getLength() != 0)
203 pRes[ nCount++ ] = aWritablePath;
204 for (int i = 0; i < 2; ++i)
206 const uno::Sequence< rtl::OUString > &rPathSeq = i == 0 ? aUserPaths : aInternalPaths;
207 const rtl::OUString *pPathSeq = rPathSeq.getConstArray();
208 for (sal_Int32 k = 0; k < rPathSeq.getLength(); ++k)
210 const bool bAddUser = &rPathSeq == &aUserPaths && (nPathFlags & PATH_FLAG_USER);
211 const bool bAddInternal = &rPathSeq == &aInternalPaths && (nPathFlags & PATH_FLAG_INTERNAL);
212 if ((bAddUser || bAddInternal) && pPathSeq[k].getLength() > 0)
213 pRes[ nCount++ ] = pPathSeq[k];
216 aRes.realloc( nCount );
219 return aRes;
222 rtl::OUString GetDictionaryWriteablePath()
224 uno::Sequence< rtl::OUString > aPaths( GetMultiPaths_Impl( A2OU("Dictionary"), PATH_FLAG_WRITABLE ) );
225 DBG_ASSERT( aPaths.getLength() == 1, "Dictionary_writable path corrupted?" );
226 String aRes;
227 if (aPaths.getLength() > 0)
228 aRes = aPaths[0];
229 return aRes;
232 uno::Sequence< rtl::OUString > GetDictionaryPaths( sal_Int16 nPathFlags )
234 return GetMultiPaths_Impl( A2OU("Dictionary"), nPathFlags );
237 uno::Sequence< rtl::OUString > GetLinguisticPaths( sal_Int16 nPathFlags )
239 return GetMultiPaths_Impl( A2OU("Linguistic"), nPathFlags );
242 String GetWritableDictionaryURL( const String &rDicName )
244 // new user writable dictionaries should be created in the 'writable' path
245 String aDirName( GetDictionaryWriteablePath() );
247 // build URL to use for a new (persistent) dictionary
248 INetURLObject aURLObj;
249 aURLObj.SetSmartProtocol( INET_PROT_FILE );
250 aURLObj.SetSmartURL( aDirName );
251 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
252 aURLObj.Append( rDicName, INetURLObject::ENCODE_ALL );
253 DBG_ASSERT(!aURLObj.HasError(), "lng : invalid URL");
255 return aURLObj.GetMainURL( INetURLObject::DECODE_TO_IURI );
259 String SearchFileInPaths(
260 const String &rFile,
261 const uno::Sequence< rtl::OUString > &rPaths )
263 //!! see also SvtPathOptions::SearchFile for the riginal code
265 String aRes;
267 // check in all paths...
268 const sal_Int32 nPaths = rPaths.getLength();
269 for (sal_Int32 k = 0; k < nPaths; ++k)
271 BOOL bIsURL = TRUE;
272 INetURLObject aObj( rPaths[k] );
273 if ( aObj.HasError() )
275 bIsURL = FALSE;
276 String aURL;
277 if ( utl::LocalFileHelper::ConvertPhysicalNameToURL( rPaths[k], aURL ) )
278 aObj.SetURL( aURL );
281 xub_StrLen i, nCount = rFile.GetTokenCount( '/' );
282 for ( i = 0; i < nCount; ++i )
283 aObj.insertName( rFile.GetToken( i, '/' ) );
284 bool bRet = ::utl::UCBContentHelper::Exists( aObj.GetMainURL( INetURLObject::NO_DECODE ) );
286 if ( bRet )
288 if ( !bIsURL )
289 ::utl::LocalFileHelper::ConvertURLToPhysicalName(
290 aObj.GetMainURL( INetURLObject::NO_DECODE ), aRes );
291 else
292 aRes = aObj.GetMainURL( INetURLObject::NO_DECODE );
293 break;
297 return aRes;
301 } // namespace linguistic