1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: actionlockguard.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
32 #define __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
34 //_______________________________________________
35 // includes of own project
37 #include <threadhelp/threadhelpbase.hxx>
38 #include <threadhelp/resetableguard.hxx>
40 //_______________________________________________
41 // includes of uno interface
42 #include <com/sun/star/document/XActionLockable.hpp>
44 //_______________________________________________
45 // includes of an other project
47 //_______________________________________________
53 namespace css
= ::com::sun::star
;
56 //_______________________________________________
59 /** @short implements a guard, which can use the interface
60 <type scope="com::sun::star::document">XActionLockable</type>.
62 @descr This guard should be used to be shure, that any lock will be
63 released. Otherwhise the locaked document can hinder the office on shutdown!
65 class ActionLockGuard
: private ThreadHelpBase
67 //-------------------------------------------
72 /** @short points to the object, which can be locked from outside. */
73 css::uno::Reference
< css::document::XActionLockable
> m_xActionLock
;
75 /** @short knows if a lock exists on the internal lock object
76 forced by this guard instance. */
77 sal_Bool m_bActionLocked
;
79 //-------------------------------------------
84 //---------------------------------------
85 /** @short default ctor to initialize a "non working guard".
87 @descr That can be usefull in cases, where no resource still exists,
88 but will be available next time. Then this guard can be used
89 in a mode "use guard for more then one resources".
93 , m_bActionLocked(sal_False
)
97 //---------------------------------------
98 /** @short initialize new guard instance and lock the given resource immediatly.
101 points to the outside resource, which should be locked.
103 ActionLockGuard(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
105 , m_bActionLocked(sal_False
)
110 //---------------------------------------
111 /** @short release this guard instance and make shure, that no lock
112 will exist afterwards on the internal wrapped resource.
114 virtual ~ActionLockGuard()
119 //---------------------------------------
120 /** @short set a new resource for locking at this guard.
122 @descr This call will fail, if an internal resource already exists
123 and is currently locked.
126 points to the outside resource, which should be locked.
128 @return TRUE, if new resource could be set and locked.
131 virtual sal_Bool
setResource(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
133 // SAFE -> ..........................
134 ResetableGuard
aMutexLock(m_aLock
);
136 if (m_bActionLocked
|| !xLock
.is())
139 m_xActionLock
= xLock
;
140 m_xActionLock
->addActionLock();
141 m_bActionLocked
= m_xActionLock
->isActionLocked();
142 // <- SAFE ..........................
147 //---------------------------------------
148 /** @short set a new resource for locking at this guard.
150 @descr This call will fail, if an internal resource already exists
151 and is currently locked.
154 points to the outside resource, which should be locked.
156 @return TRUE, if new resource could be set and locked.
159 virtual void freeResource()
161 // SAFE -> ..........................
162 ResetableGuard
aMutexLock(m_aLock
);
164 css::uno::Reference
< css::document::XActionLockable
> xLock
= m_xActionLock
;
165 sal_Bool bLocked
= m_bActionLocked
;
167 m_xActionLock
.clear();
168 m_bActionLocked
= sal_False
;
171 // <- SAFE ..........................
173 if (bLocked
&& xLock
.is())
174 xLock
->removeActionLock();
177 //---------------------------------------
178 /** @short lock the internal wrapped resource, if its not already done. */
181 // SAFE -> ..........................
182 ResetableGuard
aMutexLock(m_aLock
);
184 if (!m_bActionLocked
&& m_xActionLock
.is())
186 m_xActionLock
->addActionLock();
187 m_bActionLocked
= m_xActionLock
->isActionLocked();
189 // <- SAFE ..........................
192 //---------------------------------------
193 /** @short unlock the internal wrapped resource, if its not already done. */
194 virtual void unlock()
196 // SAFE -> ..........................
197 ResetableGuard
aMutexLock(m_aLock
);
199 if (m_bActionLocked
&& m_xActionLock
.is())
201 m_xActionLock
->removeActionLock();
202 // dont check for any locks here ...
203 // May another guard use the same lock object :-(
204 m_bActionLocked
= sal_False
;
206 // <- SAFE ..........................
210 } // namespace framework
212 #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_