1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
;
54 // ----------------------------------------------------------------------
55 ShareControlFile::ShareControlFile( const OUString
& aOrigURL
)
56 : LockFileCommon( aOrigURL
, OUString( ".~sharing." ) )
61 throw io::NotConnectedException();
64 // ----------------------------------------------------------------------
65 ShareControlFile::~ShareControlFile()
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?
97 xStream
= aContent
.openWriteableStreamNoLock();
99 catch ( ucb::InteractiveIOException
const & e
)
101 if ( e
.Code
== ucb::IOErrorCode_NOT_EXISTING
)
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
113 aContent
.setPropertyValue( OUString( "IsHidden" ), uno::makeAny( sal_True
) );
114 } catch( uno::Exception
& ) {}
116 // Try to open one more time
117 xStream
= aContent
.openWriteableStreamNoLock();
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
);
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
);
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
);
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
];
188 m_aUsersData
= ParseList( aBuffer
);
194 // ----------------------------------------------------------------------
195 void ShareControlFile::SetUsersDataAndStore( const uno::Sequence
< uno::Sequence
< OUString
> >& aUsersData
)
197 ::osl::MutexGuard
aGuard( m_aMutex
);
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
)',' );
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
);
236 throw io::NotConnectedException();
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
] )
254 aNewData
[nNewInd
] = aNewEntry
;
260 aNewData
[nNewInd
] = m_aUsersData
[nInd
];
268 aNewData
[nNewInd
++] = aNewEntry
;
270 aNewData
.realloc( nNewInd
);
271 SetUsersDataAndStore( aNewData
);
276 // ----------------------------------------------------------------------
277 bool ShareControlFile::HasOwnEntry()
279 ::osl::MutexGuard
aGuard( m_aMutex
);
283 throw io::NotConnectedException();
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
] )
303 // ----------------------------------------------------------------------
304 void ShareControlFile::RemoveEntry( const uno::Sequence
< OUString
>& aArgEntry
)
306 ::osl::MutexGuard
aGuard( m_aMutex
);
309 throw io::NotConnectedException();
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
];
334 aNewData
.realloc( nNewInd
);
335 SetUsersDataAndStore( aNewData
);
339 // try to remove the file if it is empty
344 // ----------------------------------------------------------------------
345 void ShareControlFile::RemoveFile()
347 ::osl::MutexGuard
aGuard( m_aMutex
);
350 throw io::NotConnectedException();
354 uno::Reference
<ucb::XSimpleFileAccess3
> xSimpleFileAccess(ucb::SimpleFileAccess::create(comphelper::getProcessComponentContext()));
355 xSimpleFileAccess
->kill( m_aURL
);
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */