2010-10-27 13:11:31 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 14:30:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2004-01-28 13:35:31 +00:00
|
|
|
|
2013-11-05 02:25:45 +01:00
|
|
|
#ifndef INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
|
|
|
|
#define INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
#include <com/sun/star/document/XActionLockable.hpp>
|
2014-11-17 22:45:11 +01:00
|
|
|
#include <osl/mutex.hxx>
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
namespace framework{
|
|
|
|
|
|
|
|
/** @short implements a guard, which can use the interface
|
|
|
|
<type scope="com::sun::star::document">XActionLockable</type>.
|
|
|
|
|
2014-04-18 00:22:58 +03:00
|
|
|
@descr This guard should be used to be sure, that any lock will be
|
2013-09-26 10:21:12 +02:00
|
|
|
released. Otherwise the locaked document can hinder the office on shutdown!
|
2004-01-28 13:35:31 +00:00
|
|
|
*/
|
2014-03-20 11:18:38 +01:00
|
|
|
class ActionLockGuard
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
2014-02-25 18:54:02 +01:00
|
|
|
|
2004-01-28 13:35:31 +00:00
|
|
|
// member
|
|
|
|
|
|
|
|
private:
|
2014-03-20 11:18:38 +01:00
|
|
|
osl::Mutex m_mutex;
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
/** @short points to the object, which can be locked from outside. */
|
|
|
|
css::uno::Reference< css::document::XActionLockable > m_xActionLock;
|
|
|
|
|
|
|
|
/** @short knows if a lock exists on the internal lock object
|
|
|
|
forced by this guard instance. */
|
2014-04-04 15:53:21 +02:00
|
|
|
bool m_bActionLocked;
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
// interface
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** @short default ctor to initialize a "non working guard".
|
|
|
|
|
2013-02-22 11:12:12 +01:00
|
|
|
@descr That can be useful in cases, where no resource still exists,
|
2004-01-28 13:35:31 +00:00
|
|
|
but will be available next time. Then this guard can be used
|
2014-08-01 16:56:25 +09:00
|
|
|
in a mode "use guard for more than one resources".
|
2004-01-28 13:35:31 +00:00
|
|
|
*/
|
|
|
|
ActionLockGuard()
|
2014-04-04 15:53:21 +02:00
|
|
|
: m_bActionLocked(false)
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-04-15 05:32:37 +02:00
|
|
|
/** @short initialize new guard instance and lock the given resource immediately.
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
@param xLock
|
|
|
|
points to the outside resource, which should be locked.
|
|
|
|
*/
|
|
|
|
ActionLockGuard(const css::uno::Reference< css::document::XActionLockable >& xLock)
|
2014-04-04 15:53:21 +02:00
|
|
|
: m_bActionLocked(false)
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
|
|
|
setResource(xLock);
|
|
|
|
}
|
|
|
|
|
2014-04-18 00:22:58 +03:00
|
|
|
/** @short release this guard instance and make sure, that no lock
|
2004-01-28 13:35:31 +00:00
|
|
|
will exist afterwards on the internal wrapped resource.
|
|
|
|
*/
|
|
|
|
virtual ~ActionLockGuard()
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @short set a new resource for locking at this guard.
|
|
|
|
|
|
|
|
@descr This call will fail, if an internal resource already exists
|
|
|
|
and is currently locked.
|
|
|
|
|
|
|
|
@param xLock
|
|
|
|
points to the outside resource, which should be locked.
|
|
|
|
|
2010-11-05 10:31:15 +08:00
|
|
|
@return sal_True, if new resource could be set and locked.
|
2013-02-13 18:25:22 +04:00
|
|
|
sal_False otherwise.
|
2004-01-28 13:35:31 +00:00
|
|
|
*/
|
2015-01-20 12:38:10 +02:00
|
|
|
bool setResource(const css::uno::Reference< css::document::XActionLockable >& xLock)
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
2014-03-20 11:18:38 +01:00
|
|
|
osl::MutexGuard g(m_mutex);
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
if (m_bActionLocked || !xLock.is())
|
2014-04-04 15:53:21 +02:00
|
|
|
return false;
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
m_xActionLock = xLock;
|
|
|
|
m_xActionLock->addActionLock();
|
|
|
|
m_bActionLocked = m_xActionLock->isActionLocked();
|
|
|
|
|
2014-04-04 15:53:21 +02:00
|
|
|
return true;
|
2004-01-28 13:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @short set a new resource for locking at this guard.
|
|
|
|
|
|
|
|
@descr This call will fail, if an internal resource already exists
|
|
|
|
and is currently locked.
|
|
|
|
|
|
|
|
@param xLock
|
|
|
|
points to the outside resource, which should be locked.
|
|
|
|
|
2010-11-05 10:31:15 +08:00
|
|
|
@return sal_True, if new resource could be set and locked.
|
2013-02-13 18:25:22 +04:00
|
|
|
sal_False otherwise.
|
2004-01-28 13:35:31 +00:00
|
|
|
*/
|
2015-01-20 12:38:10 +02:00
|
|
|
void freeResource()
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
|
|
|
// SAFE -> ..........................
|
2014-03-20 11:18:38 +01:00
|
|
|
osl::ClearableMutexGuard aMutexLock(m_mutex);
|
2004-01-28 13:35:31 +00:00
|
|
|
|
2014-04-06 19:36:08 +03:00
|
|
|
css::uno::Reference< css::document::XActionLockable > xLock = m_xActionLock;
|
2014-04-04 15:53:21 +02:00
|
|
|
bool bLocked = m_bActionLocked;
|
2004-01-28 13:35:31 +00:00
|
|
|
|
|
|
|
m_xActionLock.clear();
|
2014-04-04 15:53:21 +02:00
|
|
|
m_bActionLocked = false;
|
2004-01-28 13:35:31 +00:00
|
|
|
|
2014-03-20 11:18:38 +01:00
|
|
|
aMutexLock.clear();
|
2004-01-28 13:35:31 +00:00
|
|
|
// <- SAFE ..........................
|
|
|
|
|
|
|
|
if (bLocked && xLock.is())
|
|
|
|
xLock->removeActionLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @short lock the internal wrapped resource, if its not already done. */
|
2015-01-20 12:38:10 +02:00
|
|
|
void lock()
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
2014-03-20 11:18:38 +01:00
|
|
|
osl::MutexGuard g(m_mutex);
|
2004-01-28 13:35:31 +00:00
|
|
|
if (!m_bActionLocked && m_xActionLock.is())
|
|
|
|
{
|
|
|
|
m_xActionLock->addActionLock();
|
|
|
|
m_bActionLocked = m_xActionLock->isActionLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @short unlock the internal wrapped resource, if its not already done. */
|
2015-01-20 12:38:10 +02:00
|
|
|
void unlock()
|
2004-01-28 13:35:31 +00:00
|
|
|
{
|
2014-03-20 11:18:38 +01:00
|
|
|
osl::MutexGuard g(m_mutex);
|
2004-01-28 13:35:31 +00:00
|
|
|
if (m_bActionLocked && m_xActionLock.is())
|
|
|
|
{
|
|
|
|
m_xActionLock->removeActionLock();
|
|
|
|
// dont check for any locks here ...
|
|
|
|
// May another guard use the same lock object :-(
|
2014-04-04 15:53:21 +02:00
|
|
|
m_bActionLocked = false;
|
2004-01-28 13:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
|
2013-11-05 02:25:45 +01:00
|
|
|
#endif // INCLUDED_FRAMEWORK_SOURCE_INC_LOADENV_ACTIONLOCKGUARD_HXX
|
2010-10-27 13:11:31 +01:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|