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 <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-03 06:02:36 +02:00
parent e40d992f82
commit a2a41e4e5d
4 changed files with 33 additions and 6 deletions

View file

@ -136,7 +136,7 @@ void KFreeSpaceBox::setDefault()
{ {
m_watchbox->setChecked(s_kfreespacewatch); m_watchbox->setChecked(s_kfreespacewatch);
m_checktimeinput->setValue(s_kfreespacechecktime); m_checktimeinput->setValue(s_kfreespacechecktime);
m_freespaceinput->setValue(s_kfreespacefreespace); m_freespaceinput->setValue(kCalculateFreeSpace(m_soliddevice, s_kfreespacefreespace));
} }
void KFreeSpaceBox::slotWatch() void KFreeSpaceBox::slotWatch()
@ -229,7 +229,7 @@ void KCMFreeSpace::load()
KFreeSpaceBox* devicebox = new KFreeSpaceBox( KFreeSpaceBox* devicebox = new KFreeSpaceBox(
this, this,
soliddevice, soliddevice,
kfreespacewatch, kfreespacechecktime, kfreespacefreespace kfreespacewatch, kfreespacechecktime, kCalculateFreeSpace(soliddevice, kfreespacefreespace)
); );
m_deviceboxes.append(devicebox); m_deviceboxes.append(devicebox);
connect(devicebox, SIGNAL(changed()), this, SLOT(slotDeviceChanged())); connect(devicebox, SIGNAL(changed()), this, SLOT(slotDeviceChanged()));

View file

@ -95,7 +95,7 @@ void KFreeSpaceModule::slotInit()
KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this); KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this);
const bool kfreespacestatus = kfreespaceimpl->watch( const bool kfreespacestatus = kfreespaceimpl->watch(
soliddevice, soliddevice,
kfreespacechecktime, kfreespacefreespace kfreespacechecktime, kCalculateFreeSpace(soliddevice, kfreespacefreespace)
); );
if (!kfreespacestatus) { if (!kfreespacestatus) {
delete kfreespaceimpl; delete kfreespaceimpl;

View file

@ -21,7 +21,6 @@
#include <QDir> #include <QDir>
#include <klocale.h> #include <klocale.h>
#include <kdiskfreespaceinfo.h>
#include <knotification.h> #include <knotification.h>
#include <kdebug.h> #include <kdebug.h>
#include <solid/storageaccess.h> #include <solid/storageaccess.h>
@ -50,7 +49,7 @@ bool KFreeSpaceImpl::watch(const Solid::Device &soliddevice,
// NOTE: time from config is in seconds, has to be in ms here // NOTE: time from config is in seconds, has to be in ms here
m_checktime = (qBound(s_kfreespacechecktimemin, checktime, s_kfreespacechecktimemax) * 1000); m_checktime = (qBound(s_kfreespacechecktimemin, checktime, s_kfreespacechecktimemax) * 1000);
// NOTE: size from config is in MB, has to be in bytes here // 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); m_timerid = startTimer(m_checktime);
kDebug() << "Checking" << m_soliddevice.udi() kDebug() << "Checking" << m_soliddevice.udi()
<< "every" << (m_checktime / 1000) << "every" << (m_checktime / 1000)

View file

@ -19,12 +19,40 @@
#ifndef KFREESPACE_H #ifndef KFREESPACE_H
#define KFREESPACE_H #define KFREESPACE_H
#include <kdiskfreespaceinfo.h>
#include <solid/device.h>
#include <solid/storagevolume.h>
#include <kdebug.h>
static const bool s_kfreespacewatch = true; static const bool s_kfreespacewatch = true;
static const qulonglong s_kfreespacechecktime = 60; // 1 minute static const qulonglong s_kfreespacechecktime = 60; // 1 minute
static const qulonglong s_kfreespacechecktimemin = 1; static const qulonglong s_kfreespacechecktimemin = 1;
static const qulonglong s_kfreespacechecktimemax = 60; 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_kfreespacefreespacemin = 10;
static const qulonglong s_kfreespacefreespacemax = 1024; static const qulonglong s_kfreespacefreespacemax = 1024;
static qulonglong kCalculateFreeSpace(const Solid::Device &soliddevice, const qulonglong freespace)
{
const Solid::StorageVolume* solidvolume = soliddevice.as<Solid::StorageVolume>();
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 #endif // KFREESPACE_H