2007-04-29 17:35:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
|
|
|
|
*
|
|
|
|
* 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) any later version.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ruleslist.h"
|
|
|
|
|
2013-11-01 11:29:05 +00:00
|
|
|
#include <QDebug>
|
2007-04-29 17:35:43 +00:00
|
|
|
#include <kconfig.h>
|
2013-09-30 06:43:09 +00:00
|
|
|
#include <QFileDialog>
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2020-02-18 13:52:08 +00:00
|
|
|
#include "../../rules.h"
|
|
|
|
#include "rulesettings.h"
|
2007-04-29 17:35:43 +00:00
|
|
|
#include "ruleswidget.h"
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
KCMRulesList::KCMRulesList(QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
2009-05-12 15:00:07 +00:00
|
|
|
// connect both current/selected, so that current==selected (stupid QListBox :( )
|
2011-01-30 14:34:42 +00:00
|
|
|
connect(rules_listbox, SIGNAL(itemChanged(QListWidgetItem*)),
|
|
|
|
SLOT(activeChanged()));
|
|
|
|
connect(rules_listbox, SIGNAL(itemSelectionChanged()),
|
|
|
|
SLOT(activeChanged()));
|
|
|
|
connect(new_button, SIGNAL(clicked()),
|
|
|
|
SLOT(newClicked()));
|
|
|
|
connect(modify_button, SIGNAL(clicked()),
|
|
|
|
SLOT(modifyClicked()));
|
|
|
|
connect(delete_button, SIGNAL(clicked()),
|
|
|
|
SLOT(deleteClicked()));
|
|
|
|
connect(moveup_button, SIGNAL(clicked()),
|
|
|
|
SLOT(moveupClicked()));
|
|
|
|
connect(movedown_button, SIGNAL(clicked()),
|
|
|
|
SLOT(movedownClicked()));
|
2011-04-06 17:52:23 +00:00
|
|
|
connect(export_button, SIGNAL(clicked()),
|
|
|
|
SLOT(exportClicked()));
|
|
|
|
connect(import_button, SIGNAL(clicked()),
|
|
|
|
SLOT(importClicked()));
|
2011-01-30 14:34:42 +00:00
|
|
|
connect(rules_listbox, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
|
|
|
|
SLOT(modifyClicked()));
|
2007-04-29 17:35:43 +00:00
|
|
|
load();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
KCMRulesList::~KCMRulesList()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2020-02-18 13:52:08 +00:00
|
|
|
qDeleteAll(m_rules);
|
|
|
|
m_rules.clear();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2007-05-09 14:59:04 +00:00
|
|
|
void KCMRulesList::activeChanged()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-05-09 14:59:04 +00:00
|
|
|
QListWidgetItem *item = rules_listbox->currentItem();
|
2007-04-30 11:55:02 +00:00
|
|
|
int itemRow = rules_listbox->row(item);
|
|
|
|
|
2014-01-07 10:22:24 +00:00
|
|
|
if (item != nullptr) // make current==selected
|
2011-01-30 14:34:42 +00:00
|
|
|
rules_listbox->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
|
2014-01-07 10:22:24 +00:00
|
|
|
modify_button->setEnabled(item != nullptr);
|
|
|
|
delete_button->setEnabled(item != nullptr);
|
|
|
|
export_button->setEnabled(item != nullptr);
|
|
|
|
moveup_button->setEnabled(item != nullptr && itemRow > 0);
|
|
|
|
movedown_button->setEnabled(item != nullptr && itemRow < (rules_listbox->count() - 1));
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void KCMRulesList::newClicked()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-11-09 08:02:24 +00:00
|
|
|
RulesDialog dlg(this);
|
2018-12-23 07:56:15 +00:00
|
|
|
Rules* rule = dlg.edit(nullptr, {}, false);
|
2014-01-07 10:22:24 +00:00
|
|
|
if (rule == nullptr)
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2009-05-01 03:03:54 +00:00
|
|
|
int pos = rules_listbox->currentRow() + 1;
|
2011-01-30 14:34:42 +00:00
|
|
|
rules_listbox->insertItem(pos , rule->description);
|
|
|
|
rules_listbox->setCurrentRow(pos, QItemSelectionModel::ClearAndSelect);
|
2020-02-18 13:52:08 +00:00
|
|
|
m_rules.insert(m_rules.begin() + pos, rule);
|
2011-01-30 14:34:42 +00:00
|
|
|
emit changed(true);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void KCMRulesList::modifyClicked()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-30 11:55:02 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (pos == -1)
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2007-11-09 08:02:24 +00:00
|
|
|
RulesDialog dlg(this);
|
2020-02-18 13:52:08 +00:00
|
|
|
Rules *rule = dlg.edit(m_rules[pos], {}, false);
|
|
|
|
if (rule == m_rules[pos])
|
2007-04-29 17:35:43 +00:00
|
|
|
return;
|
2020-02-18 13:52:08 +00:00
|
|
|
delete m_rules[pos];
|
|
|
|
m_rules[pos] = rule;
|
2011-01-30 14:34:42 +00:00
|
|
|
rules_listbox->item(pos)->setText(rule->description);
|
|
|
|
emit changed(true);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void KCMRulesList::deleteClicked()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-30 11:55:02 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2019-08-31 14:28:37 +00:00
|
|
|
Q_ASSERT(pos != -1);
|
2011-01-30 14:34:42 +00:00
|
|
|
delete rules_listbox->takeItem(pos);
|
2020-02-18 13:52:08 +00:00
|
|
|
m_rules.erase(m_rules.begin() + pos);
|
2011-01-30 14:34:42 +00:00
|
|
|
emit changed(true);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2009-05-12 15:00:07 +00:00
|
|
|
void KCMRulesList::moveupClicked()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-30 11:55:02 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2019-08-31 14:28:37 +00:00
|
|
|
Q_ASSERT(pos != -1);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (pos > 0) {
|
|
|
|
QListWidgetItem * item = rules_listbox->takeItem(pos);
|
|
|
|
rules_listbox->insertItem(pos - 1 , item);
|
|
|
|
rules_listbox->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
|
2020-02-18 13:52:08 +00:00
|
|
|
Rules *rule = m_rules[pos];
|
|
|
|
m_rules[pos] = m_rules[pos - 1];
|
|
|
|
m_rules[pos - 1] = rule;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
emit changed(true);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2009-05-12 15:00:07 +00:00
|
|
|
void KCMRulesList::movedownClicked()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-30 11:55:02 +00:00
|
|
|
int pos = rules_listbox->currentRow();
|
2019-08-31 14:28:37 +00:00
|
|
|
Q_ASSERT(pos != -1);
|
2011-01-30 14:34:42 +00:00
|
|
|
if (pos < int(rules_listbox->count()) - 1) {
|
|
|
|
QListWidgetItem * item = rules_listbox->takeItem(pos);
|
|
|
|
rules_listbox->insertItem(pos + 1 , item);
|
|
|
|
rules_listbox->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
|
2020-02-18 13:52:08 +00:00
|
|
|
Rules *rule = m_rules[pos];
|
|
|
|
m_rules[pos] = m_rules[pos + 1];
|
|
|
|
m_rules[pos + 1] = rule;
|
2007-04-29 17:35:43 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
emit changed(true);
|
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
2011-04-06 17:52:23 +00:00
|
|
|
void KCMRulesList::exportClicked()
|
|
|
|
{
|
|
|
|
int pos = rules_listbox->currentRow();
|
2019-08-31 14:28:37 +00:00
|
|
|
Q_ASSERT(pos != -1);
|
2017-10-15 15:36:03 +00:00
|
|
|
QString path = QFileDialog::getSaveFileName(this, i18n("Export Rules"), QDir::home().absolutePath(),
|
2017-10-15 15:35:10 +00:00
|
|
|
i18n("KWin Rules (*.kwinrule)"));
|
2011-04-06 17:52:23 +00:00
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
2020-02-18 13:52:08 +00:00
|
|
|
const auto cfg = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
|
|
|
|
RuleSettings settings(cfg, m_rules[pos]->description);
|
|
|
|
settings.setDefaults();
|
|
|
|
m_rules[pos]->write(&settings);
|
|
|
|
settings.save();
|
2011-04-06 17:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KCMRulesList::importClicked()
|
|
|
|
{
|
2013-09-30 06:43:09 +00:00
|
|
|
QString path = QFileDialog::getOpenFileName(this, i18n("Import Rules"), QDir::home().absolutePath(),
|
2017-10-15 15:35:10 +00:00
|
|
|
i18n("KWin Rules (*.kwinrule)"));
|
2011-04-06 17:52:23 +00:00
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
2020-02-18 13:52:08 +00:00
|
|
|
const auto config = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
|
|
|
|
QStringList groups = config->groupList();
|
2011-04-06 17:52:23 +00:00
|
|
|
if (groups.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int pos = qMax(0, rules_listbox->currentRow());
|
2020-02-18 13:52:08 +00:00
|
|
|
for (const QString &group : groups) {
|
|
|
|
RuleSettings settings(config, group);
|
|
|
|
const bool remove = settings.deleteRule();
|
|
|
|
Rules *new_rule = new Rules(&settings);
|
2011-04-06 17:52:23 +00:00
|
|
|
|
|
|
|
// try to replace existing rule first
|
2020-02-18 13:52:08 +00:00
|
|
|
for (int i = 0; i < m_rules.count(); ++i) {
|
|
|
|
if (m_rules[i]->description == new_rule->description) {
|
|
|
|
delete m_rules[i];
|
2011-04-06 17:52:23 +00:00
|
|
|
if (remove) {
|
2020-02-18 13:52:08 +00:00
|
|
|
m_rules.remove(i);
|
2011-04-06 17:52:23 +00:00
|
|
|
delete rules_listbox->takeItem(i);
|
|
|
|
delete new_rule;
|
|
|
|
pos = qMax(0, rules_listbox->currentRow()); // might have changed!
|
2020-02-18 13:52:08 +00:00
|
|
|
} else
|
|
|
|
m_rules[i] = new_rule;
|
2014-01-07 10:22:24 +00:00
|
|
|
new_rule = nullptr;
|
2011-04-06 17:52:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't add "to be deleted" if not present
|
|
|
|
if (remove) {
|
|
|
|
delete new_rule;
|
2014-01-07 10:22:24 +00:00
|
|
|
new_rule = nullptr;
|
2011-04-06 17:52:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// plain insertion
|
|
|
|
if (new_rule) {
|
2020-02-18 13:52:08 +00:00
|
|
|
m_rules.insert(pos, new_rule);
|
2011-04-06 17:52:23 +00:00
|
|
|
rules_listbox->insertItem(pos++, new_rule->description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit changed(true);
|
|
|
|
}
|
|
|
|
|
2007-04-29 17:35:43 +00:00
|
|
|
void KCMRulesList::load()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2007-04-29 17:35:43 +00:00
|
|
|
rules_listbox->clear();
|
2020-02-18 13:52:08 +00:00
|
|
|
m_settings.load();
|
|
|
|
m_rules = m_settings.rules();
|
|
|
|
for (const auto rule : qAsConst(m_rules)) {
|
2011-01-30 14:34:42 +00:00
|
|
|
rules_listbox->addItem(rule->description);
|
|
|
|
}
|
2020-02-18 13:52:08 +00:00
|
|
|
|
|
|
|
if (m_rules.count() > 0)
|
2011-01-30 14:34:42 +00:00
|
|
|
rules_listbox->setCurrentItem(rules_listbox->item(0));
|
2009-05-12 15:00:07 +00:00
|
|
|
else
|
2014-01-07 10:22:24 +00:00
|
|
|
rules_listbox->setCurrentItem(nullptr);
|
2009-05-01 03:03:54 +00:00
|
|
|
activeChanged();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
void KCMRulesList::save()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2020-02-18 13:52:08 +00:00
|
|
|
m_settings.setRules(m_rules);
|
|
|
|
m_settings.save();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2007-04-29 17:35:43 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|