1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_TDOC_STGELEMS_HXX
25 #define INCLUDED_TDOC_STGELEMS_HXX
26 
27 #include <memory>
28 
29 #include "osl/mutex.hxx"
30 #include "rtl/ref.hxx"
31 
32 #include "cppuhelper/implbase2.hxx"
33 #include "cppuhelper/implbase5.hxx"
34 
35 #include "com/sun/star/embed/XStorage.hpp"
36 #include "com/sun/star/embed/XTransactedObject.hpp"
37 #include "com/sun/star/io/XOutputStream.hpp"
38 #include "com/sun/star/io/XStream.hpp"
39 #include "com/sun/star/io/XTruncate.hpp"
40 #include "com/sun/star/lang/XComponent.hpp"
41 
42 #include "tdoc_storage.hxx"
43 
44 namespace tdoc_ucp {
45 
46 struct MutexHolder
47 {
48     osl::Mutex m_aMutex;
49 };
50 
51 //=======================================================================
52 
53 class ParentStorageHolder : public MutexHolder
54 {
55 public:
56     ParentStorageHolder(
57         const com::sun::star::uno::Reference<
58             com::sun::star::embed::XStorage > & xParentStorage,
59         const rtl::OUString & rUri );
60 
isParentARootStorage() const61     bool isParentARootStorage() const
62     { return m_bParentIsRootStorage; }
63     com::sun::star::uno::Reference< com::sun::star::embed::XStorage >
getParentStorage() const64     getParentStorage() const
65     { return m_xParentStorage; }
setParentStorage(const com::sun::star::uno::Reference<com::sun::star::embed::XStorage> & xStg)66     void setParentStorage( const com::sun::star::uno::Reference<
67                             com::sun::star::embed::XStorage > & xStg )
68     { osl::MutexGuard aGuard( m_aMutex ); m_xParentStorage = xStg; }
69 
70 private:
71     com::sun::star::uno::Reference<
72         com::sun::star::embed::XStorage > m_xParentStorage;
73     bool                                  m_bParentIsRootStorage;
74 };
75 
76 //=======================================================================
77 
78 typedef
79     cppu::WeakImplHelper2<
80         com::sun::star::embed::XStorage,
81         com::sun::star::embed::XTransactedObject > StorageUNOBase;
82 
83 class Storage : public StorageUNOBase, public ParentStorageHolder
84 {
85 public:
86     Storage(
87         const com::sun::star::uno::Reference<
88             com::sun::star::lang::XMultiServiceFactory > & xSMgr,
89         const rtl::Reference< StorageElementFactory >  & xFactory,
90         const rtl::OUString & rUri,
91         const com::sun::star::uno::Reference<
92             com::sun::star::embed::XStorage > & xParentStorage,
93         const com::sun::star::uno::Reference<
94             com::sun::star::embed::XStorage > & xStorageToWrap );
95     virtual ~Storage();
96 
isDocumentStorage() const97     bool isDocumentStorage() const { return m_bIsDocumentStorage; }
98 
99     // XInterface
100     virtual com::sun::star::uno::Any SAL_CALL queryInterface(
101             const com::sun::star::uno::Type& aType )
102         throw ( com::sun::star::uno::RuntimeException );
103     virtual void SAL_CALL acquire()
104         throw ();
105     virtual void SAL_CALL release()
106         throw ();
107 
108     // XTypeProvider (implemnented by base, but needs to be overridden for
109     //                delegating to aggregate)
110     virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL
111     getTypes()
112         throw ( com::sun::star::uno::RuntimeException );
113     virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
114     getImplementationId()
115         throw ( com::sun::star::uno::RuntimeException );
116 
117     // XComponent ( one of XStorage bases )
118     virtual void SAL_CALL
119     dispose()
120         throw ( com::sun::star::uno::RuntimeException );
121     virtual void SAL_CALL
122     addEventListener( const com::sun::star::uno::Reference<
123                         com::sun::star::lang::XEventListener > & xListener )
124         throw ( com::sun::star::uno::RuntimeException );
125     virtual void SAL_CALL
126     removeEventListener( const com::sun::star::uno::Reference<
127                             com::sun::star::lang::XEventListener >& aListener )
128         throw ( com::sun::star::uno::RuntimeException );
129 
130     // XNameAccess ( one of XStorage bases )
131     virtual com::sun::star::uno::Any SAL_CALL
132     getByName( const rtl::OUString& aName )
133         throw ( com::sun::star::container::NoSuchElementException,
134                 com::sun::star::lang::WrappedTargetException,
135                 com::sun::star::uno::RuntimeException );
136     virtual com::sun::star::uno::Sequence< rtl::OUString > SAL_CALL
137     getElementNames()
138         throw ( com::sun::star::uno::RuntimeException );
139     virtual sal_Bool SAL_CALL
140     hasByName( const rtl::OUString& aName )
141         throw ( com::sun::star::uno::RuntimeException );
142 
143     // XElementAccess (base of XNameAccess)
144     virtual com::sun::star::uno::Type SAL_CALL
145     getElementType()
146         throw ( com::sun::star::uno::RuntimeException );
147     virtual sal_Bool SAL_CALL
148     hasElements()
149         throw ( com::sun::star::uno::RuntimeException );
150 
151     // XStorage
152     virtual void SAL_CALL
153     copyToStorage( const com::sun::star::uno::Reference<
154                     com::sun::star::embed::XStorage >& xDest )
155         throw ( com::sun::star::embed::InvalidStorageException,
156                 com::sun::star::lang::IllegalArgumentException,
157                 com::sun::star::io::IOException,
158                 com::sun::star::embed::StorageWrappedTargetException,
159                 com::sun::star::uno::RuntimeException );
160     virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL
161     openStreamElement( const ::rtl::OUString& aStreamName,
162                        sal_Int32 nOpenMode )
163         throw ( com::sun::star::embed::InvalidStorageException,
164                 com::sun::star::lang::IllegalArgumentException,
165                 com::sun::star::packages::WrongPasswordException,
166                 com::sun::star::io::IOException,
167                 com::sun::star::embed::StorageWrappedTargetException,
168                 com::sun::star::uno::RuntimeException );
169     virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL
170     openEncryptedStreamElement( const ::rtl::OUString& aStreamName,
171                                 sal_Int32 nOpenMode,
172                                 const ::rtl::OUString& aPassword )
173         throw ( com::sun::star::embed::InvalidStorageException,
174                 com::sun::star::lang::IllegalArgumentException,
175                 com::sun::star::packages::NoEncryptionException,
176                 com::sun::star::packages::WrongPasswordException,
177                 com::sun::star::io::IOException,
178                 com::sun::star::embed::StorageWrappedTargetException,
179                 com::sun::star::uno::RuntimeException );
180     virtual com::sun::star::uno::Reference< com::sun::star::embed::XStorage > SAL_CALL
181     openStorageElement( const ::rtl::OUString& aStorName,
182                         sal_Int32 nOpenMode )
183         throw ( com::sun::star::embed::InvalidStorageException,
184                 com::sun::star::lang::IllegalArgumentException,
185                 com::sun::star::io::IOException,
186                 com::sun::star::embed::StorageWrappedTargetException,
187                 com::sun::star::uno::RuntimeException );
188     virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL
189     cloneStreamElement( const ::rtl::OUString& aStreamName )
190         throw ( com::sun::star::embed::InvalidStorageException,
191                 com::sun::star::lang::IllegalArgumentException,
192                 com::sun::star::packages::WrongPasswordException,
193                 com::sun::star::io::IOException,
194                 com::sun::star::embed::StorageWrappedTargetException,
195                 com::sun::star::uno::RuntimeException );
196     virtual com::sun::star::uno::Reference< com::sun::star::io::XStream > SAL_CALL
197     cloneEncryptedStreamElement( const ::rtl::OUString& aStreamName,
198                                  const ::rtl::OUString& aPassword )
199         throw ( com::sun::star::embed::InvalidStorageException,
200                 com::sun::star::lang::IllegalArgumentException,
201                 com::sun::star::packages::NoEncryptionException,
202                 com::sun::star::packages::WrongPasswordException,
203                 com::sun::star::io::IOException,
204                 com::sun::star::embed::StorageWrappedTargetException,
205                 com::sun::star::uno::RuntimeException );
206     virtual void SAL_CALL
207     copyLastCommitTo( const com::sun::star::uno::Reference<
208                         com::sun::star::embed::XStorage >& xTargetStorage )
209         throw ( com::sun::star::embed::InvalidStorageException,
210                 com::sun::star::lang::IllegalArgumentException,
211                 com::sun::star::io::IOException,
212                 com::sun::star::embed::StorageWrappedTargetException,
213                 com::sun::star::uno::RuntimeException );
214     virtual void SAL_CALL
215     copyStorageElementLastCommitTo( const ::rtl::OUString& aStorName,
216                                     const com::sun::star::uno::Reference<
217                                         com::sun::star::embed::XStorage > &
218                                             xTargetStorage )
219         throw ( com::sun::star::embed::InvalidStorageException,
220                 com::sun::star::lang::IllegalArgumentException,
221                 com::sun::star::io::IOException,
222                 com::sun::star::embed::StorageWrappedTargetException,
223                 com::sun::star::uno::RuntimeException );
224     virtual sal_Bool SAL_CALL
225     isStreamElement( const ::rtl::OUString& aElementName )
226         throw ( com::sun::star::container::NoSuchElementException,
227                 com::sun::star::lang::IllegalArgumentException,
228                 com::sun::star::embed::InvalidStorageException,
229                 com::sun::star::uno::RuntimeException );
230     virtual sal_Bool SAL_CALL
231     isStorageElement( const ::rtl::OUString& aElementName )
232         throw ( com::sun::star::container::NoSuchElementException,
233                 com::sun::star::lang::IllegalArgumentException,
234                 com::sun::star::embed::InvalidStorageException,
235                 com::sun::star::uno::RuntimeException );
236     virtual void SAL_CALL
237     removeElement( const ::rtl::OUString& aElementName )
238         throw ( com::sun::star::embed::InvalidStorageException,
239                 com::sun::star::lang::IllegalArgumentException,
240                 com::sun::star::container::NoSuchElementException,
241                 com::sun::star::io::IOException,
242                 com::sun::star::embed::StorageWrappedTargetException,
243                 com::sun::star::uno::RuntimeException );
244     virtual void SAL_CALL
245     renameElement( const ::rtl::OUString& aEleName,
246                    const ::rtl::OUString& aNewName )
247         throw ( com::sun::star::embed::InvalidStorageException,
248                 com::sun::star::lang::IllegalArgumentException,
249                 com::sun::star::container::NoSuchElementException,
250                 com::sun::star::container::ElementExistException,
251                 com::sun::star::io::IOException,
252                 com::sun::star::embed::StorageWrappedTargetException,
253                 com::sun::star::uno::RuntimeException );
254     virtual void SAL_CALL
255     copyElementTo( const ::rtl::OUString& aElementName,
256                    const com::sun::star::uno::Reference<
257                     com::sun::star::embed::XStorage >& xDest,
258                    const ::rtl::OUString& aNewName )
259         throw ( com::sun::star::embed::InvalidStorageException,
260                 com::sun::star::lang::IllegalArgumentException,
261                 com::sun::star::container::NoSuchElementException,
262                 com::sun::star::container::ElementExistException,
263                 com::sun::star::io::IOException,
264                 com::sun::star::embed::StorageWrappedTargetException,
265                 com::sun::star::uno::RuntimeException );
266     virtual void SAL_CALL
267     moveElementTo( const ::rtl::OUString& aElementName,
268                    const com::sun::star::uno::Reference<
269                     com::sun::star::embed::XStorage >& xDest,
270                    const ::rtl::OUString& rNewName )
271         throw ( com::sun::star::embed::InvalidStorageException,
272                 com::sun::star::lang::IllegalArgumentException,
273                 com::sun::star::container::NoSuchElementException,
274                 com::sun::star::container::ElementExistException,
275                 com::sun::star::io::IOException,
276                 com::sun::star::embed::StorageWrappedTargetException,
277                 com::sun::star::uno::RuntimeException );
278 
279     // XTransactedObject
280     virtual void SAL_CALL commit()
281         throw ( com::sun::star::io::IOException,
282                 com::sun::star::lang::WrappedTargetException,
283                 com::sun::star::uno::RuntimeException );
284     virtual void SAL_CALL revert()
285         throw ( com::sun::star::io::IOException,
286                 com::sun::star::lang::WrappedTargetException,
287                 com::sun::star::uno::RuntimeException );
288 
289 private:
290     Storage( const rtl::Reference< Storage > & rFactory ); // n.i.
291 
292     rtl::Reference< StorageElementFactory >         m_xFactory;
293     com::sun::star::uno::Reference<
294         com::sun::star::uno::XAggregation >         m_xAggProxy;
295     com::sun::star::uno::Reference<
296         com::sun::star::embed::XStorage >           m_xWrappedStorage;
297     com::sun::star::uno::Reference<
298         com::sun::star::embed::XTransactedObject >  m_xWrappedTransObj;
299     com::sun::star::uno::Reference<
300         com::sun::star::lang::XComponent >          m_xWrappedComponent;
301     com::sun::star::uno::Reference<
302         com::sun::star::lang::XTypeProvider >       m_xWrappedTypeProv;
303     bool                                            m_bIsDocumentStorage;
304 
305     StorageElementFactory::StorageMap::iterator m_aContainerIt;
306 
307     friend class StorageElementFactory;
308     friend class std::auto_ptr< Storage >;
309 };
310 
311 //=======================================================================
312 
313 typedef
314     cppu::WeakImplHelper2<
315         com::sun::star::io::XOutputStream,
316         com::sun::star::lang::XComponent > OutputStreamUNOBase;
317 
318 class OutputStream : public OutputStreamUNOBase, public ParentStorageHolder
319 {
320 public:
321     OutputStream(
322         const com::sun::star::uno::Reference<
323             com::sun::star::lang::XMultiServiceFactory > & xSMgr,
324         const rtl::OUString & rUri,
325         const com::sun::star::uno::Reference<
326             com::sun::star::embed::XStorage >  & xParentStorage,
327         const com::sun::star::uno::Reference<
328             com::sun::star::io::XOutputStream > & xStreamToWrap );
329     virtual ~OutputStream();
330 
331     // XInterface
332     virtual com::sun::star::uno::Any SAL_CALL
333     queryInterface( const com::sun::star::uno::Type& aType )
334         throw ( com::sun::star::uno::RuntimeException );
335 
336     // XTypeProvider (implemnented by base, but needs to be overridden for
337     //                delegating to aggregate)
338     virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL
339     getTypes()
340         throw ( com::sun::star::uno::RuntimeException );
341     virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
342     getImplementationId()
343         throw ( com::sun::star::uno::RuntimeException );
344 
345     // XOutputStream
346     virtual void SAL_CALL
347     writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
348         throw ( com::sun::star::io::NotConnectedException,
349                 com::sun::star::io::BufferSizeExceededException,
350                 com::sun::star::io::IOException,
351                 com::sun::star::uno::RuntimeException );
352     virtual void SAL_CALL
353     flush(  )
354         throw ( com::sun::star::io::NotConnectedException,
355                 com::sun::star::io::BufferSizeExceededException,
356                 com::sun::star::io::IOException,
357                 com::sun::star::uno::RuntimeException );
358     // Note: We need to intercept this one.
359     virtual void SAL_CALL
360     closeOutput(  )
361         throw ( com::sun::star::io::NotConnectedException,
362                 com::sun::star::io::BufferSizeExceededException,
363                 com::sun::star::io::IOException,
364                 com::sun::star::uno::RuntimeException );
365 
366     // XComponent
367     // Note: We need to intercept this one.
368     virtual void SAL_CALL
369     dispose()
370         throw ( com::sun::star::uno::RuntimeException );
371     virtual void SAL_CALL
372     addEventListener( const com::sun::star::uno::Reference<
373                         com::sun::star::lang::XEventListener >& xListener )
374         throw ( com::sun::star::uno::RuntimeException );
375     virtual void SAL_CALL
376     removeEventListener( const com::sun::star::uno::Reference<
377                             com::sun::star::lang::XEventListener >& aListener )
378         throw ( com::sun::star::uno::RuntimeException );
379 
380 private:
381     com::sun::star::uno::Reference<
382         com::sun::star::uno::XAggregation >     m_xAggProxy;
383     com::sun::star::uno::Reference<
384         com::sun::star::io::XOutputStream >     m_xWrappedStream;
385     com::sun::star::uno::Reference<
386         com::sun::star::lang::XComponent >      m_xWrappedComponent;
387     com::sun::star::uno::Reference<
388         com::sun::star::lang::XTypeProvider >   m_xWrappedTypeProv;
389 };
390 
391 //=======================================================================
392 
393 typedef cppu::WeakImplHelper5< com::sun::star::io::XStream,
394                                com::sun::star::io::XOutputStream,
395                                com::sun::star::io::XTruncate,
396                                com::sun::star::io::XInputStream,
397                                com::sun::star::lang::XComponent >
398         StreamUNOBase;
399 
400 class Stream : public StreamUNOBase, public ParentStorageHolder
401 {
402 public:
403     Stream(
404         const com::sun::star::uno::Reference<
405             com::sun::star::lang::XMultiServiceFactory > & xSMgr,
406         const rtl::OUString & rUri,
407         const com::sun::star::uno::Reference<
408             com::sun::star::embed::XStorage >  & xParentStorage,
409         const com::sun::star::uno::Reference<
410             com::sun::star::io::XStream > & xStreamToWrap );
411 
412     virtual ~Stream();
413 
414     // XInterface
415     virtual com::sun::star::uno::Any SAL_CALL
416     queryInterface( const com::sun::star::uno::Type& aType )
417         throw ( com::sun::star::uno::RuntimeException );
418 
419     // XTypeProvider (implemnented by base, but needs to be overridden for
420     //                delegating to aggregate)
421     virtual com::sun::star::uno::Sequence< com::sun::star::uno::Type > SAL_CALL
422     getTypes()
423         throw ( com::sun::star::uno::RuntimeException );
424     virtual com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL
425     getImplementationId()
426         throw ( com::sun::star::uno::RuntimeException );
427 
428     // XStream
429     virtual com::sun::star::uno::Reference<
430         com::sun::star::io::XInputStream > SAL_CALL
431     getInputStream()
432         throw( com::sun::star::uno::RuntimeException );
433 
434     virtual com::sun::star::uno::Reference<
435         com::sun::star::io::XOutputStream > SAL_CALL
436     getOutputStream()
437         throw( com::sun::star::uno::RuntimeException );
438 
439     // XOutputStream
440     virtual void SAL_CALL
441     writeBytes( const com::sun::star::uno::Sequence< sal_Int8 >& aData )
442         throw( com::sun::star::io::NotConnectedException,
443                com::sun::star::io::BufferSizeExceededException,
444                com::sun::star::io::IOException,
445                com::sun::star::uno::RuntimeException );
446 
447     virtual void SAL_CALL
448     flush()
449         throw( com::sun::star::io::NotConnectedException,
450                com::sun::star::io::BufferSizeExceededException,
451                com::sun::star::io::IOException,
452                com::sun::star::uno::RuntimeException );
453 
454     virtual void SAL_CALL
455     closeOutput()
456         throw( com::sun::star::io::NotConnectedException,
457                com::sun::star::io::IOException,
458                com::sun::star::uno::RuntimeException );
459 
460     // XTruncate
461     virtual void SAL_CALL
462     truncate()
463         throw( com::sun::star::io::IOException,
464                com::sun::star::uno::RuntimeException );
465 
466     // XInputStream
467     virtual sal_Int32 SAL_CALL
468     readBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
469                sal_Int32 nBytesToRead )
470         throw( com::sun::star::io::NotConnectedException,
471                com::sun::star::io::BufferSizeExceededException,
472                com::sun::star::io::IOException,
473                com::sun::star::uno::RuntimeException );
474 
475     virtual sal_Int32 SAL_CALL
476     readSomeBytes( com::sun::star::uno::Sequence< sal_Int8 >& aData,
477                    sal_Int32 nMaxBytesToRead )
478         throw( com::sun::star::io::NotConnectedException,
479                com::sun::star::io::BufferSizeExceededException,
480                com::sun::star::io::IOException,
481                com::sun::star::uno::RuntimeException);
482 
483     virtual void SAL_CALL
484     skipBytes( sal_Int32 nBytesToSkip )
485         throw( com::sun::star::io::NotConnectedException,
486                com::sun::star::io::BufferSizeExceededException,
487                com::sun::star::io::IOException,
488                com::sun::star::uno::RuntimeException );
489 
490     virtual sal_Int32 SAL_CALL
491     available()
492         throw( com::sun::star::io::NotConnectedException,
493                com::sun::star::io::IOException,
494                com::sun::star::uno::RuntimeException );
495 
496     virtual void SAL_CALL
497     closeInput()
498         throw( com::sun::star::io::NotConnectedException,
499                com::sun::star::io::IOException,
500                com::sun::star::uno::RuntimeException );
501 
502     // XComponent
503     // Note: We need to intercept this one.
504     virtual void SAL_CALL
505     dispose()
506         throw ( com::sun::star::uno::RuntimeException );
507     virtual void SAL_CALL
508     addEventListener( const com::sun::star::uno::Reference<
509             com::sun::star::lang::XEventListener >& xListener )
510         throw ( com::sun::star::uno::RuntimeException );
511     virtual void SAL_CALL
512     removeEventListener( const com::sun::star::uno::Reference<
513             com::sun::star::lang::XEventListener >& aListener )
514         throw ( com::sun::star::uno::RuntimeException );
515 
516 private:
517     void commitChanges()
518         throw( com::sun::star::io::IOException );
519 
520     com::sun::star::uno::Reference<
521         com::sun::star::uno::XAggregation >     m_xAggProxy;
522     com::sun::star::uno::Reference<
523         com::sun::star::io::XStream >           m_xWrappedStream;
524     com::sun::star::uno::Reference<
525         com::sun::star::io::XOutputStream >     m_xWrappedOutputStream;
526     com::sun::star::uno::Reference<
527         com::sun::star::io::XTruncate >         m_xWrappedTruncate;
528     com::sun::star::uno::Reference<
529         com::sun::star::io::XInputStream >      m_xWrappedInputStream;
530     com::sun::star::uno::Reference<
531         com::sun::star::lang::XComponent >      m_xWrappedComponent;
532     com::sun::star::uno::Reference<
533         com::sun::star::lang::XTypeProvider >   m_xWrappedTypeProv;
534 };
535 
536 } // namespace tdoc_ucp
537 
538 #endif /* !INCLUDED_TDOC_STGELEMS_HXX */
539