Scripting KCM supporting Plasma Package structure
List view is replaced by KPluginSelector. This is not an optimal solution as it makes it impossible to get the selected item and reloading seems also not to work after a new script is installed. This should become a proper Model based view.
This commit is contained in:
parent
db04ab7f36
commit
75b17d6c08
4 changed files with 67 additions and 136 deletions
|
@ -13,6 +13,8 @@ kde4_add_plugin(kcm_kwin_scripts ${kcm_SRCS})
|
|||
target_link_libraries(kcm_kwin_scripts
|
||||
${KDE4_KDEUI_LIBRARY}
|
||||
${KDE4_KIO_LIBS}
|
||||
${KDE4_KCMUTILS_LIBS}
|
||||
${KDE4_PLASMA_LIBS}
|
||||
)
|
||||
|
||||
install(TARGETS kcm_kwin_scripts DESTINATION ${PLUGIN_INSTALL_DIR})
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2011 Tamas Krutki <ktamasw@gmail.com>
|
||||
* Copyright (c) 2012 Martin Gräßlin <mgraesslin@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
|
||||
|
@ -19,12 +20,7 @@
|
|||
#include "module.h"
|
||||
#include "ui_module.h"
|
||||
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QDir>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtGui/QPaintEngine>
|
||||
#include <QtCore/QFile>
|
||||
#include <QtCore/QFileInfo>
|
||||
|
||||
#include <KDE/KAboutData>
|
||||
#include <KDE/KPluginFactory>
|
||||
|
@ -32,6 +28,9 @@
|
|||
#include <KDE/KMessageBox>
|
||||
#include <KDE/KFileDialog>
|
||||
#include <KDE/KMessageWidget>
|
||||
#include <KDE/KPluginInfo>
|
||||
#include <KDE/KServiceTypeTrader>
|
||||
#include <KDE/Plasma/Package>
|
||||
|
||||
#include "version.h"
|
||||
|
||||
|
@ -39,7 +38,8 @@ K_PLUGIN_FACTORY_DECLARATION(KcmKWinScriptsFactory);
|
|||
|
||||
Module::Module(QWidget *parent, const QVariantList &args) :
|
||||
KCModule(KcmKWinScriptsFactory::componentData(), parent, args),
|
||||
ui(new Ui::Module)
|
||||
ui(new Ui::Module),
|
||||
m_kwinConfig(KSharedConfig::openConfig("kwinrc"))
|
||||
{
|
||||
KAboutData *about = new KAboutData("kwin-scripts", 0,
|
||||
ki18n("KWin Scripts"),
|
||||
|
@ -52,18 +52,13 @@ Module::Module(QWidget *parent, const QVariantList &args) :
|
|||
|
||||
ui->setupUi(this);
|
||||
|
||||
connect(ui->listWidget, SIGNAL(itemSelectionChanged()), SLOT(updateButtons()));
|
||||
connect(ui->exportSelectedButton, SIGNAL(clicked()), SLOT(exportScript()));
|
||||
connect(ui->scriptSelector, SIGNAL(changed(bool)), this, SLOT(changed()));
|
||||
connect(ui->importScriptButton, SIGNAL(clicked()), SLOT(importScript()));
|
||||
connect(ui->removeScriptButton, SIGNAL(clicked()), SLOT(removeScript()));
|
||||
|
||||
// We have no help and defaults and apply buttons.
|
||||
setButtons(buttons() ^ KCModule::Help ^ KCModule::Default ^ KCModule::Apply);
|
||||
|
||||
ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
|
||||
updateListViewContents();
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
Module::~Module()
|
||||
|
@ -71,107 +66,47 @@ Module::~Module()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
void Module::updateButtons()
|
||||
{
|
||||
if (ui->listWidget->selectedItems().isEmpty()) {
|
||||
ui->exportSelectedButton->setEnabled(false);
|
||||
ui->removeScriptButton->setEnabled(false);
|
||||
} else {
|
||||
ui->exportSelectedButton->setEnabled(true);
|
||||
ui->removeScriptButton->setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
void Module::exportScript()
|
||||
{
|
||||
QString path = KFileDialog::getSaveFileName(KUrl(), "*.kwinscript *.kws *.kwinqs|KWin scripts (*.kwinscript, *.kws, *.kwinqs)");
|
||||
|
||||
if (!path.isNull()) {
|
||||
QFile f(componentData().dirs()->findResource("data", "kwin/scripts/" + ui->listWidget->currentItem()->text()));
|
||||
|
||||
QFileInfo pathInfo(path);
|
||||
QDir dir(pathInfo.absolutePath());
|
||||
|
||||
if (dir.exists(pathInfo.fileName())) {
|
||||
dir.remove(pathInfo.fileName());
|
||||
}
|
||||
|
||||
if (f.copy(path)) {
|
||||
KMessageWidget* msgWidget = new KMessageWidget;
|
||||
msgWidget->setText(ki18n("The selected script was exported successfully.").toString());
|
||||
msgWidget->setMessageType(KMessageWidget::Positive);
|
||||
ui->verticalLayout2->insertWidget(0, msgWidget);
|
||||
msgWidget->animatedShow();
|
||||
} else {
|
||||
KMessageWidget* msgWidget = new KMessageWidget;
|
||||
msgWidget->setText(ki18n("An error occurred, the selected script could not be exported.").toString());
|
||||
msgWidget->setMessageType(KMessageWidget::Error);
|
||||
ui->verticalLayout2->insertWidget(0, msgWidget);
|
||||
msgWidget->animatedShow();
|
||||
}
|
||||
}
|
||||
|
||||
updateListViewContents();
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
void Module::importScript()
|
||||
{
|
||||
QString path = KFileDialog::getOpenFileName(KUrl(), "*.kwinscript *.kws *.kwinqs|KWin scripts (*.kwinscript, *.kws, *.kwinqs)");
|
||||
QString path = KFileDialog::getOpenFileName(KUrl(), "*.kwinscript|KWin scripts (*.kwinscript)");
|
||||
|
||||
if (!path.isNull()) {
|
||||
QFileInfo pathInfo(path);
|
||||
QString fileName(pathInfo.fileName());
|
||||
|
||||
QFile f(path);
|
||||
if (!f.copy(componentData().dirs()->saveLocation("data", "kwin/scripts/") + fileName)) {
|
||||
KMessageWidget* msgWidget = new KMessageWidget;
|
||||
msgWidget->setText(ki18n("Cannot import selected script: maybe a script already exists with the same name or there is a permission problem.").toString());
|
||||
msgWidget->setMessageType(KMessageWidget::Error);
|
||||
ui->verticalLayout2->insertWidget(0, msgWidget);
|
||||
msgWidget->animatedShow();
|
||||
} else {
|
||||
f.setFileName(componentData().dirs()->saveLocation("data", "kwin/scripts/") + fileName);
|
||||
f.open(QIODevice::ReadWrite);
|
||||
f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
|
||||
QFile::ReadGroup | QFile::ExeGroup |
|
||||
QFile::ReadUser | QFile::ExeUser | QFile::WriteUser |
|
||||
QFile::ReadOther | QFile::ExeOther);
|
||||
f.close();
|
||||
|
||||
KMessageWidget* msgWidget = new KMessageWidget;
|
||||
msgWidget->setText(ki18n("The selected script was imported successfully.").toString());
|
||||
msgWidget->setMessageType(KMessageWidget::Positive);
|
||||
ui->verticalLayout2->insertWidget(0, msgWidget);
|
||||
msgWidget->animatedShow();
|
||||
}
|
||||
if (path.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
updateListViewContents();
|
||||
updateButtons();
|
||||
}
|
||||
|
||||
void Module::removeScript()
|
||||
{
|
||||
if (KMessageBox::questionYesNo(this, ki18n("Do you really want to delete the selected script?").toString(), ki18n("Remove KWin script").toString()) == KMessageBox::Yes) {
|
||||
QDir dir(QFileInfo(componentData().dirs()->findResource("data", "kwin/scripts/" + ui->listWidget->currentItem()->text())).absolutePath());
|
||||
dir.remove(ui->listWidget->currentItem()->text());
|
||||
updateListViewContents();
|
||||
updateButtons();
|
||||
if (!Plasma::Package::installPackage(path, componentData().dirs()->saveLocation("data", "kwin/scripts/"), "kwin-script-")) {
|
||||
KMessageWidget* msgWidget = new KMessageWidget;
|
||||
msgWidget->setText(ki18n("Cannot import selected script: maybe a script already exists with the same name or there is a permission problem.").toString());
|
||||
msgWidget->setMessageType(KMessageWidget::Error);
|
||||
ui->verticalLayout2->insertWidget(0, msgWidget);
|
||||
msgWidget->animatedShow();
|
||||
}
|
||||
// TODO: reload list after successful import
|
||||
}
|
||||
|
||||
void Module::updateListViewContents()
|
||||
{
|
||||
ui->listWidget->clear();
|
||||
|
||||
QStringList dirList = componentData().dirs()->findDirs("data", "kwin/scripts");
|
||||
for (int i = 0; i < dirList.size(); i++) {
|
||||
QDir dir(dirList[i]);
|
||||
QStringList fileNameList = dir.entryList(QStringList() << "*.kws" << "*.kwinscript" << "*.kwinqs", QDir::Files, QDir::Name);
|
||||
|
||||
for (int j = 0; j < fileNameList.size(); j++) {
|
||||
ui->listWidget->addItem(fileNameList[j]);
|
||||
}
|
||||
}
|
||||
KService::List offers = KServiceTypeTrader::self()->query("KWin/Script");
|
||||
QList<KPluginInfo> scriptinfos = KPluginInfo::fromServices(offers);
|
||||
ui->scriptSelector->addPlugins(scriptinfos, KPluginSelector::ReadConfigFile, QString(), QString(), m_kwinConfig);
|
||||
}
|
||||
|
||||
void Module::defaults()
|
||||
{
|
||||
ui->scriptSelector->defaults();
|
||||
}
|
||||
|
||||
void Module::load()
|
||||
{
|
||||
updateListViewContents();
|
||||
ui->scriptSelector->load();
|
||||
|
||||
emit changed(false);
|
||||
}
|
||||
|
||||
void Module::save()
|
||||
{
|
||||
ui->scriptSelector->save();
|
||||
// TODO: reload scripts in KWin
|
||||
|
||||
emit changed(false);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#define MODULE_H
|
||||
|
||||
#include <KDE/KCModule>
|
||||
#include <KDE/KSharedConfig>
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
|
@ -42,29 +43,17 @@ public:
|
|||
* Destructor.
|
||||
*/
|
||||
~Module();
|
||||
virtual void load();
|
||||
virtual void save();
|
||||
virtual void defaults();
|
||||
|
||||
protected slots:
|
||||
/**
|
||||
* Called when the selection changes in the list view.
|
||||
* Disables/enables the remove and export buttons.
|
||||
*/
|
||||
void updateButtons();
|
||||
|
||||
/**
|
||||
* Called when the export script button is clicked.
|
||||
*/
|
||||
void exportScript();
|
||||
|
||||
/**
|
||||
* Called when the import script button is clicked.
|
||||
*/
|
||||
void importScript();
|
||||
|
||||
/**
|
||||
* Called when the remove script button is clicked.
|
||||
*/
|
||||
void removeScript();
|
||||
|
||||
private:
|
||||
/**
|
||||
* UI
|
||||
|
@ -74,6 +63,7 @@ private:
|
|||
* Updates the contents of the list view.
|
||||
*/
|
||||
void updateListViewContents();
|
||||
KSharedConfigPtr m_kwinConfig;
|
||||
};
|
||||
|
||||
#endif // MODULE_H
|
||||
|
|
|
@ -19,7 +19,17 @@
|
|||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout2">
|
||||
<item>
|
||||
<widget class="QListWidget" name="listWidget"/>
|
||||
<widget class="KPluginSelector" name="scriptSelector" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="focusPolicy">
|
||||
<enum>Qt::WheelFocus</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
|
@ -32,20 +42,6 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="exportSelectedButton">
|
||||
<property name="text">
|
||||
<string>Export selected KWin script</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="removeScriptButton">
|
||||
<property name="text">
|
||||
<string>Remove selected KWin script</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
|
@ -65,6 +61,14 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>KPluginSelector</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>kpluginselector.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
|
Loading…
Reference in a new issue