summaryrefslogtreecommitdiffstats
path: root/config.cpp
blob: e342161ff8c7f5a42595ca9bae53e0a32bbed9cf (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/******************************************************************************
 *  Copyright (C) 2017 by Lukas Fürmetz <fuermetz@mailbox.org>                *
 *                                                                            *
 *  This library 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 3 of the License or (at   *
 *  your option) any later version.                                           *
 *                                                                            *
 *  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         *
 *  Library General Public License for more details.                          *
 *                                                                            *
 *  You should have received a copy of the GNU General Public License         *
 *  along with this library; see the file LICENSE.                            *
 *  If not, see <http://www.gnu.org/licenses/>.                               *
 *****************************************************************************/

#include <KSharedConfig>
#include <KPluginFactory>
#include <krunner/abstractrunner.h>
#include <QToolButton>

#include "config.h"

K_PLUGIN_FACTORY(PassConfigFactory, registerPlugin<PassConfig>("kcm_krunner_pass");)



PassConfigForm::PassConfigForm(QWidget *parent) : QWidget(parent)
{
    setupUi(this);
    this->listSavedActions->setDragEnabled(true);
    this->listSavedActions->setDragDropMode(QAbstractItemView::InternalMove);
    this->listSavedActions->setSelectionMode(QAbstractItemView::SingleSelection);

    connect(this->checkAdditionalActions, &QCheckBox::stateChanged, [this](int state) {
        Q_UNUSED(state)

        bool isChecked = this->checkAdditionalActions->isChecked();
        this->boxNewAction->setEnabled(isChecked);
        this->boxSavedActions->setEnabled(isChecked);
        this->checkShowFileContentAction->setEnabled(isChecked);
    });

    connect(this->buttonAddAction, &QPushButton::clicked, [this]() {
        this->addPassAction(this->lineName->text(), this->lineIcon->text(), this->lineRegEx->text());
    });
}

void PassConfigForm::addPassAction(const QString &name, const QString &icon, const QString &regex, bool isNew /* = true */)
{
    // Checks
    for (auto act: this->passActions())
        if (act.name == name)
            return;

    if (name.isEmpty() || icon.isEmpty() || regex.isEmpty())
        return;

    // Widgets
    auto *listWidget = new QWidget(this);
    auto *layoutAction = new QHBoxLayout(listWidget);
    auto *buttonRemoveAction = new QToolButton(listWidget);

    buttonRemoveAction->setIcon(QIcon::fromTheme("remove"));
    layoutAction->setMargin(0);
    layoutAction->addStretch();
    layoutAction->addWidget(buttonRemoveAction);
    listWidget->setLayout(layoutAction);

    // Item
    auto *item = new QListWidgetItem(name + (isNew ? "*":""), this->listSavedActions);
    item->setData(Qt::UserRole, QVariant::fromValue(PassAction {name, icon, regex}));
    this->listSavedActions->setItemWidget(item, listWidget);

    this->clearInputs();
    emit passActionAdded();

    // Remove
    connect(buttonRemoveAction, &QToolButton::clicked, [=](bool clicked) {
        Q_UNUSED(clicked)

        delete this->listSavedActions->takeItem(this->listSavedActions->row(item));
        delete listWidget;

        emit passActionRemoved();
    });
}

QVector<PassAction> PassConfigForm::passActions()
{
    QVector<PassAction> passActions;
    for(int i = 0; i < this->listSavedActions->count(); ++i) {
        QListWidgetItem* item = this->listSavedActions->item(i);
        passActions << item->data(Qt::UserRole).value<PassAction>();
    }
    return passActions;
}

void PassConfigForm::clearPassActions()
{
    for(int i = 0; i < this->listSavedActions->count(); ++i) {
        QListWidgetItem* item = this->listSavedActions->item(i);
        delete this->listSavedActions->itemWidget(item);
    }

    this->listSavedActions->clear();
}

void PassConfigForm::clearInputs()
{
    this->lineIcon->clear();
    this->lineName->clear();
    this->lineRegEx->clear();
}


PassConfig::PassConfig(QWidget *parent, const QVariantList &args) :
        KCModule(parent, args)
{
    this->ui = new PassConfigForm(this);
    QGridLayout* layout = new QGridLayout(this);
    layout->addWidget(ui, 0, 0);

    load();

    connect(this->ui,SIGNAL(passActionAdded()),this,SLOT(changed()));
    connect(this->ui,SIGNAL(passActionRemoved()),this,SLOT(changed()));
    connect(this->ui->checkAdditionalActions,SIGNAL(stateChanged(int)),this,SLOT(changed()));
    connect(this->ui->checkShowFileContentAction,SIGNAL(stateChanged(int)),this,SLOT(changed()));
    connect(this->ui->listSavedActions,SIGNAL(itemSelectionChanged()), this, SLOT(changed()));
}

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

    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
    KConfigGroup passCfg = cfg->group("Runners");
    passCfg = KConfigGroup(&passCfg, "Pass");


    bool showActions = passCfg.readEntry(Config::showActions, false);
    bool showFileContentAction = passCfg.readEntry(Config::showFileContentAction, false);

    this->ui->checkAdditionalActions->setChecked(showActions);
    this->ui->checkShowFileContentAction->setChecked(showFileContentAction);

    // Load saved actions
    this->ui->clearPassActions();

    auto actionGroup = passCfg.group(Config::Group::Actions);
    auto groups = actionGroup.groupList();
    Q_FOREACH (auto name, groups) {
        auto group = actionGroup.group(name);
        auto passAction = PassAction::fromConfig(group);

        this->ui->addPassAction(passAction.name, passAction.icon, passAction.regex, false);
    }

    emit changed(false);
}

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

    KSharedConfig::Ptr cfg = KSharedConfig::openConfig(QStringLiteral("krunnerrc"));
    KConfigGroup passCfg = cfg->group("Runners");
    passCfg = KConfigGroup(&passCfg, "Pass");


    auto showActions = this->ui->checkAdditionalActions->isChecked();
    auto showFileContentAction =  this->ui->checkShowFileContentAction->isChecked();

    passCfg.writeEntry(Config::showActions, showActions);
    passCfg.writeEntry(Config::showFileContentAction, showFileContentAction);


    passCfg.deleteGroup(Config::Group::Actions);

    if (showActions) {
        int i = 0;
        for (PassAction act: this->ui->passActions()) {
            auto group = passCfg.group(Config::Group::Actions).group(QString::number(i++));
            act.writeToConfig(group);
        }
    }

    emit changed(false);
}

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

    ui->checkAdditionalActions->setChecked(false);
    ui->checkShowFileContentAction->setChecked(false);
    ui->clearPassActions();
    ui->clearInputs();

    emit changed(true);
}


#include "config.moc"