From 90825e2392b2d70e43c7a25b8a3752299a933894 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebindings@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kjsembed/kscript/Makefile.am | 17 ++++ kjsembed/kscript/javascript.cpp | 111 +++++++++++++++++++++++ kjsembed/kscript/javascript.desktop | 7 ++ kjsembed/kscript/javascript.h | 60 +++++++++++++ kjsembed/kscript/swaptabs.desktop | 5 ++ kjsembed/kscript/swaptabs.js | 75 ++++++++++++++++ kjsembed/kscript/swaptabs.ui | 172 ++++++++++++++++++++++++++++++++++++ 7 files changed, 447 insertions(+) create mode 100644 kjsembed/kscript/Makefile.am create mode 100644 kjsembed/kscript/javascript.cpp create mode 100644 kjsembed/kscript/javascript.desktop create mode 100644 kjsembed/kscript/javascript.h create mode 100644 kjsembed/kscript/swaptabs.desktop create mode 100644 kjsembed/kscript/swaptabs.js create mode 100644 kjsembed/kscript/swaptabs.ui (limited to 'kjsembed/kscript') diff --git a/kjsembed/kscript/Makefile.am b/kjsembed/kscript/Makefile.am new file mode 100644 index 00000000..2e3a6915 --- /dev/null +++ b/kjsembed/kscript/Makefile.am @@ -0,0 +1,17 @@ +INCLUDES = -I$(srcdir) -I$(top_srcdir) $(all_includes) + +kde_module_LTLIBRARIES = libjavascript.la + +libjavascript_la_SOURCES = javascript.cpp +libjavascript_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN) -no-undefined -avoid-version +libjavascript_la_LIBADD = -lkscript ../libkjsembed.la + + +METASOURCES = AUTO + +servicetypedir = $(kde_servicesdir) +servicetype_DATA = javascript.desktop + +katescriptsdir = $(kde_datadir)/kate/scripts +katescripts_DATA = swaptabs.desktop swaptabs.ui +katescripts_SCRIPTS = swaptabs.js diff --git a/kjsembed/kscript/javascript.cpp b/kjsembed/kscript/javascript.cpp new file mode 100644 index 00000000..31976981 --- /dev/null +++ b/kjsembed/kscript/javascript.cpp @@ -0,0 +1,111 @@ +/* This file is part of the KDE project + Copyright (C) 2003 Ian Reinhart Geiser (geiseri@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "javascript.h" +#include +#include +#include +#include +#include +#include + +#include +#include +//using namespace KScriptInterface; +typedef KGenericFactory JavaScriptFactory; +K_EXPORT_COMPONENT_FACTORY( libjavascript, JavaScriptFactory( "JavaScript" ) ) + +JavaScript::JavaScript(KScriptClientInterface *parent, const char *name, const QStringList &args ) +{ + ScriptClientInterface = parent; + m_jsembed = new KJSEmbed::KJSEmbedPart(this, "kjsembed_part"); + m_scriptLoc = ""; // arg1? + m_method = ""; // arg2? + m_jsembed->addObject( this, "KScriptInterface"); + m_jsembed->addObject( m_jsembed->view(), "console" ); +} + +JavaScript::~JavaScript() +{ +} + +QString JavaScript::script() const +{ + return m_scriptLoc; +} + +void JavaScript::setScript( const QString &scriptFile ) +{ + m_scriptLoc = scriptFile; +} + +void JavaScript::setScript( const QString &scriptFile, const QString &method ) +{ + m_scriptLoc = scriptFile; + m_method = method; +} + +void JavaScript::run(QObject *context, const QVariant &arg) +{ + QVariant retVal; + if( context ) m_jsembed->addObject(context); + + m_jsembed->runFile(m_scriptLoc, m_jsembed->globalObject() ); + + if( !m_method.isEmpty() ) + { + KJS::List args; + args.append( KJSEmbed::convertToValue(m_jsembed->globalExec(), arg) ); + KJS::Value val = m_jsembed->callMethod(m_method, args); + retVal = KJSEmbed::convertToVariant(m_jsembed->globalExec(),val); + } + ScriptClientInterface->done(KScriptClientInterface::ResultSuccess, retVal); +} + +void JavaScript::writeLine( const QString &msg ) +{ + ScriptClientInterface->output(msg); +} + +void JavaScript::writeWarning( const QString &msg ) +{ + ScriptClientInterface->warning(msg); +} + +void JavaScript::writeError( const QString &msg ) +{ + ScriptClientInterface->error(msg); +} + +void JavaScript::setProgress( int percent ) +{ + ScriptClientInterface->progress(percent); +} + +QString JavaScript::appID() const +{ + return kapp->dcopClient()->appId(); +} + +void JavaScript::kill() +{ + // not sure what to do here.... +} + +#include "javascript.moc" diff --git a/kjsembed/kscript/javascript.desktop b/kjsembed/kscript/javascript.desktop new file mode 100644 index 00000000..0055e68a --- /dev/null +++ b/kjsembed/kscript/javascript.desktop @@ -0,0 +1,7 @@ +[Desktop Entry] +Name=KJSEmbed Script Runner +Type=Service +X-KDE-Library=libjavascript +X-KDE-Script-Runner=JavaScript/kjs +ServiceTypes=KScriptRunner/KScriptRunner +Comment=Javascript scripts from inside the application. diff --git a/kjsembed/kscript/javascript.h b/kjsembed/kscript/javascript.h new file mode 100644 index 00000000..7b5143a7 --- /dev/null +++ b/kjsembed/kscript/javascript.h @@ -0,0 +1,60 @@ +/* This file is part of the KDE project + Copyright (C) 2003 Ian Reinhart Geiser (geiseri@kde.org) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef __javascript_h__ +#define __javascript_h__ + +#include +#include +#include + +namespace KJSEmbed { + class KJSEmbedPart; +} + +//using namespace KScriptInterface; +class JavaScript : public KScriptInterface +{ + Q_OBJECT + Q_PROPERTY( QString appID READ appID ) + +public: + JavaScript(KScriptClientInterface *parent, const char *name, const QStringList &args); + virtual ~JavaScript(); + QString script() const; + void setScript( const QString &scriptFile ); + void setScript( const QString &scriptLibFile, const QString &method ); + void run(QObject *context = 0, const QVariant &arg = 0); + void kill(); + + QString appID() const; + +public slots: + void writeLine( const QString &msg ); + void writeWarning( const QString &msg ); + void writeError( const QString &msg ); + void setProgress( int percent ); + +private: + KScriptClientInterface *ScriptClientInterface; + KJSEmbed::KJSEmbedPart *m_jsembed; + QString m_method; + QString m_scriptLoc; +}; + +#endif diff --git a/kjsembed/kscript/swaptabs.desktop b/kjsembed/kscript/swaptabs.desktop new file mode 100644 index 00000000..7de4dcfa --- /dev/null +++ b/kjsembed/kscript/swaptabs.desktop @@ -0,0 +1,5 @@ +[Desktop Entry] +Name=Swap tabs and spaces. +Comment=Swap tabs and space indentation in text. +Type=JavaScript/kjs +X-KDE-ScriptName=swaptabs.js diff --git a/kjsembed/kscript/swaptabs.js b/kjsembed/kscript/swaptabs.js new file mode 100644 index 00000000..6adbc2c3 --- /dev/null +++ b/kjsembed/kscript/swaptabs.js @@ -0,0 +1,75 @@ +#!/usr/bin/env kjscmd +var appID = "kate"; +StdDirs.addResourceType("swaptabs", StdDirs.kde_default("data") + "/kate/scripts"); + +var client = new DCOPClient(); +var config = new Config( this, "swaptabsrc" ); +var documentIndex = client.call(appID, "KateDocumentManager", "activeDocumentNumber()"); +var ui = StdDirs.findResource("swaptabs","swaptabs.ui"); +var dlg = Factory.loadui(ui); + +// Load prefs +dlg.count.value = config.readNumEntry("Spaces", 8 ); +dlg.swap.selectedId = config.readNumEntry("Mode", 0 ); + +if( dlg.exec() == 1 ) +{ + var spaces = dlg.count.value; + + var sourceText; + var destText; + if( dlg.selection.checked ) + sourceText = client.call(appID, "SelectionInterface#" + documentIndex, "selection()"); + else + sourceText = client.call(appID, "EditInterface#" + documentIndex, "text()"); + + if( dlg.swap.selectedId == 0 ) + destText = replaceSpaces( spaces, sourceText ); + else + destText = replaceTabs( spaces, sourceText ); + + if( dlg.selection.checked ) + { + if( client.call(appID, "SelectionInterface#" + documentIndex, "hasSelection()") ) + { + var startLine = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartLine()"); + var startCol = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartCol()"); + client.call(appID, "SelectionInterface#" + documentIndex, "removeSelectedText()"); + client.call(appID, "SelectionInterface#" + documentIndex, "clearSelection()"); + client.call(appID, "EditInterface#" + documentIndex, "insertText(uint,uint,QString)", startLine, startCol, destText); + } + else + { + alert("You must first select text."); + return false; + } + } + else + client.call(appID, "EditInterface#" + documentIndex, "setText(QString)", destText ); + + // save prefs + config.writeNumEntry("Spaces", dlg.count.value ); + config.writeNumEntry("Mode", dlg.swap.selectedId ); +} + +function replaceSpaces( count, text ) +{ + var regExp = new RegExp("[ ]{"+count+","+count+"}", "g"); + regExp.mulitline = true; + + returnText = text.replace( regExp, "\t"); + return returnText; +} + +function replaceTabs( count, text ) +{ + + var regExp = new RegExp("[\t]","g"); + regExp.mulitline = true; + + var spaces = ""; + for( var idx = 0; idx < count; ++idx) + spaces += " "; + returnText = text.replace( regExp, spaces); + return returnText; +} diff --git a/kjsembed/kscript/swaptabs.ui b/kjsembed/kscript/swaptabs.ui new file mode 100644 index 00000000..8cb243f8 --- /dev/null +++ b/kjsembed/kscript/swaptabs.ui @@ -0,0 +1,172 @@ + +MyDialog + + + MyDialog + + + + 0 + 0 + 219 + 176 + + + + Swap Tabs & Spaces + + + true + + + + unnamed + + + + layout2 + + + + unnamed + + + + textLabel1 + + + Number of spaces for a tab: + + + + + count + + + 4 + + + + + + + swap + + + Swap + + + 0 + + + + unnamed + + + + radioButton1 + + + Tabs for spaces + + + + + radioButton2 + + + Spaces for tabs + + + + + + + selection + + + Substitute only in current selection + + + + + Layout1 + + + + unnamed + + + 0 + + + 6 + + + + Horizontal Spacing2 + + + Horizontal + + + Expanding + + + + 20 + 20 + + + + + + buttonOk + + + &OK + + + + + + true + + + true + + + + + buttonCancel + + + &Cancel + + + + + + true + + + + + + + + + buttonOk + clicked() + MyDialog + accept() + + + buttonCancel + clicked() + MyDialog + reject() + + + + -- cgit v1.2.1