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 _OHIERARCHYHOLDER_HXX_
29 #define _OHIERARCHYHOLDER_HXX_
30 
31 #include <com/sun/star/embed/XStorage.hpp>
32 #include <com/sun/star/embed/XTransactionListener.hpp>
33 #include <com/sun/star/embed/XExtendedStorageStream.hpp>
34 #include <cppuhelper/implbase1.hxx>
35 
36 #include <comphelper/sequenceashashmap.hxx>
37 
38 #include <rtl/ref.hxx>
39 
40 #include <hash_map>
41 #include <list>
42 #include <vector>
43 
44 struct OHierarchyElement_Impl;
45 
46 struct eqFunc
47 {
48 	sal_Bool operator()( const rtl::OUString &r1,
49 				    	 const rtl::OUString &r2) const
50 	{
51 		return r1 == r2;
52 	}
53 };
54 typedef ::std::hash_map< ::rtl::OUString,
55 						 ::rtl::Reference< OHierarchyElement_Impl >,
56 						 ::rtl::OUStringHash,
57 						 eqFunc > OHierarchyElementList_Impl;
58 
59 typedef ::std::vector< ::rtl::OUString > OStringList_Impl;
60 typedef ::std::list< ::com::sun::star::uno::WeakReference< ::com::sun::star::embed::XExtendedStorageStream > >
61 						OWeakStorRefList_Impl;
62 
63 struct OHierarchyElement_Impl : public cppu::WeakImplHelper1< ::com::sun::star::embed::XTransactionListener >
64 {
65 	::osl::Mutex m_aMutex;
66 
67 	::rtl::Reference< OHierarchyElement_Impl > m_rParent;
68 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage > m_xOwnStorage;
69 	::com::sun::star::uno::WeakReference< ::com::sun::star::embed::XStorage > m_xWeakOwnStorage;
70 
71 	OHierarchyElementList_Impl m_aChildren;
72 
73 	OWeakStorRefList_Impl m_aOpenStreams;
74 
75 public:
76 	OHierarchyElement_Impl( OHierarchyElement_Impl* pParent, const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xStorage )
77 	: m_rParent( pParent )
78 	, m_xOwnStorage( xStorage )
79 	{}
80 
81 	OHierarchyElement_Impl( const ::com::sun::star::uno::WeakReference< ::com::sun::star::embed::XStorage >& xWeakStorage )
82 	: m_rParent( NULL )
83 	, m_xWeakOwnStorage( xWeakStorage )
84 	{}
85 
86 	void Commit();
87 
88 	void SetParent( const ::rtl::Reference< OHierarchyElement_Impl >& rParent ) { m_rParent = rParent; }
89 
90 	void TestForClosing();
91 
92 	void RemoveElement( const ::rtl::Reference< OHierarchyElement_Impl >& aRef );
93 
94 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XExtendedStorageStream >
95         GetStreamHierarchically( sal_Int32 nStorageMode,
96                                 OStringList_Impl& aPath,
97                                 sal_Int32 nStreamMode,
98                                 const ::comphelper::SequenceAsHashMap& aEncryptionData = ::comphelper::SequenceAsHashMap() );
99 
100 	void RemoveStreamHierarchically( OStringList_Impl& aListPath );
101 
102 	// XEventListener
103     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
104 		throw (::com::sun::star::uno::RuntimeException);
105 
106 
107 	// XTransactionListener
108     virtual void SAL_CALL preCommit( const ::com::sun::star::lang::EventObject& aEvent )
109 		throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
110     virtual void SAL_CALL commited( const ::com::sun::star::lang::EventObject& aEvent )
111 		throw (::com::sun::star::uno::RuntimeException);
112     virtual void SAL_CALL preRevert( const ::com::sun::star::lang::EventObject& aEvent )
113 		throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
114     virtual void SAL_CALL reverted( const ::com::sun::star::lang::EventObject& aEvent )
115 		throw (::com::sun::star::uno::RuntimeException);
116 
117 };
118 
119 class OHierarchyHolder_Impl : public ::cppu::OWeakObject
120 {
121 	::com::sun::star::uno::WeakReference< ::com::sun::star::embed::XStorage > m_xWeakOwnStorage;
122 	::rtl::Reference< OHierarchyElement_Impl > m_xChild;
123 public:
124 	OHierarchyHolder_Impl( const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& xOwnStorage )
125 	: m_xWeakOwnStorage( xOwnStorage )
126 	, m_xChild( new OHierarchyElement_Impl( ::com::sun::star::uno::WeakReference< ::com::sun::star::embed::XStorage >( xOwnStorage ) ) )
127 	{}
128 
129 	static OStringList_Impl GetListPathFromString( const ::rtl::OUString& aPath );
130 
131 	::com::sun::star::uno::Reference< ::com::sun::star::embed::XExtendedStorageStream >
132         GetStreamHierarchically( sal_Int32 nStorageMode,
133                                 OStringList_Impl& aListPath,
134                                 sal_Int32 nStreamMode,
135                                 const ::comphelper::SequenceAsHashMap& aEncryptionData = ::comphelper::SequenceAsHashMap() );
136 
137 	void RemoveStreamHierarchically( OStringList_Impl& aListPath );
138 };
139 
140 #endif // _OHIERARCHYHOLDER
141 
142