charrunner_config.cpp 4.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Copyright 2010  Anton Kreuzkamp <akreuzkamp@web.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"

22
23
24
// KF
#include <KRunner/AbstractRunner>
#include <KSharedConfig>
25
26
// Qt
#include <QDebug>
27
28
29
30
31

//Names of config-entries
static const char CONFIG_TRIGGERWORD[] = "triggerWord";
static const char CONFIG_ALIASES[] = "aliases";
static const char CONFIG_CODES[] = "codes";
32
33
34
35
36
37

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

38
39
CharacterRunnerConfig::CharacterRunnerConfig(QWidget* parent, const QVariantList& args)
    : KCModule(parent, args)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
    m_ui = new CharacterRunnerConfigForm(this);

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

    connect(m_ui->edit_trigger, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(m_ui->addItem, SIGNAL(clicked()), this, SLOT(addItem()));
    connect(m_ui->deleteItem, SIGNAL(clicked()), this, SLOT(deleteItem()));
}

void CharacterRunnerConfig::addItem() //add Item to the list-view widget
{
  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);
57
58
59
  m_ui->edit_alias->clear();
  m_ui->edit_hex->clear();

60
61
62
63
64
65
  emit changed(true);
}

void CharacterRunnerConfig::deleteItem() //remove Item to the list-view widget
{
  m_ui->list->takeTopLevelItem(m_ui->list->indexOfTopLevelItem(m_ui->list->currentItem()));
66

67
68
69
70
71
72
73
74
  emit changed(true);
}

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

    //create config-object
75
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" ));
76
77
    KConfigGroup grp = cfg->group("Runners");
    grp = KConfigGroup(&grp, "CharacterRunner");
78
79

    m_ui->edit_trigger->setText(grp.readEntry(CONFIG_TRIGGERWORD, "#")); //read out triggerword and put into the trigger-lineEdit
80
81
82
83
84
85
86
87
88
89
90
    const auto aliasList = grp.readEntry(CONFIG_ALIASES, QList<QString>());
    const auto codeList = grp.readEntry(CONFIG_CODES, QList<QString>());
    if (aliasList.size() == codeList.size()) {
        for (int i = 0; i < aliasList.size(); ++i) { //read out aliaslist and add Items to the list-view widget
            QTreeWidgetItem* item = new QTreeWidgetItem(m_ui->list, 2);
            item->setText(0, aliasList[i]);
            item->setText(1, codeList[i]);
            m_ui->list->addTopLevelItem(item);
        }
    } else {
        qWarning() << "Config entries for alias list and code list have different sizes, ignoring all.";
91
92
93
94
95
96
97
    }
    emit changed(false);
}

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

99
    //create config-object
100
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QLatin1String( "krunnerrc" ));
101
102
    KConfigGroup grp = cfg->group("Runners");
    grp = KConfigGroup(&grp, "CharacterRunner");
103

104
    grp.writeEntry(CONFIG_TRIGGERWORD,m_ui->edit_trigger->text()); //write content from the triggerword-line Edit into the config
105

106
107
108
109
110
111
112
113
114
115
116
    //Write the content of the List into the config
    QList<QString> aliaslist;
    QList<QString> codelist;
    for(int i=0; i < m_ui->list->topLevelItemCount(); i++)
    {
      QString blub = m_ui->list->topLevelItem(i)->text(0);
      aliaslist.append(blub);
      codelist.append(m_ui->list->topLevelItem(i)->text(1));
    }
    grp.writeEntry(CONFIG_ALIASES, aliaslist);
    grp.writeEntry(CONFIG_CODES, codelist);
117

118
119
120
121
122
123
124
    emit changed(false);
}

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

125
    m_ui->edit_trigger->setText(QLatin1String( "#" )); //set the content of the triggerword-lineEdit to default '#'
126
127
128
129
130
131
132
    for(int i=0; i<m_ui->list->topLevelItemCount(); i++) //remove every item from the alias-list
    {
      m_ui->list->takeTopLevelItem(i);
    }

    emit changed(true);
}
133
134
135
136
137

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

#include "charrunner_config.moc"