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