Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #ifndef __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
21 : : #define __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
22 : :
23 : :
24 : : #include <threadhelp/threadhelpbase.hxx>
25 : : #include <threadhelp/resetableguard.hxx>
26 : :
27 : : #include <com/sun/star/document/XActionLockable.hpp>
28 : :
29 : :
30 : : namespace framework{
31 : :
32 : : #ifndef css
33 : : namespace css = ::com::sun::star;
34 : : #endif
35 : :
36 : :
37 : : /** @short implements a guard, which can use the interface
38 : : <type scope="com::sun::star::document">XActionLockable</type>.
39 : :
40 : : @descr This guard should be used to be shure, that any lock will be
41 : : released. Otherwhise the locaked document can hinder the office on shutdown!
42 : : */
43 : : class ActionLockGuard : private ThreadHelpBase
44 : : {
45 : : //-------------------------------------------
46 : : // member
47 : :
48 : : private:
49 : :
50 : : /** @short points to the object, which can be locked from outside. */
51 : : css::uno::Reference< css::document::XActionLockable > m_xActionLock;
52 : :
53 : : /** @short knows if a lock exists on the internal lock object
54 : : forced by this guard instance. */
55 : : sal_Bool m_bActionLocked;
56 : :
57 : : //-------------------------------------------
58 : : // interface
59 : :
60 : : public:
61 : :
62 : : //---------------------------------------
63 : : /** @short default ctor to initialize a "non working guard".
64 : :
65 : : @descr That can be usefull in cases, where no resource still exists,
66 : : but will be available next time. Then this guard can be used
67 : : in a mode "use guard for more then one resources".
68 : : */
69 : 1763 : ActionLockGuard()
70 : : : ThreadHelpBase ( )
71 : 1763 : , m_bActionLocked(sal_False)
72 : : {
73 : 1763 : }
74 : :
75 : : //---------------------------------------
76 : : /** @short initialize new guard instance and lock the given resource immediatly.
77 : :
78 : : @param xLock
79 : : points to the outside resource, which should be locked.
80 : : */
81 : : ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
82 : : : ThreadHelpBase ( )
83 : : , m_bActionLocked(sal_False)
84 : : {
85 : : setResource(xLock);
86 : : }
87 : :
88 : : //---------------------------------------
89 : : /** @short release this guard instance and make shure, that no lock
90 : : will exist afterwards on the internal wrapped resource.
91 : : */
92 : 1763 : virtual ~ActionLockGuard()
93 : 1763 : {
94 [ + - ]: 1763 : unlock();
95 [ - + ]: 1763 : }
96 : :
97 : : //---------------------------------------
98 : : /** @short set a new resource for locking at this guard.
99 : :
100 : : @descr This call will fail, if an internal resource already exists
101 : : and is currently locked.
102 : :
103 : : @param xLock
104 : : points to the outside resource, which should be locked.
105 : :
106 : : @return sal_True, if new resource could be set and locked.
107 : : sal_False otherwhise.
108 : : */
109 : 1763 : virtual sal_Bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
110 : : {
111 : : // SAFE -> ..........................
112 [ + - ]: 1763 : ResetableGuard aMutexLock(m_aLock);
113 : :
114 [ + - ][ - + ]: 1763 : if (m_bActionLocked || !xLock.is())
[ - + ]
115 : 0 : return sal_False;
116 : :
117 [ + - ]: 1763 : m_xActionLock = xLock;
118 [ + - ][ + - ]: 1763 : m_xActionLock->addActionLock();
119 [ + - ][ + - ]: 1763 : m_bActionLocked = m_xActionLock->isActionLocked();
120 : : // <- SAFE ..........................
121 : :
122 [ + - ]: 1763 : return sal_True;
123 : : }
124 : :
125 : : //---------------------------------------
126 : : /** @short set a new resource for locking at this guard.
127 : :
128 : : @descr This call will fail, if an internal resource already exists
129 : : and is currently locked.
130 : :
131 : : @param xLock
132 : : points to the outside resource, which should be locked.
133 : :
134 : : @return sal_True, if new resource could be set and locked.
135 : : sal_False otherwhise.
136 : : */
137 : 1763 : virtual void freeResource()
138 : : {
139 : : // SAFE -> ..........................
140 [ + - ]: 1763 : ResetableGuard aMutexLock(m_aLock);
141 : :
142 : 1763 : css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock ;
143 : 1763 : sal_Bool bLocked = m_bActionLocked;
144 : :
145 : 1763 : m_xActionLock.clear();
146 : 1763 : m_bActionLocked = sal_False;
147 : :
148 [ + - ]: 1763 : aMutexLock.unlock();
149 : : // <- SAFE ..........................
150 : :
151 [ + - ][ + - ]: 1763 : if (bLocked && xLock.is())
[ + - ]
152 [ + - ][ + - ]: 1763 : xLock->removeActionLock();
[ + - ]
153 : 1763 : }
154 : :
155 : : //---------------------------------------
156 : : /** @short lock the internal wrapped resource, if its not already done. */
157 : 0 : virtual void lock()
158 : : {
159 : : // SAFE -> ..........................
160 [ # # ]: 0 : ResetableGuard aMutexLock(m_aLock);
161 : :
162 [ # # ][ # # ]: 0 : if (!m_bActionLocked && m_xActionLock.is())
[ # # ]
163 : : {
164 [ # # ][ # # ]: 0 : m_xActionLock->addActionLock();
165 [ # # ][ # # ]: 0 : m_bActionLocked = m_xActionLock->isActionLocked();
166 [ # # ]: 0 : }
167 : : // <- SAFE ..........................
168 : 0 : }
169 : :
170 : : //---------------------------------------
171 : : /** @short unlock the internal wrapped resource, if its not already done. */
172 : 1763 : virtual void unlock()
173 : : {
174 : : // SAFE -> ..........................
175 [ + - ]: 1763 : ResetableGuard aMutexLock(m_aLock);
176 : :
177 [ - + ][ # # ]: 1763 : if (m_bActionLocked && m_xActionLock.is())
[ - + ]
178 : : {
179 [ # # ][ # # ]: 0 : m_xActionLock->removeActionLock();
180 : : // dont check for any locks here ...
181 : : // May another guard use the same lock object :-(
182 : 0 : m_bActionLocked = sal_False;
183 [ + - ]: 1763 : }
184 : : // <- SAFE ..........................
185 : 1763 : }
186 : : };
187 : :
188 : : } // namespace framework
189 : :
190 : : #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
191 : :
192 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|