1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* SPDX-FileCopyrightText: 2024 Bohdan Onofriichuk <bogdan.onofriuchuk@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "spacemonitor_p.h"
#include "devicenotifier_debug.h"
#include <Solid/Device>
#include <Solid/DeviceInterface>
#include <Solid/StorageAccess>
#include <KIO/FileSystemFreeSpaceJob>
#include <QTimer>
#include <QUrl>
SpaceMonitor::SpaceMonitor(QObject *parent)
: QObject(parent)
, m_spaceWatcher(new QTimer(this))
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Begin initializing Space Monitor";
m_spaceWatcher->setSingleShot(true);
m_spaceWatcher->setInterval(std::chrono::minutes(1));
connect(m_spaceWatcher, &QTimer::timeout, this, &SpaceMonitor::updateAllStorageSpaces);
m_stateMonitor = DevicesStateMonitor::instance();
connect(m_stateMonitor.get(), &DevicesStateMonitor::stateChanged, this, &SpaceMonitor::deviceStateChanged);
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor initialized";
}
SpaceMonitor::~SpaceMonitor()
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor was removed";
m_spaceWatcher->stop();
}
std::shared_ptr<SpaceMonitor> SpaceMonitor::instance()
{
static std::weak_ptr<SpaceMonitor> s_clip;
if (s_clip.expired()) {
std::shared_ptr<SpaceMonitor> ptr{new SpaceMonitor};
s_clip = ptr;
return ptr;
}
return s_clip.lock();
}
double SpaceMonitor::getFullSize(const QString &udi) const
{
if (auto it = m_sizes.constFind(udi); it != m_sizes.constEnd()) {
return it->first;
}
return -1;
}
double SpaceMonitor::getFreeSize(const QString &udi) const
{
if (auto it = m_sizes.constFind(udi); it != m_sizes.constEnd()) {
return it->second;
}
return -1;
}
void SpaceMonitor::setIsVisible(bool status)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: is Visible changed to " << status;
if (status) {
m_spaceWatcher->setSingleShot(false);
if (!m_spaceWatcher->isActive()) {
updateAllStorageSpaces();
m_spaceWatcher->start();
}
} else {
m_spaceWatcher->setSingleShot(true);
}
}
void SpaceMonitor::addMonitoringDevice(const QString &udi)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: Adding new device " << udi;
updateStorageSpace(udi);
}
void SpaceMonitor::removeMonitoringDevice(const QString &udi)
{
if (auto it = m_sizes.find(udi); it != m_sizes.end()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: remove device " << udi;
m_sizes.remove(udi);
Q_EMIT sizeChanged(udi);
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: device " << udi << " not found";
}
}
void SpaceMonitor::forceUpdateSize(const QString &udi)
{
if (auto it = m_sizes.find(udi); it != m_sizes.end()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: forced to update size for device " << udi;
updateStorageSpace(udi);
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: device " << udi << " not found";
}
}
void SpaceMonitor::deviceStateChanged(QString udi)
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: device state changed! Force updating space";
updateStorageSpace(udi);
}
void SpaceMonitor::updateAllStorageSpaces()
{
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: Timer is out. Begin updating all storages status ";
if (m_sizes.isEmpty()) {
return;
}
QHashIterator it(m_sizes);
while (it.hasNext()) {
it.next();
updateStorageSpace(it.key());
}
}
void SpaceMonitor::updateStorageSpace(const QString &udi)
{
Solid::Device device(udi);
Solid::StorageAccess *storageaccess = device.as<Solid::StorageAccess>();
if (!storageaccess || !storageaccess->isAccessible()) {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: failed to get storage access " << udi;
m_sizes[udi].first = -1;
m_sizes[udi].second = -1;
Q_EMIT sizeChanged(udi);
return;
}
QString path = storageaccess->filePath();
// create job
KIO::FileSystemFreeSpaceJob *job = KIO::fileSystemFreeSpace(QUrl::fromLocalFile(path));
// collect and process info
connect(job, &KJob::result, this, [this, udi, job]() {
if (!job->error()) {
KIO::filesize_t size = job->size();
KIO::filesize_t available = job->availableSize();
m_sizes[udi] = {size, available};
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: storage space update finished for " << udi << "Space: " << size << "FreeSpace: " << available;
Q_EMIT sizeChanged(udi);
} else {
qCDebug(APPLETS::DEVICENOTIFIER) << "Space Monitor: Failed to get size for : " << udi;
}
});
}
#include "moc_spacemonitor_p.cpp"
|