1 /***************************************************************************
2 * Copyright (C) 2014 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #ifndef MOUNTPOINTOBSERVER_H
21 #define MOUNTPOINTOBSERVER_H
23 #include <KDiskFreeSpaceInfo>
28 * A MountPointObserver can be used to determine the free space on a mount
29 * point. It will then check the free space periodically, and emit the signal
30 * spaceInfoChanged() if the return value of spaceInfo() has changed.
32 * Since multiple users which watch paths on the same mount point can share
33 * a MountPointObserver, it is not possible to create a MountPointObserver
34 * manually. Instead, the function observerForPath(QString&) should be called,
35 * which returns either an existing or a newly created MountPointObserver for
36 * the mount point where the path is mounted. observerForPath(QString&) looks
37 * for a suitable MountPointObserver in an internal cache and creates new
38 * MountPointObservers and adds them to the cache if necessary.
40 * Reference counting is used to keep track of the number of users, and to
41 * decide when the object can be deleted. A user of this class should call
42 * the method ref() when it starts using it, and deref() when it does not need
43 * the MountPointObserver any more.
45 * The object will not be deleted immediately if the reference count reaches
46 * zero. The object will only be destroyed when the next periodic update of
47 * the free space information happens, and the reference count is still zero.
48 * This approach makes it possible to re-use the object if a new user requests
49 * the free space for the same mount point before the next update.
51 class MountPointObserver
: public QObject
55 explicit MountPointObserver(const QString
& mountPoint
, QObject
* parent
= 0);
56 virtual ~MountPointObserver() {}
60 * Obtains information about the available space on the observed mount point.
62 KDiskFreeSpaceInfo
spaceInfo() const { return m_spaceInfo
; }
65 * Call this function to indicate that the caller intends to continue using this object. An
66 * internal reference count is increased then. When the observer is not needed any more,
67 * deref() should be called, which decreases the reference count again.
69 void ref() { ++m_referenceCount
; }
72 * This function can be used to indicate that the caller does not need this MountPointObserver
73 * any more. Internally, a reference count is decreased. If the reference count is zero while
74 * update() is called, the object deletes itself.
79 Q_ASSERT(m_referenceCount
>= 0);
83 * Returns a MountPointObserver for the given \a path. If the caller intends to continue using
84 * the returned object, it must call its ref() method.
86 static MountPointObserver
* observerForPath(const QString
& path
);
90 * This signal is emitted if the information that spaceInfo() will return has changed.
92 void spaceInfoChanged();
96 * If this slot is invoked, MountPointObserver checks if the available space on the observed
97 * mount point has changed, and emits spaceInfoChanged() if that is the case.
102 const QString m_mountPoint
;
103 int m_referenceCount
;
104 KDiskFreeSpaceInfo m_spaceInfo
;
106 friend class MountPointObserverCache
;