#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; // Initialization de la class downloadFile::downloadFile() { } //Slot activated when download is cancelled void downloadFile::cancelled(int pid) { if (pid == 0) { perror("Pid = 0 : I do not kill"); // Error rsync process not launched so it can't be killed }else if (kill(pid, SIGTERM) == -1) { //TODO managing error of kill } } // launch a rsync processus downloading a file void downloadFile::download(MainWindow *mw) { string line; string errorRsync; int pos; array argv; stringstream output; vector v; int value; char buffer[4096]; // Populating array with command and parameters for popen2 argv[0] = "/usr/bin/rsync"; if (mw->connexion.bandwidthLimit == 0) { argv[1] = "--bwlimit=1000P"; }else { argv[1] = "--bwlimit=" + to_string(mw->connexion.bandwidthLimit) + mw->connexion.bandwidthLimitUnit; } argv[2] = "--port=" + to_string(mw->connexion.port); argv[3] = "-P"; argv[4] = mw->downloading.server + "::" + mw->downloading.service + "/" + mw->downloading.path; argv[5] = mw->downloading.savePath + "/"; argv[6].clear(); //launching downloading thread FILE * fp = popen2(argv, "r", mw->downloading.pid); if (fp <= (FILE *) 0) { sprintf(buffer, "popen2() failed!: returning code:%d", fileno(fp)); throw runtime_error(buffer); return; } // waiting rsync output while (fgets(buffer, 4096, fp) != nullptr) { // Downloading is cancelled, we return if (this->canceled == true) { return; } line = buffer; // extracting percentage of completion pos = line.find('%'); if (pos != -1) { line.erase(pos); pos = line.find_last_of(' '); if (pos != -1) { line.erase(0, pos); value = stoi(line); // sending progress to Main window emit progressSignal(value); } } // download ended } pclose2(fp, mw->downloading.pid); // ProgressBar to 100% and emit signal finished to main application emit progressSignal(100); emit finishedSignal(true); }