1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
21 #define __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
24 #include <threadhelp/threadhelpbase.hxx>
25 #include <threadhelp/resetableguard.hxx>
27 #include <com/sun/star/document/XActionLockable.hpp>
32 /** @short implements a guard, which can use the interface
33 <type scope="com::sun::star::document">XActionLockable</type>.
35 @descr This guard should be used to be shure, that any lock will be
36 released. Otherwhise the locaked document can hinder the office on shutdown!
38 class ActionLockGuard
: private ThreadHelpBase
40 //-------------------------------------------
45 /** @short points to the object, which can be locked from outside. */
46 css::uno::Reference
< css::document::XActionLockable
> m_xActionLock
;
48 /** @short knows if a lock exists on the internal lock object
49 forced by this guard instance. */
50 sal_Bool m_bActionLocked
;
52 //-------------------------------------------
57 //---------------------------------------
58 /** @short default ctor to initialize a "non working guard".
60 @descr That can be useful in cases, where no resource still exists,
61 but will be available next time. Then this guard can be used
62 in a mode "use guard for more then one resources".
66 , m_bActionLocked(sal_False
)
70 //---------------------------------------
71 /** @short initialize new guard instance and lock the given resource immediately.
74 points to the outside resource, which should be locked.
76 ActionLockGuard(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
78 , m_bActionLocked(sal_False
)
83 //---------------------------------------
84 /** @short release this guard instance and make shure, that no lock
85 will exist afterwards on the internal wrapped resource.
87 virtual ~ActionLockGuard()
92 //---------------------------------------
93 /** @short set a new resource for locking at this guard.
95 @descr This call will fail, if an internal resource already exists
96 and is currently locked.
99 points to the outside resource, which should be locked.
101 @return sal_True, if new resource could be set and locked.
104 virtual sal_Bool
setResource(const css::uno::Reference
< css::document::XActionLockable
>& xLock
)
106 // SAFE -> ..........................
107 ResetableGuard
aMutexLock(m_aLock
);
109 if (m_bActionLocked
|| !xLock
.is())
112 m_xActionLock
= xLock
;
113 m_xActionLock
->addActionLock();
114 m_bActionLocked
= m_xActionLock
->isActionLocked();
115 // <- SAFE ..........................
120 //---------------------------------------
121 /** @short set a new resource for locking at this guard.
123 @descr This call will fail, if an internal resource already exists
124 and is currently locked.
127 points to the outside resource, which should be locked.
129 @return sal_True, if new resource could be set and locked.
132 virtual void freeResource()
134 // SAFE -> ..........................
135 ResetableGuard
aMutexLock(m_aLock
);
137 css::uno::Reference
< css::document::XActionLockable
> xLock
= m_xActionLock
;
138 sal_Bool bLocked
= m_bActionLocked
;
140 m_xActionLock
.clear();
141 m_bActionLocked
= sal_False
;
144 // <- SAFE ..........................
146 if (bLocked
&& xLock
.is())
147 xLock
->removeActionLock();
150 //---------------------------------------
151 /** @short lock the internal wrapped resource, if its not already done. */
154 // SAFE -> ..........................
155 ResetableGuard
aMutexLock(m_aLock
);
157 if (!m_bActionLocked
&& m_xActionLock
.is())
159 m_xActionLock
->addActionLock();
160 m_bActionLocked
= m_xActionLock
->isActionLocked();
162 // <- SAFE ..........................
165 //---------------------------------------
166 /** @short unlock the internal wrapped resource, if its not already done. */
167 virtual void unlock()
169 // SAFE -> ..........................
170 ResetableGuard
aMutexLock(m_aLock
);
172 if (m_bActionLocked
&& m_xActionLock
.is())
174 m_xActionLock
->removeActionLock();
175 // dont check for any locks here ...
176 // May another guard use the same lock object :-(
177 m_bActionLocked
= sal_False
;
179 // <- SAFE ..........................
183 } // namespace framework
185 #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */