// execthread.cpp
#include "execthread.h"
// Function: ExecThread::ExecThread
// Description: The constructor
// Parameters:
// QString Conf - the command to be executed, or the conf file to be read where the command is
// int RunTimes - number of times the command should be executed. Default is 0, meaning
// unlimited times ( till stop() method is called)
// bool config - if true the first parameter (Conf) is the path to the config file where the
// command is situated. By default it's false which means Conf holds the
// command to be executed
ExecThread
::ExecThread(const QString Conf,
int RunTimes,
bool config
){
stopped = true;
// setting the command to be executed
if (!config)
CommandStr = Conf;
else
CommandStr = GetCommandStr(Conf);
// should the command be exectuted limited number of times
if (RunTimes > 0)
{
ExecTimes = RunTimes;
RepeatMode = true;
}
else
{
ExecTimes = 0;
RepeatMode = false;
}
CommandStr = CommandStr.stripWhiteSpace();
CommandStr = CommandStr.simplifyWhiteSpace();
// create the process which will execute the command
// QObject::connect( ExecProcess, SIGNAL(processExited()),
// this, SLOT(processExitedSlot()) );
connect( ExecProcess, SIGNAL(processExited()),
this, SLOT(processExitedSlot()) );
SetArgs(CommandStr);
}
// Function: ExecThread::~ExecThread
// Description: The destructor
ExecThread::~ExecThread()
{
delete ExecProcess;
}
// Funciton: GetCommandStr
// Description: Parser for the file with the command string. It accepts # style comments
// Parameters:
// QString Path - the path to the file
{
int pos;
file.setName(Path);
if (!file.exists()) return "";
if (file.open(IO_ReadOnly))
while (( !stream.atEnd() ) or (CmdStr == ""))
{
line = stream.readLine();
if ((pos = line.find('#',0))>=0) // if there is a # in the line
{
if (pos > 0) // if the line is like " aplay /usr/test.wav # play test.wav"
CmdStr = line.left(pos);
}
else
{
CmdStr = line;
}
}
return CmdStr;
}
// Funciton: setCommand
// Description: Sets the command to be executed
// Parameters:
// QString Cmd - the command
void ExecThread
::setCommand(const QString Cmd
) {
CommandStr = Cmd;
CommandStr = CommandStr.stripWhiteSpace();
CommandStr = CommandStr.simplifyWhiteSpace();
SetArgs(CommandStr);
return;
}
// Funciton: setExecTimes
// Description: Sets the number of times command should be exectuted.
// Calling this function autmatically sets RepeatMode to true
// Parameters:
// int &ExecTimesParam - number of times command to be executed
void ExecThread::setExecTimes(const int &ExecTimesParam)
{
if (ExecTimesParam > 0)
{
ExecTimes = ExecTimesParam;
RepeatMode = true;
}
else
{
ExecTimes = 0;
RepeatMode = false;
}
}
// Funciton: run
// Description: Executes the command until stopped is true. In RepeatMode
// decreases the number of execs to be done be executed
void ExecThread::run()
{
stopped = false;
ExecProcess->start();
if (RepeatMode)
{
ExecTimes--;
if (ExecTimes == 0)
{
stopped = true;
emit ExecFinished();
}
}
}
// Function: stop
// Description: Sets stopped to true. Stops the execution, but the command that is currently
// running is not terminated
void ExecThread::stop()
{
stopped = true;
}
// Function: SetArgs
// Description: Sets the command arguments to ExecProcess
// Parameters:
// QString Cmd - the command
void ExecThread
::SetArgs(QString Cmd
) {
int pos;
ExecProcess->clearArguments();
while ((pos = Cmd.find(" ",0))> 0)
{
ExecProcess->addArgument(Cmd.left(pos));
Cmd = Cmd.mid(pos + 1, 1000);
}
ExecProcess->addArgument(Cmd);
}
// Function: execsTillStop
// Description: Returns the number of exectutions till it stops ExecProcess
int ExecThread::execsTillStop()
{
return ExecTimes;
}
void ExecThread::processExitedSlot()
{
if (!stopped)
{
ExecProcess->start();
if (RepeatMode)
{
ExecTimes--;
if (ExecTimes == 0)
{
stopped = true;
emit ExecFinished();
}
}
}
}
Bookmarks