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 : /** @short implements a guard, which can use the interface
33 : <type scope="com::sun::star::document">XActionLockable</type>.
34 :
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!
37 : */
38 : class ActionLockGuard : private ThreadHelpBase
39 : {
40 : //-------------------------------------------
41 : // member
42 :
43 : private:
44 :
45 : /** @short points to the object, which can be locked from outside. */
46 : css::uno::Reference< css::document::XActionLockable > m_xActionLock;
47 :
48 : /** @short knows if a lock exists on the internal lock object
49 : forced by this guard instance. */
50 : sal_Bool m_bActionLocked;
51 :
52 : //-------------------------------------------
53 : // interface
54 :
55 : public:
56 :
57 : //---------------------------------------
58 : /** @short default ctor to initialize a "non working guard".
59 :
60 : @descr That can be usefull 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".
63 : */
64 241 : ActionLockGuard()
65 : : ThreadHelpBase ( )
66 241 : , m_bActionLocked(sal_False)
67 : {
68 241 : }
69 :
70 : //---------------------------------------
71 : /** @short initialize new guard instance and lock the given resource immediatly.
72 :
73 : @param xLock
74 : points to the outside resource, which should be locked.
75 : */
76 : ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
77 : : ThreadHelpBase ( )
78 : , m_bActionLocked(sal_False)
79 : {
80 : setResource(xLock);
81 : }
82 :
83 : //---------------------------------------
84 : /** @short release this guard instance and make shure, that no lock
85 : will exist afterwards on the internal wrapped resource.
86 : */
87 241 : virtual ~ActionLockGuard()
88 482 : {
89 241 : unlock();
90 241 : }
91 :
92 : //---------------------------------------
93 : /** @short set a new resource for locking at this guard.
94 :
95 : @descr This call will fail, if an internal resource already exists
96 : and is currently locked.
97 :
98 : @param xLock
99 : points to the outside resource, which should be locked.
100 :
101 : @return sal_True, if new resource could be set and locked.
102 : sal_False otherwhise.
103 : */
104 240 : virtual sal_Bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
105 : {
106 : // SAFE -> ..........................
107 240 : ResetableGuard aMutexLock(m_aLock);
108 :
109 240 : if (m_bActionLocked || !xLock.is())
110 0 : return sal_False;
111 :
112 240 : m_xActionLock = xLock;
113 240 : m_xActionLock->addActionLock();
114 240 : m_bActionLocked = m_xActionLock->isActionLocked();
115 : // <- SAFE ..........................
116 :
117 240 : return sal_True;
118 : }
119 :
120 : //---------------------------------------
121 : /** @short set a new resource for locking at this guard.
122 :
123 : @descr This call will fail, if an internal resource already exists
124 : and is currently locked.
125 :
126 : @param xLock
127 : points to the outside resource, which should be locked.
128 :
129 : @return sal_True, if new resource could be set and locked.
130 : sal_False otherwhise.
131 : */
132 241 : virtual void freeResource()
133 : {
134 : // SAFE -> ..........................
135 241 : ResetableGuard aMutexLock(m_aLock);
136 :
137 241 : css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock ;
138 241 : sal_Bool bLocked = m_bActionLocked;
139 :
140 241 : m_xActionLock.clear();
141 241 : m_bActionLocked = sal_False;
142 :
143 241 : aMutexLock.unlock();
144 : // <- SAFE ..........................
145 :
146 241 : if (bLocked && xLock.is())
147 240 : xLock->removeActionLock();
148 241 : }
149 :
150 : //---------------------------------------
151 : /** @short lock the internal wrapped resource, if its not already done. */
152 0 : virtual void lock()
153 : {
154 : // SAFE -> ..........................
155 0 : ResetableGuard aMutexLock(m_aLock);
156 :
157 0 : if (!m_bActionLocked && m_xActionLock.is())
158 : {
159 0 : m_xActionLock->addActionLock();
160 0 : m_bActionLocked = m_xActionLock->isActionLocked();
161 0 : }
162 : // <- SAFE ..........................
163 0 : }
164 :
165 : //---------------------------------------
166 : /** @short unlock the internal wrapped resource, if its not already done. */
167 241 : virtual void unlock()
168 : {
169 : // SAFE -> ..........................
170 241 : ResetableGuard aMutexLock(m_aLock);
171 :
172 241 : if (m_bActionLocked && m_xActionLock.is())
173 : {
174 0 : m_xActionLock->removeActionLock();
175 : // dont check for any locks here ...
176 : // May another guard use the same lock object :-(
177 0 : m_bActionLocked = sal_False;
178 241 : }
179 : // <- SAFE ..........................
180 241 : }
181 : };
182 :
183 : } // namespace framework
184 :
185 : #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_
186 :
187 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|