You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
5.4 KiB
214 lines
5.4 KiB
15 years ago
|
/***************************************************************************
|
||
|
commandengine.cpp - kfr commands feature class
|
||
|
-------------------
|
||
|
begin : fri aug 13 15:29:46 CEST 2004
|
||
|
|
||
|
copyright : (C) 2004 Emiliano Gulmini
|
||
|
email : emi_barbarossa@yahoo.it
|
||
|
***************************************************************************/
|
||
|
|
||
|
/***************************************************************************
|
||
|
* *
|
||
|
* 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. *
|
||
|
* *
|
||
|
***************************************************************************/
|
||
|
|
||
|
// QT
|
||
14 years ago
|
#include <tqdatetime.h>
|
||
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
14 years ago
|
#include <tqdom.h>
|
||
15 years ago
|
|
||
|
// KDE
|
||
|
#include <kuser.h>
|
||
|
#include <krandomsequence.h>
|
||
|
#include <kprocess.h>
|
||
|
|
||
|
// local
|
||
|
#include "commandengine.h"
|
||
|
|
||
14 years ago
|
TQString CommandEngine::datetime(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(arg);
|
||
|
if(opt == "iso")
|
||
13 years ago
|
return TQDateTime::currentDateTime(Qt::LocalTime).toString(Qt::ISODate);
|
||
15 years ago
|
if(opt == "local")
|
||
13 years ago
|
return TQDateTime::currentDateTime(Qt::LocalTime).toString(Qt::LocalDate);
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::user(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(arg);
|
||
|
KUser u;
|
||
|
if(opt == "uid")
|
||
14 years ago
|
return TQString::number(u.uid(),10);
|
||
15 years ago
|
if(opt == "gid")
|
||
14 years ago
|
return TQString::number(u.gid(),10);
|
||
15 years ago
|
if(opt == "loginname")
|
||
|
return u.loginName();
|
||
|
if(opt == "fullname")
|
||
|
return u.fullName();
|
||
|
if(opt == "homedir")
|
||
|
return u.homeDir();
|
||
|
if(opt == "shell")
|
||
|
return u.shell();
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::loadfile(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(arg);
|
||
|
|
||
14 years ago
|
TQFile f(opt);
|
||
14 years ago
|
if(!f.open(IO_ReadOnly)) return TQString();
|
||
15 years ago
|
|
||
14 years ago
|
TQTextStream t(&f);
|
||
15 years ago
|
|
||
14 years ago
|
TQString s = t.read();
|
||
15 years ago
|
|
||
|
f.close();
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::empty(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(opt);
|
||
|
Q_UNUSED(arg);
|
||
|
return "";
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::mathexp(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
/* We will use bc 1.06 by Philip A. Nelson <philnelson@acm.org> */
|
||
|
//Q_UNUSED(opt);
|
||
|
Q_UNUSED(arg);
|
||
|
|
||
14 years ago
|
TQString tempOpt = opt;
|
||
13 years ago
|
tempOpt.replace("ln","l");
|
||
|
tempOpt.replace("sin","s");
|
||
|
tempOpt.replace("cos","c");
|
||
|
tempOpt.replace("arctan","a");
|
||
|
tempOpt.replace("exp","e");
|
||
15 years ago
|
|
||
14 years ago
|
TQString program = "var=("+tempOpt+");print var";
|
||
|
TQString script = "echo '"+program+"' | bc -l;";
|
||
15 years ago
|
|
||
12 years ago
|
TDEProcess* proc = new TDEProcess();
|
||
15 years ago
|
|
||
|
proc->setUseShell(true);
|
||
|
|
||
|
*(proc) << script;
|
||
|
|
||
12 years ago
|
connect(proc, TQT_SIGNAL(receivedStdout(TDEProcess*,char*,int)), this, TQT_SLOT(slotGetScriptOutput(TDEProcess*,char*,int)));
|
||
|
connect(proc, TQT_SIGNAL(receivedStderr(TDEProcess*,char*,int)), this, TQT_SLOT(slotGetScriptError(TDEProcess*,char*,int)));
|
||
|
connect(proc, TQT_SIGNAL(processExited(TDEProcess*)), this, TQT_SLOT(slotProcessExited(TDEProcess*)));
|
||
15 years ago
|
|
||
12 years ago
|
//Through slotGetScriptOutput, m_processOutput contains the result of the TDEProcess call
|
||
|
if(!proc->start(TDEProcess::Block, TDEProcess::All))
|
||
15 years ago
|
{
|
||
14 years ago
|
return TQString();
|
||
15 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
proc->wait();
|
||
|
}
|
||
|
if(proc)
|
||
|
delete proc;
|
||
|
|
||
14 years ago
|
TQString tempbuf = m_processOutput;
|
||
14 years ago
|
m_processOutput = TQString();
|
||
15 years ago
|
|
||
|
return tempbuf;
|
||
|
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::random(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(arg);
|
||
|
long seed;
|
||
|
if(opt.isEmpty())
|
||
|
{
|
||
14 years ago
|
TQDateTime dt;
|
||
15 years ago
|
seed = dt.toTime_t();
|
||
|
}
|
||
|
else
|
||
|
seed = opt.toLong();
|
||
|
|
||
|
KRandomSequence seq(seed);
|
||
14 years ago
|
return TQString::number(seq.getLong(1000000),10);
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::stringmanip(const TQString& opt, const TQString& arg)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(opt);
|
||
|
Q_UNUSED(arg);
|
||
|
return "";
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString CommandEngine::variableValue(const TQString &variable)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString s = variable;
|
||
15 years ago
|
|
||
|
s.remove("[$").remove("$]").remove(" ");
|
||
|
|
||
13 years ago
|
if(s.contains(":") == 0)
|
||
15 years ago
|
return variable;
|
||
|
else
|
||
|
{
|
||
14 years ago
|
TQString leftValue = s.section(":",0,0),
|
||
15 years ago
|
midValue = s.section(":",1,1),
|
||
|
rightValue = s.section(":",2,2);
|
||
|
|
||
14 years ago
|
TQString opt = midValue;
|
||
|
TQString arg = rightValue;
|
||
15 years ago
|
|
||
|
if(leftValue == "stringmanip")
|
||
|
return stringmanip(opt, arg);
|
||
|
if(leftValue == "datetime")
|
||
|
return datetime(opt, arg);
|
||
|
if(leftValue == "user")
|
||
|
return user(opt, arg);
|
||
|
if(leftValue == "loadfile")
|
||
|
return loadfile(opt, arg);
|
||
|
if(leftValue == "empty")
|
||
|
return empty(opt, arg);
|
||
|
if(leftValue == "mathexp")
|
||
|
return mathexp(opt, arg);
|
||
|
if(leftValue == "random")
|
||
|
return random(opt, arg);
|
||
|
|
||
|
return variable;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//SLOTS
|
||
12 years ago
|
void CommandEngine::slotGetScriptError(TDEProcess* proc, char* s, int i)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(proc);
|
||
|
Q_UNUSED(proc);
|
||
14 years ago
|
TQCString temp(s,i+1);
|
||
15 years ago
|
if(temp.isEmpty() || temp == "\n") return;
|
||
|
}
|
||
|
|
||
12 years ago
|
void CommandEngine::slotGetScriptOutput(TDEProcess* proc, char* s, int i)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(proc);
|
||
14 years ago
|
TQCString temp(s,i+1);
|
||
15 years ago
|
|
||
|
if(temp.isEmpty() || temp == "\n") return;
|
||
|
|
||
14 years ago
|
m_processOutput += TQString::fromLocal8Bit(temp);
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
void CommandEngine::slotProcessExited(TDEProcess* proc)
|
||
15 years ago
|
{
|
||
|
Q_UNUSED(proc);
|
||
|
}
|
||
|
|
||
|
#include "commandengine.moc"
|