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