kfreespace: use the device description in the notification

it's fancy alright:
https://ibb.co/vzqTzsF

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-13 18:59:10 +03:00
parent 84745c78d7
commit 8dbfa6c2f4
5 changed files with 60 additions and 10 deletions

View file

@ -29,6 +29,7 @@
#include <solid/device.h> #include <solid/device.h>
#include <solid/storagevolume.h> #include <solid/storagevolume.h>
#include <solid/storageaccess.h> #include <solid/storageaccess.h>
#include <solid/devicenotifier.h>
K_PLUGIN_FACTORY(KFreeSpaceModuleFactory, registerPlugin<KFreeSpaceModule>();) K_PLUGIN_FACTORY(KFreeSpaceModuleFactory, registerPlugin<KFreeSpaceModule>();)
K_EXPORT_PLUGIN(KFreeSpaceModuleFactory("kfreespace")) K_EXPORT_PLUGIN(KFreeSpaceModuleFactory("kfreespace"))
@ -46,7 +47,18 @@ KFreeSpaceModule::KFreeSpaceModule(QObject *parent, const QList<QVariant> &args)
m_dirwatch->addFile(kfreespacercfile); m_dirwatch->addFile(kfreespacercfile);
connect(m_dirwatch, SIGNAL(dirty(QString)), this, SLOT(slotInit())); connect(m_dirwatch, SIGNAL(dirty(QString)), this, SLOT(slotInit()));
// TODO: watch storage devices to reinit // TODO: test it
#if 0
Solid::DeviceNotifier* solidnotifier = Solid::DeviceNotifier::instance();
connect(
solidnotifier, SIGNAL(deviceAdded(QString)),
this, SLOT(slotDeviceAdded(QString))
);
connect(
solidnotifier, SIGNAL(deviceRemoved(QString)),
this, SLOT(slotDeviceRemoved(QString))
);
#endif
} }
KFreeSpaceModule::~KFreeSpaceModule() KFreeSpaceModule::~KFreeSpaceModule()
@ -92,7 +104,8 @@ void KFreeSpaceModule::slotInit()
KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this); KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this);
const bool kfreespacestatus = kfreespaceimpl->watch( const bool kfreespacestatus = kfreespaceimpl->watch(
kfreespacedirpath, kfreespacedirpath,
kfreespacechecktime, kfreespacefreespace kfreespacechecktime, kfreespacefreespace,
soliddevice.description()
); );
if (!kfreespacestatus) { if (!kfreespacestatus) {
delete kfreespaceimpl; delete kfreespaceimpl;
@ -104,8 +117,37 @@ void KFreeSpaceModule::slotInit()
if (watcherror) { if (watcherror) {
KNotification *knotification = new KNotification("WatchError"); KNotification *knotification = new KNotification("WatchError");
knotification->setComponentData(KComponentData("kfreespace")); knotification->setComponentData(KComponentData("kfreespace"));
knotification->setTitle(i18n("Disk space watch")); knotification->setTitle(i18n("Free Space Notifier"));
knotification->setText(i18n("Unable to watch one or more devices")); knotification->setText(i18n("Unable to watch one or more devices"));
knotification->sendEvent(); knotification->sendEvent();
} }
} }
void KFreeSpaceModule::slotDeviceAdded(const QString &udi)
{
Solid::Device soliddevice(udi);
const Solid::StorageAccess* solidaccess = soliddevice.as<Solid::StorageAccess>();
if (solidaccess) {
kDebug() << "Storage access added" << udi;
connect(
solidaccess, SIGNAL(accessibilityChanged(bool,QString)),
this, SLOT(slotAccessibilityChanged(bool,QString))
);
}
}
void KFreeSpaceModule::slotDeviceRemoved(const QString &udi)
{
Solid::Device soliddevice(udi);
const Solid::StorageAccess* solidaccess = soliddevice.as<Solid::StorageAccess>();
if (solidaccess) {
kDebug() << "Storage access removed" << udi;
disconnect(solidaccess, 0, this, 0);
}
}
void KFreeSpaceModule::slotAccessibilityChanged(bool accessible, const QString &udi)
{
kDebug() << "Storage accessibility changed" << udi << accessible;
slotInit();
}

View file

@ -38,6 +38,9 @@ public:
private Q_SLOTS: private Q_SLOTS:
void slotInit(); void slotInit();
void slotDeviceAdded(const QString &udi);
void slotDeviceRemoved(const QString &udi);
void slotAccessibilityChanged(bool accessible, const QString &udi);
private: private:
KDirWatch* m_dirwatch; KDirWatch* m_dirwatch;

View file

@ -43,7 +43,8 @@ KFreeSpaceImpl::~KFreeSpaceImpl()
} }
bool KFreeSpaceImpl::watch(const QString &dirpath, bool KFreeSpaceImpl::watch(const QString &dirpath,
const qulonglong checktime, const qulonglong freespace) const qulonglong checktime, const qulonglong freespace,
const QString &description)
{ {
// qDebug() << Q_FUNC_INFO << dirpath << checktime << freespace; // qDebug() << Q_FUNC_INFO << dirpath << checktime << freespace;
m_directory = dirpath; m_directory = dirpath;
@ -51,6 +52,7 @@ bool KFreeSpaceImpl::watch(const QString &dirpath,
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 = (qBound(s_kfreespacefreespacemin, freespace, s_kfreespacefreespacemax) * 1024 * 1024);
m_description = description;
if (!QDir(m_directory).exists()) { if (!QDir(m_directory).exists()) {
kWarning() << "Directory does not exist" << m_directory; kWarning() << "Directory does not exist" << m_directory;
return false; return false;
@ -76,13 +78,14 @@ void KFreeSpaceImpl::timerEvent(QTimerEvent *event)
} }
const qulonglong freespace = kdiskinfo.available(); const qulonglong freespace = kdiskinfo.available();
const QString freespacestring = KGlobal::locale()->formatByteSize(freespace);
kDebug() << "Current" << m_directory kDebug() << "Current" << m_directory
<< "space is" << KGlobal::locale()->formatByteSize(freespace); << "space is" << freespacestring;
if (freespace <= m_freespace) { if (freespace <= m_freespace) {
KNotification *knotification = new KNotification("WatchLow"); KNotification *knotification = new KNotification("WatchLow");
knotification->setComponentData(KComponentData("kfreespace")); knotification->setComponentData(KComponentData("kfreespace"));
knotification->setTitle(i18n("Disk space watch")); knotification->setTitle(i18n("Low Disk Space"));
knotification->setText(i18n("Low Disk Space")); knotification->setText(i18n("%1 has %2 free space", m_description, freespacestring));
knotification->sendEvent(); knotification->sendEvent();
} }
} else { } else {

View file

@ -30,7 +30,8 @@ public:
~KFreeSpaceImpl(); ~KFreeSpaceImpl();
bool watch(const QString &dirpath, bool watch(const QString &dirpath,
const qulonglong checktime, const qulonglong freespace); const qulonglong checktime, const qulonglong freespace,
const QString &description);
protected: protected:
// reimplementation // reimplementation
@ -40,6 +41,7 @@ private:
QString m_directory; QString m_directory;
qulonglong m_checktime; qulonglong m_checktime;
qulonglong m_freespace; qulonglong m_freespace;
QString m_description;
int m_timerid; int m_timerid;
}; };

View file

@ -20,9 +20,9 @@
#define KFREESPACE_H #define KFREESPACE_H
static const bool s_kfreespacewatch = true; static const bool s_kfreespacewatch = true;
static const qulonglong s_kfreespacechecktime = 60; 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; // 1 minute static const qulonglong s_kfreespacechecktimemax = 60;
static const qulonglong s_kfreespacefreespace = 1024; // 1 GB static const qulonglong s_kfreespacefreespace = 1024; // 1 GB
static const qulonglong s_kfreespacefreespacemin = 10; static const qulonglong s_kfreespacefreespacemin = 10;
static const qulonglong s_kfreespacefreespacemax = 1024; static const qulonglong s_kfreespacefreespacemax = 1024;