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_svl.hxx" 30 31 #include <stdio.h> 32 33 #include <com/sun/star/ucb/XSimpleFileAccess.hpp> 34 #include <com/sun/star/ucb/XCommandEnvironment.hpp> 35 #include <com/sun/star/ucb/XContent.hpp> 36 #include <com/sun/star/ucb/InsertCommandArgument.hpp> 37 #include <com/sun/star/ucb/InteractiveIOException.hpp> 38 #include <com/sun/star/io/WrongFormatException.hpp> 39 40 #include <osl/time.h> 41 #include <osl/security.hxx> 42 #include <osl/socket.hxx> 43 44 #include <rtl/string.hxx> 45 #include <rtl/ustring.hxx> 46 #include <rtl/strbuf.hxx> 47 #include <rtl/ustrbuf.hxx> 48 49 #include <comphelper/processfactory.hxx> 50 #include <ucbhelper/content.hxx> 51 52 #include <tools/urlobj.hxx> 53 #include <tools/stream.hxx> 54 #include <unotools/bootstrap.hxx> 55 #include <unotools/streamwrap.hxx> 56 57 #include <unotools/useroptions.hxx> 58 59 #include <svl/sharecontrolfile.hxx> 60 61 using namespace ::com::sun::star; 62 63 namespace svt { 64 65 // ---------------------------------------------------------------------- 66 ShareControlFile::ShareControlFile( const ::rtl::OUString& aOrigURL, const uno::Reference< lang::XMultiServiceFactory >& xFactory ) 67 : LockFileCommon( aOrigURL, xFactory, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".~sharing." ) ) ) 68 { 69 OpenStream(); 70 71 if ( !IsValid() ) 72 throw io::NotConnectedException(); 73 } 74 75 // ---------------------------------------------------------------------- 76 ShareControlFile::~ShareControlFile() 77 { 78 try 79 { 80 Close(); 81 } 82 catch( uno::Exception& ) 83 {} 84 } 85 86 // ---------------------------------------------------------------------- 87 void ShareControlFile::OpenStream() 88 { 89 // if it is called outside of constructor the mutex must be locked already 90 91 if ( !m_xStream.is() && m_aURL.getLength() ) 92 { 93 uno::Reference< ucb::XCommandEnvironment > xDummyEnv; 94 ::ucbhelper::Content aContent = ::ucbhelper::Content( m_aURL, xDummyEnv ); 95 96 uno::Reference< ucb::XContentIdentifier > xContId( aContent.get().is() ? aContent.get()->getIdentifier() : 0 ); 97 if ( !xContId.is() || !xContId->getContentProviderScheme().equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "file" ) ) ) ) 98 throw io::IOException(); // the implementation supports only local files for now 99 100 uno::Reference< io::XStream > xStream; 101 102 // Currently the locking of the original document is intended to be used. 103 // That means that the shared file should be accessed only when the original document is locked and only by user who has locked the document. 104 // TODO/LATER: should the own file locking be used? 105 106 try 107 { 108 xStream = aContent.openWriteableStreamNoLock(); 109 } 110 catch ( ucb::InteractiveIOException const & e ) 111 { 112 if ( e.Code == ucb::IOErrorCode_NOT_EXISTING ) 113 { 114 // Create file... 115 SvMemoryStream aStream(0,0); 116 uno::Reference< io::XInputStream > xInput( new ::utl::OInputStreamWrapper( aStream ) ); 117 ucb::InsertCommandArgument aInsertArg; 118 aInsertArg.Data = xInput; 119 aInsertArg.ReplaceExisting = sal_False; 120 aContent.executeCommand( rtl::OUString::createFromAscii( "insert" ), uno::makeAny( aInsertArg ) ); 121 122 // try to let the file be hidden if possible 123 try { 124 aContent.setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "IsHidden" ) ), uno::makeAny( sal_True ) ); 125 } catch( uno::Exception& ) {} 126 127 // Try to open one more time 128 xStream = aContent.openWriteableStreamNoLock(); 129 } 130 else 131 throw; 132 } 133 134 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW ); 135 m_xInputStream.set( xStream->getInputStream(), uno::UNO_QUERY_THROW ); 136 m_xOutputStream.set( xStream->getOutputStream(), uno::UNO_QUERY_THROW ); 137 m_xTruncate.set( m_xOutputStream, uno::UNO_QUERY_THROW ); 138 m_xStream = xStream; 139 } 140 } 141 142 // ---------------------------------------------------------------------- 143 void ShareControlFile::Close() 144 { 145 // if it is called outside of destructor the mutex must be locked 146 147 if ( m_xStream.is() ) 148 { 149 try 150 { 151 if ( m_xInputStream.is() ) 152 m_xInputStream->closeInput(); 153 if ( m_xOutputStream.is() ) 154 m_xOutputStream->closeOutput(); 155 } 156 catch( uno::Exception& ) 157 {} 158 159 m_xStream = uno::Reference< io::XStream >(); 160 m_xInputStream = uno::Reference< io::XInputStream >(); 161 m_xOutputStream = uno::Reference< io::XOutputStream >(); 162 m_xSeekable = uno::Reference< io::XSeekable >(); 163 m_xTruncate = uno::Reference< io::XTruncate >(); 164 m_aUsersData.realloc( 0 ); 165 } 166 } 167 168 // ---------------------------------------------------------------------- 169 uno::Sequence< uno::Sequence< ::rtl::OUString > > ShareControlFile::GetUsersData() 170 { 171 ::osl::MutexGuard aGuard( m_aMutex ); 172 173 if ( !IsValid() ) 174 throw io::NotConnectedException(); 175 176 if ( !m_aUsersData.getLength() ) 177 { 178 sal_Int64 nLength = m_xSeekable->getLength(); 179 if ( nLength > SAL_MAX_INT32 ) 180 throw uno::RuntimeException(); 181 182 uno::Sequence< sal_Int8 > aBuffer( (sal_Int32)nLength ); 183 m_xSeekable->seek( 0 ); 184 185 sal_Int32 nRead = m_xInputStream->readBytes( aBuffer, (sal_Int32)nLength ); 186 nLength -= nRead; 187 while ( nLength > 0 ) 188 { 189 uno::Sequence< sal_Int8 > aTmpBuf( (sal_Int32)nLength ); 190 nRead = m_xInputStream->readBytes( aTmpBuf, (sal_Int32)nLength ); 191 if ( nRead > nLength ) 192 throw uno::RuntimeException(); 193 194 for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ ) 195 aBuffer[aBuffer.getLength() - (sal_Int32)nLength + nInd] = aTmpBuf[nInd]; 196 nLength -= nRead; 197 } 198 199 m_aUsersData = ParseList( aBuffer ); 200 } 201 202 return m_aUsersData; 203 } 204 205 // ---------------------------------------------------------------------- 206 void ShareControlFile::SetUsersDataAndStore( const uno::Sequence< uno::Sequence< ::rtl::OUString > >& aUsersData ) 207 { 208 ::osl::MutexGuard aGuard( m_aMutex ); 209 210 if ( !IsValid() ) 211 throw io::NotConnectedException(); 212 213 if ( !m_xTruncate.is() || !m_xOutputStream.is() || !m_xSeekable.is() ) 214 throw uno::RuntimeException(); 215 216 m_xTruncate->truncate(); 217 m_xSeekable->seek( 0 ); 218 219 ::rtl::OUStringBuffer aBuffer; 220 for ( sal_Int32 nInd = 0; nInd < aUsersData.getLength(); nInd++ ) 221 { 222 if ( aUsersData[nInd].getLength() != SHARED_ENTRYSIZE ) 223 throw lang::IllegalArgumentException(); 224 225 for ( sal_Int32 nEntryInd = 0; nEntryInd < SHARED_ENTRYSIZE; nEntryInd++ ) 226 { 227 aBuffer.append( EscapeCharacters( aUsersData[nInd][nEntryInd] ) ); 228 if ( nEntryInd < SHARED_ENTRYSIZE - 1 ) 229 aBuffer.append( (sal_Unicode)',' ); 230 else 231 aBuffer.append( (sal_Unicode)';' ); 232 } 233 } 234 235 ::rtl::OString aStringData( ::rtl::OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ) ); 236 uno::Sequence< sal_Int8 > aData( (sal_Int8*)aStringData.getStr(), aStringData.getLength() ); 237 m_xOutputStream->writeBytes( aData ); 238 m_aUsersData = aUsersData; 239 } 240 241 // ---------------------------------------------------------------------- 242 uno::Sequence< ::rtl::OUString > ShareControlFile::InsertOwnEntry() 243 { 244 ::osl::MutexGuard aGuard( m_aMutex ); 245 246 if ( !IsValid() ) 247 throw io::NotConnectedException(); 248 249 GetUsersData(); 250 uno::Sequence< ::uno::Sequence< ::rtl::OUString > > aNewData( m_aUsersData.getLength() + 1 ); 251 uno::Sequence< ::rtl::OUString > aNewEntry = GenerateOwnEntry(); 252 253 sal_Bool bExists = sal_False; 254 sal_Int32 nNewInd = 0; 255 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); nInd++ ) 256 { 257 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE ) 258 { 259 if ( m_aUsersData[nInd][SHARED_LOCALHOST_ID] == aNewEntry[SHARED_LOCALHOST_ID] 260 && m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] == aNewEntry[SHARED_SYSUSERNAME_ID] 261 && m_aUsersData[nInd][SHARED_USERURL_ID] == aNewEntry[SHARED_USERURL_ID] ) 262 { 263 if ( !bExists ) 264 { 265 aNewData[nNewInd] = aNewEntry; 266 bExists = sal_True; 267 } 268 } 269 else 270 { 271 aNewData[nNewInd] = m_aUsersData[nInd]; 272 } 273 274 nNewInd++; 275 } 276 } 277 278 if ( !bExists ) 279 aNewData[nNewInd++] = aNewEntry; 280 281 aNewData.realloc( nNewInd ); 282 SetUsersDataAndStore( aNewData ); 283 284 return aNewEntry; 285 } 286 287 // ---------------------------------------------------------------------- 288 bool ShareControlFile::HasOwnEntry() 289 { 290 ::osl::MutexGuard aGuard( m_aMutex ); 291 292 if ( !IsValid() ) 293 { 294 throw io::NotConnectedException(); 295 } 296 297 GetUsersData(); 298 uno::Sequence< ::rtl::OUString > aEntry = GenerateOwnEntry(); 299 300 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); ++nInd ) 301 { 302 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE && 303 m_aUsersData[nInd][SHARED_LOCALHOST_ID] == aEntry[SHARED_LOCALHOST_ID] && 304 m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] == aEntry[SHARED_SYSUSERNAME_ID] && 305 m_aUsersData[nInd][SHARED_USERURL_ID] == aEntry[SHARED_USERURL_ID] ) 306 { 307 return true; 308 } 309 } 310 311 return false; 312 } 313 314 // ---------------------------------------------------------------------- 315 void ShareControlFile::RemoveEntry( const uno::Sequence< ::rtl::OUString >& aArgEntry ) 316 { 317 ::osl::MutexGuard aGuard( m_aMutex ); 318 319 if ( !IsValid() ) 320 throw io::NotConnectedException(); 321 322 GetUsersData(); 323 324 uno::Sequence< ::rtl::OUString > aEntry = aArgEntry; 325 if ( aEntry.getLength() != SHARED_ENTRYSIZE ) 326 aEntry = GenerateOwnEntry(); 327 328 uno::Sequence< ::uno::Sequence< ::rtl::OUString > > aNewData( m_aUsersData.getLength() + 1 ); 329 330 sal_Int32 nNewInd = 0; 331 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); nInd++ ) 332 { 333 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE ) 334 { 335 if ( m_aUsersData[nInd][SHARED_LOCALHOST_ID] != aEntry[SHARED_LOCALHOST_ID] 336 || m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] != aEntry[SHARED_SYSUSERNAME_ID] 337 || m_aUsersData[nInd][SHARED_USERURL_ID] != aEntry[SHARED_USERURL_ID] ) 338 { 339 aNewData[nNewInd] = m_aUsersData[nInd]; 340 nNewInd++; 341 } 342 } 343 } 344 345 aNewData.realloc( nNewInd ); 346 SetUsersDataAndStore( aNewData ); 347 348 if ( !nNewInd ) 349 { 350 // try to remove the file if it is empty 351 RemoveFile(); 352 } 353 } 354 355 // ---------------------------------------------------------------------- 356 void ShareControlFile::RemoveFile() 357 { 358 ::osl::MutexGuard aGuard( m_aMutex ); 359 360 if ( !IsValid() ) 361 throw io::NotConnectedException(); 362 363 Close(); 364 365 uno::Reference< lang::XMultiServiceFactory > xFactory = ::comphelper::getProcessServiceFactory(); 366 uno::Reference< ::com::sun::star::ucb::XSimpleFileAccess > xSimpleFileAccess( 367 xFactory->createInstance( ::rtl::OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess") ), 368 uno::UNO_QUERY_THROW ); 369 xSimpleFileAccess->kill( m_aURL ); 370 } 371 372 } // namespace svt 373 374