[OpenDocString] kdeconnect-kde (cpp)
sendnotifications_config.cpp
SendNotificationsConfig::SendNotificationsConfig(QWidget *parent, const QVariantList &args)
    : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_sendnotifications"))
    , m_ui(new Ui::SendNotificationsConfigUi())
    , appModel(new NotifyingApplicationModel)
{
    qRegisterMetaTypeStreamOperators("NotifyingApplication");

    m_ui->setupUi(this);
    m_ui->appList->setIconSize(QSize(32, 32));

    m_ui->appList->setModel(appModel);

    m_ui->appList->horizontalHeader()->setSectionResizeMode(0, QHeaderView::QHeaderView::Fixed);
    m_ui->appList->horizontalHeader()->setSectionResizeMode(1, QHeaderView::QHeaderView::Stretch);
    m_ui->appList->horizontalHeader()->setSectionResizeMode(2, QHeaderView::QHeaderView::Stretch);
    for (int i = 0; i < 3; i++)
        m_ui->appList->resizeColumnToContents(i);

    connect(m_ui->appList->horizontalHeader(), SIGNAL(sortIndicatorChanged(int, Qt::SortOrder)), m_ui->appList, SLOT(sortByColumn(int)));

    connect(m_ui->check_persistent, SIGNAL(toggled(bool)), this, SLOT(changed()));
    connect(m_ui->spin_urgency, SIGNAL(editingFinished()), this, SLOT(changed()));
    connect(m_ui->check_body, SIGNAL(toggled(bool)), this, SLOT(changed()));
    connect(m_ui->check_icons, SIGNAL(toggled(bool)), this, SLOT(changed()));

    connect(appModel, SIGNAL(applicationsChanged()), this, SLOT(changed()));

    connect(config(), &KdeConnectPluginConfig::configChanged, this, &SendNotificationsConfig::loadApplications);
}
Sets up the send notifications plugin and sets up connections for header list and application list. It sets the icon size for the list and sets the resize mode to fixed, 1st column resize mode and 2nd column resize mode.
SendNotificationsConfig::~SendNotificationsConfig()
{
    delete m_ui;
}
Deletes the UI object upon destruction.
void SendNotificationsConfig::defaults()
{
    KCModule::defaults();
    m_ui->check_persistent->setChecked(false);
    m_ui->spin_urgency->setValue(0);
    m_ui->check_body->setChecked(true);
    m_ui->check_icons->setChecked(true);
    Q_EMIT changed(true);
}
Sets all default settings to their defaults. First it disables persistent mode, then sets the body and icon checked state to true. Then it emits the signal changed.
void SendNotificationsConfig::loadApplications()
{
    appModel->clearApplications();
    QVariantList list = config()->getList(QStringLiteral("applications"));
    for (const auto &a : list) {
        NotifyingApplication app = a.value();
        if (!appModel->containsApp(app.name)) {
            appModel->appendApp(app);
        }
    }
}
This loads the applications from the configuration.
void SendNotificationsConfig::load()
{
    KCModule::load();
    bool persistent = config()->getBool(QStringLiteral("generalPersistent"), false);
    m_ui->check_persistent->setChecked(persistent);
    bool body = config()->getBool(QStringLiteral("generalIncludeBody"), true);
    m_ui->check_body->setChecked(body);
    bool icons = config()->getBool(QStringLiteral("generalSynchronizeIcons"), true);
    m_ui->check_icons->setChecked(icons);
    int urgency = config()->getInt(QStringLiteral("generalUrgency"), 0);
    m_ui->spin_urgency->setValue(urgency);

    loadApplications();
    Q_EMIT changed(false);
}
This loads the configuration objects and sets the checkboxes for the internal state of the module. It also loads applications that are responsible for setting the checkboxes for the body and icon synchronization.
void SendNotificationsConfig::save()
{
    config()->set(QStringLiteral("generalPersistent"), m_ui->check_persistent->isChecked());
    config()->set(QStringLiteral("generalIncludeBody"), m_ui->check_body->isChecked());
    config()->set(QStringLiteral("generalSynchronizeIcons"), m_ui->check_icons->isChecked());
    config()->set(QStringLiteral("generalUrgency"), m_ui->spin_urgency->value());

    QVariantList list;
    const auto apps = appModel->apps();
    list.reserve(apps.size());
    for (const auto &a : apps) {
        list.append(QVariant::fromValue(a));
    }
    config()->setList(QStringLiteral("applications"), list);
    KCModule::save();
    Q_EMIT changed(false);
}
This saves the notification configurations to the internal configuration object. It sets general persistent and general include body, general synchronizeize icons and spin urgency.