1f8e07b45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3f8e07b45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4f8e07b45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5f8e07b45SAndrew Rist  * distributed with this work for additional information
6f8e07b45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7f8e07b45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8f8e07b45SAndrew Rist  * "License"); you may not use this file except in compliance
9f8e07b45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10f8e07b45SAndrew Rist  *
11f8e07b45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12f8e07b45SAndrew Rist  *
13f8e07b45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14f8e07b45SAndrew Rist  * software distributed under the License is distributed on an
15f8e07b45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16f8e07b45SAndrew Rist  * KIND, either express or implied.  See the License for the
17f8e07b45SAndrew Rist  * specific language governing permissions and limitations
18f8e07b45SAndrew Rist  * under the License.
19f8e07b45SAndrew Rist  *
20f8e07b45SAndrew Rist  *************************************************************/
21f8e07b45SAndrew Rist 
22f8e07b45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
25cdf0e10cSrcweir #define __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_______________________________________________
28cdf0e10cSrcweir // includes of own project
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <threadhelp/threadhelpbase.hxx>
31cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //_______________________________________________
34cdf0e10cSrcweir // includes of uno interface
35cdf0e10cSrcweir #include <com/sun/star/document/XActionLockable.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //_______________________________________________
38cdf0e10cSrcweir // includes of an other project
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //_______________________________________________
41cdf0e10cSrcweir // namespace
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace framework{
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #ifndef css
46cdf0e10cSrcweir namespace css = ::com::sun::star;
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //_______________________________________________
50cdf0e10cSrcweir // definitions
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /** @short  implements a guard, which can use the interface
53cdf0e10cSrcweir             <type scope="com::sun::star::document">XActionLockable</type>.
54cdf0e10cSrcweir 
5501ad09a0Smseidel     @descr  This guard should be used to be sure, that any lock will be
56*15289133Smseidel             released. Otherwise the locked document can hinder the office on shutdown!
57cdf0e10cSrcweir */
58cdf0e10cSrcweir class ActionLockGuard : private ThreadHelpBase
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     //-------------------------------------------
61cdf0e10cSrcweir     // member
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     private:
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         /** @short  points to the object, which can be locked from outside. */
66cdf0e10cSrcweir         css::uno::Reference< css::document::XActionLockable > m_xActionLock;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         /** @short  knows if a lock exists on the internal lock object
69cdf0e10cSrcweir                     forced by this guard instance. */
70cdf0e10cSrcweir         sal_Bool m_bActionLocked;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     //-------------------------------------------
73cdf0e10cSrcweir     // interface
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     public:
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         //---------------------------------------
78cdf0e10cSrcweir         /** @short  default ctor to initialize a "non working guard".
79cdf0e10cSrcweir 
8007a3d7f1SPedro Giffuni             @descr  That can be useful in cases, where no resource still exists,
81cdf0e10cSrcweir                     but will be available next time. Then this guard can be used
82cdf0e10cSrcweir                     in a mode "use guard for more then one resources".
83cdf0e10cSrcweir          */
ActionLockGuard()84cdf0e10cSrcweir         ActionLockGuard()
85cdf0e10cSrcweir             : ThreadHelpBase (         )
86cdf0e10cSrcweir             , m_bActionLocked(sal_False)
87cdf0e10cSrcweir         {
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         //---------------------------------------
9107a3d7f1SPedro Giffuni         /** @short  initialize new guard instance and lock the given resource immediately.
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             @param  xLock
94cdf0e10cSrcweir                     points to the outside resource, which should be locked.
95cdf0e10cSrcweir          */
ActionLockGuard(const css::uno::Reference<css::document::XActionLockable> & xLock)96cdf0e10cSrcweir         ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
97cdf0e10cSrcweir             : ThreadHelpBase (         )
98cdf0e10cSrcweir             , m_bActionLocked(sal_False)
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             setResource(xLock);
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         //---------------------------------------
10401ad09a0Smseidel         /** @short  release this guard instance and make sure, that no lock
105cdf0e10cSrcweir                     will exist afterwards on the internal wrapped resource.
106cdf0e10cSrcweir          */
~ActionLockGuard()107cdf0e10cSrcweir         virtual ~ActionLockGuard()
108cdf0e10cSrcweir         {
109cdf0e10cSrcweir             unlock();
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         //---------------------------------------
113cdf0e10cSrcweir         /** @short  set a new resource for locking at this guard.
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             @descr  This call will fail, if an internal resource already exists
116cdf0e10cSrcweir                     and is currently locked.
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             @param  xLock
119cdf0e10cSrcweir                     points to the outside resource, which should be locked.
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             @return sal_True, if new resource could be set and locked.
122*15289133Smseidel                     sal_False otherwise.
123cdf0e10cSrcweir          */
setResource(const css::uno::Reference<css::document::XActionLockable> & xLock)124cdf0e10cSrcweir         virtual sal_Bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
125cdf0e10cSrcweir         {
126cdf0e10cSrcweir             // SAFE -> ..........................
127cdf0e10cSrcweir             ResetableGuard aMutexLock(m_aLock);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             if (m_bActionLocked || !xLock.is())
130cdf0e10cSrcweir                 return sal_False;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             m_xActionLock = xLock;
133cdf0e10cSrcweir             m_xActionLock->addActionLock();
134cdf0e10cSrcweir             m_bActionLocked = m_xActionLock->isActionLocked();
135cdf0e10cSrcweir             // <- SAFE ..........................
136cdf0e10cSrcweir 
137cdf0e10cSrcweir             return sal_True;
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         //---------------------------------------
141cdf0e10cSrcweir         /** @short  set a new resource for locking at this guard.
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             @descr  This call will fail, if an internal resource already exists
144cdf0e10cSrcweir                     and is currently locked.
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             @param  xLock
147cdf0e10cSrcweir                     points to the outside resource, which should be locked.
148cdf0e10cSrcweir 
149cdf0e10cSrcweir             @return sal_True, if new resource could be set and locked.
150*15289133Smseidel                     sal_False otherwise.
151cdf0e10cSrcweir          */
freeResource()152cdf0e10cSrcweir         virtual void freeResource()
153cdf0e10cSrcweir         {
154cdf0e10cSrcweir             // SAFE -> ..........................
155cdf0e10cSrcweir             ResetableGuard aMutexLock(m_aLock);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             css::uno::Reference< css::document::XActionLockable > xLock   = m_xActionLock  ;
158cdf0e10cSrcweir             sal_Bool                                              bLocked = m_bActionLocked;
159cdf0e10cSrcweir 
160cdf0e10cSrcweir             m_xActionLock.clear();
161cdf0e10cSrcweir             m_bActionLocked = sal_False;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             aMutexLock.unlock();
164cdf0e10cSrcweir             // <- SAFE ..........................
165cdf0e10cSrcweir 
166cdf0e10cSrcweir             if (bLocked && xLock.is())
167cdf0e10cSrcweir                 xLock->removeActionLock();
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         //---------------------------------------
171cdf0e10cSrcweir         /** @short  lock the internal wrapped resource, if its not already done. */
lock()172cdf0e10cSrcweir         virtual void lock()
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             // SAFE -> ..........................
175cdf0e10cSrcweir             ResetableGuard aMutexLock(m_aLock);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             if (!m_bActionLocked && m_xActionLock.is())
178cdf0e10cSrcweir             {
179cdf0e10cSrcweir                 m_xActionLock->addActionLock();
180cdf0e10cSrcweir                 m_bActionLocked = m_xActionLock->isActionLocked();
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir             // <- SAFE ..........................
183cdf0e10cSrcweir         }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir         //---------------------------------------
186cdf0e10cSrcweir         /** @short  unlock the internal wrapped resource, if its not already done. */
unlock()187cdf0e10cSrcweir         virtual void unlock()
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             // SAFE -> ..........................
190cdf0e10cSrcweir             ResetableGuard aMutexLock(m_aLock);
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             if (m_bActionLocked && m_xActionLock.is())
193cdf0e10cSrcweir             {
194cdf0e10cSrcweir                 m_xActionLock->removeActionLock();
195cdf0e10cSrcweir                 // dont check for any locks here ...
196cdf0e10cSrcweir                 // May another guard use the same lock object :-(
197cdf0e10cSrcweir                 m_bActionLocked = sal_False;
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir             // <- SAFE ..........................
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir };
202cdf0e10cSrcweir 
203cdf0e10cSrcweir } // namespace framework
204cdf0e10cSrcweir 
205cdf0e10cSrcweir #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
206