From 572cd64af5e72b3d242f39b4e024262c23c5ce3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Wed, 21 Dec 2011 07:28:51 +0100 Subject: [PATCH] Import of a KCM for KWin scripts This KCM has been implemented by Tamas Krutk in the scope of Google Code-In. It is a simple KCM just listing all available scripts and allowing to import/export them. This will be further extended alongside the planned scripting changes in 4.9. --- kcmkwin/CMakeLists.txt | 3 + kcmkwin/kwinscripts/CMakeLists.txt | 19 +++ kcmkwin/kwinscripts/Messages.sh | 4 + kcmkwin/kwinscripts/kwinscripts.desktop | 17 +++ kcmkwin/kwinscripts/main.cpp | 26 ++++ kcmkwin/kwinscripts/module.cpp | 177 ++++++++++++++++++++++++ kcmkwin/kwinscripts/module.h | 79 +++++++++++ kcmkwin/kwinscripts/module.ui | 70 ++++++++++ kcmkwin/kwinscripts/version.h.cmake | 7 + workspace.cpp | 3 + 10 files changed, 405 insertions(+) create mode 100644 kcmkwin/kwinscripts/CMakeLists.txt create mode 100755 kcmkwin/kwinscripts/Messages.sh create mode 100644 kcmkwin/kwinscripts/kwinscripts.desktop create mode 100644 kcmkwin/kwinscripts/main.cpp create mode 100644 kcmkwin/kwinscripts/module.cpp create mode 100644 kcmkwin/kwinscripts/module.h create mode 100644 kcmkwin/kwinscripts/module.ui create mode 100644 kcmkwin/kwinscripts/version.h.cmake diff --git a/kcmkwin/CMakeLists.txt b/kcmkwin/CMakeLists.txt index b0739b6010..4bd3a3580c 100644 --- a/kcmkwin/CMakeLists.txt +++ b/kcmkwin/CMakeLists.txt @@ -7,6 +7,9 @@ add_subdirectory( kwincompositing ) if(KWIN_BUILD_SCREENEDGES) add_subdirectory( kwinscreenedges ) endif(KWIN_BUILD_SCREENEDGES) +if(KWIN_BUILD_SCRIPTING) + add_subdirectory( kwinscripts ) +endif(KWIN_BUILD_SCRIPTING) add_subdirectory( kwindesktop ) if( KWIN_BUILD_TABBOX ) diff --git a/kcmkwin/kwinscripts/CMakeLists.txt b/kcmkwin/kwinscripts/CMakeLists.txt new file mode 100644 index 0000000000..1df9a87165 --- /dev/null +++ b/kcmkwin/kwinscripts/CMakeLists.txt @@ -0,0 +1,19 @@ +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/version.h) +include_directories(${CMAKE_CURRENT_BINARY_DIR}) + +set(kcm_SRCS + main.cpp + module.cpp +) + +kde4_add_ui_files(kcm_SRCS module.ui) + +kde4_add_plugin(kcm_kwin_scripts ${kcm_SRCS}) + +target_link_libraries(kcm_kwin_scripts + ${KDE4_KDEUI_LIBRARY} + ${KDE4_KIO_LIBS} +) + +install(TARGETS kcm_kwin_scripts DESTINATION ${PLUGIN_INSTALL_DIR}) +install(FILES kwinscripts.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/kcmkwin/kwinscripts/Messages.sh b/kcmkwin/kwinscripts/Messages.sh new file mode 100755 index 0000000000..567ab31d53 --- /dev/null +++ b/kcmkwin/kwinscripts/Messages.sh @@ -0,0 +1,4 @@ +#!bin/sh +$EXTRACTRC `find . -name \*.rc -o -name \*.ui -o -name \*.kcfg` >> rc.cpp +$XGETTEXT `find . -name \*.cc -o -name \*.cpp -o -name \*.h` -o $podir/kcm-kwin-scripts.pot +rm -f rc.cpp diff --git a/kcmkwin/kwinscripts/kwinscripts.desktop b/kcmkwin/kwinscripts/kwinscripts.desktop new file mode 100644 index 0000000000..bf1ef94bbf --- /dev/null +++ b/kcmkwin/kwinscripts/kwinscripts.desktop @@ -0,0 +1,17 @@ +[Desktop Entry] +Exec=kcmshell4 kwin-scripts +Icon=preferences-system-windows-actions +Type=Service +X-KDE-ServiceTypes=KCModule + +X-KDE-Library=kcm_kwin_scripts +X-KDE-PluginKeyword=kwin-scripts +X-KDE-ParentApp=kcontrol + +X-KDE-Keywords=kwin script +X-KDE-System-Settings-Parent-Category=window-behaviour + +Name=KWin Scripts +Comment=Manage KWin scripts + +Categories=KDE;X-KDE-settings-system; diff --git a/kcmkwin/kwinscripts/main.cpp b/kcmkwin/kwinscripts/main.cpp new file mode 100644 index 0000000000..9c754f00a0 --- /dev/null +++ b/kcmkwin/kwinscripts/main.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2011 Tamas Krutki + * + * 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 +#include + +#include "module.h" + +K_PLUGIN_FACTORY(KcmKWinScriptsFactory, + registerPlugin("kwin-scripts");) +K_EXPORT_PLUGIN(KcmKWinScriptsFactory("kwin-scripts")) diff --git a/kcmkwin/kwinscripts/module.cpp b/kcmkwin/kwinscripts/module.cpp new file mode 100644 index 0000000000..8a9e9743f7 --- /dev/null +++ b/kcmkwin/kwinscripts/module.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2011 Tamas Krutki + * + * 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 "module.h" +#include "ui_module.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "version.h" + +K_PLUGIN_FACTORY_DECLARATION(KcmKWinScriptsFactory); + +Module::Module(QWidget *parent, const QVariantList &args) : + KCModule(KcmKWinScriptsFactory::componentData(), parent, args), + ui(new Ui::Module) +{ + KAboutData *about = new KAboutData("kwin-scripts", 0, + ki18n("KWin Scripts"), + global_s_versionStringFull, + ki18n("Configure KWin scripts"), + KAboutData::License_GPL_V2); + + about->addAuthor(ki18n("Tamás Krutki")); + setAboutData(about); + + ui->setupUi(this); + + connect(ui->listWidget, SIGNAL(itemSelectionChanged()), SLOT(updateButtons())); + connect(ui->exportSelectedButton, SIGNAL(clicked()), SLOT(exportScript())); + 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() +{ + 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)"); + + 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(); + } + } + + 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(); + } +} + +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]); + } + } +} diff --git a/kcmkwin/kwinscripts/module.h b/kcmkwin/kwinscripts/module.h new file mode 100644 index 0000000000..7d1370305e --- /dev/null +++ b/kcmkwin/kwinscripts/module.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2011 Tamas Krutki + * + * 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. + */ + +#ifndef MODULE_H +#define MODULE_H + +#include + +namespace Ui +{ +class Module; +} + +class Module : public KCModule +{ + Q_OBJECT +public: + /** + * Constructor. + * + * @param parent Parent widget of the module + * @param args Arguments for the module + */ + Module(QWidget *parent, const QVariantList &args = QVariantList()); + + /** + * Destructor. + */ + ~Module(); + +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 + */ + Ui::Module *ui; + /** + * Updates the contents of the list view. + */ + void updateListViewContents(); +}; + +#endif // MODULE_H diff --git a/kcmkwin/kwinscripts/module.ui b/kcmkwin/kwinscripts/module.ui new file mode 100644 index 0000000000..aa9937883e --- /dev/null +++ b/kcmkwin/kwinscripts/module.ui @@ -0,0 +1,70 @@ + + + Module + + + + 0 + 0 + 484 + 300 + + + + KWin script configuration + + + + + + + + + + + + + + + + + Import KWin script + + + + + + + Export selected KWin script + + + + + + + Remove selected KWin script + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + + diff --git a/kcmkwin/kwinscripts/version.h.cmake b/kcmkwin/kwinscripts/version.h.cmake new file mode 100644 index 0000000000..935fc966de --- /dev/null +++ b/kcmkwin/kwinscripts/version.h.cmake @@ -0,0 +1,7 @@ +#ifndef VERSION_H +#define VERSION_H + +static const char global_s_versionString[] = "${VERSION_STRING}"; +static const char global_s_versionStringFull[] = "${VERSION_STRING_FULL}"; + +#endif // VERSION_H diff --git a/workspace.cpp b/workspace.cpp index 3c67ae6085..34c3340a65 100644 --- a/workspace.cpp +++ b/workspace.cpp @@ -1151,6 +1151,9 @@ QStringList Workspace::configModules(bool controlCenter) #endif #ifdef KWIN_BUILD_SCREENEDGES << "kwinscreenedges" +#endif +#ifdef KWIN_BUILD_SCRIPTING + << "kwinscripts" #endif ; return args;