bump product version to 4.1.6.2
[LibreOffice.git] / svl / source / misc / sharecontrolfile.cxx
blob49443c868261bf3a300fb4f2d192519cdf83ffa3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <stdio.h>
23 #include <com/sun/star/ucb/SimpleFileAccess.hpp>
24 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
25 #include <com/sun/star/ucb/XContent.hpp>
26 #include <com/sun/star/ucb/InsertCommandArgument.hpp>
27 #include <com/sun/star/ucb/InteractiveIOException.hpp>
28 #include <com/sun/star/io/WrongFormatException.hpp>
30 #include <osl/time.h>
31 #include <osl/security.hxx>
32 #include <osl/socket.hxx>
34 #include <rtl/string.hxx>
35 #include <rtl/ustring.hxx>
36 #include <rtl/strbuf.hxx>
37 #include <rtl/ustrbuf.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <ucbhelper/content.hxx>
42 #include <tools/stream.hxx>
43 #include <unotools/bootstrap.hxx>
44 #include <unotools/streamwrap.hxx>
46 #include <unotools/useroptions.hxx>
48 #include <svl/sharecontrolfile.hxx>
50 using namespace ::com::sun::star;
52 namespace svt {
54 // ----------------------------------------------------------------------
55 ShareControlFile::ShareControlFile( const OUString& aOrigURL )
56 : LockFileCommon( aOrigURL, OUString( ".~sharing." ) )
58 OpenStream();
60 if ( !IsValid() )
61 throw io::NotConnectedException();
64 // ----------------------------------------------------------------------
65 ShareControlFile::~ShareControlFile()
67 try
69 Close();
71 catch( uno::Exception& )
75 // ----------------------------------------------------------------------
76 void ShareControlFile::OpenStream()
78 // if it is called outside of constructor the mutex must be locked already
80 if ( !m_xStream.is() && !m_aURL.isEmpty() )
82 uno::Reference< ucb::XCommandEnvironment > xDummyEnv;
83 ::ucbhelper::Content aContent = ::ucbhelper::Content( m_aURL, xDummyEnv, comphelper::getProcessComponentContext() );
85 uno::Reference< ucb::XContentIdentifier > xContId( aContent.get().is() ? aContent.get()->getIdentifier() : 0 );
86 if ( !xContId.is() || xContId->getContentProviderScheme() != "file" )
87 throw io::IOException(); // the implementation supports only local files for now
89 uno::Reference< io::XStream > xStream;
91 // Currently the locking of the original document is intended to be used.
92 // 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.
93 // TODO/LATER: should the own file locking be used?
95 try
97 xStream = aContent.openWriteableStreamNoLock();
99 catch ( ucb::InteractiveIOException const & e )
101 if ( e.Code == ucb::IOErrorCode_NOT_EXISTING )
103 // Create file...
104 SvMemoryStream aStream(0,0);
105 uno::Reference< io::XInputStream > xInput( new ::utl::OInputStreamWrapper( aStream ) );
106 ucb::InsertCommandArgument aInsertArg;
107 aInsertArg.Data = xInput;
108 aInsertArg.ReplaceExisting = sal_False;
109 aContent.executeCommand( OUString("insert"), uno::makeAny( aInsertArg ) );
111 // try to let the file be hidden if possible
112 try {
113 aContent.setPropertyValue( OUString( "IsHidden" ), uno::makeAny( sal_True ) );
114 } catch( uno::Exception& ) {}
116 // Try to open one more time
117 xStream = aContent.openWriteableStreamNoLock();
119 else
120 throw;
123 m_xSeekable.set( xStream, uno::UNO_QUERY_THROW );
124 m_xInputStream.set( xStream->getInputStream(), uno::UNO_QUERY_THROW );
125 m_xOutputStream.set( xStream->getOutputStream(), uno::UNO_QUERY_THROW );
126 m_xTruncate.set( m_xOutputStream, uno::UNO_QUERY_THROW );
127 m_xStream = xStream;
131 // ----------------------------------------------------------------------
132 void ShareControlFile::Close()
134 // if it is called outside of destructor the mutex must be locked
136 if ( m_xStream.is() )
140 if ( m_xInputStream.is() )
141 m_xInputStream->closeInput();
142 if ( m_xOutputStream.is() )
143 m_xOutputStream->closeOutput();
145 catch( uno::Exception& )
148 m_xStream = uno::Reference< io::XStream >();
149 m_xInputStream = uno::Reference< io::XInputStream >();
150 m_xOutputStream = uno::Reference< io::XOutputStream >();
151 m_xSeekable = uno::Reference< io::XSeekable >();
152 m_xTruncate = uno::Reference< io::XTruncate >();
153 m_aUsersData.realloc( 0 );
157 // ----------------------------------------------------------------------
158 uno::Sequence< uno::Sequence< OUString > > ShareControlFile::GetUsersData()
160 ::osl::MutexGuard aGuard( m_aMutex );
162 if ( !IsValid() )
163 throw io::NotConnectedException();
165 if ( !m_aUsersData.getLength() )
167 sal_Int64 nLength = m_xSeekable->getLength();
168 if ( nLength > SAL_MAX_INT32 )
169 throw uno::RuntimeException();
171 uno::Sequence< sal_Int8 > aBuffer( (sal_Int32)nLength );
172 m_xSeekable->seek( 0 );
174 sal_Int32 nRead = m_xInputStream->readBytes( aBuffer, (sal_Int32)nLength );
175 nLength -= nRead;
176 while ( nLength > 0 )
178 uno::Sequence< sal_Int8 > aTmpBuf( (sal_Int32)nLength );
179 nRead = m_xInputStream->readBytes( aTmpBuf, (sal_Int32)nLength );
180 if ( nRead > nLength )
181 throw uno::RuntimeException();
183 for ( sal_Int32 nInd = 0; nInd < nRead; nInd++ )
184 aBuffer[aBuffer.getLength() - (sal_Int32)nLength + nInd] = aTmpBuf[nInd];
185 nLength -= nRead;
188 m_aUsersData = ParseList( aBuffer );
191 return m_aUsersData;
194 // ----------------------------------------------------------------------
195 void ShareControlFile::SetUsersDataAndStore( const uno::Sequence< uno::Sequence< OUString > >& aUsersData )
197 ::osl::MutexGuard aGuard( m_aMutex );
199 if ( !IsValid() )
200 throw io::NotConnectedException();
202 if ( !m_xTruncate.is() || !m_xOutputStream.is() || !m_xSeekable.is() )
203 throw uno::RuntimeException();
205 m_xTruncate->truncate();
206 m_xSeekable->seek( 0 );
208 OUStringBuffer aBuffer;
209 for ( sal_Int32 nInd = 0; nInd < aUsersData.getLength(); nInd++ )
211 if ( aUsersData[nInd].getLength() != SHARED_ENTRYSIZE )
212 throw lang::IllegalArgumentException();
214 for ( sal_Int32 nEntryInd = 0; nEntryInd < SHARED_ENTRYSIZE; nEntryInd++ )
216 aBuffer.append( EscapeCharacters( aUsersData[nInd][nEntryInd] ) );
217 if ( nEntryInd < SHARED_ENTRYSIZE - 1 )
218 aBuffer.append( (sal_Unicode)',' );
219 else
220 aBuffer.append( (sal_Unicode)';' );
224 OString aStringData( OUStringToOString( aBuffer.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ) );
225 uno::Sequence< sal_Int8 > aData( (sal_Int8*)aStringData.getStr(), aStringData.getLength() );
226 m_xOutputStream->writeBytes( aData );
227 m_aUsersData = aUsersData;
230 // ----------------------------------------------------------------------
231 uno::Sequence< OUString > ShareControlFile::InsertOwnEntry()
233 ::osl::MutexGuard aGuard( m_aMutex );
235 if ( !IsValid() )
236 throw io::NotConnectedException();
238 GetUsersData();
239 uno::Sequence< ::uno::Sequence< OUString > > aNewData( m_aUsersData.getLength() + 1 );
240 uno::Sequence< OUString > aNewEntry = GenerateOwnEntry();
242 bool bExists = false;
243 sal_Int32 nNewInd = 0;
244 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); nInd++ )
246 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE )
248 if ( m_aUsersData[nInd][SHARED_LOCALHOST_ID] == aNewEntry[SHARED_LOCALHOST_ID]
249 && m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] == aNewEntry[SHARED_SYSUSERNAME_ID]
250 && m_aUsersData[nInd][SHARED_USERURL_ID] == aNewEntry[SHARED_USERURL_ID] )
252 if ( !bExists )
254 aNewData[nNewInd] = aNewEntry;
255 bExists = true;
258 else
260 aNewData[nNewInd] = m_aUsersData[nInd];
263 nNewInd++;
267 if ( !bExists )
268 aNewData[nNewInd++] = aNewEntry;
270 aNewData.realloc( nNewInd );
271 SetUsersDataAndStore( aNewData );
273 return aNewEntry;
276 // ----------------------------------------------------------------------
277 bool ShareControlFile::HasOwnEntry()
279 ::osl::MutexGuard aGuard( m_aMutex );
281 if ( !IsValid() )
283 throw io::NotConnectedException();
286 GetUsersData();
287 uno::Sequence< OUString > aEntry = GenerateOwnEntry();
289 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); ++nInd )
291 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE &&
292 m_aUsersData[nInd][SHARED_LOCALHOST_ID] == aEntry[SHARED_LOCALHOST_ID] &&
293 m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] == aEntry[SHARED_SYSUSERNAME_ID] &&
294 m_aUsersData[nInd][SHARED_USERURL_ID] == aEntry[SHARED_USERURL_ID] )
296 return true;
300 return false;
303 // ----------------------------------------------------------------------
304 void ShareControlFile::RemoveEntry( const uno::Sequence< OUString >& aArgEntry )
306 ::osl::MutexGuard aGuard( m_aMutex );
308 if ( !IsValid() )
309 throw io::NotConnectedException();
311 GetUsersData();
313 uno::Sequence< OUString > aEntry = aArgEntry;
314 if ( aEntry.getLength() != SHARED_ENTRYSIZE )
315 aEntry = GenerateOwnEntry();
317 uno::Sequence< ::uno::Sequence< OUString > > aNewData( m_aUsersData.getLength() + 1 );
319 sal_Int32 nNewInd = 0;
320 for ( sal_Int32 nInd = 0; nInd < m_aUsersData.getLength(); nInd++ )
322 if ( m_aUsersData[nInd].getLength() == SHARED_ENTRYSIZE )
324 if ( m_aUsersData[nInd][SHARED_LOCALHOST_ID] != aEntry[SHARED_LOCALHOST_ID]
325 || m_aUsersData[nInd][SHARED_SYSUSERNAME_ID] != aEntry[SHARED_SYSUSERNAME_ID]
326 || m_aUsersData[nInd][SHARED_USERURL_ID] != aEntry[SHARED_USERURL_ID] )
328 aNewData[nNewInd] = m_aUsersData[nInd];
329 nNewInd++;
334 aNewData.realloc( nNewInd );
335 SetUsersDataAndStore( aNewData );
337 if ( !nNewInd )
339 // try to remove the file if it is empty
340 RemoveFile();
344 // ----------------------------------------------------------------------
345 void ShareControlFile::RemoveFile()
347 ::osl::MutexGuard aGuard( m_aMutex );
349 if ( !IsValid() )
350 throw io::NotConnectedException();
352 Close();
354 uno::Reference<ucb::XSimpleFileAccess3> xSimpleFileAccess(ucb::SimpleFileAccess::create(comphelper::getProcessComponentContext()));
355 xSimpleFileAccess->kill( m_aURL );
358 } // namespace svt
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */