/***************************************************************************
 *   Copyright © 2009 Jonathan Thomas <echidnaman@kubuntu.org>             *
 *   Copyright © 2026 Erich Eickmeyer <eeickmeyer@ubuntu.com>              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "notificationhelperconfigmodule.h"

// Qt includes
#include <QButtonGroup>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QCheckBox>
#include <QGroupBox>
#include <QLabel>
#include <QRadioButton>
#include <QVBoxLayout>

// KDE includes
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KPluginFactory>

#warning oh god the class name

K_PLUGIN_FACTORY_WITH_JSON(NotificationHelperConfigFactory,
                           "kcm_notificationhelper.json",
                           registerPlugin<NotificationHelperConfigModule>();)

NotificationHelperConfigModule::NotificationHelperConfigModule(QObject *parent, const KPluginMetaData &data)
    : KCModule(parent, data)
    , m_apportCheckBox(nullptr)
    , m_driverCheckBox(nullptr)
    , m_hookCheckBox(nullptr)
    , m_installCheckBox(nullptr)
    , m_l10nCheckBox(nullptr)
    , m_rebootCheckBox(nullptr)
{
    setButtons(Apply);

    KConfig kdedrc(QStringLiteral("notificationhelper"), KConfig::NoGlobals);

    QVBoxLayout *lay = new QVBoxLayout(widget());

    QLabel *label = new QLabel(widget());
    label->setText(i18n("Show notifications for:"));

    m_apportCheckBox = new QCheckBox(i18n("Application crashes"), widget());
    m_driverCheckBox = new QCheckBox(i18n("Proprietary Driver availability"), widget());
    m_hookCheckBox = new QCheckBox(i18n("Upgrade information"), widget());
    m_installCheckBox = new QCheckBox(i18n("Restricted codec availability"), widget());
    m_l10nCheckBox = new QCheckBox(i18n("Incomplete language support"), widget());
    m_rebootCheckBox = new QCheckBox(i18n("Required reboots"), widget());

    connect(m_apportCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_driverCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_hookCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_installCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_l10nCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_rebootCheckBox, &QCheckBox::clicked, this, &NotificationHelperConfigModule::configChanged);

    QWidget *spacer = new QWidget(widget());

    QLabel *label2 = new QLabel(widget());
    label2->setText(i18n("Notification type:"));

    QButtonGroup *notifyTypeGroup = new QButtonGroup(this);
    m_comboRadio = new QRadioButton(widget());
    m_comboRadio->setText(i18n("Use both popups and tray icons"));
    m_disableKNotifyRadio = new QRadioButton(widget());
    m_disableKNotifyRadio->setText(i18n("Tray icons only"));
    m_disableTrayIconRadio = new QRadioButton(widget());
    m_disableTrayIconRadio->setText(i18n("Popup notifications only"));

    notifyTypeGroup->addButton(m_comboRadio);
    notifyTypeGroup->addButton(m_disableKNotifyRadio);
    notifyTypeGroup->addButton(m_disableTrayIconRadio);

    connect(m_comboRadio, &QRadioButton::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_disableKNotifyRadio, &QRadioButton::clicked, this, &NotificationHelperConfigModule::configChanged);
    connect(m_disableTrayIconRadio, &QRadioButton::clicked, this, &NotificationHelperConfigModule::configChanged);

    lay->addWidget(label);
    lay->addWidget(m_apportCheckBox);
    lay->addWidget(m_driverCheckBox);
    lay->addWidget(m_hookCheckBox);
    lay->addWidget(m_installCheckBox);
    lay->addWidget(m_l10nCheckBox);
    lay->addWidget(m_rebootCheckBox);
    lay->addWidget(spacer);
    lay->addWidget(label2);
    lay->addWidget(m_comboRadio);
    lay->addWidget(m_disableKNotifyRadio);
    lay->addWidget(m_disableTrayIconRadio);
    lay->addStretch();
}

NotificationHelperConfigModule::~NotificationHelperConfigModule()
{
}

void NotificationHelperConfigModule::load()
{
    KConfig cfg(QStringLiteral("notificationhelper"), KConfig::NoGlobals);
    KConfigGroup notifyGroup(&cfg, QStringLiteral("Event"));
    m_apportCheckBox->setChecked(!notifyGroup.readEntry("hideApportNotifier", false));
    m_driverCheckBox->setChecked(!notifyGroup.readEntry("hideDriverNotifier", false));
    m_hookCheckBox->setChecked(!notifyGroup.readEntry("hideHookNotifier", false));
    m_installCheckBox->setChecked(!notifyGroup.readEntry("hideInstallNotifier", false));
    m_l10nCheckBox->setChecked(!notifyGroup.readEntry("hideL10nNotifier", false));
    m_rebootCheckBox->setChecked(!notifyGroup.readEntry("hideRestartNotifier", false));

    KConfigGroup notifyTypeGroup(&cfg, QStringLiteral("NotificationType"));
    QString notifyType = notifyTypeGroup.readEntry("NotifyType", "Combo");

    if (notifyType == QLatin1String("Combo")) {
        m_comboRadio->setChecked(true);
    } else if (notifyType == QLatin1String("TrayOnly")) {
        m_disableKNotifyRadio->setChecked(true);
    } else {
        m_disableTrayIconRadio->setChecked(true);
    }
}

void NotificationHelperConfigModule::save()
{
    KConfig cfg(QStringLiteral("notificationhelper"), KConfig::NoGlobals);
    KConfigGroup notifyGroup(&cfg, QStringLiteral("Event"));

    notifyGroup.writeEntry("hideApportNotifier", !m_apportCheckBox->isChecked());
    notifyGroup.writeEntry("hideDriverNotifier", !m_driverCheckBox->isChecked());
    notifyGroup.writeEntry("hideHookNotifier", !m_hookCheckBox->isChecked());
    notifyGroup.writeEntry("hideInstallNotifier", !m_installCheckBox->isChecked());
    notifyGroup.writeEntry("hideL10nNotifier", !m_l10nCheckBox->isChecked());
    notifyGroup.writeEntry("hideRestartNotifier", !m_rebootCheckBox->isChecked());

    KConfigGroup notifyTypeGroup(&cfg, QStringLiteral("NotificationType"));
    notifyTypeGroup.writeEntry("NotifyType", m_comboRadio->isChecked() ? "Combo" :
                               m_disableKNotifyRadio->isChecked() ? "TrayOnly" :
                               m_disableTrayIconRadio->isChecked() ? "KNotifyOnly" :
                               "Combo");

    cfg.sync();
    notifyGroup.sync();

    QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kubuntu.NotificationHelper"),
                           QStringLiteral("/NotificationHelper"),
                           QStringLiteral("org.kubuntu.NotificationHelper"),
                           QStringLiteral("reloadConfig"));
    QDBusConnection::sessionBus().send(message);
}

void NotificationHelperConfigModule::defaults()
{
    m_apportCheckBox->setChecked(true);
    m_driverCheckBox->setChecked(true);
    m_hookCheckBox->setChecked(true);
    m_installCheckBox->setChecked(true);
    m_l10nCheckBox->setChecked(true);
    m_rebootCheckBox->setChecked(true);
}

void NotificationHelperConfigModule::configChanged()
{
  markAsChanged();
}

#include "notificationhelperconfigmodule.moc"
