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: misc2.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_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>
52 using namespace com::sun::star
;
57 ///////////////////////////////////////////////////////////////////////////
59 BOOL
FileExists( const String
&rMainURL
)
66 ::ucbhelper::Content
aContent( rMainURL
,
67 uno::Reference
< ::com::sun::star::ucb::XCommandEnvironment
>());
68 bExists
= aContent
.isDocument();
70 catch (uno::Exception
&)
80 String
GetFileURL( SvtPathOptions::Pathes ePath
, const String
&rFileName
)
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
);
99 String
GetModulePath( SvtPathOptions::Pathes ePath
, BOOL bAddAccessDelim
)
103 SvtPathOptions aPathOpt
;
106 case SvtPathOptions::PATH_MODULE
:
107 aRes
= aPathOpt
.GetModulePath();
109 case SvtPathOptions::PATH_LINGUISTIC
:
111 String
aTmp( aPathOpt
.GetLinguisticPath() );
112 utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp
, aRes
);
116 case SvtPathOptions::PATH_USERDICTIONARY :
118 String aTmp( aPathOpt.GetUserDictionaryPath() );
119 utl::LocalFileHelper::ConvertURLToPhysicalName( aTmp, aRes );
124 DBG_ASSERT( 0, "unexpected argument (path)" );
126 if (bAddAccessDelim
&& aRes
.Len())
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
)
150 rtl::OUString
aRes( rTxt
.copy( nTxtLen
- nTrailing
) );
151 rTxt
= rTxt
.copy( 0, nTxtLen
- nTrailing
);
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() );
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
&)
192 // build resulting sequence by adding the pathes in the following order:
194 // 2. all user pathes
195 // 3. all internal pathes
196 sal_Int32 nMaxEntries
= aInternalPaths
.getLength() + aUserPaths
.getLength();
197 if (aWritablePath
.getLength() > 0)
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
);
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?" );
227 if (aPaths
.getLength() > 0)
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(
261 const uno::Sequence
< rtl::OUString
> &rPaths
)
263 //!! see also SvtPathOptions::SearchFile for the riginal code
267 // check in all paths...
268 const sal_Int32 nPaths
= rPaths
.getLength();
269 for (sal_Int32 k
= 0; k
< nPaths
; ++k
)
272 INetURLObject
aObj( rPaths
[k
] );
273 if ( aObj
.HasError() )
277 if ( utl::LocalFileHelper::ConvertPhysicalNameToURL( rPaths
[k
], 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
) );
289 ::utl::LocalFileHelper::ConvertURLToPhysicalName(
290 aObj
.GetMainURL( INetURLObject::NO_DECODE
), aRes
);
292 aRes
= aObj
.GetMainURL( INetURLObject::NO_DECODE
);
301 } // namespace linguistic