scriptplugin.cpp (krename-4.0.9) | : | scriptplugin.cpp (krename-4.90.90) | ||
---|---|---|---|---|
skipping to change at line 20 | skipping to change at line 20 | |||
* * | * * | |||
* This program is free software; you can redistribute it and/or modify * | * 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 * | * it under the terms of the GNU General Public License as published by * | |||
* the Free Software Foundation; either version 2 of the License, or * | * the Free Software Foundation; either version 2 of the License, or * | |||
* (at your option) any later version. * | * (at your option) any later version. * | |||
* * | * * | |||
***************************************************************************/ | ***************************************************************************/ | |||
#include "scriptplugin.h" | #include "scriptplugin.h" | |||
#include <kapplication.h> | ||||
#include <kconfiggroup.h> | #include <kconfiggroup.h> | |||
#include <kfiledialog.h> | ||||
#include <kiconloader.h> | #include <kiconloader.h> | |||
#include <klistwidget.h> | ||||
#include <klocale.h> | ||||
#include <kmessagebox.h> | #include <kmessagebox.h> | |||
#include <ktemporaryfile.h> | #include <KIO/StoredTransferJob> | |||
#include <KIO/StatJob> | ||||
#include <kio/netaccess.h> | #include <KJobWidgets> | |||
#include <QTemporaryFile> | ||||
#include <QFile> | #include <QFile> | |||
#include <QLabel> | ||||
#include <QHBoxLayout> | ||||
#include <QMenu> | #include <QMenu> | |||
#include <QPointer> | ||||
#include <QTextStream> | #include <QTextStream> | |||
#include <QVariant> | #include <QVariant> | |||
#include <QFileDialog> | ||||
#include <kjs/kjsinterpreter.h> | #include <kjs/kjsinterpreter.h> | |||
#include "ui_scriptplugindialog.h" | #include "ui_scriptplugindialog.h" | |||
#include "ui_scriptpluginwidget.h" | #include "ui_scriptpluginwidget.h" | |||
#include "batchrenamer.h" | #include "batchrenamer.h" | |||
#include "krenamefile.h" | #include "krenamefile.h" | |||
const char* ScriptPlugin::s_pszFileDialogLocation = "kfiledialog://krenamejscrip | const char *ScriptPlugin::s_pszFileDialogLocation = "kfiledialog://krenamejscrip | |||
t"; | t"; | |||
const char* ScriptPlugin::s_pszVarNameIndex = "krename_index"; | const char *ScriptPlugin::s_pszVarNameIndex = "krename_index"; | |||
const char* ScriptPlugin::s_pszVarNameUrl = "krename_url"; | const char *ScriptPlugin::s_pszVarNameUrl = "krename_url"; | |||
const char* ScriptPlugin::s_pszVarNameFilename = "krename_filename"; | const char *ScriptPlugin::s_pszVarNameFilename = "krename_filename"; | |||
const char* ScriptPlugin::s_pszVarNameExtension = "krename_extension"; | const char *ScriptPlugin::s_pszVarNameExtension = "krename_extension"; | |||
const char* ScriptPlugin::s_pszVarNameDirectory = "krename_directory"; | const char *ScriptPlugin::s_pszVarNameDirectory = "krename_directory"; | |||
enum EVarType { | enum EVarType { | |||
eVarType_String = 0, | eVarType_String = 0, | |||
eVarType_Int, | eVarType_Int, | |||
eVarType_Double, | eVarType_Double, | |||
eVarType_Bool | eVarType_Bool | |||
}; | }; | |||
ScriptPlugin::ScriptPlugin( PluginLoader* loader ) | ScriptPlugin::ScriptPlugin(PluginLoader *loader) | |||
: QObject(), | : QObject(), | |||
Plugin( loader ), m_parent( NULL ) | Plugin(loader), m_parent(nullptr) | |||
{ | { | |||
m_name = i18n("JavaScript Plugin"); | m_name = i18n("JavaScript Plugin"); | |||
m_icon = "applications-development"; | m_icon = "applications-development"; | |||
m_interpreter = new KJSInterpreter(); | m_interpreter = new KJSInterpreter(); | |||
m_menu = new QMenu(); | m_menu = new QMenu(); | |||
m_widget = new Ui::ScriptPluginWidget(); | m_widget = new Ui::ScriptPluginWidget(); | |||
this->addSupportedToken("js;.*"); | this->addSupportedToken("js;.*"); | |||
m_help.append( "[js;4+5];;" + i18n("Insert a snippet of JavaScript code (4+5 in this case)") ); | m_help.append("[js;4+5];;" + i18n("Insert a snippet of JavaScript code (4+5 in this case)")); | |||
m_menu->addAction( i18n("Index of the current file"), this, SLOT(slotIns | m_menu->addAction(i18n("Index of the current file"), this, SLOT(slotInse | |||
ertIndex())); | rtIndex())); | |||
m_menu->addAction( i18n("URL of the current file"), this, SLOT(slotIns | m_menu->addAction(i18n("URL of the current file"), this, SLOT(slotInse | |||
ertUrl())); | rtUrl())); | |||
m_menu->addAction( i18n("Filename of the current file"), this, SLOT(slotIns | m_menu->addAction(i18n("Filename of the current file"), this, SLOT(slotInse | |||
ertFilename())); | rtFilename())); | |||
m_menu->addAction( i18n("Extension of the current file"), this, SLOT(slotIns | m_menu->addAction(i18n("Extension of the current file"), this, SLOT(slotInse | |||
ertExtension())); | rtExtension())); | |||
m_menu->addAction( i18n("Directory of the current file"), this, SLOT(slotIns | m_menu->addAction(i18n("Directory of the current file"), this, SLOT(slotInse | |||
ertDirectory())); | rtDirectory())); | |||
} | } | |||
ScriptPlugin::~ScriptPlugin() | ScriptPlugin::~ScriptPlugin() | |||
{ | { | |||
delete m_widget; | delete m_widget; | |||
delete m_menu; | delete m_menu; | |||
delete m_interpreter; | delete m_interpreter; | |||
} | } | |||
QString ScriptPlugin::processFile( BatchRenamer* b, int index, | QString ScriptPlugin::processFile(BatchRenamer *b, int index, | |||
const QString & filenameOrToken, EPluginType ) | const QString &filenameOrToken, EPluginType) | |||
{ | { | |||
QString token( filenameOrToken ); | QString token(filenameOrToken); | |||
QString script; | QString script; | |||
QString definitions = m_widget->textCode->toPlainText(); | QString definitions = m_widget->textCode->toPlainText(); | |||
if( token.contains( ";" ) ) | if (token.contains(";")) { | |||
{ | script = token.section(';', 1); // all sections from 1 to the last | |||
script = token.section( ';', 1 ); // all sections from 1 to the last | token = token.section(';', 0, 0).toLower(); | |||
token = token.section( ';', 0, 0 ).toLower(); | } else { | |||
} else | ||||
token = token.toLower(); | token = token.toLower(); | |||
} | ||||
if( token == "js" ) | if (token == "js") { | |||
{ | // Setup interpreter | |||
// Setup interpreter | const KRenameFile &file = b->files()->at(index); | |||
const KRenameFile & file = b->files()->at( index ); | initKRenameVars(file, index); | |||
initKRenameVars( file, index ); | ||||
// Make sure definitions are executed first | ||||
// Make sure definitions are executed first | script = definitions + '\n' + script; | |||
script = definitions + "\n" + script; | ||||
KJSResult result = m_interpreter->evaluate(script, nullptr); | ||||
KJSResult result = m_interpreter->evaluate( script, NULL ); | if (result.isException()) { | |||
if( result.isException() ) | qDebug("JavaScript Error: %s", result.errorMessage().toUtf8().data() | |||
{ | ); | |||
qDebug( "JavaScript Error: %s", result.errorMessage().toUtf8().data() | return QString(); | |||
); | } | |||
return QString::null; | ||||
} | ||||
return result.value().toString( m_interpreter->globalContext() ); | return result.value().toString(m_interpreter->globalContext()); | |||
} | } | |||
return QString::null; | return QString(); | |||
} | } | |||
const QPixmap ScriptPlugin::icon() const | const QPixmap ScriptPlugin::icon() const | |||
{ | { | |||
return KIconLoader::global()->loadIcon( m_icon, KIconLoader::NoGroup, KIconL oader::SizeSmall ); | return KIconLoader::global()->loadIcon(m_icon, KIconLoader::NoGroup, KIconLo ader::SizeSmall); | |||
} | } | |||
void ScriptPlugin::createUI( QWidget* parent ) const | void ScriptPlugin::createUI(QWidget *parent) const | |||
{ | { | |||
QStringList labels; | QStringList labels; | |||
labels << i18n("Variable Name"); | labels << i18n("Variable Name"); | |||
labels << i18n("Initial Value"); | labels << i18n("Initial Value"); | |||
const_cast<ScriptPlugin*>(this)->m_parent = parent; | const_cast<ScriptPlugin *>(this)->m_parent = parent; | |||
m_widget->setupUi( parent ); | m_widget->setupUi(parent); | |||
m_widget->listVariables->setColumnCount( 2 ); | m_widget->listVariables->setColumnCount(2); | |||
m_widget->listVariables->setHeaderLabels( labels ); | m_widget->listVariables->setHeaderLabels(labels); | |||
connect( m_widget->listVariables, SIGNAL(itemSelectionChanged()), SLOT(slotE | connect(m_widget->listVariables, &QTreeWidget::itemSelectionChanged, | |||
nableControls()) ); | this, &ScriptPlugin::slotEnableControls); | |||
connect( m_widget->buttonAdd, SIGNAL(clicked(bool)), SLOT(slotAdd()) ); | connect(m_widget->buttonAdd, &QPushButton::clicked, | |||
connect( m_widget->buttonRemove, SIGNAL(clicked(bool)), SLOT(slotRemove()) | this, &ScriptPlugin::slotAdd); | |||
); | connect(m_widget->buttonRemove, &QPushButton::clicked, | |||
connect( m_widget->buttonLoad, SIGNAL(clicked(bool)), SLOT(slotLoad()) ); | this, &ScriptPlugin::slotRemove); | |||
connect( m_widget->buttonSave, SIGNAL(clicked(bool)), SLOT(slotSave()) ); | connect(m_widget->buttonLoad, &QPushButton::clicked, | |||
connect( m_widget->textCode, SIGNAL(textChanged()), SLOT(slotEnableCont | this, &ScriptPlugin::slotLoad); | |||
rols()) ); | connect(m_widget->buttonSave, &QPushButton::clicked, | |||
this, &ScriptPlugin::slotSave); | ||||
const_cast<ScriptPlugin*>(this)->slotEnableControls(); | connect(m_widget->textCode, &QTextEdit::textChanged, | |||
this, &ScriptPlugin::slotEnableControls); | ||||
QPixmap openIcon = KIconLoader::global()->loadIcon( "document-open", KIcon | ||||
Loader::NoGroup, KIconLoader::SizeSmall ); | const_cast<ScriptPlugin *>(this)->slotEnableControls(); | |||
QPixmap saveIcon = KIconLoader::global()->loadIcon( "document-save-as", KI | ||||
conLoader::NoGroup, KIconLoader::SizeSmall ); | QPixmap openIcon = KIconLoader::global()->loadIcon("document-open", KIconL | |||
QPixmap removeIcon = KIconLoader::global()->loadIcon( "list-remove", KIconLo | oader::NoGroup, KIconLoader::SizeSmall); | |||
ader::NoGroup, KIconLoader::SizeSmall ); | QPixmap saveIcon = KIconLoader::global()->loadIcon("document-save-as", KIc | |||
QPixmap addIcon = KIconLoader::global()->loadIcon( "list-add", KIconLoade | onLoader::NoGroup, KIconLoader::SizeSmall); | |||
r::NoGroup, KIconLoader::SizeSmall ); | QPixmap removeIcon = KIconLoader::global()->loadIcon("list-remove", KIconLoa | |||
der::NoGroup, KIconLoader::SizeSmall); | ||||
m_widget->buttonLoad->setIcon( openIcon ); | QPixmap addIcon = KIconLoader::global()->loadIcon("list-add", KIconLoader | |||
m_widget->buttonSave->setIcon( saveIcon ); | ::NoGroup, KIconLoader::SizeSmall); | |||
m_widget->buttonAdd->setIcon( addIcon ); | ||||
m_widget->buttonRemove->setIcon( removeIcon ); | m_widget->buttonLoad->setIcon(openIcon); | |||
m_widget->buttonSave->setIcon(saveIcon); | ||||
m_widget->buttonAdd->setIcon(addIcon); | ||||
m_widget->buttonRemove->setIcon(removeIcon); | ||||
m_widget->buttonInsert->setMenu( m_menu ); | m_widget->buttonInsert->setMenu(m_menu); | |||
} | } | |||
void ScriptPlugin::initKRenameVars( const KRenameFile & file, int index ) | void ScriptPlugin::initKRenameVars(const KRenameFile &file, int index) | |||
{ | { | |||
// KRename definitions | // KRename definitions | |||
m_interpreter->globalObject().setProperty( m_interpreter->globalContext(), | m_interpreter->globalObject().setProperty(m_interpreter->globalContext(), | |||
ScriptPlugin::s_pszVarNameIndex, | ScriptPlugin::s_pszVarNameIndex, | |||
index ); | index); | |||
m_interpreter->globalObject().setProperty( m_interpreter->globalContext(), | m_interpreter->globalObject().setProperty(m_interpreter->globalContext(), | |||
ScriptPlugin::s_pszVarNameUrl, | ScriptPlugin::s_pszVarNameUrl, | |||
file.srcUrl().url() ); | file.srcUrl().url()); | |||
m_interpreter->globalObject().setProperty( m_interpreter->globalContext(), | m_interpreter->globalObject().setProperty(m_interpreter->globalContext(), | |||
ScriptPlugin::s_pszVarNameFilename | ScriptPlugin::s_pszVarNameFilename, | |||
, | file.srcFilename()); | |||
file.srcFilename() ); | m_interpreter->globalObject().setProperty(m_interpreter->globalContext(), | |||
m_interpreter->globalObject().setProperty( m_interpreter->globalContext(), | ScriptPlugin::s_pszVarNameExtension, | |||
ScriptPlugin::s_pszVarNameExtensio | file.srcExtension()); | |||
n, | m_interpreter->globalObject().setProperty(m_interpreter->globalContext(), | |||
file.srcExtension() ); | ScriptPlugin::s_pszVarNameDirectory, | |||
m_interpreter->globalObject().setProperty( m_interpreter->globalContext(), | file.srcDirectory()); | |||
ScriptPlugin::s_pszVarNameDirector | ||||
y, | ||||
file.srcDirectory() ); | ||||
// User definitions, set them only on first file | // User definitions, set them only on first file | |||
if( index == 0 ) | if (index == 0) { | |||
{ | for (int i = 0; i < m_widget->listVariables->topLevelItemCount(); i++) { | |||
for( int i=0; i<m_widget->listVariables->topLevelItemCount(); i++ ) | // TODO, we have to know the type of the variable! | |||
{ | QTreeWidgetItem *item = m_widget->listVariables->topLevelItem(i); | |||
// TODO, we have to know the type of the variable! | if (item) { | |||
QTreeWidgetItem* item = m_widget->listVariables->topLevelItem( i ); | EVarType eVarType = static_cast<EVarType>(item->data(1, Qt::User | |||
if( item ) | Role).toInt()); | |||
{ | const QString &name = item->text(0); | |||
EVarType eVarType = static_cast<EVarType>(item->data( 1, Qt::User | const QString &value = item->text(1); | |||
Role ).toInt()); | switch (eVarType) { | |||
const QString & name = item->text( 0 ); | default: | |||
const QString & value = item->text( 1 ); | case eVarType_String: | |||
switch( eVarType ) | m_interpreter->globalObject().setProperty(m_interpreter->glo | |||
{ | balContext(), | |||
default: | name, value.toUtf8().data()); | |||
case eVarType_String: | break; | |||
m_interpreter->globalObject().setProperty( m_interpreter- | case eVarType_Int: | |||
>globalContext(), | m_interpreter->globalObject().setProperty(m_interpreter->glo | |||
name, value.to | balContext(), | |||
Utf8().data() ); | name, value.toInt()); | |||
break; | break; | |||
case eVarType_Int: | case eVarType_Double: | |||
m_interpreter->globalObject().setProperty( m_interpreter- | m_interpreter->globalObject().setProperty(m_interpreter->glo | |||
>globalContext(), | balContext(), | |||
name, value.to | name, value.toDouble()); | |||
Int() ); | break; | |||
break; | case eVarType_Bool: | |||
case eVarType_Double: | m_interpreter->globalObject().setProperty(m_interpreter->glo | |||
m_interpreter->globalObject().setProperty( m_interpreter- | balContext(), | |||
>globalContext(), | name, | |||
name, value.to | (value.toLower() == "true" ? true : false)); | |||
Double() ); | break; | |||
break; | ||||
case eVarType_Bool: | } | |||
m_interpreter->globalObject().setProperty( m_interpreter- | } | |||
>globalContext(), | } | |||
name, | ||||
(value.toLower | ||||
() == "true" ? true : false ) ); | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
} | } | |||
} | } | |||
void ScriptPlugin::insertVariable( const char* name ) | void ScriptPlugin::insertVariable(const char *name) | |||
{ | { | |||
m_widget->textCode->insertPlainText( QString( name ) ); | m_widget->textCode->insertPlainText(QString(name)); | |||
} | } | |||
void ScriptPlugin::slotEnableControls() | void ScriptPlugin::slotEnableControls() | |||
{ | { | |||
bool bEnable = (m_widget->listVariables->selectedItems().count() != 0); | bool bEnable = (m_widget->listVariables->selectedItems().count() != 0); | |||
m_widget->buttonRemove->setEnabled( bEnable ); | m_widget->buttonRemove->setEnabled(bEnable); | |||
bEnable = !m_widget->textCode->toPlainText().isEmpty(); | bEnable = !m_widget->textCode->toPlainText().isEmpty(); | |||
m_widget->buttonSave->setEnabled( bEnable ); | m_widget->buttonSave->setEnabled(bEnable); | |||
} | } | |||
void ScriptPlugin::slotAdd() | void ScriptPlugin::slotAdd() | |||
{ | { | |||
QDialog dialog; | QPointer<QDialog> dialog = new QDialog(); | |||
Ui::ScriptPluginDialog dlg; | Ui::ScriptPluginDialog dlg; | |||
QStringList types; | QStringList types; | |||
types << i18n("String"); | types << i18n("String"); | |||
types << i18n("Int"); | types << i18n("Int"); | |||
types << i18n("Double"); | types << i18n("Double"); | |||
types << i18n("Boolean"); | types << i18n("Boolean"); | |||
dlg.setupUi( &dialog ); | dlg.setupUi(dialog); | |||
dlg.comboType->addItems( types ); | dlg.comboType->addItems(types); | |||
if( dialog.exec() == QDialog::Accepted ) | if (dialog->exec() == QDialog::Accepted) { | |||
{ | QString name = dlg.lineName->text(); | |||
QString name = dlg.lineName->text(); | QString value = dlg.lineValue->text(); | |||
QString value = dlg.lineValue->text(); | ||||
// Build a Java script statement | ||||
// Build a Java script statement | QString script = name + " = " + value + ';'; | |||
QString script = name + " = " + value + ";"; | ||||
KJSInterpreter interpreter; | ||||
KJSInterpreter interpreter; | KJSResult result = m_interpreter->evaluate(script, nullptr); | |||
KJSResult result = m_interpreter->evaluate( script, NULL ); | if (result.isException()) { | |||
if( result.isException() ) | KMessageBox::error(m_parent, | |||
{ | i18n("A JavaScript error has occurred: ") + | |||
KMessageBox::error( m_parent, | result.errorMessage(), this->name()); | |||
i18n("A JavaScript error has occured: ") + | } else { | |||
result.errorMessage(), this->name() ); | QTreeWidgetItem *item = new QTreeWidgetItem(); | |||
} | item->setText(0, name); | |||
else | item->setText(1, value); | |||
{ | item->setData(1, Qt::UserRole, QVariant(dlg.comboType->currentIndex( | |||
QTreeWidgetItem* item = new QTreeWidgetItem(); | ))); | |||
item->setText( 0, name ); | ||||
item->setText( 1, value ); | ||||
item->setData( 1, Qt::UserRole, QVariant( dlg.comboType->currentIndex | ||||
() ) ); | ||||
m_widget->listVariables->addTopLevelItem( item ); | m_widget->listVariables->addTopLevelItem(item); | |||
} | } | |||
} | } | |||
delete dialog; | ||||
} | } | |||
void ScriptPlugin::slotRemove() | void ScriptPlugin::slotRemove() | |||
{ | { | |||
QTreeWidgetItem* item = m_widget->listVariables->currentItem(); | QTreeWidgetItem *item = m_widget->listVariables->currentItem(); | |||
if( item ) | if (item) { | |||
{ | m_widget->listVariables->invisibleRootItem()->removeChild(item); | |||
m_widget->listVariables->invisibleRootItem()->removeChild( item ); | delete item; | |||
delete item; | ||||
} | } | |||
} | } | |||
void ScriptPlugin::slotLoad() | void ScriptPlugin::slotLoad() | |||
{ | { | |||
if( !m_widget->textCode->toPlainText().isEmpty() && | if (!m_widget->textCode->toPlainText().isEmpty() && | |||
KMessageBox::questionYesNo( m_parent, | KMessageBox::questionYesNo(m_parent, | |||
i18n("All currently entered definitions will | i18n("All currently entered definitions w | |||
be lost. Do you want to continue?") ) | ill be lost. Do you want to continue?")) | |||
== KMessageBox::No ) | == KMessageBox::No) { | |||
{ | return; | |||
return; | ||||
} | } | |||
KFileDialog dialog( KUrl(ScriptPlugin::s_pszFileDialogLocation), | QUrl url = QFileDialog::getOpenFileUrl(m_parent, i18n("Select file"), | |||
i18n("*|All files and directories"), | QUrl(ScriptPlugin::s_pszFileDialogLoc | |||
m_parent ); | ation)); | |||
dialog.setOperationMode( KFileDialog::Opening ); | ||||
dialog.setMode( KFile::File | KFile::ExistingOnly ); | if (!url.isEmpty()) { | |||
// Also support remote files | ||||
if( dialog.exec() == QDialog::Accepted ) | KIO::StoredTransferJob *job = KIO::storedGet(url); | |||
{ | KJobWidgets::setWindow(job, m_parent); | |||
// Also support remote files | if (job->exec()) { | |||
QString tmpFile; | m_widget->textCode->setPlainText(QString::fromLocal8Bit(job->data()) | |||
if( KIO::NetAccess::download( dialog.selectedUrl(), tmpFile, m_parent ) ) | ); | |||
{ | } else { | |||
QFile file(tmpFile); | KMessageBox::error(m_parent, job->errorString()); | |||
if( file.open(QFile::ReadOnly | QFile::Text) ) | } | |||
{ | ||||
QTextStream in(&file); | ||||
QString text = in.readAll(); | ||||
m_widget->textCode->setPlainText( text ); | ||||
file.close(); | ||||
} | ||||
else | ||||
KMessageBox::error(m_parent, i18n("Unable to open %1 for reading. | ||||
", tmpFile ) ); | ||||
KIO::NetAccess::removeTempFile( tmpFile ); | ||||
} | ||||
else | ||||
KMessageBox::error(m_parent, KIO::NetAccess::lastErrorString() ); | ||||
} | } | |||
slotEnableControls(); | slotEnableControls(); | |||
} | } | |||
void ScriptPlugin::slotSave() | void ScriptPlugin::slotSave() | |||
{ | { | |||
KFileDialog dialog( KUrl(ScriptPlugin::s_pszFileDialogLocation), | QUrl url = QFileDialog::getSaveFileUrl(m_parent, i18n("Select file"), | |||
i18n("*|All files and directories"), | QUrl(ScriptPlugin::s_pszFileDialogLoc | |||
m_parent ); | ation)); | |||
dialog.setOperationMode( KFileDialog::Saving ); | ||||
dialog.setMode( KFile::File ); | if (!url.isEmpty()) { | |||
KIO::StatJob *statJob = KIO::stat(url, KIO::StatJob::DestinationSide, 0) | ||||
if( dialog.exec() == QDialog::Accepted ) | ; | |||
{ | statJob->exec(); | |||
const KUrl url = dialog.selectedUrl(); | if (statJob->error() != KIO::ERR_DOES_NOT_EXIST) { | |||
if( KIO::NetAccess::exists( url, KIO::NetAccess::DestinationSide, m_paren | int m = KMessageBox::warningYesNo(m_parent, i18n("The file %1 alread | |||
t ) ) | y exists. " | |||
{ | "Do you want to overwrite it?", ur | |||
int m = KMessageBox::warningYesNo( m_parent, i18n("The file %1 alread | l.toDisplayString(QUrl::PreferLocalFile))); | |||
y exists. " | ||||
"Do you want to ove | if (m == KMessageBox::No) { | |||
rwrite it?", url.prettyUrl()) ); | return; | |||
} | ||||
if( m == KMessageBox::No ) | } | |||
return; | ||||
} | if (url.isLocalFile()) { | |||
QFile file(url.path()); | ||||
QString tmpName = url.path(); | if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { | |||
if( !url.isLocalFile() ) | QTextStream out(&file); | |||
{ | out << m_widget->textCode->toPlainText(); | |||
KTemporaryFile temp; | out.flush(); | |||
tmpName = temp.fileName(); | file.close(); | |||
} | } else { | |||
KMessageBox::error(m_parent, i18n("Unable to open %1 for writing | ||||
QFile file(tmpName); | .", url.path())); | |||
if( file.open(QIODevice::WriteOnly | QIODevice::Text) ) | } | |||
{ | } else { | |||
QTextStream out(&file); | KIO::StoredTransferJob *job = KIO::storedPut(m_widget->textCode->toP | |||
out << m_widget->textCode->toPlainText(); | lainText().toLocal8Bit(), url, -1); | |||
out.flush(); | KJobWidgets::setWindow(job, m_parent); | |||
file.close(); | job->exec(); | |||
if (job->error()) { | ||||
if( !url.isLocalFile() ) | KMessageBox::error(m_parent, job->errorString()); | |||
{ | } | |||
if( !KIO::NetAccess::upload( tmpName, url, m_parent ) ) | } | |||
KMessageBox::error(m_parent, KIO::NetAccess::lastErrorString( | ||||
) ); | ||||
file.remove(); | ||||
} | ||||
} | ||||
else | ||||
{ | ||||
KMessageBox::error(m_parent, i18n("Unable to open %1 for writing.", t | ||||
mpName ) ); | ||||
if( !url.isLocalFile() ) | ||||
file.remove(); | ||||
} | ||||
} | } | |||
slotEnableControls(); | slotEnableControls(); | |||
} | } | |||
void ScriptPlugin::slotTest() | void ScriptPlugin::slotTest() | |||
{ | { | |||
} | } | |||
void ScriptPlugin::slotInsertIndex() | void ScriptPlugin::slotInsertIndex() | |||
{ | { | |||
this->insertVariable( ScriptPlugin::s_pszVarNameIndex ); | this->insertVariable(ScriptPlugin::s_pszVarNameIndex); | |||
} | } | |||
void ScriptPlugin::slotInsertUrl() | void ScriptPlugin::slotInsertUrl() | |||
{ | { | |||
this->insertVariable( ScriptPlugin::s_pszVarNameUrl ); | this->insertVariable(ScriptPlugin::s_pszVarNameUrl); | |||
} | } | |||
void ScriptPlugin::slotInsertFilename() | void ScriptPlugin::slotInsertFilename() | |||
{ | { | |||
this->insertVariable( ScriptPlugin::s_pszVarNameFilename ); | this->insertVariable(ScriptPlugin::s_pszVarNameFilename); | |||
} | } | |||
void ScriptPlugin::slotInsertExtension() | void ScriptPlugin::slotInsertExtension() | |||
{ | { | |||
this->insertVariable( ScriptPlugin::s_pszVarNameExtension ); | this->insertVariable(ScriptPlugin::s_pszVarNameExtension); | |||
} | } | |||
void ScriptPlugin::slotInsertDirectory() | void ScriptPlugin::slotInsertDirectory() | |||
{ | { | |||
this->insertVariable( ScriptPlugin::s_pszVarNameDirectory ); | this->insertVariable(ScriptPlugin::s_pszVarNameDirectory); | |||
} | } | |||
void ScriptPlugin::loadConfig( KConfigGroup & group ) | void ScriptPlugin::loadConfig(KConfigGroup &group) | |||
{ | { | |||
QStringList variableNames; | QStringList variableNames; | |||
QStringList variableValues; | QStringList variableValues; | |||
QVariantList variableTypes; | QVariantList variableTypes; | |||
variableNames = group.readEntry( "JavaScriptVariableNames", variableNames | variableNames = group.readEntry("JavaScriptVariableNames", variableNames); | |||
); | variableValues = group.readEntry("JavaScriptVariableValues", variableValues) | |||
variableValues = group.readEntry( "JavaScriptVariableValues", variableValues | ; | |||
); | variableTypes = group.readEntry("JavaScriptVariableTypes", variableTypes); | |||
variableTypes = group.readEntry( "JavaScriptVariableTypes", variableTypes ) | ||||
; | int min = qMin(variableNames.count(), variableValues.count()); | |||
min = qMin(min, variableTypes.count()); | ||||
int min = qMin( variableNames.count(), variableValues.count() ); | ||||
min = qMin( min, variableTypes.count() ); | for (int i = 0; i < min; i++) { | |||
QTreeWidgetItem *item = new QTreeWidgetItem(); | ||||
for( int i=0; i < min; i++ ) | item->setText(0, variableNames[i]); | |||
{ | item->setText(1, variableValues[i]); | |||
QTreeWidgetItem* item = new QTreeWidgetItem(); | item->setData(1, Qt::UserRole, variableTypes[i]); | |||
item->setText( 0, variableNames[i] ); | ||||
item->setText( 1, variableValues[i] ); | ||||
item->setData( 1, Qt::UserRole, variableTypes[i] ); | ||||
m_widget->listVariables->addTopLevelItem( item ); | m_widget->listVariables->addTopLevelItem(item); | |||
} | } | |||
m_widget->textCode->setPlainText( group.readEntry( "JavaScriptDefinitions", QString() ) ); | m_widget->textCode->setPlainText(group.readEntry("JavaScriptDefinitions", QS tring())); | |||
} | } | |||
void ScriptPlugin::saveConfig( KConfigGroup & group ) const | void ScriptPlugin::saveConfig(KConfigGroup &group) const | |||
{ | { | |||
QStringList variableNames; | QStringList variableNames; | |||
QStringList variableValues; | QStringList variableValues; | |||
QVariantList variableTypes; | QVariantList variableTypes; | |||
for( int i=0; i<m_widget->listVariables->topLevelItemCount(); i++ ) | for (int i = 0; i < m_widget->listVariables->topLevelItemCount(); i++) { | |||
{ | QTreeWidgetItem *item = m_widget->listVariables->topLevelItem(i); | |||
QTreeWidgetItem* item = m_widget->listVariables->topLevelItem( i ); | if (item) { | |||
if( item ) | variableNames << item->text(0); | |||
{ | variableValues << item->text(1); | |||
variableNames << item->text( 0 ); | variableTypes << item->data(1, Qt::UserRole); | |||
variableValues << item->text( 1 ); | } | |||
variableTypes << item->data( 1, Qt::UserRole ); | ||||
} | ||||
} | } | |||
group.writeEntry( "JavaScriptVariableNames", variableNames ); | group.writeEntry("JavaScriptVariableNames", variableNames); | |||
group.writeEntry( "JavaScriptVariableValues", variableValues ); | group.writeEntry("JavaScriptVariableValues", variableValues); | |||
group.writeEntry( "JavaScriptVariableTypes", variableTypes ); | group.writeEntry("JavaScriptVariableTypes", variableTypes); | |||
group.writeEntry( "JavaScriptDefinitions", m_widget->textCode->toPlainText() | group.writeEntry("JavaScriptDefinitions", m_widget->textCode->toPlainText()) | |||
); | ; | |||
} | } | |||
End of changes. 51 change blocks. | ||||
315 lines changed or deleted | 264 lines changed or added |