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 #include "mountpointobservercache.h"
22 #include "mountpointobserver.h"
25 #include <KMountPoint>
29 class MountPointObserverCacheSingleton
32 MountPointObserverCache instance
;
34 K_GLOBAL_STATIC(MountPointObserverCacheSingleton
, s_MountPointObserverCache
)
37 MountPointObserverCache::MountPointObserverCache() :
38 m_observerForMountPoint(),
39 m_mountPointForObserver(),
42 m_updateTimer
= new QTimer(this);
45 MountPointObserverCache::~MountPointObserverCache()
49 MountPointObserverCache
* MountPointObserverCache::instance()
51 return &s_MountPointObserverCache
->instance
;
54 MountPointObserver
* MountPointObserverCache::observerForPath(const QString
& path
)
56 // Try to share the observer with other paths that have the same mount point.
57 QString mountPointPath
;
58 KMountPoint::Ptr mountPoint
= KMountPoint::currentMountPoints().findByPath(path
);
60 mountPointPath
= mountPoint
->mountPoint();
62 // Even if determining the mount point failed, KDiskFreeSpaceInfo might still
63 // be able to retrieve information about the path.
64 mountPointPath
= path
;
67 MountPointObserver
* observer
= m_observerForMountPoint
.value(mountPointPath
);
69 observer
= new MountPointObserver(mountPointPath
, this);
70 m_observerForMountPoint
.insert(mountPointPath
, observer
);
71 m_mountPointForObserver
.insert(observer
, mountPointPath
);
72 Q_ASSERT(m_observerForMountPoint
.count() == m_mountPointForObserver
.count());
74 connect(observer
, SIGNAL(destroyed(QObject
*)), this, SLOT(slotObserverDestroyed(QObject
*)));
76 if (!m_updateTimer
->isActive()) {
77 m_updateTimer
->start(10000);
80 connect(m_updateTimer
, SIGNAL(timeout()), observer
, SLOT(update()));
86 void MountPointObserverCache::slotObserverDestroyed(QObject
* observer
)
88 Q_ASSERT(m_mountPointForObserver
.contains(observer
));
89 const QString
& path
= m_mountPointForObserver
.value(observer
);
90 Q_ASSERT(m_observerForMountPoint
.contains(path
));
91 m_observerForMountPoint
.remove(path
);
92 m_mountPointForObserver
.remove(observer
);
94 Q_ASSERT(m_observerForMountPoint
.count() == m_mountPointForObserver
.count());
96 if (m_mountPointForObserver
.isEmpty()) {
97 m_updateTimer
->stop();