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