Changeset View
Changeset View
Standalone View
Standalone View
freespacenotifier/freespacenotifier.cpp
Show All 21 Lines | |||||
22 | #include <QDir> | 22 | #include <QDir> | ||
23 | #include <QMenu> | 23 | #include <QMenu> | ||
24 | #include <QDBusInterface> | 24 | #include <QDBusInterface> | ||
25 | #include <QDebug> | 25 | #include <QDebug> | ||
26 | 26 | | |||
27 | #include <KLocalizedString> | 27 | #include <KLocalizedString> | ||
28 | #include <KRun> | 28 | #include <KRun> | ||
29 | #include <KConfigDialog> | 29 | #include <KConfigDialog> | ||
30 | #include <KDiskFreeSpaceInfo> | | |||
31 | #include <KStatusNotifierItem> | 30 | #include <KStatusNotifierItem> | ||
32 | #include <KNotification> | 31 | #include <KNotification> | ||
33 | 32 | | |||
33 | #include <KIO/FileSystemFreeSpaceJob> | ||||
34 | | ||||
34 | #include "settings.h" | 35 | #include "settings.h" | ||
35 | #include "ui_freespacenotifier_prefs_base.h" | 36 | #include "ui_freespacenotifier_prefs_base.h" | ||
36 | 37 | | |||
37 | FreeSpaceNotifier::FreeSpaceNotifier(QObject *parent) | 38 | FreeSpaceNotifier::FreeSpaceNotifier(QObject *parent) | ||
38 | : QObject(parent) | 39 | : QObject(parent) | ||
39 | , m_lastAvailTimer(nullptr) | 40 | , m_lastAvailTimer(nullptr) | ||
40 | , m_notification(nullptr) | 41 | , m_notification(nullptr) | ||
41 | , m_sni(nullptr) | 42 | , m_sni(nullptr) | ||
Show All 24 Lines | 66 | { | |||
66 | if (!FreeSpaceNotifierSettings::enableNotification()) { | 67 | if (!FreeSpaceNotifierSettings::enableNotification()) { | ||
67 | // do nothing if notifying is disabled; | 68 | // do nothing if notifying is disabled; | ||
68 | // also stop the timer that probably got us here in the first place | 69 | // also stop the timer that probably got us here in the first place | ||
69 | timer.stop(); | 70 | timer.stop(); | ||
70 | 71 | | |||
71 | return; | 72 | return; | ||
72 | } | 73 | } | ||
73 | 74 | | |||
74 | KDiskFreeSpaceInfo fsInfo = KDiskFreeSpaceInfo::freeSpaceInfo(QDir::homePath()); | 75 | auto *job = KIO::fileSystemFreeSpace(QUrl::fromLocalFile(QDir::homePath())); | ||
75 | if (fsInfo.isValid()) { | 76 | connect(job, &KIO::FileSystemFreeSpaceJob::result, this, [this](KIO::Job* job, KIO::filesize_t size, KIO::filesize_t available) { | ||
77 | if (job->error()) { | ||||
78 | return; | ||||
79 | } | ||||
80 | | ||||
76 | int limit = FreeSpaceNotifierSettings::minimumSpace(); // MiB | 81 | int limit = FreeSpaceNotifierSettings::minimumSpace(); // MiB | ||
77 | qint64 avail = fsInfo.available() / (1024 * 1024); // to MiB | 82 | qint64 avail = available / (1024 * 1024); // to MiB | ||
78 | bool warn = false; | 83 | bool warn = false; | ||
79 | 84 | | |||
80 | if (avail < limit) { | 85 | if (avail < limit) { | ||
81 | // avail disk space dropped under a limit | 86 | // avail disk space dropped under a limit | ||
82 | if (m_lastAvail < 0 || avail < m_lastAvail / 2) { // always warn the first time or when available dropped to a half of previous one, warn again | 87 | if (m_lastAvail < 0 || avail < m_lastAvail / 2) { // always warn the first time or when available dropped to a half of previous one, warn again | ||
83 | m_lastAvail = avail; | 88 | m_lastAvail = avail; | ||
84 | warn = true; | 89 | warn = true; | ||
85 | } else if (avail > m_lastAvail) { // the user freed some space | 90 | } else if (avail > m_lastAvail) { // the user freed some space | ||
86 | m_lastAvail = avail; // so warn if it goes low again | 91 | m_lastAvail = avail; // so warn if it goes low again | ||
87 | if (m_sni) { | 92 | if (m_sni) { | ||
88 | // keep the SNI active, but don't blink | 93 | // keep the SNI active, but don't blink | ||
89 | m_sni->setStatus(KStatusNotifierItem::Active); | 94 | m_sni->setStatus(KStatusNotifierItem::Active); | ||
90 | m_sni->setToolTip(QStringLiteral("drive-harddisk"), i18n("Low Disk Space"), i18n("Remaining space in your Home folder: %1 MiB", QLocale::system().toString(avail))); | 95 | m_sni->setToolTip(QStringLiteral("drive-harddisk"), i18n("Low Disk Space"), i18n("Remaining space in your Home folder: %1 MiB", QLocale::system().toString(avail))); | ||
91 | } | 96 | } | ||
92 | } | 97 | } | ||
93 | // do not change lastAvail otherwise, to handle free space slowly going down | 98 | // do not change lastAvail otherwise, to handle free space slowly going down | ||
94 | 99 | | |||
95 | if (warn) { | 100 | if (warn) { | ||
96 | int availpct = int(100 * fsInfo.available() / fsInfo.size()); | 101 | int availpct = int(100 * available / size); | ||
97 | if (!m_sni) { | 102 | if (!m_sni) { | ||
98 | m_sni = new KStatusNotifierItem(QStringLiteral("freespacenotifier")); | 103 | m_sni = new KStatusNotifierItem(QStringLiteral("freespacenotifier")); | ||
99 | m_sni->setIconByName(QStringLiteral("drive-harddisk")); | 104 | m_sni->setIconByName(QStringLiteral("drive-harddisk")); | ||
100 | m_sni->setOverlayIconByName(QStringLiteral("dialog-warning")); | 105 | m_sni->setOverlayIconByName(QStringLiteral("dialog-warning")); | ||
101 | m_sni->setTitle(i18n("Low Disk Space")); | 106 | m_sni->setTitle(i18n("Low Disk Space")); | ||
102 | m_sni->setCategory(KStatusNotifierItem::Hardware); | 107 | m_sni->setCategory(KStatusNotifierItem::Hardware); | ||
103 | 108 | | |||
104 | QMenu *sniMenu = new QMenu(); | 109 | QMenu *sniMenu = new QMenu(); | ||
Show All 28 Lines | |||||
133 | } | 138 | } | ||
134 | } else { | 139 | } else { | ||
135 | // free space is above limit again, remove the SNI | 140 | // free space is above limit again, remove the SNI | ||
136 | if (m_sni) { | 141 | if (m_sni) { | ||
137 | m_sni->deleteLater(); | 142 | m_sni->deleteLater(); | ||
138 | m_sni = nullptr; | 143 | m_sni = nullptr; | ||
139 | } | 144 | } | ||
140 | } | 145 | } | ||
141 | } | 146 | }); | ||
142 | } | 147 | } | ||
143 | 148 | | |||
144 | void FreeSpaceNotifier::hideSni() | 149 | void FreeSpaceNotifier::hideSni() | ||
145 | { | 150 | { | ||
146 | if (m_sni) { | 151 | if (m_sni) { | ||
147 | m_sni->setStatus(KStatusNotifierItem::Passive); | 152 | m_sni->setStatus(KStatusNotifierItem::Passive); | ||
148 | QAction *action = qobject_cast<QAction*>(sender()); | 153 | QAction *action = qobject_cast<QAction*>(sender()); | ||
149 | if (action) { | 154 | if (action) { | ||
▲ Show 20 Lines • Show All 105 Lines • Show Last 20 Lines |