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