2011-12-21 06:28:51 +00:00
/*
* Copyright ( c ) 2011 Tamas Krutki < ktamasw @ gmail . com >
2012-02-18 09:38:23 +00:00
* Copyright ( c ) 2012 Martin Gräßlin < mgraesslin @ kde . org >
2011-12-21 06:28:51 +00:00
*
* 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"
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>
2012-05-06 17:20:38 +00:00
# include <QtDBus/QDBusConnection>
# include <QtDBus/QDBusMessage>
# include <QtDBus/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>
# include <KServiceTypeTrader>
2013-09-30 09:32:16 +00:00
# include <Plasma/Package>
2014-01-15 19:32:40 +00:00
# include <KNewStuff3/KNS3/DownloadDialog>
2011-12-21 06:28:51 +00:00
# include "version.h"
K_PLUGIN_FACTORY_DECLARATION ( KcmKWinScriptsFactory ) ;
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 ) ,
m_kwinConfig ( KSharedConfig : : openConfig ( " kwinrc " ) )
2011-12-21 06:28:51 +00:00
{
2013-09-30 09:15:17 +00:00
KAboutData * about = new KAboutData ( " kwin-scripts " , QString ( ) ,
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 " ) ,
2011-12-21 06:28:51 +00:00
KAboutData : : License_GPL_V2 ) ;
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 ) ;
2013-09-30 09:15:17 +00:00
ui - > ghnsButton - > setIcon ( QIcon : : fromTheme ( " get-hot-new-stuff " ) ) ;
2012-05-06 14:54:12 +00:00
2012-02-18 09:38:23 +00:00
connect ( ui - > scriptSelector , SIGNAL ( changed ( bool ) ) , this , SLOT ( changed ( ) ) ) ;
2011-12-21 06:28:51 +00:00
connect ( ui - > importScriptButton , SIGNAL ( clicked ( ) ) , SLOT ( importScript ( ) ) ) ;
2012-05-06 14:54:12 +00:00
connect ( ui - > ghnsButton , SIGNAL ( clicked ( bool ) ) , SLOT ( slotGHNSClicked ( ) ) ) ;
2011-12-21 06:28:51 +00:00
2013-09-30 09:17:40 +00:00
ui - > importScriptButton - > setEnabled ( false ) ;
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
{
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
}
2013-09-30 09:17:40 +00:00
# warning Needs adjustments to changes in Plasma::Package
#if 0
if ( ! Plasma : : Package : : installPackage ( path , QStandardPaths : : writableLocation ( QStandardPaths : : GenericDataLocation ) + " /kwin/scripts/ " , " kwin-script- " ) ) {
2012-02-18 09:38:23 +00:00
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 ( ) ;
}
2013-09-30 09:17:40 +00:00
# endif
2012-02-18 09:38:23 +00:00
// TODO: reload list after successful import
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
{
2012-03-01 09:41:17 +00:00
KService : : List offers = KServiceTypeTrader : : self ( ) - > query ( " KWin/Script " , " not (exist [X-KWin-Exclude-Listing]) or [X-KWin-Exclude-Listing] == false " ) ;
2012-02-18 09:38:23 +00:00
QList < KPluginInfo > scriptinfos = KPluginInfo : : fromServices ( offers ) ;
ui - > scriptSelector - > addPlugins ( scriptinfos , 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 ( ) ;
2012-05-06 17:20:38 +00:00
emit changed ( true ) ;
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 ( ) ;
QDBusMessage message = QDBusMessage : : createMethodCall ( " org.kde.kwin " , " /Scripting " , " org.kde.kwin.Scripting " , " start " ) ;
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
}
2012-05-06 14:54:12 +00:00
void Module : : slotGHNSClicked ( )
{
QPointer < KNS3 : : DownloadDialog > downloadDialog = new KNS3 : : DownloadDialog ( " kwinscripts.knsrc " , this ) ;
2013-09-30 09:15:17 +00:00
if ( downloadDialog - > exec ( ) = = QDialog : : Accepted ) {
2012-05-06 14:54:12 +00:00
if ( ! downloadDialog - > changedEntries ( ) . isEmpty ( ) ) {
updateListViewContents ( ) ;
}
}
delete downloadDialog ;
}
2013-09-30 09:15:17 +00:00
# include "module.moc"