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