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