]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/mountpointobservercache.cpp
[MountPointObserverCache] Update mounts less frequently
[dolphin.git] / src / statusbar / mountpointobservercache.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Frank Reininghaus <frank78ac@googlemail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "mountpointobservercache.h"
21
22 #include "mountpointobserver.h"
23
24 #include <KMountPoint>
25
26 #include <QTimer>
27
28 class MountPointObserverCacheSingleton
29 {
30 public:
31 MountPointObserverCache instance;
32 };
33 Q_GLOBAL_STATIC(MountPointObserverCacheSingleton, s_MountPointObserverCache)
34
35
36 MountPointObserverCache::MountPointObserverCache() :
37 m_observerForMountPoint(),
38 m_mountPointForObserver(),
39 m_updateTimer(nullptr)
40 {
41 m_updateTimer = new QTimer(this);
42 }
43
44 MountPointObserverCache::~MountPointObserverCache()
45 {
46 }
47
48 MountPointObserverCache* MountPointObserverCache::instance()
49 {
50 return &s_MountPointObserverCache->instance;
51 }
52
53 MountPointObserver* MountPointObserverCache::observerForUrl(const QUrl& url)
54 {
55 QUrl cachedObserverUrl;
56 // If the url is a local path we can extract the root dir by checking the mount points.
57 if (url.isLocalFile()) {
58 // Try to share the observer with other paths that have the same mount point.
59 KMountPoint::Ptr mountPoint = KMountPoint::currentMountPoints().findByPath(url.toLocalFile());
60 if (mountPoint) {
61 cachedObserverUrl = QUrl::fromLocalFile(mountPoint->mountPoint());
62 } else {
63 // Even if determining the mount point failed, the observer might still
64 // be able to retrieve information about the url.
65 cachedObserverUrl = url;
66 }
67 } else {
68 cachedObserverUrl = url;
69 }
70
71 MountPointObserver* observer = m_observerForMountPoint.value(cachedObserverUrl);
72 if (!observer) {
73 observer = new MountPointObserver(cachedObserverUrl, this);
74 m_observerForMountPoint.insert(cachedObserverUrl, observer);
75 m_mountPointForObserver.insert(observer, cachedObserverUrl);
76 Q_ASSERT(m_observerForMountPoint.count() == m_mountPointForObserver.count());
77
78 connect(observer, &MountPointObserver::destroyed, this, &MountPointObserverCache::slotObserverDestroyed);
79
80 if (!m_updateTimer->isActive()) {
81 m_updateTimer->start(60000); // 1 minute
82 }
83
84 connect(m_updateTimer, &QTimer::timeout, observer, &MountPointObserver::update);
85 }
86
87 return observer;
88 }
89
90 void MountPointObserverCache::slotObserverDestroyed(QObject* observer)
91 {
92 Q_ASSERT(m_mountPointForObserver.contains(observer));
93 const QUrl& url = m_mountPointForObserver.value(observer);
94 Q_ASSERT(m_observerForMountPoint.contains(url));
95 m_observerForMountPoint.remove(url);
96 m_mountPointForObserver.remove(observer);
97
98 Q_ASSERT(m_observerForMountPoint.count() == m_mountPointForObserver.count());
99
100 if (m_mountPointForObserver.isEmpty()) {
101 m_updateTimer->stop();
102 }
103 }