From a2a41e4e5d4b9c375c58554db3a8250c16a186f5 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sun, 3 Mar 2024 06:02:36 +0200 Subject: [PATCH] kfreespace: default to 1/10 of the total space credits to Benjamin Grant for making me do it, fallback is still 1 GB and the configuration interface remains the same but the default should fit more cases now Signed-off-by: Ivailo Monev --- kfreespace/kcm/kfreespaceconfig.cpp | 4 ++-- kfreespace/kded/kded_kfreespace.cpp | 2 +- kfreespace/kded/kfreespaceimpl.cpp | 3 +-- kfreespace/kfreespace.h | 30 ++++++++++++++++++++++++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/kfreespace/kcm/kfreespaceconfig.cpp b/kfreespace/kcm/kfreespaceconfig.cpp index 7fca0f0e..d6b288c8 100644 --- a/kfreespace/kcm/kfreespaceconfig.cpp +++ b/kfreespace/kcm/kfreespaceconfig.cpp @@ -136,7 +136,7 @@ void KFreeSpaceBox::setDefault() { m_watchbox->setChecked(s_kfreespacewatch); m_checktimeinput->setValue(s_kfreespacechecktime); - m_freespaceinput->setValue(s_kfreespacefreespace); + m_freespaceinput->setValue(kCalculateFreeSpace(m_soliddevice, s_kfreespacefreespace)); } void KFreeSpaceBox::slotWatch() @@ -229,7 +229,7 @@ void KCMFreeSpace::load() KFreeSpaceBox* devicebox = new KFreeSpaceBox( this, soliddevice, - kfreespacewatch, kfreespacechecktime, kfreespacefreespace + kfreespacewatch, kfreespacechecktime, kCalculateFreeSpace(soliddevice, kfreespacefreespace) ); m_deviceboxes.append(devicebox); connect(devicebox, SIGNAL(changed()), this, SLOT(slotDeviceChanged())); diff --git a/kfreespace/kded/kded_kfreespace.cpp b/kfreespace/kded/kded_kfreespace.cpp index d1b63ea8..0314c44f 100644 --- a/kfreespace/kded/kded_kfreespace.cpp +++ b/kfreespace/kded/kded_kfreespace.cpp @@ -95,7 +95,7 @@ void KFreeSpaceModule::slotInit() KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this); const bool kfreespacestatus = kfreespaceimpl->watch( soliddevice, - kfreespacechecktime, kfreespacefreespace + kfreespacechecktime, kCalculateFreeSpace(soliddevice, kfreespacefreespace) ); if (!kfreespacestatus) { delete kfreespaceimpl; diff --git a/kfreespace/kded/kfreespaceimpl.cpp b/kfreespace/kded/kfreespaceimpl.cpp index 920b763f..54fcff7b 100644 --- a/kfreespace/kded/kfreespaceimpl.cpp +++ b/kfreespace/kded/kfreespaceimpl.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include @@ -50,7 +49,7 @@ bool KFreeSpaceImpl::watch(const Solid::Device &soliddevice, // NOTE: time from config is in seconds, has to be in ms here m_checktime = (qBound(s_kfreespacechecktimemin, checktime, s_kfreespacechecktimemax) * 1000); // NOTE: size from config is in MB, has to be in bytes here - m_freespace = (qBound(s_kfreespacefreespacemin, freespace, s_kfreespacefreespacemax) * 1024 * 1024); + m_freespace = (kCalculateFreeSpace(soliddevice, freespace) * 1024 * 1024); m_timerid = startTimer(m_checktime); kDebug() << "Checking" << m_soliddevice.udi() << "every" << (m_checktime / 1000) diff --git a/kfreespace/kfreespace.h b/kfreespace/kfreespace.h index c77d44bc..cb37d413 100644 --- a/kfreespace/kfreespace.h +++ b/kfreespace/kfreespace.h @@ -19,12 +19,40 @@ #ifndef KFREESPACE_H #define KFREESPACE_H +#include +#include +#include +#include + static const bool s_kfreespacewatch = true; static const qulonglong s_kfreespacechecktime = 60; // 1 minute static const qulonglong s_kfreespacechecktimemin = 1; static const qulonglong s_kfreespacechecktimemax = 60; -static const qulonglong s_kfreespacefreespace = 1024; // 1 GB +static const qulonglong s_kfreespacefreespace = 0; // either the value from the config or 1/10, fallback is 1024 static const qulonglong s_kfreespacefreespacemin = 10; static const qulonglong s_kfreespacefreespacemax = 1024; +static qulonglong kCalculateFreeSpace(const Solid::Device &soliddevice, const qulonglong freespace) +{ + const Solid::StorageVolume* solidvolume = soliddevice.as(); + Q_ASSERT(solidvolume); + const qulonglong totalsize = solidvolume->size(); + if (totalsize <= 0) { + // if the total size of the device cannot be obtained then the space is the one passed + // bound to min and max, unless the passed value does not come from config (freespace + // variable is zero) + if (freespace <= 0) { + return 1024; + } + return qBound(s_kfreespacefreespacemin, freespace, s_kfreespacefreespacemax); + } + const qulonglong autosize = (totalsize / 1024 / 1024 / 10); + // the case of not being specified, 1/10 of the total space bound to min and max + if (freespace <= 0) { + return qBound(s_kfreespacefreespacemin, autosize, s_kfreespacefreespacemax); + } + // else it is the one explicitly specified bound to min and max + return qBound(s_kfreespacefreespacemin, freespace, s_kfreespacefreespacemax); +} + #endif // KFREESPACE_H