┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/statusbar/mountpointobservercache.cpp
blob: b39e26e3a6776581d215da5ae4d6b2f4989b4828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/***************************************************************************
 *   Copyright (C) 2014 by Frank Reininghaus <[email protected]>    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/

#include "mountpointobservercache.h"

#include "mountpointobserver.h"

#include <KMountPoint>

#include <QTimer>

class MountPointObserverCacheSingleton
{
public:
    MountPointObserverCache instance;
};
Q_GLOBAL_STATIC(MountPointObserverCacheSingleton, s_MountPointObserverCache)


MountPointObserverCache::MountPointObserverCache() :
    m_observerForMountPoint(),
    m_mountPointForObserver(),
    m_updateTimer(nullptr)
{
    m_updateTimer = new QTimer(this);
}

MountPointObserverCache::~MountPointObserverCache()
{
}

MountPointObserverCache* MountPointObserverCache::instance()
{
    return &s_MountPointObserverCache->instance;
}

MountPointObserver* MountPointObserverCache::observerForUrl(const QUrl& url)
{
    QUrl cachedObserverUrl;
    // If the url is a local path we can extract the root dir by checking the mount points.
    if (url.isLocalFile()) {
        // Try to share the observer with other paths that have the same mount point.
        KMountPoint::Ptr mountPoint = KMountPoint::currentMountPoints().findByPath(url.toLocalFile());
        if (mountPoint) {
            cachedObserverUrl = QUrl::fromLocalFile(mountPoint->mountPoint());
        } else {
            // Even if determining the mount point failed, the observer might still
            // be able to retrieve information about the url.
            cachedObserverUrl = url;
        }
    } else {
        cachedObserverUrl = url;
    }

    MountPointObserver* observer = m_observerForMountPoint.value(cachedObserverUrl);
    if (!observer) {
        observer = new MountPointObserver(cachedObserverUrl, this);
        m_observerForMountPoint.insert(cachedObserverUrl, observer);
        m_mountPointForObserver.insert(observer, cachedObserverUrl);
        Q_ASSERT(m_observerForMountPoint.count() == m_mountPointForObserver.count());

        connect(observer, &MountPointObserver::destroyed, this, &MountPointObserverCache::slotObserverDestroyed);

        if (!m_updateTimer->isActive()) {
            m_updateTimer->start(60000); // 1 minute
        }

        connect(m_updateTimer, &QTimer::timeout, observer, &MountPointObserver::update);
    }

    return observer;
}

void MountPointObserverCache::slotObserverDestroyed(QObject* observer)
{
    Q_ASSERT(m_mountPointForObserver.contains(observer));
    const QUrl& url = m_mountPointForObserver.value(observer);
    Q_ASSERT(m_observerForMountPoint.contains(url));
    m_observerForMountPoint.remove(url);
    m_mountPointForObserver.remove(observer);

    Q_ASSERT(m_observerForMountPoint.count() == m_mountPointForObserver.count());

    if (m_mountPointForObserver.isEmpty()) {
        m_updateTimer->stop();
    }
}