┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/statusbar/statusbarspaceinfo.cpp
diff options
context:
space:
mode:
authorFrank Reininghaus <[email protected]>2014-05-22 18:42:17 +0200
committerFrank Reininghaus <[email protected]>2014-05-22 18:42:17 +0200
commit4fe788f1157426e819f1ba31d1ee6388759cfd18 (patch)
treeb2bf5e060e0658238fcc288d2f071a1959a76994 /src/statusbar/statusbarspaceinfo.cpp
parentf27c1242371db1e69edab78f0bbca05206f47adf (diff)
Keep the "free space" information updated in all visible views
The old code would watch the free space on a mount point, i.e., determine the free space again, in 10-second intervals, only until the view became invisible once (even if it was invisible only for a very short moment, i.e., while splitting the view). This commit ensures that the mount point is watched again as soon as the corresponding view becomes visible again. Moreover, the object that watches the free space for a mount point is shared among all views that show URLs that belong to this mount point. To achieve this, there is a central cache which can be used to obtain an existing MountPointObserver for a certain path. If necessary, a new MountPointObserver is created and added to the cache. The MountPointObserver is removed from the cache and destroyed only if no views use it any more, and no new users appear until the next update (which happens every 10 seconds). This prevents that the free space is measured repeatedly when changing the current directory on the same mount point. Many thanks to Emmanuel Pescosta for the initial ideas to factor out the "free space" code and to establish a central storage for the "observer" objects, and for providing many good suggestions how to improve the code! BUG: 327708 REVIEW: 118208 FIXED-IN: 4.14.0
Diffstat (limited to 'src/statusbar/statusbarspaceinfo.cpp')
-rw-r--r--src/statusbar/statusbarspaceinfo.cpp57
1 files changed, 19 insertions, 38 deletions
diff --git a/src/statusbar/statusbarspaceinfo.cpp b/src/statusbar/statusbarspaceinfo.cpp
index 61b28334a..3692947b1 100644
--- a/src/statusbar/statusbarspaceinfo.cpp
+++ b/src/statusbar/statusbarspaceinfo.cpp
@@ -20,22 +20,17 @@
#include "statusbarspaceinfo.h"
-#include <KDiskFreeSpaceInfo>
+#include "spaceinfoobserver.h"
+
#include <KLocale>
#include <KIO/Job>
-#include <QTimer>
#include <QKeyEvent>
StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) :
KCapacityBar(KCapacityBar::DrawTextInline, parent),
- m_kBSize(0),
- m_timer(0)
+ m_observer(0)
{
- // Use a timer to update the space information. Polling is useful
- // here, as files can be deleted/added outside the scope of Dolphin.
- m_timer = new QTimer(this);
- connect(m_timer, SIGNAL(timeout()), this, SLOT(calculateSpaceInfo()));
}
StatusBarSpaceInfo::~StatusBarSpaceInfo()
@@ -46,8 +41,8 @@ void StatusBarSpaceInfo::setUrl(const KUrl& url)
{
if (m_url != url) {
m_url = url;
- if (isVisible()) {
- calculateSpaceInfo();
+ if (m_observer) {
+ m_observer->setUrl(url);
}
}
}
@@ -60,47 +55,33 @@ KUrl StatusBarSpaceInfo::url() const
void StatusBarSpaceInfo::showEvent(QShowEvent* event)
{
KCapacityBar::showEvent(event);
- if (!event->spontaneous()) {
- calculateSpaceInfo();
- m_timer->start(10000);
- }
+ m_observer.reset(new SpaceInfoObserver(m_url, this));
+ slotValuesChanged();
+ connect(m_observer.data(), SIGNAL(valuesChanged()), this, SLOT(slotValuesChanged()));
}
void StatusBarSpaceInfo::hideEvent(QHideEvent* event)
{
- m_timer->stop();
+ m_observer.reset();
KCapacityBar::hideEvent(event);
}
-void StatusBarSpaceInfo::calculateSpaceInfo()
+void StatusBarSpaceInfo::slotValuesChanged()
{
- // KDiskFreeSpace is for local paths only
- if (!m_url.isLocalFile()) {
- setText(i18nc("@info:status", "Unknown size"));
- setValue(0);
- update();
- return;
- }
-
- KDiskFreeSpaceInfo job = KDiskFreeSpaceInfo::freeSpaceInfo(m_url.toLocalFile());
- if (!job.isValid()) {
+ Q_ASSERT(m_observer);
+ const quint64 size = m_observer->size();
+ if (size == 0) {
setText(i18nc("@info:status", "Unknown size"));
setValue(0);
update();
- return;
- }
-
- KIO::filesize_t kBSize = job.size() / 1024;
- KIO::filesize_t kBUsed = job.used() / 1024;
-
- const bool valuesChanged = (kBUsed != static_cast<quint64>(value())) || (kBSize != m_kBSize);
- if (valuesChanged) {
- setText(i18nc("@info:status Free disk space", "%1 free",
- KIO::convertSize(job.available())));
+ } else {
+ const quint64 available = m_observer->available();
+ const quint64 used = size - available;
+ const int percentUsed = qRound(100.0 * qreal(used) / qreal(size));
+ setText(i18nc("@info:status Free disk space", "%1 free", KIO::convertSize(available)));
setUpdatesEnabled(false);
- m_kBSize = kBSize;
- setValue(kBSize > 0 ? (kBUsed * 100) / kBSize : 0);
+ setValue(percentUsed);
setUpdatesEnabled(true);
update();
}