#include "downloadfile.h" #include "mainwindow.h" #include "ui_mainwindow.h" #include "tools.h" #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; downloadFile::downloadFile() { } void downloadFile::cancelled() { this->canceled = true; } void downloadFile::download(QString savePath, MainWindow *mw) { string server; string service; string path; string line; string errorRsync; char cmd[4096]; int portN; int pos; int pid; QTreeWidgetItem * item; stringstream output; // QString savePath = "."; vector v; int value; //char command_out[1024] = {0}; array buffer; server.assign(mw->ui->khistorycombobox->currentText().toStdString()); portN = mw->ui->portEdit->text().toInt(); service = mw->ui->listWidget->currentItem()->text().toStdString(); pos = service.find_first_of('\n'); service.resize(pos); item = mw->ui->treeWidget->currentItem(); path = item->text(0).toStdString(); while(item->parent() != NULL) { item = item->parent(); path = item->text(0).toStdString() + "/" + path; }; sprintf(cmd, "rsync --bwlimit=100K --port %i -P %s::\"%s/%s\" \"%s/\" 2>&1", portN, server.c_str(), service.c_str(), path.c_str(), savePath.toStdString().c_str()); //unique_ptr pipe(popen(cmd, "r"), pclose); FILE * fp = popen2(cmd, "r", pid); if (!fp) { throw runtime_error("popen2() failed!"); } while (fgets(buffer.data(), buffer.size(), fp) != nullptr) { buffer.data(); if (this->canceled == true) { return; } line = buffer.data(); //cout << line << endl; pos = line.find('%'); if (pos != -1) { line.erase(pos); pos = line.find_last_of(' '); if (pos != -1) { line.erase(0, pos); value = stoi(line); cout << value << endl; emit progressSignal(value); } } buffer.empty(); } //cout << path << endl; }