source: src-qt4/pc-sysmanager/longProc.cpp @ 8f95077

releng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 8f95077 was 8f95077, checked in by Ken Moore <ken@…>, 10 months ago

Large update to the new GIT usage for the system manager. It should be almost finished, it still just needs the proper branch detection for source checkouts (and maybe ports checkouts, but I think we always want the master branch for ports by default).

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include "longProc.h"
2
3
4LongProc::LongProc(){
5  //initialize the internal worker process
6  process = new QProcess;
7  process->setProcessChannelMode(QProcess::MergedChannels);
8  connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(parseUpdate()) );
9  connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procDone()) );
10  running = false; stopped = false;
11  cmdList.clear(); dirList.clear(); infoList.clear();
12  timer = new QTimer();
13        timer->setSingleShot(true);
14        timer->setInterval(10000); //10 second ping to make sure user knows it is still running
15  connect(timer, SIGNAL(timeout()), this, SLOT(procTimeout()) );
16}
17
18LongProc::~LongProc(){
19  //Clean up the internal worker process
20  delete process;
21}
22       
23// ========================
24//     PUBLIC WRAPPER FUNCTIONS
25// ========================
26bool LongProc::startCMDs(QStringList cmds, QStringList dirs, QStringList info){
27  //Check that it is not already running
28  if(running){ return false; }
29  //Now check input lengths
30  if( (cmds.length() != dirs.length()) || (cmds.length() != info.length()) && !cmds.isEmpty() ){
31    qDebug() << "Invalid input lengths: all three inputs must have the same number of strings";
32    return false;
33  }
34  //Now save the inputs internally
35  cmdList = cmds; dirList = dirs; infoList = info;
36  //Now get the first command running
37  currentItem = 0;
38  if( !infoList[currentItem].isEmpty() ){ emit ProcMessage("--- "+infoList[currentItem]+" ---"); }
39  if( !dirList[currentItem].isEmpty() && QFile::exists(dirList[currentItem]) ){ process->setWorkingDirectory(dirList[currentItem]); }
40  timer->start();
41  process->start(cmdList[currentItem]);
42  running = true;
43  return running;
44}
45
46void LongProc::stopProc(){
47  stopped = true;
48  process->terminate();
49}
50// ====================
51//   STATIC PUBLIC FUNCTIONS
52// ====================
53bool LongProc::quickCMD(QString dir, QString cmd, QStringList args){
54  //Run a quick command without looking for output other than success/failure
55  QProcess *proc = new QProcess;
56  if( !dir.isEmpty() && QFile::exists(dir) ){ proc->setWorkingDirectory(dir); }
57  if(args.isEmpty()){ proc->start(cmd); }
58  else{ proc->start(cmd, args); }
59  while(!proc->waitForFinished(300)){ QCoreApplication::processEvents(); }
60  return (proc->exitCode() == 0);
61}
62
63// ==============
64//     PRIVATE SLOTS
65// ==============
66void LongProc::parseUpdate(){
67  while(process->canReadLine()){
68    QString output = process->readLine().simplified();
69    //could add a check here to not send the message for empty lines
70    emit ProcMessage(output);
71  }
72  timer->start(); //reset the timer
73}
74
75void LongProc::procDone(){
76  //Start the next command if there is one
77  bool success = (process->exitCode() == 0);
78  if( currentItem+1 < cmdList.length() && success && !stopped){
79    currentItem++;
80    if( !infoList[currentItem].isEmpty() ){ emit ProcMessage("\n--- "+infoList[currentItem]+" ---"); }
81    if( !dirList[currentItem].isEmpty() && QFile::exists(dirList[currentItem]) ){ process->setWorkingDirectory(dirList[currentItem]); }
82    process->start(cmdList[currentItem]);
83    timer->start(); //reset timer
84  }else{
85    //All finished
86    if(stopped){
87      emit ProcMessage("\n ---- KILLED ----");
88      stopped=false; //reset flag
89    }else if(success){
90      emit ProcMessage("\n ---- FINISHED ----");
91    }else{
92      emit ProcMessage("\n ---- ERROR ----");
93    }
94    running = false;
95    emit ProcFinished();
96    timer->stop();
97  }
98}
99
100void LongProc::procTimeout(){
101  emit ProcMessage("."); //just to make sure that a quiet and long process still looks active to the user
102  timer->start(); //restart the timer
103}
Note: See TracBrowser for help on using the repository browser.