2011-12-21 06:28:51 +00:00
/*
2020-08-02 22:22:19 +00:00
SPDX - FileCopyrightText : 2011 Tamas Krutki < ktamasw @ gmail . com >
SPDX - FileCopyrightText : 2012 Martin Gräßlin < mgraesslin @ kde . org >
SPDX - License - Identifier : GPL - 2.0 - or - later
*/
2011-12-21 06:28:51 +00:00
# include "module.h"
# include "ui_module.h"
2013-09-30 09:13:38 +00:00
# include <QFileDialog>
2013-02-26 08:00:51 +00:00
# include <QStringList>
2013-09-30 09:13:38 +00:00
# include <QStandardPaths>
2018-06-05 10:52:57 +00:00
# include <QDBusConnection>
# include <QDBusMessage>
# include <QDBusPendingCall>
2011-12-21 06:28:51 +00:00
2014-03-17 15:24:10 +00:00
# include <KAboutData>
2014-03-18 10:06:56 +00:00
# include <KLocalizedString>
2014-03-17 15:24:10 +00:00
# include <KPluginFactory>
# include <KMessageBox>
# include <KMessageWidget>
# include <KPluginInfo>
2016-10-07 12:27:19 +00:00
# include <KPackage/PackageLoader>
# include <KPackage/Package>
2020-08-28 15:32:44 +00:00
# include <KPackage/PackageStructure>
2016-10-07 12:27:19 +00:00
2016-12-13 07:26:15 +00:00
# include <KNewStuff3/KNS3/Button>
2011-12-21 06:28:51 +00:00
# include "version.h"
2021-01-05 08:53:04 +00:00
# include "kwinscriptsdata.h"
2011-12-21 06:28:51 +00:00
Module : : Module ( QWidget * parent , const QVariantList & args ) :
2013-09-30 09:15:17 +00:00
KCModule ( parent , args ) ,
2012-02-18 09:38:23 +00:00
ui ( new Ui : : Module ) ,
2021-01-05 08:53:04 +00:00
m_kwinConfig ( KSharedConfig : : openConfig ( " kwinrc " ) ) ,
m_kwinScriptsData ( new KWinScriptsData ( this ) )
2011-12-21 06:28:51 +00:00
{
2014-04-28 15:11:20 +00:00
KAboutData * about = new KAboutData ( " kwin-scripts " ,
2013-09-30 09:15:17 +00:00
i18n ( " KWin Scripts " ) ,
2011-12-21 06:28:51 +00:00
global_s_versionStringFull ,
2013-09-30 09:15:17 +00:00
i18n ( " Configure KWin scripts " ) ,
2014-05-14 02:49:33 +00:00
KAboutLicense : : GPL_V2 ) ;
2011-12-21 06:28:51 +00:00
2013-09-30 09:15:17 +00:00
about - > addAuthor ( i18n ( " Tamás Krutki " ) ) ;
2011-12-21 06:28:51 +00:00
setAboutData ( about ) ;
ui - > setupUi ( this ) ;
2016-12-13 07:26:15 +00:00
2017-04-26 15:11:34 +00:00
ui - > messageWidget - > hide ( ) ;
2016-12-13 07:26:15 +00:00
ui - > ghnsButton - > setConfigFile ( QStringLiteral ( " kwinscripts.knsrc " ) ) ;
connect ( ui - > ghnsButton , & KNS3 : : Button : : dialogFinished , this , [ this ] ( const KNS3 : : Entry : : List & changedEntries ) {
if ( ! changedEntries . isEmpty ( ) ) {
2020-08-08 15:23:12 +00:00
ui - > scriptSelector - > clearPlugins ( ) ;
2016-12-13 07:26:15 +00:00
updateListViewContents ( ) ;
}
} ) ;
2012-05-06 14:54:12 +00:00
2020-03-13 09:44:02 +00:00
connect ( ui - > scriptSelector , & KPluginSelector : : changed , this , qOverload < bool > ( & KCModule : : changed ) ) ;
connect ( ui - > scriptSelector , & KPluginSelector : : defaulted , this , qOverload < bool > ( & KCModule : : defaulted ) ) ;
2021-01-05 08:53:04 +00:00
connect ( this , & Module : : defaultsIndicatorsVisibleChanged , ui - > scriptSelector , & KPluginSelector : : setDefaultsIndicatorsVisible ) ;
2020-03-13 09:44:02 +00:00
connect ( ui - > importScriptButton , & QPushButton : : clicked , this , & Module : : importScript ) ;
2011-12-21 06:28:51 +00:00
2020-08-28 15:32:44 +00:00
ui - > scriptSelector - > setAdditionalButtonHandler ( [ this ] ( const KPluginInfo & info ) {
QPushButton * button = new QPushButton ( ui - > scriptSelector ) ;
button - > setIcon ( QIcon : : fromTheme ( QStringLiteral ( " delete " ) ) ) ;
button - > setEnabled ( QFileInfo ( info . entryPath ( ) ) . isWritable ( ) ) ;
connect ( button , & QPushButton : : clicked , this , [ this , info ] ( ) {
using namespace KPackage ;
PackageStructure * structure = PackageLoader : : self ( ) - > loadPackageStructure ( QStringLiteral ( " KWin/Script " ) ) ;
Package package ( structure ) ;
// We can get the package root from the entry path
QDir root = QFileInfo ( info . entryPath ( ) ) . dir ( ) ;
root . cdUp ( ) ;
KJob * uninstallJob = Package ( structure ) . uninstall ( info . pluginName ( ) , root . absolutePath ( ) ) ;
connect ( uninstallJob , & KJob : : result , this , [ this , uninstallJob ] ( ) {
ui - > scriptSelector - > clearPlugins ( ) ;
updateListViewContents ( ) ;
// If the uninstallation is successful the entry will be immediately removed
if ( ! uninstallJob - > errorString ( ) . isEmpty ( ) ) {
ui - > messageWidget - > setText ( i18n ( " Error when uninstalling KWin Script: %1 " , uninstallJob - > errorString ( ) ) ) ;
ui - > messageWidget - > setMessageType ( KMessageWidget : : Error ) ;
ui - > messageWidget - > animatedShow ( ) ;
}
} ) ;
} ) ;
return button ;
} ) ;
2011-12-21 06:28:51 +00:00
updateListViewContents ( ) ;
}
Module : : ~ Module ( )
{
delete ui ;
}
2012-02-18 09:38:23 +00:00
void Module : : importScript ( )
2011-12-21 06:28:51 +00:00
{
2017-04-26 15:11:34 +00:00
ui - > messageWidget - > animatedHide ( ) ;
2013-09-30 09:13:38 +00:00
QString path = QFileDialog : : getOpenFileName ( nullptr , i18n ( " Import KWin Script " ) , QDir : : homePath ( ) ,
i18n ( " *.kwinscript|KWin scripts (*.kwinscript) " ) ) ;
2012-02-18 09:38:23 +00:00
if ( path . isNull ( ) ) {
return ;
2011-12-21 06:28:51 +00:00
}
2017-04-26 15:11:34 +00:00
using namespace KPackage ;
PackageStructure * structure = PackageLoader : : self ( ) - > loadPackageStructure ( QStringLiteral ( " KWin/Script " ) ) ;
Package package ( structure ) ;
KJob * installJob = package . update ( path ) ;
installJob - > setProperty ( " packagePath " , path ) ; // so we can retrieve it later for showing the script's name
connect ( installJob , & KJob : : result , this , & Module : : importScriptInstallFinished ) ;
}
void Module : : importScriptInstallFinished ( KJob * job )
{
// if the applet is already installed, just add it to the containment
if ( job - > error ( ) ! = KJob : : NoError ) {
ui - > messageWidget - > setText ( i18nc ( " Placeholder is error message returned from the install service " , " Cannot import selected script. \n %1 " , job - > errorString ( ) ) ) ;
ui - > messageWidget - > setMessageType ( KMessageWidget : : Error ) ;
ui - > messageWidget - > animatedShow ( ) ;
return ;
2012-02-18 09:38:23 +00:00
}
2017-04-26 15:11:34 +00:00
using namespace KPackage ;
// so we can show the name of the package we just imported
PackageStructure * structure = PackageLoader : : self ( ) - > loadPackageStructure ( QStringLiteral ( " KWin/Script " ) ) ;
Package package ( structure ) ;
package . setPath ( job - > property ( " packagePath " ) . toString ( ) ) ;
Q_ASSERT ( package . isValid ( ) ) ;
ui - > messageWidget - > setText ( i18nc ( " Placeholder is name of the script that was imported " , " The script \" %1 \" was successfully imported. " , package . metadata ( ) . name ( ) ) ) ;
ui - > messageWidget - > setMessageType ( KMessageWidget : : Information ) ;
ui - > messageWidget - > animatedShow ( ) ;
updateListViewContents ( ) ;
emit changed ( true ) ;
2011-12-21 06:28:51 +00:00
}
2012-02-18 09:38:23 +00:00
void Module : : updateListViewContents ( )
2011-12-21 06:28:51 +00:00
{
2021-01-05 08:53:04 +00:00
ui - > scriptSelector - > addPlugins ( m_kwinScriptsData - > pluginInfoList ( ) , KPluginSelector : : ReadConfigFile , QString ( ) , QString ( ) , m_kwinConfig ) ;
2011-12-21 06:28:51 +00:00
}
2012-02-18 09:38:23 +00:00
void Module : : defaults ( )
2011-12-21 06:28:51 +00:00
{
2012-02-18 09:38:23 +00:00
ui - > scriptSelector - > defaults ( ) ;
2011-12-21 06:28:51 +00:00
}
2012-02-18 09:38:23 +00:00
void Module : : load ( )
2011-12-21 06:28:51 +00:00
{
2012-02-18 09:38:23 +00:00
updateListViewContents ( ) ;
ui - > scriptSelector - > load ( ) ;
emit changed ( false ) ;
2011-12-21 06:28:51 +00:00
}
2012-02-18 09:38:23 +00:00
void Module : : save ( )
2011-12-21 06:28:51 +00:00
{
2012-02-18 09:38:23 +00:00
ui - > scriptSelector - > save ( ) ;
2012-05-06 17:20:38 +00:00
m_kwinConfig - > sync ( ) ;
2014-05-15 06:50:56 +00:00
QDBusMessage message = QDBusMessage : : createMethodCall ( " org.kde.KWin " , " /Scripting " , " org.kde.kwin.Scripting " , " start " ) ;
2012-05-06 17:20:38 +00:00
QDBusConnection : : sessionBus ( ) . asyncCall ( message ) ;
2011-12-21 06:28:51 +00:00
2012-02-18 09:38:23 +00:00
emit changed ( false ) ;
2011-12-21 06:28:51 +00:00
}
2021-01-05 08:53:04 +00:00
# include "module.moc"