Kate
katecommandlinescript.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2009 Dominik Haumann <dhaumann kde org> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "katecommandlinescript.h" 00022 00023 #include <QScriptValue> 00024 #include <QScriptEngine> 00025 00026 #include <klocale.h> 00027 00028 #include "katedocument.h" 00029 #include "kateview.h" 00030 #include "katecmd.h" 00031 #include "kshell.h" 00032 00033 KateCommandLineScript::KateCommandLineScript(const QString &url, const KateCommandLineScriptHeader &header) 00034 : KateScript(url) 00035 , m_commandHeader(header) 00036 { 00037 KateCmd::self()->registerCommand (this); 00038 } 00039 00040 KateCommandLineScript::~KateCommandLineScript() 00041 { 00042 KateCmd::self()->unregisterCommand (this); 00043 } 00044 00045 const KateCommandLineScriptHeader& KateCommandLineScript::commandHeader() 00046 { 00047 return m_commandHeader; 00048 } 00049 00050 00051 bool KateCommandLineScript::callFunction(const QString& cmd, const QStringList args, QString &errorMessage) 00052 { 00053 clearExceptions(); 00054 QScriptValue command = function(cmd); 00055 if(!command.isValid()) { 00056 errorMessage = i18n("Function '%1' not found in script: %2", cmd, url()); 00057 return false; 00058 } 00059 00060 // add the arguments that we are going to pass to the function 00061 QScriptValueList arguments; 00062 foreach (const QString& arg, args) { 00063 arguments << QScriptValue(m_engine, arg); 00064 } 00065 00066 QScriptValue result = command.call(QScriptValue(), arguments); 00067 // error during the calling? 00068 if(m_engine->hasUncaughtException()) { 00069 errorMessage = backtrace(result, i18n("Error calling %1", cmd)); 00070 return false; 00071 } 00072 00073 return true; 00074 } 00075 00076 ScriptActionInfo KateCommandLineScript::actionInfo(const QString& cmd) 00077 { 00078 clearExceptions(); 00079 QScriptValue actionFunc = function("action"); 00080 if(!actionFunc.isValid()) { 00081 kDebug() << i18n("Function 'action' not found in script: %1", url()); 00082 return ScriptActionInfo(); 00083 } 00084 00085 // add the arguments that we are going to pass to the function 00086 QScriptValueList arguments; 00087 arguments << cmd; 00088 00089 QScriptValue result = actionFunc.call(QScriptValue(), arguments); 00090 // error during the calling? 00091 if(m_engine->hasUncaughtException()) { 00092 displayBacktrace(result, i18n("Error calling action(%1)", cmd)); 00093 return ScriptActionInfo(); 00094 } 00095 00096 ScriptActionInfo info; 00097 info.setCommand(cmd); 00098 info.setText(result.property("text").toString()); 00099 info.setIcon(result.property("icon").toString()); 00100 info.setCategory(result.property("category").toString()); 00101 info.setInteractive(result.property("interactive").toBool()); 00102 info.setShortcut(result.property("shortcut").toString()); 00103 00104 return info; 00105 } 00106 00107 const QStringList &KateCommandLineScript::cmds () 00108 { 00109 return m_commandHeader.functions(); 00110 } 00111 00112 bool KateCommandLineScript::exec (KTextEditor::View *view, const QString &_cmd, QString &errorMsg) 00113 { 00114 KShell::Errors errorCode; 00115 QStringList args(KShell::splitArgs(_cmd, KShell::NoOptions, &errorCode)); 00116 00117 if (errorCode != KShell::NoError) { 00118 errorMsg = i18n("Bad quoting in call: %1. Please escape single quotes with a backslash.", _cmd); 00119 return false; 00120 } 00121 00122 QString cmd(args.first()); 00123 args.removeFirst(); 00124 00125 if (!view) { 00126 errorMsg = i18n("Could not access view"); 00127 return false; 00128 } 00129 00130 if (setView(qobject_cast<KateView*>(view))) { 00131 // setView fails, if the script cannot be loaded 00132 return callFunction(cmd, args, errorMsg); 00133 } 00134 00135 return false; 00136 } 00137 00138 00139 bool KateCommandLineScript::help(KTextEditor::View* view, const QString& cmd, QString &msg) 00140 { 00141 if (!setView(qobject_cast<KateView*>(view))) { 00142 // setView fails, if the script cannot be loaded 00143 return false; 00144 } 00145 00146 clearExceptions(); 00147 QScriptValue helpFunction = function("help"); 00148 if(!helpFunction.isValid()) { 00149 return false; 00150 } 00151 00152 // add the arguments that we are going to pass to the function 00153 QScriptValueList arguments; 00154 arguments << QScriptValue(m_engine, cmd); 00155 00156 QScriptValue result = helpFunction.call(QScriptValue(), arguments); 00157 00158 // error during the calling? 00159 if(m_engine->hasUncaughtException()) { 00160 msg = backtrace(result, i18n("Error calling 'help %1'", cmd)); 00161 return false; 00162 } 00163 00164 if (result.isUndefined() || !result.isString()) { 00165 kDebug(13050) << i18n("No help specified for command '%1' in script %2", cmd, url()); 00166 return false; 00167 } 00168 msg = result.toString(); 00169 00170 return !msg.isEmpty(); 00171 } 00172 00173 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference