File: charrunner_config.cpp

package info (click to toggle)
kdeplasma-addons 4%3A5.20.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,588 kB
  • sloc: cpp: 11,342; xml: 402; javascript: 150; sh: 56; makefile: 26
file content (137 lines) | stat: -rw-r--r-- 5,241 bytes parent folder | download
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
/* Copyright 2010  Anton Kreuzkamp <akreuzkamp@web.de>
 * Copyright 2020  Alexander Lohnau <alexander.lohnau@gmx.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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 6 of version 3 of the license.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "charrunner_config.h"
#include "config_keys.h"

// KF
#include <KRunner/AbstractRunner>
#include <KSharedConfig>
#include <KMessageWidget>
#include <KLocalizedString>

CharacterRunnerConfigForm::CharacterRunnerConfigForm(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);
}

CharacterRunnerConfig::CharacterRunnerConfig(QWidget *parent, const QVariantList &args)
    : KCModule(parent, args)
{
    m_ui = new CharacterRunnerConfigForm(this);

    QGridLayout *layout = new QGridLayout(this);
    layout->addWidget(m_ui, 0, 0);

    connect(m_ui->edit_trigger, &QLineEdit::textChanged, this, &CharacterRunnerConfig::markAsChanged);
    connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::addItem);
    connect(m_ui->addItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged);
    connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::deleteItem);
    connect(m_ui->deleteItem, &QPushButton::clicked, this, &CharacterRunnerConfig::markAsChanged);
    connect(m_ui->list, &QTreeWidget::itemSelectionChanged, this, &CharacterRunnerConfig::validateDeleteButton);
    connect(m_ui->edit_alias, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton);
    connect(m_ui->edit_hex, &QLineEdit::textChanged, this, &CharacterRunnerConfig::validateAddButton);
}

void CharacterRunnerConfig::load()
{
    KCModule::load();

    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
    KConfigGroup grp = cfg->group("Runners").group("CharacterRunner");

    m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, DEFAULT_TRIGGERWORD.toString()));
    const auto aliasList = grp.readEntry(CONFIG_ALIASES, QStringList());
    const auto codeList = grp.readEntry(CONFIG_CODES, QStringList());
    if (aliasList.size() == codeList.size()) {
        for (int i = 0; i < aliasList.size(); ++i) {
            QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2);
            item->setText(0, aliasList[i]);
            item->setText(1, codeList[i]);
            m_ui->list->addTopLevelItem(item);
        }
    } else {
        const auto msg = new KMessageWidget(i18nc("Message that config is corrupted",
            "Config entries for alias list and code list have different sizes, ignoring all."), this);
        m_ui->verticalLayout->insertWidget(0, msg);
    }
}

void CharacterRunnerConfig::save()
{
    KCModule::save();

    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
    KConfigGroup grp = cfg->group("Runners").group("CharacterRunner");

    grp.writeEntry(CONFIG_TRIGGERWORD, m_ui->edit_trigger->text().isEmpty()
                                       ? DEFAULT_TRIGGERWORD.toString() : m_ui->edit_trigger->text());

    QList<QString> aliasList;
    QList<QString> codeList;
    for (int i = 0; i < m_ui->list->topLevelItemCount(); ++i) {
        aliasList.append(m_ui->list->topLevelItem(i)->text(0));
        codeList.append(m_ui->list->topLevelItem(i)->text(1));
    }
    grp.writeEntry(CONFIG_ALIASES, aliasList);
    grp.writeEntry(CONFIG_CODES, codeList);
    grp.sync();
}

void CharacterRunnerConfig::defaults()
{
    KCModule::defaults();

    m_ui->edit_trigger->setText(DEFAULT_TRIGGERWORD.toString());
    m_ui->list->clear();

    emit markAsChanged();
}

void CharacterRunnerConfig::addItem()
{
    QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->list, 2);
    item->setText(0, m_ui->edit_alias->text());
    item->setText(1, m_ui->edit_hex->text());
    m_ui->list->addTopLevelItem(item);
    m_ui->edit_alias->clear();
    m_ui->edit_hex->clear();
}

void CharacterRunnerConfig::deleteItem()
{
    m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem()));
}

void CharacterRunnerConfig::validateAddButton()
{
    m_ui->addItem->setDisabled(m_ui->edit_alias->text().isEmpty() || m_ui->edit_hex->text().isEmpty());
}

void CharacterRunnerConfig::validateDeleteButton()
{
    m_ui->deleteItem->setDisabled(!m_ui->list->selectedItems().count());
}

K_PLUGIN_FACTORY(CharacterRunnerConfigFactory,
                 registerPlugin<CharacterRunnerConfig>(QStringLiteral("kcm_krunner_charrunner"));)

#include "charrunner_config.moc"