]> cloud.milkyroute.net Git - dolphin.git/blob - src/statusbar/spaceinfoobserver.cpp
0fb018727c5e7ea68d583c4f633621c886ab3b26
[dolphin.git] / src / statusbar / spaceinfoobserver.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 "spaceinfoobserver.h"
21
22 #include "mountpointobserver.h"
23
24 #include <QUrl>
25
26 SpaceInfoObserver::SpaceInfoObserver(const QUrl& url, QObject* parent) :
27 QObject(parent),
28 m_mountPointObserver(0),
29 m_dataSize(0),
30 m_dataAvailable(0)
31 {
32 m_mountPointObserver = MountPointObserver::observerForUrl(url);
33 m_mountPointObserver->ref();
34 connect(m_mountPointObserver, &MountPointObserver::spaceInfoChanged, this, &SpaceInfoObserver::spaceInfoChanged);
35 m_mountPointObserver->update();
36 }
37
38 SpaceInfoObserver::~SpaceInfoObserver()
39 {
40 if (m_mountPointObserver) {
41 m_mountPointObserver->deref();
42 m_mountPointObserver = 0;
43 }
44 }
45
46 quint64 SpaceInfoObserver::size() const
47 {
48 return m_dataSize;
49 }
50
51 quint64 SpaceInfoObserver::available() const
52 {
53 return m_dataAvailable;
54 }
55
56 void SpaceInfoObserver::setUrl(const QUrl& url)
57 {
58 MountPointObserver* newObserver = MountPointObserver::observerForUrl(url);
59 if (newObserver != m_mountPointObserver) {
60 if (m_mountPointObserver) {
61 disconnect(m_mountPointObserver, &MountPointObserver::spaceInfoChanged, this, &SpaceInfoObserver::spaceInfoChanged);
62 m_mountPointObserver->deref();
63 m_mountPointObserver = 0;
64 }
65
66 m_mountPointObserver = newObserver;
67 m_mountPointObserver->ref();
68 connect(m_mountPointObserver, &MountPointObserver::spaceInfoChanged, this, &SpaceInfoObserver::spaceInfoChanged);
69
70 // If newObserver is cached it won't call update until the next timer update,
71 // so update the observer now.
72 m_mountPointObserver->update();
73 }
74 }
75
76 void SpaceInfoObserver::spaceInfoChanged(quint64 size, quint64 available)
77 {
78 // Make sure that the size has actually changed
79 if (m_dataSize != size || m_dataAvailable != available) {
80 m_dataSize = size;
81 m_dataAvailable = available;
82
83 emit valuesChanged();
84 }
85 }