File indexing completed on 2024-04-21 05:34:59

0001 /*
0002     SPDX-FileCopyrightText: 2006 Lukas Tinkl <ltinkl@suse.cz>
0003     SPDX-FileCopyrightText: 2008 Lubos Lunak <l.lunak@suse.cz>
0004     SPDX-FileCopyrightText: 2009 Ivo Anjo <knuckles@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "module.h"
0010 
0011 #include <KConfigDialog>
0012 #include <KMountPoint>
0013 #include <KPluginFactory>
0014 
0015 #include <QDir>
0016 
0017 #include "kded_interface.h"
0018 
0019 #include "ui_freespacenotifier_prefs_base.h"
0020 
0021 #include "settings.h"
0022 
0023 K_PLUGIN_CLASS_WITH_JSON(FreeSpaceNotifierModule, "freespacenotifier.json")
0024 
0025 FreeSpaceNotifierModule::FreeSpaceNotifierModule(QObject *parent, const QList<QVariant> &)
0026     : KDEDModule(parent)
0027 {
0028     // If the module is loaded, notifications are enabled
0029     FreeSpaceNotifierSettings::setEnableNotification(true);
0030 
0031     const QString rootPath = QStringLiteral("/");
0032     const QString homePath = QDir::homePath();
0033 
0034     const auto homeMountPoint = KMountPoint::currentMountPoints().findByPath(homePath);
0035 
0036     if (!homeMountPoint || !homeMountPoint->mountOptions().contains(QLatin1String("ro"))) {
0037         auto *homeNotifier = new FreeSpaceNotifier(homePath, ki18n("Your Home folder is running out of disk space, you have %1 MiB remaining (%2%)."), this);
0038         connect(homeNotifier, &FreeSpaceNotifier::configureRequested, this, &FreeSpaceNotifierModule::showConfiguration);
0039     }
0040 
0041     // If Home is on a separate partition from Root, warn for it, too.
0042     if (KMountPoint::Ptr rootMountPoint; !homeMountPoint
0043         || (homeMountPoint->mountPoint() != rootPath
0044             && (!(rootMountPoint = KMountPoint::currentMountPoints().findByPath(rootPath)) || !rootMountPoint->mountOptions().contains(QLatin1String("ro"))))) {
0045         auto *rootNotifier = new FreeSpaceNotifier(rootPath, ki18n("Your Root partition is running out of disk space, you have %1 MiB remaining (%2%)."), this);
0046         connect(rootNotifier, &FreeSpaceNotifier::configureRequested, this, &FreeSpaceNotifierModule::showConfiguration);
0047     }
0048 }
0049 
0050 void FreeSpaceNotifierModule::showConfiguration()
0051 {
0052     if (KConfigDialog::showDialog(QStringLiteral("settings"))) {
0053         return;
0054     }
0055 
0056     KConfigDialog *dialog = new KConfigDialog(nullptr, QStringLiteral("settings"), FreeSpaceNotifierSettings::self());
0057     QWidget *generalSettingsDlg = new QWidget();
0058 
0059     Ui::freespacenotifier_prefs_base preferences;
0060     preferences.setupUi(generalSettingsDlg);
0061 
0062     dialog->addPage(generalSettingsDlg, i18nc("The settings dialog main page name, as in 'general settings'", "General"), QStringLiteral("system-run"));
0063 
0064     connect(dialog, &KConfigDialog::finished, this, [] {
0065         if (!FreeSpaceNotifierSettings::enableNotification()) {
0066             // The idea here is to disable ourselves by telling kded to stop autostarting us, and
0067             // to kill the current running instance.
0068             org::kde::kded6 kded(QStringLiteral("org.kde.kded6"), QStringLiteral("/kded"), QDBusConnection::sessionBus());
0069             kded.setModuleAutoloading(QStringLiteral("freespacenotifier"), false);
0070             kded.unloadModule(QStringLiteral("freespacenotifier"));
0071         }
0072     });
0073 
0074     dialog->setAttribute(Qt::WA_DeleteOnClose);
0075     dialog->show();
0076 }
0077 
0078 #include "module.moc"