From 8dbfa6c2f43aecc696dc210681ab27836c3f55d1 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 13 Jun 2023 18:59:10 +0300 Subject: [PATCH] kfreespace: use the device description in the notification it's fancy alright: https://ibb.co/vzqTzsF Signed-off-by: Ivailo Monev --- kfreespace/kded/kded_kfreespace.cpp | 48 +++++++++++++++++++++++++++-- kfreespace/kded/kded_kfreespace.h | 3 ++ kfreespace/kded/kfreespaceimpl.cpp | 11 ++++--- kfreespace/kded/kfreespaceimpl.h | 4 ++- kfreespace/kfreespace.h | 4 +-- 5 files changed, 60 insertions(+), 10 deletions(-) diff --git a/kfreespace/kded/kded_kfreespace.cpp b/kfreespace/kded/kded_kfreespace.cpp index d48a3be4..0fe23929 100644 --- a/kfreespace/kded/kded_kfreespace.cpp +++ b/kfreespace/kded/kded_kfreespace.cpp @@ -29,6 +29,7 @@ #include #include #include +#include K_PLUGIN_FACTORY(KFreeSpaceModuleFactory, registerPlugin();) K_EXPORT_PLUGIN(KFreeSpaceModuleFactory("kfreespace")) @@ -46,7 +47,18 @@ KFreeSpaceModule::KFreeSpaceModule(QObject *parent, const QList &args) m_dirwatch->addFile(kfreespacercfile); 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() @@ -92,7 +104,8 @@ void KFreeSpaceModule::slotInit() KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this); const bool kfreespacestatus = kfreespaceimpl->watch( kfreespacedirpath, - kfreespacechecktime, kfreespacefreespace + kfreespacechecktime, kfreespacefreespace, + soliddevice.description() ); if (!kfreespacestatus) { delete kfreespaceimpl; @@ -104,8 +117,37 @@ void KFreeSpaceModule::slotInit() if (watcherror) { KNotification *knotification = new KNotification("WatchError"); 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->sendEvent(); } } + +void KFreeSpaceModule::slotDeviceAdded(const QString &udi) +{ + Solid::Device soliddevice(udi); + const Solid::StorageAccess* solidaccess = soliddevice.as(); + 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(); + 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(); +} \ No newline at end of file diff --git a/kfreespace/kded/kded_kfreespace.h b/kfreespace/kded/kded_kfreespace.h index 6c19aebd..01a91adb 100644 --- a/kfreespace/kded/kded_kfreespace.h +++ b/kfreespace/kded/kded_kfreespace.h @@ -38,6 +38,9 @@ public: private Q_SLOTS: void slotInit(); + void slotDeviceAdded(const QString &udi); + void slotDeviceRemoved(const QString &udi); + void slotAccessibilityChanged(bool accessible, const QString &udi); private: KDirWatch* m_dirwatch; diff --git a/kfreespace/kded/kfreespaceimpl.cpp b/kfreespace/kded/kfreespaceimpl.cpp index e5eac2bd..0782cb5d 100644 --- a/kfreespace/kded/kfreespaceimpl.cpp +++ b/kfreespace/kded/kfreespaceimpl.cpp @@ -43,7 +43,8 @@ KFreeSpaceImpl::~KFreeSpaceImpl() } 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; m_directory = dirpath; @@ -51,6 +52,7 @@ bool KFreeSpaceImpl::watch(const QString &dirpath, 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_description = description; if (!QDir(m_directory).exists()) { kWarning() << "Directory does not exist" << m_directory; return false; @@ -76,13 +78,14 @@ void KFreeSpaceImpl::timerEvent(QTimerEvent *event) } const qulonglong freespace = kdiskinfo.available(); + const QString freespacestring = KGlobal::locale()->formatByteSize(freespace); kDebug() << "Current" << m_directory - << "space is" << KGlobal::locale()->formatByteSize(freespace); + << "space is" << freespacestring; if (freespace <= m_freespace) { KNotification *knotification = new KNotification("WatchLow"); knotification->setComponentData(KComponentData("kfreespace")); - knotification->setTitle(i18n("Disk space watch")); - knotification->setText(i18n("Low Disk Space")); + knotification->setTitle(i18n("Low Disk Space")); + knotification->setText(i18n("%1 has %2 free space", m_description, freespacestring)); knotification->sendEvent(); } } else { diff --git a/kfreespace/kded/kfreespaceimpl.h b/kfreespace/kded/kfreespaceimpl.h index 33fa2d2f..5f5f51bc 100644 --- a/kfreespace/kded/kfreespaceimpl.h +++ b/kfreespace/kded/kfreespaceimpl.h @@ -30,7 +30,8 @@ public: ~KFreeSpaceImpl(); bool watch(const QString &dirpath, - const qulonglong checktime, const qulonglong freespace); + const qulonglong checktime, const qulonglong freespace, + const QString &description); protected: // reimplementation @@ -40,6 +41,7 @@ private: QString m_directory; qulonglong m_checktime; qulonglong m_freespace; + QString m_description; int m_timerid; }; diff --git a/kfreespace/kfreespace.h b/kfreespace/kfreespace.h index 554e6306..c77d44bc 100644 --- a/kfreespace/kfreespace.h +++ b/kfreespace/kfreespace.h @@ -20,9 +20,9 @@ #define KFREESPACE_H 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_kfreespacechecktimemax = 60; // 1 minute +static const qulonglong s_kfreespacechecktimemax = 60; static const qulonglong s_kfreespacefreespace = 1024; // 1 GB static const qulonglong s_kfreespacefreespacemin = 10; static const qulonglong s_kfreespacefreespacemax = 1024;