1
////////////////////////////////////////////////////////////////////////////////
2
/*! @file ScriptJob.cpp Background script execution.
3
- Part of RAdmin project.
4
- Uses RANet - Research Assistant Net Library.
5
- Uses Qt v.5 - http://qt.io/
6
- Copyright(C) 2020, Viktor E. Bursian, St.Petersburg, Russia.
7
VBursian AT gmail DOT com
8
*///////////////////////////////////////////////////////////////////////////////
13
//------------------------------------------------------------------------------
14
//--------------------------------------------------------------- sScriptJob ---
16
sScriptJob::~sScriptJob ()
18
RANet::Log.Put(sLog::Debug,"NetTools","sScriptJob destructor");
28
sScriptJob::sScriptJob (const QString & script
31
,AbortRequestedFlag(false)
32
,FailureHappendFlag(false)
34
,TheProgressTarget(100)
36
Jobs = QString(script)
37
.replace(QRegExp("\\n"),"||")
38
.replace(QRegExp("\\r"),"||")
39
.replace(QRegExp("\\s+\\|\\|"),"||")
40
.replace(QRegExp("\\|\\|\\s+"),"||")
41
.split("||",QString::SkipEmptyParts)
42
.replaceInStrings(QRegExp("^\\s+"),"")
43
.replaceInStrings(QRegExp("\\s+$"),"")
44
.replaceInStrings(QRegExp("\\s+\\|"),"|")
45
.replaceInStrings(QRegExp("\\|\\s+"),"|")
50
int sScriptJob::Progress ()
52
QMutexLocker locker(&Mutex);
53
return (100 * TheProgress) / TheProgressTarget;
57
void sScriptJob::Abort ()
59
QMutexLocker locker(&Mutex);
60
AbortRequestedFlag = true;
64
void sScriptJob::Failure (const QString & message)
66
emit OutPar(QString("<font color=red><b>%1</b></font>")
69
QMutexLocker locker(&Mutex);
70
FailureHappendFlag = true;
75
void sScriptJob::ReportNewJobStart (QString job_title)
77
emit OutPar(QString("<font color=navy><h1>%1. %2</h1></font>")
78
.arg(TheCurrentJobNum)
84
void sScriptJob::ReportNewStageStart (QString stage_title)
86
emit OutPar(QString("<h3>______ %1 ______</h3>")
92
void sScriptJob::ReportJobConclusion (QString conclusion)
94
emit OutPar(QString("<font color=navy><b>____________ ")
95
+conclusion+"</b></font>");
99
bool sScriptJob::AbortRequested ()
101
QMutexLocker locker(&Mutex);
102
return AbortRequestedFlag;
106
bool sScriptJob::FailureHappend()
108
QMutexLocker locker(&Mutex);
109
return FailureHappendFlag;
113
void sScriptJob::SetProgress (int progress)
115
QMutexLocker locker(&Mutex);
116
TheProgress = progress;
120
void sScriptJob::SetProgressTarget (int progress_target)
122
QMutexLocker locker(&Mutex);
123
TheProgressTarget = progress_target;
127
void sScriptJob::FindAndExecute (const QString & job_name
128
,QStringList & params)
130
Failure( QString("Unknown command in the script: '%1|%2'%3.")
132
.arg(params.join("|"))
133
.arg( job_name.isEmpty() ? " (empty command name)" : "" )
138
void sScriptJob::run ()
140
emit OutPar("<font color=brown><h1>Script</h1></font>");
141
for( int i = 0 ; i < Jobs.size() ; ++i ){
142
emit OutPar(QString("<font color=brown><code>%1</code></font>")
145
emit TotalJobs(Jobs.size());
146
for( int i = 0 ; i < Jobs.size() ; ++i ){
147
TheCurrentJobNum = i+1;
148
//emit OutPar("<hr>"); -- потом вставляет черту после каждого абзаца
149
//emit OutPar("<br/>"); //it works
151
QStringList Pars( Jobs.at(i).split("|") );
152
if( Pars.size() > 0 ){
153
QString Job( Pars.front() );
155
FindAndExecute(Job,Pars);
157
if( FailureHappend() || AbortRequested() ){
161
if( AbortRequested() ){
162
emit OutPar(QString("<font color=red><big>"
163
"______________________ Execution ABORTED"
166
}else if( FailureHappend() ){
167
emit OutPar(QString("<font color=red><big>"
168
"______________________ Execution FAILED"
173
emit OutPar(QString("<font color=green><big>"
174
"______________________ ALL DONE"
180
//------------------------------------------------------------------------------