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