2023-06-13 05:32:53 +03:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kded_kfreespace.h"
|
|
|
|
#include "kfreespace.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kconfiggroup.h>
|
|
|
|
#include <knotification.h>
|
|
|
|
#include <kpluginfactory.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <solid/device.h>
|
|
|
|
#include <solid/storagevolume.h>
|
|
|
|
#include <solid/storageaccess.h>
|
2023-06-13 18:59:10 +03:00
|
|
|
#include <solid/devicenotifier.h>
|
2023-06-13 05:32:53 +03:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY(KFreeSpaceModuleFactory, registerPlugin<KFreeSpaceModule>();)
|
|
|
|
K_EXPORT_PLUGIN(KFreeSpaceModuleFactory("kfreespace"))
|
|
|
|
|
|
|
|
KFreeSpaceModule::KFreeSpaceModule(QObject *parent, const QList<QVariant> &args)
|
|
|
|
: KDEDModule(parent),
|
|
|
|
m_dirwatch(nullptr)
|
|
|
|
{
|
|
|
|
Q_UNUSED(args);
|
|
|
|
|
|
|
|
slotInit();
|
|
|
|
|
|
|
|
m_dirwatch = new KDirWatch(this);
|
|
|
|
const QString kfreespacercfile = KStandardDirs::locateLocal("config", "kfreespacerc");
|
|
|
|
m_dirwatch->addFile(kfreespacercfile);
|
|
|
|
connect(m_dirwatch, SIGNAL(dirty(QString)), this, SLOT(slotInit()));
|
|
|
|
|
2023-06-13 18:59:10 +03:00
|
|
|
// 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
|
2023-06-13 05:32:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
KFreeSpaceModule::~KFreeSpaceModule()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_freespaces);
|
|
|
|
m_freespaces.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KFreeSpaceModule::slotInit()
|
|
|
|
{
|
|
|
|
kDebug() << "Initializing";
|
|
|
|
|
|
|
|
qDeleteAll(m_freespaces);
|
|
|
|
m_freespaces.clear();
|
|
|
|
|
|
|
|
KConfig kfreespaceconfig("kfreespacerc", KConfig::SimpleConfig);
|
|
|
|
bool watcherror = false;
|
|
|
|
foreach (const Solid::Device soliddevice, Solid::Device::allDevices()) {
|
|
|
|
const Solid::StorageAccess* solidaccess = soliddevice.as<Solid::StorageAccess>();
|
|
|
|
if (!solidaccess) {
|
|
|
|
continue;
|
|
|
|
} else if (solidaccess->isIgnored()) {
|
|
|
|
kDebug() << "Ignored" << soliddevice.udi();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString kfreespacedirpath = solidaccess->filePath();
|
|
|
|
if (kfreespacedirpath.isEmpty()) {
|
|
|
|
kDebug() << "Not accessible" << soliddevice.udi();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug() << Q_FUNC_INFO << soliddevice.udi();
|
|
|
|
KConfigGroup kfreespacegroup = kfreespaceconfig.group(soliddevice.udi());
|
|
|
|
const bool kfreespacewatch = kfreespacegroup.readEntry("watch", s_kfreespacewatch);
|
|
|
|
if (!kfreespacewatch) {
|
|
|
|
kDebug() << "Not watching" << soliddevice.udi();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-13 18:05:26 +03:00
|
|
|
const qulonglong kfreespacechecktime = kfreespacegroup.readEntry("checktime", s_kfreespacechecktime);
|
|
|
|
const qulonglong kfreespacefreespace = kfreespacegroup.readEntry("freespace", s_kfreespacefreespace);
|
2023-06-13 05:32:53 +03:00
|
|
|
KFreeSpaceImpl* kfreespaceimpl = new KFreeSpaceImpl(this);
|
|
|
|
const bool kfreespacestatus = kfreespaceimpl->watch(
|
|
|
|
kfreespacedirpath,
|
2023-06-13 18:59:10 +03:00
|
|
|
kfreespacechecktime, kfreespacefreespace,
|
|
|
|
soliddevice.description()
|
2023-06-13 05:32:53 +03:00
|
|
|
);
|
|
|
|
if (!kfreespacestatus) {
|
|
|
|
delete kfreespaceimpl;
|
|
|
|
watcherror = true;
|
|
|
|
} else {
|
|
|
|
m_freespaces.append(kfreespaceimpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (watcherror) {
|
|
|
|
KNotification *knotification = new KNotification("WatchError");
|
|
|
|
knotification->setComponentData(KComponentData("kfreespace"));
|
2023-06-13 18:59:10 +03:00
|
|
|
knotification->setTitle(i18n("Free Space Notifier"));
|
2023-06-13 05:32:53 +03:00
|
|
|
knotification->setText(i18n("Unable to watch one or more devices"));
|
|
|
|
knotification->sendEvent();
|
|
|
|
}
|
|
|
|
}
|
2023-06-13 18:59:10 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|