diff options
| author | Frank Reininghaus <[email protected]> | 2014-05-22 18:42:17 +0200 |
|---|---|---|
| committer | Frank Reininghaus <[email protected]> | 2014-05-22 18:42:17 +0200 |
| commit | 4fe788f1157426e819f1ba31d1ee6388759cfd18 (patch) | |
| tree | b2bf5e060e0658238fcc288d2f071a1959a76994 /src/statusbar/mountpointobserver.h | |
| parent | f27c1242371db1e69edab78f0bbca05206f47adf (diff) | |
Keep the "free space" information updated in all visible views
The old code would watch the free space on a mount point, i.e.,
determine the free space again, in 10-second intervals, only until the
view became invisible once (even if it was invisible only for a very
short moment, i.e., while splitting the view).
This commit ensures that the mount point is watched again as soon as
the corresponding view becomes visible again.
Moreover, the object that watches the free space for a mount point is
shared among all views that show URLs that belong to this mount point.
To achieve this, there is a central cache which can be used to obtain
an existing MountPointObserver for a certain path. If necessary, a new
MountPointObserver is created and added to the cache.
The MountPointObserver is removed from the cache and destroyed only if
no views use it any more, and no new users appear until the next update
(which happens every 10 seconds). This prevents that the free space is
measured repeatedly when changing the current directory on the same
mount point.
Many thanks to Emmanuel Pescosta for the initial ideas to factor out
the "free space" code and to establish a central storage for the
"observer" objects, and for providing many good suggestions how to
improve the code!
BUG: 327708
REVIEW: 118208
FIXED-IN: 4.14.0
Diffstat (limited to 'src/statusbar/mountpointobserver.h')
| -rw-r--r-- | src/statusbar/mountpointobserver.h | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/statusbar/mountpointobserver.h b/src/statusbar/mountpointobserver.h new file mode 100644 index 000000000..ac5f8ecc3 --- /dev/null +++ b/src/statusbar/mountpointobserver.h @@ -0,0 +1,109 @@ +/*************************************************************************** + * Copyright (C) 2014 by Frank Reininghaus <[email protected]> * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + +#ifndef MOUNTPOINTOBSERVER_H +#define MOUNTPOINTOBSERVER_H + +#include <KDiskFreeSpaceInfo> + +#include <QObject> + +/** + * A MountPointObserver can be used to determine the free space on a mount + * point. It will then check the free space periodically, and emit the signal + * spaceInfoChanged() if the return value of spaceInfo() has changed. + * + * Since multiple users which watch paths on the same mount point can share + * a MountPointObserver, it is not possible to create a MountPointObserver + * manually. Instead, the function observerForPath(QString&) should be called, + * which returns either an existing or a newly created MountPointObserver for + * the mount point where the path is mounted. observerForPath(QString&) looks + * for a suitable MountPointObserver in an internal cache and creates new + * MountPointObservers and adds them to the cache if necessary. + * + * Reference counting is used to keep track of the number of users, and to + * decide when the object can be deleted. A user of this class should call + * the method ref() when it starts using it, and deref() when it does not need + * the MountPointObserver any more. + * + * The object will not be deleted immediately if the reference count reaches + * zero. The object will only be destroyed when the next periodic update of + * the free space information happens, and the reference count is still zero. + * This approach makes it possible to re-use the object if a new user requests + * the free space for the same mount point before the next update. + */ +class MountPointObserver : public QObject +{ + Q_OBJECT + + explicit MountPointObserver(const QString& mountPoint, QObject* parent = 0); + virtual ~MountPointObserver() {} + +public: + /** + * Obtains information about the available space on the observed mount point. + */ + KDiskFreeSpaceInfo spaceInfo() const { return m_spaceInfo; } + + /** + * Call this function to indicate that the caller intends to continue using this object. An + * internal reference count is increased then. When the observer is not needed any more, + * deref() should be called, which decreases the reference count again. + */ + void ref() { ++m_referenceCount; } + + /** + * This function can be used to indicate that the caller does not need this MountPointObserver + * any more. Internally, a reference count is decreased. If the reference count is zero while + * update() is called, the object deletes itself. + */ + void deref() + { + --m_referenceCount; + Q_ASSERT(m_referenceCount >= 0); + } + + /** + * Returns a MountPointObserver for the given \a path. If the caller intends to continue using + * the returned object, it must call its ref() method. + */ + static MountPointObserver* observerForPath(const QString& path); + +signals: + /** + * This signal is emitted if the information that spaceInfo() will return has changed. + */ + void spaceInfoChanged(); + +public slots: + /** + * If this slot is invoked, MountPointObserver checks if the available space on the observed + * mount point has changed, and emits spaceInfoChanged() if that is the case. + */ + void update(); + +private: + const QString m_mountPoint; + int m_referenceCount; + KDiskFreeSpaceInfo m_spaceInfo; + + friend class MountPointObserverCache; +}; + +#endif |
