File indexing completed on 2024-12-01 06:51:47
0001 /*************************************************************************** 0002 cscript.cpp - one script 0003 ------------------- 0004 begin : So dec 7 2002 0005 copyright : (C) 2002-2008 by Tomas Mecir 0006 email : kmuddy@kmuddy.com 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 ***************************************************************************/ 0017 0018 #include "cscript.h" 0019 0020 #include "cactionmanager.h" 0021 #include "crunninglist.h" 0022 #include "crunningscript.h" 0023 #include "cscriptlist.h" 0024 0025 #include <KLocalizedString> 0026 #include <QProcess> 0027 0028 cScript::cScript (cList *list) : cListObject (list) 0029 { 0030 runningCount = 0; 0031 } 0032 0033 cScript::~cScript() 0034 { 0035 //nothing here 0036 } 0037 0038 cList::TraverseAction cScript::traverse (int traversalType) 0039 { 0040 cScriptList *sl = (cScriptList *) list(); 0041 if (name() != sl->nameToFind()) // are we the correct script ? 0042 return cList::Continue; 0043 sl->setNameFound (); 0044 0045 if (traversalType == SCRIPT_FIND) 0046 return cList::Stop; 0047 if (traversalType == SCRIPT_EXECUTE) 0048 { 0049 sl->runScript (this); 0050 return cList::Stop; 0051 } 0052 return cList::Stop; // unknown action 0053 } 0054 0055 void cScript::updateVisibleName () 0056 { 0057 if (name().isEmpty()) 0058 cListObject::updateVisibleName(); 0059 else 0060 setVisibleName (name()); 0061 } 0062 0063 cRunningScript *cScript::prepareToLaunch (const QString ¶ms) 0064 { 0065 //do nothing if we're a single-instance script that is already running 0066 if (boolVal ("single-instance") && (runningCount != 0)) 0067 { 0068 cActionManager::self()->invokeEvent ("message", list()->session(), i18n ("Sorry, single-instance script.")); 0069 return 0; 0070 } 0071 0072 cRunningList *rl = dynamic_cast<cRunningList *>(cActionManager::self()->object ("runninglist", list()->session())); 0073 if (!rl) return 0; 0074 0075 //create a new instance of cRunningScript 0076 cRunningScript *script = new cRunningScript (this); 0077 0078 //setup its parameters: 0079 0080 script->sendusercommands = boolVal ("send-user-commands"); 0081 script->useadvcomm = boolVal ("adv-communication"); 0082 script->flowcontrol = boolVal ("flow-control"); 0083 0084 //create a QProcess instance 0085 script->process = new QProcess; 0086 0087 script->process->setWorkingDirectory (strVal ("work-directory")); 0088 0089 //establish socket if needed 0090 if (boolVal ("enable-variables")) 0091 script->establishSocket (list()->session()); 0092 0093 //split the command into parts... 0094 QString cmd = strVal ("command"); 0095 QString pars = params.trimmed (); 0096 if (pars.length() > 0) 0097 cmd += QString(" ") + pars; 0098 QString oneparam; 0099 bool inquotedparam = false; 0100 QChar quotechar; 0101 bool wasbackslash = false; 0102 for (int i = 0; i < cmd.length(); i++) 0103 { 0104 if (wasbackslash) 0105 { 0106 oneparam += cmd[i]; 0107 wasbackslash = false; 0108 } 0109 else 0110 if (cmd[i].isSpace()) 0111 { 0112 if (inquotedparam) 0113 oneparam += cmd[i]; 0114 else 0115 if (oneparam.length() > 0) 0116 { 0117 script->args << oneparam; //add this parameter! 0118 oneparam = QString(); 0119 } 0120 } 0121 else 0122 if (cmd[i].toLatin1() == '\\') //backslash 0123 wasbackslash = true; 0124 else 0125 if (inquotedparam && (cmd[i] == quotechar)) //quote ends 0126 inquotedparam = false; 0127 else 0128 if ((cmd[i].toLatin1() == '\'') || (cmd[i].toLatin1() == '"')) 0129 { //quote starts 0130 inquotedparam = true; 0131 quotechar = cmd[i]; 0132 } 0133 else //normal character 0134 oneparam += cmd[i]; 0135 } 0136 if (oneparam.length() > 0) 0137 script->args << oneparam; //add last parameter 0138 0139 // first argument is the command name 0140 script->command = script->args.takeFirst(); 0141 0142 // Command & parameters are set 0143 //return that instance 0144 return script; 0145 } 0146 0147 void cScript::scriptIsStarting () 0148 { 0149 runningCount++; 0150 } 0151 0152 void cScript::scriptIsTerminating () 0153 { 0154 runningCount--; 0155 }