Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef _PORTSNAP_PROGRESS_H
- #define _PORTSNAP_PROGRESS_H
- #include <QObject>
- #include <QProcess>
- #include <QFile>
- #include <QString>
- #include <QStringList>
- #include <QCoreApplication>
- class PortsnapProc : public QObject{
- Q_OBJECT
- public:
- PortsnapProc(){
- process = new QProcess;
- process->setProcessChannelMode(QProcess::MergedChannels);
- connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(parseUpdate()) );
- connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procDone()) );
- }
- ~PortsnapProc(){
- delete process;
- }
- bool startInitPorts(){
- running = true;
- if( !quickCMD("", "rm -rf /usr/ports/*") ){ running = false; return false;}
- if( !quickCMD("/usr/ports", "git init") ){ running = false; }
- else{
- //start the long process
- process->setWorkingDirectory("/usr/ports/.git");
- process->start("git remote add origin https://www.github.com/pcbsd/freebsd-ports.git");
- }
- return running;
- }
- bool startPorts(){
- running = true;
- if( !quickCMD("", "/usr/ports/.git" "git fetch --depth=1") ){ running = false; return false;}
- else{
- //start the long process
- process->setWorkingDirectory("/usr/ports");
- process->start("git checkout -t origin/master");
- }
- return running;
- }
- bool startInitSource(){
- running = true;
- if( !quickCMD("", "rm -rf /usr/src/*") ){ running = false; return false;}
- if( !quickCMD("/usr/src", "git init") ){ running = false; }
- else{
- //start the long process
- process->setWorkingDirectory("/usr/src/.git");
- process->start("git remote add origin https://www.github.com/pcbsd/freebsd.git");
- }
- return running;
- }
- bool startSource(){
- running = true;
- if( !quickCMD("", "/usr/src/.git" "git fetch --depth=1") ){ running = false; return false;}
- else{
- //start the long process
- process->setWorkingDirectory("/usr/ports");
- process->start("git checkout -t origin/master");
- }
- return running;
- }
- bool isRunning(){ return running; } //so you can double check whether it is still running
- static bool quickCMD(QString dir, QString cmd, QStringList args = QStringList()){
- //Run a quick command without looking for output other than success/failure
- QProcess *proc = new QProcess;
- if( !dir.isEmpty() && QFile::exists(dir) ){ proc->setWorkingDirectory(dir); }
- if(args.isEmpty()){ proc->start(cmd); }
- else{ proc->start(cmd, args); }
- while(!proc->waitForFinished(300)){ QCoreApplication::processEvents(); }
- return (proc->exitCode() == 0);
- }
- void init(bool, QString);
- private:
- QProcess *process;
- bool running;
- PortsnapProc *longProcess;
- private slots:
- void parseUpdate(){
- while(process->canReadLine()){
- QString output = process->readLine().simplified();
- emit ProcMessage(output);
- }
- }
- void procDone(){
- running = false;
- emit ProcFinished(process->exitCode() == 0);
- }
- signals:
- void ProcMessage(QString); //a new message while it is running
- void ProcFinished(bool); //the process result (good/bad)
- };
- #endif
Add Comment
Please, Sign In to add comment