#include "mainwindow.h" #include "ui_mainwindow.h" #include "downloadfile.h" #include "tools.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; downloadFile::downloadFile() { } void downloadFile::cancelled(int pid) { if (kill(pid, SIGTERM) == -1) { //TODO gestion erreur kill } } void downloadFile::download(MainWindow *mw) { string line; string errorRsync; int pos; array argv; stringstream output; vector v; int value; char buffer[4096]; argv[0] = "/usr/bin/rsync"; if (mw->connexion.bandwidthLimit == 0) { argv[1] = "--bwlimit=1000P"; }else { output << mw->connexion.bandwidthLimit; argv[1] = "--bwlimit=" + output.str() + mw->connexion.bandwidthLimitUnit; } argv[2] = "--port=" + to_string(mw->connexion.port); argv[3] = "-P"; argv[4] = mw->connexion.server + "::" + mw->downloading.service + "/" + mw->downloading.path; argv[5] = mw->downloading.savePath + "/"; argv[6] = ""; //launching downloading thread FILE * fp = popen2(argv, "r", mw->downloading.pid); if (!fp) { throw runtime_error("popen2() failed!"); return; } while (fgets(buffer, 4096, fp) != nullptr) { if (this->canceled == true) { return; } line = buffer; pos = line.find('%'); if (pos != -1) { line.erase(pos); pos = line.find_last_of(' '); if (pos != -1) { line.erase(0, pos); value = stoi(line); emit progressSignal(value); } } } pclose2(fp, mw->downloading.pid); // ProgressBar to 100% and emit signal finished to main application emit progressSignal(100); emit finishedSignal(true); }