1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_ 29*cdf0e10cSrcweir #define __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_ 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir //_______________________________________________ 32*cdf0e10cSrcweir // includes of own project 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <threadhelp/threadhelpbase.hxx> 35*cdf0e10cSrcweir #include <threadhelp/resetableguard.hxx> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir //_______________________________________________ 38*cdf0e10cSrcweir // includes of uno interface 39*cdf0e10cSrcweir #include <com/sun/star/document/XActionLockable.hpp> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir //_______________________________________________ 42*cdf0e10cSrcweir // includes of an other project 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir //_______________________________________________ 45*cdf0e10cSrcweir // namespace 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir namespace framework{ 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #ifndef css 50*cdf0e10cSrcweir namespace css = ::com::sun::star; 51*cdf0e10cSrcweir #endif 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir //_______________________________________________ 54*cdf0e10cSrcweir // definitions 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** @short implements a guard, which can use the interface 57*cdf0e10cSrcweir <type scope="com::sun::star::document">XActionLockable</type>. 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir @descr This guard should be used to be shure, that any lock will be 60*cdf0e10cSrcweir released. Otherwhise the locaked document can hinder the office on shutdown! 61*cdf0e10cSrcweir */ 62*cdf0e10cSrcweir class ActionLockGuard : private ThreadHelpBase 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir //------------------------------------------- 65*cdf0e10cSrcweir // member 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir private: 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir /** @short points to the object, which can be locked from outside. */ 70*cdf0e10cSrcweir css::uno::Reference< css::document::XActionLockable > m_xActionLock; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir /** @short knows if a lock exists on the internal lock object 73*cdf0e10cSrcweir forced by this guard instance. */ 74*cdf0e10cSrcweir sal_Bool m_bActionLocked; 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir //------------------------------------------- 77*cdf0e10cSrcweir // interface 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir public: 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir //--------------------------------------- 82*cdf0e10cSrcweir /** @short default ctor to initialize a "non working guard". 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir @descr That can be usefull in cases, where no resource still exists, 85*cdf0e10cSrcweir but will be available next time. Then this guard can be used 86*cdf0e10cSrcweir in a mode "use guard for more then one resources". 87*cdf0e10cSrcweir */ 88*cdf0e10cSrcweir ActionLockGuard() 89*cdf0e10cSrcweir : ThreadHelpBase ( ) 90*cdf0e10cSrcweir , m_bActionLocked(sal_False) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir //--------------------------------------- 95*cdf0e10cSrcweir /** @short initialize new guard instance and lock the given resource immediatly. 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir @param xLock 98*cdf0e10cSrcweir points to the outside resource, which should be locked. 99*cdf0e10cSrcweir */ 100*cdf0e10cSrcweir ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock) 101*cdf0e10cSrcweir : ThreadHelpBase ( ) 102*cdf0e10cSrcweir , m_bActionLocked(sal_False) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir setResource(xLock); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir //--------------------------------------- 108*cdf0e10cSrcweir /** @short release this guard instance and make shure, that no lock 109*cdf0e10cSrcweir will exist afterwards on the internal wrapped resource. 110*cdf0e10cSrcweir */ 111*cdf0e10cSrcweir virtual ~ActionLockGuard() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir unlock(); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir //--------------------------------------- 117*cdf0e10cSrcweir /** @short set a new resource for locking at this guard. 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir @descr This call will fail, if an internal resource already exists 120*cdf0e10cSrcweir and is currently locked. 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir @param xLock 123*cdf0e10cSrcweir points to the outside resource, which should be locked. 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir @return sal_True, if new resource could be set and locked. 126*cdf0e10cSrcweir sal_False otherwhise. 127*cdf0e10cSrcweir */ 128*cdf0e10cSrcweir virtual sal_Bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir // SAFE -> .......................... 131*cdf0e10cSrcweir ResetableGuard aMutexLock(m_aLock); 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir if (m_bActionLocked || !xLock.is()) 134*cdf0e10cSrcweir return sal_False; 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir m_xActionLock = xLock; 137*cdf0e10cSrcweir m_xActionLock->addActionLock(); 138*cdf0e10cSrcweir m_bActionLocked = m_xActionLock->isActionLocked(); 139*cdf0e10cSrcweir // <- SAFE .......................... 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir return sal_True; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir //--------------------------------------- 145*cdf0e10cSrcweir /** @short set a new resource for locking at this guard. 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir @descr This call will fail, if an internal resource already exists 148*cdf0e10cSrcweir and is currently locked. 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir @param xLock 151*cdf0e10cSrcweir points to the outside resource, which should be locked. 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir @return sal_True, if new resource could be set and locked. 154*cdf0e10cSrcweir sal_False otherwhise. 155*cdf0e10cSrcweir */ 156*cdf0e10cSrcweir virtual void freeResource() 157*cdf0e10cSrcweir { 158*cdf0e10cSrcweir // SAFE -> .......................... 159*cdf0e10cSrcweir ResetableGuard aMutexLock(m_aLock); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock ; 162*cdf0e10cSrcweir sal_Bool bLocked = m_bActionLocked; 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir m_xActionLock.clear(); 165*cdf0e10cSrcweir m_bActionLocked = sal_False; 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir aMutexLock.unlock(); 168*cdf0e10cSrcweir // <- SAFE .......................... 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir if (bLocked && xLock.is()) 171*cdf0e10cSrcweir xLock->removeActionLock(); 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir //--------------------------------------- 175*cdf0e10cSrcweir /** @short lock the internal wrapped resource, if its not already done. */ 176*cdf0e10cSrcweir virtual void lock() 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir // SAFE -> .......................... 179*cdf0e10cSrcweir ResetableGuard aMutexLock(m_aLock); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir if (!m_bActionLocked && m_xActionLock.is()) 182*cdf0e10cSrcweir { 183*cdf0e10cSrcweir m_xActionLock->addActionLock(); 184*cdf0e10cSrcweir m_bActionLocked = m_xActionLock->isActionLocked(); 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir // <- SAFE .......................... 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir //--------------------------------------- 190*cdf0e10cSrcweir /** @short unlock the internal wrapped resource, if its not already done. */ 191*cdf0e10cSrcweir virtual void unlock() 192*cdf0e10cSrcweir { 193*cdf0e10cSrcweir // SAFE -> .......................... 194*cdf0e10cSrcweir ResetableGuard aMutexLock(m_aLock); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir if (m_bActionLocked && m_xActionLock.is()) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir m_xActionLock->removeActionLock(); 199*cdf0e10cSrcweir // dont check for any locks here ... 200*cdf0e10cSrcweir // May another guard use the same lock object :-( 201*cdf0e10cSrcweir m_bActionLocked = sal_False; 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir // <- SAFE .......................... 204*cdf0e10cSrcweir } 205*cdf0e10cSrcweir }; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir } // namespace framework 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir #endif // __FRAMEWORK_LOADENV_ACTIONLOCKGUARD_HXX_ 210