Guest User

portsnapprogress.h

a guest
Dec 27th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #ifndef _PORTSNAP_PROGRESS_H
  2. #define _PORTSNAP_PROGRESS_H
  3.  
  4. #include <QObject>
  5. #include <QProcess>
  6. #include <QFile>
  7. #include <QString>
  8. #include <QStringList>
  9. #include <QCoreApplication>
  10.  
  11. class PortsnapProc : public QObject{
  12. Q_OBJECT
  13. public:
  14.  
  15. PortsnapProc(){
  16. process = new QProcess;
  17. process->setProcessChannelMode(QProcess::MergedChannels);
  18. connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(parseUpdate()) );
  19. connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procDone()) );
  20. }
  21. ~PortsnapProc(){
  22. delete process;
  23. }
  24.  
  25. bool startInitPorts(){
  26. running = true;
  27. if( !quickCMD("", "rm -rf /usr/ports/*") ){ running = false; return false;}
  28. if( !quickCMD("/usr/ports", "git init") ){ running = false; }
  29. else{
  30. //start the long process
  31. process->setWorkingDirectory("/usr/ports/.git");
  32. process->start("git remote add origin https://www.github.com/pcbsd/freebsd-ports.git");
  33. }
  34. return running;
  35. }
  36.  
  37. bool startPorts(){
  38. running = true;
  39. if( !quickCMD("", "/usr/ports/.git" "git fetch --depth=1") ){ running = false; return false;}
  40. else{
  41. //start the long process
  42. process->setWorkingDirectory("/usr/ports");
  43. process->start("git checkout -t origin/master");
  44. }
  45. return running;
  46. }
  47.  
  48. bool startInitSource(){
  49. running = true;
  50. if( !quickCMD("", "rm -rf /usr/src/*") ){ running = false; return false;}
  51. if( !quickCMD("/usr/src", "git init") ){ running = false; }
  52. else{
  53. //start the long process
  54. process->setWorkingDirectory("/usr/src/.git");
  55. process->start("git remote add origin https://www.github.com/pcbsd/freebsd.git");
  56. }
  57. return running;
  58. }
  59.  
  60. bool startSource(){
  61. running = true;
  62. if( !quickCMD("", "/usr/src/.git" "git fetch --depth=1") ){ running = false; return false;}
  63. else{
  64. //start the long process
  65. process->setWorkingDirectory("/usr/ports");
  66. process->start("git checkout -t origin/master");
  67. }
  68. return running;
  69. }
  70.  
  71. bool isRunning(){ return running; } //so you can double check whether it is still running
  72.  
  73. static bool quickCMD(QString dir, QString cmd, QStringList args = QStringList()){
  74. //Run a quick command without looking for output other than success/failure
  75. QProcess *proc = new QProcess;
  76. if( !dir.isEmpty() && QFile::exists(dir) ){ proc->setWorkingDirectory(dir); }
  77. if(args.isEmpty()){ proc->start(cmd); }
  78. else{ proc->start(cmd, args); }
  79. while(!proc->waitForFinished(300)){ QCoreApplication::processEvents(); }
  80. return (proc->exitCode() == 0);
  81. }
  82.  
  83. void init(bool, QString);
  84.  
  85. private:
  86. QProcess *process;
  87. bool running;
  88. PortsnapProc *longProcess;
  89.  
  90. private slots:
  91. void parseUpdate(){
  92. while(process->canReadLine()){
  93. QString output = process->readLine().simplified();
  94. emit ProcMessage(output);
  95. }
  96. }
  97.  
  98. void procDone(){
  99. running = false;
  100. emit ProcFinished(process->exitCode() == 0);
  101. }
  102.  
  103. signals:
  104. void ProcMessage(QString); //a new message while it is running
  105. void ProcFinished(bool); //the process result (good/bad)
  106. };
  107. #endif
Add Comment
Please, Sign In to add comment