Changeset View
Changeset View
Standalone View
Standalone View
src/kitemviews/spaceinfoobserver.cpp
- This file was added.
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 | spaceInfoChanged(0, 0); | ||||
65 | } | ||||
66 | | ||||
67 | m_mountPointObserver = newObserver; | ||||
68 | m_mountPointObserver->ref(); | ||||
69 | connect(m_mountPointObserver, &MountPointObserver::spaceInfoChanged, this, &SpaceInfoObserver::spaceInfoChanged); | ||||
70 | | ||||
71 | // If newObserver is cached it won't call update until the next timer update, | ||||
72 | // so update the observer now. | ||||
73 | m_mountPointObserver->update(); | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
77 | void SpaceInfoObserver::spaceInfoChanged(quint64 size, quint64 available) | ||||
78 | { | ||||
79 | // Make sure that the size has actually changed | ||||
80 | if (m_dataSize != size || m_dataAvailable != available) { | ||||
81 | m_dataSize = size; | ||||
82 | m_dataAvailable = available; | ||||
83 | | ||||
84 | emit valuesChanged(); | ||||
85 | } | ||||
86 | } |